//===------------------------ nsan_platform.h -------------------*- 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 // //===----------------------------------------------------------------------===// // // Platform specific information for NSan. // //===----------------------------------------------------------------------===// #ifndef NSAN_PLATFORM_H #define NSAN_PLATFORM_H namespace __nsan { // NSan uses two regions of memory to store information: // - 'shadow memory' stores the shadow copies of numerical values stored in // application memory. // - 'shadow types' is used to determine which value type each byte of memory // belongs to. This makes sure that we always know whether a shadow value is // valid. Shadow values may be tampered with using access through other // pointer types (type punning). Each byte stores: // - bit 1-0: whether the corresponding value is of unknown (00), // float (01), double (10), or long double (11) type. // - bit 5-2: the index of this byte in the value, or 0000 if type is // unknown. // This allows handling unaligned loat load/stores by checking that a load // with a given alignment corresponds to the alignment of the store. // Any store of a non-floating point type invalidates the corresponding // bytes, so that subsequent overlapping loads (aligned or not) know that // the corresponding shadow value is no longer valid. // On Linux/x86_64, memory is laid out as follows: // // +--------------------+ 0x800000000000 (top of memory) // | application memory | // +--------------------+ 0x700000008000 (kAppAddr) // | | // | unused | // | | // +--------------------+ 0x440000008000 // | allocator | // +--------------------+ 0x400000000000 (kHeapMemBeg) // | shadow memory | // +--------------------+ 0x200000000000 (kShadowAddr) // | shadow types | // +--------------------+ 0x100000000000 (kTypesAddr) // | reserved by kernel | // +--------------------+ 0x000000000000 // // // To derive a shadow memory address from an application memory address, // bits 44-46 are cleared to bring the address into the range // [0x000000000000,0x100000000000). We scale to account for the fact that a // shadow value takes twice as much space as the original value. // Then we add kShadowAddr to put the shadow relative offset into the shadow // memory. See getShadowAddrFor(). // The process is similar for the shadow types. // The ratio of app to shadow memory. enum { … }; // The original value type of a byte in app memory. Uses LLVM terminology: // https://llvm.org/docs/LangRef.html#floating-point-types // FIXME: support half and bfloat. enum ValueType { … }; // The size of ValueType encoding, in bits. enum { … }; #if defined(__x86_64__) struct Mapping { … }; #else #error "NSan not supported for this platform!" #endif enum MappingType { … }; template <typename Mapping, int Type> uptr MappingImpl() { … } template <int Type> uptr MappingArchImpl() { … } ALWAYS_INLINE uptr AppAddr() { … } ALWAYS_INLINE uptr AllocatorAddr() { … } ALWAYS_INLINE uptr ShadowAddr() { … } ALWAYS_INLINE uptr TypesAddr() { … } ALWAYS_INLINE uptr ShadowMask() { … } } // end namespace __nsan #endif