roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.02k stars 286 forks source link

memexec + mingw crash on windows #4372

Open folkertdev opened 1 year ago

folkertdev commented 1 year ago

here is a rust binary that combines two zig programs with our surgical linker, and then memexecs the result:

binary to reproduce

the source code for that executable is here

mstorsjo commented 1 year ago

FWIW I did try to have a look at this, but the executable in the package doesn't run for me:

>linker-executable.exe
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: NotFound, message: "program not found" }', crates\linker-executable\src\main.rs:47:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Anyway, I did have a look at the error message which was mentioned on irc:

     Mingw-w64 runtime failure:
     Address 000000014000105C has no image-section

This comes from the mingw-w64 pseudo-relocation code, wanting to update addresses in the executable, whereby it wants to remap certain sections from read-only to read-write to patch things up in it. In order to do that, it needs to find what section the address belongs to. It does this by traversing the PE image header found by the __ImageBase symbol: https://github.com/mingw-w64/mingw-w64/blob/master/mingw-w64-crt/crt/pesect.c#L103-L108 And iterating over the sections: https://github.com/mingw-w64/mingw-w64/blob/master/mingw-w64-crt/crt/pesect.c#L51-L60

As for why you're hitting this when executing the combined executable it with memexec but not when executing it normally, I'm not quite sure - it sounds like some corner case is missed either in memexec or the merging of files. The address 000000014000105C looks like the executable is loaded at its desired address - so base relocation rewriting shouldn't really play any role either, although that's a big contender for what could break.

If there'd be a standalone testable setup (two minimal C based trivial executables that are merged, and a way to re-do the merging, and a way to run it with memexec) I would try to add suitable debugging in the merged executables to figure out what's going wrong.

But anyway, as you mentioned this isn't blocking you any more, feel free to disregard.