Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Unified Diff: courgette/disassembler_win32_x64.cc

Issue 1792603006: Revert of [Courgette] Clean up Disassembler; fix ELF Memory leaks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « courgette/disassembler_win32_x64.h ('k') | courgette/disassembler_win32_x64_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: courgette/disassembler_win32_x64.cc
diff --git a/courgette/disassembler_win32_x64.cc b/courgette/disassembler_win32_x64.cc
index 819b7f20caea8a03ba9a35718408fb5f47d47344..74b0fe48a7fe7800abe9ffebcf1637daabc292db 100644
--- a/courgette/disassembler_win32_x64.cc
+++ b/courgette/disassembler_win32_x64.cc
@@ -8,73 +8,37 @@
#include <stdint.h>
#include <algorithm>
-#include <iostream>
+#include <string>
+#include <vector>
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
+
#include "courgette/assembly_program.h"
#include "courgette/courgette.h"
+#include "courgette/encoded_program.h"
namespace courgette {
DisassemblerWin32X64::DisassemblerWin32X64(const void* start, size_t length)
- : Disassembler(start, length),
- incomplete_disassembly_(false),
- is_PE32_plus_(false),
- optional_header_(nullptr),
- size_of_optional_header_(0),
- offset_of_data_directories_(0),
- machine_type_(0),
- number_of_sections_(0),
- sections_(nullptr),
- has_text_section_(false),
- size_of_code_(0),
- size_of_initialized_data_(0),
- size_of_uninitialized_data_(0),
- base_of_code_(0),
- base_of_data_(0),
- image_base_(0),
- size_of_image_(0),
- number_of_data_directories_(0) {
-}
-
-FileOffset DisassemblerWin32X64::RVAToFileOffset(RVA rva) const {
- const Section* section = RVAToSection(rva);
- if (section != nullptr) {
- FileOffset offset_in_section = rva - section->virtual_address;
- // Need this extra check, since an |rva| may be valid for a section, but is
- // non-existent in an image (e.g. uninit data).
- if (offset_in_section >= section->size_of_raw_data)
- return kNoFileOffset;
-
- return static_cast<FileOffset>(section->file_offset_of_raw_data +
- offset_in_section);
- }
-
- // Small RVA values point into the file header in the loaded image.
- // RVA 0 is the module load address which Windows uses as the module handle.
- // RVA 2 sometimes occurs, I'm not sure what it is, but it would map into the
- // DOS header.
- if (rva == 0 || rva == 2)
- return static_cast<FileOffset>(rva);
-
- NOTREACHED();
- return kNoFileOffset;
-}
-
-RVA DisassemblerWin32X64::FileOffsetToRVA(FileOffset file_offset) const {
- for (int i = 0; i < number_of_sections_; ++i) {
- const Section* section = &sections_[i];
- if (file_offset >= section->file_offset_of_raw_data) {
- FileOffset offset_in_section =
- file_offset - section->file_offset_of_raw_data;
- if (offset_in_section < section->size_of_raw_data)
- return static_cast<RVA>(section->virtual_address + offset_in_section);
- }
- }
-
- NOTREACHED();
- return kNoRVA;
+ : Disassembler(start, length),
+ incomplete_disassembly_(false),
+ is_PE32_plus_(false),
+ optional_header_(NULL),
+ size_of_optional_header_(0),
+ offset_of_data_directories_(0),
+ machine_type_(0),
+ number_of_sections_(0),
+ sections_(NULL),
+ has_text_section_(false),
+ size_of_code_(0),
+ size_of_initialized_data_(0),
+ size_of_uninitialized_data_(0),
+ base_of_code_(0),
+ base_of_data_(0),
+ image_base_(0),
+ size_of_image_(0),
+ number_of_data_directories_(0) {
}
// ParseHeader attempts to match up the buffer with the Windows data
@@ -93,19 +57,18 @@
return Bad("Not MZ");
// offset from DOS header to PE header is stored in DOS header.
- FileOffset file_offset = static_cast<FileOffset>(
- ReadU32(start(), kOffsetOfFileAddressOfNewExeHeader));
-
- if (file_offset >= length())
+ uint32_t offset = ReadU32(start(), kOffsetOfFileAddressOfNewExeHeader);
+
+ if (offset >= length())
return Bad("Bad offset to PE header");
- const uint8_t* const pe_header = FileOffsetToPointer(file_offset);
+ const uint8_t* const pe_header = OffsetToPointer(offset);
const size_t kMinPEHeaderSize = 4 /*signature*/ + kSizeOfCoffHeader;
if (pe_header <= start() ||
pe_header >= end() - kMinPEHeaderSize)
- return Bad("Bad file offset to PE header");
-
- if (file_offset % 8 != 0)
+ return Bad("Bad offset to PE header");
+
+ if (offset % 8 != 0)
return Bad("Misaligned PE header");
// The 'PE' header is an IMAGE_NT_HEADERS structure as defined in WINNT.H.
@@ -206,7 +169,7 @@
size_of_optional_header_);
size_t detected_length = 0;
- for (int i = 0; i < number_of_sections_; ++i) {
+ for (int i = 0; i < number_of_sections_; ++i) {
const Section* section = &sections_[i];
// TODO(sra): consider using the 'characteristics' field of the section
@@ -304,7 +267,7 @@
RVA rva = page_rva + offset;
// TODO(sebmarchand): Skip the relocs that live outside of the image. See
// the version of this function in disassembler_win32_x86.cc.
- if (type == 10) { // IMAGE_REL_BASED_DIR64
+ if (type == 10) { // IMAGE_REL_BASED_DIR64
relocs->push_back(rva);
} else if (type == 0) { // IMAGE_REL_BASED_ABSOLUTE
// Ignore, used as padding.
@@ -324,19 +287,48 @@
}
const Section* DisassemblerWin32X64::RVAToSection(RVA rva) const {
- for (int i = 0; i < number_of_sections_; ++i) {
+ for (int i = 0; i < number_of_sections_; i++) {
const Section* section = &sections_[i];
- if (rva >= section->virtual_address) {
- FileOffset offset_in_section = rva - section->virtual_address;
- if (offset_in_section < section->virtual_size)
- return section;
- }
- }
- return nullptr;
+ uint32_t offset = rva - section->virtual_address;
+ if (offset < section->virtual_size) {
+ return section;
+ }
+ }
+ return NULL;
+}
+
+int DisassemblerWin32X64::RVAToFileOffset(RVA rva) const {
+ const Section* section = RVAToSection(rva);
+ if (section) {
+ uint32_t offset = rva - section->virtual_address;
+ if (offset < section->size_of_raw_data) {
+ return section->file_offset_of_raw_data + offset;
+ } else {
+ return kNoOffset; // In section but not in file (e.g. uninit data).
+ }
+ }
+
+ // Small RVA values point into the file header in the loaded image.
+ // RVA 0 is the module load address which Windows uses as the module handle.
+ // RVA 2 sometimes occurs, I'm not sure what it is, but it would map into the
+ // DOS header.
+ if (rva == 0 || rva == 2)
+ return rva;
+
+ NOTREACHED();
+ return kNoOffset;
+}
+
+const uint8_t* DisassemblerWin32X64::RVAToPointer(RVA rva) const {
+ int file_offset = RVAToFileOffset(rva);
+ if (file_offset == kNoOffset)
+ return NULL;
+ else
+ return OffsetToPointer(file_offset);
}
std::string DisassemblerWin32X64::SectionName(const Section* section) {
- if (section == nullptr)
+ if (section == NULL)
return "<none>";
char name[9];
memcpy(name, section->name, 8);
@@ -346,25 +338,24 @@
CheckBool DisassemblerWin32X64::ParseFile(AssemblyProgram* program) {
// Walk all the bytes in the file, whether or not in a section.
- FileOffset file_offset = 0;
+ uint32_t file_offset = 0;
while (file_offset < length()) {
const Section* section = FindNextSection(file_offset);
- if (section == nullptr) {
- // No more sections. There should not be extra stuff following last
+ if (section == NULL) {
+ // No more sections. There should not be extra stuff following last
// section.
// ParseNonSectionFileRegion(file_offset, pe_info().length(), program);
break;
}
if (file_offset < section->file_offset_of_raw_data) {
- FileOffset section_start_offset = section->file_offset_of_raw_data;
- if (!ParseNonSectionFileRegion(file_offset, section_start_offset,
- program)) {
+ uint32_t section_start_offset = section->file_offset_of_raw_data;
+ if(!ParseNonSectionFileRegion(file_offset, section_start_offset,
+ program))
return false;
- }
file_offset = section_start_offset;
}
- FileOffset end = file_offset + section->size_of_raw_data;
+ uint32_t end = file_offset + section->size_of_raw_data;
if (!ParseFileRegion(section, file_offset, end, program))
return false;
file_offset = end;
@@ -384,7 +375,7 @@
return false;
#if COURGETTE_HISTOGRAM_TARGETS
- for (size_t i = 0; i < abs32_locations_.size(); ++i) {
+ for (size_t i = 0; i < abs32_locations_.size(); ++i) {
RVA rva = abs32_locations_[i];
// The 4 bytes at the relocation are a reference to some address.
uint32_t target_address = Read32LittleEndian(RVAToPointer(rva));
@@ -395,10 +386,10 @@
}
void DisassemblerWin32X64::ParseRel32RelocsFromSections() {
- FileOffset file_offset = 0;
+ uint32_t file_offset = 0;
while (file_offset < length()) {
const Section* section = FindNextSection(file_offset);
- if (section == nullptr)
+ if (section == NULL)
break;
if (file_offset < section->file_offset_of_raw_data)
file_offset = section->file_offset_of_raw_data;
@@ -420,11 +411,11 @@
std::map<RVA, int>::iterator rel32_iter = rel32_target_rvas_.begin();
while (abs32_iter != abs32_target_rvas_.end() &&
rel32_iter != rel32_target_rvas_.end()) {
- if (abs32_iter->first < rel32_iter->first) {
+ if (abs32_iter->first < rel32_iter->first)
++abs32_iter;
- } else if (rel32_iter->first < abs32_iter->first) {
+ else if (rel32_iter->first < abs32_iter->first)
++rel32_iter;
- } else {
+ else {
++common;
++abs32_iter;
++rel32_iter;
@@ -440,18 +431,18 @@
if (!isCode)
return;
- FileOffset start_file_offset = section->file_offset_of_raw_data;
- FileOffset end_file_offset = start_file_offset + section->size_of_raw_data;
+ uint32_t start_file_offset = section->file_offset_of_raw_data;
+ uint32_t end_file_offset = start_file_offset + section->size_of_raw_data;
RVA relocs_start_rva = base_relocation_table().address_;
- const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
- const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
+ const uint8_t* start_pointer = OffsetToPointer(start_file_offset);
+ const uint8_t* end_pointer = OffsetToPointer(end_file_offset);
RVA start_rva = FileOffsetToRVA(start_file_offset);
RVA end_rva = start_rva + section->virtual_size;
// Quick way to convert from Pointer to RVA within a single Section is to
- // subtract |pointer_to_rva|.
+ // subtract 'pointer_to_rva'.
const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva;
std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin();
@@ -468,10 +459,13 @@
}
}
+ //while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva)
+ // ++abs32_pos;
+
// Heuristic discovery of rel32 locations in instruction stream: are the
// next few bytes the start of an instruction containing a rel32
// addressing mode?
- const uint8_t* rel32 = nullptr;
+ const uint8_t* rel32 = NULL;
bool is_rip_relative = false;
if (p + 5 <= end_pointer) {
@@ -522,7 +516,7 @@
RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32);
// To be valid, rel32 target must be within image, and within this
// section.
- if (target_rva < size_of_image_ && // Subsumes rva != kUnassignedRVA.
+ if (IsValidRVA(target_rva) &&
(is_rip_relative ||
(start_rva <= target_rva && target_rva < end_rva))) {
rel32_locations_.push_back(rel32_rva);
@@ -538,14 +532,14 @@
}
CheckBool DisassemblerWin32X64::ParseNonSectionFileRegion(
- FileOffset start_file_offset,
- FileOffset end_file_offset,
+ uint32_t start_file_offset,
+ uint32_t end_file_offset,
AssemblyProgram* program) {
if (incomplete_disassembly_)
return true;
if (end_file_offset > start_file_offset) {
- if (!program->EmitBytesInstruction(FileOffsetToPointer(start_file_offset),
+ if (!program->EmitBytesInstruction(OffsetToPointer(start_file_offset),
end_file_offset - start_file_offset)) {
return false;
}
@@ -555,13 +549,13 @@
}
CheckBool DisassemblerWin32X64::ParseFileRegion(const Section* section,
- FileOffset start_file_offset,
- FileOffset end_file_offset,
+ uint32_t start_file_offset,
+ uint32_t end_file_offset,
AssemblyProgram* program) {
RVA relocs_start_rva = base_relocation_table().address_;
- const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
- const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
+ const uint8_t* start_pointer = OffsetToPointer(start_file_offset);
+ const uint8_t* end_pointer = OffsetToPointer(end_file_offset);
RVA start_rva = FileOffsetToRVA(start_file_offset);
RVA end_rva = start_rva + section->virtual_size;
@@ -670,7 +664,7 @@
size_t count = p->second.size();
std::cout << std::dec << p->first << ": " << count;
if (count <= 2) {
- for (size_t i = 0; i < count; ++i)
+ for (size_t i = 0; i < count; ++i)
std::cout << " " << DescribeRVA(p->second[i]);
}
std::cout << std::endl;
@@ -681,6 +675,7 @@
}
}
#endif // COURGETTE_HISTOGRAM_TARGETS
+
// DescribeRVA is for debugging only. I would put it under #ifdef DEBUG except
// that during development I'm finding I need to call it when compiled in
@@ -700,12 +695,12 @@
}
const Section* DisassemblerWin32X64::FindNextSection(
- FileOffset file_offset) const {
+ uint32_t fileOffset) const {
const Section* best = 0;
- for (int i = 0; i < number_of_sections_; ++i) {
+ for (int i = 0; i < number_of_sections_; i++) {
const Section* section = &sections_[i];
if (section->size_of_raw_data > 0) { // i.e. has data in file.
- if (file_offset <= section->file_offset_of_raw_data) {
+ if (fileOffset <= section->file_offset_of_raw_data) {
if (best == 0 ||
section->file_offset_of_raw_data < best->file_offset_of_raw_data) {
best = section;
@@ -716,15 +711,26 @@
return best;
}
+RVA DisassemblerWin32X64::FileOffsetToRVA(uint32_t file_offset) const {
+ for (int i = 0; i < number_of_sections_; i++) {
+ const Section* section = &sections_[i];
+ uint32_t offset = file_offset - section->file_offset_of_raw_data;
+ if (offset < section->size_of_raw_data) {
+ return section->virtual_address + offset;
+ }
+ }
+ return 0;
+}
+
bool DisassemblerWin32X64::ReadDataDirectory(
int index,
ImageDataDirectory* directory) {
if (index < number_of_data_directories_) {
- FileOffset file_offset = index * 8 + offset_of_data_directories_;
- if (file_offset >= size_of_optional_header_)
+ size_t offset = index * 8 + offset_of_data_directories_;
+ if (offset >= size_of_optional_header_)
return Bad("number of data directories inconsistent");
- const uint8_t* data_directory = optional_header_ + file_offset;
+ const uint8_t* data_directory = optional_header_ + offset;
if (data_directory < start() ||
data_directory + 8 >= end())
return Bad("data directory outside image");
« no previous file with comments | « courgette/disassembler_win32_x64.h ('k') | courgette/disassembler_win32_x64_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698