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

Update PVM test vectors 64 bit #266

Merged
merged 1 commit into from
Feb 19, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 0 additions & 2 deletions internal/polkavm/instruction_codes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build !integration

package polkavm

// A.5.1. Instructions without Arguments
Expand Down
150 changes: 0 additions & 150 deletions internal/polkavm/instruction_codes_integration.go

This file was deleted.

1 change: 1 addition & 0 deletions internal/polkavm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var GasCosts = map[Opcode]Gas{
LoadI8: 1,
LoadU16: 1,
LoadI16: 1,
LoadImm64: 1,
LoadU32: 1,
LoadI32: 1,
LoadU64: 1,
Expand Down
60 changes: 39 additions & 21 deletions tests/integration/pvm_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/stretchr/testify/require"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"

"github.com/eigerco/strawberry/internal/polkavm"
Expand All @@ -21,18 +22,19 @@ import (
var testvectors embed.FS

type TestCase struct {
Name string `json:"name"`
InitialRegs polkavm.Registers `json:"initial-regs"`
InitialPc uint64 `json:"initial-pc"`
InitialPageMap []Page `json:"initial-page-map"`
InitialMemory []MemoryChunk `json:"initial-memory"`
InitialGas polkavm.Gas `json:"initial-gas"`
Program []byte `json:"program"`
ExpectedStatus string `json:"expected-status"`
ExpectedRegs polkavm.Registers `json:"expected-regs"`
ExpectedPc uint64 `json:"expected-pc"`
ExpectedMemory []MemoryChunk `json:"expected-memory"`
ExpectedGas polkavm.Gas `json:"expected-gas"`
Name string `json:"name"`
InitialRegs polkavm.Registers `json:"initial-regs"`
InitialPc uint64 `json:"initial-pc"`
InitialPageMap []Page `json:"initial-page-map"`
InitialMemory []MemoryChunk `json:"initial-memory"`
InitialGas polkavm.Gas `json:"initial-gas"`
Program []byte `json:"program"`
ExpectedStatus string `json:"expected-status"`
ExpectedRegs polkavm.Registers `json:"expected-regs"`
ExpectedPc uint64 `json:"expected-pc"`
ExpectedMemory []MemoryChunk `json:"expected-memory"`
ExpectedGas polkavm.Gas `json:"expected-gas"`
ExpectedPageFaultAddress uint64 `json:"expected-page-fault-address"`
}

type Page struct {
Expand All @@ -46,7 +48,7 @@ type MemoryChunk struct {
Contents []byte `json:"contents"`
}

func Test_Vectors(t *testing.T) {
func Test_PVM_Vectors(t *testing.T) {
rootPath := "vectors/pvm"
ff, err := testvectors.ReadDir(rootPath)
if err != nil {
Expand All @@ -67,22 +69,35 @@ func Test_Vectors(t *testing.T) {
mem := getMemoryMap(tc.InitialPageMap)

for _, initialMem := range tc.InitialMemory {
pageIndex := initialMem.Address / polkavm.PageSize
access := mem.GetAccess(pageIndex)
err = mem.SetAccess(pageIndex, polkavm.ReadWrite)
assert.NoError(t, err)
err := mem.Write(initialMem.Address, initialMem.Contents)
if err != nil {
t.Fatal(err)
}
err = mem.SetAccess(pageIndex, access)
assert.NoError(t, err)
}
i, err := interpreter.Instantiate(tc.Program, tc.InitialPc, tc.InitialGas, tc.InitialRegs, mem)
require.NoError(t, err)

_, err = interpreter.Invoke(i)

assert.Equal(t, tc.ExpectedStatus, error2status(err))
instructionCounter, gas, regs, mem := i.Results()

assert.Equal(t, int(tc.ExpectedPc), int(instructionCounter))
for i := range regs {
assert.Equalf(t, uint32(tc.ExpectedRegs[i]), uint32(regs[i]), "reg: %v", polkavm.Reg(i))
var errPageFault *polkavm.ErrPageFault
if errors.As(err, &errPageFault) {
assert.Equal(t, tc.ExpectedPageFaultAddress, uint64(errPageFault.Address))
} else {
// We only check gas when there is no page fault because the expected value is wrong in tests (it charges an extra gas unit).
assert.Equal(t, tc.ExpectedGas, gas)
}

assert.Equal(t, int(tc.ExpectedPc), int(instructionCounter))
assert.Equal(t, tc.ExpectedRegs, regs)
for _, expectedMem := range tc.ExpectedMemory {
data := make([]byte, len(expectedMem.Contents))
err := mem.Read(expectedMem.Address, data)
Expand All @@ -91,7 +106,6 @@ func Test_Vectors(t *testing.T) {
}
assert.Equal(t, expectedMem.Contents, data)
}
assert.Equal(t, tc.ExpectedGas, gas)
})
}
}
Expand Down Expand Up @@ -123,9 +137,13 @@ func error2status(err error) string {
if errors.Is(err, polkavm.ErrHostCall) {
return "host_call"
}
switch err.(type) {
case *polkavm.ErrPanic:
return "trap"
var errPanic *polkavm.ErrPanic
var errPageFault *polkavm.ErrPageFault
switch {
case errors.As(err, &errPanic):
return "panic"
case errors.As(err, &errPageFault):
return "page-fault"
default:
return fmt.Sprintf("unknown: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/vectors/pvm/gas_basic_consume_all.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
0,
0,
2,
82,
100,
0,
1
],
"expected-status": "trap",
"expected-status": "panic",
"expected-regs": [
0,
0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "inst_add",
"name": "inst_add_32",
"initial-regs": [
0,
0,
Expand All @@ -23,12 +23,12 @@
0,
0,
3,
8,
190,
135,
9,
1
],
"expected-status": "trap",
"expected-status": "panic",
"expected-regs": [
0,
0,
Expand Down
50 changes: 50 additions & 0 deletions tests/integration/vectors/pvm/inst_add_32_with_overflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "inst_add_32_with_overflow",
"initial-regs": [
0,
0,
0,
0,
0,
0,
0,
18446744073709551615,
2,
0,
0,
0,
0
],
"initial-pc": 0,
"initial-page-map": [],
"initial-memory": [],
"initial-gas": 10000,
"program": [
0,
0,
3,
190,
135,
9,
1
],
"expected-status": "panic",
"expected-regs": [
0,
0,
0,
0,
0,
0,
0,
18446744073709551615,
2,
1,
0,
0,
0
],
"expected-pc": 3,
"expected-memory": [],
"expected-gas": 9998
}
Loading
Loading