Skip to content

Commit 9600d20

Browse files
authored
Add downstream changes from Emscripten (#20)
This adds all downstream changes to emscripten-libs-21 branch.
1 parent 2078da4 commit 9600d20

72 files changed

Lines changed: 670 additions & 152 deletions

File tree

Some content is hidden

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

compiler-rt/lib/asan/asan_errors.cpp

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

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+
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_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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

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

2021
# include "asan_allocator.h"
2122
# include "asan_interceptors.h"
@@ -217,4 +218,5 @@ void ReplaceSystemMalloc() {
217218
#endif // SANITIZER_ANDROID
218219

219220
#endif // SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX ||
220-
// SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU
221+
// SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU ||
222+
// SANITIZER_EMSCRIPTEN

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
@@ -242,6 +242,15 @@ uptr __asan_region_is_poisoned(uptr beg, uptr size) {
242242
if (!size)
243243
return 0;
244244
uptr end = beg + size;
245+
#if SANITIZER_EMSCRIPTEN
246+
// XXX Emscripten hack XXX
247+
// Null pointer handling, since Emscripten does not crash on null pointer,
248+
// ASan must catch null pointer dereference by itself.
249+
// Unfortunately, this function returns 0 to mean the region is not
250+
// poisoned, so we must return 1 instead if we receive a region
251+
// starting at 0.
252+
if (!beg) return 1;
253+
#endif
245254
if (!AddrIsInMem(beg))
246255
return beg;
247256
if (!AddrIsInMem(end))

compiler-rt/lib/asan/asan_poisoning.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
6767
// probably provide higher-level interface for these operations.
6868
// For now, just memset on Windows.
6969
if (value || SANITIZER_WINDOWS == 1 ||
70+
// Emscripten doesn't have a nice way to zero whole pages.
71+
// The bulk memory proposal will allow memset to be optimized, but
72+
// even then, we still must use memset.
73+
SANITIZER_EMSCRIPTEN == 1 ||
7074
shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
7175
REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
7276
} 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)