13cab2bb3Spatrick //===-- hwasan_exceptions.cpp ---------------------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is a part of HWAddressSanitizer.
103cab2bb3Spatrick //
113cab2bb3Spatrick // HWAddressSanitizer runtime.
123cab2bb3Spatrick //===----------------------------------------------------------------------===//
133cab2bb3Spatrick
143cab2bb3Spatrick #include "hwasan_poisoning.h"
153cab2bb3Spatrick #include "sanitizer_common/sanitizer_common.h"
163cab2bb3Spatrick
173cab2bb3Spatrick #include <unwind.h>
183cab2bb3Spatrick
193cab2bb3Spatrick using namespace __hwasan;
203cab2bb3Spatrick using namespace __sanitizer;
213cab2bb3Spatrick
223cab2bb3Spatrick typedef _Unwind_Reason_Code PersonalityFn(int version, _Unwind_Action actions,
233cab2bb3Spatrick uint64_t exception_class,
243cab2bb3Spatrick _Unwind_Exception* unwind_exception,
253cab2bb3Spatrick _Unwind_Context* context);
263cab2bb3Spatrick
273cab2bb3Spatrick // Pointers to the _Unwind_GetGR and _Unwind_GetCFA functions are passed in
283cab2bb3Spatrick // instead of being called directly. This is to handle cases where the unwinder
293cab2bb3Spatrick // is statically linked and the sanitizer runtime and the program are linked
303cab2bb3Spatrick // against different unwinders. The _Unwind_Context data structure is opaque so
313cab2bb3Spatrick // it may be incompatible between unwinders.
32*810390e3Srobert typedef uintptr_t GetGRFn(_Unwind_Context* context, int index);
33*810390e3Srobert typedef uintptr_t GetCFAFn(_Unwind_Context* context);
343cab2bb3Spatrick
353cab2bb3Spatrick extern "C" SANITIZER_INTERFACE_ATTRIBUTE _Unwind_Reason_Code
__hwasan_personality_wrapper(int version,_Unwind_Action actions,uint64_t exception_class,_Unwind_Exception * unwind_exception,_Unwind_Context * context,PersonalityFn * real_personality,GetGRFn * get_gr,GetCFAFn * get_cfa)363cab2bb3Spatrick __hwasan_personality_wrapper(int version, _Unwind_Action actions,
373cab2bb3Spatrick uint64_t exception_class,
383cab2bb3Spatrick _Unwind_Exception* unwind_exception,
393cab2bb3Spatrick _Unwind_Context* context,
403cab2bb3Spatrick PersonalityFn* real_personality, GetGRFn* get_gr,
413cab2bb3Spatrick GetCFAFn* get_cfa) {
423cab2bb3Spatrick _Unwind_Reason_Code rc;
433cab2bb3Spatrick if (real_personality)
443cab2bb3Spatrick rc = real_personality(version, actions, exception_class, unwind_exception,
453cab2bb3Spatrick context);
463cab2bb3Spatrick else
473cab2bb3Spatrick rc = _URC_CONTINUE_UNWIND;
483cab2bb3Spatrick
493cab2bb3Spatrick // We only untag frames without a landing pad because landing pads are
503cab2bb3Spatrick // responsible for untagging the stack themselves if they resume.
513cab2bb3Spatrick //
523cab2bb3Spatrick // Here we assume that the frame record appears after any locals. This is not
533cab2bb3Spatrick // required by AAPCS but is a requirement for HWASAN instrumented functions.
543cab2bb3Spatrick if ((actions & _UA_CLEANUP_PHASE) && rc == _URC_CONTINUE_UNWIND) {
553cab2bb3Spatrick #if defined(__x86_64__)
563cab2bb3Spatrick uptr fp = get_gr(context, 6); // rbp
573cab2bb3Spatrick #elif defined(__aarch64__)
583cab2bb3Spatrick uptr fp = get_gr(context, 29); // x29
59*810390e3Srobert #elif SANITIZER_RISCV64
60*810390e3Srobert uptr fp = get_gr(context, 8); // x8
613cab2bb3Spatrick #else
623cab2bb3Spatrick #error Unsupported architecture
633cab2bb3Spatrick #endif
643cab2bb3Spatrick uptr sp = get_cfa(context);
653cab2bb3Spatrick TagMemory(sp, fp - sp, 0);
663cab2bb3Spatrick }
673cab2bb3Spatrick
683cab2bb3Spatrick return rc;
693cab2bb3Spatrick }
70