Skip to content

Patches from emscripten 3.1.52 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions compiler-rt/lib/asan/asan_emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ void GetAllocatorCacheRange(uptr *begin, uptr *end) {
}
#endif

u32 GetCurrentThread() { return __asan::GetCurrentThread()->tid(); }

} // namespace __lsan

#endif // SANITIZER_EMSCRIPTEN
4 changes: 2 additions & 2 deletions compiler-rt/lib/asan/asan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ INTERCEPTOR(int, pthread_detach, void *thread) {
return result;
}

INTERCEPTOR(int, pthread_exit, void *retval) {
INTERCEPTOR(void, pthread_exit, void *retval) {
asanThreadArgRetval().Finish(GetThreadSelf(), retval);
return REAL(pthread_exit)(retval);
REAL(pthread_exit)(retval);
}

# if ASAN_INTERCEPT_TRYJOIN
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/asan/asan_rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,9 @@ static void AsanInitInternal() {

ReplaceSystemMalloc();

#if !SANITIZER_EMSCRIPTEN
DisableCoreDumperIfNecessary();
#endif

InitializeShadowMemory();

Expand Down
6 changes: 3 additions & 3 deletions compiler-rt/lib/lsan/lsan_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,17 +595,17 @@ void ScanRootRegions(Frontier *frontier,
static void ProcessRootRegions(Frontier *frontier) {
if (!flags()->use_root_regions || !HasRootRegions())
return;
InternalMmapVector<Region> mapped_regions;
#if SANITIZER_EMSCRIPTEN
ScanRootRegion(frontier, root_region, 0, emscripten_get_heap_size(), true);
mapped_regions.push_back({0, emscripten_get_heap_size()});
#else
MemoryMappingLayout proc_maps(/*cache_enabled*/ true);
MemoryMappedSegment segment;
InternalMmapVector<Region> mapped_regions;
while (proc_maps.Next(&segment))
if (segment.IsReadable())
mapped_regions.push_back({segment.start, segment.end});
ScanRootRegions(frontier, mapped_regions);
#endif // SANITIZER_EMSCRIPTEN
ScanRootRegions(frontier, mapped_regions);
}

static void FloodFillTag(Frontier *frontier, ChunkTag tag) {
Expand Down
8 changes: 3 additions & 5 deletions compiler-rt/lib/lsan/lsan_common_emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,13 @@ void LockStuffAndStopTheWorld(StopTheWorldCallback callback,
CheckForLeaksParam *argument) {
// Currently, on Emscripten this does nothing and just calls the callback.
// This works fine on a single-threaded environment.
LockThreadRegistry();
LockThreads();
LockAllocator();
StopTheWorld(callback, argument);
UnlockAllocator();
UnlockThreadRegistry();
UnlockThreads();
}

u32 GetCurrentThread();

// This is based on ProcessThreads in lsan_common.cc.
// We changed this to be a callback that gets called per thread by
// ThreadRegistry::RunCallbackForEachThreadLocked.
Expand Down Expand Up @@ -142,7 +140,7 @@ static void ProcessThreadsCallback(ThreadContextBase *tctx, void *arg) {

// We can't get the SP for other threads to narrow down the range, but we
// we can for the current thread.
if (tctx->tid == GetCurrentThread()) {
if (tctx->os_id == GetTid()) {
uptr sp = (uptr) __builtin_frame_address(0);
if (sp < stack_begin || sp >= stack_end) {
// SP is outside the recorded stack range (e.g. the thread is running a
Expand Down
15 changes: 12 additions & 3 deletions compiler-rt/lib/lsan/lsan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int emscripten_builtin_pthread_create(void *thread, void *attr,
void *(*callback)(void *), void *arg);
int emscripten_builtin_pthread_join(void *th, void **ret);
int emscripten_builtin_pthread_detach(void *th);
void emscripten_builtin_pthread_exit(void *th);
}
#endif

Expand Down Expand Up @@ -452,13 +453,21 @@ INTERCEPTOR(int, pthread_create, void *th, void *attr,
ENSURE_LSAN_INITED;
EnsureMainThreadIDIsCorrect();

#if SANITIZER_EMSCRIPTEN
// In Emscripten sanitizer, attr can be nonzero but __ATTRP_C11_THREAD in case
// of C11 threads, in which case we need to run pthread_attr_init as well, so
// we treat __ATTRP_C11_THREAD like the nullptr in this function.
if (attr == __ATTRP_C11_THREAD)
attr = nullptr;
#endif

bool detached = [attr]() {
int d = 0;
return attr && !pthread_attr_getdetachstate(attr, &d) && IsStateDetached(d);
}();

__sanitizer_pthread_attr_t myattr;
if (!attr || attr == __ATTRP_C11_THREAD) {
if (!attr) {
pthread_attr_init(&myattr);
attr = &myattr;
}
Expand Down Expand Up @@ -501,9 +510,9 @@ INTERCEPTOR(int, pthread_detach, void *thread) {
return result;
}

INTERCEPTOR(int, pthread_exit, void *retval) {
INTERCEPTOR(void, pthread_exit, void *retval) {
GetThreadArgRetval().Finish(GetThreadSelf(), retval);
return REAL(pthread_exit)(retval);
REAL(pthread_exit)(retval);
}

# if SANITIZER_INTERCEPT_TRYJOIN
Expand Down
5 changes: 4 additions & 1 deletion compiler-rt/lib/lsan/lsan_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ void InitializeThreads() {
thread_arg_retval = new (thread_arg_retval_placeholder) ThreadArgRetval();
}

ThreadArgRetval &GetThreadArgRetval() { return *thread_arg_retval; }
ThreadArgRetval &GetThreadArgRetval() {
ENSURE_LSAN_INITED;
return *thread_arg_retval;
}

ThreadContextLsanBase::ThreadContextLsanBase(int tid)
: ThreadContextBase(tid) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,7 @@ struct __sanitizer_file_handle {
};
#endif

// These fields are not actually pointers, and so wasm64 must use unsigned and not uptr for them
#if SANITIZER_APPLE || SANITIZER_EMSCRIPTEN
#if SANITIZER_APPLE
struct __sanitizer_msghdr {
void *msg_name;
unsigned msg_namelen;
Expand Down
4 changes: 4 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ bool MprotectReadOnly(uptr addr, uptr size) {
}

bool MprotectReadWrite(uptr addr, uptr size) {
#if SANITIZER_EMSCRIPTEN
return true;
#else
return 0 == internal_mprotect((void *)addr, size, PROT_READ | PROT_WRITE);
#endif
}

#if !SANITIZER_APPLE
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ bool DontDumpShadowMemory(uptr addr, uptr length) {
#endif // MADV_DONTDUMP
}

#if !SANITIZER_EMSCRIPTEN
static rlim_t getlim(int res) {
rlimit rlim;
CHECK_EQ(0, getrlimit(res, &rlim));
Expand Down Expand Up @@ -127,6 +128,7 @@ void SetAddressSpaceUnlimited() {
setlim(RLIMIT_AS, RLIM_INFINITY);
CHECK(AddressSpaceIsUnlimited());
}
#endif

void Abort() {
#if !SANITIZER_GO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#define SANITIZER_REDEFINE_BUILTINS_H

// The asm hack only works with GCC and Clang.
#if !defined(_WIN32)
// XXX Emscripten This does not work in Wasm.
#if !defined(_WIN32) && !defined(__wasm__)

asm("memcpy = __sanitizer_internal_memcpy");
asm("memmove = __sanitizer_internal_memmove");
Expand Down Expand Up @@ -46,7 +47,7 @@ using unordered_set = Define_SANITIZER_COMMON_NO_REDEFINE_BUILTINS_in_cpp_file;
using vector = Define_SANITIZER_COMMON_NO_REDEFINE_BUILTINS_in_cpp_file;
} // namespace std

#endif // !_WIN32
#endif // !_WIN32 && !__wasm__

#endif // SANITIZER_REDEFINE_BUILTINS_H
#endif // SANITIZER_COMMON_NO_REDEFINE_BUILTINS
4 changes: 2 additions & 2 deletions libcxx/include/__locale
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,12 @@ public:
static const mask __regex_word = 0x4000; // 0x8000 and 0x0100 and 0x00ff are used
# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
# ifdef __APPLE__
typedef __uint32_t mask;
# elif defined(__FreeBSD__)
typedef unsigned long mask;
# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
# elif defined(__NetBSD__)
typedef unsigned short mask;
# endif
static const mask space = _CTYPE_S;
Expand Down
10 changes: 5 additions & 5 deletions libcxx/src/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ operator new(std::size_t size) _THROW_BAD_ALLOC
return p;
}

#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_HAS_NO_EXCEPTIONS)
void* _new_nothrow(size_t size) noexcept
{
/// We cannot call ::operator new(size) here because it would abort
Expand All @@ -78,7 +78,7 @@ _LIBCPP_WEAK
void*
operator new(size_t size, const std::nothrow_t&) noexcept
{
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_HAS_NO_EXCEPTIONS)
return _new_nothrow(size);
#else
void* p = nullptr;
Expand All @@ -94,7 +94,7 @@ operator new(size_t size, const std::nothrow_t&) noexcept
}
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
return p;
#endif // __EMSCRIPTEN__ && _LIBCPP_NO_EXCEPTIONS
#endif // __EMSCRIPTEN__ && _LIBCPP_HAS_NO_EXCEPTIONS
}

_LIBCPP_WEAK
Expand All @@ -108,7 +108,7 @@ _LIBCPP_WEAK
void*
operator new[](size_t size, const std::nothrow_t&) noexcept
{
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
#if defined(__EMSCRIPTEN__) && defined(_LIBCPP_HAS_NO_EXCEPTIONS)
return _new_nothrow(size);
#else
void* p = nullptr;
Expand All @@ -124,7 +124,7 @@ operator new[](size_t size, const std::nothrow_t&) noexcept
}
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
return p;
#endif // __EMSCRIPTEN__ && _LIBCPP_NO_EXCEPTIONS
#endif // __EMSCRIPTEN__ && _LIBCPP_HAS_NO_EXCEPTIONS
}

_LIBCPP_WEAK
Expand Down