brk: use direct syscalls
cover brk: use direct syscalls
Hello LTP maintainers,
This patch slightly reworks the brk01 and brk02 tests to use direct syscalls with tst_syscall rather than calling through the libc.
While running LTP on a musl-based distribution, we noticed that the brk tests were failing. It turns out that Musl prevents the use of brk with their wrapper: it always returns an error.
This isn't too egregious as using brk is deprecated in favor of malloc and it isn't part of POSIX anymore since POSIX.1-2001, but it _is_ different from glibc's beavior, which allows using it.
This patch allows proper testing of brk's functionality, independent of libc specifics, and thus allows the tests to pass on Musl-based distributions like Alpine.
Do you think this is a correct approach for LTP ?
From what I could see there are a few tests that use tst_syscall directly and it doesn't affect the logic much for brk.
Green build:
https://github.com/Teo-CD/ltp/actions/runs/3563193507
This was discovered in the context of the Morello project[0]. [0]: https://www.morello-project.org/
Teo Couprie Diaz (1):
syscalls/brk: use direct syscall
testcases/kernel/syscalls/brk/brk01.c | 15 ++++++--------- testcases/kernel/syscalls/brk/brk02.c | 14 ++++++-------- 2 files changed, 12 insertions(+), 17 deletions(-)
base-commit: 498247917f40b0a82cb149e2ec1cb518acd7b632
Signature · 5 lines
2.25.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
RFC PATCH 1/1 syscalls/brk: use direct syscall
Direct usage of brk is discouraged in favor of using malloc. Also, brk was removed from POSIX in POSIX.1-2001.
In particular, the Musl libc's brk always returns -ENOMEM which causes the LTP tests to exit prematurely. Invoking the syscall directly allows them to properly validate brk behavior.
Use tst_syscall() and handle the failure cases ourselves, as we don't depend on the libc to do it anymore.
The patch also removes the dependency on sbrk to get the current break as it has the same issues. Instead, call tst_syscall(__NR_brk, 0) which always returns the current break.
Update brk01 to use void* to unify it with brk02.
--- testcases/kernel/syscalls/brk/brk01.c | 15 ++++++--------- testcases/kernel/syscalls/brk/brk02.c | 14 ++++++-------- 2 files changed, 12 insertions(+), 17 deletions(-)
testcases/kernel/syscalls/brk/brk01.c +6 -9
@@ -9,14 +9,15 @@#include <errno.h> #include "tst_test.h" +#include "lapi/syscalls.h" void verify_brk (void ) { -uintptr_t cur_brk, new_brk; -uintptr_t inc =getpagesize () *2 -1 ; +void *cur_brk, *new_brk; +size_t inc =getpagesize () *2 -1 ;unsigned int i; - cur_brk = (uintptr_t )sbrk (0 ); + cur_brk = (void *)tst_syscall (__NR_brk,0 );for (i =0 ; i <33 ; i++) {switch (i %3 ) {
@@ -31,16 +32,12 @@ void verify_brk(void)break ; } -TST_EXP_PASS_SILENT (brk ((void *)new_brk),"brk()" ); -if (!TST_PASS ) -return ; - - cur_brk = (uintptr_t )sbrk (0 ); + cur_brk = (void *)tst_syscall (__NR_brk, new_brk);if (cur_brk != new_brk) {tst_res (TFAIL ,"brk() failed to set address have %p expected %p" , - (void *)cur_brk, (void *)new_brk); + cur_brk, new_brk);return ; }
testcases/kernel/syscalls/brk/brk02.c +6 -8
@@ -14,24 +14,22 @@#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 ); - -if (brk_addr == (void *)-1 ) -tst_brk (TBROK ,"sbrk() failed" ); +void *brk_addr = (void *)tst_syscall (__NR_brk,0 );unsigned long page_size =getpagesize ();void *addr = brk_addr + page_size; -if (brk (addr)) { +if ((void *)tst_syscall (__NR_brk, addr) < addr) {tst_res (TFAIL |TERRNO ,"Cannot expand brk() by page size" );return ; } addr += page_size; -if (brk (addr)) { +if ((void *)tst_syscall (__NR_brk, addr) < addr) {tst_res (TFAIL |TERRNO ,"Cannot expand brk() by 2x page size" );return ; }
@@ -42,12 +40,12 @@ void brk_down_vmas(void) } addr += page_size; -if (brk (addr)) { +if ((void *)tst_syscall (__NR_brk, addr) < addr) {tst_res (TFAIL |TERRNO ,"Cannot expand brk() after mprotect" );return ; } -if (brk (brk_addr)) { +if ((void *)tst_syscall (__NR_brk, brk_addr) != brk_addr) {tst_res (TFAIL |TERRNO ,"Cannot restore brk() to start address" );return ; }
Signature · 5 lines
2.25.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 12 lines
Hello LTP maintainers,
This patch slightly reworks the brk01 and brk02 tests to use direct syscalls with
tst_syscallrather than calling through the libc.While running LTP on a musl-based distribution, we noticed that the brk tests were failing. It turns out that Musl prevents the use of brk with their wrapper: it always returns an error.
This isn't too egregious as using brk is deprecated in favor of malloc and it isn't part of POSIX anymore since POSIX.1-2001, but it _is_ different from glibc's beavior, which allows using it.
Quoted 7 lines
This patch allows proper testing of brk's functionality, independent of libc specifics, and thus allows the tests to pass on Musl-based distributions like Alpine.
Do you think this is a correct approach for LTP ?
From what I could see there are a few tests that usetst_syscalldirectly and it doesn't affect the logic much for brk.
LGTM.
I wonder if it makes sense to add .test_variants [1] for glibc and uclibc, e.g. for brk01():
void verify_brk(void)
{
if (tst_variant) {
tst_res(TINFO, "Testing sbrk()");
cur_brk = (uintptr_t)sbrk(0);
} else {
tst_res(TINFO, "Testing __NR_brk");
cur_brk = (void *)tst_syscall(__NR_brk, 0);
}}
struct tst_test test = {
...
#ifdef __GLIBC__
.test_variants = 2,
#else
.test_variants = 1,
#endif
...Not sure if it should be testeed also on android, i.e:
#if defined(__GLIBC__) || defined(__ANDROID__)
Kind regards,
Petr
[1] https://github.com/linux-test-project/ltp/wiki/C-Test-API#130-testing-similar-syscalls-in-one-test
Quoted 12 lines
Green build:
https://github.com/Teo-CD/ltp/actions/runs/3563193507This was discovered in the context of the Morello project[0]. [0]: https://www.morello-project.org/
Teo Couprie Diaz (1):
syscalls/brk: use direct syscalltestcases/kernel/syscalls/brk/brk01.c | 15 ++++++--------- testcases/kernel/syscalls/brk/brk02.c | 14 ++++++-------- 2 files changed, 12 insertions(+), 17 deletions(-)
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 21 lines
void
verify_brk(void){ if (tst_variant) { tst_res(TINFO, "Testing sbrk()"); cur_brk = (uintptr_t)sbrk(0); } else { tst_res(TINFO, "Testing __NR_brk"); cur_brk = (void *)tst_syscall(__NR_brk, 0); }}
struct tst_test test = { ... #ifdef __GLIBC__ .test_variants = 2, #else .test_variants = 1, #endif ...
Quoted 2 lines
Not sure if it should be testeed also on android, i.e:
#ifdefined(__GLIBC__)||defined(__ANDROID__)
Can we rather than ifdefing things out check the actual return values?
Should be as easy as doing:
if (cur_brk == (void*)-1) tst_brk(TCONF, "sbrk() not implemented");
in the libc test variant.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 25 lines
Hi!
Quoted 21 lines
void
verify_brk(void){ if (tst_variant) { tst_res(TINFO, "Testing sbrk()"); cur_brk = (uintptr_t)sbrk(0); } else { tst_res(TINFO, "Testing __NR_brk"); cur_brk = (void *)tst_syscall(__NR_brk, 0); }}
struct tst_test test = { ... #ifdef __GLIBC__ .test_variants = 2, #else .test_variants = 1, #endif ...Quoted 2 lines
Not sure if it should be testeed also on android, i.e:
#ifdefined(__GLIBC__)||defined(__ANDROID__)
Quoted 8 lines
Can we rather than ifdefing things out check the actual return values?
Should be as easy as doing:
if (cur_brk == (void*)-1) tst_brk(TCONF, "sbrk() not implemented");in the libc test variant.
+1.
WDYT about .test_variants? In that case it'd be tested also on musl or any platform where it's implemented.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 2 lines
WDYT about .test_variants? In that case it'd be tested also on musl or any platform where it's implemented.
I would got for it, that way we would test both the kernel implemntation and that libc does something sensible, e.g. returns error.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Cyril and Petr, thanks for having a look and the discussion !
Quoted 6 lines · Cyril Hrubis
Hi!
Quoted 2 lines
WDYT about .test_variants? In that case it'd be tested also on musl or any platform where it's implemented.
I would got for it, that way we would test both the kernel implemntation and that libc does something sensible, e.g. returns error.
I'm still quite new to LTP, so I might be understanding things wrong.
My understanding of your conversation is that you're suggesting using the .test_variants to have one version of the tests going through the libc wrappers as usual, eventually skipping the test with TCONF if the libc wrapper does not implement the syscall, and one version which would be the the direct syscall I am suggesting in this patch.
Would that be correct ? If so it does make sense to me, I could try implementing that.
Best regards,
Téo
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
WDYT about .test_variants? In that case it'd be tested also on musl or any platform where it's implemented.
I would got for it, that way we would test both the kernel implemntation and that libc does something sensible, e.g. returns error.
I'm still quite new to LTP, so I might be understanding things wrong.
Quoted 8 lines
My understanding of your conversation is that you're suggesting using the .test_variants to have one version of the tests going through the libc wrappers as usual, eventually skipping the test with TCONF if the libc wrapper does not implement the syscall, and one version which would be the the direct syscall I am suggesting in this patch.
Would that be correct ? If so it does make sense to me, I could try implementing that.
Yes, exactly this.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi,
Quoted 14 lines · Cyril Hrubis
Hi!
Quoted 13 lines
Quoted 5 lines
Quoted 2 lines
WDYT about .test_variants? In that case it'd be tested also on musl or any platform where it's implemented.
I would got for it, that way we would test both the kernel implemntation and that libc does something sensible, e.g. returns error.
I'm still quite new to LTP, so I might be understanding things wrong.
My understanding of your conversation is that you're suggesting using the .test_variants to have one version of the tests going through the libc wrappers as usual, eventually skipping the test with TCONF if the libc wrapper does not implement the syscall, and one version which would be the the direct syscall I am suggesting in this patch.
Quoted 3 lines
Quoted 2 lines
Would that be correct ? If so it does make sense to me, I could try implementing that.
Yes, exactly this.
Great, thanks : I will implement this and send a revision when it's done !
Best regards,
Téo
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 15 lines · Petr Vorel
Quoted 7 lines
Hello LTP maintainers,
This patch slightly reworks the brk01 and brk02 tests to use direct syscalls withtst_syscallrather than calling through the libc. While running LTP on a musl-based distribution, we noticed that the brk tests were failing. It turns out that Musl prevents the use of brk with their wrapper: it always returns an error.
This isn't too egregious as using brk is deprecated in favor of mallocQuoted 8 lines
and it isn't part of POSIX anymore since POSIX.1-2001, but it _is_ different from glibc's beavior, which allows using it.
This patch allows proper testing of brk's functionality, independent of libc specifics, and thus allows the tests to pass on Musl-based distributions like Alpine.
Do you think this is a correct approach for LTP ?
From what I could see there are a few tests that usetst_syscalldirectly
and it doesn't affect the logic much for brk.
Quoted 1 line
LGTM.
Thanks for having a look !
Quoted 14 lines
I wonder if it makes sense to add .test_variants [1] for glibc and uclibc, e.g. for
brk01():void
verify_brk(void){ if (tst_variant) { tst_res(TINFO, "Testing sbrk()"); cur_brk = (uintptr_t)sbrk(0); } else { tst_res(TINFO, "Testing __NR_brk"); cur_brk = (void *)tst_syscall(__NR_brk, 0); }
Quoted 1 line
}
I guess I can see how it could be useful, but LTP already has tests for sbrk: should there really be testing for sbrk in the brk tests ?
I will send another reply down the thread regarding the rest of the discussion !
Best regards,
Téo
Quoted 27 lines
struct tst_test test = { ... #ifdef __GLIBC__ .test_variants = 2, #else .test_variants = 1, #endif ...Not sure if it should be testeed also on android, i.e:
#ifdefined(__GLIBC__)||defined(__ANDROID__)Kind regards,
Petr[1] https://github.com/linux-test-project/ltp/wiki/C-Test-API#130-testing-similar-syscalls-in-one-test
Quoted 9 lines
Green build:
https://github.com/Teo-CD/ltp/actions/runs/3563193507
This was discovered in the context of the Morello project[0]. [0]: https://www.morello-project.org/
Teo Couprie Diaz (1):
syscalls/brk: use direct syscalltestcases/kernel/syscalls/brk/brk01.c | 15 ++++++--------- testcases/kernel/syscalls/brk/brk02.c | 14 ++++++-------- 2 files changed, 12 insertions(+), 17 deletions(-)
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp