Debian Codegraph @

src/libc/libc-shim.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <dlfcn.h>
5 #include <errno.h>
6 #include <sys/syscall.h>
7 #include <unistd.h>
8
9 /* Each parameter is passed as a flat (type, name) pair: type1, name1, type2, name2, ... The
10 * _SHIM_DECL/_SHIM_NAME macros consume two args at a time and emit either "type name" pairs (for
11 * the function declarator) or just the names (for forwarding). _SHIM_PAIRS counts the number of
12 * pairs by indexing into a table that increments every two positions. */
13 #define _SHIM_CAT(a, b) _SHIM_CAT_(a, b)
14 #define _SHIM_CAT_(a, b) a##b
15
16 #define _SHIM_NTH(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, N, ...) N
17 #define _SHIM_PAIRS(...) _SHIM_NTH(__VA_ARGS__, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0)
18
19 #define _SHIM_DECL_1(t, n) t n
20 #define _SHIM_DECL_2(t, n, ...) t n, _SHIM_DECL_1(__VA_ARGS__)
21 #define _SHIM_DECL_3(t, n, ...) t n, _SHIM_DECL_2(__VA_ARGS__)
22 #define _SHIM_DECL_4(t, n, ...) t n, _SHIM_DECL_3(__VA_ARGS__)
23 #define _SHIM_DECL_5(t, n, ...) t n, _SHIM_DECL_4(__VA_ARGS__)
24 #define _SHIM_DECL_6(t, n, ...) t n, _SHIM_DECL_5(__VA_ARGS__)
25 #define _SHIM_DECL_7(t, n, ...) t n, _SHIM_DECL_6(__VA_ARGS__)
26 #define _SHIM_DECL_8(t, n, ...) t n, _SHIM_DECL_7(__VA_ARGS__)
27 #define _SHIM_DECL(...) _SHIM_CAT(_SHIM_DECL_, _SHIM_PAIRS(__VA_ARGS__))(__VA_ARGS__)
28
29 #define _SHIM_NAME_1(t, n) n
30 #define _SHIM_NAME_2(t, n, ...) n, _SHIM_NAME_1(__VA_ARGS__)
31 #define _SHIM_NAME_3(t, n, ...) n, _SHIM_NAME_2(__VA_ARGS__)
32 #define _SHIM_NAME_4(t, n, ...) n, _SHIM_NAME_3(__VA_ARGS__)
33 #define _SHIM_NAME_5(t, n, ...) n, _SHIM_NAME_4(__VA_ARGS__)
34 #define _SHIM_NAME_6(t, n, ...) n, _SHIM_NAME_5(__VA_ARGS__)
35 #define _SHIM_NAME_7(t, n, ...) n, _SHIM_NAME_6(__VA_ARGS__)
36 #define _SHIM_NAME_8(t, n, ...) n, _SHIM_NAME_7(__VA_ARGS__)
37 #define _SHIM_NAME(...) _SHIM_CAT(_SHIM_NAME_, _SHIM_PAIRS(__VA_ARGS__))(__VA_ARGS__)
38
39 /* The shim resolves the libc symbol via dlsym(RTLD_DEFAULT) at DSO-load time using a constructor
40 * and caches the result in a file-scope static. Constructors run single-threaded before main() and
41 * before any signal handler can fire, so the cached pointer needs no atomics: subsequent reads from
42 * any thread observe the value stored during init. Resolving eagerly also keeps dlsym() out of
43 * contexts where it is not async-signal-safe (signal handlers, between fork() and exec()).
44 *
45 * The asm barrier after dlsym() is load-bearing: without it, when LTO determines the cache store
46 * is dead (because no caller of func##_shim survives DCE) the compiler is free to tail-call
47 * dlsym() (jmp dlsym@plt). Under glibc, dlsym reads __builtin_return_address(0) to find its
48 * caller's link map; with a tail call that read lands inside ld.so's call_init(), the resulting
49 * link map has no l_scope, and _dl_lookup_symbol_x SIGSEGVs. Filed upstream in glibc by
50 * https://sourceware.org/bugzilla/show_bug.cgi?id=34156. The barrier keeps us working on
51 * unpatched libc.
52 *
53 * Each reference to `func` in the macro body is positioned as an operand of `#` or `##` so the
54 * override headers (e.g. "#define openat2 openat2_shim") don't rewrite the token before pasting or
55 * stringification. For the same reason the resolution logic isn't extracted into a helper macro —
56 * passing `func` to a nested macro would expand it as a regular argument and re-trigger the
57 * override.
58 *
59 * Defines a wrapper that calls the libc symbol if available at runtime, or falls back to the
60 * corresponding direct syscall otherwise. Each parameter is passed as type, name pairs (flat). */
61 #define DEFINE_SYSCALL_SHIM(func, ret, ...) \
62 static typeof(&func##_shim) func##_shim_cache; \
63 __attribute__((constructor)) static void func##_shim_init(void) { \
64 void *p = dlsym(RTLD_DEFAULT, #func); \
65 __asm__ volatile("" ::: "memory"); \
66 func##_shim_cache = (typeof(&func##_shim)) p; \
67 } \
68 ret func##_shim(_SHIM_DECL(__VA_ARGS__)) { \
69 if (func##_shim_cache) \
70 return func##_shim_cache(_SHIM_NAME(__VA_ARGS__)); \
71 return syscall(__NR_##func, _SHIM_NAME(__VA_ARGS__)); \
72 }
73
74 /* Like DEFINE_SYSCALL_SHIM but for libc helpers that have no corresponding syscall and report errors
75 * by returning the positive errno value directly (posix_spawn-family convention). If the libc symbol
76 * is missing at runtime, ENOSYS is returned. */
77 #define DEFINE_LIBC_SHIM(func, ret, ...) \
78 static typeof(&func##_shim) func##_shim_cache; \
79 __attribute__((constructor)) static void func##_shim_init(void) { \
80 void *p = dlsym(RTLD_DEFAULT, #func); \
81 __asm__ volatile("" ::: "memory"); \
82 func##_shim_cache = (typeof(&func##_shim)) p; \
83 } \
84 ret func##_shim(_SHIM_DECL(__VA_ARGS__)) { \
85 if (func##_shim_cache) \
86 return func##_shim_cache(_SHIM_NAME(__VA_ARGS__)); \
87 return ENOSYS; \
88 }
89
90 /* Like DEFINE_LIBC_SHIM but for libc helpers that report errors via errno + -1 return value. If the
91 * libc symbol is missing at runtime, errno is set to ENOSYS and -1 is returned. */
92 #define DEFINE_LIBC_ERRNO_SHIM(func, ret, ...) \
93 static typeof(&func##_shim) func##_shim_cache; \
94 __attribute__((constructor)) static void func##_shim_init(void) { \
95 void *p = dlsym(RTLD_DEFAULT, #func); \
96 __asm__ volatile("" ::: "memory"); \
97 func##_shim_cache = (typeof(&func##_shim)) p; \
98 } \
99 ret func##_shim(_SHIM_DECL(__VA_ARGS__)) { \
100 if (func##_shim_cache) \
101 return func##_shim_cache(_SHIM_NAME(__VA_ARGS__)); \
102 errno = ENOSYS; \
103 return -1; \
104 }
105
106 /* Like DEFINE_LIBC_ERRNO_SHIM but with an explicit string for the libc symbol name to dlsym. This is
107 * needed for functions whose libc symbol is renamed via __asm__() in the header (e.g. when glibc
108 * redirects time-related calls to their __*_time64 aliases on 32-bit systems built with
109 * _TIME_BITS=64) since dlsym() doesn't see those header-level renames, so the caller has to spell out
110 * the actual ABI symbol name that matches the struct layout the compiler picked. We can't forward to
111 * DEFINE_LIBC_ERRNO_SHIM since passing `func` as a regular argument would let the override-header
112 * #define rewrite the token (e.g. `epoll_pwait2` to `epoll_pwait2_shim`) before the inner macro
113 * could paste it, so we duplicate the body and keep every `func` reference behind `#` or `##`. */
114 #define DEFINE_LIBC_ERRNO_SHIM_NAMED(func, sym_name, ret, ...) \
115 static typeof(&func##_shim) func##_shim_cache; \
116 __attribute__((constructor)) static void func##_shim_init(void) { \
117 void *p = dlsym(RTLD_DEFAULT, sym_name); \
118 __asm__ volatile("" ::: "memory"); \
119 func##_shim_cache = (typeof(&func##_shim)) p; \
120 } \
121 ret func##_shim(_SHIM_DECL(__VA_ARGS__)) { \
122 if (func##_shim_cache) \
123 return func##_shim_cache(_SHIM_NAME(__VA_ARGS__)); \
124 errno = ENOSYS; \
125 return -1; \
126 }