Skip to content

Commit

Permalink
Allow stubbing already implemented functions
Browse files Browse the repository at this point in the history
If a function is both implemented.csv and stubbed.csv, it will now stub
it in generate_detours, but will avoid generating a stub (as that would
create a duplicate symbol linker error). This can be useful to debug
broken functions.
  • Loading branch information
roblabla committed Dec 31, 2023
1 parent e4a9064 commit 92bfd9b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
61 changes: 35 additions & 26 deletions scripts/generate_detours.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ def get_path_of_mangled_symbol(symbol):
raise Exception("Unknown symbol kind " + symbol)


output = sys.stdout
if args.output:
output = open(args.output, "w")

fun_to_mangled_map = {}
with open(args.input_def) as f:
for line in f:
Expand Down Expand Up @@ -78,48 +74,61 @@ def get_path_of_mangled_symbol(symbol):
fun_addr = int(func[1], 16)
mapping_obj[fun_name] = fun_addr

detours = {}
f = open("config/implemented.csv")
implemented_csv = csv.reader(f)

print("Detouring detours[] = {", file=output)
first = True
for implemented in implemented_csv:
if not first:
print(",", file=output)

fun_name = implemented[0]
fun_mangled_name = fun_to_mangled_map[fun_name]
fun_addr = mapping_obj[fun_name]
print(
" { " + hex(fun_addr) + ', "' + fun_mangled_name + '", FALSE }',
end="",
file=output,
)
first = False
detours[fun_name] = {
"fun_addr": fun_addr,
"fun_mangled_name": fun_mangled_name,
"stub": False,
}

f = open("config/stubbed.csv")
stubbed_csv = csv.reader(f)
first = True
for implemented in stubbed_csv:
print(",", file=output)

fun_name = implemented[0]
fun_mangled_name = fun_to_mangled_map[fun_name]
fun_addr = mapping_obj[fun_name]
print(
" { " + hex(fun_addr) + ', "' + fun_mangled_name + '", TRUE }',
end="",
file=output,
)
detours[fun_name] = {
"fun_addr": fun_addr,
"fun_mangled_name": fun_mangled_name,
"stub": True,
}

# Add some necessary detouring to share MSVCRT heap with the main executable
for fun_name in ["_malloc", "_calloc", "_realloc", "operator_new", "_free", "__msize"]:
print(",", file=output)

fun_mangled_name = fun_to_mangled_map[fun_name]
fun_addr = mapping_obj[fun_name]
detours[fun_name] = {
"fun_addr": fun_addr,
"fun_mangled_name": fun_mangled_name,
"stub": False,
}

output = sys.stdout
if args.output:
output = open(args.output, "w")

print("Detouring detours[] = {", file=output)

first = True
for detour in detours.values():
if not first:
print(",", file=output)
first = False
print(
" { " + hex(fun_addr) + ', "' + fun_mangled_name + '", FALSE }',
" { "
+ hex(detour["fun_addr"])
+ ', "'
+ detour["fun_mangled_name"]
+ '", '
+ str(detour["stub"]).upper()
+ " }",
end="",
file=output,
)
Expand Down
10 changes: 10 additions & 0 deletions scripts/generate_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"arg_types": func[5:],
}

with open("config/implemented.csv") as f:
implemented_csv = csv.reader(f)
for func in implemented_csv:
mapping_obj[func[0]]["implemented"] = True


f = open("config/stubbed.csv")
stubbed_csv = csv.reader(f)

Expand Down Expand Up @@ -54,6 +60,10 @@
for stub in stubbed_csv:
fun_name = stub[0]
fun = mapping_obj[fun_name]
if fun.get("implemented", False):
# We don't need to generate a stub for implemented functions.
continue

calling_convention = fun["calling_convention"]
ret_type = fun["ret_type"]
ret_val = ret_vals[ret_type]
Expand Down

0 comments on commit 92bfd9b

Please sign in to comment.