Back to index

glibc  2.9
bp-asm.h
Go to the documentation of this file.
00001 /* Bounded-pointer definitions for x86-64 assembler.
00002    Copyright (C) 2001 Free Software Foundation, Inc.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #ifndef _bp_asm_h_
00020 # define _bp_asm_h_ 1
00021 
00022 # if __ASSEMBLER__
00023 
00024 #  if __BOUNDED_POINTERS__
00025 
00026 /* Bounded pointers occupy three words.  */
00027 #   define PTR_SIZE 24
00028 /* Bounded pointer return values are passed back through a hidden
00029    argument that points to caller-allocate space.  The hidden arg
00030    occupies one word on the stack.  */
00031 #   define RTN_SIZE 6
00032 /* Although the caller pushes the hidden arg, the callee is
00033    responsible for popping it.  */
00034 #   define RET_PTR ret $RTN_SIZE
00035 /* Maintain frame pointer chain in leaf assembler functions for the benefit
00036    of debugging stack traces when bounds violations occur.  */
00037 #   define ENTER pushq %rbp; movq %rsp, %rbp
00038 #   define LEAVE movq %rbp, %rsp; popq %rbp
00039 /* Stack space overhead of procedure-call linkage: return address and
00040    frame pointer.  */
00041 #   define LINKAGE 16
00042 /* Stack offset of return address after calling ENTER.  */
00043 #   define PCOFF 8
00044 
00045 /* Int 5 is the "bound range" exception also raised by the "bound"
00046    instruction.  */
00047 #   define BOUNDS_VIOLATED int $5
00048 
00049 #   define CHECK_BOUNDS_LOW(VAL_REG, BP_MEM)     \
00050        cmpq 8+BP_MEM, VAL_REG;                   \
00051        jae 0f; /* continue if value >= low */    \
00052        BOUNDS_VIOLATED;                   \
00053     0:
00054 
00055 #   define CHECK_BOUNDS_HIGH(VAL_REG, BP_MEM, Jcc)      \
00056        cmpq 16+BP_MEM, VAL_REG;                  \
00057        Jcc 0f; /* continue if value < high */           \
00058        BOUNDS_VIOLATED;                          \
00059     0:
00060 
00061 #   define CHECK_BOUNDS_BOTH(VAL_REG, BP_MEM)    \
00062        cmpq 8+BP_MEM, VAL_REG;                   \
00063        jb 1f; /* die if value < low */           \
00064        cmpq 16+BP_MEM, VAL_REG;           \
00065        jb 0f; /* continue if value < high */     \
00066     1: BOUNDS_VIOLATED;                   \
00067     0:
00068 
00069 #   define CHECK_BOUNDS_BOTH_WIDE(VAL_REG, BP_MEM, LENGTH)     \
00070        CHECK_BOUNDS_LOW(VAL_REG, BP_MEM);               \
00071        addl LENGTH, VAL_REG;                                   \
00072        cmpq 16+BP_MEM, VAL_REG;                                \
00073        jbe 0f; /* continue if value <= high */                 \
00074        BOUNDS_VIOLATED;                                 \
00075     0: subq LENGTH, VAL_REG /* restore value */
00076 
00077 /* Take bounds from BP_MEM and affix them to the pointer
00078    value in %rax, stuffing all into memory at RTN(%esp).
00079    Use %rdx as a scratch register.  */
00080 
00081 #   define RETURN_BOUNDED_POINTER(BP_MEM) \
00082        movq RTN(%rsp), %rdx;                     \
00083        movq %rax, 0(%rdx);                \
00084        movq 8+BP_MEM, %rax;               \
00085        movq %rax, 4(%rdx);                \
00086        movq 16+BP_MEM, %rax;                     \
00087        movq %rax, 8(%rdx)
00088 
00089 #   define RETURN_NULL_BOUNDED_POINTER           \
00090        movl RTN(%rsp), %rdx;                     \
00091        movl %rax, 0(%rdx);                \
00092        movl %rax, 4(%rdx);                \
00093        movl %rax, 8(%rdx)
00094 
00095 /* The caller of __errno_location is responsible for allocating space
00096    for the three-word BP return-value and passing pushing its address
00097    as an implicit first argument.  */
00098 #   define PUSH_ERRNO_LOCATION_RETURN            \
00099        subl $16, %esp;                           \
00100        subl $8, %esp;                            \
00101        pushq %rsp
00102 
00103 /* __errno_location is responsible for popping the implicit first
00104    argument, but we must pop the space for the BP itself.  We also
00105    dereference the return value in order to dig out the pointer value.  */
00106 #   define POP_ERRNO_LOCATION_RETURN             \
00107        popq %rax;                         \
00108        addq $16, %rsp
00109 
00110 #  else /* !__BOUNDED_POINTERS__ */
00111 
00112 /* Unbounded pointers occupy one word.  */
00113 #   define PTR_SIZE 8
00114 /* Unbounded pointer return values are passed back in the register %rax.  */
00115 #   define RTN_SIZE 0
00116 /* Use simple return instruction for unbounded pointer values.  */
00117 #   define RET_PTR ret
00118 /* Don't maintain frame pointer chain for leaf assembler functions.  */
00119 #   define ENTER
00120 #   define LEAVE
00121 /* Stack space overhead of procedure-call linkage: return address only.  */
00122 #   define LINKAGE 8
00123 /* Stack offset of return address after calling ENTER.  */
00124 #   define PCOFF 0
00125 
00126 #   define CHECK_BOUNDS_LOW(VAL_REG, BP_MEM)
00127 #   define CHECK_BOUNDS_HIGH(VAL_REG, BP_MEM, Jcc)
00128 #   define CHECK_BOUNDS_BOTH(VAL_REG, BP_MEM)
00129 #   define CHECK_BOUNDS_BOTH_WIDE(VAL_REG, BP_MEM, LENGTH)
00130 #   define RETURN_BOUNDED_POINTER(BP_MEM)
00131 
00132 #   define RETURN_NULL_BOUNDED_POINTER
00133 
00134 #   define PUSH_ERRNO_LOCATION_RETURN
00135 #   define POP_ERRNO_LOCATION_RETURN
00136 
00137 #  endif /* !__BOUNDED_POINTERS__ */
00138 
00139 # endif /* __ASSEMBLER__ */
00140 
00141 #endif /* _bp_asm_h_ */