hwasan_exceptions.cpp 2.7 KB

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