Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix malloc & free in early constructors #2311

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from

Conversation

brooksdavis
Copy link
Member

Set a priority for the mrs constructer in the "system" range.

It's not clear to me what the right priority is. 100 works for this test, but it's seems easy to argue for something higher like 50.

@brooksdavis brooksdavis requested a review from jrtc27 February 3, 2025 21:33
libexec/early_constructor/Makefile Outdated Show resolved Hide resolved
libexec/Makefile Outdated Show resolved Hide resolved
lib/libc/stdlib/malloc/mrs/mrs.c Outdated Show resolved Hide resolved
bin/cheribsdtest/cheribsdtest_malloc.c Outdated Show resolved Hide resolved
bin/cheribsdtest/cheribsdtest_malloc.c Outdated Show resolved Hide resolved
bin/cheribsdtest/cheribsdtest_malloc.c Outdated Show resolved Hide resolved
libexec/early_constructor/early_constructor.c Outdated Show resolved Hide resolved
@brooksdavis brooksdavis force-pushed the malloc_early_constructor branch 3 times, most recently from 97aa02c to b953392 Compare February 4, 2025 19:28
@brooksdavis
Copy link
Member Author

I've switch to a model more like the one used by jemalloc. I retain a constructor with default priority, but add checks to all allocation paths that the allocator is initialized. I've also added a simple spin lock around initialization in case constructors manage to spawn threads that race the constructor.

@jrtc27
Copy link
Member

jrtc27 commented Feb 4, 2025

Hm, I also note that, unlike the dynamic library, these helpers only exist for one ABI. malloc_is_revoking makes sure to be purecap at least (technically it'll end up as benchmark ABI if you do a TARGET_ARCH=aarch64cb build but we don't do that so I guess that's ok, I'm sure other bugs like that exist), but this one doesn't, so it'll be hybrid in a hybrid world. It also means we're not testing it when statically linked, nor are we testing either for the benchmark ABI (though it would be hard to see how they'd break there), nor are we testing either for c18n.

@brooksdavis brooksdavis force-pushed the malloc_early_constructor branch from b953392 to feee935 Compare February 4, 2025 22:47
@brooksdavis
Copy link
Member Author

Now testing purecap and benchmark in static and dynamic configurations:

# file /usr/libexec/malloc_early_constructor_*
/usr/libexec/malloc_early_constructor_benchmark_dynamic: ELF 64-bit LSB pie executable, ARM aarch64, C64, CheriABI, version 1 (SYSV), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 15.0 (1500026), FreeBSD-style, pure-capability benchmark ABI, not stripped
/usr/libexec/malloc_early_constructor_benchmark_static:  ELF 64-bit LSB executable, ARM aarch64, C64, CheriABI, version 1 (SYSV), statically linked, for FreeBSD 15.0 (1500026), FreeBSD-style, pure-capability benchmark ABI, not stripped
/usr/libexec/malloc_early_constructor_dynamic:           ELF 64-bit LSB pie executable, ARM aarch64, C64, CheriABI, version 1 (SYSV), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 15.0 (1500026), FreeBSD-style, not stripped
/usr/libexec/malloc_early_constructor_static:            ELF 64-bit LSB executable, ARM aarch64, C64, CheriABI, version 1 (SYSV), statically linked, for FreeBSD  15.0 (1500026), FreeBSD-style, not stripped

The Makefiles are arguably too clever for their own good, but it's doing the right thing.

@brooksdavis brooksdavis force-pushed the malloc_early_constructor branch 3 times, most recently from a704ba8 to 85c4acc Compare February 7, 2025 21:21
@brooksdavis
Copy link
Member Author

I think this is basically ready to land

lib/libc/stdlib/malloc/mrs/mrs.c Outdated Show resolved Hide resolved
lib/libc/stdlib/malloc/mrs/mrs.c Outdated Show resolved Hide resolved
lib/libc/stdlib/malloc/mrs/mrs.c Show resolved Hide resolved
bin/cheribsdtest/cheribsdtest_malloc.c Show resolved Hide resolved
bin/cheribsdtest/cheribsdtest.c Outdated Show resolved Hide resolved
bin/cheribsdtest/cheribsdtest.c Outdated Show resolved Hide resolved
Prefer PATH_MAX which is in POSIX.

Fixes:		5c59b01 cheribsdtest: infrastructure to spawn children
Being able to access more than the name provides extra flexibility.
@brooksdavis brooksdavis force-pushed the malloc_early_constructor branch from 85c4acc to b8cfa04 Compare February 13, 2025 19:54
bin/cheribsdtest/cheribsdtest.c Outdated Show resolved Hide resolved
bin/cheribsdtest/cheribsdtest.c Outdated Show resolved Hide resolved
bin/cheribsdtest/cheribsdtest.c Outdated Show resolved Hide resolved
Add support for tests that look for a helper program with the
cheribsdtest instance's suffix appended.  This allows per-target
helpers to test things that need to be tested with a range of
compilation modes, but can't be tested directly in the cheribsdtest
binary.
Add a check (predicted false) to all allocation paths that mrs has been
initialized and add asserts to free paths.  As with jemalloc, retain a
constructor so that malloc is initialized before main() regardless.

Previously, the constructor might not be called before another
constructor called free() which lead to a NULL pointer dereference.
Protect against the possiblity of a constructor, etc creating a thread
which races a call to mrs_init_impl and put a spinlock around the actual
initialization.
Add a set of binaries with a simple constructor at priority 101 that
calls malloc and free.  Make sure they exit successfully.
@brooksdavis brooksdavis force-pushed the malloc_early_constructor branch from b8cfa04 to 57ac576 Compare February 14, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-land PR is ready to land after revisions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants