radiance/pkg/sbf/vm.go

86 lines
1.8 KiB
Go
Raw Normal View History

2022-09-03 00:32:15 -07:00
package sbf
2022-09-04 08:55:07 -07:00
import (
"errors"
"fmt"
)
2022-09-03 00:32:15 -07:00
// VM is the virtual machine abstraction, implemented by each executor.
type VM interface {
VMContext() any
2022-09-04 08:55:07 -07:00
Read(addr uint64, p []byte) error
Read8(addr uint64) (uint8, error)
Read16(addr uint64) (uint16, error)
Read32(addr uint64) (uint32, error)
Read64(addr uint64) (uint64, error)
Write(addr uint64, p []byte) error
Write8(addr uint64, x uint8) error
Write16(addr uint64, x uint16) error
Write32(addr uint64, x uint32) error
Write64(addr uint64, x uint64) error
2022-09-03 00:32:15 -07:00
}
// VMOpts specifies virtual machine parameters.
type VMOpts struct {
2022-09-04 08:55:07 -07:00
// Machine parameters
2022-09-04 15:28:39 -07:00
HeapSize int
Syscalls SyscallRegistry
2022-09-05 00:39:40 -07:00
Tracer TraceSink
2022-09-04 08:55:07 -07:00
// Execution parameters
Context any // passed to syscalls
2022-09-04 15:28:39 -07:00
MaxCU int
2022-09-04 08:55:07 -07:00
Input []byte // mapped at VaddrInput
2022-09-03 00:32:15 -07:00
}
2022-09-04 12:49:44 -07:00
type Exception struct {
PC int64
Detail error
}
func (e *Exception) Error() string {
return fmt.Sprintf("exception at %d: %s", e.PC, e.Detail)
}
func (e *Exception) Unwrap() error {
return e.Detail
2022-09-03 00:32:15 -07:00
}
// Exception codes.
var (
ExcDivideByZero = errors.New("division by zero")
ExcDivideOverflow = errors.New("divide overflow")
2022-09-04 12:49:44 -07:00
ExcOutOfCU = errors.New("compute unit overrun")
ExcCallDepth = errors.New("call depth exceeded")
2022-09-03 00:32:15 -07:00
)
2022-09-04 08:55:07 -07:00
type ExcBadAccess struct {
Addr uint64
Size uint32
Write bool
Reason string
}
func NewExcBadAccess(addr uint64, size uint32, write bool, reason string) ExcBadAccess {
return ExcBadAccess{
Addr: addr,
Size: size,
Write: write,
Reason: reason,
}
}
func (e ExcBadAccess) Error() string {
return fmt.Sprintf("bad memory access at %#x (size=%d write=%v), reason: %s", e.Addr, e.Size, e.Write, e.Reason)
}
2022-09-04 15:28:39 -07:00
type ExcCallDest struct {
Imm uint32
}
func (e ExcCallDest) Error() string {
return fmt.Sprintf("unknown symbol or syscall 0x%08x", e.Imm)
}