Skip to content

Commit ce4eb48

Browse files
authored
[mono][wasm] Add beginnings of support for WASI. (dotnet#59752)
* [mono][wasm] Add beginnings of support for WASI. * Use mono_process_current_pid () instead of getpid (). * Remove MONO_STRICT_IO_EMULATION option. * Fix the build when eventpipe is disabled. * Use HOST_BROWSER instead of HOST_WASM to protect emscripten only code.
1 parent 61c2043 commit ce4eb48

17 files changed

+111
-153
lines changed

src/mono/CMakeLists.txt

+13-2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
234234
# sys/random.h exists, but its not found
235235
set(HAVE_SYS_RANDOM_H 1)
236236
set(INTERNAL_ZLIB 1)
237+
elseif(CMAKE_SYSTEM_NAME STREQUAL "WASI")
238+
set(HOST_WASI 1)
239+
add_definitions(-D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN)
240+
add_definitions(-DNO_GLOBALIZATION_SHIM)
241+
add_definitions(-D_THREAD_SAFE)
242+
set(DISABLE_SHARED_LIBS 1)
243+
set(INTERNAL_ZLIB 1)
244+
set(DISABLE_EXECUTABLES 1)
245+
set(DISABLE_COMPONENTS 1)
237246
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
238247
set(HOST_WIN32 1)
239248
set(EXE_SUFFIX ".exe")
@@ -298,6 +307,8 @@ elseif(TARGET_SYSTEM_NAME STREQUAL "Emscripten")
298307
if (CMAKE_BUILD_TYPE STREQUAL "Release")
299308
add_compile_options(-Os)
300309
endif()
310+
elseif(TARGET_SYSTEM_NAME STREQUAL "WASI")
311+
set(TARGET_WASI 1)
301312
elseif(TARGET_SYSTEM_NAME STREQUAL "Windows")
302313
set(TARGET_WIN32 1)
303314
elseif(TARGET_SYSTEM_NAME STREQUAL "SunOS")
@@ -350,7 +361,7 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
350361
set(NO_UNALIGNED_ACCESS 1)
351362
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "s390x")
352363
set(HOST_S390X 1)
353-
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "wasm")
364+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "wasm" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "wasm32")
354365
set(HOST_WASM 1)
355366
else()
356367
message(FATAL_ERROR "CMAKE_SYSTEM_PROCESSOR='${CMAKE_SYSTEM_PROCESSOR}' not supported.")
@@ -407,7 +418,7 @@ elseif(TARGET_ARCH STREQUAL "s390x")
407418
set(MONO_ARCHITECTURE "\"s390x\"")
408419
set(TARGET_SIZEOF_VOID_P 8)
409420
set(SIZEOF_REGISTER 8)
410-
elseif(TARGET_ARCH STREQUAL "wasm")
421+
elseif(TARGET_ARCH STREQUAL "wasm" OR TARGET_ARCH STREQUAL "wasm32")
411422
set(TARGET_WASM 1)
412423
set(MONO_ARCHITECTURE "\"wasm\"")
413424
set(TARGET_SIZEOF_VOID_P 4)

src/mono/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ run-sample-coreclr:
3030

3131
# build System.Private.CoreLib.dll
3232
bcl corelib:
33-
../.././build.sh -c $(MONO_RUNTIME_CONFIG) -subset Mono.CoreLib
33+
../.././build.sh -c $(MONO_RUNTIME_CONFIG) -subset Mono.CoreLib+Libs.Pretest
3434

3535
# build runtime and copy to artifacts
3636
runtime:

src/mono/cmake/config.h.in

+21
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@
134134
/* Define to 1 if you have the <unistd.h> header file. */
135135
#cmakedefine HAVE_UNISTD_H 1
136136

137+
/* Define to 1 if you have the <signal.h> header file. */
138+
#cmakedefine HAVE_SIGNAL_H 1
139+
140+
/* Define to 1 if you have the <setjmp.h> header file. */
141+
#cmakedefine HAVE_SETJMP_H 1
142+
143+
/* Define to 1 if you have the <syslog.h> header file. */
144+
#cmakedefine HAVE_SYSLOG_H 1
145+
137146
/* Define to 1 if `major', `minor', and `makedev' are declared in <mkdev.h>.
138147
*/
139148
#cmakedefine MAJOR_IN_MKDEV 1
@@ -623,6 +632,12 @@
623632
/* Have access */
624633
#cmakedefine HAVE_ACCESS 1
625634

635+
/* Have getpid */
636+
#cmakedefine HAVE_GETPID 1
637+
638+
/* Have mktemp */
639+
#cmakedefine HAVE_MKTEMP 1
640+
626641
/* Define to 1 if you have the <sys/errno.h> header file. */
627642
#cmakedefine HAVE_SYS_ERRNO_H 1
628643

@@ -839,6 +854,12 @@
839854
/* ... */
840855
#cmakedefine HOST_WASM 1
841856

857+
/* ... */
858+
#cmakedefine HOST_BROWSER 1
859+
860+
/* ... */
861+
#cmakedefine HOST_WASI 1
862+
842863
/* ... */
843864
#cmakedefine HOST_X86 1
844865

src/mono/cmake/configure.cmake

+18-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ if(HOST_SOLARIS)
2020
set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -DGC_SOLARIS_THREADS -DGC_SOLARIS_PTHREADS -D_REENTRANT -D_POSIX_PTHREAD_SEMANTICS -DUSE_MMAP -DUSE_MUNMAP -DHOST_SOLARIS -D__EXTENSIONS__ -D_XPG4_2")
2121
endif()
2222

23+
if(HOST_WASI)
24+
set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN")
25+
endif()
26+
2327
function(ac_check_headers)
2428
foreach(arg ${ARGN})
2529
check_include_file ("${arg}" FOUND_${arg})
@@ -61,8 +65,8 @@ endfunction()
6165
ac_check_headers (
6266
sys/types.h sys/stat.h sys/filio.h sys/sockio.h sys/utime.h sys/un.h sys/syscall.h sys/uio.h sys/param.h sys/sysctl.h
6367
sys/prctl.h sys/socket.h sys/utsname.h sys/select.h sys/user.h sys/poll.h sys/wait.h sts/auxv.h sys/resource.h
64-
sys/ioctl.h sys/errno.h sys/sendfile.h sys/statvfs.h sys/statfs.h sys/mman.h sys/mount.h sys/time.h sys/random.h sys/mman.h
65-
strings.h stdint.h unistd.h netdb.h utime.h semaphore.h libproc.h alloca.h ucontext.h pwd.h elf.h
68+
sys/ioctl.h sys/errno.h sys/sendfile.h sys/statvfs.h sys/statfs.h sys/mman.h sys/mount.h sys/time.h sys/random.h
69+
strings.h stdint.h unistd.h signal.h setjmp.h syslog.h netdb.h utime.h semaphore.h libproc.h alloca.h ucontext.h pwd.h elf.h
6670
gnu/lib-names.h netinet/tcp.h netinet/in.h link.h arpa/inet.h unwind.h poll.h wchar.h linux/magic.h
6771
android/legacy_signal_inlines.h android/ndk-version.h execinfo.h pthread.h pthread_np.h net/if.h dirent.h
6872
CommonCrypto/CommonDigest.h dlfcn.h getopt.h pwd.h iconv.h alloca.h
@@ -77,7 +81,7 @@ ac_check_funcs (
7781
fork execv execve waitpid localtime_r mkdtemp getrandom execvp strlcpy stpcpy strtok_r rewinddir
7882
vasprintf strndup getpwuid_r getprotobyname getprotobyname_r getaddrinfo mach_absolute_time
7983
gethrtime read_real_time gethostbyname gethostbyname2 getnameinfo getifaddrs
80-
access inet_ntop Qp2getifaddrs)
84+
access inet_ntop Qp2getifaddrs getpid mktemp)
8185

8286
if(NOT HOST_DARWIN)
8387
# getentropy was introduced in macOS 10.12 / iOS 10.0
@@ -181,3 +185,14 @@ if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
181185
set(HAVE_SYS_SYSCTL_H 1)
182186
set(HAVE_SYS_USER_H 1)
183187
endif()
188+
189+
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
190+
# Redirected to errno.h
191+
set(SYS_ERRNO_H 0)
192+
# Some headers exist, but don't compile (wasi sdk 12.0)
193+
set(HAVE_SYS_SOCKET_H 0)
194+
set(HAVE_SYS_UN_H 0)
195+
set(HAVE_NETINET_IN_H 0)
196+
set(HAVE_NETINET_TCP_H 0)
197+
set(HAVE_ARPA_INET_H 0)
198+
endif()

src/mono/mono/component/debugger-agent.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include <mono/utils/mono-proclib.h>
8080
#include <mono/utils/w32api.h>
8181
#include <mono/utils/mono-logger-internals.h>
82+
#include <mono/utils/mono-proclib.h>
8283

8384
#include <mono/component/debugger-state-machine.h>
8485
#include "debugger-agent.h"
@@ -9513,7 +9514,7 @@ create_file_to_check_memory_address (void)
95139514
{
95149515
if (file_check_valid_memory != -1)
95159516
return;
9516-
char *file_name = g_strdup_printf ("debugger_check_valid_memory.%d", getpid());
9517+
char *file_name = g_strdup_printf ("debugger_check_valid_memory.%d", mono_process_current_pid ());
95179518
filename_check_valid_memory = g_build_filename (g_get_tmp_dir (), file_name, (const char*)NULL);
95189519
file_check_valid_memory = open(filename_check_valid_memory, O_CREAT | O_WRONLY | O_APPEND, S_IWUSR);
95199520
g_free (file_name);

src/mono/mono/component/debugger-stub.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ stub_send_enc_delta (MonoImage *image, gconstpointer dmeta_bytes, int32_t dmeta_
204204
{
205205
}
206206

207-
#ifdef HOST_WASM
207+
#ifdef HOST_BROWSER
208208

209209
#include <emscripten.h>
210210

@@ -229,6 +229,4 @@ mono_wasm_send_dbg_command (int id, int command_set, int command, guint8* data,
229229
return false;
230230
}
231231

232-
#endif // HOST_WASM
233-
234-
232+
#endif // HOST_BROWSER

src/mono/mono/eventpipe/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
if(ENABLE_PERFTRACING)
1+
include(${MONO_EVENTPIPE_SHIM_SOURCE_PATH}/gen-eventing.cmake)
22

3-
include(${MONO_EVENTPIPE_SHIM_SOURCE_PATH}/gen-eventing.cmake)
3+
if(ENABLE_PERFTRACING)
44

55
if (FEATURE_PERFTRACING_PAL_TCP)
66
add_definitions(-DENABLE_PERFTRACING_PAL_TCP)

src/mono/mono/eventpipe/gen-eventing.cmake

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,7 @@ if(ENABLE_PERFTRACING)
9494

9595
add_custom_target(${MONO_DIAGNOSTICS_TRACING_COMPONENT_NAME}-gen-sources
9696
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${MONO_DIAGNOSTICS_TRACING_COMPONENT_NAME}-gen-sources.timestamp ${MONO_DIAGNOSTICS_TRACING_COMPONENT_NAME}-gen-headers)
97-
97+
else(ENABLE_PERFTRACING)
98+
add_custom_target(${MONO_DIAGNOSTICS_TRACING_COMPONENT_NAME}-gen-headers)
99+
add_custom_target(${MONO_DIAGNOSTICS_TRACING_COMPONENT_NAME}-gen-sources)
98100
endif(ENABLE_PERFTRACING)

src/mono/mono/metadata/assembly.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <mono/utils/mono-digest.h>
4040
#include <mono/utils/mono-logger-internals.h>
4141
#include <mono/utils/mono-path.h>
42+
#include <mono/utils/mono-proclib.h>
4243
#include <mono/metadata/reflection.h>
4344
#include <mono/metadata/coree.h>
4445
#include <mono/metadata/cil-coff.h>
@@ -594,7 +595,7 @@ mono_set_rootdir (void)
594595
}
595596

596597
/* Solaris 10 style */
597-
str = g_strdup_printf ("/proc/%d/path/a.out", getpid ());
598+
str = g_strdup_printf ("/proc/%d/path/a.out", mono_process_current_pid ());
598599

599600
#if defined(HAVE_READLINK)
600601
s = readlink (str, buf, sizeof (buf)-1);

src/mono/mono/metadata/object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7851,7 +7851,7 @@ format_cmd_line (int argc, char **argv, gboolean add_host)
78517851
GString *cmd_line = NULL;
78527852

78537853
if (add_host) {
7854-
#if !defined(HOST_WIN32) && defined(HAVE_UNISTD_H)
7854+
#if !defined(HOST_WIN32) && defined(HAVE_GETPID)
78557855
host_path = mono_w32process_get_path (getpid ());
78567856
#elif defined(HOST_WIN32)
78577857
gunichar2 *host_path_ucs2 = NULL;

src/mono/mono/metadata/w32file-unix.c

-115
Original file line numberDiff line numberDiff line change
@@ -277,93 +277,6 @@ _wapi_dirname (const gchar *filename)
277277
return ret;
278278
}
279279

280-
static gboolean
281-
_wapi_lock_file_region (gint fd, off_t offset, off_t length)
282-
{
283-
struct flock lock_data;
284-
gint ret;
285-
286-
if (offset < 0 || length < 0) {
287-
mono_w32error_set_last (ERROR_INVALID_PARAMETER);
288-
return FALSE;
289-
}
290-
291-
lock_data.l_type = F_WRLCK;
292-
lock_data.l_whence = SEEK_SET;
293-
lock_data.l_start = offset;
294-
lock_data.l_len = length;
295-
296-
do {
297-
ret = fcntl (fd, F_SETLK, &lock_data);
298-
} while(ret == -1 && errno == EINTR);
299-
300-
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: fcntl returns %d", __func__, ret);
301-
302-
if (ret == -1) {
303-
/*
304-
* if locks are not available (NFS for example),
305-
* ignore the error
306-
*/
307-
if (errno == ENOLCK
308-
#ifdef EOPNOTSUPP
309-
|| errno == EOPNOTSUPP
310-
#endif
311-
#ifdef ENOTSUP
312-
|| errno == ENOTSUP
313-
#endif
314-
) {
315-
return TRUE;
316-
}
317-
318-
mono_w32error_set_last (ERROR_LOCK_VIOLATION);
319-
return FALSE;
320-
}
321-
322-
return TRUE;
323-
}
324-
325-
static gboolean
326-
_wapi_unlock_file_region (gint fd, off_t offset, off_t length)
327-
{
328-
struct flock lock_data;
329-
gint ret;
330-
331-
lock_data.l_type = F_UNLCK;
332-
lock_data.l_whence = SEEK_SET;
333-
lock_data.l_start = offset;
334-
lock_data.l_len = length;
335-
336-
do {
337-
ret = fcntl (fd, F_SETLK, &lock_data);
338-
} while(ret == -1 && errno == EINTR);
339-
340-
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: fcntl returns %d", __func__, ret);
341-
342-
if (ret == -1) {
343-
/*
344-
* if locks are not available (NFS for example),
345-
* ignore the error
346-
*/
347-
if (errno == ENOLCK
348-
#ifdef EOPNOTSUPP
349-
|| errno == EOPNOTSUPP
350-
#endif
351-
#ifdef ENOTSUP
352-
|| errno == ENOTSUP
353-
#endif
354-
) {
355-
return TRUE;
356-
}
357-
358-
mono_w32error_set_last (ERROR_LOCK_VIOLATION);
359-
return FALSE;
360-
}
361-
362-
return TRUE;
363-
}
364-
365-
static gboolean lock_while_writing = FALSE;
366-
367280
static void
368281
_wapi_set_last_error_from_errno (void)
369282
{
@@ -402,7 +315,6 @@ static gboolean
402315
file_write (FileHandle *filehandle, gpointer buffer, guint32 numbytes, guint32 *byteswritten)
403316
{
404317
gint ret;
405-
off_t current_pos = 0;
406318
MonoThreadInfo *info = mono_thread_info_current ();
407319

408320
if(byteswritten!=NULL) {
@@ -415,37 +327,13 @@ file_write (FileHandle *filehandle, gpointer buffer, guint32 numbytes, guint32 *
415327
mono_w32error_set_last (ERROR_ACCESS_DENIED);
416328
return(FALSE);
417329
}
418-
419-
if (lock_while_writing) {
420-
/* Need to lock the region we're about to write to,
421-
* because we only do advisory locking on POSIX
422-
* systems
423-
*/
424-
MONO_ENTER_GC_SAFE;
425-
current_pos = lseek (((MonoFDHandle*) filehandle)->fd, (off_t)0, SEEK_CUR);
426-
MONO_EXIT_GC_SAFE;
427-
if (current_pos == -1) {
428-
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: fd %d lseek failed: %s", __func__, ((MonoFDHandle*) filehandle)->fd, g_strerror (errno));
429-
_wapi_set_last_error_from_errno ();
430-
return(FALSE);
431-
}
432-
433-
if (_wapi_lock_file_region (((MonoFDHandle*) filehandle)->fd, current_pos, numbytes) == FALSE) {
434-
/* The error has already been set */
435-
return(FALSE);
436-
}
437-
}
438330

439331
do {
440332
MONO_ENTER_GC_SAFE;
441333
ret = write (((MonoFDHandle*) filehandle)->fd, buffer, numbytes);
442334
MONO_EXIT_GC_SAFE;
443335
} while (ret == -1 && errno == EINTR &&
444336
!mono_thread_info_is_interrupt_state (info));
445-
446-
if (lock_while_writing) {
447-
_wapi_unlock_file_region (((MonoFDHandle*) filehandle)->fd, current_pos, numbytes);
448-
}
449337

450338
if (ret == -1) {
451339
if (errno == EINTR) {
@@ -650,9 +538,6 @@ mono_w32file_init (void)
650538
mono_fdhandle_register (MONO_FDTYPE_PIPE, &file_data_callbacks);
651539

652540
mono_coop_mutex_init (&file_share_mutex);
653-
654-
if (g_hasenv ("MONO_STRICT_IO_EMULATION"))
655-
lock_while_writing = TRUE;
656541
}
657542

658543
gpointer

0 commit comments

Comments
 (0)