Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Makes sure that all EXE and DLL files in the provided directory were built | 6 """Makes sure that all EXE and DLL files in the provided directory were built |
| 7 correctly. | 7 correctly. |
| 8 | 8 |
| 9 In essense it runs a subset of BinScope tests ensuring that binaries have | 9 In essense it runs a subset of BinScope tests ensuring that binaries have |
| 10 /NXCOMPAT, /DYNAMICBASE and /SAFESEH. | 10 /NXCOMPAT, /DYNAMICBASE and /SAFESEH. |
| 11 """ | 11 """ |
| 12 | 12 |
| 13 import json | |
| 13 import os | 14 import os |
| 14 import optparse | 15 import optparse |
| 15 import sys | 16 import sys |
| 16 | 17 |
| 17 # Find /third_party/pefile based on current directory and script path. | 18 # Find /third_party/pefile based on current directory and script path. |
| 18 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', | 19 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', |
| 19 'third_party', 'pefile')) | 20 'third_party', 'pefile')) |
| 20 import pefile | 21 import pefile |
| 21 | 22 |
| 22 PE_FILE_EXTENSIONS = ['.exe', '.dll'] | 23 PE_FILE_EXTENSIONS = ['.exe', '.dll'] |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 37 def IsPEFile(path): | 38 def IsPEFile(path): |
| 38 return (os.path.isfile(path) and | 39 return (os.path.isfile(path) and |
| 39 os.path.splitext(path)[1].lower() in PE_FILE_EXTENSIONS and | 40 os.path.splitext(path)[1].lower() in PE_FILE_EXTENSIONS and |
| 40 os.path.basename(path) not in EXCLUDED_FILES) | 41 os.path.basename(path) not in EXCLUDED_FILES) |
| 41 | 42 |
| 42 def main(options, args): | 43 def main(options, args): |
| 43 directory = args[0] | 44 directory = args[0] |
| 44 pe_total = 0 | 45 pe_total = 0 |
| 45 pe_passed = 0 | 46 pe_passed = 0 |
| 46 | 47 |
| 48 failures = [] | |
| 49 | |
| 47 for file in os.listdir(directory): | 50 for file in os.listdir(directory): |
| 48 path = os.path.abspath(os.path.join(directory, file)) | 51 path = os.path.abspath(os.path.join(directory, file)) |
| 49 if not IsPEFile(path): | 52 if not IsPEFile(path): |
| 50 continue | 53 continue |
| 51 pe = pefile.PE(path, fast_load=True) | 54 pe = pefile.PE(path, fast_load=True) |
| 52 pe.parse_data_directories(directories=[ | 55 pe.parse_data_directories(directories=[ |
| 53 pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG']]) | 56 pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG']]) |
| 54 pe_total = pe_total + 1 | 57 pe_total = pe_total + 1 |
| 55 success = True | 58 success = True |
| 56 | 59 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 print("Checking %s ImageBase (0x%X < 4GB)... FAIL" % | 99 print("Checking %s ImageBase (0x%X < 4GB)... FAIL" % |
| 97 (path, pe.OPTIONAL_HEADER.ImageBase)) | 100 (path, pe.OPTIONAL_HEADER.ImageBase)) |
| 98 success = False | 101 success = False |
| 99 elif options.verbose: | 102 elif options.verbose: |
| 100 print("Checking %s ImageBase (0x%X > 4GB)... PASS" % | 103 print("Checking %s ImageBase (0x%X > 4GB)... PASS" % |
| 101 (path, pe.OPTIONAL_HEADER.ImageBase)) | 104 (path, pe.OPTIONAL_HEADER.ImageBase)) |
| 102 | 105 |
| 103 # Update tally. | 106 # Update tally. |
| 104 if success: | 107 if success: |
| 105 pe_passed = pe_passed + 1 | 108 pe_passed = pe_passed + 1 |
| 109 else: | |
| 110 failures.append(path) | |
| 106 | 111 |
| 107 print "Result: %d files found, %d files passed" % (pe_total, pe_passed) | 112 print "Result: %d files found, %d files passed" % (pe_total, pe_passed) |
| 113 | |
| 114 if options.json: | |
| 115 with open(options.json, 'w') as f: | |
| 116 json.dump(failures, f) | |
|
scottmg
2015/11/18 21:45:05
Is it useful to have something that's not an array
Paweł Hajdan Jr.
2015/11/19 11:27:29
Good question.
The consumer of that is in the sam
| |
| 117 | |
| 108 if pe_passed != pe_total: | 118 if pe_passed != pe_total: |
| 109 sys.exit(1) | 119 sys.exit(1) |
| 110 | 120 |
| 111 if __name__ == '__main__': | 121 if __name__ == '__main__': |
| 112 usage = "Usage: %prog [options] DIRECTORY" | 122 usage = "Usage: %prog [options] DIRECTORY" |
| 113 option_parser = optparse.OptionParser(usage=usage) | 123 option_parser = optparse.OptionParser(usage=usage) |
| 114 option_parser.add_option("-v", "--verbose", action="store_true", | 124 option_parser.add_option("-v", "--verbose", action="store_true", |
| 115 default=False, help="Print debug logging") | 125 default=False, help="Print debug logging") |
| 126 option_parser.add_option("--json", help="Path to JSON output file") | |
| 116 options, args = option_parser.parse_args() | 127 options, args = option_parser.parse_args() |
| 117 if not args: | 128 if not args: |
| 118 option_parser.print_help() | 129 option_parser.print_help() |
| 119 sys.exit(0) | 130 sys.exit(0) |
| 120 main(options, args) | 131 main(options, args) |
| OLD | NEW |