From 3e750e8419ff2ad1a7eb46d6d7a2a2b88d2d7be4 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Thu, 1 Dec 2022 14:22:23 +0100 Subject: [PATCH 1/2] Fix device crash caused by improper memory alignment of the plugin context buffer --- src/shared_context.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/shared_context.h b/src/shared_context.h index 54c7566..414586c 100644 --- a/src/shared_context.h +++ b/src/shared_context.h @@ -65,7 +65,9 @@ typedef struct tokenContext_t { uint8_t contractAddress[ADDRESS_LENGTH]; uint8_t methodSelector[SELECTOR_LENGTH]; }; - uint8_t pluginContext[5 * INT256_LENGTH]; + // This needs to be strictly 4 bytes aligned since pointers to it will be casted as + // plugin context struct pointers (structs that contain up to 4 bytes wide elements) + uint8_t pluginContext[5 * INT256_LENGTH] __attribute__((aligned(4))); }; #ifdef HAVE_STARKWARE @@ -77,6 +79,8 @@ typedef struct tokenContext_t { } tokenContext_t; +_Static_assert((offsetof(tokenContext_t, pluginContext) % 4) == 0, "Plugin context not aligned"); + typedef struct publicKeyContext_t { cx_ecfp_public_key_t publicKey; char address[41]; From 0d09d31dba737bf90bee00ae8e47d4e7f2169d6a Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Thu, 1 Dec 2022 15:07:37 +0100 Subject: [PATCH 2/2] Small reordering to save 4 bytes on tokenContext_t struct padding --- src/shared_context.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/shared_context.h b/src/shared_context.h index 414586c..7d3d86b 100644 --- a/src/shared_context.h +++ b/src/shared_context.h @@ -50,7 +50,6 @@ typedef enum starkQuantumType_e { typedef struct tokenContext_t { char pluginName[PLUGIN_ID_LENGTH]; - uint8_t pluginStatus; uint8_t data[INT256_LENGTH]; uint16_t fieldIndex; @@ -70,6 +69,8 @@ typedef struct tokenContext_t { uint8_t pluginContext[5 * INT256_LENGTH] __attribute__((aligned(4))); }; + uint8_t pluginStatus; + #ifdef HAVE_STARKWARE uint8_t quantum[32]; uint8_t mintingBlob[32];