Skip to content

Commit 580c7c6

Browse files
committed
[compiler-rt] Avoid using musl's syscall layer. NFC
Use libc calls instead, similar to the NetBSD and macOS implementations.
1 parent b0c461c commit 580c7c6

File tree

6 files changed

+137
-110
lines changed

6 files changed

+137
-110
lines changed

system/lib/compiler-rt/lib/sanitizer_common/sanitizer_emscripten.cpp

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@
1414
//===----------------------------------------------------------------------===//
1515

1616
#include "sanitizer_platform.h"
17+
#include "sanitizer_platform_limits_posix.h"
1718
#include "sanitizer_common.h"
1819
#include "sanitizer_stoptheworld.h"
1920

21+
#include <fcntl.h>
2022
#include <signal.h>
2123
#include <time.h>
24+
#include <unistd.h>
25+
#include <math.h>
2226

2327
#if SANITIZER_EMSCRIPTEN
2428

29+
#include <sys/stat.h>
30+
#include <sys/types.h>
31+
2532
#include <emscripten.h>
2633
#include <emscripten/stack.h>
27-
#include <sys/types.h>
34+
#include <wasi/api.h>
35+
#include <wasi/wasi-helpers.h>
2836

2937
#include "emscripten_internal.h"
3038

@@ -128,6 +136,92 @@ u64 MonotonicNanoTime() {
128136

129137
void GetMemoryProfile(fill_profile_f cb, uptr *stats) {}
130138

139+
int internal_madvise(uptr addr, uptr length, int advice) {
140+
return 0; // madvise is currently ignored
141+
}
142+
143+
uptr internal_close(fd_t fd) {
144+
return __wasi_fd_close(fd);
145+
}
146+
147+
uptr internal_open(const char *filename, int flags) {
148+
return open(filename, flags);
149+
}
150+
151+
uptr internal_open(const char *filename, int flags, u32 mode) {
152+
return open(filename, flags, mode);
153+
}
154+
155+
uptr internal_read(fd_t fd, void *buf, uptr count) {
156+
__wasi_iovec_t iov = { (uint8_t*)buf, count };
157+
size_t num;
158+
if (__wasi_syscall_ret(__wasi_fd_read(fd, &iov, 1, &num))) {
159+
return -1;
160+
}
161+
return num;
162+
}
163+
164+
uptr internal_write(fd_t fd, const void *buf, uptr count) {
165+
__wasi_ciovec_t iov = { (const uint8_t*)buf, count };
166+
size_t num;
167+
if (__wasi_syscall_ret(__wasi_fd_write(fd, &iov, 1, &num))) {
168+
return -1;
169+
}
170+
return num;
171+
}
172+
173+
uptr internal_stat(const char *path, void *buf) {
174+
return stat(path, (struct stat *)buf);
175+
}
176+
177+
uptr internal_fstat(fd_t fd, void *buf) {
178+
return fstat(fd, (struct stat *)buf);
179+
}
180+
181+
uptr internal_filesize(fd_t fd) {
182+
struct stat st;
183+
if (internal_fstat(fd, &st))
184+
return -1;
185+
return (uptr)st.st_size;
186+
}
187+
188+
uptr internal_dup(int oldfd) {
189+
return dup(oldfd);
190+
}
191+
192+
uptr internal_getpid() {
193+
return 42;
194+
}
195+
196+
uptr internal_sched_yield() {
197+
return sched_yield();
198+
}
199+
200+
void internal_sigfillset(__sanitizer_sigset_t *set) {
201+
sigfillset(set);
202+
}
203+
204+
uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
205+
__sanitizer_sigset_t *oldset) {
206+
return sigprocmask(how, set, oldset);
207+
}
208+
209+
void internal_usleep(u64 useconds) {
210+
usleep(useconds);
211+
}
212+
213+
void internal__exit(int exitcode) {
214+
__wasi_proc_exit(exitcode);
215+
}
216+
217+
tid_t GetTid() {
218+
return gettid();
219+
}
220+
221+
uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
222+
return clock_gettime(clk_id, (struct timespec *)tp);
223+
}
224+
131225
} // namespace __sanitizer
132226

133227
#endif

0 commit comments

Comments
 (0)