Skip to content

Commit

Permalink
eval: add malloc-counter test vs. libdebug
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrewh committed Oct 31, 2024
1 parent c539564 commit 9224d8c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
7 changes: 7 additions & 0 deletions tests/eval/malloc1000.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdlib.h>
int main() {
for (int i=0; i<1000; i++) {
void *m = malloc(0x100);
free(m);
}
}
7 changes: 7 additions & 0 deletions tests/eval/malloc100000.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdlib.h>
int main() {
for (int i=0; i<100000; i++) {
void *m = malloc(0x100);
free(m);
}
}
7 changes: 7 additions & 0 deletions tests/eval/malloc1000000.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdlib.h>
int main() {
for (int i=0; i<1000000; i++) {
void *m = malloc(0x100);
free(m);
}
}
3 changes: 2 additions & 1 deletion tests/eval/malloccount_libdebug.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

bin_path = Path(sys.argv[1])
d = debugger(str(bin_path.resolve()))
d.run()
r = d.run()

e = ELF(bin_path)

Expand All @@ -16,6 +16,7 @@ def malloc_counter(t, bp):

d.breakpoint(e.plt["malloc"], callback=malloc_counter, file=bin_path.name)
d.cont()
d.wait()

print(f"malloc count: {counter}")
print("pass")
1 change: 0 additions & 1 deletion tests/eval/malloccount_pyda.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def malloc_counter(p):
global counter
counter += 1


p.hook(e.plt["malloc"], malloc_counter)
p.run()

Expand Down
29 changes: 27 additions & 2 deletions tests/eval/run_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ def no_warnings_or_errors(stdout: bytes, stderr: bytes) -> bool:
lambda o, e: o.count(b"pass\n") == 1,
]
)),
("test_malloc_1000", "malloc1000.c", "malloccount_pyda.py", "malloccount_libdebug.py", RunOpts(), ExpectedResult(
retcode=0,
checkers=[
output_checker,
no_warnings_or_errors,
lambda o, e: o.count(b"pass\n") == 1,
]
)),
("test_malloc_100000", "malloc100000.c", "malloccount_pyda.py", "malloccount_libdebug.py", RunOpts(), ExpectedResult(
retcode=0,
checkers=[
output_checker,
no_warnings_or_errors,
lambda o, e: o.count(b"pass\n") == 1,
]
)),
("test_malloc_1000000", "malloc1000000.c", "malloccount_pyda.py", "malloccount_libdebug.py", RunOpts(), ExpectedResult(
retcode=0,
checkers=[
output_checker,
no_warnings_or_errors,
lambda o, e: o.count(b"pass\n") == 1,
]
)),
]

def main():
Expand Down Expand Up @@ -70,13 +94,14 @@ def main():

def run_pyda(c_exe_path, pyda_script_path, env, expected_result, test_name, debug):
def run():
return subprocess.run(f"pyda {pyda_script_path.resolve()} -- {c_exe_path.resolve()}", env=env, stdin=subprocess.DEVNULL, shell=True, timeout=10, capture_output=True)
cmd = f"pyda {pyda_script_path.resolve()} -- {c_exe_path.resolve()}"
return subprocess.run(cmd, env=env, stdin=subprocess.DEVNULL, shell=True, timeout=60, capture_output=True)

return run_tool(run, test_name, expected_result, debug)

def run_libdebug(c_exe_path, libdebug_script_path, env, expected_result, test_name, debug):
def run():
return subprocess.run(f"python3 {libdebug_script_path.resolve()} {c_exe_path.resolve()}", env=env, stdin=subprocess.DEVNULL, shell=True, timeout=10, capture_output=True)
return subprocess.run(f"python3 {libdebug_script_path.resolve()} {c_exe_path.resolve()}", env=env, stdin=subprocess.DEVNULL, shell=True, timeout=60, capture_output=True)

return run_tool(run, test_name, expected_result, debug)

Expand Down

0 comments on commit 9224d8c

Please sign in to comment.