From e72f8f11e096d36aefc41a35c718dced97c45dea Mon Sep 17 00:00:00 2001 From: Alexey Kardashevskiy Date: Wed, 2 Sep 2020 18:11:22 +1000 Subject: pkg/ifuzz: reorganize files to allow other architectures At the moment ifuzz only generates x86 instructions. In order to support instruction fuzzing for others (ARM, POWERPC), some separation of the common and arch layers is needed. This adds 2 packages: 1. "x86" where x86 instruction generator goes to 2. "ifuzzimpl which contains some common code. The goal was to keep changes to the rand.go to the minimum. The next patch will use this when adding PPC64. This should cause no behavioural change. Signed-off-by: Alexey Kardashevskiy --- pkg/ifuzz/ifuzz_test.go | 54 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'pkg/ifuzz/ifuzz_test.go') diff --git a/pkg/ifuzz/ifuzz_test.go b/pkg/ifuzz/ifuzz_test.go index 90ca2a440..a5052e581 100644 --- a/pkg/ifuzz/ifuzz_test.go +++ b/pkg/ifuzz/ifuzz_test.go @@ -10,21 +10,23 @@ import ( "testing" "time" - . "github.com/google/syzkaller/pkg/ifuzz" - _ "github.com/google/syzkaller/pkg/ifuzz/generated" + "github.com/google/syzkaller/pkg/ifuzz" + "github.com/google/syzkaller/pkg/ifuzz/ifuzzimpl" + _ "github.com/google/syzkaller/pkg/ifuzz/x86/generated" ) -func TestMode(t *testing.T) { - all := make(map[*Insn]bool) - for mode := 0; mode < ModeLast; mode++ { +func testmodearch(t *testing.T, arch string) { + all := make(map[ifuzz.Insn]bool) + for mode := 0; mode < ifuzz.ModeLast; mode++ { for priv := 0; priv < 2; priv++ { for exec := 0; exec < 2; exec++ { - cfg := &Config{ + cfg := &ifuzz.Config{ + Arch: arch, Mode: mode, Priv: priv != 0, Exec: exec != 0, } - insns := ModeInsns(cfg) + insns := ifuzzimpl.ModeInsns(cfg) t.Logf("mode=%v priv=%v exec=%v: %v instructions", mode, priv, exec, len(insns)) for _, insn := range insns { all[insn] = true @@ -35,7 +37,16 @@ func TestMode(t *testing.T) { t.Logf("total: %v instructions", len(all)) } -func TestDecode(t *testing.T) { +func TestMode(t *testing.T) { + testmodearch(t, ifuzz.ArchX86) +} + +func testdecodearch(t *testing.T, arch string) { + insnset := ifuzzimpl.Types[arch] + xedEnabled := false + if _, err := insnset.DecodeExt(0, nil); err == nil { + xedEnabled = true + } seed := time.Now().UnixNano() if os.Getenv("CI") != "" { seed = 0 // required for deterministic coverage reports @@ -44,30 +55,31 @@ func TestDecode(t *testing.T) { r := rand.New(rand.NewSource(seed)) for repeat := 0; repeat < 10; repeat++ { - for mode := 0; mode < ModeLast; mode++ { - cfg := &Config{ + for mode := 0; mode < ifuzz.ModeLast; mode++ { + cfg := &ifuzz.Config{ + Arch: arch, Mode: mode, Priv: true, Exec: true, } failed := false - for _, insn := range ModeInsns(cfg) { + for _, insn := range ifuzzimpl.ModeInsns(cfg) { text0 := insn.Encode(cfg, r) text := text0 repeat: - size, err := Decode(mode, text) + size, err := insnset.Decode(mode, text) if err != nil { - t.Errorf("decoding %v %v failed (mode=%v): %v", insn.Name, hex.EncodeToString(text), mode, err) + t.Errorf("decoding %v %v failed (mode=%v): %v", insn.GetName(), hex.EncodeToString(text), mode, err) if len(text) != len(text0) { t.Errorf("whole: %v", hex.EncodeToString(text0)) } failed = true continue } - if XedDecode != nil { - xedSize, xedErr := XedDecode(mode, text) + if xedEnabled { + xedSize, xedErr := insnset.DecodeExt(mode, text) if xedErr != nil { - t.Errorf("xed decoding %v %v failed (mode=%v): %v", insn.Name, hex.EncodeToString(text), mode, xedErr) + t.Errorf("xed decoding %v %v failed (mode=%v): %v", insn.GetName(), hex.EncodeToString(text), mode, xedErr) if len(text) != len(text0) { t.Errorf("whole: %v", hex.EncodeToString(text0)) } @@ -76,7 +88,7 @@ func TestDecode(t *testing.T) { } if size != xedSize { t.Errorf("decoding %v %v failed (mode=%v): decoded %v/%v, xed decoded %v/%v", - insn.Name, hex.EncodeToString(text), mode, size, xedSize, size, len(text)) + insn.GetName(), hex.EncodeToString(text), mode, size, xedSize, size, len(text)) if len(text) != len(text0) { t.Errorf("whole: %v", hex.EncodeToString(text0)) } @@ -84,13 +96,13 @@ func TestDecode(t *testing.T) { continue } } - if insn.Pseudo && size >= 0 && size < len(text) { + if insn.GetPseudo() && 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.Name, hex.EncodeToString(text), mode, size, len(text)) + insn.GetName(), hex.EncodeToString(text), mode, size, len(text)) if len(text) != len(text0) { t.Errorf("whole: %v", hex.EncodeToString(text0)) } @@ -103,3 +115,7 @@ func TestDecode(t *testing.T) { } } } + +func TestDecode(t *testing.T) { + testdecodearch(t, ifuzz.ArchX86) +} -- cgit mrf-deployment