Skip to content

Commit

Permalink
cheribsdtest: Add test for canonical scalar function pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgao committed Jan 26, 2025
1 parent eee9dfb commit 0173154
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
19 changes: 19 additions & 0 deletions bin/cheribsdtest/cheribsdtest_fptr_canon.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* SUCH DAMAGE.
*/

#include <sys/types.h>

#include <dlfcn.h>

#include <cheribsdtest_dynamic.h>
Expand Down Expand Up @@ -82,3 +84,20 @@ CHERIBSDTEST(fptr_canon_dlfunc,

cheribsdtest_success();
}

CHERIBSDTEST(fptr_canon_scalar,
"Check that function pointers are canonical when cast to scalar")
{
volatile ptraddr_t fptr_scalar;
void (* volatile fptr_dlsym)(void);

fptr_scalar = cheribsdtest_dynamic_dummy_func_scalar_address;
fptr_dlsym = (void (*)(void))dlsym(RTLD_DEFAULT,
"cheribsdtest_dynamic_dummy_func_scalar_only");

CHERIBSDTEST_VERIFY2(fptr_scalar == (ptraddr_t)fptr_dlsym,
"scalar %#p differs from dlsym %#p",
(void *)(uintptr_t)fptr_scalar, fptr_dlsym);

cheribsdtest_success();
}
3 changes: 3 additions & 0 deletions lib/libcheribsdtest_dynamic/cheribsdtest_dynamic.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
void cheribsdtest_dynamic_dummy_func(void);
void (*cheribsdtest_dynamic_get_dummy_fptr(void))(void);

void cheribsdtest_dynamic_dummy_func_scalar_only(void);
extern ptraddr_t cheribsdtest_dynamic_dummy_func_scalar_address;

void * __capability cheribsdtest_dynamic_identity_cap(void * __capability cap);

int cheribsdtest_dynamic_ifunc(void);
Expand Down
11 changes: 11 additions & 0 deletions lib/libcheribsdtest_dynamic/cheribsdtest_dynamic_fptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ void
{
return (&cheribsdtest_dynamic_dummy_func);
}

/*
* This function must only be referenced as a scalar.
*/
void
cheribsdtest_dynamic_dummy_func_scalar_only(void)
{
}

ptraddr_t cheribsdtest_dynamic_dummy_func_scalar_address =
(ptraddr_t)&cheribsdtest_dynamic_dummy_func_scalar_only;

Check warning on line 57 in lib/libcheribsdtest_dynamic/cheribsdtest_dynamic_fptr.c

View workflow job for this annotation

GitHub Actions / Style Checker

Missing Signed-off-by: line

Check warning on line 57 in lib/libcheribsdtest_dynamic/cheribsdtest_dynamic_fptr.c

View workflow job for this annotation

GitHub Actions / Style Checker

Missing Signed-off-by: line

Check warning on line 57 in lib/libcheribsdtest_dynamic/cheribsdtest_dynamic_fptr.c

View workflow job for this annotation

GitHub Actions / Style Checker

Missing Signed-off-by: line

0 comments on commit 0173154

Please sign in to comment.