Skip to content

Conversation

@dra27
Copy link
Owner

@dra27 dra27 commented Aug 27, 2025

No description provided.

@dra27 dra27 added relocatable PRs related to the Relocatable Compiler project CI: Full matrix Full CI test matrix labels Aug 27, 2025
@dra27 dra27 force-pushed the launcher-image-handoff branch 2 times, most recently from 7298a18 to bfd98a9 Compare August 27, 2025 16:21
@dra27 dra27 changed the title [Relocatable] Follow-up 8: Fix hand-off of bytecode image from header.c to ocamlrun [Relocatable] Follow-up 7: Fix hand-off of bytecode image from header.c to ocamlrun Aug 28, 2025
@dra27 dra27 force-pushed the launcher-image-handoff branch from bfd98a9 to 6107a9b Compare August 29, 2025 11:30
@dra27 dra27 force-pushed the launcher-image-handoff branch from 6107a9b to 3c294c8 Compare September 9, 2025 21:44
@dra27 dra27 force-pushed the relocatable-testing branch from 3c8b5c1 to 2a7e0ad Compare September 9, 2025 22:19
@dra27 dra27 force-pushed the launcher-image-handoff branch 2 times, most recently from 5b1b3e2 to 8fdfe7e Compare September 10, 2025 08:05
@dra27 dra27 changed the base branch from relocatable-testing to trunk September 13, 2025 13:36
@dra27 dra27 force-pushed the launcher-image-handoff branch from 8fdfe7e to 352f07f Compare September 13, 2025 15:39
@dra27 dra27 force-pushed the launcher-image-handoff branch from 352f07f to 9042fd9 Compare November 9, 2025 08:27
@dra27 dra27 changed the base branch from trunk to relocatable-base-trunk November 19, 2025 14:09
@dra27 dra27 force-pushed the launcher-image-handoff branch from 9042fd9 to c1d2061 Compare November 28, 2025 21:48
@dra27 dra27 changed the base branch from relocatable-base-trunk to relocatable-testing November 30, 2025 14:34
@dra27 dra27 force-pushed the relocatable-testing branch from d644c59 to 63922b2 Compare November 30, 2025 14:36
@dra27 dra27 force-pushed the launcher-image-handoff branch from c1d2061 to 60365c2 Compare November 30, 2025 14:37
@dra27 dra27 force-pushed the relocatable-testing branch from 63922b2 to 198fcfd Compare December 10, 2025 11:10
@dra27 dra27 force-pushed the launcher-image-handoff branch from 60365c2 to 64702cf Compare December 10, 2025 11:23
@dra27 dra27 force-pushed the relocatable-testing branch from 198fcfd to 56b115d Compare December 11, 2025 18:34
@dra27 dra27 force-pushed the launcher-image-handoff branch from 64702cf to 2b669f2 Compare December 11, 2025 18:34
Previously, caml_attempt_open received a pointer to the string and
passed this string to caml_search_exe_in_path. This function allocates a
fresh string which was then assigned to that pointer.

This interface was a little cryptic to follow - in particular, there
were several scenarios in which the result was not being freed. It's
also not entirely clear that caml_attempt_open involved a PATH-search,
which appears to have been applied in error when CAML_DEBUG_FILE was
added.
Bytecode startup includes a check which has no place on Windows.
Makes the behaviour of the Unix and Windows versions of the header
equivalent, in particular it means that if argv[0] doesn't describe the
executable, more things fail for Cygwin.
If a CRT application (including, therefore, another OCaml program),
exec's a bytecode program which uses the executable header, the
cbReserved2 and lpReserved2 fields of the STARTUPINFO structure are
quietly used by the CRT to pass handle information about open fds.

The status checker in the cloexec.ml test has C stubs, which causes
ocamltest to compile the bytecode version with -custom. The test is
split to have an additional intermediate pure OCaml program which simply
repeats either the Unix.execv or Unix.create_process call with the
Sys.argv it was applied. In bytecode, ocamltest compiles this program
just with -use-runtime, which means it uses the executable header on
Windows.

Without the change to header.c in this commit, that test begins to fail
in bytecode on Windows, because the CRT information about inherited
handles is not passed on to the status checker (the HANDLE values will
have been inherited, as that's a kernel function, but the CRT structures
regarding the fds are not initialised, which is what the checker than
looks at).

The fix here simplifies the code considerably - rather than initialising
a fresh STARTUPINFO structure, we simply use GetStartupInfo to retrieve
the one which was used to create the process itself and pass that to
CreateProcess - cbReserved2 and lpReserved2 are therefore passed on, and
the cloexec.ml test passes again.
@dra27 dra27 force-pushed the relocatable-testing branch from 56b115d to 5c4d9f3 Compare December 12, 2025 11:17
@dra27 dra27 force-pushed the launcher-image-handoff branch from 2b669f2 to 8fd5bd9 Compare December 12, 2025 11:17
@dra27 dra27 force-pushed the launcher-image-handoff branch 2 times, most recently from fdcc74a to 57d1e56 Compare December 12, 2025 11:42
The bytecode executable launcher (stdlib/header.c) can _only_ invoke
ocamlrun after it has opened itself in order to find out the name of the
runtime to execute. However, once ocamlrun is exec'd, the knowledge of
this file was previously lost and if it could not be recovered from
argv[0], then execution fails.

This new approach, for both Windows and Unix, instead keeps the fd for
the bytecode image open and passes its number to ocamlrun as
__OCAML_EXEC_FD in the environment. ocamlrun detects this environment
variable and uses that fd to load the bytecode image. If this fails, the
runtime does _not_ fallback to any other mechanisms.

On Windows, it is possible to recover the filename from a HANDLE. It is
not portably possible to do this on Unix, so the filename which was
opened by the stdlib/header.c is instead appended to the environment
variable.
@dra27 dra27 force-pushed the launcher-image-handoff branch from 57d1e56 to 57488db Compare December 12, 2025 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI: Full matrix Full CI test matrix relocatable PRs related to the Relocatable Compiler project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants