llvm/compiler-rt/lib/fuzzer/FuzzerInterceptors.cpp

//===-- FuzzerInterceptors.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Intercept certain libc functions to aid fuzzing.
// Linked only when other RTs that define their own interceptors are not linked.
//===----------------------------------------------------------------------===//

#include "FuzzerPlatform.h"

#if LIBFUZZER_LINUX

#define GET_CALLER_PC()

#define PTR_TO_REAL(x)
#define REAL(x)
#define FUNC_TYPE(x)
#define DEFINE_REAL(ret_type, func, ...)

#include <cassert>
#include <cstddef> // for size_t
#include <cstdint>
#include <dlfcn.h> // for dlsym()

static void *getFuncAddr(const char *name, uintptr_t wrapper_addr) {}

static int FuzzerInited =;
static bool FuzzerInitIsRunning;

static void fuzzerInit();

static void ensureFuzzerInited() {}

static int internal_strcmp_strncmp(const char *s1, const char *s2, bool strncmp,
                                   size_t n) {}

static int internal_strncmp(const char *s1, const char *s2, size_t n) {}

static int internal_strcmp(const char *s1, const char *s2) {}

static int internal_memcmp(const void *s1, const void *s2, size_t n) {}

static size_t internal_strlen(const char *s) {}

static char *internal_strstr(const char *haystack, const char *needle) {}

extern "C" {

// Weak hooks forward-declared to avoid dependency on
// <sanitizer/common_interface_defs.h>.
void __sanitizer_weak_hook_memcmp(void *called_pc, const void *s1,
                                  const void *s2, size_t n, int result);
void __sanitizer_weak_hook_strncmp(void *called_pc, const char *s1,
                                   const char *s2, size_t n, int result);
void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
                                       const char *s2, size_t n, int result);
void __sanitizer_weak_hook_strcmp(void *called_pc, const char *s1,
                                  const char *s2, int result);
void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
                                      const char *s2, int result);
void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
                                  const char *s2, char *result);
void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
                                      const char *s2, char *result);
void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
                                  const void *s2, size_t len2, void *result);

DEFINE_REAL()
DEFINE_REAL()
DEFINE_REAL()
DEFINE_REAL()
DEFINE_REAL()
DEFINE_REAL()
DEFINE_REAL()
DEFINE_REAL()
DEFINE_REAL()

ATTRIBUTE_INTERFACE int bcmp(const char *s1, const char *s2, size_t n) {}

ATTRIBUTE_INTERFACE int memcmp(const void *s1, const void *s2, size_t n) {}

ATTRIBUTE_INTERFACE int strncmp(const char *s1, const char *s2, size_t n) {}

ATTRIBUTE_INTERFACE int strcmp(const char *s1, const char *s2) {}

ATTRIBUTE_INTERFACE int strncasecmp(const char *s1, const char *s2, size_t n) {}

ATTRIBUTE_INTERFACE int strcasecmp(const char *s1, const char *s2) {}

ATTRIBUTE_INTERFACE char *strstr(const char *s1, const char *s2) {}

ATTRIBUTE_INTERFACE char *strcasestr(const char *s1, const char *s2) {}

ATTRIBUTE_INTERFACE
void *memmem(const void *s1, size_t len1, const void *s2, size_t len2) {}

__attribute__((section(".preinit_array"),
               used)) static void (*__local_fuzzer_preinit)(void) =;

} // extern "C"

static void fuzzerInit() {}

#endif