Skip to content

Commit dfd5717

Browse files
committed
Improve sanitizer detection
__has_feature(leak_sanitizer) cannot be used to detect the existence of LSan.
1 parent 559c617 commit dfd5717

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

system/include/emscripten/bind.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <emscripten/val.h>
2222
#include <emscripten/wire.h>
2323

24-
#if __has_feature(leak_sanitizer) || __has_feature(address_sanitizer)
24+
#if defined(HAS_LSAN) || __has_feature(address_sanitizer)
2525
#include <sanitizer/lsan_interface.h>
2626
#endif
2727

@@ -604,7 +604,7 @@ template<typename T>
604604
inline T* getContext(const T& t) {
605605
// not a leak because this is called once per binding
606606
auto* ret = new T(t);
607-
#if __has_feature(leak_sanitizer) || __has_feature(address_sanitizer)
607+
#if defined(HAS_LSAN) || __has_feature(address_sanitizer)
608608
__lsan_ignore_object(ret);
609609
#endif
610610
return ret;

system/lib/pthread/library_pthread.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,16 @@ extern double emscripten_receive_on_main_thread_js(int functionIndex, int numCal
139139
extern int _emscripten_notify_thread_queue(pthread_t targetThreadId, pthread_t mainThreadId);
140140
extern int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
141141

142-
#if defined(__has_feature)
143-
#if __has_feature(leak_sanitizer) || __has_feature(address_sanitizer)
144-
#define HAS_SANITIZER
142+
#if defined(HAS_LSAN) || __has_feature(address_sanitizer)
145143
#include <sanitizer/lsan_interface.h>
146144
#endif
147-
#endif
148145

149146
static void _do_call(em_queued_call* q) {
150147
// C function pointer
151148
assert(EM_FUNC_SIG_NUM_FUNC_ARGUMENTS(q->functionEnum) <= EM_QUEUED_CALL_MAX_ARGS);
152149
switch (q->functionEnum) {
153150
case EM_PROXIED_PTHREAD_CREATE:
154-
#ifdef HAS_SANITIZER
151+
#if defined(HAS_LSAN) || __has_feature(address_sanitizer)
155152
// ASan wraps the emscripten_builtin_pthread_create call in __lsan::ScopedInterceptorDisabler.
156153
// Unfortunately, that only disables it on the thread that made the call.
157154
// This is sufficient on the main thread.
@@ -163,7 +160,7 @@ static void _do_call(em_queued_call* q) {
163160
#endif
164161
q->returnValue.i =
165162
__pthread_create_js(q->args[0].vp, q->args[1].vp, q->args[2].vp, q->args[3].vp);
166-
#ifdef HAS_SANITIZER
163+
#if defined(HAS_LSAN) || __has_feature(address_sanitizer)
167164
__lsan_enable();
168165
#endif
169166
break;

tools/system_libs.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,32 @@ def get_default_variation(cls, **kwargs):
670670
return super().get_default_variation(is_asan=settings.USE_ASAN, **kwargs)
671671

672672

673+
class LsanInstrumentedLibrary(Library):
674+
def __init__(self, **kwargs):
675+
self.is_lsan = kwargs.pop('is_lsan', False)
676+
super().__init__(**kwargs)
677+
678+
def get_cflags(self):
679+
cflags = super().get_cflags()
680+
if self.is_lsan:
681+
cflags += ['-fsanitize=leak', '-DHAS_LSAN']
682+
return cflags
683+
684+
def get_base_name(self):
685+
name = super().get_base_name()
686+
if self.is_lsan:
687+
name += '-lsan'
688+
return name
689+
690+
@classmethod
691+
def vary_on(cls):
692+
return super().vary_on() + ['is_lsan']
693+
694+
@classmethod
695+
def get_default_variation(cls, **kwargs):
696+
return super().get_default_variation(is_lsan=settings.USE_LSAN, **kwargs)
697+
698+
673699
class libcompiler_rt(MTLibrary):
674700
name = 'libcompiler_rt'
675701
# compiler_rt files can't currently be part of LTO although we are hoping to remove this
@@ -690,7 +716,7 @@ class libcompiler_rt(MTLibrary):
690716
])
691717

692718

693-
class libc(AsanInstrumentedLibrary, MuslInternalLibrary, MTLibrary):
719+
class libc(AsanInstrumentedLibrary, LsanInstrumentedLibrary, MuslInternalLibrary, MTLibrary):
694720
name = 'libc'
695721

696722
# Without -fno-builtin, LLVM can optimize away or convert calls to library

0 commit comments

Comments
 (0)