| 1 |
// Copyright 2025 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package demangle |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"os" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func FuzzDemangle(f *testing.F) { |
| 15 |
for _, test := range demanglerTests { |
| 16 |
f.Add(test.input) |
| 17 |
} |
| 18 |
for _, test := range failureTests { |
| 19 |
f.Add(test.input) |
| 20 |
} |
| 21 |
|
| 22 |
// Add all the entries from demangle-expected. |
| 23 |
|
| 24 |
file, err := os.Open(filename) |
| 25 |
if err != nil { |
| 26 |
f.Fatal(err) |
| 27 |
} |
| 28 |
scanner := bufio.NewScanner(file) |
| 29 |
lineno := 1 |
| 30 |
for { |
| 31 |
format, got := getOptLine(f, scanner, &lineno) |
| 32 |
if !got { |
| 33 |
break |
| 34 |
} |
| 35 |
report := lineno |
| 36 |
input := getLine(f, scanner, &lineno) |
| 37 |
getLine(f, scanner, &lineno) |
| 38 |
|
| 39 |
skip, testNoParams := skipExpectedTest(f, format, input, report) |
| 40 |
if testNoParams { |
| 41 |
getLine(f, scanner, &lineno) |
| 42 |
} |
| 43 |
|
| 44 |
if skip { |
| 45 |
continue |
| 46 |
} |
| 47 |
|
| 48 |
f.Add(input) |
| 49 |
} |
| 50 |
|
| 51 |
if err := scanner.Err(); err != nil { |
| 52 |
f.Error(err) |
| 53 |
} |
| 54 |
|
| 55 |
file.Close() |
| 56 |
|
| 57 |
// Add all the entries from the LLVM cases. |
| 58 |
|
| 59 |
cases := readCases(f) |
| 60 |
for _, test := range cases { |
| 61 |
testStr := test[0] |
| 62 |
if strings.HasPrefix(testStr, "__Z") || strings.HasPrefix(testStr, "____Z") { |
| 63 |
testStr = testStr[1:] |
| 64 |
} |
| 65 |
if !strings.HasPrefix(testStr, "_") { |
| 66 |
continue |
| 67 |
} |
| 68 |
f.Add(testStr) |
| 69 |
} |
| 70 |
|
| 71 |
f.Fuzz(func(t *testing.T, in string) { |
| 72 |
_, err := ToString(in) |
| 73 |
if err != nil { |
| 74 |
t.Skip() |
| 75 |
} |
| 76 |
}) |
| 77 |
} |
| 78 |
|
| 79 |
func FuzzRustDemangle(f *testing.F) { |
| 80 |
file, err := os.Open(rustFilename) |
| 81 |
if err != nil { |
| 82 |
f.Fatal(err) |
| 83 |
} |
| 84 |
scanner := bufio.NewScanner(file) |
| 85 |
lineno := 1 |
| 86 |
for { |
| 87 |
format, got := getOptLine(f, scanner, &lineno) |
| 88 |
if !got { |
| 89 |
break |
| 90 |
} |
| 91 |
report := lineno |
| 92 |
input := getLine(f, scanner, &lineno) |
| 93 |
getLine(f, scanner, &lineno) |
| 94 |
|
| 95 |
if skipExpectedRustTest(f, format, input, report) { |
| 96 |
continue |
| 97 |
} |
| 98 |
|
| 99 |
f.Add(input) |
| 100 |
} |
| 101 |
|
| 102 |
if err := scanner.Err(); err != nil { |
| 103 |
f.Error(err) |
| 104 |
} |
| 105 |
|
| 106 |
file.Close() |
| 107 |
|
| 108 |
file, err = os.Open(rustCheckFilename) |
| 109 |
if err != nil { |
| 110 |
f.Fatal(err) |
| 111 |
} |
| 112 |
scanner = bufio.NewScanner(file) |
| 113 |
for scanner.Scan() { |
| 114 |
line := strings.TrimSpace(scanner.Text()) |
| 115 |
if !strings.HasPrefix(line, "CHECK: ") { |
| 116 |
continue |
| 117 |
} |
| 118 |
if !scanner.Scan() { |
| 119 |
f.Fatalf("%s: unexpected EOF", rustCheckFilename) |
| 120 |
} |
| 121 |
input := strings.TrimSpace(scanner.Text()) |
| 122 |
f.Add(input) |
| 123 |
} |
| 124 |
|
| 125 |
if err := scanner.Err(); err != nil { |
| 126 |
f.Error(err) |
| 127 |
} |
| 128 |
|
| 129 |
file.Close() |
| 130 |
|
| 131 |
f.Add(rustMangledTemplates) |
| 132 |
|
| 133 |
f.Fuzz(func(t *testing.T, in string) { |
| 134 |
_, err := ToString(in) |
| 135 |
if err != nil { |
| 136 |
t.Skip() |
| 137 |
} |
| 138 |
}) |
| 139 |
} |