asan_rtl_x86_64.S 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "asan_mapping.h"
  2. #include "sanitizer_common/sanitizer_asm.h"
  3. #if defined(__x86_64__)
  4. #include "sanitizer_common/sanitizer_platform.h"
  5. .section .text
  6. .file "asan_rtl_x86_64.S"
  7. #define NAME(n, reg, op, s, i) n##_##op##_##i##_##s##_##reg
  8. #define FNAME(reg, op, s, i) NAME(__asan_check, reg, op, s, i)
  9. #define RLABEL(reg, op, s, i) NAME(.return, reg, op, s, i)
  10. #define CLABEL(reg, op, s, i) NAME(.check, reg, op, s, i)
  11. #define FLABEL(reg, op, s, i) NAME(.fail, reg, op, s, i)
  12. #define BEGINF(reg, op, s, i) \
  13. .globl FNAME(reg, op, s, i) ;\
  14. .hidden FNAME(reg, op, s, i) ;\
  15. ASM_TYPE_FUNCTION(FNAME(reg, op, s, i)) ;\
  16. .cfi_startproc ;\
  17. FNAME(reg, op, s, i): ;\
  18. #define ENDF .cfi_endproc ;\
  19. // Access check functions for 1,2 and 4 byte types, which require extra checks.
  20. #define ASAN_MEMORY_ACCESS_INITIAL_CHECK_ADD(reg, op, s) \
  21. mov %##reg,%r10 ;\
  22. shr $0x3,%r10 ;\
  23. movsbl ASAN_SHADOW_OFFSET_CONST(%r10),%r10d ;\
  24. test %r10d,%r10d ;\
  25. jne CLABEL(reg, op, s, add) ;\
  26. RLABEL(reg, op, s, add): ;\
  27. retq ;\
  28. #define ASAN_MEMORY_ACCESS_EXTRA_CHECK_1(reg, op, i) \
  29. CLABEL(reg, op, 1, i): ;\
  30. push %rcx ;\
  31. mov %##reg,%rcx ;\
  32. and $0x7,%ecx ;\
  33. cmp %r10d,%ecx ;\
  34. pop %rcx ;\
  35. jl RLABEL(reg, op, 1, i);\
  36. mov %##reg,%rdi ;\
  37. jmp __asan_report_##op##1@PLT ;\
  38. #define ASAN_MEMORY_ACCESS_EXTRA_CHECK_2(reg, op, i) \
  39. CLABEL(reg, op, 2, i): ;\
  40. push %rcx ;\
  41. mov %##reg,%rcx ;\
  42. and $0x7,%ecx ;\
  43. add $0x1,%ecx ;\
  44. cmp %r10d,%ecx ;\
  45. pop %rcx ;\
  46. jl RLABEL(reg, op, 2, i);\
  47. mov %##reg,%rdi ;\
  48. jmp __asan_report_##op##2@PLT ;\
  49. #define ASAN_MEMORY_ACCESS_EXTRA_CHECK_4(reg, op, i) \
  50. CLABEL(reg, op, 4, i): ;\
  51. push %rcx ;\
  52. mov %##reg,%rcx ;\
  53. and $0x7,%ecx ;\
  54. add $0x3,%ecx ;\
  55. cmp %r10d,%ecx ;\
  56. pop %rcx ;\
  57. jl RLABEL(reg, op, 4, i);\
  58. mov %##reg,%rdi ;\
  59. jmp __asan_report_##op##4@PLT ;\
  60. #define ASAN_MEMORY_ACCESS_CALLBACK_ADD_1(reg, op) \
  61. BEGINF(reg, op, 1, add) ;\
  62. ASAN_MEMORY_ACCESS_INITIAL_CHECK_ADD(reg, op, 1) ;\
  63. ASAN_MEMORY_ACCESS_EXTRA_CHECK_1(reg, op, add) ;\
  64. ENDF
  65. #define ASAN_MEMORY_ACCESS_CALLBACK_ADD_2(reg, op) \
  66. BEGINF(reg, op, 2, add) ;\
  67. ASAN_MEMORY_ACCESS_INITIAL_CHECK_ADD(reg, op, 2) ;\
  68. ASAN_MEMORY_ACCESS_EXTRA_CHECK_2(reg, op, add) ;\
  69. ENDF
  70. #define ASAN_MEMORY_ACCESS_CALLBACK_ADD_4(reg, op) \
  71. BEGINF(reg, op, 4, add) ;\
  72. ASAN_MEMORY_ACCESS_INITIAL_CHECK_ADD(reg, op, 4) ;\
  73. ASAN_MEMORY_ACCESS_EXTRA_CHECK_4(reg, op, add) ;\
  74. ENDF
  75. // Access check functions for 8 and 16 byte types: no extra checks required.
  76. #define ASAN_MEMORY_ACCESS_CHECK_ADD(reg, op, s, c) \
  77. mov %##reg,%r10 ;\
  78. shr $0x3,%r10 ;\
  79. ##c $0x0,ASAN_SHADOW_OFFSET_CONST(%r10) ;\
  80. jne FLABEL(reg, op, s, add) ;\
  81. retq ;\
  82. #define ASAN_MEMORY_ACCESS_FAIL(reg, op, s, i) \
  83. FLABEL(reg, op, s, i): ;\
  84. mov %##reg,%rdi ;\
  85. jmp __asan_report_##op##s@PLT;\
  86. #define ASAN_MEMORY_ACCESS_CALLBACK_ADD_8(reg, op) \
  87. BEGINF(reg, op, 8, add) ;\
  88. ASAN_MEMORY_ACCESS_CHECK_ADD(reg, op, 8, cmpb) ;\
  89. ASAN_MEMORY_ACCESS_FAIL(reg, op, 8, add) ;\
  90. ENDF
  91. #define ASAN_MEMORY_ACCESS_CALLBACK_ADD_16(reg, op) \
  92. BEGINF(reg, op, 16, add) ;\
  93. ASAN_MEMORY_ACCESS_CHECK_ADD(reg, op, 16, cmpw) ;\
  94. ASAN_MEMORY_ACCESS_FAIL(reg, op, 16, add) ;\
  95. ENDF
  96. #define ASAN_MEMORY_ACCESS_CALLBACKS_ADD(reg) \
  97. ASAN_MEMORY_ACCESS_CALLBACK_ADD_1(reg, load) \
  98. ASAN_MEMORY_ACCESS_CALLBACK_ADD_1(reg, store) \
  99. ASAN_MEMORY_ACCESS_CALLBACK_ADD_2(reg, load) \
  100. ASAN_MEMORY_ACCESS_CALLBACK_ADD_2(reg, store) \
  101. ASAN_MEMORY_ACCESS_CALLBACK_ADD_4(reg, load) \
  102. ASAN_MEMORY_ACCESS_CALLBACK_ADD_4(reg, store) \
  103. ASAN_MEMORY_ACCESS_CALLBACK_ADD_8(reg, load) \
  104. ASAN_MEMORY_ACCESS_CALLBACK_ADD_8(reg, store) \
  105. ASAN_MEMORY_ACCESS_CALLBACK_ADD_16(reg, load) \
  106. ASAN_MEMORY_ACCESS_CALLBACK_ADD_16(reg, store) \
  107. // Instantiate all but R10 and R11 callbacks. We are using PLTSafe class with
  108. // the intrinsic, which guarantees that the code generation will never emit
  109. // R10 or R11 callback.
  110. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(RAX)
  111. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(RBX)
  112. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(RCX)
  113. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(RDX)
  114. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(RSI)
  115. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(RDI)
  116. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(RBP)
  117. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(R8)
  118. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(R9)
  119. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(R12)
  120. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(R13)
  121. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(R14)
  122. ASAN_MEMORY_ACCESS_CALLBACKS_ADD(R15)
  123. #endif
  124. NO_EXEC_STACK_DIRECTIVE