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

Side by Side Diff: courgette/disassembler.cc

Issue 6677141: Switch out use of std::string and std::vector for large allocations for a buffer class that doesn... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 #include "courgette/disassembler.h" 5 #include "courgette/disassembler.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 18 matching lines...) Expand all
29 incomplete_disassembly_(false) { 29 incomplete_disassembly_(false) {
30 } 30 }
31 31
32 virtual bool Disassemble(AssemblyProgram* target); 32 virtual bool Disassemble(AssemblyProgram* target);
33 33
34 virtual void Destroy() { delete this; } 34 virtual void Destroy() { delete this; }
35 35
36 protected: 36 protected:
37 PEInfo& pe_info() { return *pe_info_; } 37 PEInfo& pe_info() { return *pe_info_; }
38 38
39 void ParseFile(AssemblyProgram* target); 39 CheckBool ParseFile(AssemblyProgram* target);
40 bool ParseAbs32Relocs(); 40 bool ParseAbs32Relocs();
41 void ParseRel32RelocsFromSections(); 41 void ParseRel32RelocsFromSections();
42 void ParseRel32RelocsFromSection(const Section* section); 42 void ParseRel32RelocsFromSection(const Section* section);
43 43
44 void ParseNonSectionFileRegion(uint32 start_file_offset, 44 CheckBool ParseNonSectionFileRegion(uint32 start_file_offset,
45 uint32 end_file_offset, 45 uint32 end_file_offset,
46 AssemblyProgram* program); 46 AssemblyProgram* program);
47 void ParseFileRegion(const Section* section, 47 CheckBool ParseFileRegion(const Section* section,
48 uint32 start_file_offset, uint32 end_file_offset, 48 uint32 start_file_offset, uint32 end_file_offset,
49 AssemblyProgram* program); 49 AssemblyProgram* program);
50 50
51 #if COURGETTE_HISTOGRAM_TARGETS 51 #if COURGETTE_HISTOGRAM_TARGETS
52 void HistogramTargets(const char* kind, const std::map<RVA, int>& map); 52 void HistogramTargets(const char* kind, const std::map<RVA, int>& map);
53 #endif 53 #endif
54 54
55 PEInfo* pe_info_; 55 PEInfo* pe_info_;
56 bool incomplete_disassembly_; // 'true' if can leave out 'uninteresting' bits 56 bool incomplete_disassembly_; // 'true' if can leave out 'uninteresting' bits
57 57
58 std::vector<RVA> abs32_locations_; 58 std::vector<RVA> abs32_locations_;
59 std::vector<RVA> rel32_locations_; 59 std::vector<RVA> rel32_locations_;
60 60
61 #if COURGETTE_HISTOGRAM_TARGETS 61 #if COURGETTE_HISTOGRAM_TARGETS
62 std::map<RVA, int> abs32_target_rvas_; 62 std::map<RVA, int> abs32_target_rvas_;
63 std::map<RVA, int> rel32_target_rvas_; 63 std::map<RVA, int> rel32_target_rvas_;
64 #endif 64 #endif
65 }; 65 };
66 66
67 bool DisassemblerWin32X86::Disassemble(AssemblyProgram* target) { 67 bool DisassemblerWin32X86::Disassemble(AssemblyProgram* target) {
68 if (!pe_info().ok()) 68 if (!pe_info().ok())
69 return false; 69 return false;
70 70
71 target->set_image_base(pe_info().image_base()); 71 target->set_image_base(pe_info().image_base());
72 72
73 if (!ParseAbs32Relocs()) 73 if (!ParseAbs32Relocs())
74 return false; 74 return false;
75 75
76 ParseRel32RelocsFromSections(); 76 ParseRel32RelocsFromSections();
77 77
78 ParseFile(target); 78 if (!ParseFile(target))
79 return false;
79 80
80 target->DefaultAssignIndexes(); 81 target->DefaultAssignIndexes();
82
81 return true; 83 return true;
82 } 84 }
83 85
84 static uint32 Read32LittleEndian(const void* address) { 86 static uint32 Read32LittleEndian(const void* address) {
85 return *reinterpret_cast<const uint32*>(address); 87 return *reinterpret_cast<const uint32*>(address);
86 } 88 }
87 89
88 bool DisassemblerWin32X86::ParseAbs32Relocs() { 90 bool DisassemblerWin32X86::ParseAbs32Relocs() {
89 abs32_locations_.clear(); 91 abs32_locations_.clear();
90 if (!pe_info().ParseRelocs(&abs32_locations_)) 92 if (!pe_info().ParseRelocs(&abs32_locations_))
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 ++rel32_target_rvas_[target_rva]; 223 ++rel32_target_rvas_[target_rva];
222 #endif 224 #endif
223 p += 4; 225 p += 4;
224 continue; 226 continue;
225 } 227 }
226 } 228 }
227 p += 1; 229 p += 1;
228 } 230 }
229 } 231 }
230 232
231 void DisassemblerWin32X86::ParseFile(AssemblyProgram* program) { 233 CheckBool DisassemblerWin32X86::ParseFile(AssemblyProgram* program) {
234 bool ok = true;
232 // Walk all the bytes in the file, whether or not in a section. 235 // Walk all the bytes in the file, whether or not in a section.
233 uint32 file_offset = 0; 236 uint32 file_offset = 0;
234 while (file_offset < pe_info().length()) { 237 while (ok && file_offset < pe_info().length()) {
235 const Section* section = pe_info().FindNextSection(file_offset); 238 const Section* section = pe_info().FindNextSection(file_offset);
236 if (section == NULL) { 239 if (section == NULL) {
237 // No more sections. There should not be extra stuff following last 240 // No more sections. There should not be extra stuff following last
238 // section. 241 // section.
239 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program); 242 // ParseNonSectionFileRegion(file_offset, pe_info().length(), program);
240 break; 243 break;
241 } 244 }
242 if (file_offset < section->file_offset_of_raw_data) { 245 if (file_offset < section->file_offset_of_raw_data) {
243 uint32 section_start_offset = section->file_offset_of_raw_data; 246 uint32 section_start_offset = section->file_offset_of_raw_data;
244 ParseNonSectionFileRegion(file_offset, section_start_offset, program); 247 ok = ParseNonSectionFileRegion(file_offset, section_start_offset,
248 program);
245 file_offset = section_start_offset; 249 file_offset = section_start_offset;
246 } 250 }
247 uint32 end = file_offset + section->size_of_raw_data; 251 if (ok) {
248 ParseFileRegion(section, file_offset, end, program); 252 uint32 end = file_offset + section->size_of_raw_data;
249 file_offset = end; 253 ok = ParseFileRegion(section, file_offset, end, program);
254 file_offset = end;
255 }
250 } 256 }
251 257
252 #if COURGETTE_HISTOGRAM_TARGETS 258 #if COURGETTE_HISTOGRAM_TARGETS
253 HistogramTargets("abs32 relocs", abs32_target_rvas_); 259 HistogramTargets("abs32 relocs", abs32_target_rvas_);
254 HistogramTargets("rel32 relocs", rel32_target_rvas_); 260 HistogramTargets("rel32 relocs", rel32_target_rvas_);
255 #endif 261 #endif
262
263 return ok;
256 } 264 }
257 265
258 void DisassemblerWin32X86::ParseNonSectionFileRegion( 266 CheckBool DisassemblerWin32X86::ParseNonSectionFileRegion(
259 uint32 start_file_offset, 267 uint32 start_file_offset,
260 uint32 end_file_offset, 268 uint32 end_file_offset,
261 AssemblyProgram* program) { 269 AssemblyProgram* program) {
262 if (incomplete_disassembly_) 270 if (incomplete_disassembly_)
263 return; 271 return true;
264 272
265 const uint8* start = pe_info().FileOffsetToPointer(start_file_offset); 273 const uint8* start = pe_info().FileOffsetToPointer(start_file_offset);
266 const uint8* end = pe_info().FileOffsetToPointer(end_file_offset); 274 const uint8* end = pe_info().FileOffsetToPointer(end_file_offset);
267 275
268 const uint8* p = start; 276 const uint8* p = start;
269 277
270 while (p < end) { 278 bool ok = true;
271 program->EmitByteInstruction(*p); 279 while (p < end && ok) {
280 ok = program->EmitByteInstruction(*p);
272 ++p; 281 ++p;
273 } 282 }
283
284 return ok;
274 } 285 }
275 286
276 void DisassemblerWin32X86::ParseFileRegion( 287 CheckBool DisassemblerWin32X86::ParseFileRegion(
277 const Section* section, 288 const Section* section,
278 uint32 start_file_offset, uint32 end_file_offset, 289 uint32 start_file_offset, uint32 end_file_offset,
279 AssemblyProgram* program) { 290 AssemblyProgram* program) {
280 RVA relocs_start_rva = pe_info().base_relocation_table().address_; 291 RVA relocs_start_rva = pe_info().base_relocation_table().address_;
281 292
282 const uint8* start_pointer = pe_info().FileOffsetToPointer(start_file_offset); 293 const uint8* start_pointer = pe_info().FileOffsetToPointer(start_file_offset);
283 const uint8* end_pointer = pe_info().FileOffsetToPointer(end_file_offset); 294 const uint8* end_pointer = pe_info().FileOffsetToPointer(end_file_offset);
284 295
285 RVA start_rva = pe_info().FileOffsetToRVA(start_file_offset); 296 RVA start_rva = pe_info().FileOffsetToRVA(start_file_offset);
286 RVA end_rva = start_rva + section->virtual_size; 297 RVA end_rva = start_rva + section->virtual_size;
287 298
288 // Quick way to convert from Pointer to RVA within a single Section is to 299 // Quick way to convert from Pointer to RVA within a single Section is to
289 // subtract 'pointer_to_rva'. 300 // subtract 'pointer_to_rva'.
290 const uint8* const adjust_pointer_to_rva = start_pointer - start_rva; 301 const uint8* const adjust_pointer_to_rva = start_pointer - start_rva;
291 302
292 std::vector<RVA>::iterator rel32_pos = rel32_locations_.begin(); 303 std::vector<RVA>::iterator rel32_pos = rel32_locations_.begin();
293 std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin(); 304 std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin();
294 305
295 program->EmitOriginInstruction(start_rva); 306 bool ok = program->EmitOriginInstruction(start_rva);
296 307
297 const uint8* p = start_pointer; 308 const uint8* p = start_pointer;
298 309
299 while (p < end_pointer) { 310 while (ok && p < end_pointer) {
300 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); 311 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva);
301 312
302 // The base relocation table is usually in the .relocs section, but it could 313 // The base relocation table is usually in the .relocs section, but it could
303 // actually be anywhere. Make sure we skip it because we will regenerate it 314 // actually be anywhere. Make sure we skip it because we will regenerate it
304 // during assembly. 315 // during assembly.
305 if (current_rva == relocs_start_rva) { 316 if (current_rva == relocs_start_rva) {
306 program->EmitMakeRelocsInstruction(); 317 ok = program->EmitMakeRelocsInstruction();
318 if (!ok)
319 break;
307 uint32 relocs_size = pe_info().base_relocation_table().size_; 320 uint32 relocs_size = pe_info().base_relocation_table().size_;
308 if (relocs_size) { 321 if (relocs_size) {
309 p += relocs_size; 322 p += relocs_size;
310 continue; 323 continue;
311 } 324 }
312 } 325 }
313 326
314 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva) 327 while (abs32_pos != abs32_locations_.end() && *abs32_pos < current_rva)
315 ++abs32_pos; 328 ++abs32_pos;
316 329
317 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) { 330 if (abs32_pos != abs32_locations_.end() && *abs32_pos == current_rva) {
318 uint32 target_address = Read32LittleEndian(p); 331 uint32 target_address = Read32LittleEndian(p);
319 RVA target_rva = target_address - pe_info().image_base(); 332 RVA target_rva = target_address - pe_info().image_base();
320 // TODO(sra): target could be Label+offset. It is not clear how to guess 333 // TODO(sra): target could be Label+offset. It is not clear how to guess
321 // which it might be. We assume offset==0. 334 // which it might be. We assume offset==0.
322 program->EmitAbs32(program->FindOrMakeAbs32Label(target_rva)); 335 ok = program->EmitAbs32(program->FindOrMakeAbs32Label(target_rva));
336 if (!ok)
337 break;
323 p += 4; 338 p += 4;
324 continue; 339 continue;
325 } 340 }
326 341
327 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva) 342 while (rel32_pos != rel32_locations_.end() && *rel32_pos < current_rva)
328 ++rel32_pos; 343 ++rel32_pos;
329 344
330 if (rel32_pos != rel32_locations_.end() && *rel32_pos == current_rva) { 345 if (rel32_pos != rel32_locations_.end() && *rel32_pos == current_rva) {
331 RVA target_rva = current_rva + 4 + Read32LittleEndian(p); 346 RVA target_rva = current_rva + 4 + Read32LittleEndian(p);
332 program->EmitRel32(program->FindOrMakeRel32Label(target_rva)); 347 ok = program->EmitRel32(program->FindOrMakeRel32Label(target_rva));
333 p += 4; 348 p += 4;
334 continue; 349 continue;
335 } 350 }
336 351
337 if (incomplete_disassembly_) { 352 if (incomplete_disassembly_) {
338 if ((abs32_pos == abs32_locations_.end() || end_rva <= *abs32_pos) && 353 if ((abs32_pos == abs32_locations_.end() || end_rva <= *abs32_pos) &&
339 (rel32_pos == rel32_locations_.end() || end_rva <= *rel32_pos) && 354 (rel32_pos == rel32_locations_.end() || end_rva <= *rel32_pos) &&
340 (end_rva <= relocs_start_rva || current_rva >= relocs_start_rva)) { 355 (end_rva <= relocs_start_rva || current_rva >= relocs_start_rva)) {
341 // No more relocs in this section, don't bother encoding bytes. 356 // No more relocs in this section, don't bother encoding bytes.
342 break; 357 break;
343 } 358 }
344 } 359 }
345 360
346 program->EmitByteInstruction(*p); 361 ok = program->EmitByteInstruction(*p);
347 p += 1; 362 p += 1;
348 } 363 }
364
365 return ok;
349 } 366 }
350 367
351 #if COURGETTE_HISTOGRAM_TARGETS 368 #if COURGETTE_HISTOGRAM_TARGETS
352 // Histogram is printed to std::cout. It is purely for debugging the algorithm 369 // Histogram is printed to std::cout. It is purely for debugging the algorithm
353 // and is only enabled manually in 'exploration' builds. I don't want to add 370 // and is only enabled manually in 'exploration' builds. I don't want to add
354 // command-line configuration for this feature because this code has to be 371 // command-line configuration for this feature because this code has to be
355 // small, which means compiled-out. 372 // small, which means compiled-out.
356 void DisassemblerWin32X86::HistogramTargets(const char* kind, 373 void DisassemblerWin32X86::HistogramTargets(const char* kind,
357 const std::map<RVA, int>& map) { 374 const std::map<RVA, int>& map) {
358 int total = 0; 375 int total = 0;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 delete pe_info; 443 delete pe_info;
427 *output = program; 444 *output = program;
428 return C_OK; 445 return C_OK;
429 } 446 }
430 447
431 void DeleteAssemblyProgram(AssemblyProgram* program) { 448 void DeleteAssemblyProgram(AssemblyProgram* program) {
432 delete program; 449 delete program;
433 } 450 }
434 451
435 } // namespace courgette 452 } // namespace courgette
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698