Skip to content

Commit c05a2e3

Browse files
committed
Try to fix sanitizer tests
1 parent 1d01e06 commit c05a2e3

File tree

2 files changed

+79
-32
lines changed

2 files changed

+79
-32
lines changed

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

Lines changed: 73 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

@@ -127,6 +135,70 @@ u64 MonotonicNanoTime() {
127135
return (u64)ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
128136
}
129137

138+
uptr internal_close(fd_t fd) {
139+
return __wasi_fd_close(fd);
140+
}
141+
142+
uptr internal_open(const char* filename, int flags) {
143+
return open(filename, flags);
144+
}
145+
146+
uptr internal_open(const char* filename, int flags, u32 mode) {
147+
return open(filename, flags, mode);
148+
}
149+
150+
uptr internal_read(fd_t fd, void* buf, uptr count) {
151+
__wasi_iovec_t iov = { (uint8_t*)buf, count };
152+
size_t num;
153+
if (__wasi_syscall_ret(__wasi_fd_read(fd, &iov, 1, &num))) {
154+
return -1;
155+
}
156+
return num;
157+
}
158+
159+
uptr internal_write(fd_t fd, const void* buf, uptr count) {
160+
__wasi_ciovec_t iov = { (const uint8_t*)buf, count };
161+
size_t num;
162+
if (__wasi_syscall_ret(__wasi_fd_write(fd, &iov, 1, &num))) {
163+
return -1;
164+
}
165+
return num;
166+
}
167+
168+
uptr internal_stat(const char* path, void* buf) {
169+
return stat(path, (struct stat *)buf);
170+
}
171+
172+
uptr internal_dup(int oldfd) {
173+
return dup(oldfd);
174+
}
175+
176+
uptr internal_getpid() {
177+
return 42;
178+
}
179+
180+
uptr internal_sched_yield() {
181+
return 0;
182+
}
183+
184+
void internal_usleep(u64 useconds) {
185+
usleep(useconds);
186+
}
187+
188+
void internal__exit(int exitcode) {
189+
__wasi_proc_exit(exitcode);
190+
}
191+
192+
tid_t GetTid() {
193+
return gettid();
194+
}
195+
196+
extern "C" int __clock_gettime(__sanitizer_clockid_t clk_id, void* tp);
197+
198+
uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void* tp) {
199+
return __clock_gettime(clk_id, tp);
200+
}
201+
130202
} // namespace __sanitizer
131203

132204
#endif

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ extern struct ps_strings *__ps_strings;
109109
#endif
110110

111111
#if SANITIZER_EMSCRIPTEN
112-
#include <emscripten/threading.h>
113-
#include <math.h>
114-
#include <wasi/api.h>
115-
#include <wasi/wasi-helpers.h>
112+
#include <math.h> // For INFINITY
113+
#include <emscripten/threading.h> // For emscripten_futex_wait
116114
#endif
117115

118116
extern char **environ;
@@ -271,19 +269,10 @@ uptr internal_read(fd_t fd, void *buf, uptr count) {
271269
}
272270

273271
uptr internal_write(fd_t fd, const void *buf, uptr count) {
274-
#if SANITIZER_EMSCRIPTEN
275-
__wasi_ciovec_t iov = { (const uint8_t *)buf, count };
276-
size_t num;
277-
if (__wasi_syscall_ret(__wasi_fd_write(fd, &iov, 1, &num))) {
278-
return -1;
279-
}
280-
return num;
281-
#else
282272
sptr res;
283273
HANDLE_EINTR(res,
284274
(sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf, count));
285275
return res;
286-
#endif
287276
}
288277

289278
uptr internal_ftruncate(fd_t fd, uptr size) {
@@ -536,18 +525,16 @@ uptr internal_execve(const char *filename, char *const argv[],
536525
}
537526
#endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD && !SANITIZER_EMSCRIPTEN
538527

539-
#if !SANITIZER_NETBSD
528+
#if !SANITIZER_NETBSD && !SANITIZER_EMSCRIPTEN
540529
void internal__exit(int exitcode) {
541-
#if SANITIZER_EMSCRIPTEN
542-
__wasi_proc_exit(exitcode);
543-
#elif SANITIZER_FREEBSD || SANITIZER_SOLARIS
530+
#if SANITIZER_FREEBSD || SANITIZER_SOLARIS
544531
internal_syscall(SYSCALL(exit), exitcode);
545532
#else
546533
internal_syscall(SYSCALL(exit_group), exitcode);
547534
#endif
548535
Die(); // Unreachable.
549536
}
550-
#endif // !SANITIZER_NETBSD
537+
#endif // !SANITIZER_NETBSD && !SANITIZER_EMSCRIPTEN
551538

552539
// ----------------- sanitizer_common.h
553540
bool FileExists(const char *filename) {
@@ -567,16 +554,14 @@ bool DirExists(const char *path) {
567554
return S_ISDIR(st.st_mode);
568555
}
569556

570-
# if !SANITIZER_NETBSD
557+
# if !SANITIZER_NETBSD && !SANITIZER_EMSCRIPTEN
571558
tid_t GetTid() {
572559
#if SANITIZER_FREEBSD
573560
long Tid;
574561
thr_self(&Tid);
575562
return Tid;
576563
#elif SANITIZER_SOLARIS
577564
return thr_self();
578-
#elif SANITIZER_EMSCRIPTEN
579-
return gettid();
580565
#else
581566
return internal_syscall(SYSCALL(gettid));
582567
#endif
@@ -615,16 +600,6 @@ u64 NanoTime() {
615600
}
616601
#endif
617602

618-
#if SANITIZER_EMSCRIPTEN
619-
extern "C" {
620-
int __clock_gettime(__sanitizer_clockid_t clk_id, void *tp);
621-
}
622-
623-
uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
624-
return __clock_gettime(clk_id, tp);
625-
}
626-
#endif
627-
628603
// Like getenv, but reads env directly from /proc (on Linux) or parses the
629604
// 'environ' array (on some others) and does not use libc. This function
630605
// should be called first inside __asan_init.

0 commit comments

Comments
 (0)