Skip to content

Commit 8428bd6

Browse files
committed
refactor: prepare config for new merkle tree
1 parent e8dff4d commit 8428bd6

23 files changed

+1059
-744
lines changed

src/cartesi-machine.lua

+297-193
Large diffs are not rendered by default.

src/clua-i-machine.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -825,13 +825,10 @@ static int machine_obj_index_translate_virtual_address(lua_State *L) {
825825
/// \brief Replaces a memory range.
826826
/// \param L Lua state.
827827
static int machine_obj_index_replace_memory_range(lua_State *L) {
828-
lua_settop(L, 5);
828+
lua_settop(L, 2);
829829
auto &m = clua_check<clua_managed_cm_ptr<cm_machine>>(L, 1);
830-
const uint64_t start = luaL_checkinteger(L, 2);
831-
const uint64_t length = luaL_checkinteger(L, 3);
832-
const bool shared = lua_toboolean(L, 4) != 0;
833-
const char *image_filename = luaL_optstring(L, 5, nullptr);
834-
if (cm_replace_memory_range(m.get(), start, length, shared, image_filename) != 0) {
830+
const char *range_config = clua_check_json_string(L, 2);
831+
if (cm_replace_memory_range(m.get(), range_config) != 0) {
835832
return luaL_error(L, "%s", cm_get_last_error_message());
836833
}
837834
return 0;

src/json-util.cpp

+150-143
Large diffs are not rendered by default.

src/json-util.h

+46-40
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "machine-runtime-config.h"
3838
#include "machine.h"
3939
#include "semantic-version.h"
40-
#include "uarch-config.h"
4140
#include "uarch-interpret.h"
4241

4342
namespace cartesi {
@@ -68,18 +67,24 @@ void ju_get_field(const nlohmann::json &j, const K &key, T &value, const std::st
6867

6968
// Allows use contains when the index is an integer and j contains an array
7069
template <typename T>
71-
inline bool contains(const nlohmann::json &j, T i)
70+
inline bool contains(const nlohmann::json &j, T i, const std::string &path)
7271
requires(std::is_integral_v<T>)
7372
{
73+
if (!j.empty() && !j.is_array()) {
74+
throw std::invalid_argument("\""s + path + "\" not an array");
75+
}
7476
if constexpr (std::is_signed_v<T>) {
75-
return j.is_array() && i >= 0 && i < static_cast<T>(j.size());
77+
return i >= 0 && i < static_cast<T>(j.size());
7678
} else {
77-
return j.is_array() && i < j.size();
79+
return i < j.size();
7880
}
7981
}
8082

8183
// Overload for case where index is a string and j contains an object
82-
inline bool contains(const nlohmann::json &j, const std::string &s) {
84+
inline bool contains(const nlohmann::json &j, const std::string &s, const std::string &path) {
85+
if (!j.empty() && !j.is_object()) {
86+
throw std::invalid_argument("\""s + path + "\" not an object");
87+
}
8388
return j.contains(s);
8489
}
8590

@@ -340,54 +345,55 @@ template <typename K>
340345
void ju_get_opt_field(const nlohmann::json &j, const K &key, memory_range_config &value,
341346
const std::string &path = "params/");
342347

343-
/// \brief Attempts to load a cmio_buffer_config object from a field in a JSON object
348+
/// \brief Attempts to load a backing_store_config object from a field in a JSON object
344349
/// \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
345350
/// \param j JSON object to load from
346351
/// \param key Key to load value from
347352
/// \param value Object to store value
348353
/// \param path Path to j
349354
template <typename K>
350-
void ju_get_opt_field(const nlohmann::json &j, const K &key, cmio_buffer_config &value,
355+
void ju_get_opt_field(const nlohmann::json &j, const K &key, backing_store_config &value,
351356
const std::string &path = "params/");
352357

353-
/// \brief Attempts to load a flash_drive_configs object from a field in a JSON object
358+
/// \brief Attempts to load a backing_store_config_only object from a field in a JSON object
354359
/// \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
355360
/// \param j JSON object to load from
356361
/// \param key Key to load value from
357362
/// \param value Object to store value
358363
/// \param path Path to j
359364
template <typename K>
360-
void ju_get_opt_field(const nlohmann::json &j, const K &key, flash_drive_configs &value,
365+
void ju_get_opt_field(const nlohmann::json &j, const K &key, backing_store_config_only &value,
361366
const std::string &path = "params/");
362367

363-
/// \brief Attempts to load a virtio_device_config object from a field in a JSON object
368+
/// \brief Attempts to load a flash_drive_configs object from a field in a JSON object
364369
/// \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
365370
/// \param j JSON object to load from
366371
/// \param key Key to load value from
367372
/// \param value Object to store value
368373
/// \param path Path to j
369374
template <typename K>
370-
void ju_get_opt_field(const nlohmann::json &j, const K &key, virtio_device_config &value,
375+
void ju_get_opt_field(const nlohmann::json &j, const K &key, flash_drive_configs &value,
371376
const std::string &path = "params/");
372377

373-
/// \brief Attempts to load an virtio_configs object from a field in a JSON object
378+
/// \brief Attempts to load a virtio_device_config object from a field in a JSON object
374379
/// \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
375380
/// \param j JSON object to load from
376381
/// \param key Key to load value from
377382
/// \param value Object to store value
378383
/// \param path Path to j
379384
template <typename K>
380-
void ju_get_opt_field(const nlohmann::json &j, const K &key, virtio_configs &value,
385+
void ju_get_opt_field(const nlohmann::json &j, const K &key, virtio_device_config &value,
381386
const std::string &path = "params/");
382387

383-
/// \brief Attempts to load a tlb_config object from a field in a JSON object
388+
/// \brief Attempts to load an virtio_configs object from a field in a JSON object
384389
/// \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
385390
/// \param j JSON object to load from
386391
/// \param key Key to load value from
387392
/// \param value Object to store value
388393
/// \param path Path to j
389394
template <typename K>
390-
void ju_get_opt_field(const nlohmann::json &j, const K &key, tlb_config &value, const std::string &path = "params/");
395+
void ju_get_opt_field(const nlohmann::json &j, const K &key, virtio_configs &value,
396+
const std::string &path = "params/");
391397

392398
/// \brief Attempts to load a clint_config object from a field in a JSON object
393399
/// \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
@@ -445,24 +451,24 @@ template <typename K>
445451
void ju_get_opt_field(const nlohmann::json &j, const K &key, uarch_processor_config &value,
446452
const std::string &path = "params/");
447453

448-
/// \brief Attempts to load an uarch_ram_config object from a field in a JSON object
454+
/// \brief Attempts to load an uarch_config object from a field in a JSON object
449455
/// \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
450456
/// \param j JSON object to load from
451457
/// \param key Key to load value from
452458
/// \param value Object to store value
453459
/// \param path Path to j
454460
template <typename K>
455-
void ju_get_opt_field(const nlohmann::json &j, const K &key, uarch_ram_config &value,
456-
const std::string &path = "params/");
461+
void ju_get_opt_field(const nlohmann::json &j, const K &key, uarch_config &value, const std::string &path = "params/");
457462

458-
/// \brief Attempts to load an uarch_config object from a field in a JSON object
463+
/// \brief Attempts to load an hash_tree_config object from a field in a JSON object
459464
/// \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
460465
/// \param j JSON object to load from
461466
/// \param key Key to load value from
462467
/// \param value Object to store value
463468
/// \param path Path to j
464469
template <typename K>
465-
void ju_get_opt_field(const nlohmann::json &j, const K &key, uarch_config &value, const std::string &path = "params/");
470+
void ju_get_opt_field(const nlohmann::json &j, const K &key, hash_tree_config &value,
471+
const std::string &path = "params/");
466472

467473
/// \brief Attempts to load a machine_config object from a field in a JSON object
468474
/// \tparam K Key type (explicit extern declarations for uint64_t and std::string are provided)
@@ -512,7 +518,7 @@ void ju_get_opt_field(const nlohmann::json &j, const K &key, fork_result &value,
512518
template <typename K, typename A>
513519
void ju_get_opt_vector_like_field(const nlohmann::json &j, const K &key, A &value, const std::string &path) {
514520
value.clear();
515-
if (!contains(j, key)) {
521+
if (!contains(j, key, path)) {
516522
return;
517523
}
518524
const auto &jk = j[key];
@@ -542,7 +548,7 @@ void ju_get_opt_vector_like_field(const nlohmann::json &j, const K &key, A &valu
542548
/// \detail Throws error if field is missing
543549
template <typename K, typename A>
544550
void ju_get_vector_like_field(const nlohmann::json &j, const K &key, A &value, const std::string &path = "params/") {
545-
if (!contains(j, key)) {
551+
if (!contains(j, key, path)) {
546552
throw std::invalid_argument("missing field \""s + path + to_string(key) + "\""s);
547553
}
548554
return ju_get_opt_vector_like_field(j, key, value, path);
@@ -557,7 +563,7 @@ void ju_get_vector_like_field(const nlohmann::json &j, const K &key, A &value, c
557563
template <typename T, typename K>
558564
void ju_get_opt_field(const nlohmann::json &j, const K &key, optional_param<T> &value,
559565
const std::string &path = "params/") {
560-
if (contains(j, key)) {
566+
if (contains(j, key, path)) {
561567
value.emplace();
562568
ju_get_opt_field(j, key, value.value(), path);
563569
}
@@ -572,7 +578,7 @@ void ju_get_opt_field(const nlohmann::json &j, const K &key, optional_param<T> &
572578
/// \detail Throws error if field is missing
573579
template <typename T, typename K>
574580
void ju_get_field(const nlohmann::json &j, const K &key, T &value, const std::string &path) {
575-
if (!contains(j, key)) {
581+
if (!contains(j, key, path)) {
576582
throw std::invalid_argument("missing field \""s + path + to_string(key) + "\""s);
577583
}
578584
ju_get_opt_field(j, key, value, path);
@@ -604,22 +610,22 @@ void to_json(nlohmann::json &j, const bracket_note &b);
604610
void to_json(nlohmann::json &j, const std::vector<bracket_note> &bs);
605611
void to_json(nlohmann::json &j, const std::vector<access> &as);
606612
void to_json(nlohmann::json &j, const access_log &log);
613+
void to_json(nlohmann::json &j, const backing_store_config &config);
614+
void to_json(nlohmann::json &j, const backing_store_config_only &config);
607615
void to_json(nlohmann::json &j, const memory_range_config &config);
608-
void to_json(nlohmann::json &j, const cmio_buffer_config &config);
609616
void to_json(nlohmann::json &j, const processor_config &config);
610617
void to_json(nlohmann::json &j, const flash_drive_configs &fs);
611618
void to_json(nlohmann::json &j, const virtio_device_config &config);
612619
void to_json(nlohmann::json &j, const virtio_configs &vs);
613620
void to_json(nlohmann::json &j, const ram_config &config);
614621
void to_json(nlohmann::json &j, const dtb_config &config);
615-
void to_json(nlohmann::json &j, const tlb_config &config);
616622
void to_json(nlohmann::json &j, const clint_config &config);
617623
void to_json(nlohmann::json &j, const plic_config &config);
618624
void to_json(nlohmann::json &j, const htif_config &config);
619625
void to_json(nlohmann::json &j, const cmio_config &config);
620626
void to_json(nlohmann::json &j, const uarch_processor_config &config);
621-
void to_json(nlohmann::json &j, const uarch_ram_config &config);
622627
void to_json(nlohmann::json &j, const uarch_config &config);
628+
void to_json(nlohmann::json &j, const hash_tree_config &config);
623629
void to_json(nlohmann::json &j, const machine_config &config);
624630
void to_json(nlohmann::json &j, const concurrency_runtime_config &config);
625631
void to_json(nlohmann::json &j, const htif_runtime_config &config);
@@ -731,9 +737,13 @@ extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &k
731737
const std::string &base = "params/");
732738
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, memory_range_config &value,
733739
const std::string &base = "params/");
734-
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, cmio_buffer_config &value,
740+
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, backing_store_config &value,
741+
const std::string &base = "params/");
742+
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, backing_store_config &value,
735743
const std::string &base = "params/");
736-
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, cmio_buffer_config &value,
744+
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, backing_store_config_only &value,
745+
const std::string &base = "params/");
746+
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, backing_store_config_only &value,
737747
const std::string &base = "params/");
738748
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, flash_drive_configs &value,
739749
const std::string &base = "params/");
@@ -747,10 +757,6 @@ extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &k
747757
const std::string &base = "params/");
748758
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, virtio_configs &value,
749759
const std::string &base = "params/");
750-
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, tlb_config &value,
751-
const std::string &base = "params/");
752-
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, tlb_config &value,
753-
const std::string &base = "params/");
754760
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, clint_config &value,
755761
const std::string &base = "params/");
756762
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, clint_config &value,
@@ -775,14 +781,14 @@ extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &k
775781
const std::string &base = "params/");
776782
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, uarch_processor_config &value,
777783
const std::string &base = "params/");
778-
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, uarch_ram_config &value,
779-
const std::string &base = "params/");
780-
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, uarch_ram_config &value,
781-
const std::string &base = "params/");
782784
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, uarch_config &value,
783785
const std::string &base = "params/");
784786
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, uarch_config &value,
785787
const std::string &base = "params/");
788+
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, hash_tree_config &value,
789+
const std::string &base = "params/");
790+
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, hash_tree_config &value,
791+
const std::string &base = "params/");
786792
extern template void ju_get_opt_field(const nlohmann::json &j, const uint64_t &key, machine_config &value,
787793
const std::string &base = "params/");
788794
extern template void ju_get_opt_field(const nlohmann::json &j, const std::string &key, machine_config &value,
@@ -808,11 +814,11 @@ nlohmann::json to_json(const T &v) {
808814
}
809815

810816
template <typename T>
811-
T from_json(const char *s) {
817+
T from_json(const char *s, const char *path) {
812818
T value{};
813819
if (s) {
814-
const nlohmann::json j = nlohmann::json{{"value", nlohmann::json::parse(s)}};
815-
ju_get_field(j, "value"s, value, ""s);
820+
const nlohmann::json j = nlohmann::json{{path, nlohmann::json::parse(s)}};
821+
ju_get_field(j, std::string{path}, value, ""s);
816822
}
817823
return value;
818824
}

src/machine-c-api.cpp

+12-14
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,10 @@ cm_error cm_create(cm_machine *m, const char *config, const char *runtime_config
527527
if (config == nullptr) {
528528
throw std::invalid_argument("invalid machine configuration");
529529
}
530-
const auto c = cartesi::from_json<cartesi::machine_config>(config);
530+
const auto c = cartesi::from_json<cartesi::machine_config>(config, "config");
531531
cartesi::machine_runtime_config r;
532532
if (runtime_config != nullptr) {
533-
r = cartesi::from_json<cartesi::machine_runtime_config>(runtime_config);
533+
r = cartesi::from_json<cartesi::machine_runtime_config>(runtime_config, "runtime_config");
534534
}
535535
cpp_m->create(c, r);
536536
return cm_result_success();
@@ -545,7 +545,7 @@ cm_error cm_load(cm_machine *m, const char *dir, const char *runtime_config) try
545545
}
546546
cartesi::machine_runtime_config r;
547547
if (runtime_config != nullptr) {
548-
r = cartesi::from_json<cartesi::machine_runtime_config>(runtime_config);
548+
r = cartesi::from_json<cartesi::machine_runtime_config>(runtime_config, "runtime_config");
549549
}
550550
cpp_m->load(dir, r);
551551
return cm_result_success();
@@ -707,7 +707,7 @@ cm_error cm_verify_step_uarch(const cm_machine *m, const cm_hash *root_hash_befo
707707
throw std::invalid_argument("invalid access log");
708708
}
709709
const auto cpp_log = // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
710-
cartesi::from_json<cartesi::not_default_constructible<cartesi::access_log>>(log).value();
710+
cartesi::from_json<cartesi::not_default_constructible<cartesi::access_log>>(log, "log").value();
711711
const cartesi::machine::hash_type cpp_root_hash_before = convert_from_c(root_hash_before);
712712
const cartesi::machine::hash_type cpp_root_hash_after = convert_from_c(root_hash_after);
713713
if (m != nullptr) {
@@ -727,7 +727,7 @@ cm_error cm_verify_reset_uarch(const cm_machine *m, const cm_hash *root_hash_bef
727727
throw std::invalid_argument("invalid access log");
728728
}
729729
const auto cpp_log = // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
730-
cartesi::from_json<cartesi::not_default_constructible<cartesi::access_log>>(log).value();
730+
cartesi::from_json<cartesi::not_default_constructible<cartesi::access_log>>(log, "log").value();
731731
const cartesi::machine::hash_type cpp_root_hash_before = convert_from_c(root_hash_before);
732732
const cartesi::machine::hash_type cpp_root_hash_after = convert_from_c(root_hash_after);
733733
if (m != nullptr) {
@@ -931,7 +931,7 @@ cm_error cm_set_runtime_config(cm_machine *m, const char *runtime_config) try {
931931
if (runtime_config == nullptr) {
932932
throw std::invalid_argument("invalid machine runtime configuration");
933933
}
934-
auto r = cartesi::from_json<cartesi::machine_runtime_config>(runtime_config);
934+
auto r = cartesi::from_json<cartesi::machine_runtime_config>(runtime_config, "runtime_config");
935935
auto *cpp_m = convert_from_c(m);
936936
cpp_m->set_runtime_config(r);
937937
return cm_result_success();
@@ -959,14 +959,12 @@ cm_error cm_get_default_config(const cm_machine *m, const char **config) try {
959959
return cm_result_failure();
960960
}
961961

962-
cm_error cm_replace_memory_range(cm_machine *m, uint64_t start, uint64_t length, bool shared,
963-
const char *image_filename) try {
962+
cm_error cm_replace_memory_range(cm_machine *m, const char *range_config) try {
964963
auto *cpp_m = convert_from_c(m);
965-
cartesi::memory_range_config cpp_range;
966-
cpp_range.start = start;
967-
cpp_range.length = length;
968-
cpp_range.shared = shared;
969-
cpp_range.image_filename = (image_filename != nullptr) ? image_filename : "";
964+
if (range_config == nullptr) {
965+
throw std::invalid_argument("invalid memory range configuration");
966+
}
967+
const auto cpp_range = cartesi::from_json<cartesi::memory_range_config>(range_config, "range_config");
970968
cpp_m->replace_memory_range(cpp_range);
971969
return cm_result_success();
972970
} catch (...) {
@@ -1093,7 +1091,7 @@ cm_error cm_verify_send_cmio_response(const cm_machine *m, uint16_t reason, cons
10931091
throw std::invalid_argument("invalid access log");
10941092
}
10951093
const auto cpp_log = // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1096-
cartesi::from_json<cartesi::not_default_constructible<cartesi::access_log>>(log).value();
1094+
cartesi::from_json<cartesi::not_default_constructible<cartesi::access_log>>(log, "log").value();
10971095
const cartesi::machine::hash_type cpp_root_hash_before = convert_from_c(root_hash_before);
10981096
const cartesi::machine::hash_type cpp_root_hash_after = convert_from_c(root_hash_after);
10991097
if (m != nullptr) {

0 commit comments

Comments
 (0)