blob: c4af3b9eb38781f63c1085af74e23dbfb255a16a [file] [log] [blame]
Elliott Hughes8eac9af2014-05-09 19:12:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes5e62b342018-10-25 11:00:00 -070017#pragma once
Elliott Hughes8eac9af2014-05-09 19:12:08 -070018
Elliott Hughes107935d2024-12-10 21:25:05 +000019#include <stddef.h>
Evgenii Stepanovd13e9a62016-07-15 16:31:42 -070020#include <stdint.h>
21
Elliott Hughes5e62b342018-10-25 11:00:00 -070022#define BIONIC_DISALLOW_COPY_AND_ASSIGN(TypeName) \
23 TypeName(const TypeName&) = delete; \
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -070024 void operator=(const TypeName&) = delete
Elliott Hughes8eac9af2014-05-09 19:12:08 -070025
Elliott Hughes5e62b342018-10-25 11:00:00 -070026#define BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
27 TypeName() = delete; \
28 BIONIC_DISALLOW_COPY_AND_ASSIGN(TypeName)
Elliott Hughes8eac9af2014-05-09 19:12:08 -070029
Christopher Ferris03eebcb2014-06-13 13:57:51 -070030#define BIONIC_ROUND_UP_POWER_OF_2(value) \
Christopher Ferris0b13f292015-12-16 16:11:04 -080031 ((sizeof(value) == 8) \
Christopher Ferris27047fa2014-07-14 18:47:23 -070032 ? (1UL << (64 - __builtin_clzl(static_cast<unsigned long>(value)))) \
Christopher Ferris0b13f292015-12-16 16:11:04 -080033 : (1UL << (32 - __builtin_clz(static_cast<unsigned int>(value)))))
Christopher Ferris03eebcb2014-06-13 13:57:51 -070034
Christopher Ferris93ea09f2017-10-05 15:18:47 -070035#if defined(__arm__)
Ryan Savitski9db34862019-08-27 21:54:18 +010036#define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined r14")
Christopher Ferris93ea09f2017-10-05 15:18:47 -070037#elif defined(__aarch64__)
38#define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined x30")
39#elif defined(__i386__)
40#define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined \%eip")
Elliott Hughes203b0b72022-10-07 21:32:52 +000041#elif defined(__riscv)
42#define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined ra")
Christopher Ferris93ea09f2017-10-05 15:18:47 -070043#elif defined(__x86_64__)
44#define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined \%rip")
45#endif
46
Tom Cherry4362f892017-11-14 08:50:43 -080047// The arraysize(arr) macro returns the # of elements in an array arr.
48// The expression is a compile-time constant, and therefore can be
49// used in defining new arrays, for example. If you use arraysize on
50// a pointer by mistake, you will get a compile-time error.
51//
52// One caveat is that arraysize() doesn't accept any array of an
53// anonymous type or a type defined inside a function.
54//
55// This template function declaration is used in defining arraysize.
56// Note that the function doesn't need an implementation, as we only
57// use its type.
58template <typename T, size_t N>
59char (&ArraySizeHelper(T (&array)[N]))[N]; // NOLINT(readability/casting)
60
61#define arraysize(array) (sizeof(ArraySizeHelper(array)))
62
George Burgess IVfa5410f2018-08-13 17:44:06 -070063// Used to inform clang's -Wimplicit-fallthrough that a fallthrough is intended. There's no way to
64// silence (or enable, apparently) -Wimplicit-fallthrough in C yet.
65#ifdef __cplusplus
66#define __BIONIC_FALLTHROUGH [[clang::fallthrough]]
67#else
68#define __BIONIC_FALLTHROUGH
69#endif
Peter Collingbourne191ecdc2019-08-07 19:06:00 -070070
Peter Collingbourne2528dab2020-03-12 18:29:52 -070071static inline uintptr_t untag_address(uintptr_t p) {
Peter Collingbourne191ecdc2019-08-07 19:06:00 -070072#if defined(__aarch64__)
Peter Collingbourne2528dab2020-03-12 18:29:52 -070073 return p & ((1ULL << 56) - 1);
Peter Collingbourne191ecdc2019-08-07 19:06:00 -070074#else
75 return p;
76#endif
77}
Peter Collingbourne2528dab2020-03-12 18:29:52 -070078
79template <typename T>
zijunzhaof0fb4182023-06-13 01:19:37 +000080static inline T* _Nonnull untag_address(T* _Nonnull p) {
Peter Collingbourne2528dab2020-03-12 18:29:52 -070081 return reinterpret_cast<T*>(untag_address(reinterpret_cast<uintptr_t>(p)));
82}
Florian Mayer4edc20d2024-10-30 14:24:26 -070083
84// MTE globals protects internal and external global variables. One of the main
85// things that MTE globals does is force all global variable accesses to go
86// through the GOT. In the linker though, some global variables are accessed (or
87// address-taken) prior to relocations being processed. Because relocations
88// haven't run yet, the GOT entry hasn't been populated, and this leads to
89// crashes. Thus, any globals used by the linker prior to relocation should be
90// annotated with this attribute, which suppresses tagging of this global
91// variable, restoring the pc-relative address computation.
92//
93// A way to find global variables that need this attribute is to build the
94// linker/libc with `SANITIZE_TARGET=memtag_globals`, push them onto a device
95// (it doesn't have to be MTE capable), and then run an executable using
96// LD_LIBRARY_PATH and using the linker in interpreter mode (e.g.
97// `LD_LIBRARY_PATH=/data/tmp/ /data/tmp/linker64 /data/tmp/my_binary`). A
98// good heuristic is that the global variable is in a file that should be
99// compiled with `-ffreestanding` (but there are global variables there that
100// don't need this attribute).
101#if __has_feature(memtag_globals)
102#define BIONIC_USED_BEFORE_LINKER_RELOCATES __attribute__((no_sanitize("memtag")))
103#else // __has_feature(memtag_globals)
104#define BIONIC_USED_BEFORE_LINKER_RELOCATES
105#endif // __has_feature(memtag_globals)