Skip to content

Commit e130695

Browse files
authored
Add downstream changes from Emscripten (#22)
This adds all cumulative downstream changes so far to emscripten-libs-22 branch (which currently is at llvmorg-22.1.8 upstream tag).
1 parent ca7933e commit e130695

74 files changed

Lines changed: 694 additions & 165 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_fake_stack.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ FakeStack* FakeStack::Create(uptr stack_size_log) {
7777
VReport(1,
7878
"T%d: FakeStack created: %p -- %p stack_size_log: %zd; "
7979
"mmapped %zdK, noreserve=%d, true_start: %p, start of first frame: "
80-
"0x%zx\n",
80+
"%p\n",
8181
GetCurrentTidOrInvalid(), (void*)p,
8282
(void*)(p + FakeStack::RequiredSize(stack_size_log)), stack_size_log,
8383
size >> 10, flags()->uar_noreserve, res->true_start,
84-
res->GetFrame(stack_size_log, /*class_id*/ 0, /*pos*/ 0));
84+
(void*)res->GetFrame(stack_size_log, /*class_id*/ 0, /*pos*/ 0));
8585
return res;
8686
}
8787

‎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"
@@ -960,4 +960,4 @@ void InitializeAsanInterceptors() {
960960

961961
} // namespace __asan
962962

963-
#endif // !SANITIZER_FUCHSIA
963+
#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
@@ -73,7 +73,7 @@ void *__asan_memmove(void *to, const void *from, uptr size) {
7373
ASAN_MEMMOVE_IMPL(nullptr, to, from, size);
7474
}
7575

76-
#if SANITIZER_FUCHSIA
76+
#if SANITIZER_FUCHSIA || SANITIZER_EMSCRIPTEN
7777

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

86-
#else // SANITIZER_FUCHSIA
86+
#else // SANITIZER_FUCHSIA || SANITIZER_EMSCRIPTEN
8787

8888
#define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
8989
do { \
@@ -105,4 +105,4 @@ extern "C" decltype(__asan_memset) memset[[gnu::alias("__asan_memset")]];
105105

106106
#include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc"
107107

108-
#endif // SANITIZER_FUCHSIA
108+
#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 || SANITIZER_AIX
18+
SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU || \
19+
SANITIZER_AIX || 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
@@ -283,6 +283,8 @@ extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init.
283283

284284
# if defined(__sparc__) && SANITIZER_WORDSIZE == 64
285285
# include "asan_mapping_sparc64.h"
286+
# elif SANITIZER_EMSCRIPTEN
287+
# include "asan_mapping_emscripten.h"
286288
# else
287289
# define MEM_TO_SHADOW(mem) \
288290
((STRIP_MTE_TAG(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 {

0 commit comments

Comments
 (0)