Skip to content

Commit 20a90e8

Browse files
committed
remove unsigned parser
1 parent 8b26c92 commit 20a90e8

50 files changed

Lines changed: 204 additions & 158 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/src/common/actions.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
#include <stdint.h>
2020

2121
#include "apdu_codes.h"
22+
#include "borsh.h"
2223
#include "coin.h"
24+
#include "common_txdef.h"
2325
#include "crypto.h"
2426
#include "tx.h"
2527
#include "zxerror.h"
2628

27-
#include "common_txdef.h"
28-
#include "borsh.h"
29-
3029
extern uint16_t action_addrResponseLen;
3130

3231
__Z_INLINE zxerr_t app_fill_address() {

app/src/common/tx.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "zxmacros.h"
2525

2626
#if defined(TARGET_NANOX) || defined(TARGET_NANOS2) || defined(TARGET_STAX) || defined(TARGET_FLEX)
27-
#define RAM_BUFFER_SIZE 8192
27+
#define RAM_BUFFER_SIZE 4096
2828
#define FLASH_BUFFER_SIZE 16384
2929
#elif defined(TARGET_NANOS)
3030
#define RAM_BUFFER_SIZE 256
@@ -116,4 +116,3 @@ zxerr_t tx_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *ou
116116
const uint8_t *get_txn_raw() { return tx_obj.unsigned_transaction_raw.buffer.ptr; }
117117

118118
uint16_t get_txn_len() { return tx_obj.unsigned_transaction_raw.buffer.len; }
119-

app/src/parser_impl.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616

1717
#include "parser_impl.h"
1818

19+
#include "borsh.h"
20+
#include "schema_display.h"
1921
#include "schema_reader.h"
2022
#include "unsigned_transaction_reader.h"
2123
#include "zxerror.h"
22-
#include "borsh.h"
2324

2425
parser_error_t _read(parser_context_t *c, parser_tx_t *v) {
25-
CHECK_ERROR(merkle_proofs_read(c, v));
26+
CHECK_ERROR(schema_merkle_proofs_read(c, v));
2627
CHECK_ERROR(schema_extra_data_read(c, v));
27-
CHECK_ERROR(unsigned_transaction_read(c, v));
28+
CHECK_ERROR(schema_parser_transaction(c, v));
2829
CHECK_ERROR(schema_chain_hash_read(c, v));
2930

30-
// TODO: check that we have consumxed all data
3131
if (c->offset != c->buffer.len) {
3232
print_string("Failed to parse unsigned transaction\n");
3333
return parser_unexpected_error;

app/src/schema_display.c

Lines changed: 76 additions & 57 deletions
Large diffs are not rendered by default.

app/src/schema_display.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ extern "C" {
2222

2323
#include "parser_common.h"
2424

25-
parser_error_t schema_display_generic_by_index(parser_tx_t *txObj, uint32_t start_index);
26-
parser_error_t schema_display_by_primitive(parser_tx_t *txObj, primitive_t *primitive);
27-
parser_error_t schema_display(parser_tx_t *txObj, uint32_t start_index);
25+
parser_error_t schema_display_generic_by_index(parser_context_t *ctx, parser_tx_t *txObj, uint32_t start_index);
26+
parser_error_t schema_display_by_primitive(parser_context_t *ctx, parser_tx_t *txObj, primitive_t *primitive);
27+
parser_error_t schema_parser_transaction(parser_context_t *ctx, parser_tx_t *txObj);
2828

2929
#ifdef __cplusplus
3030
}

app/src/schema_helper.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ parser_error_t get_schema_type(parser_tx_t *txObj, uint32_t index, uint8_t *type
101101
return parser_ok;
102102
}
103103

104+
parser_error_t schema_get_unsigned_transaction_index(parser_tx_t *txObj, uint64_t *root_index) {
105+
CHECK_INPUT(txObj);
106+
107+
// Get root index
108+
if (txObj->schema.root_type_indices.qty <= ROLLUP_ROOTS_UNSIGNED_TRANSACTION) {
109+
return parser_root_type_indices_overflow;
110+
}
111+
112+
for (uint8_t i = 0; i <= (uint16_t)ROLLUP_ROOTS_UNSIGNED_TRANSACTION; i++) {
113+
CHECK_ERROR(read_u64(&txObj->schema.root_type_indices.indices, root_index));
114+
}
115+
txObj->schema.root_type_indices.indices.offset = 0;
116+
117+
return parser_ok;
118+
}
119+
104120
bool is_link_skip(link_t *link) {
105121
if (link->tag == LINK_IMMEDIATE && link->data.immediate.type == PRIMITIVE_SKIP) {
106122
return true;

app/src/schema_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ bool schema_find_index(uint64_t index_leaf, merkle_leaves_indices_t *indices, ui
2626
parser_error_t schema_move_leaf_offset(merkle_leaves_data_t *leaves, uint64_t index);
2727
parser_error_t schema_reset_leaf_offset(merkle_leaves_data_t *leaves);
2828
parser_error_t get_schema_type(parser_tx_t *txObj, uint32_t index, uint8_t *type);
29+
parser_error_t schema_get_unsigned_transaction_index(parser_tx_t *txObj, uint64_t *root_index);
2930
bool is_link_skip(link_t *link);
3031

3132
#ifdef __cplusplus

app/src/schema_reader.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "crypto_helper.h"
2121
#include "schema_helper.h"
2222
#include "schema_proof.h"
23+
#include "stack_manager.h"
2324

2425
parser_error_t read_fixed_point_display(parser_context_t *ctx, fixed_point_display_t *display) {
2526
CHECK_INPUT(display);
@@ -393,6 +394,8 @@ parser_error_t read_enum(parser_context_t *ctx, schema_enum_t *schema_enum) {
393394
print_buffer(&schema_enum->type_name, "type_name");
394395
print_buffer_str(&schema_enum->type_name, "type_name_str");
395396

397+
checkStack();
398+
396399
// read variants_qty
397400
CHECK_ERROR(read_u32(ctx, &schema_enum->variants_qty));
398401
print_u32("variants_qty:", schema_enum->variants_qty);
@@ -702,6 +705,7 @@ parser_error_t read_schema_type(parser_context_t *ctx, uint8_t type) {
702705
case LINKING_SCHEME_ENUM:
703706
print_string("READING ENUM");
704707
schema_enum_t enum_type = {0};
708+
checkStack();
705709
CHECK_ERROR(read_enum(ctx, &enum_type));
706710
print_string("READING ENUM DONE\n");
707711
break;
@@ -859,10 +863,12 @@ parser_error_t compute_chain_hash(parser_tx_t *txObj) {
859863
}
860864

861865
// | borsh(leaves_data) | borsh(indices_leaves) | borsh(lemmas) | borsh(tree_size) | borsh(root_hash) | borsh(chain_hash)
862-
parser_error_t merkle_proofs_read(parser_context_t *ctx, parser_tx_t *txObj) {
866+
parser_error_t schema_merkle_proofs_read(parser_context_t *ctx, parser_tx_t *txObj) {
863867
CHECK_INPUT(ctx);
864868
CHECK_INPUT(txObj);
865869

870+
checkStack();
871+
866872
// read leaf data
867873
CHECK_ERROR(read_u32(ctx, &txObj->merkle_proofs.leaves.entries));
868874
print_u32("leaves.qty:", txObj->merkle_proofs.leaves.entries);
@@ -873,10 +879,12 @@ parser_error_t merkle_proofs_read(parser_context_t *ctx, parser_tx_t *txObj) {
873879
CHECK_ERROR(read_u32(ctx, &length));
874880
print_u32("length:", length);
875881
uint8_t schema_type = 0;
882+
checkStack();
876883
CHECK_ERROR(read_u8(ctx, (uint8_t *)&schema_type));
877884
print_u8("schema.type:", schema_type);
878885
CHECK_ERROR(read_schema_type(ctx, schema_type));
879886
}
887+
return parser_unexpected_error;
880888
txObj->merkle_proofs.leaves.data.buffer.ptr = ptr_mem;
881889
txObj->merkle_proofs.leaves.data.buffer.len = ctx->offset - offset_mem;
882890
print_buffer(&txObj->merkle_proofs.leaves.data.buffer, "leaves data");

app/src/schema_reader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ parser_error_t read_name_registry(parser_context_t *ctx, name_registry_t *name_r
3333
parser_error_t read_named_field(parser_context_t *ctx, named_field_t *field);
3434
parser_error_t read_unnamed_field(parser_context_t *ctx, unnamed_field_t *field);
3535
parser_error_t read_registry(parser_context_t *ctx, registry_t *registry);
36+
parser_error_t get_schema_unsigned_transaction_index(parser_tx_t *txObj, uint64_t *root_index);
3637

3738
parser_error_t metadata_read(parser_context_t *ctx, parser_tx_t *txObj);
38-
parser_error_t merkle_proofs_read(parser_context_t *ctx, parser_tx_t *txObj);
39+
parser_error_t schema_merkle_proofs_read(parser_context_t *ctx, parser_tx_t *txObj);
3940
parser_error_t schema_extra_data_read(parser_context_t *ctx, parser_tx_t *txObj);
4041
parser_error_t schema_chain_hash_read(parser_context_t *ctx, parser_tx_t *txObj);
4142

app/src/stack_manager.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*******************************************************************************
2+
* (c) 2018 - 2024 Zondax AG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
********************************************************************************/
16+
17+
#include <stdbool.h>
18+
#include <stdint.h>
19+
20+
#include "parser_common.h"
21+
#include "zxmacros.h"
22+
23+
#if defined(LEDGER_SPECIFIC)
24+
#define STACK_SHIFT 0x20
25+
#define MINIMUM_STACK 0x400
26+
#else
27+
static int16_t recursionDepthCounter = 0;
28+
#define MAX_RECURSION_DEPTH 50
29+
#endif
30+
31+
/**
32+
* @brief Checks the available stack space to prevent stack overflow.
33+
*
34+
* @return parser_error_t Returns parser_running_out_of_stack if stack space is insufficient, otherwise parser_ok.
35+
*/
36+
parser_error_t checkStack() {
37+
#if defined(LEDGER_SPECIFIC)
38+
// NOLINTNEXTLINE(readability-identifier-length): here `p` is fine
39+
void *p = NULL;
40+
const uint32_t availableStack = (uint32_t)((void *)&p) + STACK_SHIFT - (uint32_t)&app_stack_canary;
41+
ZEMU_LOGF(50, "Check: available stack: %d\n", availableStack)
42+
if (availableStack <= MINIMUM_STACK) {
43+
return parser_running_out_of_stack;
44+
}
45+
#else
46+
if (recursionDepthCounter >= MAX_RECURSION_DEPTH) {
47+
return parser_running_out_of_stack;
48+
}
49+
recursionDepthCounter++;
50+
#endif
51+
return parser_ok;
52+
}
53+
54+
/**
55+
* @brief Frees the stack space by decrementing the recursion depth counter.
56+
*
57+
* @return parser_error_t Always returns parser_ok.
58+
*/
59+
parser_error_t freeStack(uint8_t depth) {
60+
#if !defined(LEDGER_SPECIFIC)
61+
if (recursionDepthCounter > 0) {
62+
recursionDepthCounter -= depth;
63+
}
64+
#else
65+
(void)depth;
66+
void *p = NULL;
67+
const uint32_t availableStack = (uint32_t)((void *)&p) - (uint32_t)&app_stack_canary;
68+
ZEMU_LOGF(50, "Free: available stack: %d\n", availableStack)
69+
if (availableStack <= MINIMUM_STACK) {
70+
return parser_running_out_of_stack;
71+
}
72+
#endif
73+
return parser_ok;
74+
}

0 commit comments

Comments
 (0)