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

Make try_init_allocator() a ctor #338

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions dlmalloc/src/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4562,7 +4562,7 @@ static void* tmalloc_small(mstate m, size_t nb) {

#if __wasilibc_unmodified_upstream // Forward declaration of try_init_allocator.
#else
static void try_init_allocator(void);
__attribute__((constructor(0))) static void try_init_allocator(void);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire range of 0-100 I believe is reserved for system functions like this. I'm not convinced 0 is the best choice here, but its not not hard to change in the future.

In emscripten we document the priorities in order to keep track of all the order which this stuff happens: https://github.com/emscripten-core/emscripten/blob/main/system/lib/README.md?plain=1#L9-L28

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Practically speaking, right now whenever we add a new constructor in wasi-libc, we just grep for all the constructors in the wasi-libc tree to figure out the ordering constraints, which seems ok for now, as there aren't very many.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe assigning priority 0 is a correct choice, given:

  • dlmalloc is a service which is likely used by other ctors
  • try_init_allocator itself doesn't rely on other ctors to be run before

We can also have a documentation somewhere to list all our ctors. I can add one in this PR if you folks think it's appropriate, though not sure where's the best place to add it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its ok to simply use grep as Dan says, I was just pointing out that it can get complicated, and that one needs to be careful when choosing the order of these things, once there is more than one.

There are also some things that might (one day) need to happen before malloc initialization (e.g. TLS setup, application of relocations in PIC code) although I don't think any of those apply yet in wasi-libc.

#endif

void* dlmalloc(size_t bytes) {
Expand Down Expand Up @@ -4593,13 +4593,6 @@ void* dlmalloc(size_t bytes) {
ensure_initialization(); /* initialize in sys_alloc if not using locks */
#endif

#if __wasilibc_unmodified_upstream // Try to initialize the allocator.
#else
if (!is_initialized(gm)) {
try_init_allocator();
}
#endif

if (!PREACTION(gm)) {
void* mem;
size_t nb;
Expand Down