Developer Guide and Reference

Contents

Writing a Wrapper

The pointer checker is not supported on
macOS*
systems.
You can write your own wrappers for run-time library functions. Typically, you would use one or more of the pointer checker intrinsics.
Example: Allocation Wrapping with __chkp_make_bounds
extern void *wrap_malloc(size_t bytes) { void* ppp; ppp = malloc(bytes); if (ppp) { ppp = (void*)__chkp_make_bounds(ppp, bytes); } else { ppp = (void*)0;} return ppp; }
The next example shows a wrapper that checks the validity of the pointer passed by performing writes to the first and last addresses that the C run-time routine will write. This will cause out of bounds events if necessary, while still allowing optimized handling of the C run-time library call.
Example: Checking without using Pointer Checker Intrinsics
extern void *wrap_memset(void *dst, int c, size_t size) { if (size > 0) { *(char *)dst = c; // write to first address *((char*)dst+size-1) = c; // write to last address (void)memset(dst, c, size); } return dst; }
Alternatively, you can perform the checking directly by comparing to the bounds associated with the pointer. In this case, you must first make sure that the bounds are meaningful. You can use the
__chkp_upper_bound
and
__chkp_lower_bound
intrinsics for this purpose.
Example: Upper and Lower Bound Intrinsics
extern void *wrap_memset(void *dst, int c, size_t size) { if (size > 0) { char *ub = __chkp_upper_bound(&dst); if ((intptr_t)ub != (intptr_t)-1) { char *lb = __chkp_lower_bound(&dst); char *max = (char*)dst+size-1; if (dst < lb) *(char*)dst = c; // cause bounds violation if (max > ub) *(char*)max = c; // cause bounds violation } (void)memset(dst, c, size); } return dst; }