Skip to content

Commit

Permalink
feat: add rollup test
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed Apr 17, 2024
1 parent baed41f commit f06694c
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 25 deletions.
9 changes: 6 additions & 3 deletions sys-utils/libcmt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TOOLCHAIN_PREFIX ?= riscv64-linux-gnu-
TARGET_CC := $(TOOLCHAIN_PREFIX)gcc
TARGET_AR := $(TOOLCHAIN_PREFIX)ar
TARGET_CFLAGS := -Wvla -O2 -g -Wall -pedantic -Wextra -Isrc
CFLAGS := -Wvla -O2 -g -Wall -pedantic -Wextra -Isrc
CFLAGS := -fsanitize=address,undefined -pedantic -fstrict-aliasing -Wstrict-aliasing=3 -Wvla -O2 -g -Wall -pedantic -Wextra -Isrc
CC := gcc

all: libcmt host
Expand Down Expand Up @@ -142,12 +142,15 @@ $(mock_OBJDIR)/keccak: tests/keccak.c $(mock_LIB)
$(mock_OBJDIR)/merkle: tests/merkle.c $(mock_LIB)
$(CC) $(CFLAGS) -o $@ $^

$(mock_OBJDIR)/rollup: tests/rollup.c $(mock_LIB)
$(CC) $(CFLAGS) -o $@ $^
$(mock_OBJDIR)/rollup: tests/rollup.c tests/data.h $(mock_LIB)
$(CC) -Itests $(CFLAGS) -o $@ $^

test: $(unittests_BINS)
$(foreach test,$(unittests_BINS),$(test) &&) true

tests/data.h: tests/create-data.sh
$< > $@

#-------------------------------------------------------------------------------
tools_OBJDIR := build/tools
tools_BINS := \
Expand Down
29 changes: 23 additions & 6 deletions sys-utils/libcmt/src/io-mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@
#include <stdlib.h>
#include <string.h>

/** track the number of open "devices". Mimic the kernel driver behavior by limiting it to 1 */
static int open_count = 0;

static int read_whole_file(const char *name, size_t max, void *data, size_t *length);
static int write_whole_file(const char *name, size_t length, const void *data);

int cmt_io_init(cmt_io_driver_t *_me) {
if (!_me) {
return -EINVAL;
}
cmt_io_driver_mock_t *me = &_me->mock;

if (me->tx->begin || me->rx->begin) {
if (open_count) {
return -EBUSY;
}

open_count++;
cmt_io_driver_mock_t *me = &_me->mock;

size_t tx_length = 2U << 20; // 2MB
size_t rx_length = 2U << 20; // 2MB
cmt_buf_init(me->tx, tx_length, malloc(tx_length));
Expand Down Expand Up @@ -68,10 +73,18 @@ void cmt_io_fini(cmt_io_driver_t *_me) {
if (!_me) {
return;
}

if (open_count == 0) {
return;
}

open_count--;
cmt_io_driver_mock_t *me = &_me->mock;

free(me->tx->begin);
free(me->rx->begin);

memset(_me, 0, sizeof(*_me));
}

cmt_buf_t cmt_io_get_tx(cmt_io_driver_t *me) {
Expand Down Expand Up @@ -104,12 +117,16 @@ static int load_next_input(cmt_io_driver_mock_t *me, struct cmt_io_yield *rr) {
size_t file_length = 0;
int rc = read_whole_file(filepath, cmt_buf_length(me->rx), me->rx->begin, &file_length);
if (rc) {
(void) fprintf(stderr, "failed to load \"%s\". %s\n", filepath, strerror(-rc));
if (cmt_util_debug_enabled()) {
(void) fprintf(stderr, "failed to load \"%s\". %s\n", filepath, strerror(-rc));
}
return rc;
}
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
if (sscanf(filepath, " %127[^.]%15s", me->input_filename, me->input_fileext) != 2) {
(void) fprintf(stderr, "failed to parse filename: \"%s\"\n", filepath);
if (cmt_util_debug_enabled()) {
(void) fprintf(stderr, "failed to parse filename: \"%s\"\n", filepath);
}
return -EINVAL;
}

Expand Down Expand Up @@ -146,7 +163,7 @@ static int store_output(cmt_io_driver_mock_t *me, const char *filepath, struct c
static int store_next_output(cmt_io_driver_mock_t *me, char *ns, int *seq, struct cmt_io_yield *rr) {
char filepath[128 + 32 + 8 + 16];
// NOLINTNEXTLINE(cert-err33-c, clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
snprintf(filepath, sizeof filepath, "%s.%s%d%s", me->input_filename, ns, *seq++, me->input_fileext);
snprintf(filepath, sizeof filepath, "%s.%s%d%s", me->input_filename, ns, (*seq)++, me->input_fileext);
return store_output(me, filepath, rr);
}

Expand Down
10 changes: 4 additions & 6 deletions sys-utils/libcmt/src/rollup.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint32_t address_length, const voi
cmt_buf_t of[1];
void *params_base = tx->begin + 4; // after funsel

if (address_length != CMT_ADDRESS_LENGTH)
return -EINVAL;

// clang-format off
if (DBG(cmt_abi_put_funsel(wr, VOUCHER))
|| DBG(cmt_abi_put_uint_be(wr, address_length, address_data))
|| DBG(cmt_abi_put_address(wr, address_data))
|| DBG(cmt_abi_put_uint_be(wr, value_length, value_data))
|| DBG(cmt_abi_put_bytes_s(wr, of))
|| DBG(cmt_abi_put_bytes_d(wr, of, length, data, params_base))) {
Expand Down Expand Up @@ -367,11 +370,6 @@ int cmt_gio_request(cmt_rollup_t *me, cmt_gio_t *req) {
return -ENOBUFS;
}

size_t rd_length = cmt_buf_length(rd);
if (rd_length != rr->data) {
return -EINVAL;
}

req->response_data = rd->begin;
req->response_code = rr->reason;
req->response_data_length = rr->data;
Expand Down
15 changes: 5 additions & 10 deletions sys-utils/libcmt/tests/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "buf.h"
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include "buf.h"
#include <stdio.h>

void split_in_bounds_must_succeed()
{
void split_in_bounds_must_succeed() {
uint8_t _[8];
cmt_buf_t b;
cmt_buf_init(&b, sizeof _, _);
Expand Down Expand Up @@ -63,8 +62,7 @@ void split_in_bounds_must_succeed()
printf("test_buf_split_in_bounds_must_succeed passed\n");
}

void split_out_of_bounds_must_fail(void)
{
void split_out_of_bounds_must_fail(void) {
uint8_t _[8];
cmt_buf_t b;
cmt_buf_t lhs;
Expand All @@ -74,13 +72,10 @@ void split_out_of_bounds_must_fail(void)
assert(cmt_buf_split(&b, 9, &lhs, &rhs) == -ENOBUFS);
assert(cmt_buf_split(&b, SIZE_MAX, &lhs, &rhs) == -ENOBUFS);
printf("test_buf_split_out_of_bounds_must_fail passed\n");

}

int main(void)
{
int main(void) {
split_in_bounds_must_succeed();
split_out_of_bounds_must_fail();
return 0;
}

42 changes: 42 additions & 0 deletions sys-utils/libcmt/tests/create-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

[ ! -d build/data/ ] && mkdir build/data/

echo "#ifndef DATA_H"
echo "#define DATA_H"
echo "#include <stdint.h>"
echo "uint8_t valid_advance_0[] = {"
cast calldata "EvmAdvance(uint256,address,address,uint256,uint256,uint256,bytes)" \
0x0000000000000000000000000000000000000001 \
0x0000000000000000000000000000000000000002 \
0x0000000000000000000000000000000000000003 \
0x0000000000000000000000000000000000000004 \
0x0000000000000000000000000000000000000005 \
0x0000000000000000000000000000000000000006 \
0x`echo -en "advance-0" | xxd -p -c0` | xxd -r -p | xxd -i
echo "};"

echo "uint8_t valid_inspect_0[] = {"
echo -en "inspect-0" | xxd -i
echo "};"

echo "uint8_t valid_report_0[] = {"
echo -en "report-0" | xxd -i
echo "};"

echo "uint8_t valid_exception_0[] = {"
echo -en "exception-0" | xxd -i
echo "};"

echo "uint8_t valid_voucher_0[] = {"
cast calldata "Voucher(address,uint256,bytes)" \
0x0000000000000000000000000000000000000001 \
0x00000000000000000000000000000000deadbeef \
0x`echo -en "voucher-0" | xxd -p -c0` | xxd -r -p | xxd -i
echo "};"

echo "uint8_t valid_notice_0[] = {"
cast calldata "Notice(bytes)" \
0x`echo -en "notice-0" | xxd -p -c0` | xxd -r -p | xxd -i
echo "};"
echo "#endif /* DATA_H */"
67 changes: 67 additions & 0 deletions sys-utils/libcmt/tests/data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef DATA_H
#define DATA_H
#include <stdint.h>
uint8_t valid_advance_0[] = {
0xcc, 0x7d, 0xee, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x61, 0x64, 0x76, 0x61,
0x6e, 0x63, 0x65, 0x2d, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
uint8_t valid_inspect_0[] = {
0x69, 0x6e, 0x73, 0x70, 0x65, 0x63, 0x74, 0x2d, 0x30
};
uint8_t valid_report_0[] = {
0x72, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x2d, 0x30
};
uint8_t valid_exception_0[] = {
0x65, 0x78, 0x63, 0x65, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x2d, 0x30
};
uint8_t valid_voucher_0[] = {
0x23, 0x7a, 0x81, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
0x76, 0x6f, 0x75, 0x63, 0x68, 0x65, 0x72, 0x2d, 0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
uint8_t valid_notice_0[] = {
0xc2, 0x58, 0xd6, 0xe5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x6e, 0x6f, 0x74, 0x69,
0x63, 0x65, 0x2d, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
#endif /* DATA_H */
Loading

0 comments on commit f06694c

Please sign in to comment.