Skip to content

Commit

Permalink
cheribsdtest: binaries with malloc in early constructor
Browse files Browse the repository at this point in the history
Add a set of binaries with a simple constructor at priority 101 that
calls malloc and free.  Make sure the exit successfully.
  • Loading branch information
brooksdavis committed Feb 5, 2025
1 parent 75ce19d commit 7513353
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 0 deletions.
27 changes: 27 additions & 0 deletions bin/cheribsdtest/cheribsdtest_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,33 @@ CHERIBSDTEST(malloc_revocation_ctl_suid_elfnote_enable_protctl_disable,
"malloc_is_revoking_suid_elfnote_enable", true, &arg);
}

CHERIBSDTEST(malloc_early_constructor,
"invoke malloc in an early constructor",
.ct_check_skip = cheribsdtest_skip_no_helper)
{
pid_t pid;
int res;
char *helper_path = strdup(cheribsdtest_get_helper_path());

pid = fork();
CHERIBSDTEST_VERIFY(pid >= 0);
if (pid == 0) {
char *argv[2];

argv[0] = helper_path;
argv[1] = NULL;
execve(argv[0], argv, NULL);
abort();
} else {
waitpid(pid, &res, 0);
if (WIFEXITED(res) && WEXITSTATUS(res) == 0)
cheribsdtest_success();
else
cheribsdtest_failure_errx("child %s exited improperly",
helper_path);
}
}

static void
check_mallocx(size_t size)
{
Expand Down
1 change: 1 addition & 0 deletions libexec/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ _tests= tests
.endif

.if ${MACHINE_CPU:Mcheri}
SUBDIR+= malloc_early_constructor
SUBDIR+= malloc_is_revoking
.endif

Expand Down
8 changes: 8 additions & 0 deletions libexec/malloc_early_constructor/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.include <src.opts.mk>

SUBDIR= purecap purecap-dynamic
.if (${MACHINE_ABI:Mpurecap} && ${MACHINE_ABI:Mbenchmark}) || ${MK_LIB64CB} == "yes"
SUBDIR+= purecap-benchmark purecap-benchmark-dynamic
.endif

.include <bsd.subdir.mk>
24 changes: 24 additions & 0 deletions libexec/malloc_early_constructor/Makefile.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.include <src.opts.mk>

BINDIR= /usr/libexec

.if ${.CURDIR:M*benchmark*}
.if !${MACHINE_ABI:Mbenchmark}
NEED_COMPAT= 64CB
.include <bsd.compat.mk>
.endif
.else
.if !${MACHINE_ABI:Mpurecap}
NEED_COMPAT= 64C
.include <bsd.compat.mk>
.endif
.endif

PROG:= ${.PARSEDIR:tA:T}-${.CURDIR:T}
.PATH: ${.PARSEDIR}
SRCS:= ${.PARSEDIR:tA:T}.c
MAN=

.if ${.CURDIR:M*static*}
NO_SHARED=t
.endif
21 changes: 21 additions & 0 deletions libexec/malloc_early_constructor/malloc_early_constructor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdlib.h>

/*
* Perform a malloc in the earliest allowed non-system constructor.
* If malloc is not initialized before this point it could fail.
*/
__attribute__((__constructor__(101)))
static void
foo(void)
{
void * volatile ptr;

ptr = malloc(1);
free(ptr);
}

int
main(void)
{
return (0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# See ../Makefile.inc

.include <bsd.prog.mk>
3 changes: 3 additions & 0 deletions libexec/malloc_early_constructor/purecap-benchmark/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# See ../Makefile.inc

.include <bsd.prog.mk>
3 changes: 3 additions & 0 deletions libexec/malloc_early_constructor/purecap-dynamic/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# See ../Makefile.inc

.include <bsd.prog.mk>
3 changes: 3 additions & 0 deletions libexec/malloc_early_constructor/purecap/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# See ../Makefile.inc

.include <bsd.prog.mk>

Check warning on line 3 in libexec/malloc_early_constructor/purecap/Makefile

View workflow job for this annotation

GitHub Actions / Style Checker

Missing Signed-off-by: line

0 comments on commit 7513353

Please sign in to comment.