diff options
Diffstat (limited to 'contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer')
4 files changed, 811 insertions, 0 deletions
diff --git a/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_symbolize.cpp b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_symbolize.cpp new file mode 100644 index 000000000000..c851dbbf2eb2 --- /dev/null +++ b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_symbolize.cpp @@ -0,0 +1,197 @@ +//===-- sanitizer_symbolize.cpp ---------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Implementation of weak hooks from sanitizer_symbolizer_posix_libcdep.cpp. +// +//===----------------------------------------------------------------------===// + +#include <inttypes.h> +#include <stdio.h> + +#include <string> + +#include "llvm/DebugInfo/Symbolize/DIPrinter.h" +#include "llvm/DebugInfo/Symbolize/Symbolize.h" +#include "llvm/Demangle/Demangle.h" + +static llvm::symbolize::LLVMSymbolizer *Symbolizer = nullptr; +static bool Demangle = true; +static bool InlineFrames = true; + +static llvm::symbolize::LLVMSymbolizer *getDefaultSymbolizer() { + if (Symbolizer) + return Symbolizer; + llvm::symbolize::LLVMSymbolizer::Options Opts; + Opts.Demangle = Demangle; + Opts.UntagAddresses = true; + Symbolizer = new llvm::symbolize::LLVMSymbolizer(Opts); + return Symbolizer; +} + +static llvm::symbolize::PrinterConfig getDefaultPrinterConfig() { + llvm::symbolize::PrinterConfig Config; + Config.Pretty = false; + Config.Verbose = false; + Config.PrintFunctions = true; + Config.PrintAddress = false; + Config.SourceContextLines = 0; + return Config; +} + +static llvm::symbolize::ErrorHandler symbolize_error_handler( + llvm::raw_string_ostream &OS) { + return + [&](const llvm::ErrorInfoBase &ErrorInfo, llvm::StringRef ErrorBanner) { + OS << ErrorBanner; + ErrorInfo.log(OS); + OS << '\n'; + }; +} + +namespace __sanitizer { +int internal_snprintf(char *buffer, uintptr_t length, const char *format, ...); +} // namespace __sanitizer + +extern "C" { + +typedef uint64_t u64; + +bool __sanitizer_symbolize_code(const char *ModuleName, uint64_t ModuleOffset, + char *Buffer, int MaxLength) { + std::string Result; + { + llvm::raw_string_ostream OS(Result); + llvm::symbolize::PrinterConfig Config = getDefaultPrinterConfig(); + llvm::symbolize::Request Request{ModuleName, ModuleOffset}; + auto Printer = std::make_unique<llvm::symbolize::LLVMPrinter>( + OS, symbolize_error_handler(OS), Config); + + // TODO: it is necessary to set proper SectionIndex here. + // object::SectionedAddress::UndefSection works for only absolute addresses. + if (InlineFrames) { + auto ResOrErr = getDefaultSymbolizer()->symbolizeInlinedCode( + ModuleName, + {ModuleOffset, llvm::object::SectionedAddress::UndefSection}); + if (!ResOrErr) + return false; + Printer->print(Request, ResOrErr.get()); + } else { + auto ResOrErr = getDefaultSymbolizer()->symbolizeCode( + ModuleName, + {ModuleOffset, llvm::object::SectionedAddress::UndefSection}); + if (!ResOrErr) + return false; + Printer->print(Request, ResOrErr.get()); + } + } + return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s", + Result.c_str()) < MaxLength; +} + +bool __sanitizer_symbolize_data(const char *ModuleName, uint64_t ModuleOffset, + char *Buffer, int MaxLength) { + std::string Result; + { + llvm::symbolize::PrinterConfig Config = getDefaultPrinterConfig(); + llvm::raw_string_ostream OS(Result); + llvm::symbolize::Request Request{ModuleName, ModuleOffset}; + auto Printer = std::make_unique<llvm::symbolize::LLVMPrinter>( + OS, symbolize_error_handler(OS), Config); + + // TODO: it is necessary to set proper SectionIndex here. + // object::SectionedAddress::UndefSection works for only absolute addresses. + auto ResOrErr = getDefaultSymbolizer()->symbolizeData( + ModuleName, + {ModuleOffset, llvm::object::SectionedAddress::UndefSection}); + if (!ResOrErr) + return false; + Printer->print(Request, ResOrErr.get()); + } + return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s", + Result.c_str()) < MaxLength; +} + +bool __sanitizer_symbolize_frame(const char *ModuleName, uint64_t ModuleOffset, + char *Buffer, int MaxLength) { + std::string Result; + { + llvm::symbolize::PrinterConfig Config = getDefaultPrinterConfig(); + llvm::raw_string_ostream OS(Result); + llvm::symbolize::Request Request{ModuleName, ModuleOffset}; + auto Printer = std::make_unique<llvm::symbolize::LLVMPrinter>( + OS, symbolize_error_handler(OS), Config); + + // TODO: it is necessary to set proper SectionIndex here. + // object::SectionedAddress::UndefSection works for only absolute addresses. + auto ResOrErr = getDefaultSymbolizer()->symbolizeFrame( + ModuleName, + {ModuleOffset, llvm::object::SectionedAddress::UndefSection}); + if (!ResOrErr) + return false; + Printer->print(Request, ResOrErr.get()); + } + return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s", + Result.c_str()) < MaxLength; +} + +void __sanitizer_symbolize_flush() { + if (Symbolizer) + Symbolizer->flush(); +} + +bool __sanitizer_symbolize_demangle(const char *Name, char *Buffer, + int MaxLength) { + std::string Result; + if (!llvm::nonMicrosoftDemangle(Name, Result)) + return false; + return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s", + Result.c_str()) < MaxLength; +} + +bool __sanitizer_symbolize_set_demangle(bool Value) { + // Must be called before LLVMSymbolizer created. + if (Symbolizer) + return false; + Demangle = Value; + return true; +} + +bool __sanitizer_symbolize_set_inline_frames(bool Value) { + InlineFrames = Value; + return true; +} + +// Override __cxa_atexit and ignore callbacks. +// This prevents crashes in a configuration when the symbolizer +// is built into sanitizer runtime and consequently into the test process. +// LLVM libraries have some global objects destroyed during exit, +// so if the test process triggers any bugs after that, the symbolizer crashes. +// An example stack trace of such crash: +// +// #1 __cxa_throw +// #2 std::__u::__throw_system_error +// #3 std::__u::recursive_mutex::lock +// #4 __sanitizer_llvm::ManagedStaticBase::RegisterManagedStatic +// #5 __sanitizer_llvm::errorToErrorCode +// #6 __sanitizer_llvm::getFileAux +// #7 __sanitizer_llvm::MemoryBuffer::getFileOrSTDIN +// #10 __sanitizer_llvm::symbolize::LLVMSymbolizer::getOrCreateModuleInfo +// #13 __sanitizer::Symbolizer::SymbolizeData +// #14 __tsan::SymbolizeData +// #16 __tsan::ReportRace +// #18 __tsan_write4 +// #19 race() () at test/tsan/atexit4.cpp +// #20 cxa_at_exit_wrapper +// #21 __cxa_finalize +// #22 __do_fini +// +// For the standalone llvm-symbolizer this does not hurt, +// we just don't destroy few global objects on exit. +int __cxa_atexit(void (*f)(void *a), void *arg, void *dso) { return 0; } + +} // extern "C" diff --git a/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_wrappers.cpp b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_wrappers.cpp new file mode 100644 index 000000000000..cdac2333706d --- /dev/null +++ b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_wrappers.cpp @@ -0,0 +1,192 @@ +//===-- sanitizer_wrappers.cpp ----------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Redirect some functions to sanitizer interceptors. +// +//===----------------------------------------------------------------------===// + +#include <dlfcn.h> +#include <errno.h> +#include <fcntl.h> +#include <inttypes.h> +#include <stdarg.h> +#include <stdio.h> +#include <unistd.h> + +#include <tuple> + +namespace __sanitizer { +unsigned long internal_open(const char *filename, int flags); +unsigned long internal_open(const char *filename, int flags, unsigned mode); +unsigned long internal_close(int fd); +unsigned long internal_stat(const char *path, void *buf); +unsigned long internal_lstat(const char *path, void *buf); +unsigned long internal_fstat(int fd, void *buf); +size_t internal_strlen(const char *s); +unsigned long internal_mmap(void *addr, uintptr_t length, int prot, int flags, + int fd, unsigned long long offset); +void *internal_memcpy(void *dest, const void *src, unsigned long n); +// Used to propagate errno. +bool internal_iserror(uintptr_t retval, int *rverrno = 0); +} // namespace __sanitizer + +namespace { + +template <typename T> +struct GetTypes; + +template <typename R, typename... Args> +struct GetTypes<R(Args...)> { + using Result = R; + template <size_t i> + struct Arg { + using Type = typename std::tuple_element<i, std::tuple<Args...>>::type; + }; +}; + +#define LLVM_SYMBOLIZER_GET_FUNC(Function) \ + ((__interceptor_##Function) \ + ? (__interceptor_##Function) \ + : reinterpret_cast<decltype(&Function)>(dlsym(RTLD_NEXT, #Function))) + +#define LLVM_SYMBOLIZER_INTERCEPTOR1(Function, ...) \ + GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \ + GetTypes<__VA_ARGS__>::Arg<0>::Type) __attribute__((weak)); \ + GetTypes<__VA_ARGS__>::Result Function( \ + GetTypes<__VA_ARGS__>::Arg<0>::Type arg0) { \ + return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0); \ + } + +#define LLVM_SYMBOLIZER_INTERCEPTOR2(Function, ...) \ + GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \ + GetTypes<__VA_ARGS__>::Arg<0>::Type, \ + GetTypes<__VA_ARGS__>::Arg<1>::Type) __attribute__((weak)); \ + GetTypes<__VA_ARGS__>::Result Function( \ + GetTypes<__VA_ARGS__>::Arg<0>::Type arg0, \ + GetTypes<__VA_ARGS__>::Arg<1>::Type arg1) { \ + return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1); \ + } + +#define LLVM_SYMBOLIZER_INTERCEPTOR3(Function, ...) \ + GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \ + GetTypes<__VA_ARGS__>::Arg<0>::Type, \ + GetTypes<__VA_ARGS__>::Arg<1>::Type, \ + GetTypes<__VA_ARGS__>::Arg<2>::Type) __attribute__((weak)); \ + GetTypes<__VA_ARGS__>::Result Function( \ + GetTypes<__VA_ARGS__>::Arg<0>::Type arg0, \ + GetTypes<__VA_ARGS__>::Arg<1>::Type arg1, \ + GetTypes<__VA_ARGS__>::Arg<2>::Type arg2) { \ + return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1, arg2); \ + } + +#define LLVM_SYMBOLIZER_INTERCEPTOR4(Function, ...) \ + GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \ + GetTypes<__VA_ARGS__>::Arg<0>::Type, \ + GetTypes<__VA_ARGS__>::Arg<1>::Type, \ + GetTypes<__VA_ARGS__>::Arg<2>::Type, \ + GetTypes<__VA_ARGS__>::Arg<3>::Type) __attribute__((weak)); \ + GetTypes<__VA_ARGS__>::Result Function( \ + GetTypes<__VA_ARGS__>::Arg<0>::Type arg0, \ + GetTypes<__VA_ARGS__>::Arg<1>::Type arg1, \ + GetTypes<__VA_ARGS__>::Arg<2>::Type arg2, \ + GetTypes<__VA_ARGS__>::Arg<3>::Type arg3) { \ + return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1, arg2, arg3); \ + } + +} // namespace + +// C-style interface around internal sanitizer libc functions. +extern "C" { + +#define RETURN_OR_SET_ERRNO(T, res) \ + int rverrno; \ + if (__sanitizer::internal_iserror(res, &rverrno)) { \ + errno = rverrno; \ + return (T)-1; \ + } \ + return (T)res; + +int open(const char *filename, int flags, ...) { + unsigned long res; + if (flags | O_CREAT) { + va_list va; + va_start(va, flags); + unsigned mode = va_arg(va, unsigned); + va_end(va); + res = __sanitizer::internal_open(filename, flags, mode); + } else { + res = __sanitizer::internal_open(filename, flags); + } + RETURN_OR_SET_ERRNO(int, res); +} + +int close(int fd) { + unsigned long res = __sanitizer::internal_close(fd); + RETURN_OR_SET_ERRNO(int, res); +} + +#define STAT(func, arg, buf) \ + unsigned long res = __sanitizer::internal_##func(arg, buf); \ + RETURN_OR_SET_ERRNO(int, res); + +int stat(const char *path, struct stat *buf) { STAT(stat, path, buf); } + +int lstat(const char *path, struct stat *buf) { STAT(lstat, path, buf); } + +int fstat(int fd, struct stat *buf) { STAT(fstat, fd, buf); } + +// Redirect versioned stat functions to the __sanitizer::internal() as well. +int __xstat(int version, const char *path, struct stat *buf) { + STAT(stat, path, buf); +} + +int __lxstat(int version, const char *path, struct stat *buf) { + STAT(lstat, path, buf); +} + +int __fxstat(int version, int fd, struct stat *buf) { STAT(fstat, fd, buf); } + +size_t strlen(const char *s) { return __sanitizer::internal_strlen(s); } + +void *mmap(void *addr, size_t length, int prot, int flags, int fd, + off_t offset) { + unsigned long res = + __sanitizer::internal_mmap(addr, length, prot, flags, fd, offset); + RETURN_OR_SET_ERRNO(void *, res); +} + +LLVM_SYMBOLIZER_INTERCEPTOR3(read, ssize_t(int, void *, size_t)) +LLVM_SYMBOLIZER_INTERCEPTOR4(pread, ssize_t(int, void *, size_t, off_t)) +LLVM_SYMBOLIZER_INTERCEPTOR4(pread64, ssize_t(int, void *, size_t, off64_t)) +LLVM_SYMBOLIZER_INTERCEPTOR2(realpath, char *(const char *, char *)) + +LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_cond_broadcast, int(pthread_cond_t *)) +LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_cond_wait, + int(pthread_cond_t *, pthread_mutex_t *)) +LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_lock, int(pthread_mutex_t *)) +LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_unlock, int(pthread_mutex_t *)) +LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_destroy, int(pthread_mutex_t *)) +LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_mutex_init, + int(pthread_mutex_t *, + const pthread_mutexattr_t *)) +LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutexattr_destroy, + int(pthread_mutexattr_t *)) +LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutexattr_init, int(pthread_mutexattr_t *)) +LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_mutexattr_settype, + int(pthread_mutexattr_t *, int)) +LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_getspecific, void *(pthread_key_t)) +LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_key_create, + int(pthread_key_t *, void (*)(void *))) +LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_once, + int(pthread_once_t *, void (*)(void))) +LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_setspecific, + int(pthread_key_t, const void *)) +LLVM_SYMBOLIZER_INTERCEPTOR3(pthread_sigmask, + int(int, const sigset_t *, sigset_t *)) + +} // extern "C" diff --git a/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh new file mode 100755 index 000000000000..b4702339db59 --- /dev/null +++ b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh @@ -0,0 +1,202 @@ +#!/usr/bin/env bash +# +# Run as: CLANG=bin/clang build_symbolizer.sh out.o +# If you want to use a local copy of zlib, set ZLIB_SRC. +# zlib can be downloaded from http://www.zlib.net. +# +# Script compiles self-contained object file with symbolization code. +# +# Symbols exported by the object file will be used by Sanitizer runtime +# libraries to symbolize code/data in-process. +# +# FIXME: We should really be using a simpler approach to building this object +# file, and it should be available as a regular cmake rule. Conceptually, we +# want to be doing "ld -r" followed by "objcopy -G" to create a relocatable +# object file with only our entry points exposed. However, this does not work at +# present, see https://github.com/llvm/llvm-project/issues/30098. + +set -x +set -e +set -u + +SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) +SRC_DIR=$(readlink -f $SCRIPT_DIR/..) + +if [[ $# -ne 1 ]]; then + echo "Missing output file" + exit 1 +fi + +OUTPUT=$(readlink -f $1) +COMPILER_RT_SRC=$(readlink -f ${SCRIPT_DIR}/../../../..) +LLVM_SRC=${LLVM_SRC:-${COMPILER_RT_SRC}/../llvm} +LLVM_SRC=$(readlink -f $LLVM_SRC) + +CLANG="${CLANG:-`which clang`}" +CLANG_DIR=$(readlink -f $(dirname "$CLANG")) + +CC=$CLANG_DIR/clang +CXX=$CLANG_DIR/clang++ +TBLGEN=$CLANG_DIR/llvm-tblgen +OPT=$CLANG_DIR/opt +AR=$CLANG_DIR/llvm-ar +LINK=$CLANG_DIR/llvm-link + +for F in $CC $CXX $TBLGEN $LINK $OPT $AR; do + if [[ ! -x "$F" ]]; then + echo "Missing $F" + exit 1 + fi +done + +BUILD_DIR=${PWD}/symbolizer +mkdir -p $BUILD_DIR +cd $BUILD_DIR + +ZLIB_BUILD=${BUILD_DIR}/zlib +LIBCXX_BUILD=${BUILD_DIR}/libcxx +LLVM_BUILD=${BUILD_DIR}/llvm +SYMBOLIZER_BUILD=${BUILD_DIR}/symbolizer + +FLAGS=${FLAGS:-} +ZLIB_SRC=${ZLIB_SRC:-} +TARGET_TRIPLE=$($CC -print-target-triple $FLAGS) +if [[ "$FLAGS" =~ "-m32" ]] ; then + # Avoid new wrappers. + FLAGS+=" -U_FILE_OFFSET_BITS" +fi +FLAGS+=" -fPIC -flto -Oz -g0 -DNDEBUG -target $TARGET_TRIPLE -Wno-unused-command-line-argument" +FLAGS+=" -include ${SRC_DIR}/../sanitizer_redefine_builtins.h -DSANITIZER_COMMON_REDEFINE_BUILTINS_IN_STD -Wno-language-extension-token" + +LINKFLAGS="-fuse-ld=lld -target $TARGET_TRIPLE" + +# Build zlib. +if [[ ! -d ${ZLIB_BUILD} ]]; then + if [[ -z "${ZLIB_SRC}" ]]; then + git clone https://github.com/madler/zlib ${ZLIB_BUILD} + else + ZLIB_SRC=$(readlink -f $ZLIB_SRC) + mkdir -p ${ZLIB_BUILD} + cp -r ${ZLIB_SRC}/* ${ZLIB_BUILD}/ + fi +fi + +cd ${ZLIB_BUILD} +AR="${AR}" CC="${CC}" CFLAGS="$FLAGS -Wno-deprecated-non-prototype" RANLIB=/bin/true ./configure --static +make -j libz.a + +# Build and install libcxxabi and libcxx. +if [[ ! -f ${LLVM_BUILD}/build.ninja ]]; then + rm -rf ${LIBCXX_BUILD} + mkdir -p ${LIBCXX_BUILD} + cd ${LIBCXX_BUILD} + LIBCXX_FLAGS="${FLAGS} -Wno-macro-redefined" + cmake -GNinja \ + -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_C_COMPILER_WORKS=ON \ + -DCMAKE_CXX_COMPILER_WORKS=ON \ + -DCMAKE_C_COMPILER=$CC \ + -DCMAKE_CXX_COMPILER=$CXX \ + -DLIBCXX_ABI_NAMESPACE=__InternalSymbolizer \ + '-DLIBCXX_EXTRA_SITE_DEFINES=_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS;_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS' \ + -DCMAKE_C_FLAGS_RELEASE="${LIBCXX_FLAGS}" \ + -DCMAKE_CXX_FLAGS_RELEASE="${LIBCXX_FLAGS}" \ + -DLIBCXXABI_ENABLE_ASSERTIONS=OFF \ + -DLIBCXXABI_ENABLE_EXCEPTIONS=OFF \ + -DLIBCXXABI_USE_LLVM_UNWINDER=OFF \ + -DLIBCXX_ENABLE_ASSERTIONS=OFF \ + -DLIBCXX_ENABLE_EXCEPTIONS=OFF \ + -DLIBCXX_ENABLE_RTTI=OFF \ + -DCMAKE_SHARED_LINKER_FLAGS="$LINKFLAGS" \ + -DLIBCXX_ENABLE_SHARED=OFF \ + -DLIBCXXABI_ENABLE_SHARED=OFF \ + $LLVM_SRC/../runtimes +fi +cd ${LIBCXX_BUILD} +ninja cxx cxxabi + +FLAGS="${FLAGS} -fno-rtti -fno-exceptions" +LLVM_CFLAGS="${FLAGS} -Wno-global-constructors" +LLVM_CXXFLAGS="${LLVM_CFLAGS} -nostdinc++ -I${ZLIB_BUILD} -isystem ${LIBCXX_BUILD}/include -isystem ${LIBCXX_BUILD}/include/c++/v1" + +# Build LLVM. +if [[ ! -f ${LLVM_BUILD}/build.ninja ]]; then + rm -rf ${LLVM_BUILD} + mkdir -p ${LLVM_BUILD} + cd ${LLVM_BUILD} + cmake -GNinja \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_C_COMPILER_WORKS=ON \ + -DCMAKE_CXX_COMPILER_WORKS=ON \ + -DCMAKE_C_COMPILER=$CC \ + -DCMAKE_CXX_COMPILER=$CXX \ + -DLLVM_ENABLE_LIBCXX=ON \ + -DCMAKE_C_FLAGS_RELEASE="${LLVM_CFLAGS}" \ + -DCMAKE_CXX_FLAGS_RELEASE="${LLVM_CXXFLAGS}" \ + -DCMAKE_EXE_LINKER_FLAGS="$LINKFLAGS -stdlib=libc++ -L${LIBCXX_BUILD}/lib" \ + -DLLVM_TABLEGEN=$TBLGEN \ + -DLLVM_INCLUDE_TESTS=OFF \ + -DLLVM_ENABLE_ZLIB=ON \ + -DLLVM_ENABLE_ZSTD=OFF \ + -DLLVM_ENABLE_THREADS=OFF \ + $LLVM_SRC +fi +cd ${LLVM_BUILD} +ninja LLVMSymbolize LLVMObject LLVMBinaryFormat LLVMDebugInfoDWARF LLVMSupport LLVMDebugInfoPDB LLVMDebuginfod LLVMMC LLVMDemangle LLVMTextAPI LLVMTargetParser LLVMCore + +cd ${BUILD_DIR} +rm -rf ${SYMBOLIZER_BUILD} +mkdir ${SYMBOLIZER_BUILD} +cd ${SYMBOLIZER_BUILD} + +echo "Compiling..." +SYMBOLIZER_FLAGS="$LLVM_CXXFLAGS -I${LLVM_SRC}/include -I${LLVM_BUILD}/include -std=c++17" +$CXX $SYMBOLIZER_FLAGS ${SRC_DIR}/sanitizer_symbolize.cpp ${SRC_DIR}/sanitizer_wrappers.cpp -c +$AR rc symbolizer.a sanitizer_symbolize.o sanitizer_wrappers.o + +SYMBOLIZER_API_LIST=__sanitizer_symbolize_code +SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_data +SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_frame +SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_flush +SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_demangle +SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_set_demangle +SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_set_inline_frames + +LIBCXX_ARCHIVE_DIR=$(dirname $(find $LIBCXX_BUILD -name libc++.a | head -n1)) + +# Merge all the object files together and copy the resulting library back. +$LINK $LIBCXX_ARCHIVE_DIR/libc++.a \ + $LIBCXX_ARCHIVE_DIR/libc++abi.a \ + $LLVM_BUILD/lib/libLLVMSymbolize.a \ + $LLVM_BUILD/lib/libLLVMObject.a \ + $LLVM_BUILD/lib/libLLVMBinaryFormat.a \ + $LLVM_BUILD/lib/libLLVMDebugInfoDWARF.a \ + $LLVM_BUILD/lib/libLLVMSupport.a \ + $LLVM_BUILD/lib/libLLVMDebugInfoPDB.a \ + $LLVM_BUILD/lib/libLLVMDebugInfoMSF.a \ + $LLVM_BUILD/lib/libLLVMDebugInfoCodeView.a \ + $LLVM_BUILD/lib/libLLVMDebuginfod.a \ + $LLVM_BUILD/lib/libLLVMDemangle.a \ + $LLVM_BUILD/lib/libLLVMMC.a \ + $LLVM_BUILD/lib/libLLVMTextAPI.a \ + $LLVM_BUILD/lib/libLLVMTargetParser.a \ + $LLVM_BUILD/lib/libLLVMCore.a \ + $ZLIB_BUILD/libz.a \ + symbolizer.a \ + -ignore-non-bitcode -o all.bc + +echo "Optimizing..." +$OPT -passes=internalize -internalize-public-api-list=${SYMBOLIZER_API_LIST} all.bc -o opt.bc +$CC $FLAGS -fno-lto -c opt.bc -o symbolizer.o + +echo "Checking undefined symbols..." +export LC_ALL=C +nm -f posix -g symbolizer.o | cut -f 1,2 -d \ | sort -u > undefined.new +grep -Ev "^#|^$" $SCRIPT_DIR/global_symbols.txt | sort -u > expected.new +(diff -u expected.new undefined.new | grep -E "^\+[^+]") && \ + (echo "Failed: unexpected symbols"; exit 1) + +cp -f symbolizer.o $OUTPUT + +echo "Success!" diff --git a/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/scripts/global_symbols.txt b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/scripts/global_symbols.txt new file mode 100644 index 000000000000..2ee813835087 --- /dev/null +++ b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/scripts/global_symbols.txt @@ -0,0 +1,220 @@ +# This file is used to control symbols used by internal symbolizer. We want to +# avoid unexpected dependency on function intercepted by sanitizers. + +_GLOBAL_OFFSET_TABLE_ U +_ZN11__sanitizer13internal_mmapEPvjiiiy U +_ZN11__sanitizer13internal_mmapEPvmiiiy U +_ZN11__sanitizer13internal_openEPKcij U +_ZN11__sanitizer13internal_statEPKcPv U +_ZN11__sanitizer14internal_closeEi U +_ZN11__sanitizer14internal_fstatEiPv U +_ZN11__sanitizer14internal_lstatEPKcPv U +_ZN11__sanitizer15internal_strlenEPKc U +_ZN11__sanitizer16internal_iserrorEjPi U +_ZN11__sanitizer16internal_iserrorEmPi U +_ZN11__sanitizer17internal_snprintfEPcjPKcz U +_ZN11__sanitizer17internal_snprintfEPcmPKcz U +__aarch64_cas8_acq_rel U +__aarch64_ldadd4_acq_rel U +__aarch64_ldadd8_acq_rel U +__aarch64_ldadd8_relax U +__aarch64_swp8_acq_rel U +__ashldi3 U +__ashrdi3 U +__ctype_b_loc U +__ctype_get_mb_cur_max U +__cxa_atexit U +__divdi3 U +__dso_handle U +__errno_location U +__interceptor_pread w +__interceptor_pthread_cond_broadcast w +__interceptor_pthread_cond_wait w +__interceptor_pthread_getspecific w +__interceptor_pthread_key_create w +__interceptor_pthread_mutex_destroy w +__interceptor_pthread_mutex_init w +__interceptor_pthread_mutex_lock w +__interceptor_pthread_mutex_unlock w +__interceptor_pthread_mutexattr_destroy w +__interceptor_pthread_mutexattr_init w +__interceptor_pthread_mutexattr_settype w +__interceptor_pthread_once w +__interceptor_pthread_setspecific w +__interceptor_read w +__interceptor_realpath w +__isinf U +__isoc23_sscanf U +__isoc23_strtol U +__isoc23_strtoll U +__isoc23_strtoll_l U +__isoc23_strtoull U +__isoc23_strtoull_l U +__isoc23_vsscanf U +__isoc99_sscanf U +__isoc99_vsscanf U +__lshrdi3 U +__moddi3 U +__sanitizer_internal_memcpy U +__sanitizer_internal_memmove U +__sanitizer_internal_memset U +__sanitizer_symbolize_code T +__sanitizer_symbolize_data T +__sanitizer_symbolize_demangle T +__sanitizer_symbolize_flush T +__sanitizer_symbolize_frame T +__sanitizer_symbolize_set_demangle T +__sanitizer_symbolize_set_inline_frames T +__start___lcxx_override U +__stop___lcxx_override U +__strdup U +__udivdi3 U +__umoddi3 U +_exit U +abort U +access U +aligned_alloc U +arc4random U +bcmp U +calloc U +catclose U +catgets U +catopen U +ceil U +ceilf U +cfgetospeed U +clock_gettime U +dl_iterate_phdr U +dlsym U +dup U +dup2 U +environ U +execv U +execve U +exit U +fclose U +fflush U +fileno U +fopen U +fork U +fprintf U +fputc U +fputwc U +free U +freelocale U +fwrite U +getc U +getcwd U +getenv U +getpagesize U +getpid U +getpwuid U +getrlimit U +gettimeofday U +getuid U +getwc U +ioctl U +isalnum U +isalpha U +isatty U +islower U +isprint U +isspace U +isupper U +isxdigit U +log10 U +lseek U +lseek64 U +madvise U +malloc U +mbrlen U +mbrtowc U +mbsnrtowcs U +mbsrtowcs U +mbtowc U +memchr U +memcmp U +memcpy U +memmove U +memset U +mkdir U +modf U +munmap U +newlocale U +perror U +posix_madvise U +posix_memalign U +posix_spawn U +posix_spawn_file_actions_adddup2 U +posix_spawn_file_actions_addopen U +posix_spawn_file_actions_destroy U +posix_spawn_file_actions_init U +printf U +putchar U +qsort U +raise U +rand U +readlink U +realloc U +remove U +rename U +setrlimit U +setvbuf U +sigaction U +sigaltstack U +sigemptyset U +sigfillset U +sigprocmask U +snprintf U +sprintf U +srand U +sscanf U +stderr U +stdin U +stdout U +strcat U +strchr U +strcmp U +strcpy U +strdup U +strerror U +strerror_r U +strftime_l U +strncmp U +strncpy U +strrchr U +strsep U +strtod U +strtod_l U +strtof_l U +strtok_r U +strtol U +strtold_l U +strtoll U +strtoll_l U +strtoull U +strtoull_l U +syscall U +sysconf U +tcgetattr U +tolower U +toupper U +uname U +ungetc U +ungetwc U +unlink U +uselocale U +vasprintf U +vfprintf U +vsnprintf U +vsscanf U +wait4 U +waitpid U +wcrtomb U +wcslen U +wcsnrtombs U +wmemchr U +wmemcpy U +wmemmove U +wmemset U +write U |