github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/report/bsd_test.go (about)

     1  // Copyright 2018 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 report
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/google/syzkaller/pkg/mgrconfig"
    11  	"github.com/google/syzkaller/pkg/symbolizer"
    12  )
    13  
    14  type symbolizeLineTest struct {
    15  	line   string
    16  	result string
    17  }
    18  
    19  func testSymbolizeLine(t *testing.T, ctor fn, tests []symbolizeLineTest) {
    20  	symbols := map[string][]symbolizer.Symbol{
    21  		"closef": {
    22  			{Addr: 0x815088a0, Size: 0x12f},
    23  		},
    24  		"sleep_finish_all": {
    25  			{Addr: 0x81237520, Size: 0x173},
    26  		},
    27  	}
    28  	symb := func(bin string, pcs ...uint64) ([]symbolizer.Frame, error) {
    29  		var res []symbolizer.Frame
    30  		for _, pc := range pcs {
    31  			if bin != "bsd.gdb" {
    32  				return nil, fmt.Errorf("unknown pc 0x%x", pc)
    33  			}
    34  
    35  			switch pc & 0xffffffff {
    36  			case 0x8150894f:
    37  				res = append(res, symbolizer.Frame{
    38  					Func: "closef",
    39  					File: "/bsd/src/kern_descrip.c",
    40  					Line: 1241,
    41  				})
    42  			case 0x81237542:
    43  				res = append(res,
    44  					symbolizer.Frame{
    45  						Func:   "sleep_finish_timeout",
    46  						File:   "/bsd/src/kern_synch.c",
    47  						Line:   336,
    48  						Inline: true,
    49  					},
    50  					symbolizer.Frame{
    51  						Func: "sleep_finish_all",
    52  						File: "/bsd/src/kern_synch.c",
    53  						Line: 157,
    54  					},
    55  				)
    56  			default:
    57  				return nil, fmt.Errorf("unknown pc 0x%x", pc)
    58  			}
    59  		}
    60  		return res, nil
    61  	}
    62  	reporter, _, err := ctor(&config{
    63  		kernelDirs: mgrconfig.KernelDirs{
    64  			Src:      "/bsd/src2",
    65  			BuildSrc: "/bsd/src",
    66  		},
    67  	})
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	bsd := reporter.(*bsd)
    72  	bsd.symbols = symbols
    73  	bsd.kernelObject = "bsd.gdb"
    74  	for i, test := range tests {
    75  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    76  			result := bsd.symbolizeLine(symb, []byte(test.line))
    77  			if test.result != string(result) {
    78  				t.Errorf("want %q\n\t     get %q", test.result, string(result))
    79  			}
    80  		})
    81  	}
    82  }