Direct usage of brk is discouraged in favor of using malloc and it was removed from POSIX in POSIX.1-2001. In particular, the musl libc always returns -ENOMEM for brk which means that the LTP tests fail when calling the kernel directly would pass.
sbrk has the same issues so this patch reworks the brk tests slightly to only use direct brk syscalls.
Signed-off-by: Teo Couprie Diaz teo.coupriediaz@arm.com --- As per internal discussion I believe this patch could be proposed upstream once it is in a good shape. It would fix the brk tests for musl-based systems like Alpine where they currently fail. Note that sbrk is not fixed by this patch as it is fully implemented in the libc: there is no syscall.
testcases/kernel/syscalls/brk/brk01.c | 22 ++++++++++++++++++---- testcases/kernel/syscalls/brk/brk02.c | 17 ++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/testcases/kernel/syscalls/brk/brk01.c b/testcases/kernel/syscalls/brk/brk01.c index 5f482b943..c16b46eaa 100644 --- a/testcases/kernel/syscalls/brk/brk01.c +++ b/testcases/kernel/syscalls/brk/brk01.c @@ -16,7 +16,15 @@ void verify_brk(void) size_t inc = getpagesize() * 2 - 1; unsigned int i;
- cur_brk = (uintptr_t)sbrk(0); + long ret = tst_syscall(__NR_brk, 0); + + if (ret < 0) + { + tst_res(TFAIL, "Failed to get current break"); + return; + } + + cur_brk = (uintptr_t)ret;
for (i = 0; i < 33; i++) { switch (i % 3) { @@ -31,11 +39,17 @@ void verify_brk(void) break; }
- TST_EXP_PASS_SILENT(brk((void *)new_brk), "brk()"); - if (!TST_PASS) + tst_syscall(__NR_brk, new_brk); + + ret = tst_syscall(__NR_brk, 0); + + if (ret < 0) + { + tst_res(TFAIL, "Failed to get current break"); return; + }
- cur_brk = (uintptr_t)sbrk(0); + cur_brk = (uintptr_t)ret;
if (cur_brk != new_brk) { tst_res(TFAIL, diff --git a/testcases/kernel/syscalls/brk/brk02.c b/testcases/kernel/syscalls/brk/brk02.c index 11e803cb4..19514febb 100644 --- a/testcases/kernel/syscalls/brk/brk02.c +++ b/testcases/kernel/syscalls/brk/brk02.c @@ -14,24 +14,27 @@ #include <unistd.h> #include <sys/mman.h> #include "tst_test.h" +#include "lapi/syscalls.h"
void brk_down_vmas(void) { - void *brk_addr = sbrk(0); + long ret = tst_syscall(__NR_brk, 0);
- if (brk_addr == (void *) -1) - tst_brk(TBROK, "sbrk() failed"); + if (ret < 0) + tst_brk(TBROK, "Failed to get current break"); + + void *brk_addr = (void *) ret;
unsigned long page_size = getpagesize(); void *addr = brk_addr + page_size;
- if (brk(addr)) { + if (tst_syscall(__NR_brk, addr) < addr) { tst_res(TFAIL | TERRNO, "Cannot expand brk() by page size"); return; }
addr += page_size; - if (brk(addr)) { + if (tst_syscall(__NR_brk, addr) < addr) { tst_res(TFAIL | TERRNO, "Cannot expand brk() by 2x page size"); return; } @@ -42,12 +45,12 @@ void brk_down_vmas(void) }
addr += page_size; - if (brk(addr)) { + if (tst_syscall(__NR_brk, addr) < addr) { tst_res(TFAIL | TERRNO, "Cannot expand brk() after mprotect"); return; }
- if (brk(brk_addr)) { + if (tst_syscall(__NR_brk, brk_addr) != brk_addr) { tst_res(TFAIL | TERRNO, "Cannot restore brk() to start address"); return; }