On Tuesday 06 May 2025 20:06:18 Martin Storsjö wrote:
> On Thu, 17 Apr 2025, Pali Rohár wrote:
> 
> > This will ensure that _pei386_runtime_relocator() is called as the first
> > function and that all those runtime init code functions are called in
> > guarded block which prevents duplicate code execution.
> > ---
> 
> About the purpose of these patches, the "which prevents duplicate code
> execution" - is that specifically a target of this patch? Because as far as
> I remember (or maybe I misremember?), last time we discussed such things, we
> couldn't really come up with even a theoretical case when the guard actually
> would be needed?

We have been discussing it about the dll initialization code and for dll
I think we really have not figured out any theoretical case.

But for exe initialization code I think that this could be triggered by
creating a new thread and setting the thread entry point to the PE EXE
entry point.

Theoretical test case for exe:
- exe starts, does whole initialization in crtexe.c which sets
  __native_startup_state to __initialized and executes main()
- main() creates new thread which starts executing PE entry point
- new thread now:
- takes __native_startup_lock
  -> before executing main it was released by the main thread
     so new thread instantly takes it
- check for __native_startup_state
  -> it is already __initialized, so it will skip all guarded code
  -> it has explicit "else" case when it is not __initializing and
     also not __uninitialized
- and also executes main again

So I think think that here the guarded code prevents duplicate
execution for this theoretical case.

> Then again, I presume the msvc startup code has something similar, and I
> guess it has been there for some reason at some point in time at least.

This is mingw specific code, so it is harder to compare it. But msvc
calls everything inside those guards.

> And I guess it makes things more consistent if all initialization is within
> the same guarded block...
> 
> // Martin

Yes, I think it is better to have consistent initialization.


_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to