Skip to content

Commit

Permalink
fix: fix build with gcc 12.3
Browse files Browse the repository at this point in the history
  • Loading branch information
vfusco committed Nov 30, 2023
1 parent 1e1155a commit f4282a8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
23 changes: 11 additions & 12 deletions linux/rollup/ioctl-echo-loop/ioctl-echo-loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
* limitations under the License.
*/


#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <linux/cartesi/rollup.h>

Expand Down Expand Up @@ -188,7 +187,8 @@ static int write_exception(int fd, struct rollup_bytes *bytes, unsigned verbose)
return 0;
}

static int handle_advance_state_request(int fd, struct parsed_args *args, struct rollup_finish *finish, struct rollup_bytes *bytes, struct rollup_input_metadata *metadata) {
static int handle_advance_state_request(int fd, struct parsed_args *args, struct rollup_finish *finish,
struct rollup_bytes *bytes, struct rollup_input_metadata *metadata) {
struct rollup_advance_state req;
int res = 0;
if (resize_bytes(bytes, finish->next_request_payload_length) != 0) {
Expand Down Expand Up @@ -244,7 +244,8 @@ static int handle_inspect_state_request(int fd, struct parsed_args *args, struct
return 0;
}

static int handle_request(int fd, struct parsed_args *args, struct rollup_finish *finish, struct rollup_bytes *bytes, struct rollup_input_metadata *metadata) {
static int handle_request(int fd, struct parsed_args *args, struct rollup_finish *finish, struct rollup_bytes *bytes,
struct rollup_input_metadata *metadata) {
switch (finish->next_request_type) {
case CARTESI_ROLLUP_ADVANCE_STATE:
return handle_advance_state_request(fd, args, finish, bytes, metadata);
Expand Down Expand Up @@ -293,12 +294,10 @@ int main(int argc, char *argv[]) {
break;
}
reject_advance =
(finish.next_request_type == CARTESI_ROLLUP_ADVANCE_STATE) &&
(args.reject == metadata.input_index);
reject_inspect =
(finish.next_request_type == CARTESI_ROLLUP_INSPECT_STATE) && args.reject_inspects;
throw_exception = (finish.next_request_type == CARTESI_ROLLUP_ADVANCE_STATE) &&
(args.exception == metadata.input_index);
(finish.next_request_type == CARTESI_ROLLUP_ADVANCE_STATE) && (args.reject == metadata.input_index);
reject_inspect = (finish.next_request_type == CARTESI_ROLLUP_INSPECT_STATE) && args.reject_inspects;
throw_exception =
(finish.next_request_type == CARTESI_ROLLUP_ADVANCE_STATE) && (args.exception == metadata.input_index);
if (throw_exception) {
write_exception(fd, &bytes, args.verbose);
}
Expand Down
79 changes: 42 additions & 37 deletions linux/rollup/rollup/rollup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
#include <cstdint>
#include <iostream>
#include <iterator>
#include <memory>
#include <sstream>
#include <string>
#include <string_view>
#include <sstream>
#include <memory>
#include <unordered_map>

#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/cartesi/rollup.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "json.hpp"

Expand All @@ -37,13 +38,24 @@
// NullablePointer implementation for file descriptors
class file_desc {
public:
explicit file_desc(int fd): m_fd(fd) {}
file_desc(std::nullptr_t = nullptr): m_fd(-1) {}
operator int() const { return m_fd; }
bool operator==(const file_desc &other) const {return m_fd == other.m_fd;}
bool operator!=(const file_desc &other) const {return m_fd != other.m_fd;}
bool operator==(std::nullptr_t) const {return m_fd == -1;}
bool operator!=(std::nullptr_t) const {return m_fd != -1;}
explicit file_desc(int fd) : m_fd(fd) {}
file_desc(std::nullptr_t = nullptr) : m_fd(-1) {}
operator int() const {
return m_fd;
}
bool operator==(const file_desc &other) const {
return m_fd == other.m_fd;
}
bool operator!=(const file_desc &other) const {
return m_fd != other.m_fd;
}
bool operator==(std::nullptr_t) const {
return m_fd == -1;
}
bool operator!=(std::nullptr_t) const {
return m_fd != -1;
}

private:
int m_fd;
};
Expand Down Expand Up @@ -96,7 +108,7 @@ static void file_desc_ioctl(const unique_file_desc &fd, unsigned long request, v
// Print help message with program usage
static void print_help(void) {
std::cerr <<
R"(Usage:
R"(Usage:
rollup [command]
where [command] is one of
Expand Down Expand Up @@ -186,13 +198,13 @@ static void resize_bytes(struct rollup_bytes *bytes, uint64_t size) {
// Convert a hex character into its corresponding nibble {0..15}
static uint8_t hexnibble(char a) {
if (a >= 'a' && a <= 'f') {
return a-'a'+10;
return a - 'a' + 10;
}
if (a >= 'A' && a <= 'F') {
return a-'A'+10;
return a - 'A' + 10;
}
if (a >= '0' && a <= '9') {
return a-'0';
return a - '0';
}
throw std::invalid_argument{"invalid hex character"};
return 0;
Expand All @@ -212,9 +224,9 @@ static std::string unhex(const std::string &s) {
throw std::invalid_argument{"invalid character in address"};
}
std::string res;
res.reserve(20+1);
res.reserve(20 + 1);
for (unsigned i = 2; i < s.size(); i += 2) {
res.push_back(hexbyte(s[i], s[i+1]));
res.push_back(hexbyte(s[i], s[i + 1]));
}
return res;
}
Expand All @@ -223,7 +235,7 @@ static std::string unhex(const std::string &s) {
static std::string hex(const uint8_t *data, uint64_t length) {
std::stringstream ss;
ss << "0x";
for (auto b: std::string_view{reinterpret_cast<const char *>(data), length}) {
for (auto b : std::string_view{reinterpret_cast<const char *>(data), length}) {
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<unsigned>(b);
}
return ss.str();
Expand Down Expand Up @@ -307,19 +319,13 @@ static void write_advance_state(const unique_file_desc &fd, const struct rollup_
file_desc_ioctl(fd, IOCTL_ROLLUP_READ_ADVANCE_STATE, &r);
auto payload = std::string_view{reinterpret_cast<const char *>(r.payload.data), r.payload.length};
const auto &m = r.metadata;
nlohmann::json j = {
{"request_type", "advance_state"},
{"data", {
{"payload", payload},
{"metadata", {
{"msg_sender", hex(m.msg_sender, sizeof(m.msg_sender))},
{"epoch_index", m.epoch_index},
{"input_index", m.input_index},
{"block_number", m.block_number},
{"timestamp", m.timestamp}
}}
}}
};
nlohmann::json j = {{"request_type", "advance_state"},
{"data",
{{"payload", payload},
{"metadata",
{{"msg_sender", hex(m.msg_sender, sizeof(m.msg_sender))}, {"epoch_index", m.epoch_index},
{"input_index", m.input_index}, {"block_number", m.block_number},
{"timestamp", m.timestamp}}}}}};
std::cout << j.dump(2) << '\n';
}

Expand All @@ -330,12 +336,11 @@ static void write_inspect_state(const unique_file_desc &fd, const struct rollup_
resize_bytes(&r.payload, f->next_request_payload_length);
file_desc_ioctl(fd, IOCTL_ROLLUP_READ_INSPECT_STATE, &r);
auto payload = std::string_view{reinterpret_cast<const char *>(r.payload.data), r.payload.length};
nlohmann::json j = {
{"request_type", "inspect_state"},
{"data", {
{"payload", payload},
}}
};
nlohmann::json j = {{"request_type", "inspect_state"},
{"data",
{
{"payload", payload},
}}};
std::cout << j.dump(2) << '\n';
}

Expand Down

0 comments on commit f4282a8

Please sign in to comment.