Skip to content

Commit cbd28cd

Browse files
author
Serge Guelton
committed
Fix asan infinite loop on undefined symbol
Fix #39641 Recommit of r366413 Differential Revision: https://reviews.llvm.org/D63877 llvm-svn: 366632
1 parent fc0d766 commit cbd28cd

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

compiler-rt/lib/interception/interception_linux.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static int StrCmp(const char *s1, const char *s2) {
3333
}
3434
#endif
3535

36-
static void *GetFuncAddr(const char *name) {
36+
static void *GetFuncAddr(const char *name, uptr wrapper_addr) {
3737
#if SANITIZER_NETBSD
3838
// FIXME: Find a better way to handle renames
3939
if (StrCmp(name, "sigaction"))
@@ -47,13 +47,18 @@ static void *GetFuncAddr(const char *name) {
4747
// want the address of the real definition, though, so look it up using
4848
// RTLD_DEFAULT.
4949
addr = dlsym(RTLD_DEFAULT, name);
50+
51+
// In case `name' is not loaded, dlsym ends up finding the actual wrapper.
52+
// We don't want to intercept the wrapper and have it point to itself.
53+
if ((uptr)addr == wrapper_addr)
54+
addr = nullptr;
5055
}
5156
return addr;
5257
}
5358

5459
bool InterceptFunction(const char *name, uptr *ptr_to_real, uptr func,
5560
uptr wrapper) {
56-
void *addr = GetFuncAddr(name);
61+
void *addr = GetFuncAddr(name, wrapper);
5762
*ptr_to_real = (uptr)addr;
5863
return addr && (func == wrapper);
5964
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %clangxx_asan -xc++ -shared -fPIC -o %t.so - < %s
2+
// RUN: %clang_asan %s -o %t.out -ldl
3+
//
4+
// RUN: env ASAN_OPTIONS=verbosity=1 not %t.out %t.so 2>&1 | FileCheck %s
5+
//
6+
// CHECK: {{.*}}AddressSanitizer: failed to intercept '__cxa_{{.*}}throw{{.*}}'
7+
//
8+
// REQUIRES: x86_64-target-arch && !android
9+
10+
#ifdef __cplusplus
11+
12+
static void foo(void) {
13+
int i = 0;
14+
throw(i);
15+
}
16+
17+
extern "C" {
18+
int bar(void);
19+
};
20+
int bar(void) {
21+
try {
22+
foo();
23+
} catch (int i) {
24+
return i;
25+
}
26+
return -1;
27+
}
28+
29+
#else
30+
31+
#include <assert.h>
32+
#include <dlfcn.h>
33+
34+
int main(int argc, char **argv) {
35+
int (*bar)(void);
36+
void *handle = dlopen(argv[1], RTLD_LAZY);
37+
assert(handle);
38+
bar = dlsym(handle, "bar");
39+
assert(bar);
40+
return bar();
41+
}
42+
43+
#endif

0 commit comments

Comments
 (0)