gcc: Pointer Bounds Checker builtins

1 
1 6.57 Pointer Bounds Checker Built-in Functions
1 ==============================================
1 
1 GCC provides a set of built-in functions to control Pointer Bounds
1 Checker instrumentation.  Note that all Pointer Bounds Checker builtins
1 can be used even if you compile with Pointer Bounds Checker off
1 ('-fno-check-pointer-bounds').  The behavior may differ in such case as
1 documented below.
1 
1  -- Built-in Function: void * __builtin___bnd_set_ptr_bounds (const void
1           *Q, size_t SIZE)
1 
1      This built-in function returns a new pointer with the value of Q,
1      and associate it with the bounds [Q, Q+SIZE-1].  With Pointer
1      Bounds Checker off, the built-in function just returns the first
1      argument.
1 
1           extern void *__wrap_malloc (size_t n)
1           {
1             void *p = (void *)__real_malloc (n);
1             if (!p) return __builtin___bnd_null_ptr_bounds (p);
1             return __builtin___bnd_set_ptr_bounds (p, n);
1           }
1 
1  -- Built-in Function: void * __builtin___bnd_narrow_ptr_bounds (const
1           void *P, const void *Q, size_t SIZE)
1 
1      This built-in function returns a new pointer with the value of P
1      and associates it with the narrowed bounds formed by the
1      intersection of bounds associated with Q and the bounds [P, P +
1      SIZE - 1].  With Pointer Bounds Checker off, the built-in function
1      just returns the first argument.
1 
1           void init_objects (object *objs, size_t size)
1           {
1             size_t i;
1             /* Initialize objects one-by-one passing pointers with bounds of
1                an object, not the full array of objects.  */
1             for (i = 0; i < size; i++)
1               init_object (__builtin___bnd_narrow_ptr_bounds (objs + i, objs,
1                                                               sizeof(object)));
1           }
1 
1  -- Built-in Function: void * __builtin___bnd_copy_ptr_bounds (const
1           void *Q, const void *R)
1 
1      This built-in function returns a new pointer with the value of Q,
1      and associates it with the bounds already associated with pointer
1      R.  With Pointer Bounds Checker off, the built-in function just
1      returns the first argument.
1 
1           /* Here is a way to get pointer to object's field but
1              still with the full object's bounds.  */
1           int *field_ptr = __builtin___bnd_copy_ptr_bounds (&objptr->int_field,
1                                                             objptr);
1 
1  -- Built-in Function: void * __builtin___bnd_init_ptr_bounds (const
1           void *Q)
1 
1      This built-in function returns a new pointer with the value of Q,
1      and associates it with INIT (allowing full memory access) bounds.
1      With Pointer Bounds Checker off, the built-in function just returns
1      the first argument.
1 
1  -- Built-in Function: void * __builtin___bnd_null_ptr_bounds (const
1           void *Q)
1 
1      This built-in function returns a new pointer with the value of Q,
1      and associates it with NULL (allowing no memory access) bounds.
1      With Pointer Bounds Checker off, the built-in function just returns
1      the first argument.
1 
1  -- Built-in Function: void __builtin___bnd_store_ptr_bounds (const void
1           **PTR_ADDR, const void *PTR_VAL)
1 
1      This built-in function stores the bounds associated with pointer
1      PTR_VAL and location PTR_ADDR into Bounds Table.  This can be
1      useful to propagate bounds from legacy code without touching the
1      associated pointer's memory when pointers are copied as integers.
1      With Pointer Bounds Checker off, the built-in function call is
1      ignored.
1 
1  -- Built-in Function: void __builtin___bnd_chk_ptr_lbounds (const void
1           *Q)
1 
1      This built-in function checks if the pointer Q is within the lower
1      bound of its associated bounds.  With Pointer Bounds Checker off,
1      the built-in function call is ignored.
1 
1           extern void *__wrap_memset (void *dst, int c, size_t len)
1           {
1             if (len > 0)
1               {
1                 __builtin___bnd_chk_ptr_lbounds (dst);
1                 __builtin___bnd_chk_ptr_ubounds ((char *)dst + len - 1);
1                 __real_memset (dst, c, len);
1               }
1             return dst;
1           }
1 
1  -- Built-in Function: void __builtin___bnd_chk_ptr_ubounds (const void
1           *Q)
1 
1      This built-in function checks if the pointer Q is within the upper
1      bound of its associated bounds.  With Pointer Bounds Checker off,
1      the built-in function call is ignored.
1 
1  -- Built-in Function: void __builtin___bnd_chk_ptr_bounds (const void
1           *Q, size_t SIZE)
1 
1      This built-in function checks if [Q, Q + SIZE - 1] is within the
1      lower and upper bounds associated with Q.  With Pointer Bounds
1      Checker off, the built-in function call is ignored.
1 
1           extern void *__wrap_memcpy (void *dst, const void *src, size_t n)
1           {
1             if (n > 0)
1               {
1                 __bnd_chk_ptr_bounds (dst, n);
1                 __bnd_chk_ptr_bounds (src, n);
1                 __real_memcpy (dst, src, n);
1               }
1             return dst;
1           }
1 
1  -- Built-in Function: const void * __builtin___bnd_get_ptr_lbound
1           (const void *Q)
1 
1      This built-in function returns the lower bound associated with the
1      pointer Q, as a pointer value.  This is useful for debugging using
1      'printf'.  With Pointer Bounds Checker off, the built-in function
1      returns 0.
1 
1           void *lb = __builtin___bnd_get_ptr_lbound (q);
1           void *ub = __builtin___bnd_get_ptr_ubound (q);
1           printf ("q = %p  lb(q) = %p  ub(q) = %p", q, lb, ub);
1 
1  -- Built-in Function: const void * __builtin___bnd_get_ptr_ubound
1           (const void *Q)
1 
1      This built-in function returns the upper bound (which is a pointer)
1      associated with the pointer Q.  With Pointer Bounds Checker off,
1      the built-in function returns -1.
1