tools/client/audit_linux_imports.gdb.py (1935B)
1 """Audit the built Linux resolver before any guest module initialization runs.""" 2 import hashlib 3 import json 4 import pathlib 5 import struct 6 7 import gdb 8 9 10 gdb.execute('set pagination off') 11 gdb.execute('set confirm off') 12 gdb.execute('set print thread-events off') 13 gdb.execute('tbreak attach_module') 14 gdb.execute('run') 15 16 # Read the bound import table, then query the real resolver without executing APIs. 17 count = int(gdb.parse_and_eval('(unsigned int)linux_import_count()')) 18 table = int(gdb.parse_and_eval('&imports')) 19 missing = int(gdb.parse_and_eval('&external_call')) 20 entries = [] 21 for index in range(count): 22 dll, name, ordinal = struct.unpack('<QQI4x', gdb.selected_inferior().read_memory(table + index * 24, 24)) 23 function = int(gdb.parse_and_eval('(void*)recomp_lookup_import(%d)' % (0xf0000000 + index * 4))) 24 entries.append({ 25 'dll': gdb.parse_and_eval('(char*)%d' % dll).string(), 26 'name': gdb.parse_and_eval('(char*)%d' % name).string() if name else None, 27 'ordinal': ordinal, 28 'adapter_found': function != missing, 29 }) 30 31 output = pathlib.Path('docs/linux-import-coverage.json') 32 with pathlib.Path(gdb.current_progspace().filename).open('rb') as executable: 33 digest = hashlib.file_digest(executable, 'sha256').hexdigest() 34 output.write_text(json.dumps({'executable_sha256': digest, 'imports': entries}, indent=2) + '\n') 35 supported = sum(entry['adapter_found'] for entry in entries) 36 print('%d bound symbols: %d with adapters, %d missing' % (count, supported, count - supported)) 37 for entry in entries: 38 if not entry['adapter_found']: 39 print('%s!%s' % (entry['dll'], entry['name'] or ('#%d' % entry['ordinal']))) 40 print('Adapter presence does not establish full argument/method coverage. Dynamic imports and COM methods require separate auditing.') 41 print('Known data exports are excluded. Unrecognized data exports can still be incorrectly bound as call tokens.') 42 gdb.execute('quit')