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

make error codes uniform and improve code coverage of libcmt #51

Merged
merged 2 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
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
50 changes: 3 additions & 47 deletions sys-utils/libcmt/src/io-mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "io.h"
#include "util.h"

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -25,9 +24,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 +112,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 All @@ -130,8 +126,6 @@ static int load_next_input(cmt_io_driver_mock_t *me, struct cmt_io_yield *rr) {
}
return -EINVAL;
}

assert(file_length <= INT32_MAX);
rr->reason = me->input_type;
rr->data = file_length;

Expand All @@ -150,7 +144,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 +199,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 +249,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 @@ -326,38 +317,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;
}
32 changes: 10 additions & 22 deletions sys-utils/libcmt/src/merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
#include "merkle.h"
#include "util.h"

#include <errno.h>
#include <stdbool.h>
Expand Down Expand Up @@ -128,35 +129,22 @@ void cmt_merkle_fini(cmt_merkle_t *me) {
}

int cmt_merkle_load(cmt_merkle_t *me, const char *filepath) {
FILE *fin = fopen(filepath, "rb");
if (!fin) {
return -errno;
if (!me) {
return -EINVAL;
}
size_t read = fread(me, 1, sizeof(*me), fin);
int rc = 0;
if (read < sizeof(*me)) {
rc = -ENOBUFS;
}
if (fclose(fin) != 0 && rc == 0) {
return -errno;
size_t length = 0;
int rc = cmt_util_read_whole_file(filepath, sizeof *me, me, &length);
if (length != sizeof *me) {
return -EINVAL;
}
return rc;
}

int cmt_merkle_save(cmt_merkle_t *me, const char *filepath) {
FILE *fout = fopen(filepath, "wb");
if (!fout) {
return -errno;
}
size_t written = fwrite(me, 1, sizeof(*me), fout);
int rc = 0;
if (written < sizeof(*me)) {
rc = -EIO;
if (!me) {
return -EINVAL;
}
if (fclose(fout) != 0 && rc == 0) {
return -errno;
}
return rc;
return cmt_util_write_whole_file(filepath, sizeof *me, me);
}

uint64_t cmt_merkle_get_leaf_count(cmt_merkle_t *me) {
Expand Down
37 changes: 37 additions & 0 deletions sys-utils/libcmt/src/util.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand All @@ -13,3 +15,38 @@ 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);
int eof = (fseek(file, 0, SEEK_END) == 0) && (*length == (size_t) ftell(file));
if (!eof) {
rc = -EIO;
}
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 */
Loading
Loading