Skip to content

Commit

Permalink
[CI] Add tests for heap bins(#780)
Browse files Browse the repository at this point in the history
* New CI tests for  small, large and unsorted heap bins

Co-authored-by: Grazfather <grazfather@gmail.com>
  • Loading branch information
theguy147 and Grazfather committed Jan 12, 2022
1 parent 08f4b28 commit 2975d5f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/binaries/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ canary.out: EXTRA_FLAGS := -fstack-protector-all

heap-non-main.out heap-tcache.out heap-multiple-heaps.out: EXTRA_FLAGS := -pthread

heap-bins.out: EXTRA_FLAGS := -Wno-unused-result

default.out: EXTRA_FLAGS := -fstack-protector-all -fpie -pie
18 changes: 18 additions & 0 deletions tests/binaries/heap-bins.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>

#include "utils.h"

int main(){
void *small = malloc(0x10); // small chunk
malloc(0x20); // avoid consolidation of chunks
void *large = malloc(0x410); // large chunk
malloc(0x20); // avoid consolidation of chunks
free(small);
free(large);
void *unsorted = malloc(0x420); // make sure the unsorted chunk is bigger than large chunk
malloc(0x420); // sort the freed chunks from unsorted to their corresponding bins
free(unsorted);
DebugBreak();
return EXIT_SUCCESS;
}
28 changes: 28 additions & 0 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ def test_cmd_heap_bins_fast(self):
self.assertIn("Chunk(addr=", res)
return

def test_cmd_heap_bins_large(self):
cmd = "heap bins large"
target = _target("heap-bins")
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
self.assertIn("Found 1 chunks in 1 large non-empty bins", res)
self.assertIn("Chunk(addr=", res)
self.assertIn("size=0x420", res)

def test_cmd_heap_bins_non_main(self):
cmd = "python gdb.execute('heap bins fast {}'.format(get_glibc_arena().next))"
before = ["set environment GLIBC_TUNABLES glibc.malloc.tcache_count=0"]
Expand All @@ -300,6 +309,16 @@ def test_cmd_heap_bins_non_main(self):
self.assertIn("size=0x20", res)
return

def test_cmd_heap_bins_small(self):
cmd = "heap bins small"
before = ["set environment GLIBC_TUNABLES glibc.malloc.tcache_count=0"]
target = _target("heap-bins")
res = gdb_run_silent_cmd(cmd, before=before, target=target)
self.assertNoException(res)
self.assertIn("Found 1 chunks in 1 small non-empty bins", res)
self.assertIn("Chunk(addr=", res)
self.assertIn("size=0x20", res)

def test_cmd_heap_bins_tcache(self):
cmd = "heap bins tcache"
target = _target("heap-non-main")
Expand All @@ -319,6 +338,15 @@ def test_cmd_heap_bins_tcache_all(self):
self.assertTrue(len(tcachebins_lines) == 2)
return

def test_cmd_heap_bins_unsorted(self):
cmd = "heap bins unsorted"
target = _target("heap-bins")
res = gdb_run_silent_cmd(cmd, target=target)
self.assertNoException(res)
self.assertIn("Found 1 chunks in unsorted bin", res)
self.assertIn("Chunk(addr=", res)
self.assertIn("size=0x430", res)

def test_cmd_heap_analysis(self):
cmd = "heap-analysis-helper"
target = _target("heap-analysis")
Expand Down

0 comments on commit 2975d5f

Please sign in to comment.