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