bp-asm.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Bounded-pointer definitions for x86-64 assembler.
  2. Copyright (C) 2001 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #ifndef _bp_asm_h_
  16. # define _bp_asm_h_ 1
  17. # if __ASSEMBLER__
  18. # if __BOUNDED_POINTERS__
  19. /* Bounded pointers occupy three words. */
  20. # define PTR_SIZE 24
  21. /* Bounded pointer return values are passed back through a hidden
  22. argument that points to caller-allocate space. The hidden arg
  23. occupies one word on the stack. */
  24. # define RTN_SIZE 6
  25. /* Although the caller pushes the hidden arg, the callee is
  26. responsible for popping it. */
  27. # define RET_PTR ret $RTN_SIZE
  28. /* Maintain frame pointer chain in leaf assembler functions for the benefit
  29. of debugging stack traces when bounds violations occur. */
  30. # define ENTER pushq %rbp; movq %rsp, %rbp
  31. # define LEAVE movq %rbp, %rsp; popq %rbp
  32. /* Stack space overhead of procedure-call linkage: return address and
  33. frame pointer. */
  34. # define LINKAGE 16
  35. /* Stack offset of return address after calling ENTER. */
  36. # define PCOFF 8
  37. /* Int 5 is the "bound range" exception also raised by the "bound"
  38. instruction. */
  39. # define BOUNDS_VIOLATED int $5
  40. # define CHECK_BOUNDS_LOW(VAL_REG, BP_MEM) \
  41. cmpq 8+BP_MEM, VAL_REG; \
  42. jae 0f; /* continue if value >= low */ \
  43. BOUNDS_VIOLATED; \
  44. 0:
  45. # define CHECK_BOUNDS_HIGH(VAL_REG, BP_MEM, Jcc) \
  46. cmpq 16+BP_MEM, VAL_REG; \
  47. Jcc 0f; /* continue if value < high */ \
  48. BOUNDS_VIOLATED; \
  49. 0:
  50. # define CHECK_BOUNDS_BOTH(VAL_REG, BP_MEM) \
  51. cmpq 8+BP_MEM, VAL_REG; \
  52. jb 1f; /* die if value < low */ \
  53. cmpq 16+BP_MEM, VAL_REG; \
  54. jb 0f; /* continue if value < high */ \
  55. 1: BOUNDS_VIOLATED; \
  56. 0:
  57. # define CHECK_BOUNDS_BOTH_WIDE(VAL_REG, BP_MEM, LENGTH) \
  58. CHECK_BOUNDS_LOW(VAL_REG, BP_MEM); \
  59. addl LENGTH, VAL_REG; \
  60. cmpq 16+BP_MEM, VAL_REG; \
  61. jbe 0f; /* continue if value <= high */ \
  62. BOUNDS_VIOLATED; \
  63. 0: subq LENGTH, VAL_REG /* restore value */
  64. /* Take bounds from BP_MEM and affix them to the pointer
  65. value in %rax, stuffing all into memory at RTN(%esp).
  66. Use %rdx as a scratch register. */
  67. # define RETURN_BOUNDED_POINTER(BP_MEM) \
  68. movq RTN(%rsp), %rdx; \
  69. movq %rax, 0(%rdx); \
  70. movq 8+BP_MEM, %rax; \
  71. movq %rax, 4(%rdx); \
  72. movq 16+BP_MEM, %rax; \
  73. movq %rax, 8(%rdx)
  74. # define RETURN_NULL_BOUNDED_POINTER \
  75. movl RTN(%rsp), %rdx; \
  76. movl %rax, 0(%rdx); \
  77. movl %rax, 4(%rdx); \
  78. movl %rax, 8(%rdx)
  79. /* The caller of PAL(__errno_location) is responsible for allocating space
  80. for the three-word BP return-value and passing pushing its address
  81. as an implicit first argument. */
  82. # define PUSH_ERRNO_LOCATION_RETURN \
  83. subl $16, %esp; \
  84. subl $8, %esp; \
  85. pushq %rsp
  86. /* PAL(__errno_location) is responsible for popping the implicit first
  87. argument, but we must pop the space for the BP itself. We also
  88. dereference the return value in order to dig out the pointer value. */
  89. # define POP_ERRNO_LOCATION_RETURN \
  90. popq %rax; \
  91. addq $16, %rsp
  92. # else /* !__BOUNDED_POINTERS__ */
  93. /* Unbounded pointers occupy one word. */
  94. # define PTR_SIZE 8
  95. /* Unbounded pointer return values are passed back in the register %rax. */
  96. # define RTN_SIZE 0
  97. /* Use simple return instruction for unbounded pointer values. */
  98. # define RET_PTR ret
  99. /* Don't maintain frame pointer chain for leaf assembler functions. */
  100. # define ENTER
  101. # define LEAVE
  102. /* Stack space overhead of procedure-call linkage: return address only. */
  103. # define LINKAGE 8
  104. /* Stack offset of return address after calling ENTER. */
  105. # define PCOFF 0
  106. # define CHECK_BOUNDS_LOW(VAL_REG, BP_MEM)
  107. # define CHECK_BOUNDS_HIGH(VAL_REG, BP_MEM, Jcc)
  108. # define CHECK_BOUNDS_BOTH(VAL_REG, BP_MEM)
  109. # define CHECK_BOUNDS_BOTH_WIDE(VAL_REG, BP_MEM, LENGTH)
  110. # define RETURN_BOUNDED_POINTER(BP_MEM)
  111. # define RETURN_NULL_BOUNDED_POINTER
  112. # define PUSH_ERRNO_LOCATION_RETURN
  113. # define POP_ERRNO_LOCATION_RETURN
  114. # endif /* !__BOUNDED_POINTERS__ */
  115. # endif /* __ASSEMBLER__ */
  116. #endif /* _bp_asm_h_ */