Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fuzz testing. #16

Merged
merged 8 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 50 additions & 29 deletions fuzz/fuzz_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,71 @@
#include "stellar/parser.h"
#include "stellar/formatter.h"

#define DETAIL_CAPTION_MAX_LENGTH 20
#define DETAIL_CAPTION_MAX_LENGTH 21
#define DETAIL_VALUE_MAX_LENGTH 105

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
envelope_t envelope;
memset(&envelope, 0, sizeof(envelope_t));
if (!parse_transaction_envelope(data, size, &envelope)) {
return 0;
}

bool data_exists = true;
bool is_op_header = false;
char detail_caption[DETAIL_CAPTION_MAX_LENGTH];
char detail_value[DETAIL_VALUE_MAX_LENGTH];

uint8_t signing_key[] = {0xe9, 0x33, 0x88, 0xbb, 0xfd, 0x2f, 0xbd, 0x11, 0x80, 0x6d, 0xd0,
0xbd, 0x59, 0xce, 0xa9, 0x7, 0x9e, 0x7c, 0xc7, 0xc, 0xe7, 0xb1,
0xe1, 0x54, 0xf1, 0x14, 0xcd, 0xfe, 0x4e, 0x46, 0x6e, 0xcd};

formatter_data_t fdata = {
.raw_data = data,
.raw_data_len = size,
.envelope = &envelope,
.caption = detail_caption,
.value = detail_value,
.signing_key = signing_key,
.caption_len = DETAIL_CAPTION_MAX_LENGTH,
.value_len = DETAIL_VALUE_MAX_LENGTH,
.display_sequence = true,
};

if (!reset_formatter(&fdata)) {
return 0;
}
memset(&envelope, 0, sizeof(envelope_t));
if (parse_transaction_envelope(data, size, &envelope)) {
formatter_data_t tx_fdata = {
.raw_data = data,
.raw_data_len = size,
.envelope = &envelope,
.caption = detail_caption,
.value = detail_value,
.signing_key = signing_key,
.caption_len = DETAIL_CAPTION_MAX_LENGTH,
.value_len = DETAIL_VALUE_MAX_LENGTH,
.display_sequence = true,
};
reset_formatter();

bool data_exists = true;
bool is_op_header = false;
while (true) {
if (!get_next_data(&tx_fdata, true, &data_exists, &is_op_header)) {
break;
}

while (true) {
if (!get_next_data(&fdata, true, &data_exists, &is_op_header)) {
return 0;
if (!data_exists) {
break;
}
}
}

memset(&envelope, 0, sizeof(envelope_t));
if (parse_soroban_authorization_envelope(data, size, &envelope)) {
formatter_data_t auth_fdata = {
.raw_data = data,
.raw_data_len = size,
.envelope = &envelope,
.caption = detail_caption,
.value = detail_value,
.signing_key = signing_key,
.caption_len = DETAIL_CAPTION_MAX_LENGTH,
.value_len = DETAIL_VALUE_MAX_LENGTH,
.display_sequence = true,
};

reset_formatter();

if (!data_exists) {
break;
while (true) {
if (!get_next_data(&auth_fdata, true, &data_exists, &is_op_header)) {
break;
}

if (!data_exists) {
break;
}
}
}

return 0;
}
5 changes: 1 addition & 4 deletions libstellar/formatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2492,13 +2492,10 @@ static uint8_t get_data_count(formatter_data_t *fdata) {
return op_cnt + 1;
}

bool reset_formatter(formatter_data_t *fdata) {
// TODO: fix back button?
(void) fdata;
void reset_formatter() {
explicit_bzero(formatter_stack, sizeof(formatter_stack));
formatter_index = 0;
current_data_index = 0;
return true;
}

bool get_next_data(formatter_data_t *fdata, bool forward, bool *data_exists, bool *is_op_header) {
Expand Down
2 changes: 1 addition & 1 deletion libstellar/include/stellar/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ typedef struct {
/**
* Reset the formatter state.
*/
bool reset_formatter(formatter_data_t *fdata);
void reset_formatter();

/**
* Get the next data to display.
Expand Down
2 changes: 1 addition & 1 deletion src/ui/bagl_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void prepare_display() {
.plugin_query_data_pair_count = &plugin_query_data_pair_count,
.plugin_query_data_pair = &plugin_query_data_pair,
};
reset_formatter(&fdata);
reset_formatter();

// init formatter_data
memcpy(&formatter_data, &fdata, sizeof(formatter_data_t));
Expand Down
4 changes: 2 additions & 2 deletions src/ui/nbgl_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static void prepare_tx_pages_infos(void) {
uint8_t page_line_nb = 0;
uint8_t field_len = 0;
uint8_t data_index = 0;
reset_formatter(&formatter_data);
reset_formatter();

// Reset globals.
nb_pages = 0;
Expand Down Expand Up @@ -165,7 +165,7 @@ static void prepare_tx_pages_infos(void) {

static void prepare_page(uint8_t page) {
PRINTF("prepare_page, page: %d\n", page);
reset_formatter(&formatter_data);
reset_formatter();
uint8_t data_start_index = pages_infos[page].data_idx;
bool data_exists = true;
bool is_op_header = false;
Expand Down
2 changes: 1 addition & 1 deletion tests_unit/test_auth_formatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void test_format_envelope(void **state) {
char output[4096] = {0};
bool data_exists = true;
bool is_op_header = false;
assert_true(reset_formatter(&fdata));
reset_formatter();
while (true) {
assert_true(get_next_data(&fdata, true, &data_exists, &is_op_header));
if (!data_exists) {
Expand Down
4 changes: 2 additions & 2 deletions tests_unit/test_tx_formatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void test_format_envelope(void **state) {
char output[4096] = {0};
bool data_exists = true;
bool is_op_header = false;
assert_true(reset_formatter(&fdata));
reset_formatter();
while (true) {
assert_true(get_next_data(&fdata, true, &data_exists, &is_op_header));
if (!data_exists) {
Expand Down Expand Up @@ -254,7 +254,7 @@ void test_formatter_forward(void **state) {

bool data_exists = false;
bool is_op_header = false;
assert_true(reset_formatter(&fdata));
reset_formatter();

// Flow:
// Memo Text; hello world
Expand Down
Loading