From 6d4aeb4120230899589326b5fd87afca58654c05 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 3 Feb 2026 08:03:57 +0100 Subject: feat(assets): Enforce 16-byte alignment and string safety Updates asset_packer to align static asset arrays to 16 bytes and append a null-terminator. This allows assets to be safely reinterpreted as typed pointers (e.g., float*, const char*) without copying. Updates AssetManager documentation to reflect these guarantees. --- src/util/asset_manager.h | 4 ++++ tools/asset_packer.cc | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h index b9cd778..062cd0f 100644 --- a/src/util/asset_manager.h +++ b/src/util/asset_manager.h @@ -25,5 +25,9 @@ struct AssetRecord { }; // Generic interface +// Retrieves a pointer to the asset data. +// - Static assets are guaranteed to be 16-byte aligned. +// - Static assets are guaranteed to be null-terminated (safe as C-strings). +// - 'out_size' returns the original asset size (excluding the null terminator). const uint8_t* GetAsset(AssetId asset_id, size_t* out_size = nullptr); void DropAsset(AssetId asset_id, const uint8_t* asset); diff --git a/tools/asset_packer.cc b/tools/asset_packer.cc index 2b90ecc..d86e29b 100644 --- a/tools/asset_packer.cc +++ b/tools/asset_packer.cc @@ -210,7 +210,12 @@ int main(int argc, char* argv[]) { } std::vector buffer((std::istreambuf_iterator(asset_file)), std::istreambuf_iterator()); - fprintf(assets_data_cc_file, "static const uint8_t %s[] = {\n ", + size_t original_size = buffer.size(); + buffer.push_back(0); // Null terminator for safety + + fprintf(assets_data_cc_file, "const size_t ASSET_SIZE_%s = %zu;\n", + info.name.c_str(), original_size); + fprintf(assets_data_cc_file, "alignas(16) static const uint8_t %s[] = {\n ", info.data_array_name.c_str()); for (size_t i = 0; i < buffer.size(); ++i) { if (i > 0 && i % 12 == 0) @@ -241,8 +246,8 @@ int main(int argc, char* argv[]) { info.func_name_str_name.c_str(), info.params_array_name.c_str(), info.proc_params.size()); } else { - fprintf(assets_data_cc_file, "%s, sizeof(%s), false, nullptr, nullptr, 0", - info.data_array_name.c_str(), info.data_array_name.c_str()); + fprintf(assets_data_cc_file, "%s, ASSET_SIZE_%s, false, nullptr, nullptr, 0", + info.data_array_name.c_str(), info.name.c_str()); } fprintf(assets_data_cc_file, " },\n"); } -- cgit v1.2.3