Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 17 | #pragma once |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 18 | |
Evgenii Stepanov | d13e9a6 | 2016-07-15 16:31:42 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 21 | #define BIONIC_DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 22 | TypeName(const TypeName&) = delete; \ |
Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 23 | void operator=(const TypeName&) = delete |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 25 | #define BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
| 26 | TypeName() = delete; \ |
| 27 | BIONIC_DISALLOW_COPY_AND_ASSIGN(TypeName) |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 28 | |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 29 | #define BIONIC_ROUND_UP_POWER_OF_2(value) \ |
Christopher Ferris | 0b13f29 | 2015-12-16 16:11:04 -0800 | [diff] [blame] | 30 | ((sizeof(value) == 8) \ |
Christopher Ferris | 27047fa | 2014-07-14 18:47:23 -0700 | [diff] [blame] | 31 | ? (1UL << (64 - __builtin_clzl(static_cast<unsigned long>(value)))) \ |
Christopher Ferris | 0b13f29 | 2015-12-16 16:11:04 -0800 | [diff] [blame] | 32 | : (1UL << (32 - __builtin_clz(static_cast<unsigned int>(value))))) |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 33 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 34 | static constexpr uintptr_t align_down(uintptr_t p, size_t align) { |
Evgenii Stepanov | d13e9a6 | 2016-07-15 16:31:42 -0700 | [diff] [blame] | 35 | return p & ~(align - 1); |
| 36 | } |
| 37 | |
Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 38 | static constexpr uintptr_t align_up(uintptr_t p, size_t align) { |
Evgenii Stepanov | d13e9a6 | 2016-07-15 16:31:42 -0700 | [diff] [blame] | 39 | return (p + align - 1) & ~(align - 1); |
| 40 | } |
| 41 | |
| 42 | template <typename T> |
zijunzhao | f0fb418 | 2023-06-13 01:19:37 +0000 | [diff] [blame] | 43 | static inline T* _Nonnull align_down(T* _Nonnull p, size_t align) { |
Evgenii Stepanov | d13e9a6 | 2016-07-15 16:31:42 -0700 | [diff] [blame] | 44 | return reinterpret_cast<T*>(align_down(reinterpret_cast<uintptr_t>(p), align)); |
| 45 | } |
| 46 | |
| 47 | template <typename T> |
zijunzhao | f0fb418 | 2023-06-13 01:19:37 +0000 | [diff] [blame] | 48 | static inline T* _Nonnull align_up(T* _Nonnull p, size_t align) { |
Evgenii Stepanov | d13e9a6 | 2016-07-15 16:31:42 -0700 | [diff] [blame] | 49 | return reinterpret_cast<T*>(align_up(reinterpret_cast<uintptr_t>(p), align)); |
| 50 | } |
| 51 | |
Christopher Ferris | 93ea09f | 2017-10-05 15:18:47 -0700 | [diff] [blame] | 52 | #if defined(__arm__) |
Ryan Savitski | 9db3486 | 2019-08-27 21:54:18 +0100 | [diff] [blame] | 53 | #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined r14") |
Christopher Ferris | 93ea09f | 2017-10-05 15:18:47 -0700 | [diff] [blame] | 54 | #elif defined(__aarch64__) |
| 55 | #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined x30") |
| 56 | #elif defined(__i386__) |
| 57 | #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined \%eip") |
Elliott Hughes | 203b0b7 | 2022-10-07 21:32:52 +0000 | [diff] [blame] | 58 | #elif defined(__riscv) |
| 59 | #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined ra") |
Christopher Ferris | 93ea09f | 2017-10-05 15:18:47 -0700 | [diff] [blame] | 60 | #elif defined(__x86_64__) |
| 61 | #define BIONIC_STOP_UNWIND asm volatile(".cfi_undefined \%rip") |
| 62 | #endif |
| 63 | |
Tom Cherry | 4362f89 | 2017-11-14 08:50:43 -0800 | [diff] [blame] | 64 | // The arraysize(arr) macro returns the # of elements in an array arr. |
| 65 | // The expression is a compile-time constant, and therefore can be |
| 66 | // used in defining new arrays, for example. If you use arraysize on |
| 67 | // a pointer by mistake, you will get a compile-time error. |
| 68 | // |
| 69 | // One caveat is that arraysize() doesn't accept any array of an |
| 70 | // anonymous type or a type defined inside a function. |
| 71 | // |
| 72 | // This template function declaration is used in defining arraysize. |
| 73 | // Note that the function doesn't need an implementation, as we only |
| 74 | // use its type. |
| 75 | template <typename T, size_t N> |
| 76 | char (&ArraySizeHelper(T (&array)[N]))[N]; // NOLINT(readability/casting) |
| 77 | |
| 78 | #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
| 79 | |
George Burgess IV | fa5410f | 2018-08-13 17:44:06 -0700 | [diff] [blame] | 80 | // Used to inform clang's -Wimplicit-fallthrough that a fallthrough is intended. There's no way to |
| 81 | // silence (or enable, apparently) -Wimplicit-fallthrough in C yet. |
| 82 | #ifdef __cplusplus |
| 83 | #define __BIONIC_FALLTHROUGH [[clang::fallthrough]] |
| 84 | #else |
| 85 | #define __BIONIC_FALLTHROUGH |
| 86 | #endif |
Peter Collingbourne | 191ecdc | 2019-08-07 19:06:00 -0700 | [diff] [blame] | 87 | |
Peter Collingbourne | 2528dab | 2020-03-12 18:29:52 -0700 | [diff] [blame] | 88 | static inline uintptr_t untag_address(uintptr_t p) { |
Peter Collingbourne | 191ecdc | 2019-08-07 19:06:00 -0700 | [diff] [blame] | 89 | #if defined(__aarch64__) |
Peter Collingbourne | 2528dab | 2020-03-12 18:29:52 -0700 | [diff] [blame] | 90 | return p & ((1ULL << 56) - 1); |
Peter Collingbourne | 191ecdc | 2019-08-07 19:06:00 -0700 | [diff] [blame] | 91 | #else |
| 92 | return p; |
| 93 | #endif |
| 94 | } |
Peter Collingbourne | 2528dab | 2020-03-12 18:29:52 -0700 | [diff] [blame] | 95 | |
| 96 | template <typename T> |
zijunzhao | f0fb418 | 2023-06-13 01:19:37 +0000 | [diff] [blame] | 97 | static inline T* _Nonnull untag_address(T* _Nonnull p) { |
Peter Collingbourne | 2528dab | 2020-03-12 18:29:52 -0700 | [diff] [blame] | 98 | return reinterpret_cast<T*>(untag_address(reinterpret_cast<uintptr_t>(p))); |
| 99 | } |
Florian Mayer | 4edc20d | 2024-10-30 14:24:26 -0700 | [diff] [blame] | 100 | |
| 101 | // MTE globals protects internal and external global variables. One of the main |
| 102 | // things that MTE globals does is force all global variable accesses to go |
| 103 | // through the GOT. In the linker though, some global variables are accessed (or |
| 104 | // address-taken) prior to relocations being processed. Because relocations |
| 105 | // haven't run yet, the GOT entry hasn't been populated, and this leads to |
| 106 | // crashes. Thus, any globals used by the linker prior to relocation should be |
| 107 | // annotated with this attribute, which suppresses tagging of this global |
| 108 | // variable, restoring the pc-relative address computation. |
| 109 | // |
| 110 | // A way to find global variables that need this attribute is to build the |
| 111 | // linker/libc with `SANITIZE_TARGET=memtag_globals`, push them onto a device |
| 112 | // (it doesn't have to be MTE capable), and then run an executable using |
| 113 | // LD_LIBRARY_PATH and using the linker in interpreter mode (e.g. |
| 114 | // `LD_LIBRARY_PATH=/data/tmp/ /data/tmp/linker64 /data/tmp/my_binary`). A |
| 115 | // good heuristic is that the global variable is in a file that should be |
| 116 | // compiled with `-ffreestanding` (but there are global variables there that |
| 117 | // don't need this attribute). |
| 118 | #if __has_feature(memtag_globals) |
| 119 | #define BIONIC_USED_BEFORE_LINKER_RELOCATES __attribute__((no_sanitize("memtag"))) |
| 120 | #else // __has_feature(memtag_globals) |
| 121 | #define BIONIC_USED_BEFORE_LINKER_RELOCATES |
| 122 | #endif // __has_feature(memtag_globals) |