Skip to content

Commit

Permalink
cheribsdtest: improve CHERIBSDTEST_CHECK_CALL_ERROR
Browse files Browse the repository at this point in the history
Use typeof() to make CHERIBSDTEST_CHECK_CALL_ERROR compatible with
functions that return pointers (e.g., mmap()).

Print unexpected return values with %#p to support most integer values.
  • Loading branch information
brooksdavis committed Jan 9, 2025
1 parent 9aa1ae1 commit 29da12c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions bin/cheribsdtest/cheribsdtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,23 @@ _cheribsdtest_check_errno(const char *context, int actual, int expected)
context, actual, actual_str, expected, expected_str);
}

#ifdef __CHERI_PURE_CAPABILITY__
#define __CHERIBSDTEST_PTR_FMT "%#p"
#else
#define __CHERIBSDTEST_PTR_FMT "%p"
#endif

/** Check that @p call fails and errno is set to @p expected_errno */
#define CHERIBSDTEST_CHECK_CALL_ERROR(call, expected_errno) \
do { \
errno = 0; \
int __ret = call; \
__typeof(call) __ret = call; \
int call_errno = errno; \
CHERIBSDTEST_VERIFY2(__ret == -1, \
#call " unexpectedly returned %d", __ret); \
CHERIBSDTEST_VERIFY2(__ret == (__typeof(__ret))-1, \
_Generic((__ret), \
void *: #call " unexpectedly returned " __CHERIBSDTEST_PTR_FMT, \
default: #call " unexpectedly returned %d"), \
__ret); \
_cheribsdtest_check_errno(#call, call_errno, \
expected_errno); \
} while (0)
Expand Down
2 changes: 1 addition & 1 deletion bin/cheribsdtest/cheribsdtest_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ CHERIBSDTEST(vm_notag_mprotect_no_cap,
static void
mmap_check_bad_protections(int prot, int expected_errno)
{
CHERIBSDTEST_CHECK_CALL_ERROR((int)(intptr_t)mmap(NULL, getpagesize(),
CHERIBSDTEST_CHECK_CALL_ERROR(mmap(NULL, getpagesize(),
prot, MAP_ANON, -1, 0), expected_errno);
}

Expand Down

0 comments on commit 29da12c

Please sign in to comment.