Skip to content

Commit a085400

Browse files
committed
[wasi] CoreLib load: exepath via CORE_ROOT, file mapping via open()
* minipal_getexepath: split TARGET_WASM into TARGET_BROWSER (unchanged, returns '/') and TARGET_WASI (returns CORE_ROOT + '/corerun.wasm'). SystemDomain::Init derives the system directory from the exe path; on WASI argv[0] is unreliable so we use the host-provided CORE_ROOT. * pal/src/map/map.cpp InternalCreateFileMapping: WASI has no dup() and rejects fcntl(F_DUPFD_CLOEXEC). Re-open the file by its path so the mapping owns a separate fd. Same fix pattern as init_std_handle. With these in place HelloWorld now loads System.Private.CoreLib, binds the default app domain, and reaches the CILInterp path. It currently asserts inside PortableEntryPoint::GetActualCode while compiling an ldftn (real interpreter integration work, no longer PAL plumbing). Co-authored-by: Copilot <[email protected]>
1 parent e4b0360 commit a085400

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎src/coreclr/pal/src/map/map.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,14 @@ CorUnix::InternalCreateFileMapping(
466466
// information, though...
467467
//
468468

469+
#ifdef TARGET_WASI
470+
// WASI has no dup() and rejects F_DUPFD_CLOEXEC. Re-open the file
471+
// by its path so the mapping owns a separate fd that survives the
472+
// caller closing the handle.
473+
UnixFd = open(pFileLocalData->unix_filename, O_RDONLY);
474+
#else
469475
UnixFd = fcntl(pFileLocalData->unix_fd, F_DUPFD_CLOEXEC, 0); // dup, but with CLOEXEC
476+
#endif
470477
if (-1 == UnixFd)
471478
{
472479
ERROR( "Unable to duplicate the Unix file descriptor!\n" );

‎src/native/minipal/getexepath.h‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#elif defined(__HAIKU__)
2121
#include <FindDirectory.h>
2222
#include <StorageDefs.h>
23+
#elif defined(TARGET_WASI)
24+
#include <string.h>
2325
#elif HAVE_GETAUXVAL
2426
#include <sys/auxv.h>
2527
#endif
@@ -83,9 +85,30 @@ static inline char* minipal_getexepath(void)
8385
}
8486

8587
return strdup(path);
86-
#elif defined(TARGET_WASM)
88+
#elif defined(TARGET_BROWSER)
8789
const char *browserVirtualAppBase = "/"; // keep in sync other places that define browserVirtualAppBase
8890
return strdup(browserVirtualAppBase);
91+
#elif defined(TARGET_WASI)
92+
// WASI has no /proc, no AT_EXECFN, and argv[0] is unreliable (often "/").
93+
// corerun.wasm is launched with the CORE_ROOT env var set to the directory
94+
// that holds CoreCLR (System.Private.CoreLib.dll and friends). The PAL only
95+
// needs a path whose dirname is that directory, so synthesize one here.
96+
const char* coreRoot = getenv("CORE_ROOT");
97+
if (coreRoot == NULL || coreRoot[0] == '\0')
98+
{
99+
return strdup("/");
100+
}
101+
size_t coreRootLen = strlen(coreRoot);
102+
const char* suffix = "/corerun.wasm";
103+
size_t suffixLen = strlen(suffix);
104+
char* result = (char*)malloc(coreRootLen + suffixLen + 1);
105+
if (result == NULL)
106+
{
107+
return NULL;
108+
}
109+
memcpy(result, coreRoot, coreRootLen);
110+
memcpy(result + coreRootLen, suffix, suffixLen + 1);
111+
return result;
89112
#else
90113
#ifdef __linux__
91114
const char* symlinkEntrypointExecutable = "/proc/self/exe";

0 commit comments

Comments
 (0)