Compare commits

..

3 Commits

Author SHA1 Message Date
6efe821829 fix: violating strict aliasing rules in most areas in the new code.
Yes, I am aware there are plenty of violations in `conf.c`, but I'll
likely fix/rewrite those when I will use it. Since there are some other
changes I think I'll want to make.
2025-09-04 12:54:00 +02:00
0baa1ac9e8 add a flag when debugging to generate SIGFPE when signed integer overflow occurs. (this is UB) 2025-09-04 12:54:00 +02:00
9cb0631df8 add comments to point at where strict aliasing rule violations occur in data scripts
this is definitely not all of them, a thorough rewrite must take place
to fix as many as we can.
2025-09-04 12:54:00 +02:00
5 changed files with 35 additions and 35 deletions

View File

@@ -14,7 +14,7 @@ MARCH ?= $(shell uname -m)
KERNEL ?= $(shell uname -s | tr '[:upper:]' '[:lower:]') KERNEL ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
# compilation flags # compilation flags
CFLAGS += -c -std=gnu99 -Wall -Wextra -Wpedantic -MMD -MP -Wno-pointer-arith CFLAGS += -c -std=gnu99 -Wall -Wextra -Wpedantic -MMD -MP
LDFLAGS += -flto LDFLAGS += -flto
# architecture/OS detection # architecture/OS detection

View File

@@ -16,7 +16,7 @@
#define CHUNKS 0x400 // amount of chunks in a file #define CHUNKS 0x400 // amount of chunks in a file
/* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */ /* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */
static void mvchunks(u32 *restrict table, void *src, void *dst, int src_s, int src_e) { static void mvchunks(u32 *restrict table, u8 *src, u8 *dst, int src_s, int src_e) {
assert(src > dst); assert(src > dst);
size_t len = src - dst; // acquire the amount of bytes that we shall move size_t len = src - dst; // acquire the amount of bytes that we shall move
assert(!(len % SECTOR)); assert(!(len % SECTOR));
@@ -33,7 +33,7 @@ static void mvchunks(u32 *restrict table, void *src, void *dst, int src_s, int s
/* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`. /* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`.
* `rmb` is an optional additional offset that can be applied, and signifies bytes already removed. * `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
* Returns the bytes removed by this function. */ * Returns the bytes removed by this function. */
static size_t delchunk(void *restrict buf, u32 *restrict table, size_t rmb, int sidx, int eidx) { static size_t delchunk(u8 *restrict buf, u32 *restrict table, size_t rmb, int sidx, int eidx) {
// load the table data // load the table data
size_t slen, bidx, blen; size_t slen, bidx, blen;
slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
@@ -45,16 +45,16 @@ static size_t delchunk(void *restrict buf, u32 *restrict table, size_t rmb, int
table[sidx + CHUNKS] = htobe32(time(NULL)); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead table[sidx + CHUNKS] = htobe32(time(NULL)); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead
// move the succeeding chunks over the deleted chunk // move the succeeding chunks over the deleted chunk
void *dst = buf + bidx - rmb; u8 *dst = buf + bidx - rmb;
void *src = buf + bidx + blen; u8 *src = buf + bidx + blen;
mvchunks(buf, src, dst, sidx, eidx - 1); mvchunks(table, src, dst, sidx, eidx - 1);
return blen; return blen;
} }
/* Call `delchunk` with the parameters and some defaults. Ensuring the table is copied correctly as well. /* Call `delchunk` with the parameters and some defaults. Ensuring the table is copied correctly as well.
* This is done instead of `delchunk` being globally linked, because * This is done instead of `delchunk` being globally linked, because
* `delchunk` requests more specific parameters, which is confusing outside this module. */ * `delchunk` requests more specific parameters, which is confusing outside this module. */
size_t mcx_delchunk(void *restrict buf, int chunk) { size_t mcx_delchunk(u8 *restrict buf, int chunk) {
u32 table[TABLE]; u32 table[TABLE];
memcpy(table, buf, sizeof(table)); memcpy(table, buf, sizeof(table));
size_t res = delchunk(buf, table, 0, chunk, CHUNKS); size_t res = delchunk(buf, table, 0, chunk, CHUNKS);
@@ -62,12 +62,12 @@ size_t mcx_delchunk(void *restrict buf, int chunk) {
return res; return res;
} }
size_t mcx_delchunk_range(void *restrict buf, int start, int end) { size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) {
assert(start < end && end < CHUNKS); assert(start < end && end < CHUNKS);
u32 table[TABLE]; u32 table[TABLE];
memcpy(table, buf, sizeof(table)); memcpy(table, buf, sizeof(table));
void *dst = buf + (be32toh(table[start]) >> 8) * SECTOR; u8 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR;
void *src = buf + (be32toh(table[end]) >> 8) * SECTOR; u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
src += (be32toh(table[end]) & 0xFF) * SECTOR; src += (be32toh(table[end]) & 0xFF) * SECTOR;
// zeroes-out the chunk data within this range. (and set the timestamp) // zeroes-out the chunk data within this range. (and set the timestamp)
@@ -79,7 +79,7 @@ size_t mcx_delchunk_range(void *restrict buf, int start, int end) {
// move the remaining chunks down // move the remaining chunks down
if (end < (CHUNKS - 1)) if (end < (CHUNKS - 1))
mvchunks(buf, src, dst, end, (CHUNKS - 1)); mvchunks(table, src, dst, end, (CHUNKS - 1));
memcpy(buf, table, sizeof(table)); memcpy(buf, table, sizeof(table));
return src - dst; return src - dst;
} }
@@ -93,7 +93,7 @@ static int cmp_chunkids(const void *restrict x, const void *restrict y) {
/* Sorts the chunks marked for deletion from smallest to greatest index. /* Sorts the chunks marked for deletion from smallest to greatest index.
* Then performs the deletion in this order. Making sure to only update the chunks up to the next. */ * Then performs the deletion in this order. Making sure to only update the chunks up to the next. */
size_t mcx_delchunk_bulk(void *restrict buf, const u16 *restrict chunks, int chunkc) { size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) {
// ensure the chunks ids we're working on are sorted from least to greatest // ensure the chunks ids we're working on are sorted from least to greatest
u16 chunkids[chunkc + 1]; u16 chunkids[chunkc + 1];
memcpy(chunkids, chunks, chunkc); memcpy(chunkids, chunks, chunkc);
@@ -113,9 +113,9 @@ size_t mcx_delchunk_bulk(void *restrict buf, const u16 *restrict chunks, int chu
/* Sum together the 4th byte in each location integer to compute the sector size of all chunks. /* Sum together the 4th byte in each location integer to compute the sector size of all chunks.
* Multiplying by `SECTOR`, and adding the size of the table itself. */ * Multiplying by `SECTOR`, and adding the size of the table itself. */
size_t mcx_calcsize(const void *restrict buf) { size_t mcx_calcsize(const u8 *restrict buf) {
size_t size = 0; size_t size = 0;
for (uint i = 0; i < CHUNKS; i++) for (uint i = 0; i < CHUNKS; i++)
size += *(u8 *)(buf + (i * 4) + 3); size += *(buf + (i * 4) + 3);
return (size * CHUNKS) + (TABLE * 4); return (size * CHUNKS) + (TABLE * 4);
} }

View File

@@ -18,20 +18,20 @@ struct mcx_chunk {
* The chunk's location data shall become `0`, and timestamp data the current time. * The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space. * All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed. */ * Returns the amount of bytes removed. */
size_t mcx_delchunk(void *restrict buf, int chunk) NONNULL((1)); size_t mcx_delchunk(u8 *restrict buf, int chunk) NONNULL((1));
/* Deletes the range defined by `start`—`end` (inclusive) of chunks out of `buf`. /* Deletes the range defined by `start`—`end` (inclusive) of chunks out of `buf`.
* The chunk's location data shall become `0`, and timestamp data the current time. * The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space. * All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */ * Returns the amount of bytes removed */
size_t mcx_delchunk_range(void *restrict buf, int start, int end) NONNULL((1)); size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) NONNULL((1));
/* Deletes a `chunkc` chunks from `chunks` out of `buf`. /* Deletes a `chunkc` chunks from `chunks` out of `buf`.
* If the `chunks` indices are known to be sequential, i.e. have a constant difference of `1`, `mcx_delchunk_range` should be preferred. * If the `chunks` indices are known to be sequential, i.e. have a constant difference of `1`, `mcx_delchunk_range` should be preferred.
* The chunk's location data shall become `0`, and timestamp data the current time. * The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space. * All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */ * Returns the amount of bytes removed */
size_t mcx_delchunk_bulk(void *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2)); size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2));
/* Computes the byte size of the `*.mcX` file in `buf` and returns it. */ /* Computes the byte size of the `*.mcX` file in `buf` and returns it. */
size_t mcx_calcsize(const void *restrict buf) NONNULL((1)) PURE; size_t mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE;

View File

@@ -36,7 +36,7 @@ static inline u64 buftoh64(const void *restrict buf) {
/* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`. /* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`.
* The data shall be converted to little-endian on little-endian systems * The data shall be converted to little-endian on little-endian systems
* Outputs the allocated data to `out`, returns where the next pointer would be. */ * Outputs the allocated data to `out`, returns where the next pointer would be. */
static const void *procarr(const void *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out) { static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out) {
size_t len = nmemb * size; size_t len = nmemb * size;
*out = (struct nbt_array){ *out = (struct nbt_array){
out->nmemb = nmemb, out->nmemb = nmemb,
@@ -67,7 +67,7 @@ static const void *procarr(const void *restrict buf, i32 nmemb, uint size, struc
} }
/* calls `procarr` for the simple types available. */ /* calls `procarr` for the simple types available. */
static const void *proclist(const void *restrict buf, struct nbt_array *restrict out) { static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out) {
uint size; uint size;
switch (*(u8 *)buf) { switch (*(u8 *)buf) {
@@ -88,15 +88,15 @@ static const void *proclist(const void *restrict buf, struct nbt_array *restrict
return procarr(buf, len, size, out); return procarr(buf, len, size, out);
} }
const void *nbt_proctag(const void *restrict buf, u16 slen, void *restrict out) { const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
const void *ptr, *tmp; const u8 *ptr, *tmp;
ptr = buf + 3 + slen; ptr = buf + 3 + slen;
i32 nmem; i32 nmem;
uint size; uint size;
switch (*(u8 *)buf) { switch (*buf) {
case NBT_I8: *(u8 *)out = *(u8 *)ptr; return ptr + 1; case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
case NBT_I16: *(u16 *)out = buftoh16(ptr); return ptr + 2; case NBT_I16: *(u16 *)out = buftoh16(ptr); return ptr + 2;
case NBT_I32: // fall through case NBT_I32: // fall through
case NBT_F32: *(u32 *)out = buftoh32(ptr); return ptr + 4; case NBT_F32: *(u32 *)out = buftoh32(ptr); return ptr + 4;
@@ -123,10 +123,10 @@ const void *nbt_proctag(const void *restrict buf, u16 slen, void *restrict out)
* `ptr` is assumed to be the start of the `NBT_LIST` data, e.i. The list's ID, followed by the list's length. * `ptr` is assumed to be the start of the `NBT_LIST` data, e.i. The list's ID, followed by the list's length.
* If `ID` is `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, the entire list length is computed and returned. * If `ID` is `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, the entire list length is computed and returned.
* For other types this won't be possible, and thus will add `1` to `dpt`, and write the list data to `lens` and `tags` at this new `dpt`. */ * For other types this won't be possible, and thus will add `1` to `dpt`, and write the list data to `lens` and `tags` at this new `dpt`. */
static const void *nexttag_list(const void *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) { static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
const void *tag = ptr; const u8 *tag = ptr;
ptr++; ptr++;
switch (*(u8 *)tag) { switch (*tag) {
case NBT_END: break; case NBT_END: break;
case NBT_I8: ptr += (i32)buftoh32(ptr) * 1; break; case NBT_I8: ptr += (i32)buftoh32(ptr) * 1; break;
case NBT_I16: ptr += (i32)buftoh32(ptr) * 2; break; case NBT_I16: ptr += (i32)buftoh32(ptr) * 2; break;
@@ -137,7 +137,7 @@ static const void *nexttag_list(const void *restrict ptr, uint *restrict const d
default: default:
// TODO: handle out of bounds... Might not be required if we use flexible array member // TODO: handle out of bounds... Might not be required if we use flexible array member
(*dpt)++; (*dpt)++;
tags[*dpt] = *(u8 *)tag; tags[*dpt] = *tag;
lens[*dpt] = (i32)buftoh32(ptr); lens[*dpt] = (i32)buftoh32(ptr);
break; break;
} }
@@ -151,15 +151,15 @@ static const void *nexttag_list(const void *restrict ptr, uint *restrict const d
* - `lens` shall contain `MAX_DEPTH` of items representing the list length, if the current item is non-zero we shall assume we're in a list. * - `lens` shall contain `MAX_DEPTH` of items representing the list length, if the current item is non-zero we shall assume we're in a list.
* Where the value is decremented until we reach `0`. * Where the value is decremented until we reach `0`.
* - `tags` shall contain `MAX_DEPTH` of items representing the list's stored type. */ * - `tags` shall contain `MAX_DEPTH` of items representing the list's stored type. */
static const void *nexttag(const void *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) { static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
u8 type; u8 type;
const void *ptr = tag; const u8 *ptr = tag;
if (lens[*dpt]) { if (lens[*dpt]) {
type = tags[*dpt]; type = tags[*dpt];
lens[*dpt]--; lens[*dpt]--;
*dpt -= !lens[*dpt]; *dpt -= !lens[*dpt];
} else { } else {
type = *(u8 *)tag; type = *tag;
ptr += buftoh16(tag + 1) + 3; ptr += buftoh16(tag + 1) + 3;
} }
@@ -193,8 +193,8 @@ static const void *nexttag(const void *restrict tag, uint *restrict const dpt, i
* - compound:list:int32 * - compound:list:int32
* - string * - string
*/ */
const void *nbt_nexttag(const void *restrict buf) { const u8 *nbt_nexttag(const u8 *restrict buf) {
const void *tag; const u8 *tag;
u8 tags[MAX_DEPTH] = {0}; u8 tags[MAX_DEPTH] = {0};
i32 lens[MAX_DEPTH] = {0}; i32 lens[MAX_DEPTH] = {0};
uint dpt = 0; uint dpt = 0;

View File

@@ -49,8 +49,8 @@ struct nbt_array {
* if `buf` points to `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, `*out` is assumed * if `buf` points to `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, `*out` is assumed
* to have the available byte width for one of these types. In the case of `NBT_ARR*` and `NBT_LIST` * to have the available byte width for one of these types. In the case of `NBT_ARR*` and `NBT_LIST`
* it must point to a `struct nbt_array*`. Where in the case of `NBT_LIST`, it must be one of the previous static-width types. */ * it must point to a `struct nbt_array*`. Where in the case of `NBT_LIST`, it must be one of the previous static-width types. */
const void *nbt_proctag(const void *restrict buf, u16 slen, void *restrict out) NONNULL((1, 3)); const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) NONNULL((1, 3));
/* searches for the end of a named tag without processing data, the final pointer is returned. /* searches for the end of a named tag without processing data, the final pointer is returned.
* `NULL` is returned upon failure, the otherwise returned pointer is not guaranteed to be valid. */ * `NULL` is returned upon failure, the otherwise returned pointer is not guaranteed to be valid. */
const void *nbt_nexttag(const void *restrict buf) NONNULL((1)) PURE; const u8 *nbt_nexttag(const u8 *restrict buf) NONNULL((1)) PURE;