15#define SAPI_SAFE_PTR_CANARY ((uint32_t)0x5AFE9021U)
23 if ((ptr == NULL) && (size != 0U))
43 return sp->
ptr != NULL;
48 if ((sp == NULL) || (out_ptr == NULL))
55 "sapi_safe_ptr_get: canary mismatch");
71 if ((sp == NULL) || (out_ptr == NULL))
78 "sapi_safe_ptr_offset: canary mismatch");
92 "sapi_safe_ptr_offset: offset + length overflowed size_t");
98 "sapi_safe_ptr_offset: offset + length exceeds wrapped region size");
101 *out_ptr = (
void *)(&((uint8_t *)sp->
ptr)[offset]);
sapi_status_t sapi_cast_checked_add_size(size_t a, size_t b, size_t *out)
Checked addition: *out = a + b, only if the result fits size_t.
bool sapi_safe_ptr_is_valid(const sapi_safe_ptr_t *sp)
Checks whether sp currently wraps a valid, non-invalidated region.
void sapi_safe_ptr_invalidate(sapi_safe_ptr_t *sp)
Invalidates sp: clears the wrapped pointer and the canary, so every subsequent access on sp fails SAP...
sapi_status_t sapi_safe_ptr_get(const sapi_safe_ptr_t *sp, void **out_ptr)
Retrieves the wrapped pointer directly (the whole region, no offset) after validating it.
sapi_status_t sapi_safe_ptr_init(sapi_safe_ptr_t *sp, void *ptr, size_t size)
Initializes sp to wrap [ptr, ptr + size), writing the canary.
sapi_status_t sapi_safe_ptr_offset(const sapi_safe_ptr_t *sp, size_t offset, size_t length, void **out_ptr)
Computes a bounds-checked pointer at offset within sp's region, verifying the caller's intended acces...
void sapi_safety_violation_report(sapi_safety_violation_kind_t kind, const char *file, int32_t line, const char *message)
Reports one violation to the registered handler, if any.
@ SAPI_SAFETY_VIOLATION_CORRUPTION
@ SAPI_SAFETY_VIOLATION_OUT_OF_RANGE
sapi_status_t
Common result/status codes.
@ SAPI_STATUS_INVALID_PARAM
@ SAPI_STATUS_VALUE_OUT_OF_RANGE
@ SAPI_STATUS_DATA_CORRUPTION
Checked integer casting between all fixed-width types and size_t (ADR-003). Every conversion in the c...
#define SAPI_SAFE_PTR_CANARY
Safe-pointer wrapper: bounds + NULL + corruption-canary checked access to a raw memory region,...
Opt-in notification hook for the three new safety primitives (safe pointer, checked integer arithmeti...
A bounds-checked, canary-protected wrapper around one raw memory region. Opaque to callers in spirit ...