Skip to content

Commit de1dd5f

Browse files
authored
Add downstream changes from emscripten (#16)
This includes all downstream changes made so far, including both the changes carried over from emscripten-libs-19 and the ones that were added since then. This also includes changes from the newly included emscripten's `llvm-libc` directory (which is our copy of `libc`).
1 parent ec28b8f commit de1dd5f

File tree

75 files changed

+738
-156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+738
-156
lines changed

compiler-rt/lib/asan/asan_errors.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,17 @@ ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
508508
scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr);
509509
if (far_from_bounds) scariness.Scare(10, "far-from-bounds");
510510
}
511+
#if SANITIZER_EMSCRIPTEN
512+
// If address is in the first page (64 KB), then it is likely that the
513+
// access is a result of a null pointer dereference.
514+
else if (addr < 65536) {
515+
bug_descr = "null-pointer-dereference";
516+
scariness.Scare(25, bug_descr);
517+
} else if (AddrIsInShadow(addr)) {
518+
bug_descr = "shadow-access";
519+
scariness.Scare(25, bug_descr);
520+
}
521+
#endif
511522
}
512523
}
513524

compiler-rt/lib/asan/asan_flags.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
#include "ubsan/ubsan_flags.h"
2525
#include "ubsan/ubsan_platform.h"
2626

27+
#if SANITIZER_EMSCRIPTEN
28+
#include <emscripten/heap.h>
29+
#include "emscripten_internal.h"
30+
#endif
31+
32+
2733
namespace __asan {
2834

2935
Flags asan_flags_dont_use_directly; // use via flags().
@@ -70,7 +76,11 @@ static void InitializeDefaultFlags() {
7076
CommonFlags cf;
7177
cf.CopyFrom(*common_flags());
7278
cf.detect_leaks = cf.detect_leaks && CAN_SANITIZE_LEAKS;
79+
#if !SANITIZER_EMSCRIPTEN
80+
// getenv on emscripten uses malloc, which we can't when using LSan.
81+
// You can't run external symbolizer executables anyway.
7382
cf.external_symbolizer_path = GetEnv("ASAN_SYMBOLIZER_PATH");
83+
#endif
7484
cf.malloc_context_size = kDefaultMallocContextSize;
7585
cf.intercept_tls_get_addr = true;
7686
cf.exitcode = 1;
@@ -129,6 +139,23 @@ static void InitializeDefaultFlags() {
129139
lsan_parser.ParseString(lsan_default_options);
130140
#endif
131141

142+
#if SANITIZER_EMSCRIPTEN
143+
char *options;
144+
// Override from Emscripten Module.
145+
// TODO: add EM_ASM_I64 and avoid using a double for a 64-bit pointer.
146+
#define MAKE_OPTION_LOAD(parser, name) \
147+
options = _emscripten_sanitizer_get_option(name); \
148+
parser.ParseString(options); \
149+
emscripten_builtin_free(options);
150+
151+
MAKE_OPTION_LOAD(asan_parser, "ASAN_OPTIONS");
152+
#if CAN_SANITIZE_LEAKS
153+
MAKE_OPTION_LOAD(lsan_parser, "LSAN_OPTIONS");
154+
#endif
155+
#if CAN_SANITIZE_UB
156+
MAKE_OPTION_LOAD(ubsan_parser, "UBSAN_OPTIONS");
157+
#endif
158+
#else
132159
// Override from command line.
133160
asan_parser.ParseStringFromEnv("ASAN_OPTIONS");
134161
#if CAN_SANITIZE_LEAKS
@@ -137,9 +164,15 @@ static void InitializeDefaultFlags() {
137164
#if CAN_SANITIZE_UB
138165
ubsan_parser.ParseStringFromEnv("UBSAN_OPTIONS");
139166
#endif
167+
#endif // SANITIZER_EMSCRIPTEN
140168

141169
InitializeCommonFlags();
142170

171+
#if SANITIZER_EMSCRIPTEN
172+
if (common_flags()->malloc_context_size <= 1)
173+
StackTrace::snapshot_stack = false;
174+
#endif // SANITIZER_EMSCRIPTEN
175+
//
143176
// TODO(samsonov): print all of the flags (ASan, LSan, common).
144177
DisplayHelpMessages(&asan_parser);
145178
}

compiler-rt/lib/asan/asan_globals.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ void PrintGlobalLocation(InternalScopedString *str, const __asan_global &g,
387387
// ---------------------- Interface ---------------- {{{1
388388
using namespace __asan;
389389

390+
#if !SANITIZER_EMSCRIPTEN
390391
// Apply __asan_register_globals to all globals found in the same loaded
391392
// executable or shared library as `flag'. The flag tracks whether globals have
392393
// already been registered or not for this image.
@@ -424,6 +425,7 @@ void __asan_unregister_elf_globals(uptr *flag, void *start, void *stop) {
424425
__asan_unregister_globals(globals_start, globals_stop - globals_start);
425426
*flag = 0;
426427
}
428+
#endif
427429

428430
// Register an array of globals.
429431
void __asan_register_globals(__asan_global *globals, uptr n) {

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
#include "sanitizer_common/sanitizer_internal_defs.h"
2828
#include "sanitizer_common/sanitizer_libc.h"
2929

30-
// There is no general interception at all on Fuchsia.
30+
// There is no general interception at all on Fuchsia or Emscripten.
3131
// Only the functions in asan_interceptors_memintrinsics.cpp are
3232
// really defined to replace libc functions.
33-
#if !SANITIZER_FUCHSIA
33+
#if !SANITIZER_FUCHSIA && !SANITIZER_EMSCRIPTEN
3434

3535
# if SANITIZER_POSIX
3636
# include "sanitizer_common/sanitizer_posix.h"
@@ -911,4 +911,4 @@ void InitializeAsanInterceptors() {
911911

912912
} // namespace __asan
913913

914-
#endif // !SANITIZER_FUCHSIA
914+
#endif // !SANITIZER_FUCHSIA && !SANITIZER_RTEMS && !SANITIZER_EMSCRIPTEN

compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void *__asan_memmove(void *to, const void *from, uptr size) {
7171
ASAN_MEMMOVE_IMPL(nullptr, to, from, size);
7272
}
7373

74-
#if SANITIZER_FUCHSIA
74+
#if SANITIZER_FUCHSIA || SANITIZER_EMSCRIPTEN
7575

7676
// Fuchsia doesn't use sanitizer_common_interceptors.inc, but
7777
// the only things there it wants are these three. Just define them
@@ -81,7 +81,7 @@ extern "C" decltype(__asan_memcpy) memcpy[[gnu::alias("__asan_memcpy")]];
8181
extern "C" decltype(__asan_memmove) memmove[[gnu::alias("__asan_memmove")]];
8282
extern "C" decltype(__asan_memset) memset[[gnu::alias("__asan_memset")]];
8383

84-
#else // SANITIZER_FUCHSIA
84+
#else // SANITIZER_FUCHSIA || SANITIZER_EMSCRIPTEN
8585

8686
#define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
8787
do { \
@@ -103,4 +103,4 @@ extern "C" decltype(__asan_memset) memset[[gnu::alias("__asan_memset")]];
103103

104104
#include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc"
105105

106-
#endif // SANITIZER_FUCHSIA
106+
#endif // SANITIZER_FUCHSIA || SANITIZER_EMSCRIPTEN

compiler-rt/lib/asan/asan_malloc_linux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "sanitizer_common/sanitizer_platform.h"
1717
#if SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX || \
18-
SANITIZER_NETBSD || SANITIZER_SOLARIS
18+
SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_EMSCRIPTEN
1919

2020
# include "asan_allocator.h"
2121
# include "asan_interceptors.h"

compiler-rt/lib/asan/asan_mapping.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init.
272272

273273
# if defined(__sparc__) && SANITIZER_WORDSIZE == 64
274274
# include "asan_mapping_sparc64.h"
275+
# elif SANITIZER_EMSCRIPTEN
276+
# include "asan_mapping_emscripten.h"
275277
# else
276278
# define MEM_TO_SHADOW(mem) \
277279
(((mem) >> ASAN_SHADOW_SCALE) + (ASAN_SHADOW_OFFSET))

compiler-rt/lib/asan/asan_poisoning.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ uptr __asan_region_is_poisoned(uptr beg, uptr size) {
175175
if (!size)
176176
return 0;
177177
uptr end = beg + size;
178+
#if SANITIZER_EMSCRIPTEN
179+
// XXX Emscripten hack XXX
180+
// Null pointer handling, since Emscripten does not crash on null pointer,
181+
// ASan must catch null pointer dereference by itself.
182+
// Unfortunately, this function returns 0 to mean the region is not
183+
// poisoned, so we must return 1 instead if we receive a region
184+
// starting at 0.
185+
if (!beg) return 1;
186+
#endif
178187
if (!AddrIsInMem(beg))
179188
return beg;
180189
if (!AddrIsInMem(end))

compiler-rt/lib/asan/asan_poisoning.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
5151
// probably provide higher-level interface for these operations.
5252
// For now, just memset on Windows.
5353
if (value || SANITIZER_WINDOWS == 1 ||
54+
// Emscripten doesn't have a nice way to zero whole pages.
55+
// The bulk memory proposal will allow memset to be optimized, but
56+
// even then, we still must use memset.
57+
SANITIZER_EMSCRIPTEN == 1 ||
5458
shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
5559
REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
5660
} else {

compiler-rt/lib/asan/asan_posix.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
4141
}
4242

4343
bool PlatformUnpoisonStacks() {
44+
#if SANITIZER_EMSCRIPTEN
45+
return false;
46+
#else
4447
stack_t signal_stack;
4548
CHECK_EQ(0, sigaltstack(nullptr, &signal_stack));
4649
uptr sigalt_bottom = (uptr)signal_stack.ss_sp;
@@ -64,6 +67,7 @@ bool PlatformUnpoisonStacks() {
6467
&tls_end);
6568
UnpoisonStack(stack_begin, stack_end, "default");
6669
return true;
70+
#endif
6771
}
6872

6973
// ---------------------- TSD ---------------- {{{1

0 commit comments

Comments
 (0)