build/win32/autobinscope.py

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 #!/usr/bin/env python
michael@0 2
michael@0 3 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 6
michael@0 7 # run Microsoft's Binscope tool (http://www.microsoft.com/download/en/details.aspx?id=11910)
michael@0 8 # against a fresh Windows build. output a 'binscope.log' file with full details
michael@0 9 # of the run and appropriate strings to integrate with the buildbots
michael@0 10
michael@0 11 # from the docs : "The error code returned when running under the command line is equal
michael@0 12 # to the number of failures the tool reported plus the number of errors. BinScope will return
michael@0 13 # 0 only if there are no errors or failures."
michael@0 14
michael@0 15 # the symbol dir should point to the symbol dir hierarchy created
michael@0 16 # via running make buildsymbols in a windows build's objdir
michael@0 17
michael@0 18 import sys
michael@0 19 import subprocess
michael@0 20 import os
michael@0 21
michael@0 22 BINSCOPE_OUTPUT_LOGFILE = r".\binscope_xml_output.log"
michael@0 23
michael@0 24 # usage
michael@0 25 if len(sys.argv) < 3:
michael@0 26 print """usage : autobinscope.by path_to_binary path_to_symbols [log_file_path]"
michael@0 27 log_file_path is optional, log will be written to .\binscope_xml_output.log by default"""
michael@0 28 sys.exit(0)
michael@0 29
michael@0 30 binary_path = sys.argv[1]
michael@0 31 symbol_path = sys.argv[2]
michael@0 32
michael@0 33 if len(sys.argv) == 4:
michael@0 34 log_file_path = sys.argv[3]
michael@0 35 else:
michael@0 36 log_file_path = BINSCOPE_OUTPUT_LOGFILE
michael@0 37
michael@0 38 # execute binscope against the binary, using the BINSCOPE environment
michael@0 39 # variable as the path to binscope.exe
michael@0 40 try:
michael@0 41 binscope_path = os.environ['BINSCOPE']
michael@0 42 except KeyError:
michael@0 43 print "BINSCOPE environment variable is not set, can't check DEP/ASLR etc. status."
michael@0 44 sys.exit(0)
michael@0 45
michael@0 46 try:
michael@0 47 proc = subprocess.Popen([binscope_path, "/target", binary_path,
michael@0 48 "/output", log_file_path, "/sympath", symbol_path,
michael@0 49 "/c", "ATLVersionCheck", "/c", "ATLVulnCheck", "/c", "FunctionPointersCheck",
michael@0 50 "/c", "SharedSectionCheck", "/c", "APTCACheck", "/c", "NXCheck",
michael@0 51 "/c", "GSCheck", "/c", "GSFunctionSafeBuffersCheck", "/c", "GSFriendlyInitCheck",
michael@0 52 "/c", "CompilerVersionCheck", "/c", "SafeSEHCheck", "/c", "SNCheck",
michael@0 53 "/c", "DBCheck"], stdout=subprocess.PIPE)
michael@0 54
michael@0 55 except WindowsError, (errno, strerror):
michael@0 56 if errno != 2 and errno != 3:
michael@0 57 print "Unexpected error ! \nError " + str(errno) + " : " + strerror + "\nExiting !\n"
michael@0 58 sys.exit(0)
michael@0 59 else:
michael@0 60 print "Could not locate binscope at location : %s\n" % binscope_path
michael@0 61 print "Binscope wasn't installed or the BINSCOPE env variable wasn't set correctly, skipping this check and exiting..."
michael@0 62 sys.exit(0)
michael@0 63
michael@0 64 proc.wait()
michael@0 65
michael@0 66 output = proc.communicate()[0]
michael@0 67
michael@0 68 # is this a PASS or a FAIL ?
michael@0 69 if proc.returncode != 0:
michael@0 70 print "Error count: %d" % proc.returncode
michael@0 71 print "TEST-UNEXPECTED-FAIL | autobinscope.py | %s is missing a needed Windows protection, such as /GS or ASLR" % binary_path
michael@0 72 logfile = open(log_file_path, "r")
michael@0 73 for line in logfile:
michael@0 74 print(line),
michael@0 75 else:
michael@0 76 print "TEST-PASS | autobinscope.py | %s succeeded" % binary_path

mercurial