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

Side by Side Diff: courgette/disassembler.h

Issue 1676683002: [Courgette] Clean up Disassembler; fix ELF Memory leaks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Wrap #include <iostream> under #if COURGETTE_HISTOGRAM_TARGETS. 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 unified diff | Download patch
« no previous file with comments | « no previous file | courgette/disassembler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COURGETTE_DISASSEMBLER_H_ 5 #ifndef COURGETTE_DISASSEMBLER_H_
6 #define COURGETTE_DISASSEMBLER_H_ 6 #define COURGETTE_DISASSEMBLER_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "courgette/courgette.h" 12 #include "courgette/courgette.h"
13 #include "courgette/image_utils.h" 13 #include "courgette/image_utils.h"
14 14
15 namespace courgette { 15 namespace courgette {
16 16
17 class AssemblyProgram; 17 class AssemblyProgram;
18 18
19 class Disassembler { 19 class Disassembler : public AddressTranslator {
20 public: 20 public:
21 virtual ~Disassembler(); 21 virtual ~Disassembler();
22 22
23 virtual ExecutableType kind() { return EXE_UNKNOWN; } 23 // AddressTranslator interfaces.
24 virtual RVA FileOffsetToRVA(FileOffset file_offset) const override = 0;
25 virtual FileOffset RVAToFileOffset(RVA rva) const override = 0;
26 const uint8_t* FileOffsetToPointer(FileOffset file_offset) const override;
27 const uint8_t* RVAToPointer(RVA rva) const override;
24 28
25 // ok() may always be called but returns 'true' only after ParseHeader 29 virtual ExecutableType kind() const = 0;
26 // succeeds.
27 bool ok() const { return failure_reason_ == NULL; }
28 30
29 // Returns 'true' if the buffer appears to be a valid executable of the 31 // Returns true if the buffer appears to be a valid executable of the expected
30 // expected type. It is not required that this be called before Disassemble. 32 // type, and false otherwise. This needs not be called before Disassemble().
31 virtual bool ParseHeader() = 0; 33 virtual bool ParseHeader() = 0;
32 34
33 // Disassembles the item passed to the factory method into the output 35 // Disassembles the item passed to the factory method into the output
34 // parameter 'program'. 36 // parameter 'program'.
35 virtual bool Disassemble(AssemblyProgram* program) = 0; 37 virtual bool Disassemble(AssemblyProgram* program) = 0;
36 38
37 // Returns the length of the source executable. May reduce after ParseHeader. 39 // ok() may always be called but returns true only after ParseHeader()
40 // succeeds.
41 bool ok() const { return failure_reason_ == nullptr; }
42
43 // Returns the length of the image. May reduce after ParseHeader().
38 size_t length() const { return length_; } 44 size_t length() const { return length_; }
39 const uint8_t* start() const { return start_; } 45 const uint8_t* start() const { return start_; }
40 const uint8_t* end() const { return end_; } 46 const uint8_t* end() const { return end_; }
41 47
42 // Returns a pointer into the memory copy of the file format.
43 // FileOffsetToPointer(0) returns a pointer to the start of the file format.
44 const uint8_t* OffsetToPointer(size_t offset) const;
45
46 protected: 48 protected:
47 Disassembler(const void* start, size_t length); 49 Disassembler(const void* start, size_t length);
48 50
49 bool Good(); 51 bool Good();
50 bool Bad(const char *reason); 52 bool Bad(const char *reason);
51 53
52 // Returns true if the array lies within our memory region. 54 // Returns true if the array lies within our memory region.
53 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) { 55 bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) {
54 return offset <= length() && elements <= (length() - offset) / element_size; 56 return offset <= length() && elements <= (length() - offset) / element_size;
55 } 57 }
56 58
57 // Reduce the length of the image in memory. Does not actually free 59 // Reduce the length of the image in memory. Does not actually free
58 // (or realloc) any memory. Usually only called via ParseHeader() 60 // (or realloc) any memory. Usually only called via ParseHeader().
59 void ReduceLength(size_t reduced_length); 61 void ReduceLength(size_t reduced_length);
60 62
61 private: 63 private:
62 const char* failure_reason_; 64 const char* failure_reason_;
63 65
64 // 66 //
65 // Basic information that is always valid after Construction, though 67 // Basic information that is always valid after construction, although
66 // ParseHeader may shorten the length if the executable is shorter than 68 // ParseHeader() may shorten |length_| if the executable is shorter than the
67 // the total data. 69 // total data.
68 // 70 //
69 size_t length_; // In current memory. 71 size_t length_; // In current memory.
70 const uint8_t* start_; // In current memory, base for 'file offsets'. 72 const uint8_t* start_; // In current memory, base for 'file offsets'.
71 const uint8_t* end_; // In current memory. 73 const uint8_t* end_; // In current memory.
72 74
73 DISALLOW_COPY_AND_ASSIGN(Disassembler); 75 DISALLOW_COPY_AND_ASSIGN(Disassembler);
74 }; 76 };
75 77
76 } // namespace courgette 78 } // namespace courgette
77 79
78 #endif // COURGETTE_DISASSEMBLER_H_ 80 #endif // COURGETTE_DISASSEMBLER_H_
OLDNEW
« no previous file with comments | « no previous file | courgette/disassembler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698