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

Forge fuzz differential parsing test #125

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: '1.22.x'
- name: Install solc
run: |
sudo apt-get update
sudo apt-get install -y python3 python3-pip
pip3 install solc-select
solc-select install 0.8.15
solc-select use 0.8.15
- name: Build FFI
run: go build
working-directory: rvgo/scripts/go-ffi
run: make build-ffi && make build-parse-diff-ffi
working-directory: rvgo
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
- name: Run foundry tests
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ bin

rvgo/bin
rvgo/scripts/go-ffi/go-ffi
rvgo/scripts/parse-diff-ffi/slow
rvgo/test/testdata

rvsol/cache
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ fuzz-mac:
fuzz \
fuzz-mac

fuzz-ffi: build
make -C ./rvgo build-parse-diff-ffi
make -C ./rvsol fuzz-ffi
.PHONY: fuzz-ffi

OP_PROGRAM_PATH ?= $(MONOREPO_ROOT)/op-program/bin-riscv/op-program-client-riscv.elf

prestate: build-rvgo op-program-riscv
Expand Down
4 changes: 4 additions & 0 deletions rvgo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ build-ffi:
env GO111MODULE=on GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) go build -v $(LDFLAGS) -o ./scripts/go-ffi/go-ffi ./scripts/go-ffi
.PHONY: build-ffi

build-parse-diff-ffi:
env GO111MODULE=on GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) go build -v $(LDFLAGS) -o ./scripts/parse-diff-ffi/slow ./scripts/parse-diff-ffi
.PHONY: build-parse-diff-ffi

clean:
rm -rf ./bin
rm ./scripts/go-ffi/go-ffi
Expand Down
95 changes: 95 additions & 0 deletions rvgo/scripts/parse-diff-ffi/parse_slow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"flag"
"fmt"

"github.com/holiman/uint256"

"github.com/ethereum-optimism/asterisc/rvgo/slow"
)

func main() {
function := flag.String("fuzz", "ParseTypeI", "fuzz function")
input := flag.Int64("number", 0, "input to parse")
flag.Parse()

number := *input

//u256_input := slow.ShortToU256(uint16(number))

toU64 := slow.U64(*uint256.NewInt(uint64(number)))

switch *function {
case "ParseTypeI":
{
resultU64 := slow.ParseImmTypeI(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseTypeS":
{
resultU64 := slow.ParseImmTypeS(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseTypeB":
{
resultU64 := slow.ParseImmTypeB(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseTypeU":
{
resultU64 := slow.ParseImmTypeU(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseTypeJ":
{
resultU64 := slow.ParseImmTypeJ(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseOpcode":
{
resultU64 := slow.ParseOpcode(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseRd":
{
resultU64 := slow.ParseRd(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseFunct3":
{
resultU64 := slow.ParseFunct3(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseRs1":
{
resultU64 := slow.ParseRs1(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseRs2":
{
resultU64 := slow.ParseRs2(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
case "ParseFunct7":
{
resultU64 := slow.ParseFunct7(toU64) // type should be U64
h := fmt.Sprintf("%064x", slow.Val(resultU64))
fmt.Print(h)
}
default:
{
panic("unknown input")
}
}
}
49 changes: 49 additions & 0 deletions rvgo/slow/slow-parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package slow

func ParseImmTypeI(instr U64) U64 {
return parseImmTypeI(instr)
}

func ParseImmTypeS(instr U64) U64 {
return parseImmTypeS(instr)
}

func ParseImmTypeB(instr U64) U64 {
return parseImmTypeB(instr)
}

func ParseImmTypeU(instr U64) U64 {
return parseImmTypeU(instr)
}

func ParseImmTypeJ(instr U64) U64 {
return parseImmTypeJ(instr)
}

func ParseOpcode(instr U64) U64 {
return parseOpcode(instr)
}

func ParseRd(instr U64) U64 {
return parseRd(instr)
}

func ParseFunct3(instr U64) U64 {
return parseFunct3(instr)
}

func ParseRs1(instr U64) U64 {
return parseRs1(instr)
}

func ParseRs2(instr U64) U64 {
return parseRs2(instr)
}

func ParseFunct7(instr U64) U64 {
return parseFunct7(instr)
}

func Val(v U64) uint64 {
return v.val()
}
8 changes: 6 additions & 2 deletions rvsol/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ test:
.PHONY: test

lint-fix:
forge fmt
forge fmt ./**/*.sol
.PHONY: lint-fix

lint-check:
forge fmt && git diff --exit-code
forge fmt ./**/*.sol && git diff --exit-code
.PHONY: lint-check

fuzz-ffi:
forge test --match-path ./test/Yul64Test.sol
.PHONY: fuzz-ffi
33 changes: 33 additions & 0 deletions rvsol/src/YulDeployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pragma solidity 0.8.15;

import "forge-std/Test.sol";

contract YulDeployer is Test {
/**
* @notice Deploys a Yul contract and returns the address where the contract was deployed
* @param fileName - The file name of the Yul contract (e.g., "Example.yul" becomes "Example")
* @return deployedAddress - The address where the contract was deployed
*/
function deployContract(string memory fileName) public returns (address) {
string memory bashCommand = string.concat(
'cast abi-encode "f(bytes)" $(solc --strict-assembly ./src/yul/',
string.concat(fileName, ".yul --bin | grep '^[0-9a-fA-Z]*$')")
);

string[] memory inputs = new string[](3);
inputs[0] = "bash";
inputs[1] = "-c";
inputs[2] = bashCommand;

bytes memory bytecode = abi.decode(vm.ffi(inputs), (bytes));

address deployedAddress;
assembly {
deployedAddress := create(0, add(bytecode, 0x20), mload(bytecode))
}

require(deployedAddress != address(0), "YulDeployer could not deploy contract");

return deployedAddress;
}
}
Loading
Loading