diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2020-11-20 18:35:45 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-11-21 08:46:20 +0100 |
| commit | bdaddecf580cf0c89652f1152061577f2aa7458f (patch) | |
| tree | 4609e99323cac7ca8a5a72db2ecf59505bf82e7e /pkg | |
| parent | 9071fc0e22cf194e6e048dff403155cbb0205a1f (diff) | |
pkg/ifuzz/ifuzzimpl: simplify Insn interface
We don't need GetMode, GetPriv, IsCompatible in Insn interface.
Replace GetName and GetPseudo with single Info method.
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/ifuzz/ifuzz_test.go | 11 | ||||
| -rw-r--r-- | pkg/ifuzz/ifuzzimpl/ifuzzimpl.go | 6 | ||||
| -rw-r--r-- | pkg/ifuzz/powerpc/powerpc.go | 38 | ||||
| -rw-r--r-- | pkg/ifuzz/x86/encode.go | 2 | ||||
| -rw-r--r-- | pkg/ifuzz/x86/x86.go | 18 |
5 files changed, 18 insertions, 57 deletions
diff --git a/pkg/ifuzz/ifuzz_test.go b/pkg/ifuzz/ifuzz_test.go index d62048304..e82edc87a 100644 --- a/pkg/ifuzz/ifuzz_test.go +++ b/pkg/ifuzz/ifuzz_test.go @@ -69,12 +69,13 @@ func testDecode(t *testing.T, arch string) { } failed := false for _, insn := range allInsns(arch, mode, true, true) { + name, pseudo := insn.Info() text0 := insn.Encode(cfg, r) text := text0 repeat: size, err := insnset.Decode(mode, text) if err != nil { - t.Errorf("decoding %v %v failed (mode=%v): %v", insn.GetName(), hex.EncodeToString(text), mode, err) + t.Errorf("decoding %v %v failed (mode=%v): %v", name, hex.EncodeToString(text), mode, err) if len(text) != len(text0) { t.Errorf("whole: %v", hex.EncodeToString(text0)) } @@ -84,7 +85,7 @@ func testDecode(t *testing.T, arch string) { if xedEnabled { xedSize, xedErr := insnset.DecodeExt(mode, text) if xedErr != nil { - t.Errorf("xed decoding %v %v failed (mode=%v): %v", insn.GetName(), hex.EncodeToString(text), mode, xedErr) + t.Errorf("xed decoding %v %v failed (mode=%v): %v", name, hex.EncodeToString(text), mode, xedErr) if len(text) != len(text0) { t.Errorf("whole: %v", hex.EncodeToString(text0)) } @@ -93,7 +94,7 @@ func testDecode(t *testing.T, arch string) { } if size != xedSize { t.Errorf("decoding %v %v failed (mode=%v): decoded %v/%v, xed decoded %v/%v", - insn.GetName(), hex.EncodeToString(text), mode, size, xedSize, size, len(text)) + name, hex.EncodeToString(text), mode, size, xedSize, size, len(text)) if len(text) != len(text0) { t.Errorf("whole: %v", hex.EncodeToString(text0)) } @@ -101,13 +102,13 @@ func testDecode(t *testing.T, arch string) { continue } } - if insn.GetPseudo() && size >= 0 && size < len(text) { + if pseudo && size >= 0 && size < len(text) { text = text[size:] goto repeat } if size != len(text) { t.Errorf("decoding %v %v failed (mode=%v): decoded %v/%v", - insn.GetName(), hex.EncodeToString(text), mode, size, len(text)) + name, hex.EncodeToString(text), mode, size, len(text)) if len(text) != len(text0) { t.Errorf("whole: %v", hex.EncodeToString(text0)) } diff --git a/pkg/ifuzz/ifuzzimpl/ifuzzimpl.go b/pkg/ifuzz/ifuzzimpl/ifuzzimpl.go index 3de38ea59..31ca522cd 100644 --- a/pkg/ifuzz/ifuzzimpl/ifuzzimpl.go +++ b/pkg/ifuzz/ifuzzimpl/ifuzzimpl.go @@ -20,11 +20,7 @@ type ( ) type Insn interface { - GetName() string - GetMode() int - GetPseudo() bool - GetPriv() bool - IsCompatible(cfg *Config) bool + Info() (string, bool) Encode(cfg *Config, r *rand.Rand) []byte } diff --git a/pkg/ifuzz/powerpc/powerpc.go b/pkg/ifuzz/powerpc/powerpc.go index 5559c491c..5f8332510 100644 --- a/pkg/ifuzz/powerpc/powerpc.go +++ b/pkg/ifuzz/powerpc/powerpc.go @@ -102,18 +102,18 @@ func Register(insns []*Insn) { insnMap: make(map[string]*Insn), } for _, insn := range insnset.Insns { - insnset.insnMap[insn.GetName()] = insn + insnset.insnMap[insn.Name] = insn } insnset.initPseudo() for mode := ifuzzimpl.Mode(0); mode < ifuzzimpl.ModeLast; mode++ { for _, insn := range insnset.Insns { - if insn.GetMode()&(1<<uint(mode)) == 0 { + if insn.mode()&(1<<uint(mode)) == 0 { continue } - if insn.GetPseudo() { + if insn.Pseudo { insnset.modeInsns[mode][ifuzzimpl.TypeExec] = append(insnset.modeInsns[mode][ifuzzimpl.TypeExec], insn) - } else if insn.GetPriv() { + } else if insn.Priv { insnset.modeInsns[mode][ifuzzimpl.TypePriv] = append(insnset.modeInsns[mode][ifuzzimpl.TypePriv], insn) insnset.modeInsns[mode][ifuzzimpl.TypeAll] = @@ -129,37 +129,13 @@ func Register(insns []*Insn) { ifuzzimpl.Arches[ifuzzimpl.ArchPowerPC] = insnset } -func (insn Insn) GetName() string { - return insn.Name +func (insn *Insn) Info() (string, bool) { + return insn.Name, insn.Pseudo } -func (insn Insn) GetMode() int { +func (insn Insn) mode() int { if insn.M64 { return (1 << ifuzzimpl.ModeLong64) } return (1 << ifuzzimpl.ModeLong64) | (1 << ifuzzimpl.ModeProt32) } - -func (insn Insn) GetPriv() bool { - return insn.Priv -} - -func (insn Insn) GetPseudo() bool { - return insn.Pseudo -} - -func (insn Insn) IsCompatible(cfg *ifuzzimpl.Config) bool { - if cfg.Mode < 0 || cfg.Mode >= ifuzzimpl.ModeLast { - panic("bad mode") - } - if insn.Priv && !cfg.Priv { - return false - } - if insn.Pseudo && !cfg.Exec { - return false - } - if insn.M64 && ((1 << uint(cfg.Mode)) != ifuzzimpl.ModeLong64) { - return false - } - return true -} diff --git a/pkg/ifuzz/x86/encode.go b/pkg/ifuzz/x86/encode.go index 9258bc41c..536031b06 100644 --- a/pkg/ifuzz/x86/encode.go +++ b/pkg/ifuzz/x86/encode.go @@ -15,7 +15,7 @@ import ( // nolint: gocyclo, nestif, gocognit, funlen func (insn *Insn) Encode(cfg *ifuzzimpl.Config, r *rand.Rand) []byte { - if !insn.IsCompatible(cfg) { + if !insn.isCompatible(cfg) { panic("instruction is not suitable for this mode") } if insn.Pseudo { diff --git a/pkg/ifuzz/x86/x86.go b/pkg/ifuzz/x86/x86.go index 3e3ae845d..7251d1f7b 100644 --- a/pkg/ifuzz/x86/x86.go +++ b/pkg/ifuzz/x86/x86.go @@ -88,20 +88,8 @@ func (insnset *InsnSetX86) GetInsns(mode ifuzzimpl.Mode, typ ifuzzimpl.Type) []i return insnset.modeInsns[mode][typ] } -func (insn Insn) GetName() string { - return insn.Name -} - -func (insn Insn) GetMode() int { - return insn.Mode -} - -func (insn Insn) GetPriv() bool { - return insn.Priv -} - -func (insn Insn) GetPseudo() bool { - return insn.Pseudo +func (insn *Insn) Info() (string, bool) { + return insn.Name, insn.Pseudo } func generateArg(cfg *ifuzzimpl.Config, r *rand.Rand, size int) []byte { @@ -114,7 +102,7 @@ func generateArg(cfg *ifuzzimpl.Config, r *rand.Rand, size int) []byte { return arg } -func (insn Insn) IsCompatible(cfg *ifuzzimpl.Config) bool { +func (insn *Insn) isCompatible(cfg *ifuzzimpl.Config) bool { if cfg.Mode < 0 || cfg.Mode >= ifuzzimpl.ModeLast { panic("bad mode") } |
