/src/libjxl/third_party/highway/hwy/abort.cc
Line | Count | Source |
1 | | // Copyright 2019 Google LLC |
2 | | // Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
3 | | // SPDX-License-Identifier: Apache-2.0 |
4 | | // SPDX-License-Identifier: BSD-3-Clause |
5 | | |
6 | | #include "hwy/abort.h" |
7 | | |
8 | | #include <stdarg.h> |
9 | | #include <stdio.h> |
10 | | #include <stdlib.h> |
11 | | |
12 | | #include <string> |
13 | | |
14 | | #include "hwy/base.h" |
15 | | |
16 | | #if HWY_IS_ASAN || HWY_IS_MSAN || HWY_IS_TSAN |
17 | | #include "sanitizer/common_interface_defs.h" // __sanitizer_print_stack_trace |
18 | | #endif |
19 | | |
20 | | namespace hwy { |
21 | | |
22 | | namespace { |
23 | 0 | std::string GetBaseName(std::string const& file_name) { |
24 | 0 | auto last_slash = file_name.find_last_of("/\\"); |
25 | 0 | return file_name.substr(last_slash + 1); |
26 | 0 | } |
27 | | } // namespace |
28 | | |
29 | 0 | HWY_DLLEXPORT AbortFunc& GetAbortFunc() { |
30 | 0 | static AbortFunc func; |
31 | 0 | return func; |
32 | 0 | } |
33 | | |
34 | 0 | HWY_DLLEXPORT AbortFunc SetAbortFunc(AbortFunc func) { |
35 | 0 | const AbortFunc prev = GetAbortFunc(); |
36 | 0 | GetAbortFunc() = func; |
37 | 0 | return prev; |
38 | 0 | } |
39 | | |
40 | | HWY_DLLEXPORT HWY_NORETURN void HWY_FORMAT(3, 4) |
41 | 0 | Abort(const char* file, int line, const char* format, ...) { |
42 | 0 | char buf[800]; |
43 | 0 | va_list args; |
44 | 0 | va_start(args, format); |
45 | 0 | vsnprintf(buf, sizeof(buf), format, args); |
46 | 0 | va_end(args); |
47 | |
|
48 | 0 | AbortFunc handler = GetAbortFunc(); |
49 | 0 | if (handler != nullptr) { |
50 | 0 | handler(file, line, buf); |
51 | 0 | } else { |
52 | 0 | fprintf(stderr, "Abort at %s:%d: %s\n", GetBaseName(file).data(), line, |
53 | 0 | buf); |
54 | 0 | } |
55 | | |
56 | | // If compiled with any sanitizer, they can also print a stack trace. |
57 | | #if HWY_IS_ASAN || HWY_IS_MSAN || HWY_IS_TSAN |
58 | | __sanitizer_print_stack_trace(); |
59 | | #endif // HWY_IS_* |
60 | 0 | fflush(stderr); |
61 | | |
62 | | // Now terminate the program: |
63 | | #if HWY_ARCH_RISCV |
64 | | exit(1); // trap/abort just freeze Spike. |
65 | | #elif HWY_IS_DEBUG_BUILD && !HWY_COMPILER_MSVC |
66 | | // Facilitates breaking into a debugger, but don't use this in non-debug |
67 | | // builds because it looks like "illegal instruction", which is misleading. |
68 | | __builtin_trap(); |
69 | | #else |
70 | 0 | abort(); // Compile error without this due to HWY_NORETURN. |
71 | 0 | #endif |
72 | 0 | } |
73 | | |
74 | | } // namespace hwy |