This series updates the brk LTP tests to use direct syscalls which makes the compat tests pass on our musl-based distribution. Add a simple sanity check for purecap that will fail if brk is not disabled.
Depends on merging the corresponding Linux patch disabling brk : https://op-lists.linaro.org/archives/list/linux-morello@op-lists.linaro.org/...
Teo Couprie Diaz (2): syscalls/brk: use direct syscall syscalls/brk: add a sanity check for purecap
testcases/kernel/syscalls/brk/brk01.c | 35 ++++++++++++++++++++++++--- testcases/kernel/syscalls/brk/brk02.c | 17 +++++++------ 2 files changed, 41 insertions(+), 11 deletions(-)
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.
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; }
brk is disabled in purecap so in normal operation the test would be skipped by LTP. If that is not the case, guarantee the test will fail.
Signed-off-by: Teo Couprie Diaz teo.coupriediaz@arm.com --- testcases/kernel/syscalls/brk/brk01.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
diff --git a/testcases/kernel/syscalls/brk/brk01.c b/testcases/kernel/syscalls/brk/brk01.c index c16b46eaa..5157a5605 100644 --- a/testcases/kernel/syscalls/brk/brk01.c +++ b/testcases/kernel/syscalls/brk/brk01.c @@ -9,7 +9,19 @@ #include <errno.h>
#include "tst_test.h" +#include "lapi/syscalls.h"
+#ifdef __CHERI_PURE_CAPABILITY__ +void verify_brk(void) +{ + /* + * tst_syscall skips the test if the syscall returns -ENOSYS, + * which is the expected behavior in purecap. + */ + tst_syscall(__NR_brk, 0); + tst_res(TFAIL, "brk should not be implemented in purecap"); +} +#else void verify_brk(void) { uintptr_t cur_brk, new_brk; @@ -65,6 +77,7 @@ void verify_brk(void)
tst_res(TPASS, "brk() works fine"); } +#endif /* __CHERI_PURE_CAPABILITY__ */
static struct tst_test test = { .test_all = verify_brk,
Hi Teo,
On 17-10-2022 17:59, Teo Couprie Diaz wrote:
This series updates the brk LTP tests to use direct syscalls which makes the compat tests pass on our musl-based distribution. Add a simple sanity check for purecap that will fail if brk is not disabled.
Depends on merging the corresponding Linux patch disabling brk : https://op-lists.linaro.org/archives/list/linux-morello@op-lists.linaro.org/...
I believe this series should be on the Morello LTP mailing list: linux-morello-ltp@op-lists.linaro.org
Thanks, Tudor
Teo Couprie Diaz (2): syscalls/brk: use direct syscall syscalls/brk: add a sanity check for purecap
testcases/kernel/syscalls/brk/brk01.c | 35 ++++++++++++++++++++++++--- testcases/kernel/syscalls/brk/brk02.c | 17 +++++++------ 2 files changed, 41 insertions(+), 11 deletions(-)
On 17/10/2022 18:14, Tudor Cretu wrote:
Hi Teo,
On 17-10-2022 17:59, Teo Couprie Diaz wrote:
This series updates the brk LTP tests to use direct syscalls which makes the compat tests pass on our musl-based distribution. Add a simple sanity check for purecap that will fail if brk is not disabled.
Depends on merging the corresponding Linux patch disabling brk : https://op-lists.linaro.org/archives/list/linux-morello@op-lists.linaro.org/...
I believe this series should be on the Morello LTP mailing list: linux-morello-ltp@op-lists.linaro.org
You are absolutely right : I realized my mistake later in the evening... Re-sent to the LTP mailing list, my apologies for not paying attention.
Thanks, Tudor
Thanks for pointing it out ! Téo
Teo Couprie Diaz (2): syscalls/brk: use direct syscall syscalls/brk: add a sanity check for purecap
testcases/kernel/syscalls/brk/brk01.c | 35 ++++++++++++++++++++++++--- testcases/kernel/syscalls/brk/brk02.c | 17 +++++++------ 2 files changed, 41 insertions(+), 11 deletions(-)
linux-morello@op-lists.linaro.org