Path: blob/main/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan_platform.h
35233 views
//===-- dfsan_platform.h ----------------------------------------*- C++ -*-===//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 DataFlowSanitizer.9//10// Platform specific information for DFSan.11//===----------------------------------------------------------------------===//1213#ifndef DFSAN_PLATFORM_H14#define DFSAN_PLATFORM_H1516#include "sanitizer_common/sanitizer_common.h"17#include "sanitizer_common/sanitizer_platform.h"1819namespace __dfsan {2021using __sanitizer::uptr;2223// TODO: The memory mapping code to setup a 1:1 shadow is based on msan.24// Consider refactoring these into a shared implementation.2526struct MappingDesc {27uptr start;28uptr end;29enum Type {30INVALID = 1,31ALLOCATOR = 2,32APP = 4,33SHADOW = 8,34ORIGIN = 16,35} type;36const char *name;37};3839// Note: MappingDesc::ALLOCATOR entries are only used to check for memory40// layout compatibility. The actual allocation settings are in41// dfsan_allocator.cpp, which need to be kept in sync.42#if SANITIZER_LINUX && SANITIZER_WORDSIZE == 644344# if defined(__aarch64__)45// The mapping assumes 48-bit VMA. AArch64 maps:46// - 0x0000000000000-0x0100000000000: 39/42/48-bits program own segments47// - 0x0a00000000000-0x0b00000000000: 48-bits PIE program segments48// Ideally, this would extend to 0x0c00000000000 (2^45 bytes - the49// maximum ASLR region for 48-bit VMA) but it is too hard to fit in50// the larger app/shadow/origin regions.51// - 0x0e00000000000-0x1000000000000: 48-bits libraries segments52const MappingDesc kMemoryLayout[] = {53{0X0000000000000, 0X0100000000000, MappingDesc::APP, "app-10-13"},54{0X0100000000000, 0X0200000000000, MappingDesc::SHADOW, "shadow-14"},55{0X0200000000000, 0X0300000000000, MappingDesc::INVALID, "invalid"},56{0X0300000000000, 0X0400000000000, MappingDesc::ORIGIN, "origin-14"},57{0X0400000000000, 0X0600000000000, MappingDesc::SHADOW, "shadow-15"},58{0X0600000000000, 0X0800000000000, MappingDesc::ORIGIN, "origin-15"},59{0X0800000000000, 0X0A00000000000, MappingDesc::INVALID, "invalid"},60{0X0A00000000000, 0X0B00000000000, MappingDesc::APP, "app-14"},61{0X0B00000000000, 0X0C00000000000, MappingDesc::SHADOW, "shadow-10-13"},62{0X0C00000000000, 0X0D00000000000, MappingDesc::INVALID, "invalid"},63{0X0D00000000000, 0X0E00000000000, MappingDesc::ORIGIN, "origin-10-13"},64{0X0E00000000000, 0X0E40000000000, MappingDesc::ALLOCATOR, "allocator"},65{0X0E40000000000, 0X1000000000000, MappingDesc::APP, "app-15"},66};67# define MEM_TO_SHADOW(mem) ((uptr)mem ^ 0xB00000000000ULL)68# define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x200000000000ULL)6970# else71// All of the following configurations are supported.72// ASLR disabled: main executable and DSOs at 0x55555000000073// PIE and ASLR: main executable and DSOs at 0x7f000000000074// non-PIE: main executable below 0x100000000, DSOs at 0x7f000000000075// Heap at 0x700000000000.76const MappingDesc kMemoryLayout[] = {77{0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},78{0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},79{0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},80{0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},81{0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},82{0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},83{0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},84{0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},85{0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},86{0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},87{0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},88{0x700000000000ULL, 0x740000000000ULL, MappingDesc::ALLOCATOR, "allocator"},89{0x740000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};90# define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)91# define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)92# endif9394#else95# error "Unsupported platform"96#endif9798const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);99100#define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))101102#ifndef __clang__103__attribute__((optimize("unroll-loops")))104#endif105inline bool106addr_is_type(uptr addr, int mapping_types) {107// It is critical for performance that this loop is unrolled (because then it is108// simplified into just a few constant comparisons).109#ifdef __clang__110# pragma unroll111#endif112for (unsigned i = 0; i < kMemoryLayoutSize; ++i)113if ((kMemoryLayout[i].type & mapping_types) &&114addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)115return true;116return false;117}118119#define MEM_IS_APP(mem) \120(addr_is_type((uptr)(mem), MappingDesc::APP | MappingDesc::ALLOCATOR))121#define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)122#define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)123124} // namespace __dfsan125126#endif127128129