xref: /aosp_15_r20/external/gwp_asan/gwp_asan/platform_specific/utilities_posix.cpp (revision b302aa5039729da396909ef03e815160dab4448c)
1 //===-- utilities_posix.cpp -------------------------------------*- 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 #include <alloca.h>
10 #include <features.h> // IWYU pragma: keep (for __BIONIC__ macro)
11 #include <inttypes.h>
12 #include <stdint.h>
13 #include <string.h>
14 
15 #ifdef __BIONIC__
16 #include "gwp_asan/definitions.h"
17 #include <stdlib.h>
18 extern "C" GWP_ASAN_WEAK void android_set_abort_message(const char *);
19 #else // __BIONIC__
20 #include <stdio.h>
21 #endif
22 
23 namespace gwp_asan {
die(const char * Message)24 void die(const char *Message) {
25 #ifdef __BIONIC__
26   if (&android_set_abort_message != nullptr)
27     android_set_abort_message(Message);
28   abort();
29 #else  // __BIONIC__
30   fprintf(stderr, "%s", Message);
31   __builtin_trap();
32 #endif // __BIONIC__
33 }
34 
dieWithErrorCode(const char * Message,int64_t ErrorCode)35 void dieWithErrorCode(const char *Message, int64_t ErrorCode) {
36 #ifdef __BIONIC__
37   if (&android_set_abort_message == nullptr)
38     abort();
39 
40   size_t buffer_size = strlen(Message) + 48;
41   char *buffer = static_cast<char *>(alloca(buffer_size));
42   snprintf(buffer, buffer_size, "%s (Error Code: %" PRId64 ")", Message,
43            ErrorCode);
44   android_set_abort_message(buffer);
45   abort();
46 #else  // __BIONIC__
47   fprintf(stderr, "%s (Error Code: %" PRId64 ")", Message, ErrorCode);
48   __builtin_trap();
49 #endif // __BIONIC__
50 }
51 } // namespace gwp_asan
52