Skip to content

Commit

Permalink
refactor!: make proofs mandatory in access logs
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 4, 2024
1 parent 9c3216a commit 7b3e35a
Show file tree
Hide file tree
Showing 29 changed files with 353 additions and 819 deletions.
12 changes: 2 additions & 10 deletions src/access-log.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,31 +224,23 @@ class access_log {
public:
/// \brief Type of access log
class type {
bool m_proofs; ///< Includes proofs
bool m_annotations; ///< Includes annotations
bool m_large_data; ///< Includes data bigger than 8 bytes
public:
/// \brief Default constructor
/// \param proofs Include proofs
/// \param annotations Include annotations (default false)
explicit type(bool proofs, bool annotations = false, bool large_data = false) :
m_proofs(proofs),
/// \param large_data Include large data (default false)
explicit type(bool annotations = false, bool large_data = false) :
m_annotations(annotations),
m_large_data(large_data) {
;
}
explicit type(int log_type) :
m_proofs(static_cast<bool>(log_type & CM_ACCESS_LOG_TYPE_PROOFS)),
m_annotations(static_cast<bool>(log_type & CM_ACCESS_LOG_TYPE_ANNOTATIONS)),
m_large_data(static_cast<bool>(log_type & CM_ACCESS_LOG_TYPE_LARGE_DATA)) {
;
}

/// \brief Returns whether log includes proofs
bool has_proofs(void) const {
return m_proofs;
}

/// \brief Returns whether log includes annotations
bool has_annotations(void) const {
return m_annotations;
Expand Down
10 changes: 2 additions & 8 deletions src/cartesi-machine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2150,17 +2150,11 @@ if gdb_stub then gdb_stub:close() end
if log_step_uarch then
assert(config.processor.iunrep == 0, "micro step proof is meaningless in unreproducible mode")
stderr("Gathering micro step log: please wait\n")
util.dump_log(
machine:log_step_uarch(cartesi.ACCESS_LOG_TYPE_PROOFS | cartesi.ACCESS_LOG_TYPE_ANNOTATIONS),
io.stderr
)
util.dump_log(machine:log_step_uarch(cartesi.ACCESS_LOG_TYPE_ANNOTATIONS), io.stderr)
end
if log_reset_uarch then
stderr("Resetting microarchitecture state: please wait\n")
util.dump_log(
machine:log_reset_uarch(cartesi.ACCESS_LOG_TYPE_PROOFS | cartesi.ACCESS_LOG_TYPE_ANNOTATIONS),
io.stderr
)
util.dump_log(machine:log_reset_uarch(cartesi.ACCESS_LOG_TYPE_ANNOTATIONS), io.stderr)
end
if dump_memory_ranges then dump_pmas(machine) end
if final_hash then
Expand Down
1 change: 0 additions & 1 deletion src/clua-cartesi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ CM_API int luaopen_cartesi(lua_State *L) {
clua_setintegerfield(L, CM_BREAK_REASON_REACHED_TARGET_MCYCLE, "BREAK_REASON_REACHED_TARGET_MCYCLE", -1);
clua_setintegerfield(L, CM_UARCH_BREAK_REASON_REACHED_TARGET_CYCLE, "UARCH_BREAK_REASON_REACHED_TARGET_CYCLE", -1);
clua_setintegerfield(L, CM_UARCH_BREAK_REASON_UARCH_HALTED, "UARCH_BREAK_REASON_UARCH_HALTED", -1);
clua_setintegerfield(L, CM_ACCESS_LOG_TYPE_PROOFS, "ACCESS_LOG_TYPE_PROOFS", -1);
clua_setintegerfield(L, CM_ACCESS_LOG_TYPE_ANNOTATIONS, "ACCESS_LOG_TYPE_ANNOTATIONS", -1);
clua_setintegerfield(L, CM_ACCESS_LOG_TYPE_LARGE_DATA, "ACCESS_LOG_TYPE_LARGE_DATA", -1);
clua_setintegerfield(L, CM_CMIO_YIELD_COMMAND_AUTOMATIC, "CMIO_YIELD_COMMAND_AUTOMATIC", -1);
Expand Down
57 changes: 19 additions & 38 deletions src/clua-jsonrpc-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,12 @@ static int jsonrpc_machine_class_verify_step_uarch(lua_State *L) {
lua_settop(L, 5);
auto &managed_jsonrpc_mgr = clua_check<clua_managed_cm_ptr<cm_jsonrpc_mgr>>(L, stubidx, ctxidx);
const char *log = clua_check_schemed_json_string(L, 2, "AccessLog", ctxidx);
if (!lua_isnil(L, 1) || !lua_isnil(L, 3)) {
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_jsonrpc_verify_step_uarch(managed_jsonrpc_mgr.get(), &root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_jsonrpc_verify_step_uarch(managed_jsonrpc_mgr.get(), nullptr, log, nullptr) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_jsonrpc_verify_step_uarch(managed_jsonrpc_mgr.get(), &root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
return 0;
}
Expand All @@ -86,18 +80,12 @@ static int jsonrpc_machine_class_verify_reset_uarch(lua_State *L) {
lua_settop(L, 5);
auto &managed_jsonrpc_mgr = clua_check<clua_managed_cm_ptr<cm_jsonrpc_mgr>>(L, stubidx, ctxidx);
const char *log = clua_check_schemed_json_string(L, 2, "AccessLog", ctxidx);
if (!lua_isnil(L, 1) || !lua_isnil(L, 3)) {
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_jsonrpc_verify_reset_uarch(managed_jsonrpc_mgr.get(), &root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_jsonrpc_verify_reset_uarch(managed_jsonrpc_mgr.get(), nullptr, log, nullptr) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_jsonrpc_verify_reset_uarch(managed_jsonrpc_mgr.get(), &root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
return 0;
}
Expand All @@ -114,20 +102,13 @@ static int jsonrpc_machine_class_verify_send_cmio_response(lua_State *L) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
const auto *data = reinterpret_cast<const unsigned char *>(luaL_checklstring(L, 2, &length));
const char *log = clua_check_schemed_json_string(L, 4, "AccessLog", ctxidx);
if (!lua_isnil(L, 3) || !lua_isnil(L, 5)) {
cm_hash root_hash{};
clua_check_cm_hash(L, 3, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 5, &target_hash);
if (cm_jsonrpc_verify_send_cmio_response(managed_jsonrpc_mgr.get(), reason, data, length, &root_hash, log,
&target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_jsonrpc_verify_send_cmio_response(managed_jsonrpc_mgr.get(), reason, data, length, nullptr, log,
nullptr) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
cm_hash root_hash{};
clua_check_cm_hash(L, 3, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 5, &target_hash);
if (cm_jsonrpc_verify_send_cmio_response(managed_jsonrpc_mgr.get(), reason, data, length, &root_hash, log,
&target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
return 0;
}
Expand Down
54 changes: 18 additions & 36 deletions src/clua-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@ static int machine_class_index_get_reg_address(lua_State *L) {
static int machine_class_index_verify_step_uarch(lua_State *L) {
lua_settop(L, 4);
const char *log = clua_check_schemed_json_string(L, 2, "AccessLog");
if (!lua_isnil(L, 1) || !lua_isnil(L, 3)) {
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_verify_step_uarch(&root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_verify_step_uarch(nullptr, log, nullptr) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_verify_step_uarch(&root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
return 0;
}
Expand All @@ -63,18 +57,12 @@ static int machine_class_index_verify_step_uarch(lua_State *L) {
static int machine_class_index_verify_reset_uarch(lua_State *L) {
lua_settop(L, 4);
const char *log = clua_check_schemed_json_string(L, 2, "AccessLog");
if (!lua_isnil(L, 1) || !lua_isnil(L, 3)) {
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_verify_reset_uarch(&root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_verify_reset_uarch(nullptr, log, nullptr) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_verify_reset_uarch(&root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
return 0;
}
Expand All @@ -87,18 +75,12 @@ static int machine_class_index_verify_send_cmio_response(lua_State *L) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
const auto *data = reinterpret_cast<const unsigned char *>(luaL_checklstring(L, 2, &length));
const char *log = clua_check_schemed_json_string(L, 4, "AccessLog");
if (!lua_isnil(L, 3) || !lua_isnil(L, 5)) {
cm_hash root_hash{};
clua_check_cm_hash(L, 3, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 5, &target_hash);
if (cm_verify_send_cmio_response(reason, data, length, &root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_verify_send_cmio_response(reason, data, length, nullptr, log, nullptr) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
cm_hash root_hash{};
clua_check_cm_hash(L, 3, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 5, &target_hash);
if (cm_verify_send_cmio_response(reason, data, length, &root_hash, log, &target_hash) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
return 0;
}
Expand Down
17 changes: 6 additions & 11 deletions src/json-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,13 +1082,11 @@ void ju_get_opt_field(const nlohmann::json &j, const K &key, not_default_constru
}
const auto &jk = j[key];
const auto new_path = path + to_string(key) + "/";
bool has_proofs = false;
ju_get_field(jk, "has_proofs"s, has_proofs, new_path);
bool has_annotations = false;
ju_get_field(jk, "has_annotations"s, has_annotations, new_path);
bool has_large_data = false;
ju_get_field(jk, "has_large_data"s, has_large_data, new_path);
optional.emplace(has_proofs, has_annotations, has_large_data);
optional.emplace(has_annotations, has_large_data);
}

template void ju_get_opt_field<uint64_t>(const nlohmann::json &j, const uint64_t &key,
Expand All @@ -1113,12 +1111,10 @@ void ju_get_opt_field(const nlohmann::json &j, const K &key, not_default_constru
}
std::vector<access> accesses;
ju_get_vector_like_field(jk, "accesses"s, accesses, new_path);
if (log_type.value().has_proofs()) {
for (unsigned i = 0; i < accesses.size(); ++i) {
if (!accesses[i].get_sibling_hashes().has_value()) {
throw std::invalid_argument(
"field \""s + new_path + "accesses/" + to_string(i) + "\" missing sibling hashes");
}
for (unsigned i = 0; i < accesses.size(); ++i) {
if (!accesses[i].get_sibling_hashes().has_value()) {
throw std::invalid_argument(
"field \""s + new_path + "accesses/" + to_string(i) + "\" missing sibling hashes");
}
}
std::vector<bracket_note> brackets;
Expand Down Expand Up @@ -1745,8 +1741,7 @@ void to_json(nlohmann::json &j, const std::vector<access> &as) {
}

void to_json(nlohmann::json &j, const access_log::type &log_type) {
j = nlohmann::json{{"has_proofs", log_type.has_proofs()}, {"has_annotations", log_type.has_annotations()},
{"has_large_data", log_type.has_large_data()}};
j = nlohmann::json{{"has_annotations", log_type.has_annotations()}, {"has_large_data", log_type.has_large_data()}};
}

void to_json(nlohmann::json &j, const access_log &log) {
Expand Down
91 changes: 5 additions & 86 deletions src/jsonrpc-discover.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,29 +235,8 @@
},

{
"name": "machine.verify_step_uarch_log",
"summary": "Verifies an access log",
"params": [ {
"name":"log",
"description": "Access log to verify",
"required": true,
"schema": {
"$ref": "#/components/schemas/AccessLog"
}
}
],
"result": {
"name": "status",
"description": "True when operation succeeded",
"schema": {
"type": "boolean"
}
}
},

{
"name": "machine.verify_step_uarch_state_transition",
"summary": "Verifies a state transition",
"name": "machine.verify_step_uarch",
"summary": "Verifies a state transition caused by log_step_uarch",
"params": [ {
"name":"root_hash_before",
"description": "State hash before transition described by access log",
Expand Down Expand Up @@ -291,28 +270,7 @@
},

{
"name": "machine.verify_reset_uarch_log",
"summary": "Verifies an access log produced by a log_reset_uarch",
"params": [ {
"name":"log",
"description": "Access log to verify",
"required": true,
"schema": {
"$ref": "#/components/schemas/AccessLog"
}
}
],
"result": {
"name": "status",
"description": "True when operation succeeded",
"schema": {
"type": "boolean"
}
}
},

{
"name": "machine.verify_reset_uarch_state_transition",
"name": "machine.verify_reset_uarch",
"summary": "Verifies a state transition caused by log_reset_uarch",
"params": [ {
"name":"root_hash_before",
Expand Down Expand Up @@ -795,43 +753,8 @@
},

{
"name": "machine.verify_send_cmio_response_log",
"summary": "Checks the internal consistency of an access log produced by send_cmio_response",
"params": [ {
"name":"reason",
"description": "Reason for sending response",
"required": true,
"schema": {
"$ref": "#/components/schemas/UnsignedInteger"
}
}, {
"name":"data",
"description": "Response data sent when the log was generated.",
"required": true,
"schema": {
"$ref": "#/components/schemas/Base64String"
}
}, {
"name":"log",
"description": "Access log to verify",
"required": true,
"schema": {
"$ref": "#/components/schemas/AccessLog"
}
}
],
"result": {
"name": "status",
"description": "True when operation succeeded",
"schema": {
"type": "boolean"
}
}
},

{
"name": "machine.verify_send_cmio_response_state_transition",
"summary": "Checks the validity of state transitions caused by send_cmio_response",
"name": "machine.verify_send_cmio_response",
"summary": "Verifies a state transition caused by log_send_cmio_response",
"params": [ {
"name":"reason",
"description": "Reason for sending response",
Expand Down Expand Up @@ -1541,9 +1464,6 @@
"title": "AccessLogType",
"type": "object",
"properties": {
"has_proofs": {
"type": "boolean"
},
"has_annotations": {
"type": "boolean"
},
Expand All @@ -1552,7 +1472,6 @@
}
},
"required": [
"has_proofs",
"has_annotations",
"has_large_data"
]
Expand Down
Loading

0 comments on commit 7b3e35a

Please sign in to comment.