Skip to content

start code: implement __init_array_start, __init_array_end #20852

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

Merged
merged 2 commits into from
Jul 30, 2024

Conversation

andrewrk
Copy link
Member

Tested by observing __sanitizer_cov_8bit_counters_init and __sanitizer_cov_pcs_init being called in a libc-less build.

@andrewrk andrewrk requested a review from jacobly0 July 29, 2024 06:54
@alexrp
Copy link
Member

alexrp commented Jul 29, 2024

I would suggest also splitting up the calls to callMainWithArgs() and std.posix.exit() here, and calling the function pointers stored between __fini_array_start/__fini_array_end.

@andrewrk
Copy link
Member Author

@Rexicon226 how are we feeling about @divExact?

@andrewrk
Copy link
Member Author

andrewrk commented Jul 29, 2024

I would suggest also splitting up the calls to callMainWithArgs() and std.posix.exit() here, and calling the function pointers stored between __fini_array_start/__fini_array_end.

hmmm I don't know about that. I like it better when the process just ends. The OS cleans up all the memory and open handles, and any resource not cleaned up by the OS is a bug because the process could have been terminated abnormally. Not implementing those destructors forces people to code correctly.

@alexrp
Copy link
Member

alexrp commented Jul 29, 2024

hmmm I don't know about that. I like it better when the process just ends. The OS cleans up all the memory and open handles, and any resource not cleaned up by the OS is a bug because the process could have been terminated abnormally. Not implementing those destructors forces people to code correctly.

It's needed for correctness in some cases though. If I build some C code that uses __attribute__((destructor)) for cleanup into a Zig program that doesn't link libc, those destructors just won't be called. For an example of why this might actually matter for more than just simple handle cleanup, see #20382. Actually, you know what, that one's a bad example because it's about shared libraries primarily. The general point still stands about arbitrary C code, though. Agree it's not a concern for pure Zig code.

@andrewrk andrewrk removed the request for review from jacobly0 July 29, 2024 18:39
@andrewrk andrewrk enabled auto-merge July 29, 2024 20:25
Copy link
Member

@jacobly0 jacobly0 left a comment

Choose a reason for hiding this comment

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

I would suggest also splitting up the calls to callMainWithArgs() and std.posix.exit() here, and calling the function pointers stored between __fini_array_start/__fini_array_end.

hmmm I don't know about that. I like it better when the process just ends. The OS cleans up all the memory and open handles, and any resource not cleaned up by the OS is a bug because the process could have been terminated abnormally. Not implementing those destructors forces people to code correctly.

I'd argue that "not running destructors" should be the behavior of std.process.cleanExit in release modes, not the default. In fact, if handling is added between callMainWithArgs and std.posix.exit as was suggested, this would already be the case since cleanExit calls exit itself in release modes, instead of returning from main.

As for there being no use case for destructors, the first one that occurs to me is flushing compiler instrumentation data to disk for non-fuzzing use cases. In this case, it may be desirable to not write misleading profiling data when the process exits abnormally, maybe because you just got the command line syntax wrong the first time.

That said, I don't think there should be explicit language/std support for defining destructors in Zig, that use case seems questionable and the friction should remain with creating them from Zig.

@andrewrk
Copy link
Member Author

For that use case specifically, you would expect the executable to exit(1) (as opposed to trapping) on an invalid command line syntax right? Are you suggesting to not run the destructors if the exit code is nonzero?

@jacobly0
Copy link
Member

jacobly0 commented Jul 30, 2024

Yeah I forgot that calling std.posix.exit calls the destructors when linking libc, not sure how much of a use case there is when you aren't depending on C libraries. In any case, maybe having them both in std.options but disabled by default is the way to go.

@andrewrk
Copy link
Member Author

That seems reasonable. It's enough friction to satisfy me.

@andrewrk andrewrk merged commit f219286 into master Jul 30, 2024
10 checks passed
@andrewrk andrewrk deleted the init-array-start-code branch July 30, 2024 01:40
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.

3 participants