|
| 1 | +// Copyright 2025 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 symbolizer_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "bufio" |
| 8 | + "bytes" |
| 9 | + "math/rand" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "path/filepath" |
| 13 | + "runtime" |
| 14 | + "strconv" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/google/syzkaller/pkg/symbolizer" |
| 20 | + "github.com/google/syzkaller/sys/targets" |
| 21 | +) |
| 22 | + |
| 23 | +func BenchmarkSymbolize(b *testing.B) { |
| 24 | + if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" { |
| 25 | + b.Skip("skipping on non-linux/amd64") |
| 26 | + } |
| 27 | + |
| 28 | + // Find vmlinux. |
| 29 | + vmlinux := os.Getenv("VMLINUX") |
| 30 | + if vmlinux == "" { |
| 31 | + // Try default location. |
| 32 | + vmlinux = filepath.Join(os.Getenv("HOME"), "projects/fuzzing-qemu/linux-stable/vmlinux") |
| 33 | + } |
| 34 | + if _, err := os.Stat(vmlinux); err != nil { |
| 35 | + b.Skipf("vmlinux not found at %v: %v", vmlinux, err) |
| 36 | + } |
| 37 | + |
| 38 | + // Create native symbolizer. |
| 39 | + target := targets.Get("linux", "amd64") |
| 40 | + target.KernelObject = vmlinux |
| 41 | + |
| 42 | + symb, err := symbolizer.Make(target, vmlinux) |
| 43 | + if err != nil { |
| 44 | + b.Fatalf("failed to create symbolizer: %v", err) |
| 45 | + } |
| 46 | + defer symb.Close() |
| 47 | + |
| 48 | + // Gather interesting PCs. |
| 49 | + // We want random PCs across the entire binary to stress the cache and searching. |
| 50 | + pcs := gatherAllPCs(nil, vmlinux) |
| 51 | + if len(pcs) == 0 { |
| 52 | + b.Fatal("no pcs found") |
| 53 | + } |
| 54 | + |
| 55 | + rng := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 56 | + b.ResetTimer() |
| 57 | + |
| 58 | + for i := 0; i < b.N; i++ { |
| 59 | + pc := pcs[rng.Intn(len(pcs))] |
| 60 | + _, err := symb.Symbolize(vmlinux, pc) |
| 61 | + if err != nil { |
| 62 | + b.Fatalf("symbolize failed: %v", err) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func gatherAllPCs(t *testing.T, bin string) []uint64 { |
| 68 | + cmd := exec.Command("nm", "-n", bin) |
| 69 | + out, err := cmd.Output() |
| 70 | + if err != nil { |
| 71 | + if t != nil { |
| 72 | + t.Fatalf("nm failed: %v", err) |
| 73 | + } |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + var pcs []uint64 |
| 78 | + scanner := bufio.NewScanner(bytes.NewReader(out)) |
| 79 | + for scanner.Scan() { |
| 80 | + line := scanner.Text() |
| 81 | + parts := strings.Fields(line) |
| 82 | + if len(parts) < 3 { |
| 83 | + continue |
| 84 | + } |
| 85 | + ifParts := parts[1] |
| 86 | + if ifParts == "t" || ifParts == "T" { |
| 87 | + addr, err := strconv.ParseUint(parts[0], 16, 64) |
| 88 | + // Kernel text usually starts high. |
| 89 | + if err == nil && addr > 0xffffffff80000000 { |
| 90 | + pcs = append(pcs, addr) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return pcs |
| 95 | +} |
0 commit comments