Restrict global and function references to memory size
This commit is contained in:
@@ -12,10 +12,10 @@ func randOperand(t vm.OperandNumericType) *vm.Operand {
|
|||||||
|
|
||||||
switch op.Type {
|
switch op.Type {
|
||||||
case vm.GlobalMemoryIndex:
|
case vm.GlobalMemoryIndex:
|
||||||
op.Value = RandBiasedUint64()
|
op.Value = RandBiasedUint64n(vm.GlobalMemorySize)
|
||||||
|
|
||||||
case vm.FunctionMemoryIndex:
|
case vm.FunctionMemoryIndex:
|
||||||
op.Value = RandBiasedUint64()
|
op.Value = RandBiasedUint64n(vm.FunctionMemorySize)
|
||||||
|
|
||||||
case vm.Literal:
|
case vm.Literal:
|
||||||
switch t {
|
switch t {
|
||||||
|
|||||||
10
gen/rand.go
10
gen/rand.go
@@ -9,6 +9,16 @@ func RandBiasedUint64() uint64 {
|
|||||||
return (rand.Uint64() | 0x8000000000000000) >> rand.Intn(65)
|
return (rand.Uint64() | 0x8000000000000000) >> rand.Intn(65)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Like RandBiasedUint64() but always returns < n
|
||||||
|
func RandBiasedUint64n(n uint64) uint64 {
|
||||||
|
for {
|
||||||
|
ret := RandBiasedUint64()
|
||||||
|
if ret < n {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate a random int64 with an even distribution of the tuple
|
// Generate a random int64 with an even distribution of the tuple
|
||||||
// {sign, bits.Len64(math.Abs())}
|
// {sign, bits.Len64(math.Abs())}
|
||||||
func RandBiasedInt64() uint64 {
|
func RandBiasedInt64() uint64 {
|
||||||
|
|||||||
@@ -3,7 +3,17 @@ package test
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
import "github.com/firestuff/subcoding/gen"
|
import "github.com/firestuff/subcoding/gen"
|
||||||
|
import "github.com/firestuff/subcoding/asm"
|
||||||
|
|
||||||
func TestRandProgram(t *testing.T) {
|
func TestRandProgram(t *testing.T) {
|
||||||
gen.RandProgram()
|
for i := 0; i < 100; i++ {
|
||||||
|
prog := gen.RandProgram()
|
||||||
|
|
||||||
|
src, err := asm.Disassemble(prog)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log(src)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package vm
|
package vm
|
||||||
|
|
||||||
|
const FunctionMemorySize = 16
|
||||||
|
|
||||||
type stackFrame struct {
|
type stackFrame struct {
|
||||||
previousFunctionIndex int64
|
previousFunctionIndex int64
|
||||||
previousInstructionIndex int64
|
previousInstructionIndex int64
|
||||||
@@ -10,6 +12,6 @@ func newStackFrame(state *State) *stackFrame {
|
|||||||
return &stackFrame{
|
return &stackFrame{
|
||||||
previousFunctionIndex: state.functionIndex,
|
previousFunctionIndex: state.functionIndex,
|
||||||
previousInstructionIndex: state.instructionIndex,
|
previousInstructionIndex: state.instructionIndex,
|
||||||
functionMemory: NewMemory(16),
|
functionMemory: NewMemory(FunctionMemorySize),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package vm
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
|
const GlobalMemorySize = 16
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
running bool
|
running bool
|
||||||
err error
|
err error
|
||||||
@@ -18,7 +20,7 @@ type State struct {
|
|||||||
func NewState(prog *Program) (*State, error) {
|
func NewState(prog *Program) (*State, error) {
|
||||||
return &State{
|
return &State{
|
||||||
program: prog,
|
program: prog,
|
||||||
globalMemory: NewMemory(16),
|
globalMemory: NewMemory(GlobalMemorySize),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user