Restrict global and function references to memory size

This commit is contained in:
Ian Gulliver
2021-11-20 18:12:17 -10:00
parent bdb6b17187
commit bd84a5b969
6 changed files with 35 additions and 11 deletions

View File

@@ -12,10 +12,10 @@ func randOperand(t vm.OperandNumericType) *vm.Operand {
switch op.Type {
case vm.GlobalMemoryIndex:
op.Value = RandBiasedUint64()
op.Value = RandBiasedUint64n(vm.GlobalMemorySize)
case vm.FunctionMemoryIndex:
op.Value = RandBiasedUint64()
op.Value = RandBiasedUint64n(vm.FunctionMemorySize)
case vm.Literal:
switch t {

View File

@@ -9,6 +9,16 @@ func RandBiasedUint64() uint64 {
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
// {sign, bits.Len64(math.Abs())}
func RandBiasedInt64() uint64 {