OLD | NEW |
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 // This is the transformation and adjustment for all executables. | 5 // This is the transformation and adjustment for all executables. |
6 // The executable type is determined by ParseDetectedExecutable function. | 6 // The executable type is determined by ParseDetectedExecutable function. |
7 | 7 |
8 #ifndef COURGETTE_WIN32_X86_GENERATOR_H_ | 8 #ifndef COURGETTE_WIN32_X86_GENERATOR_H_ |
9 #define COURGETTE_WIN32_X86_GENERATOR_H_ | 9 #define COURGETTE_WIN32_X86_GENERATOR_H_ |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "courgette/assembly_program.h" | 14 #include "courgette/assembly_program.h" |
15 #include "courgette/ensemble.h" | 15 #include "courgette/ensemble.h" |
16 #include "courgette/program_detector.h" | |
17 | 16 |
18 namespace courgette { | 17 namespace courgette { |
19 | 18 |
20 class PatchGeneratorX86_32 : public TransformationPatchGenerator { | 19 class PatchGeneratorX86_32 : public TransformationPatchGenerator { |
21 public: | 20 public: |
22 PatchGeneratorX86_32(Element* old_element, | 21 PatchGeneratorX86_32(Element* old_element, |
23 Element* new_element, | 22 Element* new_element, |
24 PatcherX86_32* patcher, | 23 PatcherX86_32* patcher, |
25 ExecutableType kind) | 24 ExecutableType kind) |
26 : TransformationPatchGenerator(old_element, new_element, patcher), | 25 : TransformationPatchGenerator(old_element, new_element, patcher), |
(...skipping 28 matching lines...) Expand all Loading... |
55 // serializing them. | 54 // serializing them. |
56 Status Transform(SourceStreamSet* corrected_parameters, | 55 Status Transform(SourceStreamSet* corrected_parameters, |
57 SinkStreamSet* old_transformed_element, | 56 SinkStreamSet* old_transformed_element, |
58 SinkStreamSet* new_transformed_element) { | 57 SinkStreamSet* new_transformed_element) { |
59 // Don't expect any corrected parameters. | 58 // Don't expect any corrected parameters. |
60 if (!corrected_parameters->Empty()) | 59 if (!corrected_parameters->Empty()) |
61 return C_GENERAL_ERROR; | 60 return C_GENERAL_ERROR; |
62 | 61 |
63 // Generate old version of program using |corrected_parameters|. | 62 // Generate old version of program using |corrected_parameters|. |
64 // TODO(sra): refactor to use same code from patcher_. | 63 // TODO(sra): refactor to use same code from patcher_. |
65 scoped_ptr<AssemblyProgram> old_program; | 64 AssemblyProgram* old_program = NULL; |
66 Status old_parse_status = | 65 Status old_parse_status = |
67 ParseDetectedExecutable(old_element_->region().start(), | 66 ParseDetectedExecutable(old_element_->region().start(), |
68 old_element_->region().length(), | 67 old_element_->region().length(), |
69 &old_program); | 68 &old_program); |
70 if (old_parse_status != C_OK) { | 69 if (old_parse_status != C_OK) { |
71 LOG(ERROR) << "Cannot parse an executable " << old_element_->Name(); | 70 LOG(ERROR) << "Cannot parse an executable " << old_element_->Name(); |
72 return old_parse_status; | 71 return old_parse_status; |
73 } | 72 } |
74 | 73 |
75 // TODO(huangs): Move the block below to right before |new_program| gets | 74 AssemblyProgram* new_program = NULL; |
76 // used, so we can reduce Courgette-gen peak memory. | |
77 scoped_ptr<AssemblyProgram> new_program; | |
78 Status new_parse_status = | 75 Status new_parse_status = |
79 ParseDetectedExecutable(new_element_->region().start(), | 76 ParseDetectedExecutable(new_element_->region().start(), |
80 new_element_->region().length(), | 77 new_element_->region().length(), |
81 &new_program); | 78 &new_program); |
82 if (new_parse_status != C_OK) { | 79 if (new_parse_status != C_OK) { |
| 80 DeleteAssemblyProgram(old_program); |
83 LOG(ERROR) << "Cannot parse an executable " << new_element_->Name(); | 81 LOG(ERROR) << "Cannot parse an executable " << new_element_->Name(); |
84 return new_parse_status; | 82 return new_parse_status; |
85 } | 83 } |
86 | 84 |
87 scoped_ptr<EncodedProgram> old_encoded; | 85 EncodedProgram* old_encoded = NULL; |
88 Status old_encode_status = Encode(*old_program, &old_encoded); | 86 Status old_encode_status = Encode(old_program, &old_encoded); |
89 if (old_encode_status != C_OK) | 87 if (old_encode_status != C_OK) { |
| 88 DeleteAssemblyProgram(old_program); |
90 return old_encode_status; | 89 return old_encode_status; |
| 90 } |
91 | 91 |
92 Status old_write_status = | 92 Status old_write_status = |
93 WriteEncodedProgram(old_encoded.get(), old_transformed_element); | 93 WriteEncodedProgram(old_encoded, old_transformed_element); |
| 94 DeleteEncodedProgram(old_encoded); |
| 95 if (old_write_status != C_OK) { |
| 96 DeleteAssemblyProgram(old_program); |
| 97 return old_write_status; |
| 98 } |
94 | 99 |
95 old_encoded.reset(); | 100 Status adjust_status = Adjust(*old_program, new_program); |
| 101 DeleteAssemblyProgram(old_program); |
| 102 if (adjust_status != C_OK) { |
| 103 DeleteAssemblyProgram(new_program); |
| 104 return adjust_status; |
| 105 } |
96 | 106 |
97 if (old_write_status != C_OK) | 107 EncodedProgram* new_encoded = NULL; |
98 return old_write_status; | 108 Status new_encode_status = Encode(new_program, &new_encoded); |
99 | 109 DeleteAssemblyProgram(new_program); |
100 Status adjust_status = Adjust(*old_program, new_program.get()); | |
101 old_program.reset(); | |
102 if (adjust_status != C_OK) | |
103 return adjust_status; | |
104 | |
105 scoped_ptr<EncodedProgram> new_encoded; | |
106 Status new_encode_status = Encode(*new_program, &new_encoded); | |
107 if (new_encode_status != C_OK) | 110 if (new_encode_status != C_OK) |
108 return new_encode_status; | 111 return new_encode_status; |
109 | 112 |
110 new_program.reset(); | 113 Status new_write_status = |
| 114 WriteEncodedProgram(new_encoded, new_transformed_element); |
| 115 DeleteEncodedProgram(new_encoded); |
| 116 if (new_write_status != C_OK) |
| 117 return new_write_status; |
111 | 118 |
112 Status new_write_status = | 119 return C_OK; |
113 WriteEncodedProgram(new_encoded.get(), new_transformed_element); | |
114 return new_write_status; | |
115 } | 120 } |
116 | 121 |
117 Status Reform(SourceStreamSet* transformed_element, | 122 Status Reform(SourceStreamSet* transformed_element, |
118 SinkStream* reformed_element) { | 123 SinkStream* reformed_element) { |
119 return TransformationPatchGenerator::Reform(transformed_element, | 124 return TransformationPatchGenerator::Reform(transformed_element, |
120 reformed_element); | 125 reformed_element); |
121 } | 126 } |
122 | 127 |
123 private: | 128 private: |
124 virtual ~PatchGeneratorX86_32() { } | 129 virtual ~PatchGeneratorX86_32() { } |
125 | 130 |
126 ExecutableType kind_; | 131 ExecutableType kind_; |
127 | 132 |
128 DISALLOW_COPY_AND_ASSIGN(PatchGeneratorX86_32); | 133 DISALLOW_COPY_AND_ASSIGN(PatchGeneratorX86_32); |
129 }; | 134 }; |
130 | 135 |
131 } // namespace courgette | 136 } // namespace courgette |
132 | |
133 #endif // COURGETTE_WIN32_X86_GENERATOR_H_ | 137 #endif // COURGETTE_WIN32_X86_GENERATOR_H_ |
OLD | NEW |