Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
perf: init debug_print_buffer in run_polyjuice()
Browse files Browse the repository at this point in the history
All the elements of global arrays are initialized to default values
(zero for arithmetic types and NULL for pointers).
`memset` caused by the initialization of such arrays is not necessary.
  • Loading branch information
floustar authored and Flouse committed Aug 10, 2021
1 parent f5dc663 commit 886d97e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
6 changes: 6 additions & 0 deletions c/polyjuice.h
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,12 @@ int clean_evmc_result_and_return(evmc_result *res, int code) {
}

int run_polyjuice() {
#ifndef NO_DEBUG_LOG
// init buffer for debug_print
char buffer[DEBUG_BUFFER_SIZE];
debug_buffer = buffer;
#endif

int ret;

/* prepare context */
Expand Down
16 changes: 11 additions & 5 deletions c/polyjuice_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@
#define debug_print(s) {}
#define debug_print_int(prefix, value) {}
#define debug_print_data(prefix, data, data_len) {}
#else /* #ifdef NO_DEBUG_LOG */
static char debug_buffer[64 * 1024];
#else /* NO_DEBUG_LOG */
/* 64 KB */
#define DEBUG_BUFFER_SIZE 65536
static char *debug_buffer;
void debug_print_data(const char *prefix, const uint8_t *data,
uint32_t data_len) {
if (data_len > 31 * 1024) {
ckb_debug("warning: print_data is too large");
if (data_len > (DEBUG_BUFFER_SIZE - 1024) / 2 - 1) { // leave 1KB to prefix
ckb_debug("warning: length of data is too large");
return;
}

int offset = 0;
offset += sprintf(debug_buffer, "%s 0x", prefix);
if (offset > 1024) {
ckb_debug("warning: length of prefix is too large");
return;
}
for (size_t i = 0; i < data_len; i++) {
offset += sprintf(debug_buffer + offset, "%02x", data[i]);
}
Expand All @@ -36,7 +42,7 @@ void debug_print_int(const char* prefix, int64_t ret) {
sprintf(debug_buffer, "%s => %ld", prefix, ret);
ckb_debug(debug_buffer);
}
#endif /* #ifdef NO_DEBUG_LOG */
#endif /* NO_DEBUG_LOG */

/* polyjuice contract account (normal/create2) script args size*/
static const uint32_t CONTRACT_ACCOUNT_SCRIPT_ARGS_SIZE = 32 + 4 + 20;
Expand Down
6 changes: 6 additions & 0 deletions c/tests/test_contracts.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,12 @@ int init_mock_state() {
#endif

int main() {
#ifndef NO_DEBUG_LOG
// init buffer for debug_print
char buffer[DEBUG_BUFFER_SIZE];
debug_buffer = buffer;
#endif

#ifdef FUZZING
if (0 != init_mock_state()) {
ckb_debug("[init_mock_state] failed to init_mock_state");
Expand Down
6 changes: 6 additions & 0 deletions c/tests/test_rlp.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ void test(const char *sender_hex, uint32_t nonce, const char *expected_rlp, cons
}

int main() {
#ifndef NO_DEBUG_LOG
// init buffer for debug_print
char buffer[DEBUG_BUFFER_SIZE];
debug_buffer = buffer;
#endif

test("004ec07d2329997267ec62b4166639513386f32e", 142, "d794004ec07d2329997267ec62b4166639513386f32e818e", "8d7bb25141ff9c4c77e9e208b6bf4d1d3ca684b0");
test("004ec07d2329997267ec62b4166639513386f32e", 512, "d894004ec07d2329997267ec62b4166639513386f32e820200", "ecf98cb7016edb8e306e844a420597770de4e555");
test("004ec07d2329997267ec62b4166639513386f32e", 1111, "d894004ec07d2329997267ec62b4166639513386f32e820457", "e2be2e38fc6dc1eed5cab7e5af984c8ebb8c0854");
Expand Down
2 changes: 1 addition & 1 deletion deps/godwoken-scripts

0 comments on commit 886d97e

Please sign in to comment.