|
| 1 | +//===-- FuzzerInterceptors.cpp --------------------------------------------===// |
| 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 | +// Intercept certain libc functions to aid fuzzing. |
| 9 | +// Linked only when other RTs that define their own interceptors are not linked. |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#include "FuzzerPlatform.h" |
| 13 | + |
| 14 | +#if LIBFUZZER_LINUX |
| 15 | + |
| 16 | +#define GET_CALLER_PC() __builtin_return_address(0) |
| 17 | + |
| 18 | +#define PTR_TO_REAL(x) real_##x |
| 19 | +#define REAL(x) __interception::PTR_TO_REAL(x) |
| 20 | +#define FUNC_TYPE(x) x##_type |
| 21 | +#define DEFINE_REAL(ret_type, func, ...) \ |
| 22 | + typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \ |
| 23 | + namespace __interception { \ |
| 24 | + FUNC_TYPE(func) PTR_TO_REAL(func); \ |
| 25 | + } |
| 26 | + |
| 27 | +#include <cassert> |
| 28 | +#include <cstdint> |
| 29 | +#include <dlfcn.h> // for dlsym() |
| 30 | +#include <sanitizer/common_interface_defs.h> |
| 31 | + |
| 32 | +static void *getFuncAddr(const char *name, uintptr_t wrapper_addr) { |
| 33 | + void *addr = dlsym(RTLD_NEXT, name); |
| 34 | + if (!addr) { |
| 35 | + // If the lookup using RTLD_NEXT failed, the sanitizer runtime library is |
| 36 | + // later in the library search order than the DSO that we are trying to |
| 37 | + // intercept, which means that we cannot intercept this function. We still |
| 38 | + // want the address of the real definition, though, so look it up using |
| 39 | + // RTLD_DEFAULT. |
| 40 | + addr = dlsym(RTLD_DEFAULT, name); |
| 41 | + |
| 42 | + // In case `name' is not loaded, dlsym ends up finding the actual wrapper. |
| 43 | + // We don't want to intercept the wrapper and have it point to itself. |
| 44 | + if (reinterpret_cast<uintptr_t>(addr) == wrapper_addr) |
| 45 | + addr = nullptr; |
| 46 | + } |
| 47 | + return addr; |
| 48 | +} |
| 49 | + |
| 50 | +static int FuzzerInited = 0; |
| 51 | +static bool FuzzerInitIsRunning; |
| 52 | + |
| 53 | +static void fuzzerInit(); |
| 54 | + |
| 55 | +static void ensureFuzzerInited() { |
| 56 | + assert(!FuzzerInitIsRunning); |
| 57 | + if (!FuzzerInited) { |
| 58 | + fuzzerInit(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +extern "C" { |
| 63 | + |
| 64 | +DEFINE_REAL(int, memcmp, const void *, const void *, size_t) |
| 65 | +DEFINE_REAL(int, strncmp, const char *, const char *, size_t) |
| 66 | +DEFINE_REAL(int, strcmp, const char *, const char *) |
| 67 | +DEFINE_REAL(int, strncasecmp, const char *, const char *, size_t) |
| 68 | +DEFINE_REAL(int, strcasecmp, const char *, const char *) |
| 69 | +DEFINE_REAL(char *, strstr, const char *, const char *) |
| 70 | +DEFINE_REAL(char *, strcasestr, const char *, const char *) |
| 71 | +DEFINE_REAL(void *, memmem, const void *, size_t, const void *, size_t) |
| 72 | + |
| 73 | +ATTRIBUTE_INTERFACE int memcmp(const void *s1, const void *s2, size_t n) { |
| 74 | + ensureFuzzerInited(); |
| 75 | + int result = REAL(memcmp)(s1, s2, n); |
| 76 | + __sanitizer_weak_hook_memcmp(GET_CALLER_PC(), s1, s2, n, result); |
| 77 | + |
| 78 | + return result; |
| 79 | +} |
| 80 | + |
| 81 | +ATTRIBUTE_INTERFACE int strncmp(const char *s1, const char *s2, size_t n) { |
| 82 | + ensureFuzzerInited(); |
| 83 | + int result = REAL(strncmp)(s1, s2, n); |
| 84 | + __sanitizer_weak_hook_strncmp(GET_CALLER_PC(), s1, s2, n, result); |
| 85 | + |
| 86 | + return result; |
| 87 | +} |
| 88 | + |
| 89 | +ATTRIBUTE_INTERFACE int strcmp(const char *s1, const char *s2) { |
| 90 | + ensureFuzzerInited(); |
| 91 | + int result = REAL(strcmp)(s1, s2); |
| 92 | + __sanitizer_weak_hook_strcmp(GET_CALLER_PC(), s1, s2, result); |
| 93 | + |
| 94 | + return result; |
| 95 | +} |
| 96 | + |
| 97 | +ATTRIBUTE_INTERFACE int strncasecmp(const char *s1, const char *s2, size_t n) { |
| 98 | + ensureFuzzerInited(); |
| 99 | + int result = REAL(strncasecmp)(s1, s2, n); |
| 100 | + __sanitizer_weak_hook_strncasecmp(GET_CALLER_PC(), s1, s2, n, result); |
| 101 | + |
| 102 | + return result; |
| 103 | +} |
| 104 | + |
| 105 | +ATTRIBUTE_INTERFACE int strcasecmp(const char *s1, const char *s2) { |
| 106 | + ensureFuzzerInited(); |
| 107 | + int result = REAL(strcasecmp)(s1, s2); |
| 108 | + __sanitizer_weak_hook_strcasecmp(GET_CALLER_PC(), s1, s2, result); |
| 109 | + |
| 110 | + return result; |
| 111 | +} |
| 112 | + |
| 113 | +ATTRIBUTE_INTERFACE char *strstr(const char *s1, const char *s2) { |
| 114 | + ensureFuzzerInited(); |
| 115 | + char *result = REAL(strstr)(s1, s2); |
| 116 | + __sanitizer_weak_hook_strstr(GET_CALLER_PC(), s1, s2, result); |
| 117 | + |
| 118 | + return result; |
| 119 | +} |
| 120 | + |
| 121 | +ATTRIBUTE_INTERFACE char *strcasestr(const char *s1, const char *s2) { |
| 122 | + ensureFuzzerInited(); |
| 123 | + char *result = REAL(strcasestr)(s1, s2); |
| 124 | + __sanitizer_weak_hook_strcasestr(GET_CALLER_PC(), s1, s2, result); |
| 125 | + |
| 126 | + return result; |
| 127 | +} |
| 128 | + |
| 129 | +ATTRIBUTE_INTERFACE |
| 130 | +void *memmem(const void *s1, size_t len1, const void *s2, size_t len2) { |
| 131 | + ensureFuzzerInited(); |
| 132 | + void *result = REAL(memmem)(s1, len1, s2, len2); |
| 133 | + __sanitizer_weak_hook_memmem(GET_CALLER_PC(), s1, len1, s2, len2, result); |
| 134 | + |
| 135 | + return result; |
| 136 | +} |
| 137 | + |
| 138 | +__attribute__((section(".preinit_array"), |
| 139 | + used)) static void (*__local_fuzzer_preinit)(void) = fuzzerInit; |
| 140 | + |
| 141 | +} // extern "C" |
| 142 | + |
| 143 | +static void fuzzerInit() { |
| 144 | + assert(!FuzzerInitIsRunning); |
| 145 | + if (FuzzerInited) |
| 146 | + return; |
| 147 | + FuzzerInitIsRunning = true; |
| 148 | + |
| 149 | + REAL(memcmp) = reinterpret_cast<memcmp_type>( |
| 150 | + getFuncAddr("memcmp", reinterpret_cast<uintptr_t>(&memcmp))); |
| 151 | + REAL(strncmp) = reinterpret_cast<strncmp_type>( |
| 152 | + getFuncAddr("strncmp", reinterpret_cast<uintptr_t>(&strncmp))); |
| 153 | + REAL(strcmp) = reinterpret_cast<strcmp_type>( |
| 154 | + getFuncAddr("strcmp", reinterpret_cast<uintptr_t>(&strcmp))); |
| 155 | + REAL(strncasecmp) = reinterpret_cast<strncasecmp_type>( |
| 156 | + getFuncAddr("strncasecmp", reinterpret_cast<uintptr_t>(&strncasecmp))); |
| 157 | + REAL(strcasecmp) = reinterpret_cast<strcasecmp_type>( |
| 158 | + getFuncAddr("strcasecmp", reinterpret_cast<uintptr_t>(&strcasecmp))); |
| 159 | + REAL(strstr) = reinterpret_cast<strstr_type>( |
| 160 | + getFuncAddr("strstr", reinterpret_cast<uintptr_t>(&strstr))); |
| 161 | + REAL(strcasestr) = reinterpret_cast<strcasestr_type>( |
| 162 | + getFuncAddr("strcasestr", reinterpret_cast<uintptr_t>(&strcasestr))); |
| 163 | + REAL(memmem) = reinterpret_cast<memmem_type>( |
| 164 | + getFuncAddr("memmem", reinterpret_cast<uintptr_t>(&memmem))); |
| 165 | + |
| 166 | + FuzzerInitIsRunning = false; |
| 167 | + FuzzerInited = 1; |
| 168 | +} |
| 169 | + |
| 170 | +#endif |
0 commit comments