diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2c6befa8e..1dd8dd729 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -100,7 +100,7 @@ jobs: path: | uarch-ram.bin uarch-pristine-ram.c - uarch-pristine-state-hash.cpp + uarch-pristine-hash.c cartesi-machine-v${{ env.MACHINE_EMULATOR_VERSION }}_amd64.deb cartesi-machine-v${{ env.MACHINE_EMULATOR_VERSION }}_arm64.deb @@ -686,5 +686,5 @@ jobs: artifacts/cartesi-machine-*.deb artifacts/uarch-ram.bin artifacts/uarch-pristine-ram.c - artifacts/uarch-pristine-state-hash.cpp + artifacts/uarch-pristine-hash.c uarch-logs/uarch-riscv-tests-json-logs-*.tar.gz diff --git a/.gitignore b/.gitignore index d5cf7364f..7ec0ccb3d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ build pkg third-party/downloads third-party/mongoose-* -src/compute-uarch-pristine-hash src/remote-cartesi-machine src/jsonrpc-remote-cartesi-machine src/merkle-tree-hash diff --git a/Makefile b/Makefile index 59be5a156..1e5f2f817 100644 --- a/Makefile +++ b/Makefile @@ -285,6 +285,8 @@ copy: ID=`docker create $(DOCKER_PLATFORM) $(DEBIAN_IMG)` && \ docker cp $$ID:/usr/src/emulator/$(DEB_FILENAME) . && \ docker cp $$ID:/usr/src/emulator/uarch/uarch-ram.bin . && \ + docker cp $$ID:/usr/src/emulator/uarch/uarch-pristine-ram.c . && \ + docker cp $$ID:/usr/src/emulator/uarch/uarch-pristine-hash.c . && \ docker rm $$ID check-linux-env: @@ -325,30 +327,39 @@ $(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_PATH) $(LUA_INSTALL_CPATH) install-headers: $(INC_INSTALL_PATH) $(INSTALL_FILE) $(EMU_TO_INC) $(INC_INSTALL_PATH) -install-emulator: $(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_CPATH)/cartesi $(LUA_INSTALL_PATH)/cartesi $(IMAGES_INSTALL_PATH) - $(INSTALL_EXEC) $(EMU_TO_BIN) $(BIN_INSTALL_PATH) - $(INSTALL_EXEC) $(EMU_TO_LIB) $(LIB_INSTALL_PATH) +install-static-libs: $(LIB_INSTALL_PATH) $(INSTALL_FILE) $(EMU_TO_LIB_A) $(LIB_INSTALL_PATH) + $(STRIP_STATIC) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB_A)) + +install-shared-libs: $(LIB_INSTALL_PATH) + $(INSTALL_EXEC) $(EMU_TO_LIB) $(LIB_INSTALL_PATH) + $(SYMLINK) $(LIBCARTESI_SO) $(LIB_INSTALL_PATH)/$(LIBCARTESI) + $(SYMLINK) $(LIBCARTESI_SO_GRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_GRPC) + $(SYMLINK) $(LIBCARTESI_SO_JSONRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_JSONRPC) + $(STRIP_SHARED) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB)) + +install-lua-libs: $(LUA_INSTALL_PATH)/cartesi $(LUA_INSTALL_CPATH)/cartesi $(INSTALL_FILE) $(EMU_LUA_TO_BIN) $(LUA_INSTALL_PATH) + $(INSTALL_FILE) $(EMU_TO_LUA_PATH) $(LUA_INSTALL_PATH)/cartesi $(INSTALL_EXEC) $(EMU_TO_LUA_CPATH) $(LUA_INSTALL_CPATH) $(INSTALL_EXEC) $(EMU_TO_LUA_CARTESI_CPATH) $(LUA_INSTALL_CPATH)/cartesi - $(INSTALL_FILE) $(EMU_TO_LUA_PATH) $(LUA_INSTALL_PATH)/cartesi + $(STRIP_SHARED) $(subst src/,$(LUA_INSTALL_CPATH)/,$(EMU_TO_LUA_CPATH)) + $(STRIP_SHARED) $(subst src/,$(LUA_INSTALL_CPATH)/,$(EMU_TO_LUA_CARTESI_CPATH)) + +install-bins: $(BIN_INSTALL_PATH) + $(INSTALL_EXEC) $(EMU_TO_BIN) $(BIN_INSTALL_PATH) + $(STRIP_BINARY) $(subst src/,$(BIN_INSTALL_PATH)/,$(EMU_TO_BIN)) + +install-lua-bins: $(BIN_INSTALL_PATH) cat tools/template/cartesi-machine.template | sed 's|ARG_LUA_PATH|$(LUA_RUNTIME_PATH)/?.lua|g;s|ARG_LUA_CPATH|$(LUA_RUNTIME_CPATH)/?.so|g;s|ARG_IMAGES_PATH|$(IMAGES_RUNTIME_PATH)|g;s|ARG_LUA_RUNTIME_PATH|$(LUA_RUNTIME_PATH)|g' > $(BIN_INSTALL_PATH)/cartesi-machine cat tools/template/cartesi-machine-stored-hash.template | sed 's|ARG_LUA_PATH|$(LUA_RUNTIME_PATH)/?.lua|g;s|ARG_LUA_CPATH|$(LUA_RUNTIME_CPATH)/?.so|g;s|ARG_LUA_RUNTIME_PATH|$(LUA_RUNTIME_PATH)|g' > $(BIN_INSTALL_PATH)/cartesi-machine-stored-hash cat tools/template/rollup-memory-range.template | sed 's|ARG_LUA_PATH|$(LUA_RUNTIME_PATH)/?.lua|g;s|ARG_LUA_CPATH|$(LUA_RUNTIME_CPATH)/?.so|g;s|ARG_LUA_RUNTIME_PATH|$(LUA_RUNTIME_PATH)|g' > $(BIN_INSTALL_PATH)/rollup-memory-range $(CHMOD_EXEC) $(BIN_INSTALL_PATH)/cartesi-machine $(BIN_INSTALL_PATH)/cartesi-machine-stored-hash $(BIN_INSTALL_PATH)/rollup-memory-range - $(SYMLINK) $(LIBCARTESI_SO) $(LIB_INSTALL_PATH)/$(LIBCARTESI) - $(SYMLINK) $(LIBCARTESI_SO_GRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_GRPC) - $(SYMLINK) $(LIBCARTESI_SO_JSONRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_JSONRPC) - $(INSTALL_DIR) tools/gdb $(SHARE_INSTALL_PATH)/gdb -install-strip: install-emulator - $(STRIP_BINARY) $(subst src/,$(BIN_INSTALL_PATH)/,$(EMU_TO_BIN)) - $(STRIP_SHARED) $(subst src/,$(LUA_INSTALL_CPATH)/,$(EMU_TO_LUA_CPATH)) - $(STRIP_SHARED) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB)) - $(STRIP_STATIC) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB_A)) +install-shared-files: $(IMAGES_INSTALL_PATH) + $(INSTALL_DIR) tools/gdb $(SHARE_INSTALL_PATH)/gdb -install: install-strip install-headers +install: install-headers install-static-libs install-shared-libs install-lua-libs install-bins install-lua-bins install-shared-files install-uarch: install $(UARCH_INSTALL_PATH) $(INSTALL_FILE) uarch/$(UARCH_TO_SHARE) $(UARCH_INSTALL_PATH) diff --git a/src/.gitignore b/src/.gitignore index 1df370a27..7f78e35f1 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -10,6 +10,3 @@ compile_flags.txt coverage* jsonrpc-discover.cpp machine-c-version.h -uarch-pristine-state-hash.cpp -uarch-pristine-ram.c - diff --git a/src/Makefile b/src/Makefile index 224e31cf7..09800c335 100644 --- a/src/Makefile +++ b/src/Makefile @@ -36,9 +36,10 @@ GRPC_CPP_PLUGIN=$(shell which grpc_cpp_plugin) # Are we on an environment that has the riscv64-cartesi-linux-gnu-* toolchain? DEV_ENV_HAS_TOOLCHAIN ?= no -# If instead of building the microarchitecture you wamt to use a pre-built image file, modify this variable to +# If instead of building the microarchitecture you want to use a pre-built image file, modify this variable to # indicate the path of the desired file. -UARCH_RAM_IMAGE ?= ../uarch/uarch-ram.bin +UARCH_PRISTINE_RAM_C ?= ../uarch/uarch-pristine-ram.c +UARCH_PRISTINE_HASH_C ?= ../uarch/uarch-pristine-hash.c # Code instrumentation release?=no @@ -243,9 +244,7 @@ PGO_WORKLOAD=\ dhrystone 5000000; \ whetstone 25000 -# We ignore uarch-pristine-ram.c because it is generated by xxd. -# We ignore uarch-pristine-state-hash.cpp because it is generated by compute-uarch-pristine-hash. -LINTER_IGNORE_SOURCES=uarch-pristine-ram.c uarch-pristine-state-hash.cpp +LINTER_IGNORE_SOURCES= LINTER_IGNORE_HEADERS=%.pb.h LINTER_SOURCES=$(filter-out $(LINTER_IGNORE_SOURCES),$(strip $(wildcard *.cpp) $(wildcard *.c))) LINTER_HEADERS=$(filter-out $(LINTER_IGNORE_HEADERS),$(strip $(wildcard *.hpp) $(wildcard *.h))) @@ -257,7 +256,7 @@ CLANG_FORMAT=clang-format CLANG_FORMAT_UARCH_FILES:=$(wildcard ../uarch/*.cpp) CLANG_FORMAT_UARCH_FILES:=$(filter-out %uarch-printf%,$(strip $(CLANG_FORMAT_UARCH_FILES))) CLANG_FORMAT_FILES:=$(wildcard *.cpp) $(wildcard *.c) $(wildcard *.h) $(wildcard *.hpp) $(CLANG_FORMAT_UARCH_FILES) -CLANG_FORMAT_IGNORE_FILES:=uarch-pristine-ram.c uarch-pristine-state-hash.cpp +CLANG_FORMAT_IGNORE_FILES:= CLANG_FORMAT_FILES:=$(filter-out %.pb.h,$(strip $(CLANG_FORMAT_FILES))) CLANG_FORMAT_FILES:=$(filter-out $(CLANG_FORMAT_IGNORE_FILES),$(strip $(CLANG_FORMAT_FILES))) @@ -346,7 +345,8 @@ LIBCARTESI_OBJS:= \ uarch-interpret.o \ machine-c-api.o \ uarch-pristine-ram.o \ - uarch-pristine-state-hash.o + uarch-pristine-state-hash.o \ + uarch-pristine-hash.o CARTESI_CLUA_OBJS:= \ clua.o \ @@ -354,7 +354,8 @@ CARTESI_CLUA_OBJS:= \ clua-htif.o \ clua-machine-util.o \ uarch-pristine-ram.o \ - uarch-pristine-state-hash.o + uarch-pristine-state-hash.o \ + uarch-pristine-hash.o LUACARTESI_OBJS:= \ clua-cartesi.o \ @@ -381,10 +382,6 @@ LIBCARTESI_MERKLE_TREE_OBJS:= \ MERKLE_TREE_HASH_OBJS:= \ merkle-tree-hash.o -COMPUTE_UARCH_PRISTINE_HASH_OBJS := \ - compute-uarch-pristine-hash.o \ - uarch-pristine-ram.o - LIBCARTESI_GRPC_OBJS:= \ grpc-virtual-machine.o \ grpc-machine-c-api.o \ @@ -397,7 +394,8 @@ LIBCARTESI_JSONRPC_OBJS:= \ jsonrpc-machine-c-api.o \ mongoose.o \ uarch-pristine-ram.o \ - uarch-pristine-state-hash.o + uarch-pristine-state-hash.o \ + uarch-pristine-hash.o LUACARTESI_GRPC_OBJS:= \ clua-cartesi-grpc.o \ @@ -421,7 +419,8 @@ REMOTE_CARTESI_MACHINE_OBJS:= \ remote-machine.o \ slog.o \ uarch-pristine-ram.o \ - uarch-pristine-state-hash.o + uarch-pristine-state-hash.o \ + uarch-pristine-hash.o JSONRPC_REMOTE_CARTESI_MACHINE_OBJS:= \ jsonrpc-remote-machine.o \ @@ -429,7 +428,8 @@ JSONRPC_REMOTE_CARTESI_MACHINE_OBJS:= \ mongoose.o \ slog.o \ uarch-pristine-ram.o \ - uarch-pristine-state-hash.o + uarch-pristine-state-hash.o \ + uarch-pristine-hash.o ifeq ($(gperf),yes) DEFS+=-DGPERF @@ -538,9 +538,6 @@ $(PROFILE_DATA): llvm-profdata merge -output=default.profdata default*.profraw endif -compute-uarch-pristine-hash: $(COMPUTE_UARCH_PRISTINE_HASH_OBJS) libcartesi_merkle_tree.a - $(CXX) -o $@ $^ $(HASH_LIBS) $(LDFLAGS) $(EXELDFLAGS) - merkle-tree-hash: $(MERKLE_TREE_HASH_OBJS) libcartesi_merkle_tree.a $(CXX) -o $@ $^ $(HASH_LIBS) $(LDFLAGS) $(EXELDFLAGS) @@ -579,15 +576,6 @@ jsonrpc-discover.cpp: jsonrpc-discover.json echo ')json";' >> jsonrpc-discover.cpp echo '} // namespace cartesi' >> jsonrpc-discover.cpp -uarch-pristine-state-hash.cpp: compute-uarch-pristine-hash - @echo '// This file is auto-generated and should not be modified' > $@ - @echo '#include "uarch-pristine-state-hash.h"' >> $@ - @echo 'namespace cartesi {' >> $@ - @echo ' const machine_merkle_tree::hash_type uarch_pristine_state_hash{' >> $@ - @./compute-uarch-pristine-hash >> $@ - @echo ' };' >> $@ - @echo '} // namespace cartesi' >> $@ - %.clang-tidy: %.cpp $(PROTO_SOURCES) machine-c-version.h @$(CLANG_TIDY) --header-filter='$(CLANG_TIDY_HEADER_FILTER)' $< -- $(CXXFLAGS) $(LUA_INC) $(GRPC_PROTOBUF_INC) 2>/dev/null @$(CXX) $(CXXFLAGS) $(LUA_INC) $(GRPC_PROTOBUF_INC) $< -MM -MT $@ -MF $@.d > /dev/null 2>&1 @@ -604,6 +592,12 @@ mongoose.o: ../third-party/mongoose-7.12/mongoose.c sha3.o: ../third-party/tiny_sha3/sha3.c $(CC) $(CFLAGS) -c -o $@ $< +uarch-pristine-ram.o: $(UARCH_PRISTINE_RAM_C) + $(CC) $(CFLAGS) -c -o $@ $< + +uarch-pristine-hash.o: $(UARCH_PRISTINE_HASH_C) + $(CC) $(CFLAGS) -c -o $@ $< + %.o: %.cpp machine-c-version.h $(CXX) $(CXXFLAGS) -c -o $@ $< @@ -613,20 +607,21 @@ sha3.o: ../third-party/tiny_sha3/sha3.c %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< -uarch-pristine-ram.c: $(UARCH_RAM_IMAGE) - @xxd -i -n uarch_pristine_ram $< > $@ +../uarch/uarch-pristine-ram.c ../uarch/uarch-pristine-hash.c: generate-uarch-pristine -$(UARCH_RAM_IMAGE): +generate-uarch-pristine: +ifeq (,$(wildcard ../uarch/uarch-pristine-hash.c)) @if [ "$(DEV_ENV_HAS_TOOLCHAIN)" = "yes" ]; then \ $(MAKE) -C .. uarch; \ else \ $(MAKE) -C .. uarch-with-linux-env; \ fi +endif clean: clean-auto-generated clean-coverage clean-profile clean-proto-sources clean-tidy clean-libcartesi clean-executables clean-auto-generated: - @rm -f jsonrpc-discover.cpp machine-c-version.h uarch-pristine-ram.c uarch-pristine-state-hash.cpp + @rm -f jsonrpc-discover.cpp machine-c-version.h clean-proto-sources: @rm -f *.pb.cc *.pb.h diff --git a/src/clua-cartesi.cpp b/src/clua-cartesi.cpp index ffc9e6b2b..c627f8707 100644 --- a/src/clua-cartesi.cpp +++ b/src/clua-cartesi.cpp @@ -23,7 +23,7 @@ #include "machine-c-api.h" #include "riscv-constants.h" #include "uarch-constants.h" -#include "uarch-pristine-state-hash.h" +#include "uarch-pristine.h" /// \file /// \brief Scripting interface for the Cartesi SDK. @@ -130,8 +130,8 @@ CM_API int luaopen_cartesi(lua_State *L) { clua_setintegerfield(L, UARCH_RAM_LENGTH, "UARCH_RAM_LENGTH", -1); clua_setintegerfield(L, UARCH_RAM_START_ADDRESS, "UARCH_RAM_START_ADDRESS", -1); // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - clua_setlstringfield(L, reinterpret_cast(uarch_pristine_state_hash.data()), - uarch_pristine_state_hash.size(), "UARCH_PRISTINE_STATE_HASH", -1); + clua_setlstringfield(L, reinterpret_cast(uarch_pristine_hash), uarch_pristine_hash_len, + "UARCH_PRISTINE_STATE_HASH", -1); clua_setintegerfield(L, MVENDORID_INIT, "MVENDORID", -1); clua_setintegerfield(L, MARCHID_INIT, "MARCHID", -1); clua_setintegerfield(L, MIMPID_INIT, "MIMPID", -1); diff --git a/src/uarch-pristine-state-hash.cpp b/src/uarch-pristine-state-hash.cpp new file mode 100644 index 000000000..cb22da4c4 --- /dev/null +++ b/src/uarch-pristine-state-hash.cpp @@ -0,0 +1,36 @@ +// Copyright Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: LGPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) any +// later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License along +// with this program (see COPYING). If not, see . +// + +#include "uarch-pristine-state-hash.h" +#include "uarch-pristine.h" + +#include + +namespace cartesi { + +static machine_merkle_tree::hash_type get_uarch_pristine_state_hash() noexcept { + machine_merkle_tree::hash_type h; + for (std::size_t i = 0; i < h.size(); ++i) { + h[i] = uarch_pristine_hash[i]; + } + return h; +} + +/// \brief Hash of the pristine uarch state. +/// \details This hash is computed at compile time by the program compute-uarch-pristine-hash.cpp +const machine_merkle_tree::hash_type uarch_pristine_state_hash = get_uarch_pristine_state_hash(); + +} // namespace cartesi diff --git a/src/uarch-pristine.h b/src/uarch-pristine.h new file mode 100644 index 000000000..91143e92b --- /dev/null +++ b/src/uarch-pristine.h @@ -0,0 +1,32 @@ +// Copyright Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: LGPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) any +// later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License along +// with this program (see COPYING). If not, see . +// + +#ifndef UARCH_PRISTINE_H +#define UARCH_PRISTINE_H + +/// \brief Embedded pristine uarch ram image. This symbol is created by "xxd" +extern "C" const unsigned char uarch_pristine_ram[]; + +/// \brief Length of the embedded pristine uarch ram image. This symbol is created by "xxd" +extern "C" const unsigned int uarch_pristine_ram_len; + +/// \brief Embedded pristine uarch ram image. This symbol is created by "compute-uarch-pristine-hash" +extern "C" const unsigned char uarch_pristine_hash[]; + +/// \brief Length of the embedded pristine uarch ram image. This symbol is created by "compute-uarch-pristine-hash" +extern "C" const unsigned int uarch_pristine_hash_len; + +#endif // UARCH_PRISTINE_H diff --git a/src/uarch-state.h b/src/uarch-state.h index 2458fe133..de8a31cca 100644 --- a/src/uarch-state.h +++ b/src/uarch-state.h @@ -25,12 +25,7 @@ #include "pma.h" #include "riscv-constants.h" - -/// \brief Embedded pristine uarch ram image. This symbol is created by "xxd" -extern "C" const unsigned char uarch_pristine_ram[]; - -/// \brief Length of the embedded pristine uarch ram image. This symbol is created by "xxd" -extern "C" const uint32_t uarch_pristine_ram_len; +#include "uarch-pristine.h" namespace cartesi { diff --git a/uarch/.gitignore b/uarch/.gitignore index 2a673dc05..6717e095a 100644 --- a/uarch/.gitignore +++ b/uarch/.gitignore @@ -5,3 +5,6 @@ *.bin .DS_Store .vscode +uarch-pristine-hash.c +uarch-pristine-ram.c +compute-uarch-pristine-hash diff --git a/uarch/Makefile b/uarch/Makefile index 382bb3953..8468e1e38 100644 --- a/uarch/Makefile +++ b/uarch/Makefile @@ -5,14 +5,19 @@ THIRD_PARTY_DIR := ../third-party BOOST_INC_DIR = /usr/include/boost ifeq ($(UNAME),Darwin) +HOST_CXX := clang++ # Homebrew installation ifneq (,$(wildcard /usr/local/opt/boost/lib)) BOOST_INC_DIR =/usr/local/opt/boost/include else # Macports installation BOOST_INC_DIR = /opt/local/include endif +else +HOST_CXX := g++ endif +HOST_CFLAGS := -I$(THIRD_PARTY_DIR)/tiny_sha3 -I$(EMULATOR_SRC_DIR) + CC := $(TOOLCHAIN_PREFIX)gcc LD := $(TOOLCHAIN_PREFIX)ld CXX := $(TOOLCHAIN_PREFIX)g++ @@ -69,19 +74,41 @@ EMULATOR_SOURCES=\ shadow-pmas.cpp \ clint.cpp +COMPUTE_UARCH_CPP_SOURCES=\ + compute-uarch-pristine-hash.cpp \ + $(EMULATOR_SRC_DIR)/machine-merkle-tree.cpp \ + $(EMULATOR_SRC_DIR)/back-merkle-tree.cpp \ + $(EMULATOR_SRC_DIR)/pristine-merkle-tree.cpp \ + $(EMULATOR_SRC_DIR)/complete-merkle-tree.cpp \ + $(EMULATOR_SRC_DIR)/full-merkle-tree.cpp +COMPUTE_UARCH_C_SOURCES=\ + $(THIRD_PARTY_DIR)/tiny_sha3/sha3.c \ + uarch-pristine-ram.c + UARCH_OBJS = $(patsubst %.c,%.uarch_c.o,$(patsubst %.cpp,%.uarch_cpp.o,$(UARCH_SOURCES))) EMULATOR_OBJS = $(patsubst %.c,%.emulator_c.o,$(patsubst %.cpp,%.emulator_cpp.o,$(EMULATOR_SOURCES))) TARGETS=$(UARCH_OBJS) $(EMULATOR_OBJS) .PHONY: all clean -all: $(TARGETS) uarch-ram.bin +all: $(TARGETS) uarch-ram.bin uarch-pristine-ram.c uarch-pristine-hash.c + +compute-uarch-pristine-hash: $(COMPUTE_UARCH_CPP_SOURCES) $(COMPUTE_UARCH_C_SOURCES) + $(HOST_CXX) $(HOST_CFLAGS) -o $@ -x c $(COMPUTE_UARCH_C_SOURCES) -x c++ $(COMPUTE_UARCH_CPP_SOURCES) + +uarch-pristine-hash.c: compute-uarch-pristine-hash + ./compute-uarch-pristine-hash > $@ + +uarch-pristine-ram.c: uarch-ram.bin + @(echo '// This file is auto-generated and should not be modified'; \ + echo '#include '; \ + xxd -i -n uarch_pristine_ram $<) > $@ || rm $@ uarch-ram.bin: uarch-ram.elf $(OBJCOPY) -S -O binary $^ $@ uarch-ram.elf: $(EMULATOR_OBJS) $(UARCH_OBJS) uarch-ram-entry.o uarch-ram.ld - $(CXX) $(CFLAGS) $(CXXFLAGS) -Wl,-Tuarch-ram.ld -o $@ $(EMULATOR_OBJS) $(UARCH_OBJS) uarch-ram-entry.o -lgcc + $(CXX) $(CFLAGS) $(CXXFLAGS) -Wl,-Tuarch-ram.ld -o $@ $(EMULATOR_OBJS) $(UARCH_OBJS) uarch-ram-entry.o -lgcc uarch-ram-entry.o: uarch-ram-entry.S $(CC) $(CFLAGS) -c -o $@ $( $@ -clean: +clean-executables: + @rm -f compute-uarch-pristine-hash + +clean-auto-generated: + @rm -f uarch-pristine-hash.c uarch-pristine-ram.c + +clean: clean-executables clean-auto-generated @rm -f *.ld *.elf *.bin *.tmp link.ld *.o diff --git a/src/compute-uarch-pristine-hash.cpp b/uarch/compute-uarch-pristine-hash.cpp similarity index 86% rename from src/compute-uarch-pristine-hash.cpp rename to uarch/compute-uarch-pristine-hash.cpp index 4ddf82b22..59c28f2ae 100644 --- a/src/compute-uarch-pristine-hash.cpp +++ b/uarch/compute-uarch-pristine-hash.cpp @@ -18,11 +18,11 @@ #include #include -#include "machine-merkle-tree.h" -#include "shadow-uarch-state.h" -#include "uarch-constants.h" -#include "uarch-state.h" -#include "unique-c-ptr.h" +#include +#include +#include +#include +#include /// \file /// \brief This program computes the hash of the pristine uarch state ad writes it to stdout @@ -113,11 +113,24 @@ int main(int argc, char *argv[]) try { proof_type proof = tree.get_proof(UARCH_STATE_START_ADDRESS, UARCH_STATE_LOG2_SIZE, nullptr); auto &uarch_state_hash = proof.get_target_hash(); + // Print header + std::cout << "// This file is auto-generated and should not be modified" << std::endl; + // Print hash + std::cout << "unsigned char uarch_pristine_hash[] = {\n "; + int i = 0; for (auto c : uarch_state_hash) { - std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast(c) << ", "; + if (i > 0 && i % 12 == 0) { + std::cout << ",\n "; + } else if (i > 0) { + std::cout << ", "; + } + std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast(c); + i++; } - std::cout << std::endl; + std::cout << "\n};\nunsigned int uarch_pristine_hash_len = " << std::dec << uarch_state_hash.size() << ";" + << std::endl; + return 0; } catch (std::exception &e) { std::cerr << "Caught exception: " << e.what() << '\n';