github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/ifuzz/arm64_test.go (about)

     1  // Copyright 2024 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
     5  
     6  import (
     7  	"encoding/binary"
     8  	"encoding/hex"
     9  	"fmt"
    10  	"strconv"
    11  	"testing"
    12  
    13  	"github.com/google/syzkaller/pkg/ifuzz/arm64"
    14  	"github.com/google/syzkaller/pkg/ifuzz/iset"
    15  )
    16  
    17  func PrintInsn(insn arm64.Insn) {
    18  	operands := ""
    19  	for i, op := range insn.Operands {
    20  		field := insn.Fields[i]
    21  		operands += fmt.Sprintf("%s:%d=%x ", field.Name, field.Length, op)
    22  	}
    23  	fmt.Printf("{ \"%s\" [0x%x] %s }\n", insn.Name, insn.AsUInt32, operands)
    24  }
    25  
    26  func parseAndPrint(from uint32) {
    27  	insn, _ := arm64.ParseInsn(from)
    28  	PrintInsn(insn)
    29  }
    30  
    31  func TestSomething(t *testing.T) {
    32  	parseAndPrint(0x0)
    33  	parseAndPrint(0xff3ffc00)
    34  	parseAndPrint(0x5e20b800)
    35  	parseAndPrint(0x52800021)
    36  	parseAndPrint(0x1b020020)
    37  	parseAndPrint(0x1b007c21)
    38  	parseAndPrint(0xb9400fe0)
    39  }
    40  
    41  func TestSum(t *testing.T) {
    42  	data := [][2]string{
    43  		{"d10043ff", "sub	sp, sp, #0x10"},
    44  		{"b9000fe0", "str	w0, [sp, #12]"},
    45  		{"b9000be1", "str	w1, [sp, #8]"},
    46  		{"b90007e2", "str	w2, [sp, #4]"},
    47  		{"b9400be1", "ldr	w1, [sp, #8]"},
    48  		{"b94007e0", "ldr	w0, [sp, #4]"},
    49  		{"1b007c21", "mul	w1, w1, w0"},
    50  		{"b9400fe0", "ldr	w0, [sp, #12]"},
    51  		{"0b000020", "add	w0, w1, w0"},
    52  		{"910043ff", "add	sp, sp, #0x10"},
    53  		{"d65f03c0", "ret"},
    54  	}
    55  	for _, pair := range data {
    56  		opcode, err := strconv.ParseUint(pair[0], 16, 32)
    57  		if err != nil {
    58  			t.Fatalf("failed to parse opcode")
    59  		}
    60  		fmt.Printf("%s\n", pair[1])
    61  		parseAndPrint(uint32(opcode))
    62  	}
    63  }
    64  
    65  func TestDecodeSamples(t *testing.T) {
    66  	testData := []string{
    67  		"2000000b",
    68  		"0000409b000028d5007008d5008080880000000e0038201e007008d5000028d50020000c0000181e",
    69  		"000cc0380094002f0100a0d40000600d000880b8000000fa0000208a000c40380068284e000008d5",
    70  		// x0[*x1] = *x2
    71  		"280080b9490040b9097828b8",
    72  		// hvc(x0, x1, x2, x3, x4)
    73  		"e00180d2210080d2420080d2630080d2840080d2020000d4",
    74  		"20e09fd200c0b0f2210080d2420080d2630080d2840080d2020000d4",
    75  		"000080d200c0b0f2210080d2420080d2630080d2840080d2020000d4",
    76  	}
    77  	insnset := iset.Arches["arm64"]
    78  	for _, str := range testData {
    79  		text, err := hex.DecodeString(str)
    80  		fmt.Printf("Decoding % x\n", text)
    81  		if err != nil {
    82  			t.Fatalf("invalid hex string")
    83  		}
    84  		for len(text) != 0 {
    85  			size, err := insnset.Decode(iset.ModeLong64, text)
    86  			if size == 0 || err != nil {
    87  				t.Errorf("failed to decode text: %v", text)
    88  				break
    89  			}
    90  			parseAndPrint(binary.LittleEndian.Uint32(text[0:4]))
    91  			text = text[size:]
    92  		}
    93  	}
    94  }