2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2022-11-26 11:18:30 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2022-10-04 19:04:13 +00:00
|
|
|
* Copyright (c) 2022, Nico Weber <thakis@chromium.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-09 13:46:22 +00:00
|
|
|
#pragma once
|
|
|
|
|
AK: Include <features.h> before checking for platform macros
AK/Platform.h did not include any other header file, but expected
various macros to be defined. While many of the macros checked here are
predefined by the compiler (i.e. GCC's TARGET_OS_CPP_BUILTINS), some
may be defined by the system headers instead. In particular, so is
__GLIBC__ on glibc-based systems.
We have to include some system header for getting __GLIBC__ (or not).
It could be possible to include something relatively small and
innocuous, like <string.h> for example, but that would still clutter
the name space and make other code that would use <string.h>
functionality, but forget to include it, build on accident; we wouldn't
want that. At the end of the day, the header that actually defines
__GLIBC__ (or not) is <features.h>. It's typically included from other
glibc headers, and not by user code directly, which makes it unlikely
to mask other code accidentlly forgetting to include it, since it
wouldn't include it in the first place.
<features.h> is not defined by POSIX and could be missing on other
systems (but it seems to be present at least when using either glibc or
musl), so guard its inclusion with __has_include().
Specifically, this fixes AK/StackInfo.cpp not picking up the glibc code
path in the cross aarch64-gnu (GNU/Hurd on 64-bit ARM) Lagom build.
2024-04-25 12:34:27 +00:00
|
|
|
#if __has_include(<features.h>)
|
|
|
|
# include <features.h>
|
|
|
|
#endif
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#ifndef USING_AK_GLOBALLY
|
|
|
|
# define USING_AK_GLOBALLY 1
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 13:46:22 +00:00
|
|
|
#ifdef __x86_64__
|
2023-03-02 11:47:03 +00:00
|
|
|
# define AK_IS_ARCH_X86_64() 1
|
|
|
|
#else
|
|
|
|
# define AK_IS_ARCH_X86_64() 0
|
2019-07-09 13:46:22 +00:00
|
|
|
#endif
|
|
|
|
|
2023-09-03 17:44:26 +00:00
|
|
|
#if defined(__i386__) && !defined(__x86_64__)
|
|
|
|
# define AK_IS_ARCH_I386() 1
|
|
|
|
#else
|
|
|
|
# define AK_IS_ARCH_I386() 0
|
|
|
|
#endif
|
|
|
|
|
2021-10-11 22:38:16 +00:00
|
|
|
#ifdef __aarch64__
|
2023-03-02 11:47:03 +00:00
|
|
|
# define AK_IS_ARCH_AARCH64() 1
|
|
|
|
#else
|
|
|
|
# define AK_IS_ARCH_AARCH64() 0
|
2021-10-11 22:38:16 +00:00
|
|
|
#endif
|
|
|
|
|
2023-08-20 17:46:05 +00:00
|
|
|
#if defined(__riscv) && __riscv_xlen == 64
|
|
|
|
# define AK_IS_ARCH_RISCV64() 1
|
|
|
|
#else
|
|
|
|
# define AK_IS_ARCH_RISCV64() 0
|
|
|
|
#endif
|
|
|
|
|
2024-06-24 21:13:27 +00:00
|
|
|
#if defined(__ppc64__) || defined(__PPC64__)
|
|
|
|
# define AK_IS_ARCH_PPC64() 1
|
|
|
|
#else
|
|
|
|
# define AK_IS_ARCH_PPC64() 0
|
|
|
|
#endif
|
|
|
|
#if defined(__ppc64le__) || defined(__PPC64LE__)
|
|
|
|
# define AK_IS_ARCH_PPC64LE() 1
|
|
|
|
#else
|
|
|
|
# define AK_IS_ARCH_PPC64LE() 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__ppc__) || defined(__PPC__) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__)
|
|
|
|
# define AK_IS_ARCH_PPC() 1
|
|
|
|
#else
|
|
|
|
# define AK_IS_ARCH_PPC() 0
|
|
|
|
#endif
|
|
|
|
|
2022-11-20 03:23:14 +00:00
|
|
|
#ifdef __wasm32__
|
2023-03-02 11:47:03 +00:00
|
|
|
# define AK_IS_ARCH_WASM32() 1
|
|
|
|
#else
|
|
|
|
# define AK_IS_ARCH_WASM32() 0
|
2022-11-20 03:23:14 +00:00
|
|
|
#endif
|
|
|
|
|
2022-09-20 16:09:33 +00:00
|
|
|
#if (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8) || defined(_WIN64)
|
|
|
|
# define AK_ARCH_64_BIT
|
|
|
|
#else
|
|
|
|
# define AK_ARCH_32_BIT
|
|
|
|
#endif
|
|
|
|
|
2023-04-08 05:34:01 +00:00
|
|
|
#if defined(__clang__) || defined(__CLION_IDE__) || defined(__CLION_IDE_)
|
2022-10-04 19:04:13 +00:00
|
|
|
# define AK_COMPILER_CLANG
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
# define AK_COMPILER_GCC
|
|
|
|
#endif
|
|
|
|
|
2024-05-17 16:39:29 +00:00
|
|
|
#if defined(AK_COMPILER_CLANG) && defined(__apple_build_version__)
|
|
|
|
# define AK_COMPILER_APPLE_CLANG
|
|
|
|
#endif
|
|
|
|
|
2023-09-03 18:59:06 +00:00
|
|
|
#if defined(__GLIBC__)
|
|
|
|
# define AK_LIBC_GLIBC
|
|
|
|
# define AK_LIBC_GLIBC_PREREQ(maj, min) __GLIBC_PREREQ((maj), (min))
|
|
|
|
#else
|
|
|
|
# define AK_LIBC_GLIBC_PREREQ(maj, min) 0
|
|
|
|
#endif
|
|
|
|
|
2022-10-01 12:23:49 +00:00
|
|
|
#if defined(__serenity__)
|
|
|
|
# define AK_OS_SERENITY
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
# define AK_OS_LINUX
|
|
|
|
#endif
|
|
|
|
|
2024-02-27 13:32:29 +00:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__) && !defined(__IOS__)
|
2020-12-27 22:38:32 +00:00
|
|
|
# define AK_OS_MACOS
|
|
|
|
# define AK_OS_BSD_GENERIC
|
|
|
|
#endif
|
|
|
|
|
2024-02-27 13:32:29 +00:00
|
|
|
#if defined(__IOS__)
|
|
|
|
# define AK_OS_IOS
|
|
|
|
# define AK_OS_BSD_GENERIC
|
|
|
|
#endif
|
|
|
|
|
2022-10-01 12:23:49 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
# define AK_OS_BSD_GENERIC
|
|
|
|
# define AK_OS_FREEBSD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__NetBSD__)
|
|
|
|
# define AK_OS_BSD_GENERIC
|
|
|
|
# define AK_OS_NETBSD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
# define AK_OS_BSD_GENERIC
|
|
|
|
# define AK_OS_OPENBSD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__DragonFly__)
|
2020-12-27 22:38:32 +00:00
|
|
|
# define AK_OS_BSD_GENERIC
|
2022-10-01 12:23:49 +00:00
|
|
|
# define AK_OS_DRAGONFLY
|
2020-12-27 22:38:32 +00:00
|
|
|
#endif
|
|
|
|
|
2023-02-25 16:57:26 +00:00
|
|
|
#if defined(__sun)
|
|
|
|
# define AK_OS_BSD_GENERIC
|
|
|
|
# define AK_OS_SOLARIS
|
|
|
|
#endif
|
|
|
|
|
2023-08-27 18:41:32 +00:00
|
|
|
#if defined(__HAIKU__)
|
|
|
|
# define AK_OS_HAIKU
|
|
|
|
#endif
|
|
|
|
|
2023-09-03 19:20:58 +00:00
|
|
|
#if defined(__gnu_hurd__)
|
|
|
|
# define AK_OS_GNU_HURD
|
|
|
|
#endif
|
|
|
|
|
2024-04-04 19:55:48 +00:00
|
|
|
#if defined(__MACH__)
|
|
|
|
# define AK_OS_MACH
|
|
|
|
#endif
|
|
|
|
|
2022-09-20 07:15:12 +00:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
# define AK_OS_WINDOWS
|
|
|
|
#endif
|
|
|
|
|
2022-07-11 07:04:39 +00:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
# define STR(x) __STR(x)
|
|
|
|
# define __STR(x) #x
|
|
|
|
# if __ANDROID_API__ < 30
|
|
|
|
# pragma message "Invalid android API " STR(__ANDROID_API__)
|
|
|
|
# error "Build configuration not tested on configured Android API version"
|
|
|
|
# endif
|
|
|
|
# undef STR
|
|
|
|
# undef __STR
|
|
|
|
# define AK_OS_ANDROID
|
|
|
|
#endif
|
|
|
|
|
2022-11-20 03:23:14 +00:00
|
|
|
#if defined(__EMSCRIPTEN__)
|
|
|
|
# define AK_OS_EMSCRIPTEN
|
|
|
|
#endif
|
|
|
|
|
2023-03-02 11:47:03 +00:00
|
|
|
#define ARCH(arch) (AK_IS_ARCH_##arch())
|
2019-07-09 13:46:22 +00:00
|
|
|
|
2023-09-03 17:44:26 +00:00
|
|
|
#if ARCH(X86_64) || ARCH(I386)
|
2021-12-01 17:10:36 +00:00
|
|
|
# define VALIDATE_IS_X86()
|
|
|
|
#else
|
|
|
|
# define VALIDATE_IS_X86() static_assert(false, "Trying to include x86 only header on non x86 platform");
|
|
|
|
#endif
|
|
|
|
|
2022-10-16 14:55:42 +00:00
|
|
|
#if ARCH(AARCH64)
|
|
|
|
# define VALIDATE_IS_AARCH64()
|
|
|
|
#else
|
|
|
|
# define VALIDATE_IS_AARCH64() static_assert(false, "Trying to include aarch64 only header on non aarch64 platform");
|
|
|
|
#endif
|
|
|
|
|
2023-08-20 17:46:05 +00:00
|
|
|
#if ARCH(RISCV64)
|
|
|
|
# define VALIDATE_IS_RISCV64()
|
|
|
|
#else
|
|
|
|
# define VALIDATE_IS_RISCV64() static_assert(false, "Trying to include riscv64 only header on non riscv64 platform");
|
|
|
|
#endif
|
|
|
|
|
2023-06-23 09:18:29 +00:00
|
|
|
// Apple Clang 14.0.3 (shipped in Xcode 14.3) has a bug that causes __builtin_subc{,l,ll}
|
|
|
|
// to incorrectly return whether a borrow occurred on AArch64. See our writeup for the Qemu
|
|
|
|
// issue also caused by it: https://gitlab.com/qemu-project/qemu/-/issues/1659#note_1408275831
|
|
|
|
#if ARCH(AARCH64) && defined(__apple_build_version__) && __clang_major__ == 14
|
|
|
|
# define AK_BUILTIN_SUBC_BROKEN
|
|
|
|
#endif
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
#ifdef ALWAYS_INLINE
|
2020-08-25 13:11:15 +00:00
|
|
|
# undef ALWAYS_INLINE
|
2020-04-30 09:43:25 +00:00
|
|
|
#endif
|
2021-06-20 14:59:51 +00:00
|
|
|
#define ALWAYS_INLINE __attribute__((always_inline)) inline
|
2020-04-30 09:43:25 +00:00
|
|
|
|
|
|
|
#ifdef NEVER_INLINE
|
2020-08-25 13:11:15 +00:00
|
|
|
# undef NEVER_INLINE
|
2020-04-30 09:43:25 +00:00
|
|
|
#endif
|
2021-06-20 14:59:51 +00:00
|
|
|
#define NEVER_INLINE __attribute__((noinline))
|
2020-04-30 09:43:25 +00:00
|
|
|
|
|
|
|
#ifdef FLATTEN
|
2020-08-25 13:11:15 +00:00
|
|
|
# undef FLATTEN
|
2020-04-30 09:43:25 +00:00
|
|
|
#endif
|
2021-06-20 14:59:51 +00:00
|
|
|
#define FLATTEN __attribute__((flatten))
|
2020-04-30 09:43:25 +00:00
|
|
|
|
2021-06-29 13:45:24 +00:00
|
|
|
#ifdef RETURNS_NONNULL
|
|
|
|
# undef RETURNS_NONNULL
|
|
|
|
#endif
|
|
|
|
#define RETURNS_NONNULL __attribute__((returns_nonnull))
|
|
|
|
|
2024-04-08 00:52:13 +00:00
|
|
|
#ifdef NO_SANITIZE_COVERAGE
|
|
|
|
# undef NO_SANITIZE_COVERAGE
|
|
|
|
#endif
|
|
|
|
#if defined(AK_COMPILER_CLANG)
|
|
|
|
# define NO_SANITIZE_COVERAGE __attribute__((no_sanitize("coverage")))
|
|
|
|
#else
|
|
|
|
# define NO_SANITIZE_COVERAGE __attribute__((no_sanitize_coverage))
|
|
|
|
#endif
|
|
|
|
|
2021-05-12 11:51:02 +00:00
|
|
|
#ifdef NO_SANITIZE_ADDRESS
|
|
|
|
# undef NO_SANITIZE_ADDRESS
|
|
|
|
#endif
|
2024-04-10 20:11:31 +00:00
|
|
|
#if defined(AK_COMPILER_CLANG)
|
|
|
|
# define NO_SANITIZE_ADDRESS __attribute__((no_sanitize("address")))
|
|
|
|
#else
|
|
|
|
# define NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
|
|
|
|
#endif
|
2021-05-12 11:51:02 +00:00
|
|
|
|
2021-07-05 13:34:07 +00:00
|
|
|
#ifdef NAKED
|
|
|
|
# undef NAKED
|
|
|
|
#endif
|
2023-05-22 09:11:29 +00:00
|
|
|
#if !ARCH(AARCH64) || defined(AK_COMPILER_CLANG)
|
2022-10-12 20:05:41 +00:00
|
|
|
# define NAKED __attribute__((naked))
|
|
|
|
#else
|
2023-05-22 09:11:29 +00:00
|
|
|
// GCC doesn't support __attribute__((naked)) on AArch64. We use NAKED to mark functions
|
|
|
|
// that are entirely written in inline assembly; having these be inlined would cause
|
|
|
|
// various problems, such as explicit `ret` instructions accidentally exiting the caller
|
|
|
|
// function or GCC discarding function arguments as they appear "dead".
|
|
|
|
# define NAKED NEVER_INLINE
|
2022-10-12 20:05:41 +00:00
|
|
|
#endif
|
2021-07-05 13:34:07 +00:00
|
|
|
|
2021-07-15 11:50:55 +00:00
|
|
|
#ifdef DISALLOW
|
|
|
|
# undef DISALLOW
|
|
|
|
#endif
|
2022-10-04 19:04:13 +00:00
|
|
|
#if defined(AK_COMPILER_CLANG)
|
2021-07-15 11:50:55 +00:00
|
|
|
# define DISALLOW(message) __attribute__((diagnose_if(1, message, "error")))
|
|
|
|
#else
|
|
|
|
# define DISALLOW(message) __attribute__((error(message)))
|
|
|
|
#endif
|
|
|
|
|
2021-05-27 23:49:40 +00:00
|
|
|
// GCC doesn't have __has_feature but clang does
|
|
|
|
#ifndef __has_feature
|
|
|
|
# define __has_feature(...) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
|
|
|
|
# define HAS_ADDRESS_SANITIZER
|
|
|
|
# define ASAN_POISON_MEMORY_REGION(addr, size) __asan_poison_memory_region(addr, size)
|
|
|
|
# define ASAN_UNPOISON_MEMORY_REGION(addr, size) __asan_unpoison_memory_region(addr, size)
|
2024-04-03 05:53:54 +00:00
|
|
|
# define LSAN_REGISTER_ROOT_REGION(base, size) __lsan_register_root_region(base, size)
|
|
|
|
# define LSAN_UNREGISTER_ROOT_REGION(base, size) __lsan_unregister_root_region(base, size)
|
2021-05-27 23:49:40 +00:00
|
|
|
#else
|
|
|
|
# define ASAN_POISON_MEMORY_REGION(addr, size)
|
|
|
|
# define ASAN_UNPOISON_MEMORY_REGION(addr, size)
|
2024-04-03 05:53:54 +00:00
|
|
|
# define LSAN_REGISTER_ROOT_REGION(base, size)
|
|
|
|
# define LSAN_UNREGISTER_ROOT_REGION(base, size)
|
2021-05-27 23:49:40 +00:00
|
|
|
#endif
|
|
|
|
|
2022-10-09 21:23:23 +00:00
|
|
|
#ifndef AK_OS_SERENITY
|
2022-12-12 14:39:24 +00:00
|
|
|
# ifdef AK_OS_WINDOWS
|
|
|
|
// FIXME: No idea where to get this, but it's 4096 anyway :^)
|
|
|
|
# define PAGE_SIZE 4096
|
2022-12-12 16:27:01 +00:00
|
|
|
# else
|
|
|
|
# include <unistd.h>
|
|
|
|
# undef PAGE_SIZE
|
|
|
|
# define PAGE_SIZE sysconf(_SC_PAGESIZE)
|
2021-09-01 01:23:29 +00:00
|
|
|
# endif
|
2019-07-25 09:51:24 +00:00
|
|
|
#endif
|
2019-07-25 12:25:45 +00:00
|
|
|
|
2022-09-20 07:15:12 +00:00
|
|
|
#if defined(AK_OS_WINDOWS)
|
|
|
|
# define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC
|
|
|
|
#endif
|
|
|
|
|
2023-09-06 18:22:08 +00:00
|
|
|
#if defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_FREEBSD) || defined(AK_OS_HAIKU)
|
2020-12-27 22:38:32 +00:00
|
|
|
# define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC
|
|
|
|
# define CLOCK_REALTIME_COARSE CLOCK_REALTIME
|
|
|
|
#endif
|
2022-03-04 13:57:38 +00:00
|
|
|
|
2022-07-31 20:32:33 +00:00
|
|
|
#ifndef AK_SYSTEM_CACHE_ALIGNMENT_SIZE
|
2023-03-02 11:47:03 +00:00
|
|
|
# if ARCH(AARCH64) || ARCH(X86_64)
|
2022-07-31 20:32:33 +00:00
|
|
|
# define AK_SYSTEM_CACHE_ALIGNMENT_SIZE 64
|
2022-03-04 13:57:38 +00:00
|
|
|
# else
|
2022-07-31 20:32:33 +00:00
|
|
|
# define AK_SYSTEM_CACHE_ALIGNMENT_SIZE 128
|
2022-03-04 13:57:38 +00:00
|
|
|
# endif
|
2022-07-31 20:32:33 +00:00
|
|
|
#endif /* AK_SYSTEM_CACHE_ALIGNMENT_SIZE */
|
2022-03-04 13:57:38 +00:00
|
|
|
|
2022-07-31 20:32:33 +00:00
|
|
|
#ifdef AK_CACHE_ALIGNED
|
|
|
|
# undef AK_CACHE_ALIGNED
|
2022-03-04 13:57:38 +00:00
|
|
|
#endif
|
2022-07-31 20:32:33 +00:00
|
|
|
#define AK_CACHE_ALIGNED alignas(AK_SYSTEM_CACHE_ALIGNMENT_SIZE)
|