Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_linux.cpp
35233 views
//===-- asan_linux.cpp ----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of AddressSanitizer, an address sanity checker.9//10// Linux-specific details.11//===----------------------------------------------------------------------===//1213#include "sanitizer_common/sanitizer_platform.h"14#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \15SANITIZER_SOLARIS1617# include <dlfcn.h>18# include <fcntl.h>19# include <limits.h>20# include <pthread.h>21# include <stdio.h>22# include <sys/mman.h>23# include <sys/resource.h>24# include <sys/syscall.h>25# include <sys/time.h>26# include <sys/types.h>27# include <unistd.h>28# include <unwind.h>2930# include "asan_interceptors.h"31# include "asan_internal.h"32# include "asan_premap_shadow.h"33# include "asan_thread.h"34# include "sanitizer_common/sanitizer_flags.h"35# include "sanitizer_common/sanitizer_hash.h"36# include "sanitizer_common/sanitizer_libc.h"37# include "sanitizer_common/sanitizer_procmaps.h"3839# if SANITIZER_FREEBSD40# include <sys/link_elf.h>41# endif4243# if SANITIZER_SOLARIS44# include <link.h>45# endif4647# if SANITIZER_ANDROID || SANITIZER_FREEBSD || SANITIZER_SOLARIS48# include <ucontext.h>49# elif SANITIZER_NETBSD50# include <link_elf.h>51# include <ucontext.h>52# else53# include <link.h>54# include <sys/ucontext.h>55# endif5657typedef enum {58ASAN_RT_VERSION_UNDEFINED = 0,59ASAN_RT_VERSION_DYNAMIC,60ASAN_RT_VERSION_STATIC,61} asan_rt_version_t;6263// FIXME: perhaps also store abi version here?64extern "C" {65SANITIZER_INTERFACE_ATTRIBUTE66asan_rt_version_t __asan_rt_version;67}6869namespace __asan {7071void InitializePlatformInterceptors() {}72void InitializePlatformExceptionHandlers() {}73bool IsSystemHeapAddress(uptr addr) { return false; }7475# if ASAN_PREMAP_SHADOW76uptr FindPremappedShadowStart(uptr shadow_size_bytes) {77uptr granularity = GetMmapGranularity();78uptr shadow_start = reinterpret_cast<uptr>(&__asan_shadow);79uptr premap_shadow_size = PremapShadowSize();80uptr shadow_size = RoundUpTo(shadow_size_bytes, granularity);81// We may have mapped too much. Release extra memory.82UnmapFromTo(shadow_start + shadow_size, shadow_start + premap_shadow_size);83return shadow_start;84}85# endif8687uptr FindDynamicShadowStart() {88uptr shadow_size_bytes = MemToShadowSize(kHighMemEnd);89# if ASAN_PREMAP_SHADOW90if (!PremapShadowFailed())91return FindPremappedShadowStart(shadow_size_bytes);92# endif9394return MapDynamicShadow(shadow_size_bytes, ASAN_SHADOW_SCALE,95/*min_shadow_base_alignment*/ 0, kHighMemEnd,96GetMmapGranularity());97}9899void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {100UNIMPLEMENTED();101}102103void FlushUnneededASanShadowMemory(uptr p, uptr size) {104// Since asan's mapping is compacting, the shadow chunk may be105// not page-aligned, so we only flush the page-aligned portion.106ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size));107}108109# if SANITIZER_ANDROID110// FIXME: should we do anything for Android?111void AsanCheckDynamicRTPrereqs() {}112void AsanCheckIncompatibleRT() {}113# else114static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,115void *data) {116VReport(2, "info->dlpi_name = %s\tinfo->dlpi_addr = %p\n", info->dlpi_name,117(void *)info->dlpi_addr);118119const char **name = (const char **)data;120121// Ignore first entry (the main program)122if (!*name) {123*name = "";124return 0;125}126127# if SANITIZER_LINUX128// Ignore vDSO. glibc versions earlier than 2.15 (and some patched129// by distributors) return an empty name for the vDSO entry, so130// detect this as well.131if (!info->dlpi_name[0] ||132internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)133return 0;134# endif135# if SANITIZER_FREEBSD136// Ignore vDSO.137if (internal_strcmp(info->dlpi_name, "[vdso]") == 0)138return 0;139# endif140141*name = info->dlpi_name;142return 1;143}144145static bool IsDynamicRTName(const char *libname) {146return internal_strstr(libname, "libclang_rt.asan") ||147internal_strstr(libname, "libasan.so");148}149150static void ReportIncompatibleRT() {151Report("Your application is linked against incompatible ASan runtimes.\n");152Die();153}154155void AsanCheckDynamicRTPrereqs() {156if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)157return;158159// Ensure that dynamic RT is the first DSO in the list160const char *first_dso_name = nullptr;161dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);162if (first_dso_name && first_dso_name[0] && !IsDynamicRTName(first_dso_name)) {163Report(164"ASan runtime does not come first in initial library list; "165"you should either link runtime to your application or "166"manually preload it with LD_PRELOAD.\n");167Die();168}169}170171void AsanCheckIncompatibleRT() {172if (ASAN_DYNAMIC) {173if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {174__asan_rt_version = ASAN_RT_VERSION_DYNAMIC;175} else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {176ReportIncompatibleRT();177}178} else {179if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {180// Ensure that dynamic runtime is not present. We should detect it181// as early as possible, otherwise ASan interceptors could bind to182// the functions in dynamic ASan runtime instead of the functions in183// system libraries, causing crashes later in ASan initialization.184MemoryMappingLayout proc_maps(/*cache_enabled*/ true);185char filename[PATH_MAX];186MemoryMappedSegment segment(filename, sizeof(filename));187while (proc_maps.Next(&segment)) {188if (IsDynamicRTName(segment.filename)) {189Report(190"Your application is linked against "191"incompatible ASan runtimes.\n");192Die();193}194}195__asan_rt_version = ASAN_RT_VERSION_STATIC;196} else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {197ReportIncompatibleRT();198}199}200}201# endif // SANITIZER_ANDROID202203# if ASAN_INTERCEPT_SWAPCONTEXT204constexpr u32 kAsanContextStackFlagsMagic = 0x51260eea;205206static int HashContextStack(const ucontext_t &ucp) {207MurMur2Hash64Builder hash(kAsanContextStackFlagsMagic);208hash.add(reinterpret_cast<uptr>(ucp.uc_stack.ss_sp));209hash.add(ucp.uc_stack.ss_size);210return static_cast<int>(hash.get());211}212213void SignContextStack(void *context) {214ucontext_t *ucp = reinterpret_cast<ucontext_t *>(context);215ucp->uc_stack.ss_flags = HashContextStack(*ucp);216}217218void ReadContextStack(void *context, uptr *stack, uptr *ssize) {219const ucontext_t *ucp = reinterpret_cast<const ucontext_t *>(context);220if (HashContextStack(*ucp) == ucp->uc_stack.ss_flags) {221*stack = reinterpret_cast<uptr>(ucp->uc_stack.ss_sp);222*ssize = ucp->uc_stack.ss_size;223return;224}225*stack = 0;226*ssize = 0;227}228# endif // ASAN_INTERCEPT_SWAPCONTEXT229230void *AsanDlSymNext(const char *sym) { return dlsym(RTLD_NEXT, sym); }231232bool HandleDlopenInit() {233// Not supported on this platform.234static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,235"Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");236return false;237}238239} // namespace __asan240241#endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||242// SANITIZER_SOLARIS243244245