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

Add UArchReset test case #64

Merged
merged 8 commits into from
Apr 24, 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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly-5be158ba6dc7c798a6f032026fe60fc01686b33b
version: nightly-008922d5165c764859bc540d7298045eebf5bc60

- run: forge build
- run: forge fmt --check
- run: forge fmt --check src test
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly-5be158ba6dc7c798a6f032026fe60fc01686b33b
version: nightly-008922d5165c764859bc540d7298045eebf5bc60

- name: Run all tests
run: make test-all
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ test/uarch-log
.vscode
*.log
test/UArchReplay_*.t.sol
src/UArchConstants.sol
src/UArchStep.sol
src/UArchReset.sol
.DS_Store
87 changes: 42 additions & 45 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
EMULATOR_DIR ?= ../emulator
TEST_DIR := test
DOWNLOADDIR := downloads
READY_SRC_DIR := ready_src
SRC_DIR := src

EMULATOR_VERSION ?= v0.16.1
EMULATOR_VERSION ?= v0.17.0

TESTS_DATA_FILE ?= cartesi-machine-tests-data-$(EMULATOR_VERSION).deb
TESTS_DATA_DOWNLOAD_URL := https://github.com/cartesi/machine-emulator/releases/download/$(EMULATOR_VERSION)/$(TESTS_DATA_FILE)
Expand All @@ -16,7 +16,8 @@ LOG_DOWNLOAD_FILEPATH := $(DOWNLOADDIR)/$(LOG_TEST_FILE)
LOG_TEST_DIR := $(TEST_DIR)/uarch-log

DOWNLOADFILES := $(TESTS_DATA_DOWNLOAD_FILEPATH) $(LOG_DOWNLOAD_FILEPATH)
GENERATEDFILES := $(READY_SRC_DIR)/*.sol
GENERATEDFILES := $(SRC_DIR)/*.sol
DEPDIRS = $(TESTS_DATA_DIR) $(LOG_TEST_DIR)

help:
@echo 'Cleaning targets:'
Expand All @@ -36,72 +37,42 @@ help:
@echo ' test-prod - test production code'
@echo ' test-replay - test log files'

$(TESTS_DATA_DOWNLOAD_FILEPATH):
@mkdir -p $(DOWNLOADDIR)
@wget -nc $(TESTS_DATA_DOWNLOAD_URL) -P $(DOWNLOADDIR)

$(LOG_DOWNLOAD_FILEPATH):
@mkdir -p $(DOWNLOADDIR)
@wget -nc $(LOG_DOWNLOAD_URL) -P $(DOWNLOADDIR)

all: build test-all

build: generate-step generate-reset generate-constants
forge build --use 0.8.21
build: generate-step generate-reset generate-constants generate-prod
forge build --use 0.8.21

clean:
rm -rf src/UArchConstants.sol src/UArchStep.sol test/UArchReplay_*.t.sol
rm -rf $(TESTS_DATA_DIR) $(LOG_TEST_DIR) $(DOWNLOADDIR)
rm -rf test/UArchReplay_*.t.sol
rm -rf $(DEPDIRS) $(DOWNLOADDIR)
forge clean

shasum-download: $(DOWNLOADFILES)
shasum -a 256 $^ > $@

shasum-generated: $(GENERATEDFILES)
shasum -a 256 $^ > $@

checksum-download: $(DOWNLOADFILES)
@shasum -ca 256 shasum-download

checksum-mock:
@shasum -ca 256 shasum-mock

checksum-prod:
@shasum -ca 256 shasum-prod

pretest: checksum-download
mkdir -p $(TESTS_DATA_DIR) $(LOG_TEST_DIR)
ar p $(TESTS_DATA_DOWNLOAD_FILEPATH) data.tar.xz | tar -xJf - --strip-components=7 -C $(TESTS_DATA_DIR) ./usr/share/cartesi-machine/tests/data/uarch
tar -xzf $(LOG_DOWNLOAD_FILEPATH) --strip-components=1 -C $(LOG_TEST_DIR)
rm -f $(TESTS_DATA_DIR)/*.dump $(TESTS_DATA_DIR)/*.elf

test-all:
$(MAKE) test-mock
$(MAKE) test-prod
$(MAKE) test-replay
$(MAKE) test-prod

test-mock: pretest
test-mock: dep
$(MAKE) generate-mock
forge test --use 0.8.21 -vv --match-contract UArchInterpret

test-prod: pretest
test-prod: dep
$(MAKE) generate-prod
forge test --use 0.8.21 -vv --no-match-contract "UArchInterpret|UArchReplay"
forge test --use 0.8.21 -vv --no-match-contract "UArchInterpret|UArchReplay|UArchReset"

test-replay: pretest
test-replay: dep
$(MAKE) generate-prod
$(MAKE) generate-replay
forge test --use 0.8.21 -vv --match-contract UArchReplay
forge test --use 0.8.21 -vv --match-contract "UArchReplay|UArchReset"

generate-mock:
./helper_scripts/generate_AccessLogs.sh mock
$(MAKE) fmt
$(MAKE) checksum-mock

generate-prod:
./helper_scripts/generate_AccessLogs.sh prod
$(MAKE) fmt
$(MAKE) checksum-prod

generate-replay:
./helper_scripts/generate_ReplayTests.sh
Expand All @@ -117,9 +88,35 @@ generate-reset: $(EMULATOR_DIR)/src/uarch-reset-state.cpp
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_UArchReset.sh

fmt:
forge fmt
forge fmt src test


download: $(DOWNLOADDIR)

dep: $(DEPDIRS)

$(DOWNLOADDIR):
@mkdir -p $(DOWNLOADDIR)
@wget -nc $(TESTS_DATA_DOWNLOAD_URL) -P $(DOWNLOADDIR)
@wget -nc $(LOG_DOWNLOAD_URL) -P $(DOWNLOADDIR)
$(MAKE) checksum-download

shasum-download:
shasum -a 256 $(DOWNLOADFILES) > shasum-download

checksum-download:
@shasum -ca 256 shasum-download

$(TESTS_DATA_DIR): | download
@mkdir -p $(TESTS_DATA_DIR)
@ar p $(TESTS_DATA_DOWNLOAD_FILEPATH) data.tar.xz | tar -xJf - --strip-components=7 -C $(TESTS_DATA_DIR) ./usr/share/cartesi-machine/tests/data/uarch
@rm -f $(TESTS_DATA_DIR)/*.dump $(TESTS_DATA_DIR)/*.elf

$(LOG_TEST_DIR): | download
@mkdir -p $(LOG_TEST_DIR)
@tar -xzf $(LOG_DOWNLOAD_FILEPATH) --strip-components=1 -C $(LOG_TEST_DIR)

submodules:
git submodule update --init --recursive

.PHONY: help all build clean checksum-download checksum-mock checksum-prod fmt generate-mock generate-prod generate-replay generate-step pretest submodules test-all test-mock test-prod test-replay generate-constants generate-reset
.PHONY: help all build clean checksum-download shasum-download fmt generate-mock generate-prod generate-replay generate-step pretest submodules test-all test-mock test-prod test-replay generate-constants generate-reset
20 changes: 9 additions & 11 deletions helper_scripts/generate_AccessLogs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ if [ "$1" == "-h" ] || ([ "$1" != "prod" ] && [ "$1" != "mock" ] && [ $# -gt 0 ]
fi

if [ "$1" == "prod" ] || [ $# -eq 0 ]; then
find src -type f -name '*.sol' | $SED 's/src\///' | xargs -I {} gpp \
-U "" "" "(" "," ")" "(" ")" "//:#" "\\" \
-M "//:#" "\n" " " " " "\n" "(" ")" \
-I "src" \
src/{} -o ready_src/{}
gpp -U "" "" "(" "," ")" "(" ")" "//:#" "\\" \
-M "//:#" "\n" " " " " "\n" "(" ")" \
-I "templates" \
templates/AccessLogs.sol.template -o src/AccessLogs.sol
else
find src -type f -name '*.sol' | $SED 's/src\///' | xargs -I {} gpp \
-U "" "" "(" "," ")" "(" ")" "//:#" "\\" \
-M "//:#" "\n" " " " " "\n" "(" ")" \
-I "src" \
src/{} -o ready_src/{} \
-Dtest
gpp -U "" "" "(" "," ")" "(" ")" "//:#" "\\" \
-M "//:#" "\n" " " " " "\n" "(" ")" \
-I "templates" \
templates/AccessLogs.sol.template -o src/AccessLogs.sol \
-Dtest
fi
2 changes: 2 additions & 0 deletions helper_scripts/generate_UArchConstants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ out:write(' uint64 constant UARCH_RAM_LENGTH = 0x' .. hex(cartesi.UARCH_RAM_L
out:write(' uint64 constant RESET_POSITION = 0x' .. hex(cartesi.UARCH_STATE_START_ADDRESS) .. ';\n')
out:write(' uint8 constant RESET_ALIGNED_SIZE = ' .. cartesi.UARCH_STATE_LOG2_SIZE .. ';\n')
out:write(' bytes32 constant PRESTINE_STATE = 0x' .. hexstring(cartesi.UARCH_PRISTINE_STATE_HASH) .. ';\n')
out:write(' uint64 constant UARCH_ECALL_FN_HALT = ' .. cartesi.UARCH_ECALL_FN_HALT .. ';\n')
out:write(' uint64 constant UARCH_ECALL_FN_PUTCHAR = ' .. cartesi.UARCH_ECALL_FN_PUTCHAR .. ';\n')

out:close()
13 changes: 7 additions & 6 deletions helper_scripts/generate_UArchStep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ CPP_STEP_H_PATH=${EMULATOR_DIR}"/src/uarch-step.h"
TEMPLATE_FILE="./templates/UArchStep.sol.template"
TARGET_FILE="src/UArchStep.sol"
COMPAT_FILE="src/UArchCompat.sol"
CONSTANTS_FILE="src/UArchConstants.sol"
KEYWORD_START="START OF AUTO-GENERATED CODE"
KEYWORD_END="END OF AUTO-GENERATED CODE"

# function with to be internal
INTERNAL_FN="step"
# function with unused variable, to silence warning
UNUSED_INSN_FN="executeFENCE"

# grab head and tail of the template
start=`cat "$TEMPLATE_FILE" | grep "$KEYWORD_START" -n | grep -Eo "[0-9]*"`
Expand All @@ -37,24 +36,26 @@ h_src=`echo "enum ${BASH_REMATCH[1]} {${BASH_REMATCH[2]}}"`
COMPAT_FNS=`cat $COMPAT_FILE | grep -o "function [^(]*(" | $SED "s/function//g" | $SED "s/(//g"`
COMPAT_FNS=`echo $COMPAT_FNS | $SED -E "s/( |\n)/|/g"`

# get constant names from UArchConstants.sol
CONSTANTS=`cat $CONSTANTS_FILE | grep -E -o 'constant\s+[^ ]*' | $SED -E "s/constant//g; s/ //g" | tr '\n' '|' | sed "s/.$//"`

cpp_src=`cat "$CPP_STEP_PATH"`
pattern="namespace cartesi \{(.*)\}"
[[ $cpp_src =~ $pattern ]]
# replace cpp specific syntaxes with solidity ones
cpp_src=`echo "${BASH_REMATCH[1]}" \
| $SED "/template/d" \
| $SED "/dumpInsn/d" \
| $SED "/note/d" \
| $SED "/note = a.make_scoped_note/d" \
| $SED "/(void) note/d" \
| $SED "s/constexpr//g" \
| $SED "s/UarchState &a/AccessLogs.Context memory a/g" \
| $SED "s/throw std::runtime_error/revert/g" \
| $SED "s/::/./g" \
| $SED "s/UINT64_MAX/type(uint64).max/g" \
| $SED -E "s/UArchStepStatus uarch_step/static inline UArchStepStatus step/g" \
| $SED -E "s/static inline (\w+) ($INTERNAL_FN)\(([^\n]*)\) \{/function \2\(\3\) internal pure returns \(\1\)\{/g" \
| $SED -E "s/static inline (\w+) (\w+)\(([^\n]*)\) \{/function \2\(\3\) private pure returns \(\1\)\{/g" \
| $SED -E "s/([^\n]*) $UNUSED_INSN_FN([^\n]*) uint32 insn,([^\n]*)/\1 $UNUSED_INSN_FN\2 uint32,\3/g" \
| $SED -E "s/($COMPAT_FNS)/UArchCompat.\1/g" \
| $SED -E "s/([^a-zA-Z])($CONSTANTS)([^a-zA-Z])/UArchConstants.\1\2\3/g" \
| $SED "s/ returns (void)//g"`

# compose the solidity file from all components
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"cartesi"
],
"files": [
"src/*",
"ready_src/*"
"templates/*",
"src/*"
],
"license": "Apache-2.0"
}
121 changes: 0 additions & 121 deletions ready_src/Buffer.sol

This file was deleted.

Loading
Loading