hwasan_setjmp_x86_64.S 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //===-- hwasan_setjmp_x86_64.S --------------------------------------------===//
  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. // setjmp interceptor for x86_64.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_common/sanitizer_asm.h"
  13. #if HWASAN_WITH_INTERCEPTORS && defined(__x86_64__)
  14. #include "sanitizer_common/sanitizer_platform.h"
  15. // We want to save the context of the calling function.
  16. // That requires
  17. // 1) No modification of the return address by this function.
  18. // 2) No modification of the stack pointer by this function.
  19. // 3) (no modification of any other saved register, but that's not really going
  20. // to occur, and hence isn't as much of a worry).
  21. //
  22. // There's essentially no way to ensure that the compiler will not modify the
  23. // stack pointer when compiling a C function.
  24. // Hence we have to write this function in assembly.
  25. //
  26. // TODO: Handle Intel CET.
  27. .section .text
  28. .file "hwasan_setjmp_x86_64.S"
  29. .global __interceptor_setjmp
  30. ASM_TYPE_FUNCTION(__interceptor_setjmp)
  31. __interceptor_setjmp:
  32. CFI_STARTPROC
  33. _CET_ENDBR
  34. xorl %esi, %esi
  35. jmp __interceptor_sigsetjmp
  36. CFI_ENDPROC
  37. ASM_SIZE(__interceptor_setjmp)
  38. .global __interceptor_sigsetjmp
  39. ASM_TYPE_FUNCTION(__interceptor_sigsetjmp)
  40. __interceptor_sigsetjmp:
  41. CFI_STARTPROC
  42. _CET_ENDBR
  43. // Save callee save registers.
  44. mov %rbx, (0*8)(%rdi)
  45. mov %rbp, (1*8)(%rdi)
  46. mov %r12, (2*8)(%rdi)
  47. mov %r13, (3*8)(%rdi)
  48. mov %r14, (4*8)(%rdi)
  49. mov %r15, (5*8)(%rdi)
  50. // Save SP as it was in caller's frame.
  51. lea 8(%rsp), %rdx
  52. mov %rdx, (6*8)(%rdi)
  53. // Save return address.
  54. mov (%rsp), %rax
  55. mov %rax, (7*8)(%rdi)
  56. jmp __sigjmp_save
  57. CFI_ENDPROC
  58. ASM_SIZE(__interceptor_sigsetjmp)
  59. .macro WEAK_ALIAS first second
  60. .weak \second
  61. .equ \second\(), \first
  62. .endm
  63. WEAK_ALIAS __interceptor_sigsetjmp, __sigsetjmp
  64. WEAK_ALIAS __interceptor_setjmp, _setjmp
  65. #endif
  66. // We do not need executable stack.
  67. NO_EXEC_STACK_DIRECTIVE