Skip to content

Commit 100a0e3

Browse files
committed
Update harnesses
1 parent 6bbfd6d commit 100a0e3

File tree

3 files changed

+61
-53
lines changed

3 files changed

+61
-53
lines changed

fuzzing/extra/TxParser.cmake

Lines changed: 0 additions & 31 deletions
This file was deleted.

fuzzing/fuzz_dispatcher.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <sys/types.h>
5+
#include "globals.h"
6+
#include "dispatcher.h"
7+
8+
#include <setjmp.h>
9+
10+
global_ctx_t G_context;
11+
const internal_storage_t N_storage_real;
12+
13+
jmp_buf fuzz_exit_jump_buf;
14+
15+
// Fuzz entry point
16+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
17+
if (setjmp(fuzz_exit_jump_buf) == 0 && size > 6) {
18+
command_t cmd;
19+
cmd.cla = data[0];
20+
cmd.ins = data[1] % 8;
21+
cmd.p1 = data[2];
22+
cmd.p2 = data[3];
23+
cmd.lc = data[4];
24+
25+
if (size > 5 && cmd.lc > 0) {
26+
size_t data_len = size - 5;
27+
if (cmd.lc > data_len) cmd.lc = data_len;
28+
29+
cmd.data = (uint8_t *) &data[5];
30+
} else {
31+
cmd.data = NULL;
32+
}
33+
apdu_dispatcher(&cmd);
34+
}
35+
return 0;
36+
}

fuzzing/fuzz_tx_parser.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,37 @@
33
#include <string.h>
44
#include <sys/types.h>
55

6-
#include "deserialize.h"
7-
#include "utils.h"
8-
#include "tx_types.h"
6+
#include "transaction/deserialize.h"
7+
#include "transaction/utils.h"
8+
#include "transaction/tx_types.h"
99
#include "format.h"
10+
#include <setjmp.h>
1011

12+
jmp_buf fuzz_exit_jump_buf;
1113
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
12-
buffer_t buf = {.ptr = data, .size = size, .offset = 0};
13-
transaction_t tx;
14-
parser_status_e status;
15-
char nonce[21] = {0};
16-
char address[21] = {0};
17-
char amount[21] = {0};
18-
char tx_memo[466] = {0};
14+
if (setjmp(fuzz_exit_jump_buf) == 0) {
15+
buffer_t buf = {.ptr = data, .size = size, .offset = 0};
16+
transaction_t tx;
17+
parser_status_e status;
18+
char nonce[21] = {0};
19+
char address[21] = {0};
20+
char amount[21] = {0};
21+
char tx_memo[466] = {0};
1922

20-
memset(&tx, 0, sizeof(tx));
23+
memset(&tx, 0, sizeof(tx));
2124

22-
status = transaction_deserialize(&buf, &tx);
25+
status = transaction_deserialize(&buf, &tx);
2326

24-
if (status == PARSING_OK) {
25-
format_u64(nonce, sizeof(nonce), tx.nonce);
26-
printf("nonce: %s\n", nonce);
27-
format_hex(tx.to, ADDRESS_LEN, address, sizeof(address));
28-
printf("address: %s\n", address);
29-
format_fpu64(amount, sizeof(amount), tx.value, 3); // exponent of smallest unit is 3
30-
printf("amount: %s\n", amount);
31-
transaction_utils_format_memo(tx.memo, tx.memo_len, tx_memo, sizeof(tx_memo));
32-
printf("memo: %s\n", tx_memo);
27+
if (status == PARSING_OK) {
28+
format_u64(nonce, sizeof(nonce), tx.nonce);
29+
printf("nonce: %s\n", nonce);
30+
format_hex(tx.to, ADDRESS_LEN, address, sizeof(address));
31+
printf("address: %s\n", address);
32+
format_fpu64(amount, sizeof(amount), tx.value, 3); // exponent of smallest unit is 3
33+
printf("amount: %s\n", amount);
34+
transaction_utils_format_memo(tx.memo, tx.memo_len, tx_memo, sizeof(tx_memo));
35+
printf("memo: %s\n", tx_memo);
36+
}
3337
}
34-
3538
return 0;
3639
}

0 commit comments

Comments
 (0)