Home | History | Annotate | Download | only in ifuzz
      1 // Copyright 2017 syzkaller project authors. All rights reserved.
      2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
      3 
      4 package ifuzz_test
      5 
      6 import (
      7 	"encoding/hex"
      8 	"math/rand"
      9 	"testing"
     10 	"time"
     11 
     12 	. "github.com/google/syzkaller/pkg/ifuzz"
     13 	_ "github.com/google/syzkaller/pkg/ifuzz/generated"
     14 )
     15 
     16 func TestMode(t *testing.T) {
     17 	all := make(map[*Insn]bool)
     18 	for mode := 0; mode < ModeLast; mode++ {
     19 		for priv := 0; priv < 2; priv++ {
     20 			for exec := 0; exec < 2; exec++ {
     21 				cfg := &Config{
     22 					Mode: mode,
     23 					Priv: priv != 0,
     24 					Exec: exec != 0,
     25 				}
     26 				insns := ModeInsns(cfg)
     27 				t.Logf("mode=%v priv=%v exec=%v: %v instructions", mode, priv, exec, len(insns))
     28 				for _, insn := range insns {
     29 					all[insn] = true
     30 				}
     31 			}
     32 		}
     33 	}
     34 	t.Logf("total: %v instructions", len(all))
     35 }
     36 
     37 func TestDecode(t *testing.T) {
     38 	seed := int64(time.Now().UnixNano())
     39 	t.Logf("seed=%v", seed)
     40 	r := rand.New(rand.NewSource(seed))
     41 
     42 	for repeat := 0; repeat < 10; repeat++ {
     43 		for mode := 0; mode < ModeLast; mode++ {
     44 			cfg := &Config{
     45 				Mode: mode,
     46 				Priv: true,
     47 				Exec: true,
     48 			}
     49 			failed := false
     50 			for _, insn := range ModeInsns(cfg) {
     51 				text0 := insn.Encode(cfg, r)
     52 				text := text0
     53 			repeat:
     54 				size, err := Decode(mode, text)
     55 				if err != nil {
     56 					t.Errorf("decoding %v %v failed (mode=%v): %v", insn.Name, hex.EncodeToString(text), mode, err)
     57 					if len(text) != len(text0) {
     58 						t.Errorf("whole: %v", hex.EncodeToString(text0))
     59 					}
     60 					failed = true
     61 					continue
     62 				}
     63 				if XedDecode != nil {
     64 					xedSize, xedErr := XedDecode(mode, text)
     65 					if xedErr != nil {
     66 						t.Errorf("xed decoding %v %v failed (mode=%v): %v", insn.Name, hex.EncodeToString(text), mode, xedErr)
     67 						if len(text) != len(text0) {
     68 							t.Errorf("whole: %v", hex.EncodeToString(text0))
     69 						}
     70 						failed = true
     71 						continue
     72 					}
     73 					if size != xedSize {
     74 						t.Errorf("decoding %v %v failed (mode=%v): decoded %v/%v, xed decoded %v/%v",
     75 							insn.Name, hex.EncodeToString(text), mode, size, xedSize, size, len(text))
     76 						if len(text) != len(text0) {
     77 							t.Errorf("whole: %v", hex.EncodeToString(text0))
     78 						}
     79 						failed = true
     80 						continue
     81 					}
     82 				}
     83 				if insn.Pseudo && size >= 0 && size < len(text) {
     84 					text = text[size:]
     85 					goto repeat
     86 				}
     87 				if size != len(text) {
     88 					t.Errorf("decoding %v %v failed (mode=%v): decoded %v/%v",
     89 						insn.Name, hex.EncodeToString(text), mode, size, len(text))
     90 					if len(text) != len(text0) {
     91 						t.Errorf("whole: %v", hex.EncodeToString(text0))
     92 					}
     93 					failed = true
     94 				}
     95 			}
     96 			if failed {
     97 				return
     98 			}
     99 		}
    100 	}
    101 }
    102