bitwiseworks / libc

LIBC Next (kLIBC fork)
9 stars 4 forks source link

calls to _CRT_init and _CRT_term are not balanced when booting via F2 #141

Open lerdmann opened 1 month ago

lerdmann commented 1 month ago

I booted up to a commandline by hitting F2. My intention was to use ZIP.EXE, built with libc. The effect was that just invoking ZIP.EXE without any parameters would make ZIP.EXE hang. I waited for 30 seconds, then I hit Ctrl-C which properly ended the program. When I boot up normally, that is, with Presentation Manager active, I can run ZIP.EXE without a problem.

I enabled libc logging for the F2 boot and had a look at the log.

I am running into this error condition:

https://github.com/bitwiseworks/libc/blame/7a15da98a2c81aeb6341859bf5409586eea19241/src/emx/src/lib/startup/startup.c#L116

This is because of fork support: libc/src/emx/src/lib/startup/386/dll0.s DLL standard entry point skips calling _DLL_InitTerm on DLL initialization if an instance is forked: https://github.com/bitwiseworks/libc/blob/7a15da98a2c81aeb6341859bf5409586eea19241/src/emx/src/lib/startup/386/dll0.s#L80

However, it is _DLL_InitTerm that eventually calls _CRT_init or _CRT_term (depending on if a DLL is initialized or terminates). _CRT_term is eventually called more often than _CRT_init which makes "cRefs" become negative.

Either _DLL_InitTerm also needs to be invoked for the forked process or the reference counting needs to be reworked.

Or, more likely, in DLL standard entry point, on forking, we might skip __init_dll on DLL initialization but we shall not skip _DLL_InitTerm. That would also ensure that if one has a customized _DLL_InitTerm routine that this customization becomes active not only in the original process but also in the forked process.

lerdmann commented 1 month ago

66b72545-004d-ZIP-libc_normalboot.log 66b72609-0006-ZIP-libc_f2boot.log

To better illustrate the problem, find attached log files where I booted normally and where I booted via F2. In both cases, I ran the ZIP.EXE program.

lerdmann commented 1 month ago

I think part of the problem stems from the fact, that both, "abort" and "exit" directly call _CRT_term without _CRT_init having been called correspondingly (circumventing the call to _DLL_InitTerm): https://github.com/bitwiseworks/libc/blob/7a15da98a2c81aeb6341859bf5409586eea19241/src/emx/src/lib/misc/abort.c#L48 https://github.com/bitwiseworks/libc/blob/7a15da98a2c81aeb6341859bf5409586eea19241/src/emx/src/lib/startup/exit.c#L85