-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathanalyze-python.py
More file actions
338 lines (265 loc) · 10.3 KB
/
Copy pathanalyze-python.py
File metadata and controls
338 lines (265 loc) · 10.3 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python3
import asyncio
import importlib
import importlib.metadata
import inspect
import os.path
import signal
import subprocess
import sys
import traceback
from contextlib import redirect_stdout, redirect_stderr
from dataclasses import dataclass
from typing import Optional
from unittest.mock import MagicMock
PY_EXTENSION = '.py'
EXECUTION_LOG_PATH = '/execution.log'
EXECUTION_TIMEOUT_SECONDS = 10
@dataclass
class Package:
"""Class for tracking a package."""
name: str
version: Optional[str] = None
local_path: Optional[str] = None
def install_arg(self) -> str:
if self.local_path:
return self.local_path
elif self.version:
return f'{self.name}=={self.version}'
else:
return self.name
def install(package):
"""Pip install."""
arg = package.install_arg()
try:
output = subprocess.check_output(
(sys.executable, '-m', 'pip', 'install', '--pre', arg),
stderr=subprocess.STDOUT)
print('Install succeeded:')
print(output.decode())
except subprocess.CalledProcessError as e:
print('Failed to install:')
print(e.output.decode())
# Always raise.
# Install failing is either an interesting issue, or an opportunity to
# improve the analysis.
raise
def path_to_import(path):
"""Convert a path to import."""
if path.name == '__init__.py':
import_path = str(path.parent)
else:
import_path = str(path).removesuffix(PY_EXTENSION)
return import_path.replace('/', '.')
def module_paths_to_import(package):
"""Returns list of paths of modules to import (or execute) for the package."""
paths = []
for f in importlib.metadata.files(package.name):
# TODO: pyc, C extensions?
if f.suffix == PY_EXTENSION:
paths.append(path_to_import(f))
return paths
def import_package(package):
"""Import phase for analyzing the package."""
for p in module_paths_to_import(package):
import_module(p)
def import_single_module(import_path):
module_dir = os.path.dirname(import_path)
sys.path.append(module_dir)
module_name = os.path.basename(import_path).rstrip(PY_EXTENSION)
print(f'Import single module at {import_path}')
import_module(module_name)
def import_module(import_path):
print('Importing', import_path)
# noinspection PyBroadException
try:
importlib.import_module(import_path)
# catch everything, including SystemExit and KeyboardInterrupt
except BaseException:
print('Failed to import', import_path)
traceback.print_exc()
return
def execute_package(package):
"""Execute phase for analyzing the package."""
for p in module_paths_to_import(package):
try:
module = importlib.import_module(p)
except BaseException:
# We reach here when import fails. So skip this package.
continue
else:
execute_module(module)
def execute_module(module):
# Setup for module execution
# 1. handler for function execution timeout alarms
# 2. redirect stdout and stderr to execution log file
signal.signal(signal.SIGALRM, handler=alarm_handler)
with open(EXECUTION_LOG_PATH, 'at') as log, redirect_stdout(log), redirect_stderr(log):
# noinspection PyBroadException
try:
do_execute(module)
# want to catch everything since code execution may cause some weird behaviour
except BaseException:
print('Failed to execute code for module', module)
traceback.print_exc()
# restore default signal handler for SIGALRM
signal.signal(signal.SIGALRM, signal.SIG_DFL)
def do_execute(module):
"""Best-effort execution of code in a module"""
print('[module]', module)
# Keep track of all types belonging to the module we've seen so far in return values,
# so that we can recursively explore each one's methods without going in infinite loops.
# Using instances returned by module code is likely to be a more useful than ones
# instantiated with mocked constructor args
seen_types = set()
def should_investigate(t):
return t.__module__ == module.__name__ and t not in seen_types
def mark_seen(t):
seen_types.add(t)
instantiated_types = set()
skipped_names = []
for (name, member) in inspect.getmembers(module):
if inspect.isfunction(member):
return_value = try_invoke_function(member, name)
return_type = return_value.__class__
# TODO should it be DFS or BFS?
if should_investigate(return_type):
print('[investigate type]', return_type)
mark_seen(return_type)
try_call_methods(return_value, return_type, should_investigate, mark_seen)
elif inspect.isclass(member):
instance = try_instantiate_class(member, name)
assert instance.__class__ == member
if instance is not None and member not in instantiated_types:
instantiated_types.add(member)
try_call_methods(instance, name, should_investigate, mark_seen)
else:
skipped_names.append(name)
print('[skipped members]', ' '.join(skipped_names))
def alarm_handler(sig_num, frame):
raise TimeoutError('Timeout exceeded for function execution')
# Call a function with mock arguments based on its declared signature.
# The arguments are of type MagicMock, whose instances will return
# dummy values for any method called on them.
# Exceptions must be handled by the caller.
def invoke_function(obj):
signature = inspect.signature(obj)
args = []
kwargs = {}
for name, param in signature.parameters.items():
# use MagicMock to create semi-realistic function argument values
# https://docs.python.org/3/library/unittest.mock.html
value = MagicMock() if param.default == param.empty else param.default
match param.kind:
case param.POSITIONAL_ONLY:
args.append(value)
case param.KEYWORD_ONLY | param.POSITIONAL_OR_KEYWORD:
kwargs[name] = value
case param.VAR_POSITIONAL: # when *args appears in signature
pass # ignore
case param.VAR_KEYWORD: # when **args appears in signature
pass # ignore
# bind args and invoke the function
# any exceptions will be propagated to the caller
bound = signature.bind(*args, **kwargs)
# set timeout to prevent hangs
signal.alarm(EXECUTION_TIMEOUT_SECONDS)
# run function and await the result if necessary
# ret_obj is the object returned by the function, which may need
# further evaluation / awaiting to produce the return value
ret_obj = obj(*bound.args, **bound.kwargs)
if inspect.isasyncgen(ret_obj):
# async generator - await in a loop
async def execute():
return [x async for x in ret_obj]
ret_val = asyncio.run(execute())
elif inspect.isgenerator(ret_obj):
# normal generator - execute in a loop
ret_val = [x for x in ret_obj]
elif inspect.iscoroutine(ret_obj):
# async function - await run
ret_val = asyncio.run(ret_obj)
else:
# normal function - just run
ret_val = ret_obj
signal.alarm(0)
return ret_val
# Execute a callable and catch any exception, logging to stdout
def run_and_catch_all(c: callable):
try:
return c()
except BaseException as e:
# catch ALL exceptions, including KeyboardInterrupt and system exit
print(type(e), e, sep=': ')
def try_invoke_function(f, name, is_method=False):
print('[method]' if is_method else '[function]', name)
def invoke():
return invoke_function(f)
ret = run_and_catch_all(invoke)
if ret is not None:
print('[return value]', repr(ret))
return ret
def try_instantiate_class(c, name):
print('[class]', name)
def instantiate():
return invoke_function(c)
return run_and_catch_all(instantiate)
# tries to call the methods of the given object instance
# should_investigate and mark_seen are mutable input/output variables
# that track which types have been traversed
def try_call_methods(instance, class_name, should_investigate, mark_seen):
print('[instance methods]', class_name)
def is_non_init_method(m):
return inspect.ismethod(m) and m.__name__ != '__init__'
for method_name, method in inspect.getmembers(instance, is_non_init_method):
return_value = try_invoke_function(method, method_name, is_method=True)
return_type = return_value.__class__
# TODO should it be DFS or BFS?
if should_investigate(return_type):
print('[investigate type]', return_type)
mark_seen(return_type)
try_call_methods(return_value, return_type, should_investigate, mark_seen)
PHASES = {
'all': [install, import_package, execute_module],
'install': [install],
'import': [import_package],
'execute': [execute_package],
}
def main() -> int:
args = list(sys.argv)
script = args.pop(0)
if len(args) < 2 or len(args) > 4:
print(f'Usage: {script} [--local file | --version version] phase package_name')
return -1
# Parse the arguments manually to avoid introducing unnecessary dependencies
# and side effects that add noise to the strace output.
local_path = None
version = None
package_name = None
if args[0] == '--local':
args.pop(0)
local_path = args.pop(0)
elif args[0] == '--version':
args.pop(0)
version = args.pop(0)
phase = args.pop(0)
if args:
package_name = args.pop(0)
if phase not in PHASES:
print(f'Unknown phase {phase} specified.')
return 1
if package_name is None:
# single module mode
if phase == 'import' and local_path is not None:
import_single_module(local_path)
return 0
else:
print('install requested but no package name given, or local file missing for single module import')
return 1
package = Package(name=package_name, version=version, local_path=local_path)
# Execute for the specified phase.
for phase in PHASES[phase]:
phase(package)
return 0
if __name__ == '__main__':
exit(main())