diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2020-11-20 17:30:23 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-11-21 08:46:20 +0100 |
| commit | 5405d2e2ed019de7452677eacfc7de9562a8ea12 (patch) | |
| tree | 7b2f9f488a4b57ce9a40e4d16bc263c2f00d8e48 /pkg/ifuzz/x86 | |
| parent | 9bc78a846441516a33a7fd3b245380f463ba88ed (diff) | |
pkg/ifuzz: invert ifuzz and ifuzzimpl
ifuzzimpl imports the public interface package ifuzz
and prog package needs to import ifuzzimpl (implementation guts that
nobody outside of ifuzz should care about). This is not right.
Invert everything so that prog package only needs to import ifuzz
and ifuzz imports ifuzzimpl.
Diffstat (limited to 'pkg/ifuzz/x86')
| -rw-r--r-- | pkg/ifuzz/x86/decode.go | 17 | ||||
| -rw-r--r-- | pkg/ifuzz/x86/encode.go | 19 | ||||
| -rw-r--r-- | pkg/ifuzz/x86/gen/gen.go | 16 | ||||
| -rw-r--r-- | pkg/ifuzz/x86/pseudo.go | 85 | ||||
| -rw-r--r-- | pkg/ifuzz/x86/x86.go | 63 |
5 files changed, 97 insertions, 103 deletions
diff --git a/pkg/ifuzz/x86/decode.go b/pkg/ifuzz/x86/decode.go index ca611ac69..e02a3a63f 100644 --- a/pkg/ifuzz/x86/decode.go +++ b/pkg/ifuzz/x86/decode.go @@ -5,26 +5,27 @@ package x86 import ( "fmt" - "github.com/google/syzkaller/pkg/ifuzz" + + "github.com/google/syzkaller/pkg/ifuzz/ifuzzimpl" ) // Decode decodes instruction length for the given mode. // It can have falsely decode incorrect instructions, // but should not fail to decode correct instructions. // nolint: gocyclo, nestif, gocognit, funlen -func (insnset *InsnSetX86) Decode(mode int, text []byte) (int, error) { +func (insnset *InsnSetX86) Decode(mode ifuzzimpl.Mode, text []byte) (int, error) { if len(text) == 0 { return 0, fmt.Errorf("zero-length instruction") } prefixes := prefixes32 var operSize, immSize, dispSize, addrSize int switch mode { - case ifuzz.ModeLong64: + case ifuzzimpl.ModeLong64: operSize, immSize, dispSize, addrSize = 4, 4, 4, 8 prefixes = prefixes64 - case ifuzz.ModeProt32: + case ifuzzimpl.ModeProt32: operSize, immSize, dispSize, addrSize = 4, 4, 4, 4 - case ifuzz.ModeProt16, ifuzz.ModeReal16: + case ifuzzimpl.ModeProt16, ifuzzimpl.ModeReal16: operSize, immSize, dispSize, addrSize = 2, 2, 2, 2 default: panic("bad mode") @@ -35,7 +36,7 @@ func (insnset *InsnSetX86) Decode(mode int, text []byte) (int, error) { if len(text) > 1 { // There are only 2 32-bit instructions that look like VEX-prefixed but are actually not: LDS, LES. // They always reference memory (mod!=3), but all VEX instructions have "mod=3" where LDS/LES would have mod. - if (text[0] == 0xc4 || text[0] == 0xc5) && (mode == ifuzz.ModeLong64 || text[1]&0xc0 == 0xc0) { + if (text[0] == 0xc4 || text[0] == 0xc5) && (mode == ifuzzimpl.ModeLong64 || text[1]&0xc0 == 0xc0) { vex = true } // There is only one instruction that looks like XOP-prefixed but is actually not: POP. @@ -207,7 +208,7 @@ nextInsn: return 0, fmt.Errorf("unknown instruction") } -var XedDecode func(mode int, text []byte) (int, error) +var XedDecode func(mode ifuzzimpl.Mode, text []byte) (int, error) var ( prefixes32 = map[byte]bool{ @@ -225,7 +226,7 @@ var ( } ) -func (insnset *InsnSetX86) DecodeExt(mode int, text []byte) (int, error) { +func (insnset *InsnSetX86) DecodeExt(mode ifuzzimpl.Mode, text []byte) (int, error) { if XedDecode != nil && text != nil && len(text) > 0 { return XedDecode(mode, text) } diff --git a/pkg/ifuzz/x86/encode.go b/pkg/ifuzz/x86/encode.go index b73a22c14..9258bc41c 100644 --- a/pkg/ifuzz/x86/encode.go +++ b/pkg/ifuzz/x86/encode.go @@ -8,12 +8,13 @@ package x86 import ( - "github.com/google/syzkaller/pkg/ifuzz" "math/rand" + + "github.com/google/syzkaller/pkg/ifuzz/ifuzzimpl" ) // nolint: gocyclo, nestif, gocognit, funlen -func (insn *Insn) Encode(cfg *ifuzz.Config, r *rand.Rand) []byte { +func (insn *Insn) Encode(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { if !insn.IsCompatible(cfg) { panic("instruction is not suitable for this mode") } @@ -23,11 +24,11 @@ func (insn *Insn) Encode(cfg *ifuzz.Config, r *rand.Rand) []byte { var operSize, immSize, dispSize, addrSize int switch cfg.Mode { - case ifuzz.ModeLong64: + case ifuzzimpl.ModeLong64: operSize, immSize, dispSize, addrSize = 4, 4, 4, 8 - case ifuzz.ModeProt32: + case ifuzzimpl.ModeProt32: operSize, immSize, dispSize, addrSize = 4, 4, 4, 4 - case ifuzz.ModeProt16, ifuzz.ModeReal16: + case ifuzzimpl.ModeProt16, ifuzzimpl.ModeReal16: operSize, immSize, dispSize, addrSize = 2, 2, 2, 2 default: panic("bad mode") @@ -53,7 +54,7 @@ func (insn *Insn) Encode(cfg *ifuzz.Config, r *rand.Rand) []byte { if !insn.No66Prefix { prefixes = append(prefixes, 0x66) // operand size } - if cfg.Mode == ifuzz.ModeLong64 || !insn.Mem32 { + if cfg.Mode == ifuzzimpl.ModeLong64 || !insn.Mem32 { prefixes = append(prefixes, 0x67) // address size } if !insn.NoRepPrefix { @@ -70,7 +71,7 @@ func (insn *Insn) Encode(cfg *ifuzz.Config, r *rand.Rand) []byte { // REX var rex byte - if cfg.Mode == ifuzz.ModeLong64 && r.Intn(2) == 0 { + if cfg.Mode == ifuzzimpl.ModeLong64 && r.Intn(2) == 0 { // bit 0 - B // bit 1 - X // bit 2 - R @@ -118,7 +119,7 @@ func (insn *Insn) Encode(cfg *ifuzz.Config, r *rand.Rand) []byte { code = append(code, insn.Vex) vexR = byte(1) vexX = byte(1) - if cfg.Mode == ifuzz.ModeLong64 { + if cfg.Mode == ifuzzimpl.ModeLong64 { vexR = byte(r.Intn(2)) vexX = byte(r.Intn(2)) } @@ -146,7 +147,7 @@ func (insn *Insn) Encode(cfg *ifuzz.Config, r *rand.Rand) []byte { code = append(code, vexR<<7|vexX<<6|vexB<<5|insn.VexMap) code = append(code, W<<7|vvvv<<3|L<<2|pp) // TODO: short encoding - if cfg.Mode != ifuzz.ModeLong64 { + if cfg.Mode != ifuzzimpl.ModeLong64 { vvvv |= 8 } } diff --git a/pkg/ifuzz/x86/gen/gen.go b/pkg/ifuzz/x86/gen/gen.go index d1a490625..3622ba248 100644 --- a/pkg/ifuzz/x86/gen/gen.go +++ b/pkg/ifuzz/x86/gen/gen.go @@ -13,7 +13,7 @@ import ( "strconv" "strings" - "github.com/google/syzkaller/pkg/ifuzz" + "github.com/google/syzkaller/pkg/ifuzz/ifuzzimpl" "github.com/google/syzkaller/pkg/ifuzz/x86" "github.com/google/syzkaller/pkg/serializer" ) @@ -102,7 +102,7 @@ func main() { insn.Extension = vals[0] switch insn.Extension { case "FMA", "AVX2", "AVX", "F16C", "BMI2", "BMI", "XOP", "FMA4", "AVXAES", "BMI1", "AVX2GATHER": - insn.Mode = 1<<ifuzz.ModeLong64 | 1<<ifuzz.ModeProt32 + insn.Mode = 1<<ifuzzimpl.ModeLong64 | 1<<ifuzzimpl.ModeProt32 } insn.Avx2Gather = insn.Extension == "AVX2GATHER" case "PATTERN": @@ -201,7 +201,7 @@ func parsePattern(insn *x86.Insn, vals []string) error { return errSkip("") } if insn.Mode == 0 { - insn.Mode = 1<<ifuzz.ModeLast - 1 + insn.Mode = 1<<ifuzzimpl.ModeLast - 1 } insn.Mod = -100 insn.Reg = -100 @@ -314,7 +314,7 @@ func parsePattern(insn *x86.Insn, vals []string) error { // VOP/VEX case v == "XOPV": insn.Vex = 0x8f - insn.Mode &^= 1 << ifuzz.ModeReal16 + insn.Mode &^= 1 << ifuzzimpl.ModeReal16 case v == "EVV": insn.Vex = 0xc4 case v == "VV1": @@ -355,13 +355,13 @@ func parsePattern(insn *x86.Insn, vals []string) error { // Modes. case v == "mode64": - insn.Mode &= 1 << ifuzz.ModeLong64 + insn.Mode &= 1 << ifuzzimpl.ModeLong64 case v == "not64": - insn.Mode &^= 1 << ifuzz.ModeLong64 + insn.Mode &^= 1 << ifuzzimpl.ModeLong64 case v == "mode32": - insn.Mode &= 1 << ifuzz.ModeProt32 + insn.Mode &= 1 << ifuzzimpl.ModeProt32 case v == "mode16": - insn.Mode &= 1<<ifuzz.ModeProt16 | 1<<ifuzz.ModeReal16 + insn.Mode &= 1<<ifuzzimpl.ModeProt16 | 1<<ifuzzimpl.ModeReal16 case v == "eamode64", v == "eamode32", v == "eamode16", diff --git a/pkg/ifuzz/x86/pseudo.go b/pkg/ifuzz/x86/pseudo.go index 1efbcfbcc..412b5813f 100644 --- a/pkg/ifuzz/x86/pseudo.go +++ b/pkg/ifuzz/x86/pseudo.go @@ -4,18 +4,19 @@ package x86 import ( - "github.com/google/syzkaller/pkg/ifuzz" "math/rand" + + "github.com/google/syzkaller/pkg/ifuzz/ifuzzimpl" ) // nolint: funlen func (insnset *InsnSetX86) initPseudo() { insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_RDMSR", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) msr := msrs[r.Intn(len(msrs))] gen.mov32(regECX, msr) @@ -25,10 +26,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_WRMSR", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) msr := msrs[r.Intn(len(msrs))] v := generateInt(cfg, r, 8) @@ -41,10 +42,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_PCI_READ", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) addr, port, size := pciAddrPort(r) gen.out32(0xcf8, addr) @@ -54,10 +55,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_PCI_WRITE", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) addr, port, size := pciAddrPort(r) val := generateInt(cfg, r, 4) @@ -68,10 +69,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_PORT_READ", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) port := ports[r.Intn(len(ports))] gen.in(port, r.Intn(3)) @@ -80,10 +81,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_PORT_WRITE", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) port := ports[r.Intn(len(ports))] val := generateInt(cfg, r, 4) @@ -93,10 +94,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_XOR_CR", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) cr := controlRegisters[r.Intn(len(controlRegisters))] var v uint32 @@ -114,10 +115,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_XOR_EFER", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) gen.mov32(regECX, eferMSR) gen.byte(0x0f, 0x32) // rdmsr @@ -129,16 +130,16 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_SET_BREAK", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) br := uint8(r.Intn(4)) loc := uint32(r.Intn(4)) typ := uint32(r.Intn(16)) addr := generateInt(cfg, r, 8) - if cfg.Mode == ifuzz.ModeLong64 { + if cfg.Mode == ifuzzimpl.ModeLong64 { gen.mov64(regRAX, addr) } else { gen.mov32(regEAX, uint32(addr)) @@ -152,13 +153,13 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_LOAD_SEG", - Mode: 1<<ifuzz.ModeLast - 1, + Mode: 1<<ifuzzimpl.ModeLast - 1, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) sel := randSelector(r) - if cfg.Mode == ifuzz.ModeReal16 { + if cfg.Mode == ifuzzimpl.ModeReal16 { sel = uint16(generateInt(cfg, r, 8)) >> 4 } reg := uint8(r.Intn(6)) @@ -169,14 +170,14 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_FAR_JMP", - Mode: 1<<ifuzz.ModeLong64 | 1<<ifuzz.ModeProt32 | 1<<ifuzz.ModeProt16, + Mode: 1<<ifuzzimpl.ModeLong64 | 1<<ifuzzimpl.ModeProt32 | 1<<ifuzzimpl.ModeProt16, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) sel := randSelector(r) off := generateInt(cfg, r, 4) - if cfg.Mode == ifuzz.ModeLong64 { + if cfg.Mode == ifuzzimpl.ModeLong64 { gen.mov32toSPaddr(uint32(sel), 0) gen.mov32toSPaddr(uint32(off), 2) if r.Intn(2) == 0 { @@ -190,7 +191,7 @@ func (insnset *InsnSetX86) initPseudo() { } else { gen.byte(0x9a) // lcall $imm16, $imm16/32 } - if cfg.Mode == ifuzz.ModeProt16 { + if cfg.Mode == ifuzzimpl.ModeProt16 { gen.imm16(uint16(off)) } else { gen.imm32(uint32(off)) @@ -202,10 +203,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_LTR_LLDT", - Mode: 1<<ifuzz.ModeLong64 | 1<<ifuzz.ModeProt32 | 1<<ifuzz.ModeProt16, + Mode: 1<<ifuzzimpl.ModeLong64 | 1<<ifuzzimpl.ModeProt32 | 1<<ifuzzimpl.ModeProt16, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) sel := randSelector(r) gen.mov16(regAX, sel) @@ -219,10 +220,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_LGIDT", - Mode: 1<<ifuzz.ModeLong64 | 1<<ifuzz.ModeProt32 | 1<<ifuzz.ModeProt16, + Mode: 1<<ifuzzimpl.ModeLong64 | 1<<ifuzzimpl.ModeProt32 | 1<<ifuzzimpl.ModeProt16, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) limit := uint32(generateInt(cfg, r, 2)) base := uint32(generateInt(cfg, r, 4)) @@ -240,10 +241,10 @@ func (insnset *InsnSetX86) initPseudo() { }) insnset.Insns = append(insnset.Insns, &Insn{ Name: "PSEUDO_HYPERCALL", - Mode: 1<<ifuzz.ModeLong64 | 1<<ifuzz.ModeProt32 | 1<<ifuzz.ModeProt16, + Mode: 1<<ifuzzimpl.ModeLong64 | 1<<ifuzzimpl.ModeProt32 | 1<<ifuzzimpl.ModeProt16, Priv: true, Pseudo: true, - generator: func(cfg *ifuzz.Config, r *rand.Rand) []byte { + generator: func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { gen := makeGen(cfg, r) switch r.Intn(2) { case 0: @@ -280,12 +281,12 @@ const ( ) type generator struct { - mode int + mode ifuzzimpl.Mode r *rand.Rand text []byte } -func makeGen(cfg *ifuzz.Config, r *rand.Rand) *generator { +func makeGen(cfg *ifuzzimpl.Config, r *rand.Rand) *generator { return &generator{ mode: cfg.Mode, r: r, @@ -311,9 +312,9 @@ func (gen *generator) imm64(v uint64) { func (gen *generator) operand16() { switch gen.mode { - case ifuzz.ModeLong64, ifuzz.ModeProt32: + case ifuzzimpl.ModeLong64, ifuzzimpl.ModeProt32: gen.byte(0x66) - case ifuzz.ModeProt16, ifuzz.ModeReal16: + case ifuzzimpl.ModeProt16, ifuzzimpl.ModeReal16: default: panic("bad mode") } @@ -321,8 +322,8 @@ func (gen *generator) operand16() { func (gen *generator) operand32() { switch gen.mode { - case ifuzz.ModeLong64, ifuzz.ModeProt32: - case ifuzz.ModeProt16, ifuzz.ModeReal16: + case ifuzzimpl.ModeLong64, ifuzzimpl.ModeProt32: + case ifuzzimpl.ModeProt16, ifuzzimpl.ModeReal16: gen.byte(0x66) default: panic("bad mode") @@ -331,8 +332,8 @@ func (gen *generator) operand32() { func (gen *generator) addr32() { switch gen.mode { - case ifuzz.ModeLong64, ifuzz.ModeProt32: - case ifuzz.ModeProt16, ifuzz.ModeReal16: + case ifuzzimpl.ModeLong64, ifuzzimpl.ModeProt32: + case ifuzzimpl.ModeProt16, ifuzzimpl.ModeReal16: gen.byte(0x67) default: panic("bad mode") @@ -384,7 +385,7 @@ func (gen *generator) mov32(reg int, v uint32) { } func (gen *generator) mov64(reg int, v uint64) { - if gen.mode != ifuzz.ModeLong64 { + if gen.mode != ifuzzimpl.ModeLong64 { panic("bad mode") } gen.byte(0x48) diff --git a/pkg/ifuzz/x86/x86.go b/pkg/ifuzz/x86/x86.go index 1583040ad..3e3ae845d 100644 --- a/pkg/ifuzz/x86/x86.go +++ b/pkg/ifuzz/x86/x86.go @@ -7,9 +7,9 @@ package x86 import ( - "github.com/google/syzkaller/pkg/ifuzz" - "github.com/google/syzkaller/pkg/ifuzz/ifuzzimpl" "math/rand" + + "github.com/google/syzkaller/pkg/ifuzz/ifuzzimpl" ) type Insn struct { @@ -44,57 +44,48 @@ type Insn struct { VexP int8 Avx2Gather bool - generator func(cfg *ifuzz.Config, r *rand.Rand) []byte // for pseudo instructions + generator func(cfg *ifuzzimpl.Config, r *rand.Rand) []byte // for pseudo instructions } -const ( - typeExec = iota - typePriv - typeUser - typeAll - typeLast -) - type InsnSetX86 struct { - modeInsns [ifuzz.ModeLast][typeLast][]ifuzz.Insn + modeInsns [ifuzzimpl.ModeLast][ifuzzimpl.TypeLast][]ifuzzimpl.Insn Insns []*Insn } func Register(insns []*Insn) { - var insnset InsnSetX86 - - insnset.Insns = insns - if len(insnset.Insns) == 0 { + if len(insns) == 0 { panic("no instructions") } + insnset := &InsnSetX86{ + Insns: insns, + } insnset.initPseudo() - for mode := 0; mode < ifuzz.ModeLast; mode++ { + for mode := ifuzzimpl.Mode(0); mode < ifuzzimpl.ModeLast; mode++ { for _, insn := range insnset.Insns { if insn.Mode&(1<<uint(mode)) == 0 { continue } if insn.Pseudo { - insnset.modeInsns[mode][typeExec] = - append(insnset.modeInsns[mode][typeExec], ifuzz.Insn(insn)) + insnset.modeInsns[mode][ifuzzimpl.TypeExec] = + append(insnset.modeInsns[mode][ifuzzimpl.TypeExec], insn) } else if insn.Priv { - insnset.modeInsns[mode][typePriv] = - append(insnset.modeInsns[mode][typePriv], ifuzz.Insn(insn)) - insnset.modeInsns[mode][typeAll] = - append(insnset.modeInsns[mode][typeAll], ifuzz.Insn(insn)) + insnset.modeInsns[mode][ifuzzimpl.TypePriv] = + append(insnset.modeInsns[mode][ifuzzimpl.TypePriv], insn) + insnset.modeInsns[mode][ifuzzimpl.TypeAll] = + append(insnset.modeInsns[mode][ifuzzimpl.TypeAll], insn) } else { - insnset.modeInsns[mode][typeUser] = - append(insnset.modeInsns[mode][typeUser], ifuzz.Insn(insn)) - insnset.modeInsns[mode][typeAll] = - append(insnset.modeInsns[mode][typeAll], ifuzz.Insn(insn)) + insnset.modeInsns[mode][ifuzzimpl.TypeUser] = + append(insnset.modeInsns[mode][ifuzzimpl.TypeUser], insn) + insnset.modeInsns[mode][ifuzzimpl.TypeAll] = + append(insnset.modeInsns[mode][ifuzzimpl.TypeAll], insn) } } } - - ifuzzimpl.Register(ifuzz.ArchX86, ifuzz.InsnSet(&insnset)) + ifuzzimpl.Arches[ifuzzimpl.ArchX86] = insnset } -func (insnset *InsnSetX86) GetInsns(mode, insntype int) []ifuzz.Insn { - return insnset.modeInsns[mode][insntype] +func (insnset *InsnSetX86) GetInsns(mode ifuzzimpl.Mode, typ ifuzzimpl.Type) []ifuzzimpl.Insn { + return insnset.modeInsns[mode][typ] } func (insn Insn) GetName() string { @@ -113,7 +104,7 @@ func (insn Insn) GetPseudo() bool { return insn.Pseudo } -func generateArg(cfg *ifuzz.Config, r *rand.Rand, size int) []byte { +func generateArg(cfg *ifuzzimpl.Config, r *rand.Rand, size int) []byte { v := generateInt(cfg, r, size) arg := make([]byte, size) for i := 0; i < size; i++ { @@ -123,8 +114,8 @@ func generateArg(cfg *ifuzz.Config, r *rand.Rand, size int) []byte { return arg } -func (insn Insn) IsCompatible(cfg *ifuzz.Config) bool { - if cfg.Mode < 0 || cfg.Mode >= ifuzz.ModeLast { +func (insn Insn) IsCompatible(cfg *ifuzzimpl.Config) bool { + if cfg.Mode < 0 || cfg.Mode >= ifuzzimpl.ModeLast { panic("bad mode") } if insn.Priv && !cfg.Priv { @@ -139,7 +130,7 @@ func (insn Insn) IsCompatible(cfg *ifuzz.Config) bool { return true } -func generateInt(cfg *ifuzz.Config, r *rand.Rand, size int) uint64 { +func generateInt(cfg *ifuzzimpl.Config, r *rand.Rand, size int) uint64 { if size != 1 && size != 2 && size != 4 && size != 8 { panic("bad arg size") } @@ -154,7 +145,7 @@ func generateInt(cfg *ifuzz.Config, r *rand.Rand, size int) uint64 { case x < 30: v = uint64(r.Int63()) case x < 40: - v = ifuzz.SpecialNumbers[r.Intn(len(ifuzz.SpecialNumbers))] + v = ifuzzimpl.SpecialNumbers[r.Intn(len(ifuzzimpl.SpecialNumbers))] if r.Intn(5) == 0 { v += uint64(r.Intn(33)) - 16 } |
