js/src/devtools/rootAnalysis/explain.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/python
michael@0 2
michael@0 3 import re
michael@0 4 import argparse
michael@0 5
michael@0 6 parser = argparse.ArgumentParser(description='Process some integers.')
michael@0 7 parser.add_argument('rootingHazards', nargs='?', default='rootingHazards.txt')
michael@0 8 parser.add_argument('gcFunctions', nargs='?', default='gcFunctions.txt')
michael@0 9 parser.add_argument('hazards', nargs='?', default='hazards.txt')
michael@0 10 parser.add_argument('extra', nargs='?', default='unnecessary.txt')
michael@0 11 parser.add_argument('refs', nargs='?', default='refs.txt')
michael@0 12 args = parser.parse_args()
michael@0 13
michael@0 14 num_hazards = 0
michael@0 15 num_refs = 0
michael@0 16 try:
michael@0 17 with open(args.rootingHazards) as rootingHazards, \
michael@0 18 open(args.hazards, 'w') as hazards, \
michael@0 19 open(args.extra, 'w') as extra, \
michael@0 20 open(args.refs, 'w') as refs:
michael@0 21 current_gcFunction = None
michael@0 22
michael@0 23 # Map from a GC function name to the list of hazards resulting from
michael@0 24 # that GC function
michael@0 25 hazardousGCFunctions = {}
michael@0 26
michael@0 27 # List of tuples (gcFunction, index of hazard) used to maintain the
michael@0 28 # ordering of the hazards
michael@0 29 hazardOrder = []
michael@0 30
michael@0 31 for line in rootingHazards:
michael@0 32 m = re.match(r'^Time: (.*)', line)
michael@0 33 mm = re.match(r'^Run on:', line)
michael@0 34 if m or mm:
michael@0 35 print >>hazards, line
michael@0 36 print >>extra, line
michael@0 37 print >>refs, line
michael@0 38 continue
michael@0 39
michael@0 40 m = re.match(r'^Function.*has unnecessary root', line)
michael@0 41 if m:
michael@0 42 print >>extra, line
michael@0 43 continue
michael@0 44
michael@0 45 m = re.match(r'^Function.*takes unsafe address of unrooted', line)
michael@0 46 if m:
michael@0 47 num_refs += 1
michael@0 48 print >>refs, line
michael@0 49 continue
michael@0 50
michael@0 51 m = re.match(r"^Function.*has unrooted.*of type.*live across GC call ('?)(.*?)('?) at \S+:\d+$", line)
michael@0 52 if m:
michael@0 53 # Function names are surrounded by single quotes. Field calls
michael@0 54 # are unquoted.
michael@0 55 current_gcFunction = m.group(2)
michael@0 56 hazardousGCFunctions.setdefault(current_gcFunction, []).append(line)
michael@0 57 hazardOrder.append((current_gcFunction, len(hazardousGCFunctions[current_gcFunction]) - 1))
michael@0 58 num_hazards += 1
michael@0 59 continue
michael@0 60
michael@0 61 if current_gcFunction:
michael@0 62 if not line.strip():
michael@0 63 # Blank line => end of this hazard
michael@0 64 current_gcFunction = None
michael@0 65 else:
michael@0 66 hazardousGCFunctions[current_gcFunction][-1] += line
michael@0 67
michael@0 68 with open(args.gcFunctions) as gcFunctions:
michael@0 69 gcExplanations = {} # gcFunction => stack showing why it can GC
michael@0 70
michael@0 71 current_func = None
michael@0 72 explanation = None
michael@0 73 for line in gcFunctions:
michael@0 74 m = re.match(r'^GC Function: (.*)', line)
michael@0 75 if m:
michael@0 76 if current_func:
michael@0 77 gcExplanations[current_func] = explanation
michael@0 78 current_func = None
michael@0 79 if m.group(1) in hazardousGCFunctions:
michael@0 80 current_func = m.group(1)
michael@0 81 explanation = line
michael@0 82 elif current_func:
michael@0 83 explanation += line
michael@0 84 if current_func:
michael@0 85 gcExplanations[current_func] = explanation
michael@0 86
michael@0 87 for gcFunction, index in hazardOrder:
michael@0 88 gcHazards = hazardousGCFunctions[gcFunction]
michael@0 89 if gcFunction in gcExplanations:
michael@0 90 print >>hazards, (gcHazards[index] + gcExplanations[gcFunction])
michael@0 91 else:
michael@0 92 print >>hazards, gcHazards[index]
michael@0 93
michael@0 94 except IOError as e:
michael@0 95 print 'Failed: %s' % str(e)
michael@0 96
michael@0 97 print("Wrote %s" % args.hazards)
michael@0 98 print("Wrote %s" % args.extra)
michael@0 99 print("Wrote %s" % args.refs)
michael@0 100 print("Found %d hazards and %d unsafe references" % (num_hazards, num_refs))

mercurial