Skip to content

Call destructors#309

Merged
yuri91 merged 4 commits intomasterfrom
call-destructors
Jun 30, 2025
Merged

Call destructors#309
yuri91 merged 4 commits intomasterfrom
call-destructors

Conversation

@Maqrkk
Copy link
Member

@Maqrkk Maqrkk commented Jun 23, 2025

Run global destructors when exit is called. Exit is now automatically called after main in WASI mode, and when -pthread is passed.

@Maqrkk Maqrkk requested a review from yuri91 June 23, 2025 10:52
if (DtorFunc->isNullValue())
break; // Found a null terminator, skip the rest.

// We do not add global destructors that are genericjs.
Copy link
Member

Choose a reason for hiding this comment

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

Investigate if it is possible to inherit from the pass, or use it from another pass, instead of editing its source directly.
Don't spend too much time on it though, if it seems difficult

This is no longer necessary, as placing these in the start function during CallConstructors ensures they are reachable, and thus GDA will not remove them.
@Maqrkk Maqrkk force-pushed the call-destructors branch 2 times, most recently from 3a1caa1 to ef5ed32 Compare June 26, 2025 09:28
Maqrkk added 3 commits June 27, 2025 10:21
This caused a crash in asan with the current state of constructors and destructors. Originally, it's meant to be a thread_local with a constructor that runs lazily on first use. Currently in cheerp, thread_local constructors are not supported. Removing the initializer is safe because the struct is only ever used as a global, and it's initialized to zero by default.
This was not a problem before, because the constructor was never run. Since it initialized to zero it had no effect. With the work on global destructors however, there is now a reason to run this constructor.
Previously this was unused, but now with global destructors this can be called.
This commit enables global destructors to run after main. Destructors that are in genericjs are not run. The new wrapper pass is run to lower functions with the destructor attribute into calls to __cxa_atexit. If the libc exit function is not reachable from the code, the destructors are removed. Finally, exit is now called automatically after the main function has finished running in WASI mode, or when -pthread is passed.
@Maqrkk Maqrkk force-pushed the call-destructors branch from ef5ed32 to 5a90003 Compare June 27, 2025 13:33
Copy link
Member

@yuri91 yuri91 left a comment

Choose a reason for hiding this comment

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

Nice work

@yuri91 yuri91 merged commit e7dc431 into master Jun 30, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants