Skip to content

Commit

Permalink
Revert of [Courgette] Clean up Disassembler; fix ELF Memory leaks. (p…
Browse files Browse the repository at this point in the history
…atchset crosswalk-project#15 id:270001 of https://codereview.chromium.org/1676683002/ )

Reason for revert:
Regressed linux sizes (iostream maybe?)

https://build.chromium.org/p/chromium/builders/Linux/builds/72899/steps/sizes/logs/stdio

Original issue's description:
> [Courgette] Clean up Disassembler; fix ELF Memory leaks.
>
> Cleaning up code surrounding Disassembler:
> - Extract AddressTranslator interface to be used across subclasses.
> - Use FileOffset = size_t by context.
> - Detailed comments & TODOs in DisassemblerElf32ARM.
> - Fix DisassemblerElf32ARM memory leaks.
> - Lots of superficial stylistic changes.
>
> Except for AddressTranslator routines and unit tests, shying away
> from control flow and logic changes.
>
> BUG=579206
>
> Committed: https://crrev.com/58b822d441f5c982e879e536fa3c1cbac8fd339a
> Cr-Commit-Position: refs/heads/master@{#380881}

[email protected],[email protected],[email protected],[email protected],[email protected]
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=579206

Review URL: https://codereview.chromium.org/1792603006

Cr-Commit-Position: refs/heads/master@{#380885}
  • Loading branch information
sgraham authored and Commit bot committed Mar 12, 2016
1 parent 2eb2d38 commit 4a95ca5
Show file tree
Hide file tree
Showing 19 changed files with 880 additions and 882 deletions.
20 changes: 5 additions & 15 deletions courgette/disassembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,24 @@

#include "courgette/disassembler.h"

#include "base/logging.h"

namespace courgette {

Disassembler::Disassembler(const void* start, size_t length)
: failure_reason_("uninitialized") {
: failure_reason_("uninitialized") {
start_ = reinterpret_cast<const uint8_t*>(start);
length_ = length;
end_ = start_ + length_;
};

Disassembler::~Disassembler() {};

const uint8_t* Disassembler::FileOffsetToPointer(FileOffset file_offset) const {
CHECK_LE(file_offset, static_cast<FileOffset>(end_ - start_));
return start_ + file_offset;
}

const uint8_t* Disassembler::RVAToPointer(RVA rva) const {
FileOffset file_offset = RVAToFileOffset(rva);
if (file_offset == kNoFileOffset)
return nullptr;

return FileOffsetToPointer(file_offset);
const uint8_t* Disassembler::OffsetToPointer(size_t offset) const {
assert(start_ + offset <= end_);
return start_ + offset;
}

bool Disassembler::Good() {
failure_reason_ = nullptr;
failure_reason_ = NULL;
return true;
}

Expand Down
34 changes: 16 additions & 18 deletions courgette/disassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,33 @@ namespace courgette {

class AssemblyProgram;

class Disassembler : public AddressTranslator {
class Disassembler {
public:
virtual ~Disassembler();

// AddressTranslator interfaces.
virtual RVA FileOffsetToRVA(FileOffset file_offset) const override = 0;
virtual FileOffset RVAToFileOffset(RVA rva) const override = 0;
const uint8_t* FileOffsetToPointer(FileOffset file_offset) const override;
const uint8_t* RVAToPointer(RVA rva) const override;
virtual ExecutableType kind() { return EXE_UNKNOWN; }

virtual ExecutableType kind() const = 0;
// ok() may always be called but returns 'true' only after ParseHeader
// succeeds.
bool ok() const { return failure_reason_ == NULL; }

// Returns true if the buffer appears to be a valid executable of the expected
// type, and false otherwise. This needs not be called before Disassemble().
// Returns 'true' if the buffer appears to be a valid executable of the
// expected type. It is not required that this be called before Disassemble.
virtual bool ParseHeader() = 0;

// Disassembles the item passed to the factory method into the output
// parameter 'program'.
virtual bool Disassemble(AssemblyProgram* program) = 0;

// ok() may always be called but returns true only after ParseHeader()
// succeeds.
bool ok() const { return failure_reason_ == nullptr; }

// Returns the length of the image. May reduce after ParseHeader().
// Returns the length of the source executable. May reduce after ParseHeader.
size_t length() const { return length_; }
const uint8_t* start() const { return start_; }
const uint8_t* end() const { return end_; }

// Returns a pointer into the memory copy of the file format.
// FileOffsetToPointer(0) returns a pointer to the start of the file format.
const uint8_t* OffsetToPointer(size_t offset) const;

protected:
Disassembler(const void* start, size_t length);

Expand All @@ -57,16 +55,16 @@ class Disassembler : public AddressTranslator {
}

// Reduce the length of the image in memory. Does not actually free
// (or realloc) any memory. Usually only called via ParseHeader().
// (or realloc) any memory. Usually only called via ParseHeader()
void ReduceLength(size_t reduced_length);

private:
const char* failure_reason_;

//
// Basic information that is always valid after construction, although
// ParseHeader() may shorten |length_| if the executable is shorter than the
// total data.
// Basic information that is always valid after Construction, though
// ParseHeader may shorten the length if the executable is shorter than
// the total data.
//
size_t length_; // In current memory.
const uint8_t* start_; // In current memory, base for 'file offsets'.
Expand Down
Loading

0 comments on commit 4a95ca5

Please sign in to comment.