Skip to content

Commit

Permalink
fix!: error values and improve coverage
Browse files Browse the repository at this point in the history
refactor: move read/write file to util
- remove unreachable if block
  • Loading branch information
mpolitzer committed Apr 29, 2024
1 parent 158948a commit ecdd0cd
Show file tree
Hide file tree
Showing 11 changed files with 457 additions and 152 deletions.
8 changes: 4 additions & 4 deletions sys-utils/libcmt/src/abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel) {

int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]) {
if (n > CMT_WORD_LENGTH) {
return EDOM;
return -EDOM;
}
for (size_t i = 0; i < n; ++i) {
out[CMT_WORD_LENGTH - 1 - i] = data[i];
Expand All @@ -52,7 +52,7 @@ int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_L

int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]) {
if (n > CMT_WORD_LENGTH) {
return EDOM;
return -EDOM;
}
for (size_t i = 0; i < CMT_WORD_LENGTH - n; ++i) {
out[i] = 0;
Expand All @@ -73,7 +73,7 @@ int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_WORD_LENGTH]

int cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out) {
if (n > CMT_WORD_LENGTH) {
return EDOM;
return -EDOM;
}
for (size_t i = 0; i < CMT_WORD_LENGTH - n; ++i) {
if (data[i]) {
Expand All @@ -88,7 +88,7 @@ int cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_

int cmt_abi_decode_uint_nn(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out) {
if (n > CMT_WORD_LENGTH) {
return EDOM;
return -EDOM;
}
for (size_t i = 0; i < CMT_WORD_LENGTH - n; ++i) {
if (data[i]) {
Expand Down
46 changes: 3 additions & 43 deletions sys-utils/libcmt/src/io-mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
/** 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;
Expand Down Expand Up @@ -116,7 +113,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_buf_length(me->rx), me->rx->begin, &file_length);
int rc = cmt_util_read_whole_file(filepath, cmt_buf_length(me->rx), me->rx->begin, &file_length);
if (rc) {
if (cmt_util_debug_enabled()) {
(void) fprintf(stderr, "failed to load \"%s\". %s\n", filepath, strerror(-rc));
Expand Down Expand Up @@ -150,7 +147,7 @@ static int store_output(cmt_io_driver_mock_t *me, const char *filepath, struct c
return -ENOBUFS;
}

int rc = write_whole_file(filepath, rr->data, me->tx->begin);
int rc = cmt_util_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;
Expand Down Expand Up @@ -205,7 +202,7 @@ static int mock_rx_rejected(cmt_io_driver_mock_t *me, struct cmt_io_yield *rr) {
}
(void) fprintf(stderr, "%s:%d no revert for the mock implementation\n", __FILE__, __LINE__);
if (load_next_input(me, rr)) {
return -1;
return -ENOSYS;
}
return 0;
}
Expand Down Expand Up @@ -255,9 +252,6 @@ static int mock_tx_gio(cmt_io_driver_mock_t *me, struct cmt_io_yield *rr) {

/* These behaviours are defined by the cartesi-machine emulator */
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 (rr->cmd == HTIF_YIELD_CMD_MANUAL) {
Expand Down Expand Up @@ -327,37 +321,3 @@ int cmt_io_yield(cmt_io_driver_t *_me, struct cmt_io_yield *rr) {
return rc;
}

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

FILE *file = fopen(name, "rb");
if (!file) {
return -errno;
}

*length = fread(data, 1, max, file);
if (!feof(file)) {
rc = -ENOBUFS;
}
if (fclose(file) != 0) {
rc = -errno;
}
return rc;
}

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

FILE *file = fopen(name, "wb");
if (!file) {
return -errno;
}

if (fwrite(data, 1, length, file) != length) {
rc = -EIO;
}
if (fclose(file) != 0) {
rc = -errno;
}
return rc;
}
6 changes: 6 additions & 0 deletions sys-utils/libcmt/src/merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ void cmt_merkle_fini(cmt_merkle_t *me) {
}

int cmt_merkle_load(cmt_merkle_t *me, const char *filepath) {
if (!me) {
return -EINVAL;
}
FILE *fin = fopen(filepath, "rb");
if (!fin) {
return -errno;
Expand All @@ -144,6 +147,9 @@ int cmt_merkle_load(cmt_merkle_t *me, const char *filepath) {
}

int cmt_merkle_save(cmt_merkle_t *me, const char *filepath) {
if (!me) {
return -EINVAL;
}
FILE *fout = fopen(filepath, "wb");
if (!fout) {
return -errno;
Expand Down
39 changes: 39 additions & 0 deletions sys-utils/libcmt/src/util.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -13,3 +16,39 @@ bool cmt_util_debug_enabled(void) {

return enabled;
}

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

FILE *file = fopen(name, "rb");
if (!file) {
return -errno;
}

*length = fread(data, 1, max, file);
if (!feof(file)) {
rc = -ENOBUFS;
}
if (fclose(file) != 0) {
rc = -errno;
}
return rc;
}

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

FILE *file = fopen(name, "wb");
if (!file) {
return -errno;
}

if (fwrite(data, 1, length, file) != length) {
rc = -EIO;
}
if (fclose(file) != 0) {
rc = -errno;
}
return rc;
}

25 changes: 25 additions & 0 deletions sys-utils/libcmt/src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,29 @@
*/
bool cmt_util_debug_enabled(void);

/** Read whole file `name` contents into `data` and set `length`.
* @param name[in] - file path
* @param max[in] - size of `data` in bytes
* @param data[out] - file contents
* @param length[out] - actual size in `bytes` written to `data`
*
* @return
* | | |
* |-----|--------------------|
* | 0 |success |
* | < 0 |negative errno value| */
int cmt_util_read_whole_file(const char *name, size_t max, void *data, size_t *length);

/** Write the contents of `data` into file `name`.
* @param name[in] - file path
* @param length[in] - size of `data` in bytes
* @param data[out] - file contents
*
* @return
* | | |
* |-----|--------------------|
* | 0 |success |
* | < 0 |negative errno value| */
int cmt_util_write_whole_file(const char *name, size_t length, const void *data);

#endif /* CMT_UTIL_H */

0 comments on commit ecdd0cd

Please sign in to comment.