Skip to content

Commit 75e6133

Browse files
authored
[interception] Implement interception on AIX (llvm#138608)
Implement AIX specific interception functions. Issue: llvm#138916
1 parent e1328fd commit 75e6133

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

compiler-rt/lib/interception/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Build for the runtime interception helper library.
22

33
set(INTERCEPTION_SOURCES
4+
interception_aix.cpp
45
interception_linux.cpp
56
interception_mac.cpp
67
interception_win.cpp
@@ -9,6 +10,7 @@ set(INTERCEPTION_SOURCES
910

1011
set(INTERCEPTION_HEADERS
1112
interception.h
13+
interception_aix.h
1214
interception_linux.h
1315
interception_mac.h
1416
interception_win.h

compiler-rt/lib/interception/interception.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#if !SANITIZER_LINUX && !SANITIZER_FREEBSD && !SANITIZER_APPLE && \
2121
!SANITIZER_NETBSD && !SANITIZER_WINDOWS && !SANITIZER_FUCHSIA && \
22-
!SANITIZER_SOLARIS && !SANITIZER_HAIKU
22+
!SANITIZER_SOLARIS && !SANITIZER_HAIKU && !SANITIZER_AIX
2323
# error "Interception doesn't work on this operating system."
2424
#endif
2525

@@ -168,6 +168,16 @@ const interpose_substitution substitution_##func_name[] \
168168
extern "C" ret_type func(__VA_ARGS__);
169169
# define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \
170170
extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
171+
#elif SANITIZER_AIX
172+
# define WRAP(x) __interceptor_##x
173+
# define TRAMPOLINE(x) WRAP(x)
174+
// # define WRAPPER_NAME(x) "__interceptor_" #x
175+
# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
176+
// AIX's linker will not select the weak symbol, so don't use weak for the
177+
// interceptors.
178+
# define DECLARE_WRAPPER(ret_type, func, ...) \
179+
extern "C" ret_type func(__VA_ARGS__) \
180+
__attribute__((alias("__interceptor_" #func), visibility("default")));
171181
#elif !SANITIZER_FUCHSIA // LINUX, FREEBSD, NETBSD, SOLARIS
172182
# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
173183
# if ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT
@@ -367,12 +377,17 @@ inline void DoesNotSupportStaticLinking() {}
367377

368378
#define INCLUDED_FROM_INTERCEPTION_LIB
369379

370-
#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
380+
#if SANITIZER_AIX
381+
# include "interception_aix.h"
382+
# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_AIX(func)
383+
# define INTERCEPT_FUNCTION_VER(func, symver) INTERCEPT_FUNCTION_AIX(func)
384+
385+
#elif SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
371386
SANITIZER_SOLARIS || SANITIZER_HAIKU
372387

373-
# include "interception_linux.h"
374-
# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
375-
# define INTERCEPT_FUNCTION_VER(func, symver) \
388+
# include "interception_linux.h"
389+
# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
390+
# define INTERCEPT_FUNCTION_VER(func, symver) \
376391
INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver)
377392
#elif SANITIZER_APPLE
378393
# include "interception_mac.h"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===-- interception_aix.cpp ------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file is a part of AddressSanitizer, an address sanity checker.
10+
//
11+
// AIX-specific interception methods.
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "interception.h"
15+
#include "sanitizer_common/sanitizer_common.h"
16+
17+
#if SANITIZER_AIX
18+
19+
# include <dlfcn.h> // for dlsym()
20+
21+
namespace __interception {
22+
23+
static void *GetFuncAddr(const char *name, uptr wrapper_addr) {
24+
// AIX dlsym can only defect the functions that are exported, so
25+
// on AIX, we can not intercept some basic functions like memcpy.
26+
// FIXME: if we are going to ship dynamic asan library, we may need to search
27+
// all the loaded modules with RTLD_DEFAULT if RTLD_NEXT failed.
28+
void *addr = dlsym(RTLD_NEXT, name);
29+
30+
// In case `name' is not loaded, dlsym ends up finding the actual wrapper.
31+
// We don't want to intercept the wrapper and have it point to itself.
32+
if ((uptr)addr == wrapper_addr)
33+
addr = nullptr;
34+
return addr;
35+
}
36+
37+
bool InterceptFunction(const char *name, uptr *ptr_to_real, uptr func,
38+
uptr wrapper) {
39+
void *addr = GetFuncAddr(name, wrapper);
40+
*ptr_to_real = (uptr)addr;
41+
return addr && (func == wrapper);
42+
}
43+
44+
} // namespace __interception
45+
#endif // SANITIZER_AIX
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- interception_aix.h --------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file is a part of AddressSanitizer, an address sanity checker.
10+
//
11+
// AIX-specific interception methods.
12+
//===----------------------------------------------------------------------===//
13+
14+
#if SANITIZER_AIX
15+
16+
# if !defined(INCLUDED_FROM_INTERCEPTION_LIB)
17+
# error \
18+
"interception_aix.h should be included from interception library only"
19+
# endif
20+
21+
# ifndef INTERCEPTION_AIX_H
22+
# define INTERCEPTION_AIX_H
23+
24+
namespace __interception {
25+
bool InterceptFunction(const char *name, uptr *ptr_to_real, uptr func,
26+
uptr wrapper);
27+
} // namespace __interception
28+
29+
# define INTERCEPT_FUNCTION_AIX(func) \
30+
::__interception::InterceptFunction( \
31+
#func, (::__interception::uptr *)&REAL(func), \
32+
(::__interception::uptr) & (func), \
33+
(::__interception::uptr) & WRAP(func))
34+
35+
# endif // INTERCEPTION_AIX_H
36+
#endif // SANITIZER_AIX

0 commit comments

Comments
 (0)