1 //===-- hwasan_exceptions.cpp ---------------------------------------------===//
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 // This file is a part of HWAddressSanitizer.
10 //
11 // HWAddressSanitizer runtime.
12 //===----------------------------------------------------------------------===//
13 
14 #include "hwasan_poisoning.h"
15 #include "sanitizer_common/sanitizer_common.h"
16 
17 #include <unwind.h>
18 
19 using namespace __hwasan;
20 using namespace __sanitizer;
21 
22 typedef _Unwind_Reason_Code PersonalityFn(int version, _Unwind_Action actions,
23                                           uint64_t exception_class,
24                                           _Unwind_Exception* unwind_exception,
25                                           _Unwind_Context* context);
26 
27 // Pointers to the _Unwind_GetGR and _Unwind_GetCFA functions are passed in
28 // instead of being called directly. This is to handle cases where the unwinder
29 // is statically linked and the sanitizer runtime and the program are linked
30 // against different unwinders. The _Unwind_Context data structure is opaque so
31 // it may be incompatible between unwinders.
32 typedef _Unwind_Word GetGRFn(_Unwind_Context* context, int index);
33 typedef _Unwind_Word GetCFAFn(_Unwind_Context* context);
34 
35 extern "C" _Unwind_Reason_Code __hwasan_personality_wrapper(
36     int version, _Unwind_Action actions, uint64_t exception_class,
37     _Unwind_Exception* unwind_exception, _Unwind_Context* context,
38     PersonalityFn* real_personality, GetGRFn* get_gr, GetCFAFn* get_cfa) {
39   _Unwind_Reason_Code rc;
40   if (real_personality)
41     rc = real_personality(version, actions, exception_class, unwind_exception,
42                           context);
43   else
44     rc = _URC_CONTINUE_UNWIND;
45 
46   // We only untag frames without a landing pad because landing pads are
47   // responsible for untagging the stack themselves if they resume.
48   //
49   // Here we assume that the frame record appears after any locals. This is not
50   // required by AAPCS but is a requirement for HWASAN instrumented functions.
51   if ((actions & _UA_CLEANUP_PHASE) && rc == _URC_CONTINUE_UNWIND) {
52 #if defined(__x86_64__)
53     uptr fp = get_gr(context, 6); // rbp
54 #elif defined(__aarch64__)
55     uptr fp = get_gr(context, 29); // x29
56 #else
57 #error Unsupported architecture
58 #endif
59     uptr sp = get_cfa(context);
60     TagMemory(sp, fp - sp, 0);
61   }
62 
63   return rc;
64 }
65