1//===-- sanitizer_mac.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is shared between various sanitizers' runtime libraries and
10// provides definitions for OSX-specific functions.
11//===----------------------------------------------------------------------===//
12#ifndef SANITIZER_APPLE_H
13#define SANITIZER_APPLE_H
14
15#include "sanitizer_common.h"
16#include "sanitizer_platform.h"
17#if SANITIZER_APPLE
18#include "sanitizer_posix.h"
19
20// The earliest (macOS-aligned) version that requires debug memory
21# define DARWIN_DEBUG_MEMORY_VERSION_FLOOR (MacosVersion(27, 0))
22
23// VM size threshold threshold above which we will use debug memory (448GB)
24# define DARWIN_DEBUG_MEMORY_VMOFFSET_FLOOR 0x7000000000UL
25
26// The lowest address which sanitizers should expect to map
27// on embedded devices with debug memory (VM_MEMORY_DEBUG)
28# define DARWIN_DEBUG_MEMORY_START 0x40000000000UL
29
30namespace __sanitizer {
31
32struct MemoryMappingLayoutData {
33 int current_image;
34 u32 current_magic;
35 u32 current_filetype;
36 ModuleArch current_arch;
37 u8 current_uuid[kModuleUUIDSize];
38 int current_load_cmd_count;
39 const char *current_load_cmd_addr;
40 bool current_instrumented;
41};
42
43template <typename VersionType>
44struct VersionBase {
45 u16 major;
46 u16 minor;
47
48 VersionBase(u16 major, u16 minor) : major(major), minor(minor) {}
49
50 bool operator>=(const VersionType &other) const {
51 return major > other.major ||
52 (major == other.major && minor >= other.minor);
53 }
54 bool operator<(const VersionType &other) const { return !(*this >= other); }
55};
56
57template <typename VersionType>
58bool operator==(const VersionBase<VersionType> &self,
59 const VersionBase<VersionType> &other) {
60 return self.major == other.major && self.minor == other.minor;
61}
62
63struct MacosVersion : VersionBase<MacosVersion> {
64 MacosVersion(u16 major, u16 minor) : VersionBase(major, minor) {}
65};
66
67struct DarwinKernelVersion : VersionBase<DarwinKernelVersion> {
68 DarwinKernelVersion(u16 major, u16 minor) : VersionBase(major, minor) {}
69};
70
71struct ReservedRange {
72 uptr beg, end;
73};
74
75MacosVersion GetMacosAlignedVersion();
76DarwinKernelVersion GetDarwinKernelVersion();
77void GetAppRanges(InternalMmapVector<ReservedRange>& ranges);
78
79extern bool debug_region_activated;
80ALWAYS_INLINE static bool DebugMemoryActive() { return debug_region_activated; }
81bool ActivateDebugMemory();
82uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
83 uptr* largest_gap_found, uptr* max_occupied_addr,
84 bool use_debug_vm);
85
86char **GetEnviron();
87
88void RestrictMemoryToMaxAddress(uptr max_address);
89bool MemoryRangeIsKernelReserved(uptr range_start, uptr range_end);
90
91using ThreadEventCallback = void (*)(uptr thread);
92using ThreadCreateEventCallback = void (*)(uptr thread, bool gcd_worker);
93struct ThreadEventCallbacks {
94 ThreadCreateEventCallback create;
95 ThreadEventCallback start;
96 ThreadEventCallback terminate;
97 ThreadEventCallback destroy;
98};
99
100void InstallPthreadIntrospectionHook(const ThreadEventCallbacks &callbacks);
101
102} // namespace __sanitizer
103
104#endif // SANITIZER_APPLE
105#endif // SANITIZER_APPLE_H
106