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