1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package ifuzz
import (
"encoding/hex"
"math/rand"
"os"
"testing"
"time"
"github.com/google/syzkaller/pkg/ifuzz/iset"
)
var allArches = []string{ArchX86, ArchPowerPC}
func TestMode(t *testing.T) {
for _, arch := range allArches {
t.Run(arch, func(t *testing.T) {
testMode(t, arch)
})
}
}
func testMode(t *testing.T, arch string) {
all := make(map[iset.Insn]bool)
for mode := iset.Mode(0); mode < iset.ModeLast; mode++ {
for priv := 0; priv < 2; priv++ {
for exec := 0; exec < 2; exec++ {
insns := allInsns(arch, mode, priv != 0, exec != 0)
t.Logf("mode=%v priv=%v exec=%v: %v instructions", mode, priv, exec, len(insns))
for _, insn := range insns {
all[insn] = true
}
}
}
}
t.Logf("total: %v instructions", len(all))
}
func TestDecode(t *testing.T) {
for _, arch := range allArches {
t.Run(arch, func(t *testing.T) {
testDecode(t, arch)
})
}
}
func testDecode(t *testing.T, arch string) {
insnset := iset.Arches[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
}
t.Logf("seed=%v", seed)
r := rand.New(rand.NewSource(seed))
for repeat := 0; repeat < 10; repeat++ {
for mode := iset.Mode(0); mode < iset.ModeLast; mode++ {
cfg := &iset.Config{
Mode: mode,
Priv: true,
Exec: true,
}
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", name, hex.EncodeToString(text), mode, err)
if len(text) != len(text0) {
t.Errorf("whole: %v", hex.EncodeToString(text0))
}
failed = true
continue
}
if xedEnabled {
xedSize, xedErr := insnset.DecodeExt(mode, text)
if xedErr != nil {
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))
}
failed = true
continue
}
if size != xedSize {
t.Errorf("decoding %v %v failed (mode=%v): decoded %v/%v, xed decoded %v/%v",
name, hex.EncodeToString(text), mode, size, xedSize, size, len(text))
if len(text) != len(text0) {
t.Errorf("whole: %v", hex.EncodeToString(text0))
}
failed = true
continue
}
}
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",
name, hex.EncodeToString(text), mode, size, len(text))
if len(text) != len(text0) {
t.Errorf("whole: %v", hex.EncodeToString(text0))
}
failed = true
}
}
if failed {
return
}
}
}
}
func allInsns(arch string, mode iset.Mode, priv, exec bool) []iset.Insn {
insnset := iset.Arches[arch]
insns := insnset.GetInsns(mode, iset.TypeUser)
if priv {
insns = append(insns, insnset.GetInsns(mode, iset.TypePriv)...)
if exec {
insns = append(insns, insnset.GetInsns(mode, iset.TypeExec)...)
}
}
return insns
}
|