When I run run_tests.py on my computer, I get the following error:
-------
brk_near_huge (2M: 32): brk_near_huge: malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
brk_near_huge (2M: 64): brk_near_huge: malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
-------
My uname is "Linux GW-Boat 5.4.0-47-generic #51-Ubuntu SMP Fri Sep 4 19:50:52 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux"
This error only occurs when the QUIET_TEST environment variable is set.
After tracking the heap information, I found that calling malloc for the first time after sbrk(0) would cause the brk0 obtained by sbrk(0) to fail. The following is bt information:
► f 0 7ffff7dfb260 malloc
f 1 7ffff7de3aae fopen64+30
f 2 7ffff7de3aae fopen64+30
f 3 5555555568ee read_maps+78
f 4 555555557204 test_addr_huge+52
Because malloc needs a heap to save malloc_chunk related information, and will apply for a large of virtual memory from the kernel through brk(), so the heap has grown
When executing memset(brk0, 0, newbrk-brk0);, malloc_chunk information will also be erased, so an error will occur when executing PASS();->printf->·····->malloc.The following is bt information:
► f 0 7ffff7da418b raise+203
f 1 7ffff7d83859 abort+299
f 2 7ffff7df645a
f 3 7ffff7df8abf sysmalloc+1855
f 4 7ffff7df9913 _int_malloc+3363
f 5 7ffff7dfb2d4 malloc+116
f 6 7ffff7de2e84 _IO_file_doallocate+148
f 7 7ffff7df3050 _IO_doallocbuf+80
f 8 7ffff7df20b0 _IO_file_overflow+432
f 9 7ffff7df0835 _IO_file_xsputn+229
f 10 7ffff7df0835 _IO_file_xsputn+229
So we should call malloc() explicitly to initialize heap-related information.
The following is my solution for reference, maybe there is a better solution
---
diff --git a/tests/brk_near_huge.c b/tests/brk_near_huge.c
index c9662f4..f323ec8 100644
--- a/tests/brk_near_huge.c
+++ b/tests/brk_near_huge.c
@@ -73,8 +73,13 @@ int main(int argc, char *argv[])
int fd;
void *brk0, *hugemap_addr, *newbrk;
char *p;
+ void *tmp;
int err;
+ /*Let malloc be initialized explicitly*/
+ tmp = malloc(32);
+ free(tmp);
+
test_init(argc, argv);
hpage_size = check_hugepagesize();