Add alignment checks

This is when I have to cast a pointer type to another that might need
higher alignment.

Signed-off-by: Gavin D. Howard <gavin@yzena.com>
master
Gavin D. Howard 2 months ago
parent 65c2db3a13
commit 80af8c2bd6
Signed by: gavin
GPG Key ID: F890265DD80E4E90

@ -64,6 +64,8 @@
#include "alloc.h"
#include "allocpage.h"
#include "../yc.h"
#include "../concurrency/strucon.h"
y_ulong y_alloc_pagesize;
@ -288,6 +290,8 @@ y_allocpage_destroy(void* page)
y_usize n;
y_uchar* ptr;
y_assert(yc_alignment_matches(p2, y_uchar), "Invalid alignment");
dtor = p2->dtor;
n = sizeof(y_allocpage);
ptr = (y_uchar*) (void*) p2;

@ -534,6 +534,8 @@ y_stackpool_exitFunc(y_Stackpool p, const char* func)
ptr = y_stackmmap_last(mmap, alloc->size);
}
y_assert(yc_alignment_matches(ptr, char*), "Invalid alignment");
func2 = *((char**) (void*) ptr);
yc_assert(func2 != NULL, YC_ASSERT_NULL);
@ -625,6 +627,8 @@ y_stackpool_printStack_notrace(y_stackpool* p, y_File f)
have_one = true;
y_assert(yc_alignment_matches(ptr, char*), "Invalid alignment");
func = *((char**) (void*) ptr);
s = y_fputs_locked_notrace(f, func, strlen(func));
@ -877,6 +881,9 @@ y_stackpool_checkAllocValidity(y_stackpool* p)
{
y_stackalloc* alptr;
y_assert(yc_alignment_matches(aptr, y_stackalloc),
"Invalid alignment");
alptr = (y_stackalloc*) (void*) aptr;
y_assert(mmap_used == 0 || mmap_used >= alptr->size,
@ -887,6 +894,8 @@ y_stackpool_checkAllocValidity(y_stackpool* p)
if (mmap_used == 0)
{
char* temp;
if (mmaps_used == sizeof(y_allocpage))
{
y_assert(cur_mmaps != prev_mmaps, "Too few mmaps");
@ -898,9 +907,13 @@ y_stackpool_checkAllocValidity(y_stackpool* p)
}
mmaps_used -= sizeof(y_stackmmap);
cur_mmap = (y_stackmmap*) (void*) (((char*) cur_mmaps) +
(mmaps_used -
sizeof(y_stackmmap)));
temp = (((char*) cur_mmaps) +
(mmaps_used - sizeof(y_stackmmap)));
y_assert(yc_alignment_matches(temp, y_stackmmap),
"Invalid alignment");
cur_mmap = (y_stackmmap*) (void*) temp;
mmap_used = cur_mmap->used;
}
@ -954,6 +967,9 @@ y_stackpool_checkMallocValidity(y_stackpool* p)
{
y_stackmalloc* malptr;
y_assert(yc_alignment_matches(mptr, y_stackmalloc),
"Invalid alignment");
malptr = (y_stackmalloc*) (void*) mptr;
y_assert(malptr->size > 0, "Invalid malloc size");

@ -630,6 +630,7 @@ y_vec_addTree(y_Vector v, y_usize idx)
y_tree t;
y_treevec* tv;
y_Tree ret;
y_uchar* temp;
y_call_dbg();
@ -644,7 +645,11 @@ y_vec_addTree(y_Vector v, y_usize idx)
s = y_vec_addHelper(v, idx, 1, &t);
if (y_err(s != y_STATUS_SUCCESS)) y_return_dbg(NULL);
ret = (y_Tree) (void*) (v->array + idx * sizeof(y_tree));
temp = v->array + idx * sizeof(y_tree);
y_assert(yc_alignment_matches(temp, y_tree), "Invalid alignment");
ret = (y_Tree) (void*) temp;
y_return_dbg(ret);
}
@ -747,6 +752,7 @@ y_vec_pushTree(y_Vector v)
y_treevec* tv;
y_usize len;
y_Tree ret;
y_uchar* temp;
y_call_dbg();
@ -763,7 +769,11 @@ y_vec_pushTree(y_Vector v)
s = y_vec_pushHelper(v, 1, &t);
if (y_err(s != y_STATUS_SUCCESS)) y_return_dbg(NULL);
ret = (y_Tree) (void*) (v->array + len * sizeof(y_tree));
temp = v->array + len * sizeof(y_tree);
y_assert(yc_alignment_matches(temp, y_tree), "Invalid alignment");
ret = (y_Tree) (void*) temp;
y_return_dbg(ret);
}
@ -903,6 +913,7 @@ y_vec_pushTreeAt(y_Vector v, y_usize idx)
y_tree t;
y_treevec* tv;
y_Tree ret;
y_uchar* temp;
y_call_dbg();
@ -917,7 +928,11 @@ y_vec_pushTreeAt(y_Vector v, y_usize idx)
s = y_vec_pushAtHelper(v, idx, 1, &t);
if (y_err(s != y_STATUS_SUCCESS)) y_return_dbg(NULL);
ret = (y_Tree) (void*) (v->array + idx * sizeof(y_tree));
temp = v->array + idx * sizeof(y_tree);
y_assert(yc_alignment_matches(temp, y_tree), "Invalid alignment");
ret = (y_Tree) (void*) temp;
y_return_dbg(ret);
}

@ -446,6 +446,8 @@ y_fs_os_getcwd(y_Path* res)
goto err;
}
y_assert(yc_alignment_matches(v.array, y_pchar), "Invalid alignment");
res->a = (y_pchar*) (void*) v.array;
y_syscall_dbg("y_pstrlen");

@ -65,6 +65,8 @@
#include "multiplex.h"
#include "../yc.h"
#include <stdint.h>
/**
@ -83,7 +85,13 @@ y_multiplex_data_destroy(void* ptrs)
y_call_dbg();
y_assert(yc_alignment_matches(ptrs, y_uchar*), "Invalid alignment");
ps = (y_uchar**) ptrs;
y_assert(yc_alignment_matches(ps[y_MULTIPLEX_DATA_IDX], y_uchar*),
"Invalid alignment");
data = (y_MultiplexData*) (void*) ps[y_MULTIPLEX_DATA_IDX];
if (data->dtor != NULL)

@ -776,6 +776,9 @@ yao_parse_fn(y_lang_Parse* p, y_lang_Token* t)
}
while (more_args);
y_assert(yc_alignment_matches(args_builder.array, yvm_Local),
"Invalid alignment");
// XXX: This is a cheese, but it works for now.
args.a = (yvm_Local*) (void*) args_builder.array;
args.len = args_builder.len;

@ -755,6 +755,7 @@ typedef y_simd256 yc_int32;
#define y_MEMCPY_INC(n) \
do \
{ \
y_assert(yc_alignment_matches(s, yc_int##n), "Invalid alignment"); \
*((yc_int##n*) (void*) d) = *((const yc_int##n*) (const void*) s); \
d += n; \
s += n; \
@ -1347,6 +1348,9 @@ simple:
y_assert(n <= old_n, "n wrapped around; this is a bug");
#endif // YC_DEBUG_CODE
y_assert(yc_alignment_matches(s, yc_int16), "Invalid alignment");
y_assert(yc_alignment_matches(d, yc_int16), "Invalid alignment");
// The cast to void gets rid of cast alignment warnings.
d16 = (yc_int16*) (void*) d;
s16 = (const yc_int16*) (const void*) s;
@ -1529,6 +1533,9 @@ simple:
y_assert(n <= old_n, "n wrapped around; this is a bug");
#endif // YC_DEBUG_CODE
y_assert(yc_alignment_matches(s, yc_int8), "Invalid alignment");
y_assert(yc_alignment_matches(d, yc_int8), "Invalid alignment");
// The cast to void gets rid of cast alignment warnings.
d8 = (yc_int8*) (void*) d;
s8 = (const yc_int8*) (const void*) s;
@ -1640,6 +1647,9 @@ simple:
y_assert(n <= old_n, "n wrapped around; this is a bug");
#endif // YC_DEBUG_CODE
y_assert(yc_alignment_matches(s, yc_int4), "Invalid alignment");
y_assert(yc_alignment_matches(d, yc_int4), "Invalid alignment");
// The cast to void gets rid of cast alignment warnings.
d4 = (yc_int4*) (void*) d;
s4 = (const yc_int4*) (const void*) s;
@ -1701,6 +1711,9 @@ simple:
y_assert(n <= old_n, "n wrapped around; this is a bug");
#endif // YC_DEBUG_CODE
y_assert(yc_alignment_matches(s, yc_int2), "Invalid alignment");
y_assert(yc_alignment_matches(d, yc_int2), "Invalid alignment");
// The cast to void gets rid of cast alignment warnings.
d2 = (yc_int2*) (void*) d;
s2 = (const yc_int2*) (const void*) s;

@ -94,6 +94,18 @@ extern "C"
// TODO: Go through and add const to every applicable parameter for every
// function.
/**
* @def yc_alignment_matches
* Returns non-zero (true) if the alignment of the given pointer is valid for
* the given type.
* @param p The pointer to test the alignment of.
* @param t The type to test the alignment against.
* @return True if the pointer's alignment is valid, false otherwise.
* @pre @a p must be a pointer.
* @pre @a t must be a type name.
*/
#define yc_alignment_matches(p, t) (((y_uptr) (p)) % alignof(t) == 0)
/**
* @def y_TEMP_UNUSED
* A macro to label function arguments as unused. This is only meant to be used

Loading…
Cancel
Save