Compare commits

...

5 Commits

Author SHA1 Message Date
077d3a147a add comments to point at where strict aliasing occurs 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-03 22:39:15 +02:00
c1dc1b7856 Use void * over u8 *, to be more explicit we're working with raw data, and not just bytes.
The main problem with all my buffer-parsing code so-far is that they
often... always break strict aliasing rules.
Using a `void *` will make these bugs more explicit / noticable.
2025-09-03 22:38:37 +02:00
ad9b056dfa delete redundant COLOUR32 definitions 2025-09-01 16:26:38 +02:00
a939e3e0f0 add quit in main.c to atexit, for added safety 2025-09-01 16:22:45 +02:00
e02e2a13f3 write a portable version of endian.h, to replace the system's endian.h.
utilising GNU C standard library for some of the functionality, or GNU C
extensions.
2025-08-29 11:48:06 +02:00
8 changed files with 100 additions and 78 deletions

View File

@@ -1,9 +1,45 @@
/* Copyright (c) 2025 Quinn
* Licensed under the MIT Licence. See LICENSE for details */
#pragma once
#if __has_include(<endian.h>)
#include <endian.h>
#ifndef PORTABLE_ENDIAN_H
#define PORTABLE_ENDIAN_H
#if defined(__GNUC__)
/* test for the byteswap header */
#if __has_include(<byteswap.h>)
#include <byteswap.h>
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define le16toh(x) (x)
#define le32toh(x) (x)
#define le64toh(x) (x)
#define htole16(x) (x)
#define htole32(x) (x)
#define htole64(x) (x)
#define be16toh(x) __bswap16(x)
#define be32toh(x) __bswap32(x)
#define be64toh(x) __bswap64(x)
#define htobe16(x) __bswap16(x)
#define htobe32(x) __bswap32(x)
#define htobe64(x) __bswap64(x)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define le16toh(x) __bswap16(x)
#define le32toh(x) __bswap32(x)
#define le64toh(x) __bswap64(x)
#define htole16(x) __bswap16(x)
#define htole32(x) __bswap32(x)
#define htole64(x) __bswap64(x)
#define be16toh(x) (x)
#define be32toh(x) (x)
#define be64toh(x) (x)
#define htobe16(x) (x)
#define htobe32(x) (x)
#define htobe64(x) (x)
#else
#error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc)
#endif /* byte order */
/* otherwise, utilise the compiler built-ins */
#else
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define le16toh(x) __uint16_identity(x)
@@ -33,5 +69,12 @@
#define htobe64(x) __uint64_identity(x)
#else
#error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc)
#endif
#endif
#endif /* byte order */
#endif /* has byteswap.h */
#else
#error GNU C is unavailable
#endif /* __GNUC__ */
#endif /* PORTABLE_ENDIAN_H */

View File

@@ -14,7 +14,7 @@ MARCH ?= $(shell uname -m)
KERNEL ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
# compilation flags
CFLAGS += -c -std=gnu99 -Wall -Wextra -Wpedantic -MMD -MP
CFLAGS += -c -std=gnu99 -Wall -Wextra -Wpedantic -MMD -MP -Wno-pointer-arith
LDFLAGS += -flto
# architecture/OS detection
@@ -51,7 +51,7 @@ PROF = rel
CFLAGS += -DNDEBUG -O2
endif
CFLAGS += $(shell pkg-config --cflags glfw3 libarchive) -Ilib/glad/include
CFLAGS += $(shell pkg-config --cflags glfw3 libarchive) -Ilib/include -Ilib/glad/include
LDFLAGS += $(shell pkg-config --libs glfw3 libarchive) -lm
# get source files

View File

@@ -3,12 +3,12 @@
#include "mcx.h"
#include <assert.h>
#include <endian.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../util/compat/endian.h"
#include "../util/intdef.h"
#define TABLE 0x2000 // table byte size
@@ -16,10 +16,10 @@
#define CHUNKS 0x400 // amount of chunks in a file
/* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */
static void mvchunks(u8 *restrict buf, u8 *src, u8 *dst, int src_s, int src_e) {
static void mvchunks(u8 *restrict buf, void *src, void *dst, int src_s, int src_e) {
assert(src > dst);
u32 *table = (u32 *)buf;
size_t len = src - dst; // acquire the amount of bytes that we shall move
u32 *table = (u32 *)buf; // BUG: strict aliasing
size_t len = src - dst; // acquire the amount of bytes that we shall move
assert(!(len % SECTOR));
// count how many bytes we need to move, whilst updating location data
@@ -34,9 +34,9 @@ static void mvchunks(u8 *restrict buf, u8 *src, u8 *dst, int src_s, int src_e) {
/* 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.
* Returns the bytes removed by this function. */
static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
static size_t delchunk(void *restrict buf, size_t rmb, int sidx, int eidx) {
// load the table data
u32 *table = (u32 *)buf;
u32 *table = (u32 *)buf; // BUG: strict aliasing
size_t slen, bidx, blen;
slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
bidx = (be32toh(table[sidx]) >> 8) * SECTOR; // acquire and compute the byte offset the chunk starts at
@@ -47,8 +47,8 @@ static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
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
u8 *dst = buf + bidx - rmb;
u8 *src = buf + bidx + blen;
void *dst = buf + bidx - rmb;
void *src = buf + bidx + blen;
mvchunks(buf, src, dst, sidx, eidx - 1);
return blen;
}
@@ -56,22 +56,22 @@ static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
/* Just call `delchunk` with the parameters and some defaults.
* This is done instead of `delchunk` being globally linked, because
* `delchunk` requests more specific parameters, which is confusing outside this module. */
size_t mcx_delchunk(u8 *restrict buf, int chunk) {
size_t mcx_delchunk(void *restrict buf, int chunk) {
return delchunk(buf, 0, chunk, CHUNKS);
}
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) {
size_t mcx_delchunk_range(void *restrict buf, int start, int end) {
assert(start < end && end < CHUNKS);
u32 *table = (u32 *)buf;
u8 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR;
u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
u32 *table = (u32 *)buf; // BUG: strict aliasing
void *dst = buf + (be32toh(table[start]) >> 8) * SECTOR;
void *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
src += (be32toh(table[end]) & 0xFF) * SECTOR;
// zeroes-out the chunk data within this range. (and set the timestamp)
u32 ts = htobe32(time(NULL));
for (int i = start; i <= end; i++) {
table[i] = 0;
table[i + CHUNKS] = ts;
table[i] = 0; // BUG: strict aliasing
table[i + CHUNKS] = ts; // BUG: strict aliasing
}
// move the remaining chunks down
@@ -89,7 +89,7 @@ static int cmp_chunkids(const void *restrict x, const void *restrict y) {
/* 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. */
size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) {
size_t mcx_delchunk_bulk(void *restrict buf, const u16 *restrict chunks, int chunkc) {
// ensure the chunks ids we're working on are sorted from least to greatest
u16 chunkids[chunkc + 1];
memcpy(chunkids, chunks, chunkc);
@@ -104,9 +104,9 @@ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunk
/* 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. */
size_t mcx_calcsize(const u8 *restrict buf) {
size_t mcx_calcsize(const void *restrict buf) {
size_t size = 0;
for (uint i = 0; i < CHUNKS; i++)
size += *(buf + (i * 4) + 3);
size += *(u8 *)(buf + (i * 4) + 3);
return (size * CHUNKS) + TABLE;
}

View File

@@ -18,20 +18,20 @@ struct mcx_chunk {
* The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed. */
size_t mcx_delchunk(u8 *restrict buf, int chunk) NONNULL((1));
size_t mcx_delchunk(void *restrict buf, int chunk) NONNULL((1));
/* 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.
* All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) NONNULL((1));
size_t mcx_delchunk_range(void *restrict buf, int start, int end) NONNULL((1));
/* 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.
* The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */
size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2));
size_t mcx_delchunk_bulk(void *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2));
/* Computes the byte size of the `*.mcX` file in `buf` and returns it. */
size_t mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE;
size_t mcx_calcsize(const void *restrict buf) NONNULL((1)) PURE;

View File

@@ -3,11 +3,11 @@
#include "nbt.h"
#include <assert.h>
#include <endian.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "../util/compat/endian.h"
#include "../util/intdef.h"
#define MAX_DEPTH 512
@@ -15,7 +15,7 @@
/* 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
* Outputs the allocated data to `out`, returns where the next pointer would be. */
static const u8 *procarr(const u8 *restrict buf, i32 nmem, uint size, struct nbt_array *restrict *restrict out) {
static const void *procarr(const void *restrict buf, i32 nmem, uint size, struct nbt_array *restrict *restrict out) {
size_t len = nmem * size;
*out = malloc(sizeof(struct nbt_array) + len);
if (!*out) return buf + len;
@@ -30,6 +30,7 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmem, uint size, struct nbt
if (size == 1) return buf;
size_t i = 0;
while (i < len) {
// BUG: strict aliasing
switch (size) {
case 2: *(u16 *)((*out)->dat + i) = be16toh(*(u16 *)((*out)->dat + i)); break;
case 4: *(u32 *)((*out)->dat + i) = be32toh(*(u32 *)((*out)->dat + i)); break;
@@ -43,12 +44,12 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmem, uint size, struct nbt
}
/* calls `procarr` for the simple types available. */
static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict *restrict out) {
static const void *proclist(const void *restrict buf, struct nbt_array *restrict *restrict out) {
uint size;
*out = NULL;
switch (*buf) {
switch (*(u8 *)buf) {
case NBT_I8: size = 1; break;
case NBT_I16: size = 2; break;
case NBT_I32: // fall through
@@ -59,19 +60,20 @@ static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict *re
}
buf++;
i32 len = (i32)be32toh(*(u32 *)buf);
i32 len = (i32)be32toh(*(u32 *)buf); // BUG: strict aliasing
buf += 4;
return procarr(buf, len, size, out);
}
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
const void *nbt_proctag(const void *restrict buf, u16 slen, void *restrict out) {
const u8 *ptr, *tmp;
ptr = buf + 3 + slen;
i32 nmem;
uint size;
switch (*buf) {
// BUG: strict aliasing
switch (*(u8 *)buf) {
case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
case NBT_I16: *(u16 *)out = be16toh(*(u16 *)ptr); return ptr + 2;
case NBT_I32: // fall through
@@ -99,10 +101,11 @@ const u8 *nbt_proctag(const u8 *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.
* 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`. */
static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
const u8 *tag = ptr;
static const void *nexttag_list(const void *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
const void *tag = ptr;
ptr++;
switch (*tag) {
// BUG: strict aliasing
switch (*(u8 *)tag) {
case NBT_END: break;
case NBT_I8: ptr += (i32)be32toh(*(u32 *)ptr) * 1; break;
case NBT_I16: ptr += (i32)be32toh(*(u32 *)ptr) * 2; break;
@@ -113,8 +116,8 @@ static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt,
default:
// TODO: handle out of bounds... Might not be required if we use flexible array member
(*dpt)++;
tags[*dpt] = *tag;
lens[*dpt] = (i32)be32toh(*(u32 *)ptr);
tags[*dpt] = *(u8 *)tag;
lens[*dpt] = (i32)be32toh(*(u32 *)ptr); // BUG: strict aliasing
break;
}
ptr += 4;
@@ -127,18 +130,19 @@ static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt,
* - `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`.
* - `tags` shall contain `MAX_DEPTH` of items representing the list's stored type. */
static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
static const void *nexttag(const void *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) {
u8 type;
const u8 *ptr = tag;
const void *ptr = tag;
if (lens[*dpt]) {
type = tags[*dpt];
lens[*dpt]--;
*dpt -= !lens[*dpt];
} else {
type = *tag;
ptr += be16toh(*(u16 *)(tag + 1)) + 3;
type = *(u8 *)tag;
ptr += be16toh(*(u16 *)(tag + 1)) + 3; // BUG: strict aliasing
}
// BUG: strict aliasing
switch (type) {
case NBT_I8: ptr += 1; break;
case NBT_I16: ptr += 2; break;
@@ -169,8 +173,8 @@ static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *
* - compound:list:int32
* - string
*/
const u8 *nbt_nexttag(const u8 *restrict buf) {
const u8 *tag;
const void *nbt_nexttag(const void *restrict buf) {
const void *tag;
u8 tags[MAX_DEPTH] = {0};
i32 lens[MAX_DEPTH] = {0};
uint dpt = 0;

View File

@@ -3,11 +3,11 @@
#pragma once
#include <assert.h>
#include <endian.h>
#include <stdbool.h>
#include <stdlib.h>
#include "../util/atrb.h"
#include "../util/compat/endian.h"
#include "../util/intdef.h"
/* NBT (named binary tag) is a tree data structure. Tags have a numeric type ID, name and a payload.
@@ -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
* 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. */
const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) NONNULL((1, 3));
const void *nbt_proctag(const void *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.
* `NULL` is returned upon failure, the otherwise returned pointer is not guaranteed to be valid. */
const u8 *nbt_nexttag(const u8 *restrict buf) NONNULL((1)) PURE;
const void *nbt_nexttag(const void *restrict buf) NONNULL((1)) PURE;

View File

@@ -31,13 +31,15 @@ static inline int init(void) {
return 0;
}
static inline void quit(void) {
static void quit(void) {
glfwTerminate();
}
int main(int argc, char **argv) {
(void)argc, (void)argv;
printf("debug: [DBG], info: [INF], warning: [WAR], error: [ERR], fatal: [FAT]\n");
atexit(quit);
if (init()) fatal("failed to initialize!");
window_loop();

View File

@@ -1,27 +0,0 @@
/* Copyright (c) 2025 Quinn
* Licensed under the MIT Licence. See LICENSE for details */
#pragma once
#include <stdint.h>
#include "vec.h"
#define COLOUR32_BLACK ((u8vec4){0x00, 0x00, 0x00, 0xFF})
#define COLOUR32_RED ((u8vec4){0xFF, 0x00, 0x00, 0xFF})
#define COLOUR32_YELLOW ((u8vec4){0xFF, 0xFF, 0x00, 0xFF})
#define COLOUR32_ORANGE ((u8vec4){0xFF, 0x6D, 0x00, 0xFF})
#define COLOUR32_GREEN ((u8vec4){0x00, 0xFF, 0x00, 0xFF})
#define COLOUR32_CYAN ((u8vec4){0x00, 0xFF, 0xFF, 0xFF})
#define COLOUR32_BLUE ((u8vec4){0x00, 0x00, 0xFF, 0xFF})
#define COLOUR32_MAGENTA ((u8vec4){0xFF, 0x00, 0xFF, 0xFF})
#define COLOUR32_WHITE ((u8vec4){0xFF, 0xFF, 0xFF, 0xFF})
// american macros:
#define COLOR32_BLACK COLOUR32_BLACK
#define COLOR32_RED COLOUR32_RED
#define COLOR32_YELLOW COLOUR32_YELLOW
#define COLOR32_ORANGE COLOUR32_ORANGE
#define COLOR32_GREEN COLOUR32_GREEN
#define COLOR32_CYAN COLOUR32_CYAN
#define COLOR32_BLUE COLOUR32_BLUE
#define COLOR32_MAGENTA COLOUR32_MAGENTA
#define COLOR32_WHITE COLOUR32_WHITE