-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype_augmentor.py
executable file
·84 lines (64 loc) · 2.82 KB
/
type_augmentor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
"""
The goal of this module is to take the explicit qualified type information of
a set of functions as well as a call tree and to expand the type information
to include implicit type information. The following rules apply:
- A function with direct type X also has indirect type X
- A function that calls a function with indirect type X also has indirect type X
"""
import sys
from scrapers import AnnotationKind
def determine_indirect_type( function, call_tree,
funptr_types, function_types ):
"""
For the given function, determine its indirect type by recursively
walking the call tree and colleting function types that are indirectly
called by this function
"""
visited = set( [ function ] )
qualifiers = set()
for child in call_tree.calls( function ):
qualifiers |= _rec_determine_indirect_type(
child, call_tree, funptr_types, function_types, visited )
return qualifiers
def _rec_determine_indirect_type( function, call_tree, funptr_types,
function_types, visited ):
"""
Recurse the call tree starting at |function| and collecting the types
of everything visited. Do not recurse on |visited|.
"""
types = []
types += funptr_types.get( function, [] )
types += function_types.get( function, [] )
for child in call_tree.calls( function ):
if child not in visited:
visited.add( child )
types += _rec_determine_indirect_type(
child, call_tree, funptr_types, function_types,
visited )
return set( [ ( AnnotationKind.INDIRECT, qual ) for ( _, qual ) in types ] )
def augment_types( call_tree, funptr_types, function_types ):
"""
Given a call tree, the types of all function pointers, and the types of all
functions (augmented with overrides), determine the indirect types of
all functions and return a mapping of function to its complete type
"""
types = {}
for function in function_types.keys():
indirect_types = determine_indirect_type(
function, call_tree, funptr_types, function_types )
direct_types = function_types[ function ]
types[ function ] = indirect_types | direct_types
return types
if __name__ == '__main__':
from pprint import pprint
import scrapers
import call_tree
from ast_helpers import get_translation_unit
target = get_translation_unit( sys.argv[ 1 ] )
call_tree = call_tree.build_call_tree( target )
overrides = scrapers.Overrides.scrape( target )
func_types = scrapers.FunctionQualifiers.scrape( target )
funcptr_types = scrapers.FunctionPointers.scrape( target )
call_tree.augment_with_overrides( overrides )
pprint( augment_types( call_tree, funcptr_types, func_types ) )