From 3c1e8ac55cb1385a75641095179cef906d482cc4 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 16 Jul 2019 17:46:47 +0000 Subject: [PATCH] tests: robustify old_mmap test Do not assume that old mmap syscall is implemented in the kernel when __NR_mmap is defined. * tests/alarm.c (main): Print mmap return code using sprintrc, do not invoke mprotect if mmap is not implemented. --- tests/old_mmap.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/tests/old_mmap.c b/tests/old_mmap.c index 92ab3efb..216ecca5 100644 --- a/tests/old_mmap.c +++ b/tests/old_mmap.c @@ -22,6 +22,7 @@ || defined __s390__ || defined __s390x__) \ && (defined PATH_TRACING || !(defined __s390x__ || defined __m68k__)) +# include # include # include # include @@ -35,8 +36,9 @@ int main(void) { long rc = syscall(__NR_mmap, 0); + const bool implemented = rc != -1 || errno != ENOSYS; # ifndef PATH_TRACING - printf("mmap(NULL) = %ld %s (%m)\n", rc, errno2name()); + printf("mmap(NULL) = %s\n", sprintrc(rc)); # endif const unsigned long args1_c[6] = { @@ -60,42 +62,51 @@ main(void) rc = syscall(__NR_mmap, args); # if XLAT_RAW - printf("mmap(%#lx, %lu, %#x, %#x, %d, %#lx) = %ld %s (%m)\n", + printf("mmap(%#lx, %lu, %#x, %#x, %d, %#lx) = %s\n", args1_c[0], args1_c[1], PROT_READ|PROT_EXEC, MAP_FILE | MAP_FIXED, - (int) args1_c[4], args1_c[5], rc, errno2name()); + (int) args1_c[4], args1_c[5], sprintrc(rc)); # elif XLAT_VERBOSE printf("mmap(%#lx, %lu, %#x /* PROT_READ|PROT_EXEC */" - ", %#x /* MAP_FILE|MAP_FIXED */, %d, %#lx) = %ld %s (%m)\n", + ", %#x /* MAP_FILE|MAP_FIXED */, %d, %#lx) = %s\n", args1_c[0], args1_c[1], PROT_READ|PROT_EXEC, MAP_FILE | MAP_FIXED, - (int) args1_c[4], args1_c[5], rc, errno2name()); + (int) args1_c[4], args1_c[5], sprintrc(rc)); # else printf("mmap(%#lx, %lu, PROT_READ|PROT_EXEC, MAP_FILE|MAP_FIXED" - ", %d, %#lx) = %ld %s (%m)\n", + ", %d, %#lx) = %s\n", args1_c[0], args1_c[1], (int) args1_c[4], args1_c[5], - rc, errno2name()); + sprintrc(rc)); # endif memcpy(args, args2_c, sizeof(args2_c)); rc = syscall(__NR_mmap, args); # ifndef PATH_TRACING + const char *errstr; + if (implemented) { + char *str; + if (asprintf(&str, "%#lx", rc) < 0) + perror_msg_and_fail("asprintf"); + errstr = str; + } else { + errstr = sprintrc(rc); + } # if XLAT_RAW - printf("mmap(NULL, %lu, %#x, %#x, %d, %#lx) = %#lx\n", + printf("mmap(NULL, %lu, %#x, %#x, %d, %#lx) = %s\n", args2_c[1], PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, - (int) args2_c[4], args2_c[5], rc); + (int) args2_c[4], args2_c[5], errstr); # elif XLAT_VERBOSE printf("mmap(NULL, %lu, %#x /* PROT_READ|PROT_WRITE */" - ", %#x /* MAP_PRIVATE|MAP_ANONYMOUS */, %d, %#lx) = %#lx\n", + ", %#x /* MAP_PRIVATE|MAP_ANONYMOUS */, %d, %#lx) = %s\n", args2_c[1], PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, - (int) args2_c[4], args2_c[5], rc); + (int) args2_c[4], args2_c[5], errstr); # else printf("mmap(NULL, %lu, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS" - ", %d, %#lx) = %#lx\n", - args2_c[1], (int) args2_c[4], args2_c[5], rc); + ", %d, %#lx) = %s\n", + args2_c[1], (int) args2_c[4], args2_c[5], errstr); # endif # endif void *addr = (void *) rc; - if (mprotect(addr, page_size, PROT_NONE)) + if (implemented && mprotect(addr, page_size, PROT_NONE)) perror_msg_and_fail("mprotect(%p, %lu, PROT_NONE)", addr, page_size); -- 2.40.0