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

Side by Side Diff: courgette/disassembler_win32.cc

Issue 2055343002: Courgette: Add static method QuickDetect() to optimize program detection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sync Created 4 years, 5 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 | « courgette/disassembler_win32.h ('k') | courgette/disassembler_win32_x64.h » ('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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_win32.h" 5 #include "courgette/disassembler_win32.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "courgette/assembly_program.h" 13 #include "courgette/assembly_program.h"
14 #include "courgette/courgette.h" 14 #include "courgette/courgette.h"
15 15
16 #if COURGETTE_HISTOGRAM_TARGETS 16 #if COURGETTE_HISTOGRAM_TARGETS
17 #include <iostream> 17 #include <iostream>
18 #endif 18 #endif
19 19
20 namespace courgette { 20 namespace courgette {
21 21
22 DisassemblerWin32::DisassemblerWin32(const void* start, size_t length) 22 DisassemblerWin32::DisassemblerWin32(const uint8_t* start, size_t length)
23 : Disassembler(start, length) {} 23 : Disassembler(start, length) {}
24 24
25 RVA DisassemblerWin32::FileOffsetToRVA(FileOffset file_offset) const { 25 RVA DisassemblerWin32::FileOffsetToRVA(FileOffset file_offset) const {
26 for (int i = 0; i < number_of_sections_; ++i) { 26 for (int i = 0; i < number_of_sections_; ++i) {
27 const Section* section = &sections_[i]; 27 const Section* section = &sections_[i];
28 if (file_offset >= section->file_offset_of_raw_data) { 28 if (file_offset >= section->file_offset_of_raw_data) {
29 FileOffset offset_in_section = 29 FileOffset offset_in_section =
30 file_offset - section->file_offset_of_raw_data; 30 file_offset - section->file_offset_of_raw_data;
31 if (offset_in_section < section->size_of_raw_data) 31 if (offset_in_section < section->size_of_raw_data)
32 return static_cast<RVA>(section->virtual_address + offset_in_section); 32 return static_cast<RVA>(section->virtual_address + offset_in_section);
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 331
332 std::string DisassemblerWin32::SectionName(const Section* section) { 332 std::string DisassemblerWin32::SectionName(const Section* section) {
333 if (section == nullptr) 333 if (section == nullptr)
334 return "<none>"; 334 return "<none>";
335 char name[9]; 335 char name[9];
336 memcpy(name, section->name, 8); 336 memcpy(name, section->name, 8);
337 name[8] = '\0'; // Ensure termination. 337 name[8] = '\0'; // Ensure termination.
338 return name; 338 return name;
339 } 339 }
340 340
341 // static
342 bool DisassemblerWin32::QuickDetect(const uint8_t* start,
343 size_t length,
344 uint16_t magic) {
345 if (length < kOffsetOfFileAddressOfNewExeHeader + 4 /* size */)
346 return false;
347
348 // Have 'MZ' magic for a DOS header?
349 if (start[0] != 'M' || start[1] != 'Z')
350 return false;
351
352 FileOffset file_offset = static_cast<FileOffset>(
353 ReadU32(start, kOffsetOfFileAddressOfNewExeHeader));
354 if (file_offset >= length || file_offset % 8 != 0)
355 return false;
356 const uint8_t* const pe_header = start + file_offset;
357 const size_t kMinPEHeaderSize = 4 /*signature*/ + kSizeOfCoffHeader;
358 if (pe_header <= start || pe_header + kMinPEHeaderSize >= start + length)
359 return false;
360
361 const uint8_t* optional_header = pe_header + 4 + kSizeOfCoffHeader;
362 // Check we can read the magic.
363 if (optional_header + 2 >= start + length)
364 return false;
365 if (magic != ReadU16(optional_header, 0))
366 return false;
367
368 return true;
369 }
370
341 RvaVisitor* DisassemblerWin32::CreateAbs32TargetRvaVisitor() { 371 RvaVisitor* DisassemblerWin32::CreateAbs32TargetRvaVisitor() {
342 return new RvaVisitor_Abs32(abs32_locations_, *this); 372 return new RvaVisitor_Abs32(abs32_locations_, *this);
343 } 373 }
344 374
345 RvaVisitor* DisassemblerWin32::CreateRel32TargetRvaVisitor() { 375 RvaVisitor* DisassemblerWin32::CreateRel32TargetRvaVisitor() {
346 return new RvaVisitor_Rel32(rel32_locations_, *this); 376 return new RvaVisitor_Rel32(rel32_locations_, *this);
347 } 377 }
348 378
349 void DisassemblerWin32::RemoveUnusedRel32Locations( 379 void DisassemblerWin32::RemoveUnusedRel32Locations(
350 AssemblyProgram* program) { 380 AssemblyProgram* program) {
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 directory->size_ = static_cast<uint32_t>(size); 677 directory->size_ = static_cast<uint32_t>(size);
648 return true; 678 return true;
649 } else { 679 } else {
650 directory->address_ = 0; 680 directory->address_ = 0;
651 directory->size_ = 0; 681 directory->size_ = 0;
652 return true; 682 return true;
653 } 683 }
654 } 684 }
655 685
656 } // namespace courgette 686 } // namespace courgette
OLDNEW
« no previous file with comments | « courgette/disassembler_win32.h ('k') | courgette/disassembler_win32_x64.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698