Skip to content

Commit 605e1ad

Browse files
[compiler-rt][profile] Add initial support for WebAssembly/WASI
This patch adds initial support for WebAssembly/WASI to the profile runtime library on the top of wasi-libc. This is a part of the ongoing patch series to add coverage support for WebAssembly/WASI. The patch includes the following changes: * Add wasm32-wasi to the list of supported architectures/OSes. * Exclude unsupported features for WASI: flock, madvise, uname. * Enable some user-space emulation provided by wasi-libc: mmap, getpid
1 parent e8f01b0 commit 605e1ad

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ set(ALL_HWASAN_SUPPORTED_ARCH ${X86_64} ${ARM64} ${RISCV64})
7777
set(ALL_MEMPROF_SUPPORTED_ARCH ${X86_64})
7878
set(ALL_PROFILE_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${PPC32} ${PPC64}
7979
${MIPS32} ${MIPS64} ${S390X} ${SPARC} ${SPARCV9} ${HEXAGON}
80-
${RISCV32} ${RISCV64} ${LOONGARCH64})
80+
${RISCV32} ${RISCV64} ${LOONGARCH64} ${WASM32})
8181
set(ALL_CTX_PROFILE_SUPPORTED_ARCH ${X86_64})
8282
if (OS_NAME MATCHES "FreeBSD")
8383
set(ALL_TSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64})

compiler-rt/cmake/config-ix.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ else()
822822
endif()
823823

824824
if (PROFILE_SUPPORTED_ARCH AND NOT LLVM_USE_SANITIZER AND
825-
OS_NAME MATCHES "Darwin|Linux|FreeBSD|Windows|Android|Fuchsia|SunOS|NetBSD|AIX")
825+
OS_NAME MATCHES "Darwin|Linux|FreeBSD|Windows|Android|Fuchsia|SunOS|NetBSD|AIX|WASI")
826826
set(COMPILER_RT_HAS_PROFILE TRUE)
827827
else()
828828
set(COMPILER_RT_HAS_PROFILE FALSE)

compiler-rt/lib/profile/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ int main() {
3838
3939
" COMPILER_RT_TARGET_HAS_FCNTL_LCK)
4040

41+
CHECK_CXX_SOURCE_COMPILES("
42+
#include <sys/file.h>
43+
44+
int fd;
45+
int main() {
46+
flock(fd, LOCK_EX);
47+
return 0;
48+
}
49+
50+
" COMPILER_RT_TARGET_HAS_FLOCK)
51+
4152
CHECK_CXX_SOURCE_COMPILES("
4253
#include <sys/utsname.h>
4354
int main() {
@@ -93,6 +104,13 @@ if(FUCHSIA OR UNIX)
93104
-Wno-pedantic)
94105
endif()
95106

107+
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
108+
set(EXTRA_FLAGS
109+
${EXTRA_FLAGS}
110+
-D_WASI_EMULATED_MMAN
111+
-D_WASI_EMULATED_GETPID)
112+
endif()
113+
96114
if(COMPILER_RT_TARGET_HAS_ATOMICS)
97115
set(EXTRA_FLAGS
98116
${EXTRA_FLAGS}
@@ -105,6 +123,12 @@ if(COMPILER_RT_TARGET_HAS_FCNTL_LCK)
105123
-DCOMPILER_RT_HAS_FCNTL_LCK=1)
106124
endif()
107125

126+
if(COMPILER_RT_TARGET_HAS_FLOCK)
127+
set(EXTRA_FLAGS
128+
${EXTRA_FLAGS}
129+
-DCOMPILER_RT_HAS_FLOCK=1)
130+
endif()
131+
108132
if(COMPILER_RT_TARGET_HAS_UNAME)
109133
set(EXTRA_FLAGS
110134
${EXTRA_FLAGS}

compiler-rt/lib/profile/GCDAProfiling.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ void llvm_reset_counters(void) {
584584
}
585585
}
586586

587-
#if !defined(_WIN32)
587+
#if !defined(_WIN32) && !defined(__wasi__)
588588
COMPILER_RT_VISIBILITY
589589
pid_t __gcov_fork() {
590590
pid_t parent_pid = getpid();

compiler-rt/lib/profile/InstrProfilingPort.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#endif
5555

5656
#define COMPILER_RT_MAX_HOSTLEN 128
57-
#ifdef __ORBIS__
57+
#if defined(__ORBIS__) || defined(__wasi__)
5858
#define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))
5959
#else
6060
#define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)

compiler-rt/lib/profile/InstrProfilingUtil.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ COMPILER_RT_VISIBILITY int lprofLockFd(int fd) {
152152
}
153153
}
154154
return 0;
155-
#else
155+
#elif defined(COMPILER_RT_HAS_FLOCK)
156156
flock(fd, LOCK_EX);
157157
return 0;
158+
#else
159+
return 0;
158160
#endif
159161
}
160162

@@ -177,9 +179,11 @@ COMPILER_RT_VISIBILITY int lprofUnlockFd(int fd) {
177179
}
178180
}
179181
return 0;
180-
#else
182+
#elif defined(COMPILER_RT_HAS_FLOCK)
181183
flock(fd, LOCK_UN);
182184
return 0;
185+
#else
186+
return 0;
183187
#endif
184188
}
185189

@@ -353,8 +357,8 @@ COMPILER_RT_VISIBILITY void lprofRestoreSigKill(void) {
353357

354358
COMPILER_RT_VISIBILITY int lprofReleaseMemoryPagesToOS(uintptr_t Begin,
355359
uintptr_t End) {
356-
#if defined(__ve__)
357-
// VE doesn't support madvise.
360+
#if defined(__ve__) || defined(__wasi__)
361+
// VE and WASI doesn't support madvise.
358362
return 0;
359363
#else
360364
size_t PageSize = getpagesize();

0 commit comments

Comments
 (0)