Safe API Framework
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Loading...
Searching...
No Matches
sapi_safe_ptr.c
Go to the documentation of this file.
1
7
10
15#define SAPI_SAFE_PTR_CANARY ((uint32_t)0x5AFE9021U)
16
18{
19 if (sp == NULL)
20 {
22 }
23 if ((ptr == NULL) && (size != 0U))
24 {
26 }
27 sp->ptr = ptr;
28 sp->size = size;
30 return SAPI_STATUS_OK;
31}
32
34{
35 if (sp == NULL)
36 {
37 return false;
38 }
40 {
41 return false;
42 }
43 return sp->ptr != NULL;
44}
45
47{
48 if ((sp == NULL) || (out_ptr == NULL))
49 {
51 }
53 {
55 "sapi_safe_ptr_get: canary mismatch");
57 }
58 if (sp->ptr == NULL)
59 {
61 }
62 *out_ptr = sp->ptr;
63 return SAPI_STATUS_OK;
64}
65
66sapi_status_t sapi_safe_ptr_offset(const sapi_safe_ptr_t *sp, size_t offset, size_t length, void **out_ptr)
67{
68 size_t end;
69 sapi_status_t status;
70
71 if ((sp == NULL) || (out_ptr == NULL))
72 {
74 }
76 {
78 "sapi_safe_ptr_offset: canary mismatch");
80 }
81 if (sp->ptr == NULL)
82 {
84 }
85 /* Checked, not raw, addition: offset + length must not itself
86 * overflow size_t before it can even be compared against sp->size
87 * (REQ-OAL-SAFEPTR-002). */
88 status = sapi_cast_checked_add_size(offset, length, &end);
89 if (status != SAPI_STATUS_OK)
90 {
92 "sapi_safe_ptr_offset: offset + length overflowed size_t");
94 }
95 if (end > sp->size)
96 {
98 "sapi_safe_ptr_offset: offset + length exceeds wrapped region size");
100 }
101 *out_ptr = (void *)(&((uint8_t *)sp->ptr)[offset]);
102 return SAPI_STATUS_OK;
103}
104
106{
107 if (sp == NULL)
108 {
109 return;
110 }
111 sp->ptr = NULL;
112 sp->size = 0U;
113 sp->canary = 0U;
114}
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.
Definition sapi_cast.c:1148
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.
Definition sapi_status.h:27
@ SAPI_STATUS_INVALID_PARAM
Definition sapi_status.h:29
@ SAPI_STATUS_VALUE_OUT_OF_RANGE
Definition sapi_status.h:39
@ SAPI_STATUS_DATA_CORRUPTION
Definition sapi_status.h:37
@ SAPI_STATUS_OK
Definition sapi_status.h:28
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 ...