1//===-- platform.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#ifndef SCUDO_PLATFORM_H_
10#define SCUDO_PLATFORM_H_
11
12// Transitive includes of stdint.h specify some of the defines checked below.
13#include <stdint.h>
14
15#if defined(__linux__) && !defined(__TRUSTY__)
16#define SCUDO_LINUX 1
17#else
18#define SCUDO_LINUX 0
19#endif
20
21// See https://android.googlesource.com/platform/bionic/+/master/docs/defines.md
22#if defined(__BIONIC__)
23#define SCUDO_ANDROID 1
24// Transitive includes of unistd.h will get PAGE_SIZE if it is defined.
25#include <unistd.h>
26#if defined(PAGE_SIZE)
27#define SCUDO_PAGE_SIZE PAGE_SIZE
28#endif
29#else
30#define SCUDO_ANDROID 0
31#endif
32
33#if defined(__Fuchsia__)
34#define SCUDO_FUCHSIA 1
35#else
36#define SCUDO_FUCHSIA 0
37#endif
38
39#if defined(__TRUSTY__)
40#define SCUDO_TRUSTY 1
41#else
42#define SCUDO_TRUSTY 0
43#endif
44
45#if defined(__riscv) && (__riscv_xlen == 64)
46#define SCUDO_RISCV64 1
47#else
48#define SCUDO_RISCV64 0
49#endif
50
51#if defined(__LP64__)
52#define SCUDO_WORDSIZE 64U
53#else
54#define SCUDO_WORDSIZE 32U
55#endif
56
57#if SCUDO_WORDSIZE == 64U
58#define FIRST_32_SECOND_64(a, b) (b)
59#else
60#define FIRST_32_SECOND_64(a, b) (a)
61#endif
62
63#ifndef SCUDO_CAN_USE_PRIMARY64
64#define SCUDO_CAN_USE_PRIMARY64 (SCUDO_WORDSIZE == 64U)
65#endif
66
67#ifndef SCUDO_CAN_USE_MTE
68#define SCUDO_CAN_USE_MTE (SCUDO_LINUX || SCUDO_TRUSTY)
69#endif
70
71#ifndef SCUDO_ENABLE_HOOKS
72#define SCUDO_ENABLE_HOOKS 0
73#endif
74
75#ifndef SCUDO_MIN_ALIGNMENT_LOG
76// We force malloc-type functions to be aligned to std::max_align_t, but there
77// is no reason why the minimum alignment for all other functions can't be 8
78// bytes. Except obviously for applications making incorrect assumptions.
79// TODO(kostyak): define SCUDO_MIN_ALIGNMENT_LOG 3
80#define SCUDO_MIN_ALIGNMENT_LOG FIRST_32_SECOND_64(3, 4)
81#endif
82
83#if defined(__aarch64__)
84#define SCUDO_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 48)
85#else
86#define SCUDO_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
87#endif
88
89// Older gcc have issues aligning to a constexpr, and require an integer.
90// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56859 among others.
91#if defined(__powerpc__) || defined(__powerpc64__)
92#define SCUDO_CACHE_LINE_SIZE 128
93#else
94#define SCUDO_CACHE_LINE_SIZE 64
95#endif
96
97#define SCUDO_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 12)
98
99#endif // SCUDO_PLATFORM_H_
100