aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_SafeMemory.h
blob: 0383fe39c5b927178599753a647b111414f50e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_SafeMemory.h
 * @ingroup developer
 *
 * @brief Facade to memory functions to make calls safer.
 *
 * This is a facade to interpose between an application and the standard C library functions posix_memalign(3),
 * malloc(3), calloc(3), realloc(3) and free(3) that establishes detectable boundaries around an allocated memory segment,
 * records a stack backtrace for each allocation,
 * detects buffer overruns and underruns by checking the boundaries when the memory is deallocated,
 * and tries to prevent a stray pointer to reference the memory again once it's been deallocated.
 *
 * The allocated memory consists of three contiguous segments: the prefix, the memory usable by the caller, and the suffix.
 * The memory usable by the caller is aligned as specified by the caller.
 * The alignment must be a power of 2 greater than or equal to the size of a {@code void *}.
 * <pre>
 * +--base  +-prefix     +-- aligned memory   +-- suffix aligned on (void *)
 * v        v            v                    v
 * |________|PPPPPPPPPPPP|mmmmmmmmm...mmmm|___|SSSSSSSSS
 *                                         ^
 *                                         +-- variable padding
 * </pre>
 * Where '-' indicates padding, 'P' indicates the prefix data structure, 'm'
 * indicates contiguous memory for use by the caller, and 'S" indicates the suffix data structure.
 *
 * To enable this facade, you must include the following line in your execution before any allocations are performed.
 *
 * @code
 * {
 *     parcMemory_SetInterface(&PARCSafeMemoryAsPARCMemory);
 * }
 * @endcode
 *
 */
#ifndef libparc_parc_SafeMemory_h
#define libparc_parc_SafeMemory_h

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

#include <parc/algol/parc_Memory.h>

typedef enum parc_safety_memory_state {
    PARCSafeMemoryState_OK = 0,
    PARCSafeMemoryState_MISMATCHED = 1,
    PARCSafeMemoryState_UNDERRUN = 2,
    PARCSafeMemoryState_OVERRUN = 3,
    PARCSafeMemoryState_NOTHINGALLOCATED = 4,
    PARCSafeMemoryState_ALREADYFREE = 5
} PARCSafeMemoryState;

/**
 * Generate a readable, null-terminated C string representation
 * for the specified `PARCSafeMemoryState` type.
 *
 * @param [in] status A `PARCSafeMemoryState` value.
 *
 * @return A null-terminated C string that must be freed when done.
 *
 * Example:
 * @code
 * {
 *     size_t size = 100;
 *     uint32_t alignment = sizeof(void *);
 *     void *memory;
 *
 *     memory = parcSafeMemory_Allocate(size);
 *     PARCSafeMemoryState state = parcSafeMemory_Destroy(&memory);
 *
 *     printf("SafeMemoryState = %s\n", parcSafeMemoryState_ToString(state));
 * }
 * @endcode
 */
const char *parcSafeMemoryState_ToString(PARCSafeMemoryState status);

/**
 * Memory operations defined by {@link PARCMemoryInterface}
 * and implemented by the Safe Memory functions.
 */
extern PARCMemoryInterface PARCSafeMemoryAsPARCMemory;

/**
 * Allocate Safe Memory.
 *
 * Allocate memory through the configured memory allocator, setting the environment to track this memory.
 *
 * @param [in] size The number of bytes necessary.
 *
 * @return non-NULL A pointer to allocated memory.
 * @return NULL Memory could not be allocated.
 *
 * Example:
 * @code
 * {
 *     size_t size = 100;
 *     void *memory = parcSafeMemory_Allocate(size);
 * }
 * @endcode
 */
void *parcSafeMemory_Allocate(size_t size);

/**
 * Allocate Safe Memory.
 *
 * Allocate memory through the configured memory allocator, setting the environment to track this memory.
 *
 * @param [in] requestedSize The number of bytes necessary.
 *
 * @return non-NULL A pointer to allocated memory.
 * @return NULL Memory could not be allocated.
 *
 * Example:
 * @code
 * {
 *     size_t size = 100;
 *     void *memory = parcSafeMemory_Allocate(size);
 * }
 * @endcode
 */
void *parcSafeMemory_AllocateAndClear(size_t requestedSize);

/**
 * Allocate aligned memory.
 *
 * Allocates @p size bytes of memory such that the allocation's
 * base address is an exact multiple of alignment,
 * and returns the allocation in the value pointed to by @p pointer.
 *
 * The requested alignment must be a power of 2 greater than or equal to `sizeof(void *)`.
 *
 * Memory that is allocated can be used as an argument in subsequent call `Reallocate`, however
 * `Reallocate` is not guaranteed to preserve the original alignment.
 *
 * @param [out] pointer A pointer to a `void *` pointer that will be set to the address of the allocated memory.
 * @param [in] alignment A power of 2 greater than or equal to `sizeof(void *)`
 * @param [in] size The number of bytes to allocate.
 *
 * @return 0 Successful
 * @return EINVAL The alignment parameter is not a power of 2 at least as large as sizeof(void *)
 * @return ENOMEM Memory allocation error.
 *
 * Example:
 * @code
 * {
 *     void *allocatedMemory;
 *
 *     int failure = parcSafeMemory_MemAlign(&allocatedMemory, sizeof(void *), 100);
 *     if (failure == 0) {
 *         parcSafeMemory_Deallocate(&allocatedMemory);
 *         // allocatedMemory is now equal to zero.
 *     }
 * }
 * @endcode
 * @see `posix_memalign`
 */
int parcSafeMemory_MemAlign(void **pointer, size_t alignment, size_t size);

/**
 * Deallocate memory previously allocated with {@link parcSafeMemory_Allocate}
 *
 * The value pointed to by @p pointer will be set to NULL.
 *
 * @param [in,out] pointer A pointer to a pointer to the allocated memory.
 *
 * Example:
 * @code
 * {
 *     size_t size = 100;
 *     void *memory = parcSafeMemory_Allocate(size);
 *
 *     parcSafeMemory_Deallocate(&memory);
 * }
 * @endcode
 */
void parcSafeMemory_Deallocate(void **pointer);

/**
 * A (mostly) suitable replacement for realloc(3).
 * The primary difference is that it is an error if newSize is zero.
 * If the newSize is equal to the old size, then NULL is returned.
 *
 * @param [in] original Pointer to the original memory
 * @param [in] newSize The size of the newly re-allocated memory.
 *
 * @return Non-NULL A pointer to the newly allocated memory
 * @return NULL An error occurred (newSize == oldSize, or newSize == 0)
 *
 * Example:
 * @code
 * {
 *     void *memory = parcSafeMemory_Allocate(100);
 *
 *     size_t newLength = 0;
 *     unsigned char *newMemory = parcSafeMemory_Reallocate(memory, newLength);
 *
 *     assertTrue(newMemory == NULL, "Expected NULL, actual %p", newMemory);
 * }
 * @endcode
 */
void *parcSafeMemory_Reallocate(void *original, size_t newSize);

/**
 * Duplicate the given null-terminated C string.
 *
 * @param [in] string A pointer to a null-terminated C string.
 * @param [in] length The length of the string, not including the terminating null character.
 *
 * @return non-NULL Allocated Safe Memory containing the duplicate string. This must be freed via `parcSafeMemory_Deallocate`.
 * @return NULL Memory could not be allocated.
 *
 * Example:
 * @code
 * {
 *     char *string = "hello world";
 *     char *actual = parcSafeMemory_StringDuplicate(string, strlen(string));
 *     ...
 * }
 * @endcode
 *
 * @see parcSafeMemory_Deallocate
 */
char *parcSafeMemory_StringDuplicate(const char *string, size_t length);

/**
 * Return the number of outstanding allocations.
 *
 * In practice, every allocation should be matched with a corresponding deallocation.
 * This return the number of allocations that have not been deallocated.
 *
 * @return The number of outstanding allocations.
 *
 */
uint32_t parcSafeMemory_Outstanding(void);

/**
 * Display information about outstanding memory allocations.
 *
 * To enable this function, you must include the following line in your execution before any allocations are performed.
 *
 * @code
 * {
 *     parcMemory_SetInterface(&PARCSafeMemoryAsPARCMemory);
 * }
 * @endcode
 *
 * @param [in] outputFd Output file descriptor.
 *
 * @return The number of currenly outstanding allocations.
 *
 * Example:
 * @code
 * {
 *     parcMemory_SetInterface(&PARCSafeMemoryAsPARCMemory);
 *
 *     ...
 *
 *     FILE *fd = fopen ("log.txt", "w");
 *     size_t outstandingAllocations = parcSafeMemory_ReportAllocation(fd);
 * }
 * @endcode
 */
uint32_t parcSafeMemory_ReportAllocation(int outputFd);

/**
 * Determine if a pointer to Safe Memory is valid.
 *
 * Invalid indicates the memory is overrun or underrun.
 *
 * To enable this function, you must include the following line in your execution before any allocations are performed.
 *
 * @code
 * {
 *     parcMemory_SetInterface(&PARCSafeMemoryAsPARCMemory);
 * }
 * @endcode
 *
 * @param [in] memory A pointer to previously allocated Safe Memory.
 *
 * @return true The memory is valid;
 * @return false The memory is invalid;
 *
 * Example:
 * @code
 * {
 *     void *memory = parcSafeMemory_Allocate(100);
 *     if (parcSafeMemory_IsValid(memory) == false) {
 *         printf("Memory is invalid\n");
 *     }
 * @endcode
 */
bool parcSafeMemory_IsValid(const void *memory);

/**
 * Print a human readable representation of the given PARC Safe Memory array.
 *
 * To enable this function, you must include the following line in your execution before any allocations are performed.
 *
 * @code
 * {
 *     parcMemory_SetInterface(&PARCSafeMemoryAsPARCMemory);
 * }
 * @endcode
 *
 * @param [in] indentation The level of indentation to use to pretty-print the output.
 * @param [in] memory A pointer to the memory to display.
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *instance = parcBuffer_Create();
 *
 *     parcBuffer_Display(instance, 0);
 *
 *     parcBuffer_Release(&instance);
 * }
 * @endcode
 */
void parcSafeMemory_Display(const void *memory, int indentation);
#endif // libparc_parc_SafeMemory_h