Skip to content

Commit 1304b34

Browse files
authored
Cherry-picking of changed from emscripten-libs-18 onto llvmorg-19.1.4 (#13)
1 parent aadaa00 commit 1304b34

File tree

64 files changed

+717
-150
lines changed

Some content is hidden

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

64 files changed

+717
-150
lines changed

compiler-rt/lib/asan/asan_errors.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
500500
scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr);
501501
if (far_from_bounds) scariness.Scare(10, "far-from-bounds");
502502
}
503+
#if SANITIZER_EMSCRIPTEN
504+
// If address is in the first page (64 KB), then it is likely that the
505+
// access is a result of a null pointer dereference.
506+
else if (addr < 65536) {
507+
bug_descr = "null-pointer-dereference";
508+
scariness.Scare(25, bug_descr);
509+
} else if (AddrIsInShadow(addr)) {
510+
bug_descr = "shadow-access";
511+
scariness.Scare(25, bug_descr);
512+
}
513+
#endif
503514
}
504515
}
505516

compiler-rt/lib/asan/asan_flags.cpp

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

25+
#if SANITIZER_EMSCRIPTEN
26+
#include <emscripten/heap.h>
27+
#include "emscripten_internal.h"
28+
#endif
29+
30+
2531
namespace __asan {
2632

2733
Flags asan_flags_dont_use_directly; // use via flags().
@@ -54,7 +60,11 @@ void InitializeFlags() {
5460
CommonFlags cf;
5561
cf.CopyFrom(*common_flags());
5662
cf.detect_leaks = cf.detect_leaks && CAN_SANITIZE_LEAKS;
63+
#if !SANITIZER_EMSCRIPTEN
64+
// getenv on emscripten uses malloc, which we can't when using LSan.
65+
// You can't run external symbolizer executables anyway.
5766
cf.external_symbolizer_path = GetEnv("ASAN_SYMBOLIZER_PATH");
67+
#endif
5868
cf.malloc_context_size = kDefaultMallocContextSize;
5969
cf.intercept_tls_get_addr = true;
6070
cf.exitcode = 1;
@@ -115,6 +125,23 @@ void InitializeFlags() {
115125
lsan_parser.ParseString(lsan_default_options);
116126
#endif
117127

128+
#if SANITIZER_EMSCRIPTEN
129+
char *options;
130+
// Override from Emscripten Module.
131+
// TODO: add EM_ASM_I64 and avoid using a double for a 64-bit pointer.
132+
#define MAKE_OPTION_LOAD(parser, name) \
133+
options = _emscripten_sanitizer_get_option(name); \
134+
parser.ParseString(options); \
135+
emscripten_builtin_free(options);
136+
137+
MAKE_OPTION_LOAD(asan_parser, "ASAN_OPTIONS");
138+
#if CAN_SANITIZE_LEAKS
139+
MAKE_OPTION_LOAD(lsan_parser, "LSAN_OPTIONS");
140+
#endif
141+
#if CAN_SANITIZE_UB
142+
MAKE_OPTION_LOAD(ubsan_parser, "UBSAN_OPTIONS");
143+
#endif
144+
#else
118145
// Override from command line.
119146
asan_parser.ParseStringFromEnv("ASAN_OPTIONS");
120147
#if CAN_SANITIZE_LEAKS
@@ -123,12 +150,18 @@ void InitializeFlags() {
123150
#if CAN_SANITIZE_UB
124151
ubsan_parser.ParseStringFromEnv("UBSAN_OPTIONS");
125152
#endif
153+
#endif // SANITIZER_EMSCRIPTEN
126154

127155
InitializeCommonFlags();
128156

129157
// TODO(eugenis): dump all flags at verbosity>=2?
130158
if (Verbosity()) ReportUnrecognizedFlags();
131159

160+
#if SANITIZER_EMSCRIPTEN
161+
if (common_flags()->malloc_context_size <= 1)
162+
StackTrace::snapshot_stack = false;
163+
#endif // SANITIZER_EMSCRIPTEN
164+
132165
if (common_flags()->help) {
133166
// TODO(samsonov): print all of the flags (ASan, LSan, common).
134167
asan_parser.PrintFlagDescriptions();

compiler-rt/lib/asan/asan_globals.cpp

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

328+
#if !SANITIZER_EMSCRIPTEN
328329
// Apply __asan_register_globals to all globals found in the same loaded
329330
// executable or shared library as `flag'. The flag tracks whether globals have
330331
// already been registered or not for this image.
@@ -362,6 +363,7 @@ void __asan_unregister_elf_globals(uptr *flag, void *start, void *stop) {
362363
__asan_unregister_globals(globals_start, globals_stop - globals_start);
363364
*flag = 0;
364365
}
366+
#endif
365367

366368
// Register an array of globals.
367369
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"
@@ -883,4 +883,4 @@ void InitializeAsanInterceptors() {
883883

884884
} // namespace __asan
885885

886-
#endif // !SANITIZER_FUCHSIA
886+
#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
@@ -174,6 +174,15 @@ uptr __asan_region_is_poisoned(uptr beg, uptr size) {
174174
if (!size)
175175
return 0;
176176
uptr end = beg + size;
177+
#if SANITIZER_EMSCRIPTEN
178+
// XXX Emscripten hack XXX
179+
// Null pointer handling, since Emscripten does not crash on null pointer,
180+
// ASan must catch null pointer dereference by itself.
181+
// Unfortunately, this function returns 0 to mean the region is not
182+
// poisoned, so we must return 1 instead if we receive a region
183+
// starting at 0.
184+
if (!beg) return 1;
185+
#endif
177186
if (!AddrIsInMem(beg))
178187
return beg;
179188
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_size);
6568
UnpoisonStack(default_bottom, default_bottom + stack_size, "default");
6669
return true;
70+
#endif
6771
}
6872

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

0 commit comments

Comments
 (0)