Skip to content

Commit

Permalink
refactor: lint, format, cmt_buf_split
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed Apr 8, 2024
1 parent 3849236 commit af52c5a
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 873 deletions.
3 changes: 1 addition & 2 deletions sys-utils/libcmt/.clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
Expand All @@ -16,4 +15,4 @@ ConstructorInitializerAllOnOneLineOrOnePerLine: true
IndentCaseLabels: true
IndentWidth: 4
SpaceAfterCStyleCast: true
Standard: c++17
Standard: Cpp03
1 change: 1 addition & 0 deletions sys-utils/libcmt/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Checks: >-
-bugprone-easily-swappable-parameters,
cert*,
clang-analyzer*,
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,
cppcoreguidelines*,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-non-private-member-variables-in-classes,
Expand Down
2 changes: 2 additions & 0 deletions sys-utils/libcmt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ libcmt_SRC := \
src/keccak.c \
src/merkle.c \
src/rollup.c \
src/util.c \
src/io.c

libcmt_OBJDIR := build/lib
Expand Down Expand Up @@ -90,6 +91,7 @@ mock_SRC := \
src/keccak.c \
src/merkle.c \
src/rollup.c \
src/util.c \
src/io-mock.c

mock_OBJDIR := build/mock
Expand Down
17 changes: 9 additions & 8 deletions sys-utils/libcmt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,19 @@ simple way to generate testing data is to use the @p cast tool from

Encoding an @p EvmAdvance:
```
cast calldata "EvmAdvance(address,uint256,uint256,uint256,bytes)" \
0x0000000000000000000000000000000000000000 \
0x0000000000000000000000000000000000000000 \
0x0000000000000000000000000000000000000000 \
0x0000000000000000000000000000000000000000 \
cast calldata "EvmAdvance(uint256,address,address,uint256,uint256,uint256,bytes)" \
0x0000000000000000000000000000000000000001 \
0x0000000000000000000000000000000000000002 \
0x0000000000000000000000000000000000000003 \
0x0000000000000000000000000000000000000004 \
0x0000000000000000000000000000000000000005 \
0x0000000000000000000000000000000000000006 \
0x`echo "advance-0" | xxd -p -c0` | xxd -r -p > 0.bin
```

Encoding an @p EvmInspect:
Inspect states require no encoding.
```
cast calldata "EvmInspect(bytes)" \
0x`echo "inspect-0" | xxd -p -c0` | xxd -r -p > 1.bin
echo -en "inspect-0" > 1.bin
```

## parsing outputs
Expand Down
10 changes: 6 additions & 4 deletions sys-utils/libcmt/src/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ int cmt_buf_split(const cmt_buf_t *me, size_t lhs_length, cmt_buf_t *lhs, cmt_bu
return -EINVAL;
}

lhs->begin = me->begin;
lhs->end = rhs->begin = me->begin + lhs_length;
rhs->end = me->end;
// handle aliasing
cmt_buf_t tmp[1] = {*me};
lhs->begin = tmp->begin;
lhs->end = rhs->begin = tmp->begin + lhs_length;
rhs->end = tmp->end;

return lhs->end > me->end ? -ENOBUFS : 0;
return lhs->end > tmp->end ? -ENOBUFS : 0;
}

static int comma(const uint8_t *s, const uint8_t *end) {
Expand Down
75 changes: 48 additions & 27 deletions sys-utils/libcmt/src/io-mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
* limitations under the License.
*/
#include "io.h"
#include "util.h"

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CMT_TX_BUF_MAX_LENGTH (2UL * 1024 * 1024) // 2MB
#define CMT_RX_BUF_MAX_LENGTH (2UL * 1024 * 1024) // 2MB

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);

Expand All @@ -33,8 +31,10 @@ int cmt_io_init(cmt_io_driver_t *_me) {
}
cmt_io_driver_mock_t *me = &_me->mock;

cmt_buf_init(me->tx, CMT_TX_BUF_MAX_LENGTH, malloc(CMT_TX_BUF_MAX_LENGTH));
cmt_buf_init(me->rx, CMT_RX_BUF_MAX_LENGTH, malloc(CMT_RX_BUF_MAX_LENGTH));
size_t tx_length = 2U << 20; // 2MB
size_t rx_length = 2U << 20; // 2MB
cmt_buf_init(me->tx, tx_length, malloc(tx_length));
cmt_buf_init(me->rx, rx_length, malloc(rx_length));

if (!me->tx->begin || !me->rx->begin) {
free(me->tx->begin);
Expand Down Expand Up @@ -99,7 +99,7 @@ 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_RX_BUF_MAX_LENGTH, me->rx->begin, &file_length);
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));
return rc;
Expand All @@ -117,32 +117,26 @@ static int load_next_input(cmt_io_driver_mock_t *me, struct cmt_io_yield *rr) {
me->output_seq = 0;
me->report_seq = 0;
me->exception_seq = 0;
me->rx->end = me->rx->begin + file_length;

if (getenv("CMT_DEBUG")) {
if (cmt_util_debug_enabled()) {
(void) fprintf(stderr, "processing filename: \"%s\" (%lu), type: %d\n", filepath, file_length, me->input_type);
}
return 0;
}

static int store_output(cmt_io_driver_mock_t *me, const char *filepath, struct cmt_io_yield *rr) {
if (rr->data > CMT_TX_BUF_MAX_LENGTH) {
if (rr->data > cmt_buf_length(me->rx)) {
return -ENOBUFS;
}

// char filepath[128 + 1 + 8 + 16];
// snprintf(filepath, sizeof filepath, "%s.%s%d%s", me->input_filename, ns, *seq, me->input_fileext);

int rc = write_whole_file(filepath, rr->data, me->tx->begin);
if (rc) {
(void) fprintf(stderr, "failed to store \"%s\". %s\n", filepath, strerror(-rc));
return rc;
}
if (getenv("CMT_DEBUG")) {
if (cmt_util_debug_enabled()) {
(void) fprintf(stderr, "wrote filename: \"%s\" (%u)\n", filepath, rr->data);
}

// seq[0] += 1;
return 0;
}

Expand Down Expand Up @@ -220,23 +214,12 @@ static int mock_tx_exception(cmt_io_driver_mock_t *me, struct cmt_io_yield *rr)
}

/* These behaviours are defined by the cartesi-machine emulator */
int cmt_io_yield(cmt_io_driver_t *_me, struct cmt_io_yield *rr) {
static int cmt_io_yield_inner(cmt_io_driver_t *_me, struct cmt_io_yield *rr) {
if (!_me) {
return -EINVAL;
}
cmt_io_driver_mock_t *me = &_me->mock;

if (getenv("CMT_DEBUG")) {
(void) fprintf(stderr,
"yield {\n"
"\t.dev = %d,\n"
"\t.cmd = %d,\n"
"\t.reason = %d,\n"
"\t.data = %d,\n"
"};\n",
rr->dev, rr->cmd, rr->reason, rr->data);
}

if (rr->cmd == HTIF_YIELD_CMD_MANUAL) {
switch (rr->reason) {
case HTIF_YIELD_MANUAL_REASON_RX_ACCEPTED:
Expand Down Expand Up @@ -266,6 +249,44 @@ int cmt_io_yield(cmt_io_driver_t *_me, struct cmt_io_yield *rr) {
return 0;
}

/* emulate io.c:cmt_io_yield behavior (go and check it does if you change it) */
int cmt_io_yield(cmt_io_driver_t *_me, struct cmt_io_yield *rr) {
if (!_me) {
return -EINVAL;
}
if (!rr) {
return -EINVAL;
}

bool debug = cmt_util_debug_enabled();
if (debug) {
(void) fprintf(stderr,
"tohost {\n"
"\t.dev = %d,\n"
"\t.cmd = %d,\n"
"\t.reason = %d,\n"
"\t.data = %d,\n"
"};\n",
rr->dev, rr->cmd, rr->reason, rr->data);
}
int rc = cmt_io_yield_inner(_me, rr);
if (rc) {
return rc;
}

if (debug) {
(void) fprintf(stderr,
"fromhost {\n"
"\t.dev = %d,\n"
"\t.cmd = %d,\n"
"\t.reason = %d,\n"
"\t.data = %d,\n"
"};\n",
rr->dev, rr->cmd, rr->reason, rr->data);
}
return rc;
}

static int read_whole_file(const char *name, size_t max, void *data, size_t *length) {
int rc = 0;

Expand Down
46 changes: 18 additions & 28 deletions sys-utils/libcmt/src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
#include "io.h"
#include "util.h"

#include <stdbool.h>
#include <stdio.h>
Expand Down Expand Up @@ -60,9 +61,6 @@ int cmt_io_init(cmt_io_driver_t *_me) {
goto do_unmap;
}

me->rx_max_length = setup.rx.length;
me->rx_fromhost_length = 0;

cmt_buf_init(me->tx, setup.tx.length, tx);
cmt_buf_init(me->rx, setup.rx.length, rx);
return 0;
Expand All @@ -89,42 +87,43 @@ void cmt_io_fini(cmt_io_driver_t *_me) {
}

cmt_buf_t cmt_io_get_tx(cmt_io_driver_t *me) {
static const cmt_buf_t empty = {NULL, NULL};
const cmt_buf_t empty = {NULL, NULL};
if (!me) {
return empty;
}
return *me->ioctl.tx;
}

static uint32_t min(uint32_t a, uint32_t b) {
return a < b ? a : b;
}

cmt_buf_t cmt_io_get_rx(cmt_io_driver_t *me) {
static const cmt_buf_t empty = {NULL, NULL};
const cmt_buf_t empty = {NULL, NULL};
if (!me) {
return empty;
}
cmt_buf_t rx = *me->ioctl.rx;
rx.end = rx.begin + min(me->ioctl.rx_max_length, me->ioctl.rx_fromhost_length);
return rx;
return *me->ioctl.rx;
}

static uint64_t pack(struct cmt_io_yield *rr) {
return ((uint64_t) rr->dev << 56) | ((uint64_t) rr->cmd << 56 >> 8) | ((uint64_t) rr->reason << 48 >> 16) |
((uint64_t) rr->data << 32 >> 32);
// clang-format off
return ((uint64_t) rr->dev << 56)
| ((uint64_t) rr->cmd << 56 >> 8)
| ((uint64_t) rr->reason << 48 >> 16)
| ((uint64_t) rr->data << 32 >> 32);
// clang-format on
}

static struct cmt_io_yield unpack(uint64_t x) {
// clang-format off
struct cmt_io_yield out = {
x >> 56,
x << 8 >> 56,
x >> 56,
x << 8 >> 56,
x << 16 >> 48,
x << 32 >> 32,
};
// clang-format on
return out;
}

/* io-mock.c:cmt_io_yield emulates this behavior (go and check it does if you change it) */
int cmt_io_yield(cmt_io_driver_t *_me, struct cmt_io_yield *rr) {
if (!_me) {
return -EINVAL;
Expand All @@ -134,15 +133,8 @@ int cmt_io_yield(cmt_io_driver_t *_me, struct cmt_io_yield *rr) {
}
cmt_io_driver_ioctl_t *me = &_me->ioctl;

static bool checked = false;
static bool enabled = false;

if (!checked) {
enabled = getenv("CMT_DEBUG") != NULL;
checked = true;
}

if (enabled) {
bool debug = cmt_util_debug_enabled();
if (debug) {
(void) fprintf(stderr,
"tohost {\n"
"\t.dev = %d,\n"
Expand All @@ -158,9 +150,7 @@ int cmt_io_yield(cmt_io_driver_t *_me, struct cmt_io_yield *rr) {
}
*rr = unpack(req);

me->rx_fromhost_length = rr->data;

if (enabled) {
if (debug) {
(void) fprintf(stderr,
"fromhost {\n"
"\t.dev = %d,\n"
Expand Down
15 changes: 8 additions & 7 deletions sys-utils/libcmt/src/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ enum {
typedef struct {
cmt_buf_t tx[1];
cmt_buf_t rx[1];
uint32_t rx_max_length;
uint32_t rx_fromhost_length;
int fd;
} cmt_io_driver_ioctl_t;

Expand Down Expand Up @@ -127,9 +125,12 @@ typedef struct cmt_io_yield {
/** Open the io device and initialize the driver. Release its resources with @ref cmt_io_fini.
*
* @param [in] me A uninitialized @ref cmt_io_driver state
* @returns
* - 0 on success
* - negative errno code on error */
*
* @return
* | | |
* |--:|-----------------------------|
* | 0| success |
* |< 0| failure with a -errno value | */
int cmt_io_init(cmt_io_driver_t *me);

/** Release the driver resources and close the io device.
Expand All @@ -142,15 +143,15 @@ void cmt_io_fini(cmt_io_driver_t *me);
*
* @param [in] me A successfully initialized state by @ref cmt_io_init
* @return
* - writable memory region (check @ref cmt_buf_t)
* - writable memory region as defined in the setup structure (check @ref cmt_buf_t)
* @note memory is valid until @ref cmt_io_fini is called. */
cmt_buf_t cmt_io_get_tx(cmt_io_driver_t *me);

/** Retrieve the receive buffer @p rx
*
* @param [in] me A successfully initialized state by @ref cmt_io_init
* @return
* - readable memory region (check @ref cmt_buf_t)
* - readable memory region as defined in the setup structure (check @ref cmt_buf_t)
* @note memory is valid until @ref cmt_io_fini is called. */
cmt_buf_t cmt_io_get_rx(cmt_io_driver_t *me);

Expand Down
Loading

0 comments on commit af52c5a

Please sign in to comment.