From 4574c223f2d790e57dbd262d8f40c30d790ae88b Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Thu, 30 Jun 2022 18:43:01 +0200 Subject: [PATCH] Clean up mem allocation alignment macro usage + small refactoring of the typehash dependencies search --- src_features/signMessageEIP712/context.c | 2 +- src_features/signMessageEIP712/field_hash.c | 2 +- src_features/signMessageEIP712/mem_utils.h | 2 +- src_features/signMessageEIP712/path.c | 4 +-- src_features/signMessageEIP712/type_hash.c | 37 +++++++++++---------- src_features/signMessageEIP712/ui_logic.c | 2 +- 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src_features/signMessageEIP712/context.c b/src_features/signMessageEIP712/context.c index 1e64090..ce56704 100644 --- a/src_features/signMessageEIP712/context.c +++ b/src_features/signMessageEIP712/context.c @@ -22,7 +22,7 @@ bool eip712_context_init(void) // init global variables mem_init(); - if ((eip712_context = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*eip712_context), *eip712_context)) == NULL) + if ((eip712_context = MEM_ALLOC_AND_ALIGN_TYPE(*eip712_context)) == NULL) { return false; } diff --git a/src_features/signMessageEIP712/field_hash.c b/src_features/signMessageEIP712/field_hash.c index 20398d4..3289ce3 100644 --- a/src_features/signMessageEIP712/field_hash.c +++ b/src_features/signMessageEIP712/field_hash.c @@ -19,7 +19,7 @@ bool field_hash_init(void) { if (fh == NULL) { - if ((fh = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*fh), *fh)) == NULL) + if ((fh = MEM_ALLOC_AND_ALIGN_TYPE(*fh)) == NULL) { return false; } diff --git a/src_features/signMessageEIP712/mem_utils.h b/src_features/signMessageEIP712/mem_utils.h index f339a92..3064f9b 100644 --- a/src_features/signMessageEIP712/mem_utils.h +++ b/src_features/signMessageEIP712/mem_utils.h @@ -6,7 +6,7 @@ #include #include -#define MEM_ALLOC_AND_ALIGN_TO_TYPE(size, type) (mem_alloc_and_align(size, __alignof__(type))) +#define MEM_ALLOC_AND_ALIGN_TYPE(type) mem_alloc_and_align(sizeof(type), __alignof__(type)) char *mem_alloc_and_copy_char(char c); void *mem_alloc_and_copy(const void *data, size_t size); diff --git a/src_features/signMessageEIP712/path.c b/src_features/signMessageEIP712/path.c index 2639589..4be0cb3 100644 --- a/src_features/signMessageEIP712/path.c +++ b/src_features/signMessageEIP712/path.c @@ -359,7 +359,7 @@ bool path_set_root(const char *const struct_name, uint8_t name_length) // TODO: Move elsewhere cx_sha3_t *hash_ctx; const uint8_t *thash_ptr; - if ((hash_ctx = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*hash_ctx), *hash_ctx)) == NULL) + if ((hash_ctx = MEM_ALLOC_AND_ALIGN_TYPE(*hash_ctx)) == NULL) { return false; } @@ -638,7 +638,7 @@ bool path_init(void) { if (path_struct == NULL) { - path_struct = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*path_struct), *path_struct); + path_struct = MEM_ALLOC_AND_ALIGN_TYPE(*path_struct); } return path_struct != NULL; } diff --git a/src_features/signMessageEIP712/type_hash.c b/src_features/signMessageEIP712/type_hash.c index b234c66..fe7ca08 100644 --- a/src_features/signMessageEIP712/type_hash.c +++ b/src_features/signMessageEIP712/type_hash.c @@ -85,10 +85,10 @@ static bool encode_and_hash_type(const void *const struct_ptr) * @param[in,out] deps pointer to the first dependency pointer */ static void sort_dependencies(uint8_t deps_count, - void **deps) + const void **deps) { bool changed; - void *tmp_ptr; + const void *tmp_ptr; const char *name1, *name2; uint8_t namelen1, namelen2; int str_cmp_result; @@ -120,13 +120,13 @@ static void sort_dependencies(uint8_t deps_count, * * @param[in] structs_array pointer to structs array * @param[out] deps_count count of how many struct dependencie pointers - * @param[in] deps pointer to the first dependency pointer + * @param[in] first_dep pointer to the first dependency pointer * @param[in] struct_ptr pointer to the struct we are getting the dependencies of * @return \ref false in case of a memory allocation error, \ref true otherwise */ -static bool get_struct_dependencies(const void *const structs_array, +static const void **get_struct_dependencies(const void *const structs_array, uint8_t *const deps_count, - void *const *const deps, + const void **first_dep, const void *const struct_ptr) { uint8_t fields_count; @@ -151,7 +151,7 @@ static bool get_struct_dependencies(const void *const structs_array, for (dep_idx = 0; dep_idx < *deps_count; ++dep_idx) { // it's a match! - if (*(deps + dep_idx) == arg_struct_ptr) + if (*(first_dep + dep_idx) == arg_struct_ptr) { break; } @@ -159,18 +159,23 @@ static bool get_struct_dependencies(const void *const structs_array, // if it's not present in the array, add it and recurse into it if (dep_idx == *deps_count) { - if ((new_dep = mem_alloc(sizeof(void*))) == NULL) + *deps_count += 1; + if ((new_dep = MEM_ALLOC_AND_ALIGN_TYPE(void*)) == NULL) { - return false; + return NULL; + } + if (*deps_count == 1) + { + first_dep = new_dep; } *new_dep = arg_struct_ptr; - *deps_count += 1; - get_struct_dependencies(structs_array, deps_count, deps, arg_struct_ptr); + // TODO: Move away from recursive calls + get_struct_dependencies(structs_array, deps_count, first_dep, arg_struct_ptr); } } field_ptr = get_next_struct_field(field_ptr); } - return true; + return first_dep; } /** @@ -190,17 +195,13 @@ const uint8_t *type_hash(const void *const structs_array, struct_name, struct_name_length); uint8_t deps_count = 0; - void **deps; + const void **deps; uint8_t *hash_ptr; void *mem_loc_bak = mem_alloc(0); cx_keccak_init(&global_sha3, 256); // init hash - // get list of structs (own + dependencies), properly ordered - if ((deps = MEM_ALLOC_AND_ALIGN_TO_TYPE(0, *deps)) == NULL) - { - return NULL; - } - if (get_struct_dependencies(structs_array, &deps_count, deps, struct_ptr) == false) + deps = get_struct_dependencies(structs_array, &deps_count, NULL, struct_ptr); + if ((deps_count > 0) && (deps == NULL)) { return NULL; } diff --git a/src_features/signMessageEIP712/ui_logic.c b/src_features/signMessageEIP712/ui_logic.c index a260e31..eef3aeb 100644 --- a/src_features/signMessageEIP712/ui_logic.c +++ b/src_features/signMessageEIP712/ui_logic.c @@ -352,7 +352,7 @@ void ui_712_end_sign(void) */ bool ui_712_init(void) { - if ((ui_ctx = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*ui_ctx), *ui_ctx))) + if ((ui_ctx = MEM_ALLOC_AND_ALIGN_TYPE(*ui_ctx))) { ui_ctx->shown = false; ui_ctx->end_reached = false;