Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- go/src/cmd/internal/archive/archive.go +503 -0
- go/src/cmd/internal/archive/archive_test.go +388 -0
- go/src/cmd/internal/bio/buf.go +148 -0
- go/src/cmd/internal/bio/buf_mmap.go +62 -0
- go/src/cmd/internal/bio/buf_nommap.go +11 -0
- go/src/cmd/internal/bootstrap_test/experiment_toolid_test.go +106 -0
- go/src/cmd/internal/bootstrap_test/overlaydir_test.go +88 -0
- go/src/cmd/internal/bootstrap_test/reboot_test.go +98 -0
- go/src/cmd/internal/browser/browser.go +67 -0
- go/src/cmd/internal/buildid/buildid.go +343 -0
- go/src/cmd/internal/buildid/buildid_test.go +251 -0
- go/src/cmd/internal/buildid/note.go +213 -0
- go/src/cmd/internal/buildid/rewrite.go +220 -0
- go/src/cmd/internal/codesign/codesign.go +267 -0
- go/src/cmd/internal/cov/covcmd/cmddefs.go +97 -0
- go/src/cmd/internal/cov/mreader.go +85 -0
- go/src/cmd/internal/cov/read_test.go +98 -0
- go/src/cmd/internal/cov/readcovdata.go +282 -0
- go/src/cmd/internal/disasm/disasm.go +464 -0
- go/src/cmd/internal/dwarf/dwarf.go +1695 -0
- go/src/cmd/internal/dwarf/dwarf_defs.go +550 -0
- go/src/cmd/internal/dwarf/dwarf_test.go +38 -0
- go/src/cmd/internal/dwarf/putvarabbrevgen.go +201 -0
- go/src/cmd/internal/dwarf/putvarabbrevgen_test.go +316 -0
- go/src/cmd/internal/edit/edit.go +93 -0
- go/src/cmd/internal/edit/edit_test.go +28 -0
- go/src/cmd/internal/fuzztest/script_test.go +22 -0
- go/src/cmd/internal/fuzztest/testdata/script/README +286 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz.txt +503 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_cache.txt +97 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_cgo.txt +28 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_chatty.txt +103 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_cleanup.txt +67 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_context.txt +47 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_cov.txt +36 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_deadline.txt +46 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_dup_cache.txt +53 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_err_deadlock.txt +50 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_fuzztime.txt +129 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_io_error.txt +102 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_limit_dup_entry.txt +37 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_match.txt +40 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_minimize.txt +203 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_minimize_dirty_cov.txt +85 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_minimize_interesting.txt +230 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_multiple.txt +50 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_mutate_crash.txt +323 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_mutate_fail.txt +103 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_mutator.txt +166 -0
- go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_mutator_repeat.txt +75 -0
go/src/cmd/internal/archive/archive.go
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2013 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 archive implements reading of archive files generated by the Go
|
| 6 |
+
// toolchain.
|
| 7 |
+
package archive
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"bufio"
|
| 11 |
+
"bytes"
|
| 12 |
+
"cmd/internal/bio"
|
| 13 |
+
"cmd/internal/goobj"
|
| 14 |
+
"errors"
|
| 15 |
+
"fmt"
|
| 16 |
+
"io"
|
| 17 |
+
"log"
|
| 18 |
+
"os"
|
| 19 |
+
"strconv"
|
| 20 |
+
"strings"
|
| 21 |
+
"time"
|
| 22 |
+
"unicode/utf8"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
/*
|
| 26 |
+
The archive format is:
|
| 27 |
+
|
| 28 |
+
First, on a line by itself
|
| 29 |
+
!<arch>
|
| 30 |
+
|
| 31 |
+
Then zero or more file records. Each file record has a fixed-size one-line header
|
| 32 |
+
followed by data bytes followed by an optional padding byte. The header is:
|
| 33 |
+
|
| 34 |
+
%-16s%-12d%-6d%-6d%-8o%-10d`
|
| 35 |
+
name mtime uid gid mode size
|
| 36 |
+
|
| 37 |
+
(note the trailing backquote). The %-16s here means at most 16 *bytes* of
|
| 38 |
+
the name, and if shorter, space padded on the right.
|
| 39 |
+
*/
|
| 40 |
+
|
| 41 |
+
// A Data is a reference to data stored in an object file.
|
| 42 |
+
// It records the offset and size of the data, so that a client can
|
| 43 |
+
// read the data only if necessary.
|
| 44 |
+
type Data struct {
|
| 45 |
+
Offset int64
|
| 46 |
+
Size int64
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
type Archive struct {
|
| 50 |
+
f *os.File
|
| 51 |
+
Entries []Entry
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
func (a *Archive) File() *os.File { return a.f }
|
| 55 |
+
|
| 56 |
+
type Entry struct {
|
| 57 |
+
Name string
|
| 58 |
+
Type EntryType
|
| 59 |
+
Mtime int64
|
| 60 |
+
Uid int
|
| 61 |
+
Gid int
|
| 62 |
+
Mode os.FileMode
|
| 63 |
+
Data
|
| 64 |
+
Obj *GoObj // nil if this entry is not a Go object file
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
type EntryType int
|
| 68 |
+
|
| 69 |
+
const (
|
| 70 |
+
EntryPkgDef EntryType = iota
|
| 71 |
+
EntryGoObj
|
| 72 |
+
EntryNativeObj
|
| 73 |
+
EntrySentinelNonObj
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
func (e *Entry) String() string {
|
| 77 |
+
return fmt.Sprintf("%s %6d/%-6d %12d %s %s",
|
| 78 |
+
(e.Mode & 0777).String(),
|
| 79 |
+
e.Uid,
|
| 80 |
+
e.Gid,
|
| 81 |
+
e.Size,
|
| 82 |
+
time.Unix(e.Mtime, 0).Format(timeFormat),
|
| 83 |
+
e.Name)
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
type GoObj struct {
|
| 87 |
+
TextHeader []byte
|
| 88 |
+
Arch string
|
| 89 |
+
Data
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
const (
|
| 93 |
+
entryHeader = "%s%-12d%-6d%-6d%-8o%-10d`\n"
|
| 94 |
+
// In entryHeader the first entry, the name, is always printed as 16 bytes right-padded.
|
| 95 |
+
entryLen = 16 + 12 + 6 + 6 + 8 + 10 + 1 + 1
|
| 96 |
+
timeFormat = "Jan _2 15:04 2006"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
var (
|
| 100 |
+
archiveHeader = []byte("!<arch>\n")
|
| 101 |
+
archiveMagic = []byte("`\n")
|
| 102 |
+
goobjHeader = []byte("go objec") // truncated to size of archiveHeader
|
| 103 |
+
|
| 104 |
+
errCorruptArchive = errors.New("corrupt archive")
|
| 105 |
+
errTruncatedArchive = errors.New("truncated archive")
|
| 106 |
+
errCorruptObject = errors.New("corrupt object file")
|
| 107 |
+
errNotObject = errors.New("unrecognized object file format")
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
type ErrGoObjOtherVersion struct{ magic []byte }
|
| 111 |
+
|
| 112 |
+
func (e ErrGoObjOtherVersion) Error() string {
|
| 113 |
+
return fmt.Sprintf("go object of a different version: %q", e.magic)
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// An objReader is an object file reader.
|
| 117 |
+
type objReader struct {
|
| 118 |
+
a *Archive
|
| 119 |
+
b *bio.Reader
|
| 120 |
+
err error
|
| 121 |
+
offset int64
|
| 122 |
+
limit int64
|
| 123 |
+
tmp [256]byte
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
func (r *objReader) init(f *os.File) {
|
| 127 |
+
r.a = &Archive{f, nil}
|
| 128 |
+
r.offset, _ = f.Seek(0, io.SeekCurrent)
|
| 129 |
+
r.limit, _ = f.Seek(0, io.SeekEnd)
|
| 130 |
+
f.Seek(r.offset, io.SeekStart)
|
| 131 |
+
r.b = bio.NewReader(f)
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// error records that an error occurred.
|
| 135 |
+
// It returns only the first error, so that an error
|
| 136 |
+
// caused by an earlier error does not discard information
|
| 137 |
+
// about the earlier error.
|
| 138 |
+
func (r *objReader) error(err error) error {
|
| 139 |
+
if r.err == nil {
|
| 140 |
+
if err == io.EOF {
|
| 141 |
+
err = io.ErrUnexpectedEOF
|
| 142 |
+
}
|
| 143 |
+
r.err = err
|
| 144 |
+
}
|
| 145 |
+
// panic("corrupt") // useful for debugging
|
| 146 |
+
return r.err
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
// peek returns the next n bytes without advancing the reader.
|
| 150 |
+
func (r *objReader) peek(n int) ([]byte, error) {
|
| 151 |
+
if r.err != nil {
|
| 152 |
+
return nil, r.err
|
| 153 |
+
}
|
| 154 |
+
if r.offset >= r.limit {
|
| 155 |
+
r.error(io.ErrUnexpectedEOF)
|
| 156 |
+
return nil, r.err
|
| 157 |
+
}
|
| 158 |
+
b, err := r.b.Peek(n)
|
| 159 |
+
if err != nil {
|
| 160 |
+
if err != bufio.ErrBufferFull {
|
| 161 |
+
r.error(err)
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
return b, err
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
// readByte reads and returns a byte from the input file.
|
| 168 |
+
// On I/O error or EOF, it records the error but returns byte 0.
|
| 169 |
+
// A sequence of 0 bytes will eventually terminate any
|
| 170 |
+
// parsing state in the object file. In particular, it ends the
|
| 171 |
+
// reading of a varint.
|
| 172 |
+
func (r *objReader) readByte() byte {
|
| 173 |
+
if r.err != nil {
|
| 174 |
+
return 0
|
| 175 |
+
}
|
| 176 |
+
if r.offset >= r.limit {
|
| 177 |
+
r.error(io.ErrUnexpectedEOF)
|
| 178 |
+
return 0
|
| 179 |
+
}
|
| 180 |
+
b, err := r.b.ReadByte()
|
| 181 |
+
if err != nil {
|
| 182 |
+
if err == io.EOF {
|
| 183 |
+
err = io.ErrUnexpectedEOF
|
| 184 |
+
}
|
| 185 |
+
r.error(err)
|
| 186 |
+
b = 0
|
| 187 |
+
} else {
|
| 188 |
+
r.offset++
|
| 189 |
+
}
|
| 190 |
+
return b
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
// readFull reads exactly len(b) bytes from the input file.
|
| 194 |
+
// If an error occurs, read returns the error but also
|
| 195 |
+
// records it, so it is safe for callers to ignore the result
|
| 196 |
+
// as long as delaying the report is not a problem.
|
| 197 |
+
func (r *objReader) readFull(b []byte) error {
|
| 198 |
+
if r.err != nil {
|
| 199 |
+
return r.err
|
| 200 |
+
}
|
| 201 |
+
if r.offset+int64(len(b)) > r.limit {
|
| 202 |
+
return r.error(io.ErrUnexpectedEOF)
|
| 203 |
+
}
|
| 204 |
+
n, err := io.ReadFull(r.b, b)
|
| 205 |
+
r.offset += int64(n)
|
| 206 |
+
if err != nil {
|
| 207 |
+
return r.error(err)
|
| 208 |
+
}
|
| 209 |
+
return nil
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
// skip skips n bytes in the input.
|
| 213 |
+
func (r *objReader) skip(n int64) {
|
| 214 |
+
if n < 0 {
|
| 215 |
+
r.error(fmt.Errorf("debug/goobj: internal error: misuse of skip"))
|
| 216 |
+
}
|
| 217 |
+
if n < int64(len(r.tmp)) {
|
| 218 |
+
// Since the data is so small, a just reading from the buffered
|
| 219 |
+
// reader is better than flushing the buffer and seeking.
|
| 220 |
+
r.readFull(r.tmp[:n])
|
| 221 |
+
} else if n <= int64(r.b.Buffered()) {
|
| 222 |
+
// Even though the data is not small, it has already been read.
|
| 223 |
+
// Advance the buffer instead of seeking.
|
| 224 |
+
for n > int64(len(r.tmp)) {
|
| 225 |
+
r.readFull(r.tmp[:])
|
| 226 |
+
n -= int64(len(r.tmp))
|
| 227 |
+
}
|
| 228 |
+
r.readFull(r.tmp[:n])
|
| 229 |
+
} else {
|
| 230 |
+
// Seek, giving up buffered data.
|
| 231 |
+
r.b.MustSeek(r.offset+n, io.SeekStart)
|
| 232 |
+
r.offset += n
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
// New writes to f to make a new archive.
|
| 237 |
+
func New(f *os.File) (*Archive, error) {
|
| 238 |
+
_, err := f.Write(archiveHeader)
|
| 239 |
+
if err != nil {
|
| 240 |
+
return nil, err
|
| 241 |
+
}
|
| 242 |
+
return &Archive{f: f}, nil
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
// Parse parses an object file or archive from f.
|
| 246 |
+
func Parse(f *os.File, verbose bool) (*Archive, error) {
|
| 247 |
+
var r objReader
|
| 248 |
+
r.init(f)
|
| 249 |
+
t, err := r.peek(8)
|
| 250 |
+
if err != nil {
|
| 251 |
+
if err == io.EOF {
|
| 252 |
+
err = io.ErrUnexpectedEOF
|
| 253 |
+
}
|
| 254 |
+
return nil, err
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
switch {
|
| 258 |
+
default:
|
| 259 |
+
return nil, errNotObject
|
| 260 |
+
|
| 261 |
+
case bytes.Equal(t, archiveHeader):
|
| 262 |
+
if err := r.parseArchive(verbose); err != nil {
|
| 263 |
+
return nil, err
|
| 264 |
+
}
|
| 265 |
+
case bytes.Equal(t, goobjHeader):
|
| 266 |
+
off := r.offset
|
| 267 |
+
o := &GoObj{}
|
| 268 |
+
if err := r.parseObject(o, r.limit-off); err != nil {
|
| 269 |
+
return nil, err
|
| 270 |
+
}
|
| 271 |
+
r.a.Entries = []Entry{{
|
| 272 |
+
Name: f.Name(),
|
| 273 |
+
Type: EntryGoObj,
|
| 274 |
+
Data: Data{off, r.limit - off},
|
| 275 |
+
Obj: o,
|
| 276 |
+
}}
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
return r.a, nil
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
// trimSpace removes trailing spaces from b and returns the corresponding string.
|
| 283 |
+
// This effectively parses the form used in archive headers.
|
| 284 |
+
func trimSpace(b []byte) string {
|
| 285 |
+
return string(bytes.TrimRight(b, " "))
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
// parseArchive parses a Unix archive of Go object files.
|
| 289 |
+
func (r *objReader) parseArchive(verbose bool) error {
|
| 290 |
+
r.readFull(r.tmp[:8]) // consume header (already checked)
|
| 291 |
+
for r.offset < r.limit {
|
| 292 |
+
if err := r.readFull(r.tmp[:60]); err != nil {
|
| 293 |
+
return err
|
| 294 |
+
}
|
| 295 |
+
data := r.tmp[:60]
|
| 296 |
+
|
| 297 |
+
// Each file is preceded by this text header (slice indices in first column):
|
| 298 |
+
// 0:16 name
|
| 299 |
+
// 16:28 date
|
| 300 |
+
// 28:34 uid
|
| 301 |
+
// 34:40 gid
|
| 302 |
+
// 40:48 mode
|
| 303 |
+
// 48:58 size
|
| 304 |
+
// 58:60 magic - `\n
|
| 305 |
+
// We only care about name, size, and magic, unless in verbose mode.
|
| 306 |
+
// The fields are space-padded on the right.
|
| 307 |
+
// The size is in decimal.
|
| 308 |
+
// The file data - size bytes - follows the header.
|
| 309 |
+
// Headers are 2-byte aligned, so if size is odd, an extra padding
|
| 310 |
+
// byte sits between the file data and the next header.
|
| 311 |
+
// The file data that follows is padded to an even number of bytes:
|
| 312 |
+
// if size is odd, an extra padding byte is inserted betw the next header.
|
| 313 |
+
if len(data) < 60 {
|
| 314 |
+
return errTruncatedArchive
|
| 315 |
+
}
|
| 316 |
+
if !bytes.Equal(data[58:60], archiveMagic) {
|
| 317 |
+
return errCorruptArchive
|
| 318 |
+
}
|
| 319 |
+
name := trimSpace(data[0:16])
|
| 320 |
+
var err error
|
| 321 |
+
get := func(start, end, base, bitsize int) int64 {
|
| 322 |
+
if err != nil {
|
| 323 |
+
return 0
|
| 324 |
+
}
|
| 325 |
+
var v int64
|
| 326 |
+
v, err = strconv.ParseInt(trimSpace(data[start:end]), base, bitsize)
|
| 327 |
+
return v
|
| 328 |
+
}
|
| 329 |
+
size := get(48, 58, 10, 64)
|
| 330 |
+
var (
|
| 331 |
+
mtime int64
|
| 332 |
+
uid, gid int
|
| 333 |
+
mode os.FileMode
|
| 334 |
+
)
|
| 335 |
+
if verbose {
|
| 336 |
+
mtime = get(16, 28, 10, 64)
|
| 337 |
+
uid = int(get(28, 34, 10, 32))
|
| 338 |
+
gid = int(get(34, 40, 10, 32))
|
| 339 |
+
mode = os.FileMode(get(40, 48, 8, 32))
|
| 340 |
+
}
|
| 341 |
+
if err != nil {
|
| 342 |
+
return errCorruptArchive
|
| 343 |
+
}
|
| 344 |
+
data = data[60:]
|
| 345 |
+
fsize := size + size&1
|
| 346 |
+
if fsize < 0 || fsize < size {
|
| 347 |
+
return errCorruptArchive
|
| 348 |
+
}
|
| 349 |
+
switch name {
|
| 350 |
+
case "__.PKGDEF":
|
| 351 |
+
r.a.Entries = append(r.a.Entries, Entry{
|
| 352 |
+
Name: name,
|
| 353 |
+
Type: EntryPkgDef,
|
| 354 |
+
Mtime: mtime,
|
| 355 |
+
Uid: uid,
|
| 356 |
+
Gid: gid,
|
| 357 |
+
Mode: mode,
|
| 358 |
+
Data: Data{r.offset, size},
|
| 359 |
+
})
|
| 360 |
+
r.skip(size)
|
| 361 |
+
case "preferlinkext", "dynimportfail":
|
| 362 |
+
if size == 0 {
|
| 363 |
+
// These are not actual objects, but rather sentinel
|
| 364 |
+
// entries put into the archive by the Go command to
|
| 365 |
+
// be read by the linker. See #62036.
|
| 366 |
+
r.a.Entries = append(r.a.Entries, Entry{
|
| 367 |
+
Name: name,
|
| 368 |
+
Type: EntrySentinelNonObj,
|
| 369 |
+
Mtime: mtime,
|
| 370 |
+
Uid: uid,
|
| 371 |
+
Gid: gid,
|
| 372 |
+
Mode: mode,
|
| 373 |
+
Data: Data{r.offset, size},
|
| 374 |
+
})
|
| 375 |
+
break
|
| 376 |
+
}
|
| 377 |
+
fallthrough
|
| 378 |
+
default:
|
| 379 |
+
var typ EntryType
|
| 380 |
+
var o *GoObj
|
| 381 |
+
offset := r.offset
|
| 382 |
+
p, err := r.peek(8)
|
| 383 |
+
if err != nil {
|
| 384 |
+
return err
|
| 385 |
+
}
|
| 386 |
+
if bytes.Equal(p, goobjHeader) {
|
| 387 |
+
typ = EntryGoObj
|
| 388 |
+
o = &GoObj{}
|
| 389 |
+
err := r.parseObject(o, size)
|
| 390 |
+
if err != nil {
|
| 391 |
+
return err
|
| 392 |
+
}
|
| 393 |
+
} else {
|
| 394 |
+
typ = EntryNativeObj
|
| 395 |
+
r.skip(size)
|
| 396 |
+
}
|
| 397 |
+
r.a.Entries = append(r.a.Entries, Entry{
|
| 398 |
+
Name: name,
|
| 399 |
+
Type: typ,
|
| 400 |
+
Mtime: mtime,
|
| 401 |
+
Uid: uid,
|
| 402 |
+
Gid: gid,
|
| 403 |
+
Mode: mode,
|
| 404 |
+
Data: Data{offset, size},
|
| 405 |
+
Obj: o,
|
| 406 |
+
})
|
| 407 |
+
}
|
| 408 |
+
if size&1 != 0 {
|
| 409 |
+
r.skip(1)
|
| 410 |
+
}
|
| 411 |
+
}
|
| 412 |
+
return nil
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
// parseObject parses a single Go object file.
|
| 416 |
+
// The object file consists of a textual header ending in "\n!\n"
|
| 417 |
+
// and then the part we want to parse begins.
|
| 418 |
+
// The format of that part is defined in a comment at the top
|
| 419 |
+
// of cmd/internal/goobj/objfile.go.
|
| 420 |
+
func (r *objReader) parseObject(o *GoObj, size int64) error {
|
| 421 |
+
h := make([]byte, 0, 256)
|
| 422 |
+
var c1, c2, c3 byte
|
| 423 |
+
for {
|
| 424 |
+
c1, c2, c3 = c2, c3, r.readByte()
|
| 425 |
+
h = append(h, c3)
|
| 426 |
+
// The new export format can contain 0 bytes.
|
| 427 |
+
// Don't consider them errors, only look for r.err != nil.
|
| 428 |
+
if r.err != nil {
|
| 429 |
+
return errCorruptObject
|
| 430 |
+
}
|
| 431 |
+
if c1 == '\n' && c2 == '!' && c3 == '\n' {
|
| 432 |
+
break
|
| 433 |
+
}
|
| 434 |
+
}
|
| 435 |
+
o.TextHeader = h
|
| 436 |
+
hs := strings.Fields(string(h))
|
| 437 |
+
if len(hs) >= 4 {
|
| 438 |
+
o.Arch = hs[3]
|
| 439 |
+
}
|
| 440 |
+
o.Offset = r.offset
|
| 441 |
+
o.Size = size - int64(len(h))
|
| 442 |
+
|
| 443 |
+
p, err := r.peek(8)
|
| 444 |
+
if err != nil {
|
| 445 |
+
return err
|
| 446 |
+
}
|
| 447 |
+
if !bytes.Equal(p, []byte(goobj.Magic)) {
|
| 448 |
+
if bytes.HasPrefix(p, []byte("\x00go1")) && bytes.HasSuffix(p, []byte("ld")) {
|
| 449 |
+
return r.error(ErrGoObjOtherVersion{p[1:]}) // strip the \x00 byte
|
| 450 |
+
}
|
| 451 |
+
return r.error(errCorruptObject)
|
| 452 |
+
}
|
| 453 |
+
r.skip(o.Size)
|
| 454 |
+
return nil
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
// AddEntry adds an entry to the end of a, with the content from r.
|
| 458 |
+
func (a *Archive) AddEntry(typ EntryType, name string, mtime int64, uid, gid int, mode os.FileMode, size int64, r io.Reader) {
|
| 459 |
+
off, err := a.f.Seek(0, io.SeekEnd)
|
| 460 |
+
if err != nil {
|
| 461 |
+
log.Fatal(err)
|
| 462 |
+
}
|
| 463 |
+
n, err := fmt.Fprintf(a.f, entryHeader, exactly16Bytes(name), mtime, uid, gid, mode, size)
|
| 464 |
+
if err != nil || n != entryLen {
|
| 465 |
+
log.Fatal("writing entry header: ", err)
|
| 466 |
+
}
|
| 467 |
+
n1, _ := io.CopyN(a.f, r, size)
|
| 468 |
+
if n1 != size {
|
| 469 |
+
log.Fatal(err)
|
| 470 |
+
}
|
| 471 |
+
if (off+size)&1 != 0 {
|
| 472 |
+
a.f.Write([]byte{0}) // pad to even byte
|
| 473 |
+
}
|
| 474 |
+
a.Entries = append(a.Entries, Entry{
|
| 475 |
+
Name: name,
|
| 476 |
+
Type: typ,
|
| 477 |
+
Mtime: mtime,
|
| 478 |
+
Uid: uid,
|
| 479 |
+
Gid: gid,
|
| 480 |
+
Mode: mode,
|
| 481 |
+
Data: Data{off + entryLen, size},
|
| 482 |
+
})
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
// exactly16Bytes truncates the string if necessary so it is at most 16 bytes long,
|
| 486 |
+
// then pads the result with spaces to be exactly 16 bytes.
|
| 487 |
+
// Fmt uses runes for its width calculation, but we need bytes in the entry header.
|
| 488 |
+
func exactly16Bytes(s string) string {
|
| 489 |
+
for len(s) > 16 {
|
| 490 |
+
_, wid := utf8.DecodeLastRuneInString(s)
|
| 491 |
+
s = s[:len(s)-wid]
|
| 492 |
+
}
|
| 493 |
+
const sixteenSpaces = " "
|
| 494 |
+
s += sixteenSpaces[:16-len(s)]
|
| 495 |
+
return s
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
// architecture-independent object file output
|
| 499 |
+
const HeaderSize = 60
|
| 500 |
+
|
| 501 |
+
func FormatHeader(arhdr []byte, name string, size int64) {
|
| 502 |
+
copy(arhdr[:], fmt.Sprintf("%-16s%-12d%-6d%-6d%-8o%-10d`\n", name, 0, 0, 0, 0644, size))
|
| 503 |
+
}
|
go/src/cmd/internal/archive/archive_test.go
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 archive
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"debug/elf"
|
| 10 |
+
"debug/macho"
|
| 11 |
+
"debug/pe"
|
| 12 |
+
"fmt"
|
| 13 |
+
"internal/testenv"
|
| 14 |
+
"internal/xcoff"
|
| 15 |
+
"io"
|
| 16 |
+
"os"
|
| 17 |
+
"path/filepath"
|
| 18 |
+
"runtime"
|
| 19 |
+
"sync"
|
| 20 |
+
"testing"
|
| 21 |
+
"unicode/utf8"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
var buildDir string
|
| 25 |
+
|
| 26 |
+
func TestMain(m *testing.M) {
|
| 27 |
+
if !testenv.HasGoBuild() {
|
| 28 |
+
return
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
exit := m.Run()
|
| 32 |
+
|
| 33 |
+
if buildDir != "" {
|
| 34 |
+
os.RemoveAll(buildDir)
|
| 35 |
+
}
|
| 36 |
+
os.Exit(exit)
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
func copyDir(dst, src string) error {
|
| 40 |
+
err := os.MkdirAll(dst, 0777)
|
| 41 |
+
if err != nil {
|
| 42 |
+
return err
|
| 43 |
+
}
|
| 44 |
+
entries, err := os.ReadDir(src)
|
| 45 |
+
if err != nil {
|
| 46 |
+
return err
|
| 47 |
+
}
|
| 48 |
+
for _, entry := range entries {
|
| 49 |
+
err = copyFile(filepath.Join(dst, entry.Name()), filepath.Join(src, entry.Name()))
|
| 50 |
+
if err != nil {
|
| 51 |
+
return err
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
return nil
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
func copyFile(dst, src string) (err error) {
|
| 58 |
+
var s, d *os.File
|
| 59 |
+
s, err = os.Open(src)
|
| 60 |
+
if err != nil {
|
| 61 |
+
return err
|
| 62 |
+
}
|
| 63 |
+
defer s.Close()
|
| 64 |
+
d, err = os.Create(dst)
|
| 65 |
+
if err != nil {
|
| 66 |
+
return err
|
| 67 |
+
}
|
| 68 |
+
defer func() {
|
| 69 |
+
e := d.Close()
|
| 70 |
+
if err == nil {
|
| 71 |
+
err = e
|
| 72 |
+
}
|
| 73 |
+
}()
|
| 74 |
+
_, err = io.Copy(d, s)
|
| 75 |
+
if err != nil {
|
| 76 |
+
return err
|
| 77 |
+
}
|
| 78 |
+
return nil
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
var (
|
| 82 |
+
buildOnce sync.Once
|
| 83 |
+
builtGoobjs goobjPaths
|
| 84 |
+
buildErr error
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
type goobjPaths struct {
|
| 88 |
+
go1obj string
|
| 89 |
+
go2obj string
|
| 90 |
+
goarchive string
|
| 91 |
+
cgoarchive string
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
func buildGoobj(t *testing.T) goobjPaths {
|
| 95 |
+
buildOnce.Do(func() {
|
| 96 |
+
buildErr = func() (err error) {
|
| 97 |
+
buildDir, err = os.MkdirTemp("", "TestGoobj")
|
| 98 |
+
if err != nil {
|
| 99 |
+
return err
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
go1obj := filepath.Join(buildDir, "go1.o")
|
| 103 |
+
go2obj := filepath.Join(buildDir, "go2.o")
|
| 104 |
+
goarchive := filepath.Join(buildDir, "go.a")
|
| 105 |
+
cgoarchive := ""
|
| 106 |
+
|
| 107 |
+
gotool, err := testenv.GoTool()
|
| 108 |
+
if err != nil {
|
| 109 |
+
return err
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
go1src := filepath.Join("testdata", "go1.go")
|
| 113 |
+
go2src := filepath.Join("testdata", "go2.go")
|
| 114 |
+
|
| 115 |
+
importcfgfile := filepath.Join(buildDir, "importcfg")
|
| 116 |
+
testenv.WriteImportcfg(t, importcfgfile, nil, go1src, go2src)
|
| 117 |
+
|
| 118 |
+
out, err := testenv.Command(t, gotool, "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", go1obj, go1src).CombinedOutput()
|
| 119 |
+
if err != nil {
|
| 120 |
+
return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out)
|
| 121 |
+
}
|
| 122 |
+
out, err = testenv.Command(t, gotool, "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", go2obj, go2src).CombinedOutput()
|
| 123 |
+
if err != nil {
|
| 124 |
+
return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out)
|
| 125 |
+
}
|
| 126 |
+
out, err = testenv.Command(t, gotool, "tool", "pack", "c", goarchive, go1obj, go2obj).CombinedOutput()
|
| 127 |
+
if err != nil {
|
| 128 |
+
return fmt.Errorf("go tool pack c %s %s %s: %v\n%s", goarchive, go1obj, go2obj, err, out)
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
if testenv.HasCGO() {
|
| 132 |
+
cgoarchive = filepath.Join(buildDir, "mycgo.a")
|
| 133 |
+
gopath := filepath.Join(buildDir, "gopath")
|
| 134 |
+
err = copyDir(filepath.Join(gopath, "src", "mycgo"), filepath.Join("testdata", "mycgo"))
|
| 135 |
+
if err == nil {
|
| 136 |
+
err = os.WriteFile(filepath.Join(gopath, "src", "mycgo", "go.mod"), []byte("module mycgo\n"), 0666)
|
| 137 |
+
}
|
| 138 |
+
if err != nil {
|
| 139 |
+
return err
|
| 140 |
+
}
|
| 141 |
+
cmd := testenv.Command(t, gotool, "build", "-buildmode=archive", "-o", cgoarchive, "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "mycgo")
|
| 142 |
+
cmd.Dir = filepath.Join(gopath, "src", "mycgo")
|
| 143 |
+
cmd.Env = append(os.Environ(), "GOPATH="+gopath)
|
| 144 |
+
out, err = cmd.CombinedOutput()
|
| 145 |
+
if err != nil {
|
| 146 |
+
return fmt.Errorf("go install mycgo: %v\n%s", err, out)
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
builtGoobjs = goobjPaths{
|
| 151 |
+
go1obj: go1obj,
|
| 152 |
+
go2obj: go2obj,
|
| 153 |
+
goarchive: goarchive,
|
| 154 |
+
cgoarchive: cgoarchive,
|
| 155 |
+
}
|
| 156 |
+
return nil
|
| 157 |
+
}()
|
| 158 |
+
})
|
| 159 |
+
|
| 160 |
+
if buildErr != nil {
|
| 161 |
+
t.Helper()
|
| 162 |
+
t.Fatal(buildErr)
|
| 163 |
+
}
|
| 164 |
+
return builtGoobjs
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
func TestParseGoobj(t *testing.T) {
|
| 168 |
+
path := buildGoobj(t).go1obj
|
| 169 |
+
|
| 170 |
+
f, err := os.Open(path)
|
| 171 |
+
if err != nil {
|
| 172 |
+
t.Fatal(err)
|
| 173 |
+
}
|
| 174 |
+
defer f.Close()
|
| 175 |
+
|
| 176 |
+
a, err := Parse(f, false)
|
| 177 |
+
if err != nil {
|
| 178 |
+
t.Fatal(err)
|
| 179 |
+
}
|
| 180 |
+
if len(a.Entries) != 2 {
|
| 181 |
+
t.Errorf("expect 2 entry, found %d", len(a.Entries))
|
| 182 |
+
}
|
| 183 |
+
for _, e := range a.Entries {
|
| 184 |
+
if e.Type == EntryPkgDef {
|
| 185 |
+
continue
|
| 186 |
+
}
|
| 187 |
+
if e.Type != EntryGoObj {
|
| 188 |
+
t.Errorf("wrong type of object: want EntryGoObj, got %v", e.Type)
|
| 189 |
+
}
|
| 190 |
+
if !bytes.Contains(e.Obj.TextHeader, []byte(runtime.GOARCH)) {
|
| 191 |
+
t.Errorf("text header does not contain GOARCH %s: %q", runtime.GOARCH, e.Obj.TextHeader)
|
| 192 |
+
}
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
func TestParseArchive(t *testing.T) {
|
| 197 |
+
path := buildGoobj(t).goarchive
|
| 198 |
+
|
| 199 |
+
f, err := os.Open(path)
|
| 200 |
+
if err != nil {
|
| 201 |
+
t.Fatal(err)
|
| 202 |
+
}
|
| 203 |
+
defer f.Close()
|
| 204 |
+
|
| 205 |
+
a, err := Parse(f, false)
|
| 206 |
+
if err != nil {
|
| 207 |
+
t.Fatal(err)
|
| 208 |
+
}
|
| 209 |
+
if len(a.Entries) != 3 {
|
| 210 |
+
t.Errorf("expect 3 entry, found %d", len(a.Entries))
|
| 211 |
+
}
|
| 212 |
+
var found1 bool
|
| 213 |
+
var found2 bool
|
| 214 |
+
for _, e := range a.Entries {
|
| 215 |
+
if e.Type == EntryPkgDef {
|
| 216 |
+
continue
|
| 217 |
+
}
|
| 218 |
+
if e.Type != EntryGoObj {
|
| 219 |
+
t.Errorf("wrong type of object: want EntryGoObj, got %v", e.Type)
|
| 220 |
+
}
|
| 221 |
+
if !bytes.Contains(e.Obj.TextHeader, []byte(runtime.GOARCH)) {
|
| 222 |
+
t.Errorf("text header does not contain GOARCH %s: %q", runtime.GOARCH, e.Obj.TextHeader)
|
| 223 |
+
}
|
| 224 |
+
if e.Name == "go1.o" {
|
| 225 |
+
found1 = true
|
| 226 |
+
}
|
| 227 |
+
if e.Name == "go2.o" {
|
| 228 |
+
found2 = true
|
| 229 |
+
}
|
| 230 |
+
}
|
| 231 |
+
if !found1 {
|
| 232 |
+
t.Errorf(`object "go1.o" not found`)
|
| 233 |
+
}
|
| 234 |
+
if !found2 {
|
| 235 |
+
t.Errorf(`object "go2.o" not found`)
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
func TestParseCGOArchive(t *testing.T) {
|
| 240 |
+
testenv.MustHaveCGO(t)
|
| 241 |
+
|
| 242 |
+
path := buildGoobj(t).cgoarchive
|
| 243 |
+
|
| 244 |
+
f, err := os.Open(path)
|
| 245 |
+
if err != nil {
|
| 246 |
+
t.Fatal(err)
|
| 247 |
+
}
|
| 248 |
+
defer f.Close()
|
| 249 |
+
|
| 250 |
+
a, err := Parse(f, false)
|
| 251 |
+
if err != nil {
|
| 252 |
+
t.Fatal(err)
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
c1 := "c1"
|
| 256 |
+
c2 := "c2"
|
| 257 |
+
switch runtime.GOOS {
|
| 258 |
+
case "darwin", "ios":
|
| 259 |
+
c1 = "_" + c1
|
| 260 |
+
c2 = "_" + c2
|
| 261 |
+
case "windows":
|
| 262 |
+
if runtime.GOARCH == "386" {
|
| 263 |
+
c1 = "_" + c1
|
| 264 |
+
c2 = "_" + c2
|
| 265 |
+
}
|
| 266 |
+
case "aix":
|
| 267 |
+
c1 = "." + c1
|
| 268 |
+
c2 = "." + c2
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
var foundgo, found1, found2 bool
|
| 272 |
+
|
| 273 |
+
for _, e := range a.Entries {
|
| 274 |
+
switch e.Type {
|
| 275 |
+
default:
|
| 276 |
+
t.Errorf("unknown object type")
|
| 277 |
+
case EntryPkgDef:
|
| 278 |
+
continue
|
| 279 |
+
case EntryGoObj:
|
| 280 |
+
foundgo = true
|
| 281 |
+
if !bytes.Contains(e.Obj.TextHeader, []byte(runtime.GOARCH)) {
|
| 282 |
+
t.Errorf("text header does not contain GOARCH %s: %q", runtime.GOARCH, e.Obj.TextHeader)
|
| 283 |
+
}
|
| 284 |
+
continue
|
| 285 |
+
case EntryNativeObj:
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
obj := io.NewSectionReader(f, e.Offset, e.Size)
|
| 289 |
+
switch runtime.GOOS {
|
| 290 |
+
case "darwin", "ios":
|
| 291 |
+
mf, err := macho.NewFile(obj)
|
| 292 |
+
if err != nil {
|
| 293 |
+
t.Fatal(err)
|
| 294 |
+
}
|
| 295 |
+
if mf.Symtab == nil {
|
| 296 |
+
continue
|
| 297 |
+
}
|
| 298 |
+
for _, s := range mf.Symtab.Syms {
|
| 299 |
+
switch s.Name {
|
| 300 |
+
case c1:
|
| 301 |
+
found1 = true
|
| 302 |
+
case c2:
|
| 303 |
+
found2 = true
|
| 304 |
+
}
|
| 305 |
+
}
|
| 306 |
+
case "windows":
|
| 307 |
+
pf, err := pe.NewFile(obj)
|
| 308 |
+
if err != nil {
|
| 309 |
+
t.Fatal(err)
|
| 310 |
+
}
|
| 311 |
+
for _, s := range pf.Symbols {
|
| 312 |
+
switch s.Name {
|
| 313 |
+
case c1:
|
| 314 |
+
found1 = true
|
| 315 |
+
case c2:
|
| 316 |
+
found2 = true
|
| 317 |
+
}
|
| 318 |
+
}
|
| 319 |
+
case "aix":
|
| 320 |
+
xf, err := xcoff.NewFile(obj)
|
| 321 |
+
if err != nil {
|
| 322 |
+
t.Fatal(err)
|
| 323 |
+
}
|
| 324 |
+
for _, s := range xf.Symbols {
|
| 325 |
+
switch s.Name {
|
| 326 |
+
case c1:
|
| 327 |
+
found1 = true
|
| 328 |
+
case c2:
|
| 329 |
+
found2 = true
|
| 330 |
+
}
|
| 331 |
+
}
|
| 332 |
+
default: // ELF
|
| 333 |
+
ef, err := elf.NewFile(obj)
|
| 334 |
+
if err != nil {
|
| 335 |
+
t.Fatal(err)
|
| 336 |
+
}
|
| 337 |
+
syms, err := ef.Symbols()
|
| 338 |
+
if err != nil {
|
| 339 |
+
t.Fatal(err)
|
| 340 |
+
}
|
| 341 |
+
for _, s := range syms {
|
| 342 |
+
switch s.Name {
|
| 343 |
+
case c1:
|
| 344 |
+
found1 = true
|
| 345 |
+
case c2:
|
| 346 |
+
found2 = true
|
| 347 |
+
}
|
| 348 |
+
}
|
| 349 |
+
}
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
if !foundgo {
|
| 353 |
+
t.Errorf(`go object not found`)
|
| 354 |
+
}
|
| 355 |
+
if !found1 {
|
| 356 |
+
t.Errorf(`symbol %q not found`, c1)
|
| 357 |
+
}
|
| 358 |
+
if !found2 {
|
| 359 |
+
t.Errorf(`symbol %q not found`, c2)
|
| 360 |
+
}
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
func TestExactly16Bytes(t *testing.T) {
|
| 364 |
+
var tests = []string{
|
| 365 |
+
"",
|
| 366 |
+
"a",
|
| 367 |
+
"日本語",
|
| 368 |
+
"1234567890123456",
|
| 369 |
+
"12345678901234567890",
|
| 370 |
+
"1234567890123本語4567890",
|
| 371 |
+
"12345678901234日本語567890",
|
| 372 |
+
"123456789012345日本語67890",
|
| 373 |
+
"1234567890123456日本語7890",
|
| 374 |
+
"1234567890123456日本語7日本語890",
|
| 375 |
+
}
|
| 376 |
+
for _, str := range tests {
|
| 377 |
+
got := exactly16Bytes(str)
|
| 378 |
+
if len(got) != 16 {
|
| 379 |
+
t.Errorf("exactly16Bytes(%q) is %q, length %d", str, got, len(got))
|
| 380 |
+
}
|
| 381 |
+
// Make sure it is full runes.
|
| 382 |
+
for _, c := range got {
|
| 383 |
+
if c == utf8.RuneError {
|
| 384 |
+
t.Errorf("exactly16Bytes(%q) is %q, has partial rune", str, got)
|
| 385 |
+
}
|
| 386 |
+
}
|
| 387 |
+
}
|
| 388 |
+
}
|
go/src/cmd/internal/bio/buf.go
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2015 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 bio implements common I/O abstractions used within the Go toolchain.
|
| 6 |
+
package bio
|
| 7 |
+
|
| 8 |
+
import (
|
| 9 |
+
"bufio"
|
| 10 |
+
"io"
|
| 11 |
+
"log"
|
| 12 |
+
"os"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
// Reader implements a seekable buffered io.Reader.
|
| 16 |
+
type Reader struct {
|
| 17 |
+
f *os.File
|
| 18 |
+
*bufio.Reader
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// Writer implements a seekable buffered io.Writer.
|
| 22 |
+
type Writer struct {
|
| 23 |
+
f *os.File
|
| 24 |
+
*bufio.Writer
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
// Create creates the file named name and returns a Writer
|
| 28 |
+
// for that file.
|
| 29 |
+
func Create(name string) (*Writer, error) {
|
| 30 |
+
f, err := os.Create(name)
|
| 31 |
+
if err != nil {
|
| 32 |
+
return nil, err
|
| 33 |
+
}
|
| 34 |
+
return &Writer{f: f, Writer: bufio.NewWriter(f)}, nil
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// Open returns a Reader for the file named name.
|
| 38 |
+
func Open(name string) (*Reader, error) {
|
| 39 |
+
f, err := os.Open(name)
|
| 40 |
+
if err != nil {
|
| 41 |
+
return nil, err
|
| 42 |
+
}
|
| 43 |
+
return NewReader(f), nil
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// NewReader returns a Reader from an open file.
|
| 47 |
+
func NewReader(f *os.File) *Reader {
|
| 48 |
+
return &Reader{f: f, Reader: bufio.NewReader(f)}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
func (r *Reader) MustSeek(offset int64, whence int) int64 {
|
| 52 |
+
if whence == 1 {
|
| 53 |
+
offset -= int64(r.Buffered())
|
| 54 |
+
}
|
| 55 |
+
off, err := r.f.Seek(offset, whence)
|
| 56 |
+
if err != nil {
|
| 57 |
+
log.Fatalf("seeking in output: %v", err)
|
| 58 |
+
}
|
| 59 |
+
r.Reset(r.f)
|
| 60 |
+
return off
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
func (w *Writer) MustSeek(offset int64, whence int) int64 {
|
| 64 |
+
if err := w.Flush(); err != nil {
|
| 65 |
+
log.Fatalf("writing output: %v", err)
|
| 66 |
+
}
|
| 67 |
+
off, err := w.f.Seek(offset, whence)
|
| 68 |
+
if err != nil {
|
| 69 |
+
log.Fatalf("seeking in output: %v", err)
|
| 70 |
+
}
|
| 71 |
+
return off
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
func (r *Reader) Offset() int64 {
|
| 75 |
+
off, err := r.f.Seek(0, 1)
|
| 76 |
+
if err != nil {
|
| 77 |
+
log.Fatalf("seeking in output [0, 1]: %v", err)
|
| 78 |
+
}
|
| 79 |
+
off -= int64(r.Buffered())
|
| 80 |
+
return off
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
func (w *Writer) Offset() int64 {
|
| 84 |
+
if err := w.Flush(); err != nil {
|
| 85 |
+
log.Fatalf("writing output: %v", err)
|
| 86 |
+
}
|
| 87 |
+
off, err := w.f.Seek(0, 1)
|
| 88 |
+
if err != nil {
|
| 89 |
+
log.Fatalf("seeking in output [0, 1]: %v", err)
|
| 90 |
+
}
|
| 91 |
+
return off
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
func (r *Reader) Close() error {
|
| 95 |
+
return r.f.Close()
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
func (w *Writer) Close() error {
|
| 99 |
+
err := w.Flush()
|
| 100 |
+
err1 := w.f.Close()
|
| 101 |
+
if err == nil {
|
| 102 |
+
err = err1
|
| 103 |
+
}
|
| 104 |
+
return err
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
func (r *Reader) File() *os.File {
|
| 108 |
+
return r.f
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
func (w *Writer) File() *os.File {
|
| 112 |
+
return w.f
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
// Slice reads the next length bytes of r into a slice.
|
| 116 |
+
//
|
| 117 |
+
// This slice may be backed by mmap'ed memory. Currently, this memory
|
| 118 |
+
// will never be unmapped. The second result reports whether the
|
| 119 |
+
// backing memory is read-only.
|
| 120 |
+
func (r *Reader) Slice(length uint64) ([]byte, bool, error) {
|
| 121 |
+
if length == 0 {
|
| 122 |
+
return []byte{}, false, nil
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
data, ok := r.sliceOS(length)
|
| 126 |
+
if ok {
|
| 127 |
+
return data, true, nil
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
data = make([]byte, length)
|
| 131 |
+
_, err := io.ReadFull(r, data)
|
| 132 |
+
if err != nil {
|
| 133 |
+
return nil, false, err
|
| 134 |
+
}
|
| 135 |
+
return data, false, nil
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// SliceRO returns a slice containing the next length bytes of r
|
| 139 |
+
// backed by a read-only mmap'd data. If the mmap cannot be
|
| 140 |
+
// established (limit exceeded, region too small, etc) a nil slice
|
| 141 |
+
// will be returned. If mmap succeeds, it will never be unmapped.
|
| 142 |
+
func (r *Reader) SliceRO(length uint64) []byte {
|
| 143 |
+
data, ok := r.sliceOS(length)
|
| 144 |
+
if ok {
|
| 145 |
+
return data
|
| 146 |
+
}
|
| 147 |
+
return nil
|
| 148 |
+
}
|
go/src/cmd/internal/bio/buf_mmap.go
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2019 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 |
+
//go:build unix
|
| 6 |
+
|
| 7 |
+
package bio
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"runtime"
|
| 11 |
+
"sync/atomic"
|
| 12 |
+
"syscall"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
// mmapLimit is the maximum number of mmaped regions to create before
|
| 16 |
+
// falling back to reading into a heap-allocated slice. This exists
|
| 17 |
+
// because some operating systems place a limit on the number of
|
| 18 |
+
// distinct mapped regions per process. As of this writing:
|
| 19 |
+
//
|
| 20 |
+
// Darwin unlimited
|
| 21 |
+
// DragonFly 1000000 (vm.max_proc_mmap)
|
| 22 |
+
// FreeBSD unlimited
|
| 23 |
+
// Linux 65530 (vm.max_map_count) // TODO: query /proc/sys/vm/max_map_count?
|
| 24 |
+
// NetBSD unlimited
|
| 25 |
+
// OpenBSD unlimited
|
| 26 |
+
var mmapLimit int32 = 1<<31 - 1
|
| 27 |
+
|
| 28 |
+
func init() {
|
| 29 |
+
// Linux is the only practically concerning OS.
|
| 30 |
+
if runtime.GOOS == "linux" {
|
| 31 |
+
mmapLimit = 30000
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
func (r *Reader) sliceOS(length uint64) ([]byte, bool) {
|
| 36 |
+
// For small slices, don't bother with the overhead of a
|
| 37 |
+
// mapping, especially since we have no way to unmap it.
|
| 38 |
+
const threshold = 16 << 10
|
| 39 |
+
if length < threshold {
|
| 40 |
+
return nil, false
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// Have we reached the mmap limit?
|
| 44 |
+
if atomic.AddInt32(&mmapLimit, -1) < 0 {
|
| 45 |
+
atomic.AddInt32(&mmapLimit, 1)
|
| 46 |
+
return nil, false
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// Page-align the offset.
|
| 50 |
+
off := r.Offset()
|
| 51 |
+
align := syscall.Getpagesize()
|
| 52 |
+
aoff := off &^ int64(align-1)
|
| 53 |
+
|
| 54 |
+
data, err := syscall.Mmap(int(r.f.Fd()), aoff, int(length+uint64(off-aoff)), syscall.PROT_READ, syscall.MAP_SHARED|syscall.MAP_FILE)
|
| 55 |
+
if err != nil {
|
| 56 |
+
return nil, false
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
data = data[off-aoff:]
|
| 60 |
+
r.MustSeek(int64(length), 1)
|
| 61 |
+
return data, true
|
| 62 |
+
}
|
go/src/cmd/internal/bio/buf_nommap.go
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2019 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 |
+
//go:build !unix
|
| 6 |
+
|
| 7 |
+
package bio
|
| 8 |
+
|
| 9 |
+
func (r *Reader) sliceOS(length uint64) ([]byte, bool) {
|
| 10 |
+
return nil, false
|
| 11 |
+
}
|
go/src/cmd/internal/bootstrap_test/experiment_toolid_test.go
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2019 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 |
+
//go:build explicit
|
| 6 |
+
|
| 7 |
+
package bootstrap_test
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"bytes"
|
| 11 |
+
"errors"
|
| 12 |
+
"internal/testenv"
|
| 13 |
+
"os"
|
| 14 |
+
"os/exec"
|
| 15 |
+
"path/filepath"
|
| 16 |
+
"runtime"
|
| 17 |
+
"testing"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
// TestExperimentToolID verifies that GOEXPERIMENT settings built
|
| 21 |
+
// into the toolchain influence tool ids in the Go command.
|
| 22 |
+
// This test requires bootstrapping the toolchain twice, so it's very expensive.
|
| 23 |
+
// It must be run explicitly with -tags=explicit.
|
| 24 |
+
// Verifies go.dev/issue/33091.
|
| 25 |
+
func TestExperimentToolID(t *testing.T) {
|
| 26 |
+
if testing.Short() {
|
| 27 |
+
t.Skip("skipping test that rebuilds the entire toolchain twice")
|
| 28 |
+
}
|
| 29 |
+
switch runtime.GOOS {
|
| 30 |
+
case "android", "ios", "js", "wasip1":
|
| 31 |
+
t.Skipf("skipping because the toolchain does not have to bootstrap on GOOS=%s", runtime.GOOS)
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
realGoroot := testenv.GOROOT(t)
|
| 35 |
+
|
| 36 |
+
// Set up GOROOT.
|
| 37 |
+
goroot := t.TempDir()
|
| 38 |
+
gorootSrc := filepath.Join(goroot, "src")
|
| 39 |
+
if err := overlayDir(gorootSrc, filepath.Join(realGoroot, "src")); err != nil {
|
| 40 |
+
t.Fatal(err)
|
| 41 |
+
}
|
| 42 |
+
gorootLib := filepath.Join(goroot, "lib")
|
| 43 |
+
if err := overlayDir(gorootLib, filepath.Join(realGoroot, "lib")); err != nil {
|
| 44 |
+
t.Fatal(err)
|
| 45 |
+
}
|
| 46 |
+
if err := os.WriteFile(filepath.Join(goroot, "VERSION"), []byte("go1.999"), 0666); err != nil {
|
| 47 |
+
t.Fatal(err)
|
| 48 |
+
}
|
| 49 |
+
env := append(os.Environ(), "GOROOT=", "GOROOT_BOOTSTRAP="+realGoroot)
|
| 50 |
+
|
| 51 |
+
// Use a clean cache.
|
| 52 |
+
gocache := t.TempDir()
|
| 53 |
+
env = append(env, "GOCACHE="+gocache)
|
| 54 |
+
|
| 55 |
+
// Build the toolchain without GOEXPERIMENT.
|
| 56 |
+
var makeScript string
|
| 57 |
+
switch runtime.GOOS {
|
| 58 |
+
case "windows":
|
| 59 |
+
makeScript = "make.bat"
|
| 60 |
+
case "plan9":
|
| 61 |
+
makeScript = "make.rc"
|
| 62 |
+
default:
|
| 63 |
+
makeScript = "make.bash"
|
| 64 |
+
}
|
| 65 |
+
makeScriptPath := filepath.Join(realGoroot, "src", makeScript)
|
| 66 |
+
runCmd(t, gorootSrc, env, makeScriptPath)
|
| 67 |
+
|
| 68 |
+
// Verify compiler version string.
|
| 69 |
+
goCmdPath := filepath.Join(goroot, "bin", "go")
|
| 70 |
+
gotVersion := bytes.TrimSpace(runCmd(t, gorootSrc, env, goCmdPath, "tool", "compile", "-V=full"))
|
| 71 |
+
wantVersion := []byte(`compile version go1.999`)
|
| 72 |
+
if !bytes.Equal(gotVersion, wantVersion) {
|
| 73 |
+
t.Errorf("compile version without experiment is unexpected:\ngot %q\nwant %q", gotVersion, wantVersion)
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
// Build a package in a mode not handled by the make script.
|
| 77 |
+
runCmd(t, gorootSrc, env, goCmdPath, "build", "-race", "archive/tar")
|
| 78 |
+
|
| 79 |
+
// Rebuild the toolchain with GOEXPERIMENT.
|
| 80 |
+
env = append(env, "GOEXPERIMENT=fieldtrack")
|
| 81 |
+
runCmd(t, gorootSrc, env, makeScriptPath)
|
| 82 |
+
|
| 83 |
+
// Verify compiler version string.
|
| 84 |
+
gotVersion = bytes.TrimSpace(runCmd(t, gorootSrc, env, goCmdPath, "tool", "compile", "-V=full"))
|
| 85 |
+
wantVersion = []byte(`compile version go1.999 X:fieldtrack`)
|
| 86 |
+
if !bytes.Equal(gotVersion, wantVersion) {
|
| 87 |
+
t.Errorf("compile version with experiment is unexpected:\ngot %q\nwant %q", gotVersion, wantVersion)
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
// Build the same package. We should not get a cache conflict.
|
| 91 |
+
runCmd(t, gorootSrc, env, goCmdPath, "build", "-race", "archive/tar")
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
func runCmd(t *testing.T, dir string, env []string, path string, args ...string) []byte {
|
| 95 |
+
cmd := exec.Command(path, args...)
|
| 96 |
+
cmd.Dir = dir
|
| 97 |
+
cmd.Env = env
|
| 98 |
+
out, err := cmd.Output()
|
| 99 |
+
if err != nil {
|
| 100 |
+
if ee, ok := errors.AsType[*exec.ExitError](err); ok {
|
| 101 |
+
out = append(out, ee.Stderr...)
|
| 102 |
+
}
|
| 103 |
+
t.Fatalf("%s failed:\n%s\n%s", cmd, out, err)
|
| 104 |
+
}
|
| 105 |
+
return out
|
| 106 |
+
}
|
go/src/cmd/internal/bootstrap_test/overlaydir_test.go
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2019 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 bootstrap_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"io"
|
| 9 |
+
"io/fs"
|
| 10 |
+
"os"
|
| 11 |
+
"path/filepath"
|
| 12 |
+
"strings"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
// overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
|
| 16 |
+
//
|
| 17 |
+
// TODO: Once we no longer need to support the misc module in GOPATH mode,
|
| 18 |
+
// factor this function out into a package to reduce duplication.
|
| 19 |
+
func overlayDir(dstRoot, srcRoot string) error {
|
| 20 |
+
dstRoot = filepath.Clean(dstRoot)
|
| 21 |
+
if err := os.MkdirAll(dstRoot, 0777); err != nil {
|
| 22 |
+
return err
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
srcRoot, err := filepath.Abs(srcRoot)
|
| 26 |
+
if err != nil {
|
| 27 |
+
return err
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
return filepath.WalkDir(srcRoot, func(srcPath string, entry fs.DirEntry, err error) error {
|
| 31 |
+
if err != nil || srcPath == srcRoot {
|
| 32 |
+
return err
|
| 33 |
+
}
|
| 34 |
+
if filepath.Base(srcPath) == "testdata" {
|
| 35 |
+
// We're just building, so no need to copy those.
|
| 36 |
+
return fs.SkipDir
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
suffix := strings.TrimPrefix(srcPath, srcRoot)
|
| 40 |
+
for len(suffix) > 0 && suffix[0] == filepath.Separator {
|
| 41 |
+
suffix = suffix[1:]
|
| 42 |
+
}
|
| 43 |
+
dstPath := filepath.Join(dstRoot, suffix)
|
| 44 |
+
|
| 45 |
+
info, err := entry.Info()
|
| 46 |
+
if err != nil {
|
| 47 |
+
return err
|
| 48 |
+
}
|
| 49 |
+
perm := info.Mode() & os.ModePerm
|
| 50 |
+
if info.Mode()&os.ModeSymlink != 0 {
|
| 51 |
+
info, err = os.Stat(srcPath)
|
| 52 |
+
if err != nil {
|
| 53 |
+
return err
|
| 54 |
+
}
|
| 55 |
+
perm = info.Mode() & os.ModePerm
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// Always make copies of directories.
|
| 59 |
+
// If we add a file in the overlay, we don't want to add it in the original.
|
| 60 |
+
if info.IsDir() {
|
| 61 |
+
return os.MkdirAll(dstPath, perm|0200)
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// If we can use a hard link, do that instead of copying bytes.
|
| 65 |
+
// Go builds don't like symlinks in some cases, such as go:embed.
|
| 66 |
+
if err := os.Link(srcPath, dstPath); err == nil {
|
| 67 |
+
return nil
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// Otherwise, copy the bytes.
|
| 71 |
+
src, err := os.Open(srcPath)
|
| 72 |
+
if err != nil {
|
| 73 |
+
return err
|
| 74 |
+
}
|
| 75 |
+
defer src.Close()
|
| 76 |
+
|
| 77 |
+
dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
|
| 78 |
+
if err != nil {
|
| 79 |
+
return err
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
_, err = io.Copy(dst, src)
|
| 83 |
+
if closeErr := dst.Close(); err == nil {
|
| 84 |
+
err = closeErr
|
| 85 |
+
}
|
| 86 |
+
return err
|
| 87 |
+
})
|
| 88 |
+
}
|
go/src/cmd/internal/bootstrap_test/reboot_test.go
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2019 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 bootstrap_test verifies that the current GOROOT can be used to bootstrap
|
| 6 |
+
// itself.
|
| 7 |
+
package bootstrap_test
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"fmt"
|
| 11 |
+
"internal/testenv"
|
| 12 |
+
"io"
|
| 13 |
+
"os"
|
| 14 |
+
"os/exec"
|
| 15 |
+
"path/filepath"
|
| 16 |
+
"runtime"
|
| 17 |
+
"strings"
|
| 18 |
+
"testing"
|
| 19 |
+
"time"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
func TestRepeatBootstrap(t *testing.T) {
|
| 23 |
+
if testing.Short() {
|
| 24 |
+
t.Skip("skipping test that rebuilds the entire toolchain")
|
| 25 |
+
}
|
| 26 |
+
switch runtime.GOOS {
|
| 27 |
+
case "android", "ios", "js", "wasip1":
|
| 28 |
+
t.Skipf("skipping because the toolchain does not have to bootstrap on GOOS=%s", runtime.GOOS)
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
realGoroot := testenv.GOROOT(t)
|
| 32 |
+
|
| 33 |
+
// To ensure that bootstrapping doesn't unexpectedly depend
|
| 34 |
+
// on the Go repo's git metadata, add a fake (unreadable) git
|
| 35 |
+
// directory above the simulated GOROOT.
|
| 36 |
+
// This mimics the configuration one much have when
|
| 37 |
+
// building from distro-packaged source code
|
| 38 |
+
// (see https://go.dev/issue/54852).
|
| 39 |
+
parent := t.TempDir()
|
| 40 |
+
dotGit := filepath.Join(parent, ".git")
|
| 41 |
+
if err := os.Mkdir(dotGit, 000); err != nil {
|
| 42 |
+
t.Fatal(err)
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
overlayStart := time.Now()
|
| 46 |
+
|
| 47 |
+
goroot := filepath.Join(parent, "goroot")
|
| 48 |
+
|
| 49 |
+
gorootSrc := filepath.Join(goroot, "src")
|
| 50 |
+
if err := overlayDir(gorootSrc, filepath.Join(realGoroot, "src")); err != nil {
|
| 51 |
+
t.Fatal(err)
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
gorootLib := filepath.Join(goroot, "lib")
|
| 55 |
+
if err := overlayDir(gorootLib, filepath.Join(realGoroot, "lib")); err != nil {
|
| 56 |
+
t.Fatal(err)
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
t.Logf("GOROOT overlay set up in %s", time.Since(overlayStart))
|
| 60 |
+
|
| 61 |
+
if err := os.WriteFile(filepath.Join(goroot, "VERSION"), []byte(runtime.Version()), 0666); err != nil {
|
| 62 |
+
t.Fatal(err)
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
var makeScript string
|
| 66 |
+
switch runtime.GOOS {
|
| 67 |
+
case "windows":
|
| 68 |
+
makeScript = "make.bat"
|
| 69 |
+
case "plan9":
|
| 70 |
+
makeScript = "make.rc"
|
| 71 |
+
default:
|
| 72 |
+
makeScript = "make.bash"
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
var stdout strings.Builder
|
| 76 |
+
cmd := exec.Command(filepath.Join(goroot, "src", makeScript))
|
| 77 |
+
cmd.Dir = gorootSrc
|
| 78 |
+
cmd.Env = append(cmd.Environ(), "GOROOT=", "GOROOT_BOOTSTRAP="+realGoroot)
|
| 79 |
+
cmd.Stderr = os.Stderr
|
| 80 |
+
cmd.Stdout = io.MultiWriter(os.Stdout, &stdout)
|
| 81 |
+
if err := cmd.Run(); err != nil {
|
| 82 |
+
t.Fatal(err)
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// Test that go.dev/issue/42563 hasn't regressed.
|
| 86 |
+
t.Run("PATH reminder", func(t *testing.T) {
|
| 87 |
+
var want string
|
| 88 |
+
switch gorootBin := filepath.Join(goroot, "bin"); runtime.GOOS {
|
| 89 |
+
default:
|
| 90 |
+
want = fmt.Sprintf("*** You need to add %s to your PATH.", gorootBin)
|
| 91 |
+
case "plan9":
|
| 92 |
+
want = fmt.Sprintf("*** You need to bind %s before /bin.", gorootBin)
|
| 93 |
+
}
|
| 94 |
+
if got := stdout.String(); !strings.Contains(got, want) {
|
| 95 |
+
t.Errorf("reminder %q is missing from %s stdout:\n%s", want, makeScript, got)
|
| 96 |
+
}
|
| 97 |
+
})
|
| 98 |
+
}
|
go/src/cmd/internal/browser/browser.go
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2016 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 browser provides utilities for interacting with users' browsers.
|
| 6 |
+
package browser
|
| 7 |
+
|
| 8 |
+
import (
|
| 9 |
+
"os"
|
| 10 |
+
"os/exec"
|
| 11 |
+
"runtime"
|
| 12 |
+
"time"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
// Commands returns a list of possible commands to use to open a url.
|
| 16 |
+
func Commands() [][]string {
|
| 17 |
+
var cmds [][]string
|
| 18 |
+
if exe := os.Getenv("BROWSER"); exe != "" {
|
| 19 |
+
cmds = append(cmds, []string{exe})
|
| 20 |
+
}
|
| 21 |
+
switch runtime.GOOS {
|
| 22 |
+
case "darwin":
|
| 23 |
+
cmds = append(cmds, []string{"/usr/bin/open"})
|
| 24 |
+
case "windows":
|
| 25 |
+
cmds = append(cmds, []string{"cmd", "/c", "start"})
|
| 26 |
+
default:
|
| 27 |
+
if os.Getenv("DISPLAY") != "" {
|
| 28 |
+
// xdg-open is only for use in a desktop environment.
|
| 29 |
+
cmds = append(cmds, []string{"xdg-open"})
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
cmds = append(cmds,
|
| 33 |
+
[]string{"chrome"},
|
| 34 |
+
[]string{"google-chrome"},
|
| 35 |
+
[]string{"chromium"},
|
| 36 |
+
[]string{"firefox"},
|
| 37 |
+
)
|
| 38 |
+
return cmds
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// Open tries to open url in a browser and reports whether it succeeded.
|
| 42 |
+
func Open(url string) bool {
|
| 43 |
+
for _, args := range Commands() {
|
| 44 |
+
cmd := exec.Command(args[0], append(args[1:], url)...)
|
| 45 |
+
if cmd.Start() == nil && appearsSuccessful(cmd, 3*time.Second) {
|
| 46 |
+
return true
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
return false
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
// appearsSuccessful reports whether the command appears to have run successfully.
|
| 53 |
+
// If the command runs longer than the timeout, it's deemed successful.
|
| 54 |
+
// If the command runs within the timeout, it's deemed successful if it exited cleanly.
|
| 55 |
+
func appearsSuccessful(cmd *exec.Cmd, timeout time.Duration) bool {
|
| 56 |
+
errc := make(chan error, 1)
|
| 57 |
+
go func() {
|
| 58 |
+
errc <- cmd.Wait()
|
| 59 |
+
}()
|
| 60 |
+
|
| 61 |
+
select {
|
| 62 |
+
case <-time.After(timeout):
|
| 63 |
+
return true
|
| 64 |
+
case err := <-errc:
|
| 65 |
+
return err == nil
|
| 66 |
+
}
|
| 67 |
+
}
|
go/src/cmd/internal/buildid/buildid.go
ADDED
|
@@ -0,0 +1,343 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 buildid
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"debug/elf"
|
| 10 |
+
"fmt"
|
| 11 |
+
"internal/xcoff"
|
| 12 |
+
"io"
|
| 13 |
+
"io/fs"
|
| 14 |
+
"os"
|
| 15 |
+
"strconv"
|
| 16 |
+
"strings"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
var (
|
| 20 |
+
errBuildIDMalformed = fmt.Errorf("malformed object file")
|
| 21 |
+
|
| 22 |
+
bangArch = []byte("!<arch>")
|
| 23 |
+
pkgdef = []byte("__.PKGDEF")
|
| 24 |
+
goobject = []byte("go object ")
|
| 25 |
+
buildid = []byte("build id ")
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
// ReadFile reads the build ID from an archive or executable file.
|
| 29 |
+
func ReadFile(name string) (id string, err error) {
|
| 30 |
+
f, err := os.Open(name)
|
| 31 |
+
if err != nil {
|
| 32 |
+
return "", err
|
| 33 |
+
}
|
| 34 |
+
defer f.Close()
|
| 35 |
+
|
| 36 |
+
buf := make([]byte, 8)
|
| 37 |
+
if _, err := f.ReadAt(buf, 0); err != nil {
|
| 38 |
+
return "", err
|
| 39 |
+
}
|
| 40 |
+
if string(buf) != "!<arch>\n" {
|
| 41 |
+
if string(buf) == "<bigaf>\n" {
|
| 42 |
+
return readGccgoBigArchive(name, f)
|
| 43 |
+
}
|
| 44 |
+
return readBinary(name, f)
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Read just enough of the target to fetch the build ID.
|
| 48 |
+
// The archive is expected to look like:
|
| 49 |
+
//
|
| 50 |
+
// !<arch>
|
| 51 |
+
// __.PKGDEF 0 0 0 644 7955 `
|
| 52 |
+
// go object darwin amd64 devel X:none
|
| 53 |
+
// build id "b41e5c45250e25c9fd5e9f9a1de7857ea0d41224"
|
| 54 |
+
//
|
| 55 |
+
// The variable-sized strings are GOOS, GOARCH, and the experiment list (X:none).
|
| 56 |
+
// Reading the first 1024 bytes should be plenty.
|
| 57 |
+
data := make([]byte, 1024)
|
| 58 |
+
n, err := io.ReadFull(f, data)
|
| 59 |
+
if err != nil && n == 0 {
|
| 60 |
+
return "", err
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
tryGccgo := func() (string, error) {
|
| 64 |
+
return readGccgoArchive(name, f)
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// Archive header.
|
| 68 |
+
for i := 0; ; i++ { // returns during i==3
|
| 69 |
+
j := bytes.IndexByte(data, '\n')
|
| 70 |
+
if j < 0 {
|
| 71 |
+
return tryGccgo()
|
| 72 |
+
}
|
| 73 |
+
line := data[:j]
|
| 74 |
+
data = data[j+1:]
|
| 75 |
+
switch i {
|
| 76 |
+
case 0:
|
| 77 |
+
if !bytes.Equal(line, bangArch) {
|
| 78 |
+
return tryGccgo()
|
| 79 |
+
}
|
| 80 |
+
case 1:
|
| 81 |
+
if !bytes.HasPrefix(line, pkgdef) {
|
| 82 |
+
return tryGccgo()
|
| 83 |
+
}
|
| 84 |
+
case 2:
|
| 85 |
+
if !bytes.HasPrefix(line, goobject) {
|
| 86 |
+
return tryGccgo()
|
| 87 |
+
}
|
| 88 |
+
case 3:
|
| 89 |
+
if !bytes.HasPrefix(line, buildid) {
|
| 90 |
+
// Found the object header, just doesn't have a build id line.
|
| 91 |
+
// Treat as successful, with empty build id.
|
| 92 |
+
return "", nil
|
| 93 |
+
}
|
| 94 |
+
id, err := strconv.Unquote(string(line[len(buildid):]))
|
| 95 |
+
if err != nil {
|
| 96 |
+
return tryGccgo()
|
| 97 |
+
}
|
| 98 |
+
return id, nil
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
// readGccgoArchive tries to parse the archive as a standard Unix
|
| 104 |
+
// archive file, and fetch the build ID from the _buildid.o entry.
|
| 105 |
+
// The _buildid.o entry is written by (*Builder).gccgoBuildIDELFFile
|
| 106 |
+
// in cmd/go/internal/work/exec.go.
|
| 107 |
+
func readGccgoArchive(name string, f *os.File) (string, error) {
|
| 108 |
+
bad := func() (string, error) {
|
| 109 |
+
return "", &fs.PathError{Op: "parse", Path: name, Err: errBuildIDMalformed}
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
off := int64(8)
|
| 113 |
+
for {
|
| 114 |
+
if _, err := f.Seek(off, io.SeekStart); err != nil {
|
| 115 |
+
return "", err
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
// TODO(iant): Make a debug/ar package, and use it
|
| 119 |
+
// here and in cmd/link.
|
| 120 |
+
var hdr [60]byte
|
| 121 |
+
if _, err := io.ReadFull(f, hdr[:]); err != nil {
|
| 122 |
+
if err == io.EOF {
|
| 123 |
+
// No more entries, no build ID.
|
| 124 |
+
return "", nil
|
| 125 |
+
}
|
| 126 |
+
return "", err
|
| 127 |
+
}
|
| 128 |
+
off += 60
|
| 129 |
+
|
| 130 |
+
sizeStr := strings.TrimSpace(string(hdr[48:58]))
|
| 131 |
+
size, err := strconv.ParseInt(sizeStr, 0, 64)
|
| 132 |
+
if err != nil {
|
| 133 |
+
return bad()
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
name := strings.TrimSpace(string(hdr[:16]))
|
| 137 |
+
if name == "_buildid.o/" {
|
| 138 |
+
sr := io.NewSectionReader(f, off, size)
|
| 139 |
+
e, err := elf.NewFile(sr)
|
| 140 |
+
if err != nil {
|
| 141 |
+
return bad()
|
| 142 |
+
}
|
| 143 |
+
s := e.Section(".go.buildid")
|
| 144 |
+
if s == nil {
|
| 145 |
+
return bad()
|
| 146 |
+
}
|
| 147 |
+
data, err := s.Data()
|
| 148 |
+
if err != nil {
|
| 149 |
+
return bad()
|
| 150 |
+
}
|
| 151 |
+
return string(data), nil
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
off += size
|
| 155 |
+
if off&1 != 0 {
|
| 156 |
+
off++
|
| 157 |
+
}
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
// readGccgoBigArchive tries to parse the archive as an AIX big
|
| 162 |
+
// archive file, and fetch the build ID from the _buildid.o entry.
|
| 163 |
+
// The _buildid.o entry is written by (*Builder).gccgoBuildIDXCOFFFile
|
| 164 |
+
// in cmd/go/internal/work/exec.go.
|
| 165 |
+
func readGccgoBigArchive(name string, f *os.File) (string, error) {
|
| 166 |
+
bad := func() (string, error) {
|
| 167 |
+
return "", &fs.PathError{Op: "parse", Path: name, Err: errBuildIDMalformed}
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// Read fixed-length header.
|
| 171 |
+
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
| 172 |
+
return "", err
|
| 173 |
+
}
|
| 174 |
+
var flhdr [128]byte
|
| 175 |
+
if _, err := io.ReadFull(f, flhdr[:]); err != nil {
|
| 176 |
+
return "", err
|
| 177 |
+
}
|
| 178 |
+
// Read first member offset.
|
| 179 |
+
offStr := strings.TrimSpace(string(flhdr[68:88]))
|
| 180 |
+
off, err := strconv.ParseInt(offStr, 10, 64)
|
| 181 |
+
if err != nil {
|
| 182 |
+
return bad()
|
| 183 |
+
}
|
| 184 |
+
for {
|
| 185 |
+
if off == 0 {
|
| 186 |
+
// No more entries, no build ID.
|
| 187 |
+
return "", nil
|
| 188 |
+
}
|
| 189 |
+
if _, err := f.Seek(off, io.SeekStart); err != nil {
|
| 190 |
+
return "", err
|
| 191 |
+
}
|
| 192 |
+
// Read member header.
|
| 193 |
+
var hdr [112]byte
|
| 194 |
+
if _, err := io.ReadFull(f, hdr[:]); err != nil {
|
| 195 |
+
return "", err
|
| 196 |
+
}
|
| 197 |
+
// Read member name length.
|
| 198 |
+
namLenStr := strings.TrimSpace(string(hdr[108:112]))
|
| 199 |
+
namLen, err := strconv.ParseInt(namLenStr, 10, 32)
|
| 200 |
+
if err != nil {
|
| 201 |
+
return bad()
|
| 202 |
+
}
|
| 203 |
+
if namLen == 10 {
|
| 204 |
+
var nam [10]byte
|
| 205 |
+
if _, err := io.ReadFull(f, nam[:]); err != nil {
|
| 206 |
+
return "", err
|
| 207 |
+
}
|
| 208 |
+
if string(nam[:]) == "_buildid.o" {
|
| 209 |
+
sizeStr := strings.TrimSpace(string(hdr[0:20]))
|
| 210 |
+
size, err := strconv.ParseInt(sizeStr, 10, 64)
|
| 211 |
+
if err != nil {
|
| 212 |
+
return bad()
|
| 213 |
+
}
|
| 214 |
+
off += int64(len(hdr)) + namLen + 2
|
| 215 |
+
if off&1 != 0 {
|
| 216 |
+
off++
|
| 217 |
+
}
|
| 218 |
+
sr := io.NewSectionReader(f, off, size)
|
| 219 |
+
x, err := xcoff.NewFile(sr)
|
| 220 |
+
if err != nil {
|
| 221 |
+
return bad()
|
| 222 |
+
}
|
| 223 |
+
data := x.CSect(".go.buildid")
|
| 224 |
+
if data == nil {
|
| 225 |
+
return bad()
|
| 226 |
+
}
|
| 227 |
+
return string(data), nil
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
// Read next member offset.
|
| 232 |
+
offStr = strings.TrimSpace(string(hdr[20:40]))
|
| 233 |
+
off, err = strconv.ParseInt(offStr, 10, 64)
|
| 234 |
+
if err != nil {
|
| 235 |
+
return bad()
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
var (
|
| 241 |
+
goBuildPrefix = []byte("\xff Go build ID: \"")
|
| 242 |
+
goBuildEnd = []byte("\"\n \xff")
|
| 243 |
+
|
| 244 |
+
elfPrefix = []byte("\x7fELF")
|
| 245 |
+
|
| 246 |
+
machoPrefixes = [][]byte{
|
| 247 |
+
{0xfe, 0xed, 0xfa, 0xce},
|
| 248 |
+
{0xfe, 0xed, 0xfa, 0xcf},
|
| 249 |
+
{0xce, 0xfa, 0xed, 0xfe},
|
| 250 |
+
{0xcf, 0xfa, 0xed, 0xfe},
|
| 251 |
+
}
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
var readSize = 32 * 1024 // changed for testing
|
| 255 |
+
|
| 256 |
+
// readBinary reads the build ID from a binary.
|
| 257 |
+
//
|
| 258 |
+
// ELF binaries store the build ID in a proper PT_NOTE section.
|
| 259 |
+
//
|
| 260 |
+
// Other binary formats are not so flexible. For those, the linker
|
| 261 |
+
// stores the build ID as non-instruction bytes at the very beginning
|
| 262 |
+
// of the text segment, which should appear near the beginning
|
| 263 |
+
// of the file. This is clumsy but fairly portable. Custom locations
|
| 264 |
+
// can be added for other binary types as needed, like we did for ELF.
|
| 265 |
+
func readBinary(name string, f *os.File) (id string, err error) {
|
| 266 |
+
// Read the first 32 kB of the binary file.
|
| 267 |
+
// That should be enough to find the build ID.
|
| 268 |
+
// In ELF files, the build ID is in the leading headers,
|
| 269 |
+
// which are typically less than 4 kB, not to mention 32 kB.
|
| 270 |
+
// In Mach-O files, there's no limit, so we have to parse the file.
|
| 271 |
+
// On other systems, we're trying to read enough that
|
| 272 |
+
// we get the beginning of the text segment in the read.
|
| 273 |
+
// The offset where the text segment begins in a hello
|
| 274 |
+
// world compiled for each different object format today:
|
| 275 |
+
//
|
| 276 |
+
// Plan 9: 0x20
|
| 277 |
+
// Windows: 0x600
|
| 278 |
+
//
|
| 279 |
+
data := make([]byte, readSize)
|
| 280 |
+
_, err = io.ReadFull(f, data)
|
| 281 |
+
if err == io.ErrUnexpectedEOF {
|
| 282 |
+
err = nil
|
| 283 |
+
}
|
| 284 |
+
if err != nil {
|
| 285 |
+
return "", err
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
if bytes.HasPrefix(data, elfPrefix) {
|
| 289 |
+
return readELF(name, f, data)
|
| 290 |
+
}
|
| 291 |
+
for _, m := range machoPrefixes {
|
| 292 |
+
if bytes.HasPrefix(data, m) {
|
| 293 |
+
return readMacho(name, f, data)
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
return readRaw(name, data)
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
// readRaw finds the raw build ID stored in text segment data.
|
| 300 |
+
func readRaw(name string, data []byte) (id string, err error) {
|
| 301 |
+
i := bytes.Index(data, goBuildPrefix)
|
| 302 |
+
if i < 0 {
|
| 303 |
+
// Missing. Treat as successful but build ID empty.
|
| 304 |
+
return "", nil
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
j := bytes.Index(data[i+len(goBuildPrefix):], goBuildEnd)
|
| 308 |
+
if j < 0 {
|
| 309 |
+
return "", &fs.PathError{Op: "parse", Path: name, Err: errBuildIDMalformed}
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
quoted := data[i+len(goBuildPrefix)-1 : i+len(goBuildPrefix)+j+1]
|
| 313 |
+
id, err = strconv.Unquote(string(quoted))
|
| 314 |
+
if err != nil {
|
| 315 |
+
return "", &fs.PathError{Op: "parse", Path: name, Err: errBuildIDMalformed}
|
| 316 |
+
}
|
| 317 |
+
return id, nil
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
// HashToString converts the hash h to a string to be recorded
|
| 321 |
+
// in package archives and binaries as part of the build ID.
|
| 322 |
+
// We use the first 120 bits of the hash (5 chunks of 24 bits each) and encode
|
| 323 |
+
// it in base64, resulting in a 20-byte string. Because this is only used for
|
| 324 |
+
// detecting the need to rebuild installed files (not for lookups
|
| 325 |
+
// in the object file cache), 120 bits are sufficient to drive the
|
| 326 |
+
// probability of a false "do not need to rebuild" decision to effectively zero.
|
| 327 |
+
// We embed two different hashes in archives and four in binaries,
|
| 328 |
+
// so cutting to 20 bytes is a significant savings when build IDs are displayed.
|
| 329 |
+
// (20*4+3 = 83 bytes compared to 64*4+3 = 259 bytes for the
|
| 330 |
+
// more straightforward option of printing the entire h in base64).
|
| 331 |
+
func HashToString(h [32]byte) string {
|
| 332 |
+
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
| 333 |
+
const chunks = 5
|
| 334 |
+
var dst [chunks * 4]byte
|
| 335 |
+
for i := 0; i < chunks; i++ {
|
| 336 |
+
v := uint32(h[3*i])<<16 | uint32(h[3*i+1])<<8 | uint32(h[3*i+2])
|
| 337 |
+
dst[4*i+0] = b64[(v>>18)&0x3F]
|
| 338 |
+
dst[4*i+1] = b64[(v>>12)&0x3F]
|
| 339 |
+
dst[4*i+2] = b64[(v>>6)&0x3F]
|
| 340 |
+
dst[4*i+3] = b64[v&0x3F]
|
| 341 |
+
}
|
| 342 |
+
return string(dst[:])
|
| 343 |
+
}
|
go/src/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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 buildid
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"crypto/sha256"
|
| 10 |
+
"debug/elf"
|
| 11 |
+
"encoding/binary"
|
| 12 |
+
"internal/obscuretestdata"
|
| 13 |
+
"os"
|
| 14 |
+
"reflect"
|
| 15 |
+
"strings"
|
| 16 |
+
"testing"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
const (
|
| 20 |
+
expectedID = "abcdefghijklmnopqrstuvwxyz.1234567890123456789012345678901234567890123456789012345678901234"
|
| 21 |
+
newID = "bcdefghijklmnopqrstuvwxyza.2345678901234567890123456789012345678901234567890123456789012341"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
func TestReadFile(t *testing.T) {
|
| 25 |
+
f, err := os.CreateTemp("", "buildid-test-")
|
| 26 |
+
if err != nil {
|
| 27 |
+
t.Fatal(err)
|
| 28 |
+
}
|
| 29 |
+
tmp := f.Name()
|
| 30 |
+
defer os.Remove(tmp)
|
| 31 |
+
f.Close()
|
| 32 |
+
|
| 33 |
+
// Use obscured files to prevent Apple’s notarization service from
|
| 34 |
+
// mistaking them as candidates for notarization and rejecting the entire
|
| 35 |
+
// toolchain.
|
| 36 |
+
// See golang.org/issue/34986
|
| 37 |
+
var files = []string{
|
| 38 |
+
"p.a.base64",
|
| 39 |
+
"a.elf.base64",
|
| 40 |
+
"a.macho.base64",
|
| 41 |
+
"a.pe.base64",
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
for _, name := range files {
|
| 45 |
+
f, err := obscuretestdata.DecodeToTempFile("testdata/" + name)
|
| 46 |
+
if err != nil {
|
| 47 |
+
t.Errorf("obscuretestdata.DecodeToTempFile(testdata/%s): %v", name, err)
|
| 48 |
+
continue
|
| 49 |
+
}
|
| 50 |
+
defer os.Remove(f)
|
| 51 |
+
id, err := ReadFile(f)
|
| 52 |
+
if id != expectedID || err != nil {
|
| 53 |
+
t.Errorf("ReadFile(testdata/%s) = %q, %v, want %q, nil", f, id, err, expectedID)
|
| 54 |
+
}
|
| 55 |
+
old := readSize
|
| 56 |
+
readSize = 2048
|
| 57 |
+
id, err = ReadFile(f)
|
| 58 |
+
readSize = old
|
| 59 |
+
if id != expectedID || err != nil {
|
| 60 |
+
t.Errorf("ReadFile(%s) [readSize=2k] = %q, %v, want %q, nil", f, id, err, expectedID)
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
data, err := os.ReadFile(f)
|
| 64 |
+
if err != nil {
|
| 65 |
+
t.Fatal(err)
|
| 66 |
+
}
|
| 67 |
+
m, _, err := FindAndHash(bytes.NewReader(data), expectedID, 1024)
|
| 68 |
+
if err != nil {
|
| 69 |
+
t.Errorf("FindAndHash(%s): %v", f, err)
|
| 70 |
+
continue
|
| 71 |
+
}
|
| 72 |
+
if err := os.WriteFile(tmp, data, 0666); err != nil {
|
| 73 |
+
t.Error(err)
|
| 74 |
+
continue
|
| 75 |
+
}
|
| 76 |
+
tf, err := os.OpenFile(tmp, os.O_WRONLY, 0)
|
| 77 |
+
if err != nil {
|
| 78 |
+
t.Error(err)
|
| 79 |
+
continue
|
| 80 |
+
}
|
| 81 |
+
err = Rewrite(tf, m, newID)
|
| 82 |
+
err2 := tf.Close()
|
| 83 |
+
if err != nil {
|
| 84 |
+
t.Errorf("Rewrite(%s): %v", f, err)
|
| 85 |
+
continue
|
| 86 |
+
}
|
| 87 |
+
if err2 != nil {
|
| 88 |
+
t.Fatal(err2)
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
id, err = ReadFile(tmp)
|
| 92 |
+
if id != newID || err != nil {
|
| 93 |
+
t.Errorf("ReadFile(%s after Rewrite) = %q, %v, want %q, nil", f, id, err, newID)
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// Test an ELF PT_NOTE segment with an Align field of 0.
|
| 97 |
+
// Do this by rewriting the file data.
|
| 98 |
+
if strings.Contains(name, "elf") {
|
| 99 |
+
// We only expect a 64-bit ELF file.
|
| 100 |
+
if elf.Class(data[elf.EI_CLASS]) != elf.ELFCLASS64 {
|
| 101 |
+
continue
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// We only expect a little-endian ELF file.
|
| 105 |
+
if elf.Data(data[elf.EI_DATA]) != elf.ELFDATA2LSB {
|
| 106 |
+
continue
|
| 107 |
+
}
|
| 108 |
+
order := binary.LittleEndian
|
| 109 |
+
|
| 110 |
+
var hdr elf.Header64
|
| 111 |
+
if err := binary.Read(bytes.NewReader(data), order, &hdr); err != nil {
|
| 112 |
+
t.Error(err)
|
| 113 |
+
continue
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
phoff := hdr.Phoff
|
| 117 |
+
phnum := int(hdr.Phnum)
|
| 118 |
+
phsize := uint64(hdr.Phentsize)
|
| 119 |
+
|
| 120 |
+
for i := 0; i < phnum; i++ {
|
| 121 |
+
var phdr elf.Prog64
|
| 122 |
+
if err := binary.Read(bytes.NewReader(data[phoff:]), order, &phdr); err != nil {
|
| 123 |
+
t.Error(err)
|
| 124 |
+
continue
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
if elf.ProgType(phdr.Type) == elf.PT_NOTE {
|
| 128 |
+
// Increase the size so we keep
|
| 129 |
+
// reading notes.
|
| 130 |
+
order.PutUint64(data[phoff+4*8:], phdr.Filesz+1)
|
| 131 |
+
|
| 132 |
+
// Clobber the Align field to zero.
|
| 133 |
+
order.PutUint64(data[phoff+6*8:], 0)
|
| 134 |
+
|
| 135 |
+
// Clobber the note type so we
|
| 136 |
+
// keep reading notes.
|
| 137 |
+
order.PutUint32(data[phdr.Off+12:], 0)
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
phoff += phsize
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
if err := os.WriteFile(tmp, data, 0666); err != nil {
|
| 144 |
+
t.Error(err)
|
| 145 |
+
continue
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
id, err := ReadFile(tmp)
|
| 149 |
+
// Because we clobbered the note type above,
|
| 150 |
+
// we don't expect to see a Go build ID.
|
| 151 |
+
// The issue we are testing for was a crash
|
| 152 |
+
// in Readfile; see issue #62097.
|
| 153 |
+
if id != "" || err != nil {
|
| 154 |
+
t.Errorf("ReadFile with zero ELF Align = %q, %v, want %q, nil", id, err, "")
|
| 155 |
+
continue
|
| 156 |
+
}
|
| 157 |
+
}
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
func TestFindAndHash(t *testing.T) {
|
| 162 |
+
buf := make([]byte, 64)
|
| 163 |
+
buf2 := make([]byte, 64)
|
| 164 |
+
id := make([]byte, 8)
|
| 165 |
+
zero := make([]byte, 8)
|
| 166 |
+
for i := range id {
|
| 167 |
+
id[i] = byte(i)
|
| 168 |
+
}
|
| 169 |
+
numError := 0
|
| 170 |
+
errorf := func(msg string, args ...any) {
|
| 171 |
+
t.Errorf(msg, args...)
|
| 172 |
+
if numError++; numError > 20 {
|
| 173 |
+
t.Logf("stopping after too many errors")
|
| 174 |
+
t.FailNow()
|
| 175 |
+
}
|
| 176 |
+
}
|
| 177 |
+
for bufSize := len(id); bufSize <= len(buf); bufSize++ {
|
| 178 |
+
for j := range buf {
|
| 179 |
+
for k := 0; k < 2*len(id) && j+k < len(buf); k++ {
|
| 180 |
+
for i := range buf {
|
| 181 |
+
buf[i] = 1
|
| 182 |
+
}
|
| 183 |
+
copy(buf[j:], id)
|
| 184 |
+
copy(buf[j+k:], id)
|
| 185 |
+
var m []int64
|
| 186 |
+
if j+len(id) <= j+k {
|
| 187 |
+
m = append(m, int64(j))
|
| 188 |
+
}
|
| 189 |
+
if j+k+len(id) <= len(buf) {
|
| 190 |
+
m = append(m, int64(j+k))
|
| 191 |
+
}
|
| 192 |
+
copy(buf2, buf)
|
| 193 |
+
for _, p := range m {
|
| 194 |
+
copy(buf2[p:], zero)
|
| 195 |
+
}
|
| 196 |
+
h := sha256.Sum256(buf2)
|
| 197 |
+
|
| 198 |
+
matches, hash, err := FindAndHash(bytes.NewReader(buf), string(id), bufSize)
|
| 199 |
+
if err != nil {
|
| 200 |
+
errorf("bufSize=%d j=%d k=%d: findAndHash: %v", bufSize, j, k, err)
|
| 201 |
+
continue
|
| 202 |
+
}
|
| 203 |
+
if !reflect.DeepEqual(matches, m) {
|
| 204 |
+
errorf("bufSize=%d j=%d k=%d: findAndHash: matches=%v, want %v", bufSize, j, k, matches, m)
|
| 205 |
+
continue
|
| 206 |
+
}
|
| 207 |
+
if hash != h {
|
| 208 |
+
errorf("bufSize=%d j=%d k=%d: findAndHash: matches correct, but hash=%x, want %x", bufSize, j, k, hash, h)
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
func TestExcludedReader(t *testing.T) {
|
| 216 |
+
const s = "0123456789abcdefghijklmn"
|
| 217 |
+
tests := []struct {
|
| 218 |
+
start, end int64 // excluded range
|
| 219 |
+
results []string // expected results of reads
|
| 220 |
+
}{
|
| 221 |
+
{12, 15, []string{"0123456789", "ab\x00\x00\x00fghij", "klmn"}}, // within one read
|
| 222 |
+
{8, 21, []string{"01234567\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", "\x00lmn"}}, // across multiple reads
|
| 223 |
+
{10, 20, []string{"0123456789", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", "klmn"}}, // a whole read
|
| 224 |
+
{0, 5, []string{"\x00\x00\x00\x00\x0056789", "abcdefghij", "klmn"}}, // start
|
| 225 |
+
{12, 24, []string{"0123456789", "ab\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00"}}, // end
|
| 226 |
+
}
|
| 227 |
+
p := make([]byte, 10)
|
| 228 |
+
for _, test := range tests {
|
| 229 |
+
r := &excludedReader{strings.NewReader(s), 0, test.start, test.end}
|
| 230 |
+
for _, res := range test.results {
|
| 231 |
+
n, err := r.Read(p)
|
| 232 |
+
if err != nil {
|
| 233 |
+
t.Errorf("read failed: %v", err)
|
| 234 |
+
}
|
| 235 |
+
if n != len(res) {
|
| 236 |
+
t.Errorf("unexpected number of bytes read: want %d, got %d", len(res), n)
|
| 237 |
+
}
|
| 238 |
+
if string(p[:n]) != res {
|
| 239 |
+
t.Errorf("unexpected bytes: want %q, got %q", res, p[:n])
|
| 240 |
+
}
|
| 241 |
+
}
|
| 242 |
+
}
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
func TestEmptyID(t *testing.T) {
|
| 246 |
+
r := strings.NewReader("aha!")
|
| 247 |
+
matches, hash, err := FindAndHash(r, "", 1000)
|
| 248 |
+
if matches != nil || hash != ([32]byte{}) || err == nil || !strings.Contains(err.Error(), "no id") {
|
| 249 |
+
t.Errorf("FindAndHash: want nil, [32]byte{}, no id specified, got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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2015 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 buildid
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"debug/elf"
|
| 10 |
+
"debug/macho"
|
| 11 |
+
"encoding/binary"
|
| 12 |
+
"fmt"
|
| 13 |
+
"io"
|
| 14 |
+
"io/fs"
|
| 15 |
+
"os"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
func readAligned4(r io.Reader, sz int32) ([]byte, error) {
|
| 19 |
+
full := (sz + 3) &^ 3
|
| 20 |
+
data := make([]byte, full)
|
| 21 |
+
_, err := io.ReadFull(r, data)
|
| 22 |
+
if err != nil {
|
| 23 |
+
return nil, err
|
| 24 |
+
}
|
| 25 |
+
data = data[:sz]
|
| 26 |
+
return data, nil
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
func ReadELFNote(filename, name string, typ int32) ([]byte, error) {
|
| 30 |
+
f, err := elf.Open(filename)
|
| 31 |
+
if err != nil {
|
| 32 |
+
return nil, err
|
| 33 |
+
}
|
| 34 |
+
defer f.Close()
|
| 35 |
+
for _, sect := range f.Sections {
|
| 36 |
+
if sect.Type != elf.SHT_NOTE {
|
| 37 |
+
continue
|
| 38 |
+
}
|
| 39 |
+
r := sect.Open()
|
| 40 |
+
for {
|
| 41 |
+
var namesize, descsize, noteType int32
|
| 42 |
+
err = binary.Read(r, f.ByteOrder, &namesize)
|
| 43 |
+
if err != nil {
|
| 44 |
+
if err == io.EOF {
|
| 45 |
+
break
|
| 46 |
+
}
|
| 47 |
+
return nil, fmt.Errorf("read namesize failed: %v", err)
|
| 48 |
+
}
|
| 49 |
+
err = binary.Read(r, f.ByteOrder, &descsize)
|
| 50 |
+
if err != nil {
|
| 51 |
+
return nil, fmt.Errorf("read descsize failed: %v", err)
|
| 52 |
+
}
|
| 53 |
+
err = binary.Read(r, f.ByteOrder, ¬eType)
|
| 54 |
+
if err != nil {
|
| 55 |
+
return nil, fmt.Errorf("read type failed: %v", err)
|
| 56 |
+
}
|
| 57 |
+
noteName, err := readAligned4(r, namesize)
|
| 58 |
+
if err != nil {
|
| 59 |
+
return nil, fmt.Errorf("read name failed: %v", err)
|
| 60 |
+
}
|
| 61 |
+
desc, err := readAligned4(r, descsize)
|
| 62 |
+
if err != nil {
|
| 63 |
+
return nil, fmt.Errorf("read desc failed: %v", err)
|
| 64 |
+
}
|
| 65 |
+
if name == string(noteName) && typ == noteType {
|
| 66 |
+
return desc, nil
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
return nil, nil
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
var elfGoNote = []byte("Go\x00\x00")
|
| 74 |
+
var elfGNUNote = []byte("GNU\x00")
|
| 75 |
+
|
| 76 |
+
// The Go build ID is stored in a note described by an ELF PT_NOTE prog
|
| 77 |
+
// header. The caller has already opened filename, to get f, and read
|
| 78 |
+
// at least 4 kB out, in data.
|
| 79 |
+
func readELF(name string, f *os.File, data []byte) (buildid string, err error) {
|
| 80 |
+
// Assume the note content is in the data, already read.
|
| 81 |
+
// Rewrite the ELF header to set shoff and shnum to 0, so that we can pass
|
| 82 |
+
// the data to elf.NewFile and it will decode the Prog list but not
|
| 83 |
+
// try to read the section headers and the string table from disk.
|
| 84 |
+
// That's a waste of I/O when all we care about is the Prog list
|
| 85 |
+
// and the one ELF note.
|
| 86 |
+
switch elf.Class(data[elf.EI_CLASS]) {
|
| 87 |
+
case elf.ELFCLASS32:
|
| 88 |
+
data[32], data[33], data[34], data[35] = 0, 0, 0, 0
|
| 89 |
+
data[48] = 0
|
| 90 |
+
data[49] = 0
|
| 91 |
+
case elf.ELFCLASS64:
|
| 92 |
+
data[40], data[41], data[42], data[43] = 0, 0, 0, 0
|
| 93 |
+
data[44], data[45], data[46], data[47] = 0, 0, 0, 0
|
| 94 |
+
data[60] = 0
|
| 95 |
+
data[61] = 0
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
const elfGoBuildIDTag = 4
|
| 99 |
+
const gnuBuildIDTag = 3
|
| 100 |
+
|
| 101 |
+
ef, err := elf.NewFile(bytes.NewReader(data))
|
| 102 |
+
if err != nil {
|
| 103 |
+
return "", &fs.PathError{Path: name, Op: "parse", Err: err}
|
| 104 |
+
}
|
| 105 |
+
var gnu string
|
| 106 |
+
for _, p := range ef.Progs {
|
| 107 |
+
if p.Type != elf.PT_NOTE || p.Filesz < 16 {
|
| 108 |
+
continue
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
var note []byte
|
| 112 |
+
if p.Off+p.Filesz < uint64(len(data)) {
|
| 113 |
+
note = data[p.Off : p.Off+p.Filesz]
|
| 114 |
+
} else {
|
| 115 |
+
// For some linkers, such as the Solaris linker,
|
| 116 |
+
// the buildid may not be found in data (which
|
| 117 |
+
// likely contains the first 16kB of the file)
|
| 118 |
+
// or even the first few megabytes of the file
|
| 119 |
+
// due to differences in note segment placement;
|
| 120 |
+
// in that case, extract the note data manually.
|
| 121 |
+
_, err = f.Seek(int64(p.Off), io.SeekStart)
|
| 122 |
+
if err != nil {
|
| 123 |
+
return "", err
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
note = make([]byte, p.Filesz)
|
| 127 |
+
_, err = io.ReadFull(f, note)
|
| 128 |
+
if err != nil {
|
| 129 |
+
return "", err
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
filesz := p.Filesz
|
| 134 |
+
off := p.Off
|
| 135 |
+
for filesz >= 16 {
|
| 136 |
+
nameSize := ef.ByteOrder.Uint32(note)
|
| 137 |
+
valSize := ef.ByteOrder.Uint32(note[4:])
|
| 138 |
+
tag := ef.ByteOrder.Uint32(note[8:])
|
| 139 |
+
nname := note[12:16]
|
| 140 |
+
if nameSize == 4 && 16+valSize <= uint32(len(note)) && tag == elfGoBuildIDTag && bytes.Equal(nname, elfGoNote) {
|
| 141 |
+
return string(note[16 : 16+valSize]), nil
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
if nameSize == 4 && 16+valSize <= uint32(len(note)) && tag == gnuBuildIDTag && bytes.Equal(nname, elfGNUNote) {
|
| 145 |
+
gnu = string(note[16 : 16+valSize])
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
nameSize = (nameSize + 3) &^ 3
|
| 149 |
+
valSize = (valSize + 3) &^ 3
|
| 150 |
+
notesz := uint64(12 + nameSize + valSize)
|
| 151 |
+
if filesz <= notesz {
|
| 152 |
+
break
|
| 153 |
+
}
|
| 154 |
+
off += notesz
|
| 155 |
+
align := p.Align
|
| 156 |
+
if align != 0 {
|
| 157 |
+
alignedOff := (off + align - 1) &^ (align - 1)
|
| 158 |
+
notesz += alignedOff - off
|
| 159 |
+
off = alignedOff
|
| 160 |
+
}
|
| 161 |
+
filesz -= notesz
|
| 162 |
+
note = note[notesz:]
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
// If we didn't find a Go note, use a GNU note if available.
|
| 167 |
+
// This is what gccgo uses.
|
| 168 |
+
if gnu != "" {
|
| 169 |
+
return gnu, nil
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
// No note. Treat as successful but build ID empty.
|
| 173 |
+
return "", nil
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
// The Go build ID is stored at the beginning of the Mach-O __text segment.
|
| 177 |
+
// The caller has already opened filename, to get f, and read a few kB out, in data.
|
| 178 |
+
// Sadly, that's not guaranteed to hold the note, because there is an arbitrary amount
|
| 179 |
+
// of other junk placed in the file ahead of the main text.
|
| 180 |
+
func readMacho(name string, f *os.File, data []byte) (buildid string, err error) {
|
| 181 |
+
// If the data we want has already been read, don't worry about Mach-O parsing.
|
| 182 |
+
// This is both an optimization and a hedge against the Mach-O parsing failing
|
| 183 |
+
// in the future due to, for example, the name of the __text section changing.
|
| 184 |
+
if b, err := readRaw(name, data); b != "" && err == nil {
|
| 185 |
+
return b, err
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
mf, err := macho.NewFile(f)
|
| 189 |
+
if err != nil {
|
| 190 |
+
return "", &fs.PathError{Path: name, Op: "parse", Err: err}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
sect := mf.Section("__text")
|
| 194 |
+
if sect == nil {
|
| 195 |
+
// Every binary has a __text section. Something is wrong.
|
| 196 |
+
return "", &fs.PathError{Path: name, Op: "parse", Err: fmt.Errorf("cannot find __text section")}
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
// It should be in the first few bytes, but read a lot just in case,
|
| 200 |
+
// especially given our past problems on OS X with the build ID moving.
|
| 201 |
+
// There shouldn't be much difference between reading 4kB and 32kB:
|
| 202 |
+
// the hard part is getting to the data, not transferring it.
|
| 203 |
+
n := sect.Size
|
| 204 |
+
if n > uint64(readSize) {
|
| 205 |
+
n = uint64(readSize)
|
| 206 |
+
}
|
| 207 |
+
buf := make([]byte, n)
|
| 208 |
+
if _, err := f.ReadAt(buf, int64(sect.Offset)); err != nil {
|
| 209 |
+
return "", err
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
return readRaw(name, buf)
|
| 213 |
+
}
|
go/src/cmd/internal/buildid/rewrite.go
ADDED
|
@@ -0,0 +1,220 @@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 buildid
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"cmd/internal/codesign"
|
| 10 |
+
imacho "cmd/internal/macho"
|
| 11 |
+
"crypto/sha256"
|
| 12 |
+
"debug/elf"
|
| 13 |
+
"debug/macho"
|
| 14 |
+
"fmt"
|
| 15 |
+
"io"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
// FindAndHash reads all of r and returns the offsets of occurrences of id.
|
| 19 |
+
// While reading, findAndHash also computes and returns
|
| 20 |
+
// a hash of the content of r, but with occurrences of id replaced by zeros.
|
| 21 |
+
// FindAndHash reads bufSize bytes from r at a time.
|
| 22 |
+
// If bufSize == 0, FindAndHash uses a reasonable default.
|
| 23 |
+
func FindAndHash(r io.Reader, id string, bufSize int) (matches []int64, hash [32]byte, err error) {
|
| 24 |
+
if bufSize == 0 {
|
| 25 |
+
bufSize = 31 * 1024 // bufSize+little will likely fit in 32 kB
|
| 26 |
+
}
|
| 27 |
+
if len(id) == 0 {
|
| 28 |
+
return nil, [32]byte{}, fmt.Errorf("buildid.FindAndHash: no id specified")
|
| 29 |
+
}
|
| 30 |
+
if len(id) > bufSize {
|
| 31 |
+
return nil, [32]byte{}, fmt.Errorf("buildid.FindAndHash: buffer too small")
|
| 32 |
+
}
|
| 33 |
+
zeros := make([]byte, len(id))
|
| 34 |
+
idBytes := []byte(id)
|
| 35 |
+
|
| 36 |
+
r0 := r // preserve original type of r
|
| 37 |
+
|
| 38 |
+
// For Mach-O files, we want to exclude the code signature.
|
| 39 |
+
// The code signature contains hashes of the whole file (except the signature
|
| 40 |
+
// itself), including the buildid. So the buildid cannot contain the signature.
|
| 41 |
+
r = excludeMachoCodeSignature(r)
|
| 42 |
+
|
| 43 |
+
// With the "-B gobuildid" linker option (which will be the default on some
|
| 44 |
+
// platforms), the host build ID (GNU build ID, Mach-O UUID) depends on the
|
| 45 |
+
// Go buildid. So ignore the host build ID, to avoid convergence problem.
|
| 46 |
+
r = excludeHostBuildID(r, r0)
|
| 47 |
+
|
| 48 |
+
// The strategy is to read the file through buf, looking for id,
|
| 49 |
+
// but we need to worry about what happens if id is broken up
|
| 50 |
+
// and returned in parts by two different reads.
|
| 51 |
+
// We allocate a tiny buffer (at least len(id)) and a big buffer (bufSize bytes)
|
| 52 |
+
// next to each other in memory and then copy the tail of
|
| 53 |
+
// one read into the tiny buffer before reading new data into the big buffer.
|
| 54 |
+
// The search for id is over the entire tiny+big buffer.
|
| 55 |
+
tiny := (len(id) + 127) &^ 127 // round up to 128-aligned
|
| 56 |
+
buf := make([]byte, tiny+bufSize)
|
| 57 |
+
h := sha256.New()
|
| 58 |
+
start := tiny
|
| 59 |
+
for offset := int64(0); ; {
|
| 60 |
+
// The file offset maintained by the loop corresponds to &buf[tiny].
|
| 61 |
+
// buf[start:tiny] is left over from previous iteration.
|
| 62 |
+
// After reading n bytes into buf[tiny:], we process buf[start:tiny+n].
|
| 63 |
+
n, err := io.ReadFull(r, buf[tiny:])
|
| 64 |
+
if err != io.ErrUnexpectedEOF && err != io.EOF && err != nil {
|
| 65 |
+
return nil, [32]byte{}, err
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// Process any matches.
|
| 69 |
+
for {
|
| 70 |
+
i := bytes.Index(buf[start:tiny+n], idBytes)
|
| 71 |
+
if i < 0 {
|
| 72 |
+
break
|
| 73 |
+
}
|
| 74 |
+
matches = append(matches, offset+int64(start+i-tiny))
|
| 75 |
+
h.Write(buf[start : start+i])
|
| 76 |
+
h.Write(zeros)
|
| 77 |
+
start += i + len(id)
|
| 78 |
+
}
|
| 79 |
+
if n < bufSize {
|
| 80 |
+
// Did not fill buffer, must be at end of file.
|
| 81 |
+
h.Write(buf[start : tiny+n])
|
| 82 |
+
break
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// Process all but final tiny bytes of buf (bufSize = len(buf)-tiny).
|
| 86 |
+
// Note that start > len(buf)-tiny is possible, if the search above
|
| 87 |
+
// found an id ending in the final tiny fringe. That's OK.
|
| 88 |
+
if start < len(buf)-tiny {
|
| 89 |
+
h.Write(buf[start : len(buf)-tiny])
|
| 90 |
+
start = len(buf) - tiny
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// Slide ending tiny-sized fringe to beginning of buffer.
|
| 94 |
+
copy(buf[0:], buf[bufSize:])
|
| 95 |
+
start -= bufSize
|
| 96 |
+
offset += int64(bufSize)
|
| 97 |
+
}
|
| 98 |
+
h.Sum(hash[:0])
|
| 99 |
+
return matches, hash, nil
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
func Rewrite(w io.WriterAt, pos []int64, id string) error {
|
| 103 |
+
b := []byte(id)
|
| 104 |
+
for _, p := range pos {
|
| 105 |
+
if _, err := w.WriteAt(b, p); err != nil {
|
| 106 |
+
return err
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Update Mach-O code signature, if any.
|
| 111 |
+
if f, cmd, ok := findMachoCodeSignature(w); ok {
|
| 112 |
+
if codesign.Size(int64(cmd.Dataoff), "a.out") == int64(cmd.Datasize) {
|
| 113 |
+
// Update the signature if the size matches, so we don't need to
|
| 114 |
+
// fix up headers. Binaries generated by the Go linker should have
|
| 115 |
+
// the expected size. Otherwise skip.
|
| 116 |
+
text := f.Segment("__TEXT")
|
| 117 |
+
cs := make([]byte, cmd.Datasize)
|
| 118 |
+
codesign.Sign(cs, w.(io.Reader), "a.out", int64(cmd.Dataoff), int64(text.Offset), int64(text.Filesz), f.Type == macho.TypeExec)
|
| 119 |
+
if _, err := w.WriteAt(cs, int64(cmd.Dataoff)); err != nil {
|
| 120 |
+
return err
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
return nil
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
func excludeMachoCodeSignature(r io.Reader) io.Reader {
|
| 129 |
+
_, cmd, ok := findMachoCodeSignature(r)
|
| 130 |
+
if !ok {
|
| 131 |
+
return r
|
| 132 |
+
}
|
| 133 |
+
return &excludedReader{r, 0, int64(cmd.Dataoff), int64(cmd.Dataoff + cmd.Datasize)}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
func excludeHostBuildID(r, r0 io.Reader) io.Reader {
|
| 137 |
+
off, sz, ok := findHostBuildID(r0)
|
| 138 |
+
if !ok {
|
| 139 |
+
return r
|
| 140 |
+
}
|
| 141 |
+
return &excludedReader{r, 0, off, off + sz}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
// excludedReader wraps an io.Reader. Reading from it returns the bytes from
|
| 145 |
+
// the underlying reader, except that when the byte offset is within the
|
| 146 |
+
// range between start and end, it returns zero bytes.
|
| 147 |
+
type excludedReader struct {
|
| 148 |
+
r io.Reader
|
| 149 |
+
off int64 // current offset
|
| 150 |
+
start, end int64 // the range to be excluded (read as zero)
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
func (r *excludedReader) Read(p []byte) (int, error) {
|
| 154 |
+
n, err := r.r.Read(p)
|
| 155 |
+
if n > 0 && r.off+int64(n) > r.start && r.off < r.end {
|
| 156 |
+
cstart := r.start - r.off
|
| 157 |
+
if cstart < 0 {
|
| 158 |
+
cstart = 0
|
| 159 |
+
}
|
| 160 |
+
cend := r.end - r.off
|
| 161 |
+
if cend > int64(n) {
|
| 162 |
+
cend = int64(n)
|
| 163 |
+
}
|
| 164 |
+
zeros := make([]byte, cend-cstart)
|
| 165 |
+
copy(p[cstart:cend], zeros)
|
| 166 |
+
}
|
| 167 |
+
r.off += int64(n)
|
| 168 |
+
return n, err
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
func findMachoCodeSignature(r any) (*macho.File, codesign.CodeSigCmd, bool) {
|
| 172 |
+
ra, ok := r.(io.ReaderAt)
|
| 173 |
+
if !ok {
|
| 174 |
+
return nil, codesign.CodeSigCmd{}, false
|
| 175 |
+
}
|
| 176 |
+
f, err := macho.NewFile(ra)
|
| 177 |
+
if err != nil {
|
| 178 |
+
return nil, codesign.CodeSigCmd{}, false
|
| 179 |
+
}
|
| 180 |
+
cmd, ok := codesign.FindCodeSigCmd(f)
|
| 181 |
+
return f, cmd, ok
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
func findHostBuildID(r io.Reader) (offset int64, size int64, ok bool) {
|
| 185 |
+
ra, ok := r.(io.ReaderAt)
|
| 186 |
+
if !ok {
|
| 187 |
+
return 0, 0, false
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
ef, err := elf.NewFile(ra)
|
| 191 |
+
if err == nil {
|
| 192 |
+
// ELF file. Find GNU build ID section.
|
| 193 |
+
sect := ef.Section(".note.gnu.build-id")
|
| 194 |
+
if sect == nil {
|
| 195 |
+
return 0, 0, false
|
| 196 |
+
}
|
| 197 |
+
// Skip over the 3-word note "header" and "GNU\x00".
|
| 198 |
+
return int64(sect.Offset + 16), int64(sect.Size - 16), true
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
mf, err := macho.NewFile(ra)
|
| 202 |
+
if err != nil {
|
| 203 |
+
return 0, 0, false
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
// Mach-O file. Find LC_UUID load command.
|
| 207 |
+
reader := imacho.NewLoadCmdReader(io.NewSectionReader(ra, 0, 1<<63-1), mf.ByteOrder, imacho.FileHeaderSize(mf))
|
| 208 |
+
for i := uint32(0); i < mf.Ncmd; i++ {
|
| 209 |
+
cmd, err := reader.Next()
|
| 210 |
+
if err != nil {
|
| 211 |
+
break
|
| 212 |
+
}
|
| 213 |
+
if cmd.Cmd == imacho.LC_UUID {
|
| 214 |
+
// The UUID is the data in the LC_UUID load command,
|
| 215 |
+
// skipping over the 8-byte command header.
|
| 216 |
+
return reader.Offset() + 8, int64(cmd.Len - 8), true
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
return 0, 0, false
|
| 220 |
+
}
|
go/src/cmd/internal/codesign/codesign.go
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 3 |
+
// license that can be found in the LICENSE file.
|
| 4 |
+
|
| 5 |
+
// Package codesign provides basic functionalities for
|
| 6 |
+
// ad-hoc code signing of Mach-O files.
|
| 7 |
+
//
|
| 8 |
+
// This is not a general tool for code-signing. It is made
|
| 9 |
+
// specifically for the Go toolchain. It uses the same
|
| 10 |
+
// ad-hoc signing algorithm as the Darwin linker.
|
| 11 |
+
package codesign
|
| 12 |
+
|
| 13 |
+
import (
|
| 14 |
+
"crypto/sha256"
|
| 15 |
+
"debug/macho"
|
| 16 |
+
"encoding/binary"
|
| 17 |
+
"io"
|
| 18 |
+
|
| 19 |
+
"cmd/internal/hash"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
// Code signature layout.
|
| 23 |
+
//
|
| 24 |
+
// The code signature is a block of bytes that contains
|
| 25 |
+
// a SuperBlob, which contains one or more Blobs. For ad-hoc
|
| 26 |
+
// signing, a single CodeDirectory Blob suffices.
|
| 27 |
+
//
|
| 28 |
+
// A SuperBlob starts with its header (the binary representation
|
| 29 |
+
// of the SuperBlob struct), followed by a list of (in our case,
|
| 30 |
+
// one) Blobs (offset and size). A CodeDirectory Blob starts
|
| 31 |
+
// with its head (the binary representation of CodeDirectory struct),
|
| 32 |
+
// followed by the identifier (as a C string) and the hashes, at
|
| 33 |
+
// the corresponding offsets.
|
| 34 |
+
//
|
| 35 |
+
// The signature data must be included in the __LINKEDIT segment.
|
| 36 |
+
// In the Mach-O file header, an LC_CODE_SIGNATURE load command
|
| 37 |
+
// points to the data.
|
| 38 |
+
|
| 39 |
+
const (
|
| 40 |
+
pageSizeBits = 12
|
| 41 |
+
pageSize = 1 << pageSizeBits
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
const LC_CODE_SIGNATURE = 0x1d
|
| 45 |
+
|
| 46 |
+
// Constants and struct layouts are from
|
| 47 |
+
// https://opensource.apple.com/source/xnu/xnu-4903.270.47/osfmk/kern/cs_blobs.h
|
| 48 |
+
|
| 49 |
+
const (
|
| 50 |
+
CSMAGIC_REQUIREMENT = 0xfade0c00 // single Requirement blob
|
| 51 |
+
CSMAGIC_REQUIREMENTS = 0xfade0c01 // Requirements vector (internal requirements)
|
| 52 |
+
CSMAGIC_CODEDIRECTORY = 0xfade0c02 // CodeDirectory blob
|
| 53 |
+
CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0 // embedded form of signature data
|
| 54 |
+
CSMAGIC_DETACHED_SIGNATURE = 0xfade0cc1 // multi-arch collection of embedded signatures
|
| 55 |
+
|
| 56 |
+
CSSLOT_CODEDIRECTORY = 0 // slot index for CodeDirectory
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
const (
|
| 60 |
+
CS_HASHTYPE_SHA1 = 1
|
| 61 |
+
CS_HASHTYPE_SHA256 = 2
|
| 62 |
+
CS_HASHTYPE_SHA256_TRUNCATED = 3
|
| 63 |
+
CS_HASHTYPE_SHA384 = 4
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
const (
|
| 67 |
+
CS_EXECSEG_MAIN_BINARY = 0x1 // executable segment denotes main binary
|
| 68 |
+
CS_EXECSEG_ALLOW_UNSIGNED = 0x10 // allow unsigned pages (for debugging)
|
| 69 |
+
CS_EXECSEG_DEBUGGER = 0x20 // main binary is debugger
|
| 70 |
+
CS_EXECSEG_JIT = 0x40 // JIT enabled
|
| 71 |
+
CS_EXECSEG_SKIP_LV = 0x80 // skip library validation
|
| 72 |
+
CS_EXECSEG_CAN_LOAD_CDHASH = 0x100 // can bless cdhash for execution
|
| 73 |
+
CS_EXECSEG_CAN_EXEC_CDHASH = 0x200 // can execute blessed cdhash
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
type Blob struct {
|
| 77 |
+
typ uint32 // type of entry
|
| 78 |
+
offset uint32 // offset of entry
|
| 79 |
+
// data follows
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
func (b *Blob) put(out []byte) []byte {
|
| 83 |
+
out = put32be(out, b.typ)
|
| 84 |
+
out = put32be(out, b.offset)
|
| 85 |
+
return out
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
const blobSize = 2 * 4
|
| 89 |
+
|
| 90 |
+
type SuperBlob struct {
|
| 91 |
+
magic uint32 // magic number
|
| 92 |
+
length uint32 // total length of SuperBlob
|
| 93 |
+
count uint32 // number of index entries following
|
| 94 |
+
// blobs []Blob
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
func (s *SuperBlob) put(out []byte) []byte {
|
| 98 |
+
out = put32be(out, s.magic)
|
| 99 |
+
out = put32be(out, s.length)
|
| 100 |
+
out = put32be(out, s.count)
|
| 101 |
+
return out
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
const superBlobSize = 3 * 4
|
| 105 |
+
|
| 106 |
+
type CodeDirectory struct {
|
| 107 |
+
magic uint32 // magic number (CSMAGIC_CODEDIRECTORY)
|
| 108 |
+
length uint32 // total length of CodeDirectory blob
|
| 109 |
+
version uint32 // compatibility version
|
| 110 |
+
flags uint32 // setup and mode flags
|
| 111 |
+
hashOffset uint32 // offset of hash slot element at index zero
|
| 112 |
+
identOffset uint32 // offset of identifier string
|
| 113 |
+
nSpecialSlots uint32 // number of special hash slots
|
| 114 |
+
nCodeSlots uint32 // number of ordinary (code) hash slots
|
| 115 |
+
codeLimit uint32 // limit to main image signature range
|
| 116 |
+
hashSize uint8 // size of each hash in bytes
|
| 117 |
+
hashType uint8 // type of hash (cdHashType* constants)
|
| 118 |
+
_pad1 uint8 // unused (must be zero)
|
| 119 |
+
pageSize uint8 // log2(page size in bytes); 0 => infinite
|
| 120 |
+
_pad2 uint32 // unused (must be zero)
|
| 121 |
+
scatterOffset uint32
|
| 122 |
+
teamOffset uint32
|
| 123 |
+
_pad3 uint32
|
| 124 |
+
codeLimit64 uint64
|
| 125 |
+
execSegBase uint64
|
| 126 |
+
execSegLimit uint64
|
| 127 |
+
execSegFlags uint64
|
| 128 |
+
// data follows
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
func (c *CodeDirectory) put(out []byte) []byte {
|
| 132 |
+
out = put32be(out, c.magic)
|
| 133 |
+
out = put32be(out, c.length)
|
| 134 |
+
out = put32be(out, c.version)
|
| 135 |
+
out = put32be(out, c.flags)
|
| 136 |
+
out = put32be(out, c.hashOffset)
|
| 137 |
+
out = put32be(out, c.identOffset)
|
| 138 |
+
out = put32be(out, c.nSpecialSlots)
|
| 139 |
+
out = put32be(out, c.nCodeSlots)
|
| 140 |
+
out = put32be(out, c.codeLimit)
|
| 141 |
+
out = put8(out, c.hashSize)
|
| 142 |
+
out = put8(out, c.hashType)
|
| 143 |
+
out = put8(out, c._pad1)
|
| 144 |
+
out = put8(out, c.pageSize)
|
| 145 |
+
out = put32be(out, c._pad2)
|
| 146 |
+
out = put32be(out, c.scatterOffset)
|
| 147 |
+
out = put32be(out, c.teamOffset)
|
| 148 |
+
out = put32be(out, c._pad3)
|
| 149 |
+
out = put64be(out, c.codeLimit64)
|
| 150 |
+
out = put64be(out, c.execSegBase)
|
| 151 |
+
out = put64be(out, c.execSegLimit)
|
| 152 |
+
out = put64be(out, c.execSegFlags)
|
| 153 |
+
return out
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
const codeDirectorySize = 13*4 + 4 + 4*8
|
| 157 |
+
|
| 158 |
+
// CodeSigCmd is Mach-O LC_CODE_SIGNATURE load command.
|
| 159 |
+
type CodeSigCmd struct {
|
| 160 |
+
Cmd uint32 // LC_CODE_SIGNATURE
|
| 161 |
+
Cmdsize uint32 // sizeof this command (16)
|
| 162 |
+
Dataoff uint32 // file offset of data in __LINKEDIT segment
|
| 163 |
+
Datasize uint32 // file size of data in __LINKEDIT segment
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
func FindCodeSigCmd(f *macho.File) (CodeSigCmd, bool) {
|
| 167 |
+
get32 := f.ByteOrder.Uint32
|
| 168 |
+
for _, l := range f.Loads {
|
| 169 |
+
data := l.Raw()
|
| 170 |
+
cmd := get32(data)
|
| 171 |
+
if cmd == LC_CODE_SIGNATURE {
|
| 172 |
+
return CodeSigCmd{
|
| 173 |
+
cmd,
|
| 174 |
+
get32(data[4:]),
|
| 175 |
+
get32(data[8:]),
|
| 176 |
+
get32(data[12:]),
|
| 177 |
+
}, true
|
| 178 |
+
}
|
| 179 |
+
}
|
| 180 |
+
return CodeSigCmd{}, false
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
func put32be(b []byte, x uint32) []byte { binary.BigEndian.PutUint32(b, x); return b[4:] }
|
| 184 |
+
func put64be(b []byte, x uint64) []byte { binary.BigEndian.PutUint64(b, x); return b[8:] }
|
| 185 |
+
func put8(b []byte, x uint8) []byte { b[0] = x; return b[1:] }
|
| 186 |
+
func puts(b, s []byte) []byte { n := copy(b, s); return b[n:] }
|
| 187 |
+
|
| 188 |
+
// Size computes the size of the code signature.
|
| 189 |
+
// id is the identifier used for signing (a field in CodeDirectory blob, which
|
| 190 |
+
// has no significance in ad-hoc signing).
|
| 191 |
+
func Size(codeSize int64, id string) int64 {
|
| 192 |
+
nhashes := (codeSize + pageSize - 1) / pageSize
|
| 193 |
+
idOff := int64(codeDirectorySize)
|
| 194 |
+
hashOff := idOff + int64(len(id)+1)
|
| 195 |
+
cdirSz := hashOff + nhashes*hash.Size32
|
| 196 |
+
return int64(superBlobSize+blobSize) + cdirSz
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
// Sign generates an ad-hoc code signature and writes it to out.
|
| 200 |
+
// out must have length at least Size(codeSize, id).
|
| 201 |
+
// data is the file content without the signature, of size codeSize.
|
| 202 |
+
// textOff and textSize is the file offset and size of the text segment.
|
| 203 |
+
// isMain is true if this is a main executable.
|
| 204 |
+
// id is the identifier used for signing (a field in CodeDirectory blob, which
|
| 205 |
+
// has no significance in ad-hoc signing).
|
| 206 |
+
func Sign(out []byte, data io.Reader, id string, codeSize, textOff, textSize int64, isMain bool) {
|
| 207 |
+
nhashes := (codeSize + pageSize - 1) / pageSize
|
| 208 |
+
idOff := int64(codeDirectorySize)
|
| 209 |
+
hashOff := idOff + int64(len(id)+1)
|
| 210 |
+
sz := Size(codeSize, id)
|
| 211 |
+
|
| 212 |
+
// emit blob headers
|
| 213 |
+
sb := SuperBlob{
|
| 214 |
+
magic: CSMAGIC_EMBEDDED_SIGNATURE,
|
| 215 |
+
length: uint32(sz),
|
| 216 |
+
count: 1,
|
| 217 |
+
}
|
| 218 |
+
blob := Blob{
|
| 219 |
+
typ: CSSLOT_CODEDIRECTORY,
|
| 220 |
+
offset: superBlobSize + blobSize,
|
| 221 |
+
}
|
| 222 |
+
cdir := CodeDirectory{
|
| 223 |
+
magic: CSMAGIC_CODEDIRECTORY,
|
| 224 |
+
length: uint32(sz) - (superBlobSize + blobSize),
|
| 225 |
+
version: 0x20400,
|
| 226 |
+
flags: 0x20002, // adhoc | linkerSigned
|
| 227 |
+
hashOffset: uint32(hashOff),
|
| 228 |
+
identOffset: uint32(idOff),
|
| 229 |
+
nCodeSlots: uint32(nhashes),
|
| 230 |
+
codeLimit: uint32(codeSize),
|
| 231 |
+
hashSize: hash.Size32,
|
| 232 |
+
hashType: CS_HASHTYPE_SHA256,
|
| 233 |
+
pageSize: uint8(pageSizeBits),
|
| 234 |
+
execSegBase: uint64(textOff),
|
| 235 |
+
execSegLimit: uint64(textSize),
|
| 236 |
+
}
|
| 237 |
+
if isMain {
|
| 238 |
+
cdir.execSegFlags = CS_EXECSEG_MAIN_BINARY
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
outp := out
|
| 242 |
+
outp = sb.put(outp)
|
| 243 |
+
outp = blob.put(outp)
|
| 244 |
+
outp = cdir.put(outp)
|
| 245 |
+
|
| 246 |
+
// emit the identifier
|
| 247 |
+
outp = puts(outp, []byte(id+"\000"))
|
| 248 |
+
|
| 249 |
+
// emit hashes
|
| 250 |
+
var buf [pageSize]byte
|
| 251 |
+
p := 0
|
| 252 |
+
for p < int(codeSize) {
|
| 253 |
+
n, err := io.ReadFull(data, buf[:])
|
| 254 |
+
if err == io.EOF {
|
| 255 |
+
break
|
| 256 |
+
}
|
| 257 |
+
if err != nil && err != io.ErrUnexpectedEOF {
|
| 258 |
+
panic(err)
|
| 259 |
+
}
|
| 260 |
+
if p+n > int(codeSize) {
|
| 261 |
+
n = int(codeSize) - p
|
| 262 |
+
}
|
| 263 |
+
p += n
|
| 264 |
+
b := sha256.Sum256(buf[:n])
|
| 265 |
+
outp = puts(outp, b[:])
|
| 266 |
+
}
|
| 267 |
+
}
|
go/src/cmd/internal/cov/covcmd/cmddefs.go
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 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 covcmd
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"crypto/sha256"
|
| 9 |
+
"fmt"
|
| 10 |
+
"internal/coverage"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
// CoverPkgConfig is a bundle of information passed from the Go
|
| 14 |
+
// command to the cover command during "go build -cover" runs. The
|
| 15 |
+
// Go command creates and fills in a struct as below, then passes
|
| 16 |
+
// file containing the encoded JSON for the struct to the "cover"
|
| 17 |
+
// tool when instrumenting the source files in a Go package.
|
| 18 |
+
type CoverPkgConfig struct {
|
| 19 |
+
// File into which cmd/cover should emit summary info
|
| 20 |
+
// when instrumentation is complete.
|
| 21 |
+
OutConfig string
|
| 22 |
+
|
| 23 |
+
// Import path for the package being instrumented.
|
| 24 |
+
PkgPath string
|
| 25 |
+
|
| 26 |
+
// Package name.
|
| 27 |
+
PkgName string
|
| 28 |
+
|
| 29 |
+
// Instrumentation granularity: one of "perfunc" or "perblock" (default)
|
| 30 |
+
Granularity string
|
| 31 |
+
|
| 32 |
+
// Module path for this package (empty if no go.mod in use)
|
| 33 |
+
ModulePath string
|
| 34 |
+
|
| 35 |
+
// Local mode indicates we're doing a coverage build or test of a
|
| 36 |
+
// package selected via local import path, e.g. "./..." or
|
| 37 |
+
// "./foo/bar" as opposed to a non-relative import path. See the
|
| 38 |
+
// corresponding field in cmd/go's PackageInternal struct for more
|
| 39 |
+
// info.
|
| 40 |
+
Local bool
|
| 41 |
+
|
| 42 |
+
// EmitMetaFile if non-empty is the path to which the cover tool should
|
| 43 |
+
// directly emit a coverage meta-data file for the package, if the
|
| 44 |
+
// package has any functions in it. The go command will pass in a value
|
| 45 |
+
// here if we've been asked to run "go test -cover" on a package that
|
| 46 |
+
// doesn't have any *_test.go files.
|
| 47 |
+
EmitMetaFile string
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// CoverFixupConfig contains annotations/notes generated by the
|
| 51 |
+
// cmd/cover tool (during instrumentation) to be passed on to the
|
| 52 |
+
// compiler when the instrumented code is compiled. The cmd/cover tool
|
| 53 |
+
// creates a struct of this type, JSON-encodes it, and emits the
|
| 54 |
+
// result to a file, which the Go command then passes to the compiler
|
| 55 |
+
// when the instrumented package is built.
|
| 56 |
+
type CoverFixupConfig struct {
|
| 57 |
+
// Name of the variable (created by cmd/cover) containing the
|
| 58 |
+
// encoded meta-data for the package.
|
| 59 |
+
MetaVar string
|
| 60 |
+
|
| 61 |
+
// Length of the meta-data.
|
| 62 |
+
MetaLen int
|
| 63 |
+
|
| 64 |
+
// Hash computed by cmd/cover of the meta-data.
|
| 65 |
+
MetaHash string
|
| 66 |
+
|
| 67 |
+
// Instrumentation strategy. For now this is always set to
|
| 68 |
+
// "normal", but in the future we may add new values (for example,
|
| 69 |
+
// if panic paths are instrumented, or if the instrumenter
|
| 70 |
+
// eliminates redundant counters).
|
| 71 |
+
Strategy string
|
| 72 |
+
|
| 73 |
+
// Prefix assigned to the names of counter variables generated
|
| 74 |
+
// during instrumentation by cmd/cover.
|
| 75 |
+
CounterPrefix string
|
| 76 |
+
|
| 77 |
+
// Name chosen for the package ID variable generated during
|
| 78 |
+
// instrumentation.
|
| 79 |
+
PkgIdVar string
|
| 80 |
+
|
| 81 |
+
// Counter mode (e.g. set/count/atomic)
|
| 82 |
+
CounterMode string
|
| 83 |
+
|
| 84 |
+
// Counter granularity (perblock or perfunc).
|
| 85 |
+
CounterGranularity string
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// MetaFileForPackage returns the expected name of the meta-data file
|
| 89 |
+
// for the package whose import path is 'importPath' in cases where
|
| 90 |
+
// we're using meta-data generated by the cover tool, as opposed to a
|
| 91 |
+
// meta-data file created at runtime.
|
| 92 |
+
func MetaFileForPackage(importPath string) string {
|
| 93 |
+
var r [32]byte
|
| 94 |
+
sum := sha256.Sum256([]byte(importPath))
|
| 95 |
+
copy(r[:], sum[:])
|
| 96 |
+
return coverage.MetaFilePref + fmt.Sprintf(".%x", r)
|
| 97 |
+
}
|
go/src/cmd/internal/cov/mreader.go
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 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 cov
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"cmd/internal/bio"
|
| 9 |
+
"io"
|
| 10 |
+
"os"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
// This file contains the helper "MReader", a wrapper around bio plus
|
| 14 |
+
// an "mmap'd read-only" view of the file obtained from bio.SliceRO().
|
| 15 |
+
// MReader is designed to implement the io.ReaderSeeker interface.
|
| 16 |
+
// Since bio.SliceOS() is not guaranteed to succeed, MReader falls back
|
| 17 |
+
// on explicit reads + seeks provided by bio.Reader if needed.
|
| 18 |
+
|
| 19 |
+
type MReader struct {
|
| 20 |
+
f *os.File
|
| 21 |
+
rdr *bio.Reader
|
| 22 |
+
fileView []byte
|
| 23 |
+
off int64
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
func NewMreader(f *os.File) (*MReader, error) {
|
| 27 |
+
rdr := bio.NewReader(f)
|
| 28 |
+
fi, err := f.Stat()
|
| 29 |
+
if err != nil {
|
| 30 |
+
return nil, err
|
| 31 |
+
}
|
| 32 |
+
r := MReader{
|
| 33 |
+
f: f,
|
| 34 |
+
rdr: rdr,
|
| 35 |
+
fileView: rdr.SliceRO(uint64(fi.Size())),
|
| 36 |
+
}
|
| 37 |
+
return &r, nil
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
func (r *MReader) Read(p []byte) (int, error) {
|
| 41 |
+
if r.fileView != nil {
|
| 42 |
+
amt := len(p)
|
| 43 |
+
toread := r.fileView[r.off:]
|
| 44 |
+
if len(toread) < 1 {
|
| 45 |
+
return 0, io.EOF
|
| 46 |
+
}
|
| 47 |
+
if len(toread) < amt {
|
| 48 |
+
amt = len(toread)
|
| 49 |
+
}
|
| 50 |
+
copy(p, toread)
|
| 51 |
+
r.off += int64(amt)
|
| 52 |
+
return amt, nil
|
| 53 |
+
}
|
| 54 |
+
return io.ReadFull(r.rdr, p)
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
func (r *MReader) ReadByte() (byte, error) {
|
| 58 |
+
if r.fileView != nil {
|
| 59 |
+
toread := r.fileView[r.off:]
|
| 60 |
+
if len(toread) < 1 {
|
| 61 |
+
return 0, io.EOF
|
| 62 |
+
}
|
| 63 |
+
rv := toread[0]
|
| 64 |
+
r.off++
|
| 65 |
+
return rv, nil
|
| 66 |
+
}
|
| 67 |
+
return r.rdr.ReadByte()
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
func (r *MReader) Seek(offset int64, whence int) (int64, error) {
|
| 71 |
+
if r.fileView == nil {
|
| 72 |
+
return r.rdr.MustSeek(offset, whence), nil
|
| 73 |
+
}
|
| 74 |
+
switch whence {
|
| 75 |
+
case io.SeekStart:
|
| 76 |
+
r.off = offset
|
| 77 |
+
return offset, nil
|
| 78 |
+
case io.SeekCurrent:
|
| 79 |
+
return r.off, nil
|
| 80 |
+
case io.SeekEnd:
|
| 81 |
+
r.off = int64(len(r.fileView)) + offset
|
| 82 |
+
return r.off, nil
|
| 83 |
+
}
|
| 84 |
+
panic("other modes not implemented")
|
| 85 |
+
}
|
go/src/cmd/internal/cov/read_test.go
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 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 cov_test
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"cmd/internal/cov"
|
| 9 |
+
"fmt"
|
| 10 |
+
"internal/coverage"
|
| 11 |
+
"internal/coverage/decodecounter"
|
| 12 |
+
"internal/coverage/decodemeta"
|
| 13 |
+
"internal/coverage/pods"
|
| 14 |
+
"internal/testenv"
|
| 15 |
+
"os"
|
| 16 |
+
"path/filepath"
|
| 17 |
+
"testing"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
// visitor implements the CovDataVisitor interface in a very stripped
|
| 21 |
+
// down way, just keeps track of interesting events.
|
| 22 |
+
type visitor struct {
|
| 23 |
+
metaFileCount int
|
| 24 |
+
counterFileCount int
|
| 25 |
+
funcCounterData int
|
| 26 |
+
metaFuncCount int
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
func (v *visitor) BeginPod(p pods.Pod) {}
|
| 30 |
+
func (v *visitor) EndPod(p pods.Pod) {}
|
| 31 |
+
func (v *visitor) VisitMetaDataFile(mdf string, mfr *decodemeta.CoverageMetaFileReader) {
|
| 32 |
+
v.metaFileCount++
|
| 33 |
+
}
|
| 34 |
+
func (v *visitor) BeginCounterDataFile(cdf string, cdr *decodecounter.CounterDataReader, dirIdx int) {
|
| 35 |
+
v.counterFileCount++
|
| 36 |
+
}
|
| 37 |
+
func (v *visitor) EndCounterDataFile(cdf string, cdr *decodecounter.CounterDataReader, dirIdx int) {}
|
| 38 |
+
func (v *visitor) VisitFuncCounterData(payload decodecounter.FuncPayload) { v.funcCounterData++ }
|
| 39 |
+
func (v *visitor) EndCounters() {}
|
| 40 |
+
func (v *visitor) BeginPackage(pd *decodemeta.CoverageMetaDataDecoder, pkgIdx uint32) {}
|
| 41 |
+
func (v *visitor) EndPackage(pd *decodemeta.CoverageMetaDataDecoder, pkgIdx uint32) {}
|
| 42 |
+
func (v *visitor) VisitFunc(pkgIdx uint32, fnIdx uint32, fd *coverage.FuncDesc) { v.metaFuncCount++ }
|
| 43 |
+
func (v *visitor) Finish() {}
|
| 44 |
+
|
| 45 |
+
func TestIssue58411(t *testing.T) {
|
| 46 |
+
testenv.MustHaveGoBuild(t)
|
| 47 |
+
|
| 48 |
+
// Build a tiny test program with -cover. Smallness is important;
|
| 49 |
+
// it is one of the factors that triggers issue 58411.
|
| 50 |
+
d := t.TempDir()
|
| 51 |
+
exepath := filepath.Join(d, "small.exe")
|
| 52 |
+
path := filepath.Join("testdata", "small.go")
|
| 53 |
+
cmd := testenv.Command(t, testenv.GoToolPath(t), "build",
|
| 54 |
+
"-o", exepath, "-cover", path)
|
| 55 |
+
b, err := cmd.CombinedOutput()
|
| 56 |
+
if len(b) != 0 {
|
| 57 |
+
t.Logf("## build output:\n%s", b)
|
| 58 |
+
}
|
| 59 |
+
if err != nil {
|
| 60 |
+
t.Fatalf("build error: %v", err)
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// Run to produce coverage data. Note the large argument; we need a large
|
| 64 |
+
// argument (more than 4k) to trigger the bug, but the overall file
|
| 65 |
+
// has to remain small (since large files will be read with mmap).
|
| 66 |
+
covdir := filepath.Join(d, "covdata")
|
| 67 |
+
if err = os.Mkdir(covdir, 0777); err != nil {
|
| 68 |
+
t.Fatalf("creating covdir: %v", err)
|
| 69 |
+
}
|
| 70 |
+
large := fmt.Sprintf("%07999d", 0)
|
| 71 |
+
cmd = testenv.Command(t, exepath, "1", "2", "3", large)
|
| 72 |
+
cmd.Dir = covdir
|
| 73 |
+
cmd.Env = append(os.Environ(), "GOCOVERDIR="+covdir)
|
| 74 |
+
b, err = cmd.CombinedOutput()
|
| 75 |
+
if err != nil {
|
| 76 |
+
t.Logf("## run output:\n%s", b)
|
| 77 |
+
t.Fatalf("build error: %v", err)
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
vis := &visitor{}
|
| 81 |
+
|
| 82 |
+
// Read resulting coverage data. Without the fix, this would
|
| 83 |
+
// yield a "short read" error.
|
| 84 |
+
const verbosityLevel = 0
|
| 85 |
+
const flags = 0
|
| 86 |
+
cdr := cov.MakeCovDataReader(vis, []string{covdir}, verbosityLevel, flags, nil)
|
| 87 |
+
err = cdr.Visit()
|
| 88 |
+
if err != nil {
|
| 89 |
+
t.Fatalf("visit failed: %v", err)
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// make sure we saw a few things just for grins
|
| 93 |
+
const want = "{metaFileCount:1 counterFileCount:1 funcCounterData:1 metaFuncCount:1}"
|
| 94 |
+
got := fmt.Sprintf("%+v", *vis)
|
| 95 |
+
if want != got {
|
| 96 |
+
t.Errorf("visitor contents: want %v got %v\n", want, got)
|
| 97 |
+
}
|
| 98 |
+
}
|
go/src/cmd/internal/cov/readcovdata.go
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 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 cov
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"cmd/internal/bio"
|
| 9 |
+
"fmt"
|
| 10 |
+
"internal/coverage"
|
| 11 |
+
"internal/coverage/decodecounter"
|
| 12 |
+
"internal/coverage/decodemeta"
|
| 13 |
+
"internal/coverage/pods"
|
| 14 |
+
"io"
|
| 15 |
+
"os"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
// CovDataReader is a general-purpose helper/visitor object for
|
| 19 |
+
// reading coverage data files in a structured way. Clients create a
|
| 20 |
+
// CovDataReader to process a given collection of coverage data file
|
| 21 |
+
// directories, then pass in a visitor object with methods that get
|
| 22 |
+
// invoked at various important points. CovDataReader is intended
|
| 23 |
+
// to facilitate common coverage data file operations such as
|
| 24 |
+
// merging or intersecting data files, analyzing data files, or
|
| 25 |
+
// dumping data files.
|
| 26 |
+
type CovDataReader struct {
|
| 27 |
+
vis CovDataVisitor
|
| 28 |
+
indirs []string
|
| 29 |
+
matchpkg func(name string) bool
|
| 30 |
+
flags CovDataReaderFlags
|
| 31 |
+
err error
|
| 32 |
+
verbosityLevel int
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// MakeCovDataReader creates a CovDataReader object to process the
|
| 36 |
+
// given set of input directories. Here 'vis' is a visitor object
|
| 37 |
+
// providing methods to be invoked as we walk through the data,
|
| 38 |
+
// 'indirs' is the set of coverage data directories to examine,
|
| 39 |
+
// 'verbosityLevel' controls the level of debugging trace messages
|
| 40 |
+
// (zero for off, higher for more output), 'flags' stores flags that
|
| 41 |
+
// indicate what to do if errors are detected, and 'matchpkg' is a
|
| 42 |
+
// caller-provided function that can be used to select specific
|
| 43 |
+
// packages by name (if nil, then all packages are included).
|
| 44 |
+
func MakeCovDataReader(vis CovDataVisitor, indirs []string, verbosityLevel int, flags CovDataReaderFlags, matchpkg func(name string) bool) *CovDataReader {
|
| 45 |
+
return &CovDataReader{
|
| 46 |
+
vis: vis,
|
| 47 |
+
indirs: indirs,
|
| 48 |
+
matchpkg: matchpkg,
|
| 49 |
+
verbosityLevel: verbosityLevel,
|
| 50 |
+
flags: flags,
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// CovDataVisitor defines hooks for clients of CovDataReader. When the
|
| 55 |
+
// coverage data reader makes its way through a coverage meta-data
|
| 56 |
+
// file and counter data files, it will invoke the methods below to
|
| 57 |
+
// hand off info to the client. The normal sequence of expected
|
| 58 |
+
// visitor method invocations is:
|
| 59 |
+
//
|
| 60 |
+
// for each pod P {
|
| 61 |
+
// BeginPod(p)
|
| 62 |
+
// let MF be the meta-data file for P
|
| 63 |
+
// VisitMetaDataFile(MF)
|
| 64 |
+
// for each counter data file D in P {
|
| 65 |
+
// BeginCounterDataFile(D)
|
| 66 |
+
// for each live function F in D {
|
| 67 |
+
// VisitFuncCounterData(F)
|
| 68 |
+
// }
|
| 69 |
+
// EndCounterDataFile(D)
|
| 70 |
+
// }
|
| 71 |
+
// EndCounters(MF)
|
| 72 |
+
// for each package PK in MF {
|
| 73 |
+
// BeginPackage(PK)
|
| 74 |
+
// if <PK matched according to package pattern and/or modpath> {
|
| 75 |
+
// for each function PF in PK {
|
| 76 |
+
// VisitFunc(PF)
|
| 77 |
+
// }
|
| 78 |
+
// }
|
| 79 |
+
// EndPackage(PK)
|
| 80 |
+
// }
|
| 81 |
+
// EndPod(p)
|
| 82 |
+
// }
|
| 83 |
+
// Finish()
|
| 84 |
+
|
| 85 |
+
type CovDataVisitor interface {
|
| 86 |
+
// Invoked at the start and end of a given pod (a pod here is a
|
| 87 |
+
// specific coverage meta-data files with the counter data files
|
| 88 |
+
// that correspond to it).
|
| 89 |
+
BeginPod(p pods.Pod)
|
| 90 |
+
EndPod(p pods.Pod)
|
| 91 |
+
|
| 92 |
+
// Invoked when the reader is starting to examine the meta-data
|
| 93 |
+
// file for a pod. Here 'mdf' is the path of the file, and 'mfr'
|
| 94 |
+
// is an open meta-data reader.
|
| 95 |
+
VisitMetaDataFile(mdf string, mfr *decodemeta.CoverageMetaFileReader)
|
| 96 |
+
|
| 97 |
+
// Invoked when the reader processes a counter data file, first
|
| 98 |
+
// the 'begin' method at the start, then the 'end' method when
|
| 99 |
+
// we're done with the file.
|
| 100 |
+
BeginCounterDataFile(cdf string, cdr *decodecounter.CounterDataReader, dirIdx int)
|
| 101 |
+
EndCounterDataFile(cdf string, cdr *decodecounter.CounterDataReader, dirIdx int)
|
| 102 |
+
|
| 103 |
+
// Invoked once for each live function in the counter data file.
|
| 104 |
+
VisitFuncCounterData(payload decodecounter.FuncPayload)
|
| 105 |
+
|
| 106 |
+
// Invoked when we've finished processing the counter files in a
|
| 107 |
+
// POD (e.g. no more calls to VisitFuncCounterData).
|
| 108 |
+
EndCounters()
|
| 109 |
+
|
| 110 |
+
// Invoked for each package in the meta-data file for the pod,
|
| 111 |
+
// first the 'begin' method when processing of the package starts,
|
| 112 |
+
// then the 'end' method when we're done
|
| 113 |
+
BeginPackage(pd *decodemeta.CoverageMetaDataDecoder, pkgIdx uint32)
|
| 114 |
+
EndPackage(pd *decodemeta.CoverageMetaDataDecoder, pkgIdx uint32)
|
| 115 |
+
|
| 116 |
+
// Invoked for each function the package being visited.
|
| 117 |
+
VisitFunc(pkgIdx uint32, fnIdx uint32, fd *coverage.FuncDesc)
|
| 118 |
+
|
| 119 |
+
// Invoked when all counter + meta-data file processing is complete.
|
| 120 |
+
Finish()
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
type CovDataReaderFlags uint32
|
| 124 |
+
|
| 125 |
+
const (
|
| 126 |
+
CovDataReaderNoFlags CovDataReaderFlags = 0
|
| 127 |
+
PanicOnError = 1 << iota
|
| 128 |
+
PanicOnWarning
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
func (r *CovDataReader) Visit() error {
|
| 132 |
+
podlist, err := pods.CollectPods(r.indirs, false)
|
| 133 |
+
if err != nil {
|
| 134 |
+
return fmt.Errorf("reading inputs: %v", err)
|
| 135 |
+
}
|
| 136 |
+
if len(podlist) == 0 {
|
| 137 |
+
r.warn("no applicable files found in input directories")
|
| 138 |
+
}
|
| 139 |
+
for _, p := range podlist {
|
| 140 |
+
if err := r.visitPod(p); err != nil {
|
| 141 |
+
return err
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
r.vis.Finish()
|
| 145 |
+
return nil
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
func (r *CovDataReader) verb(vlevel int, s string, a ...any) {
|
| 149 |
+
if r.verbosityLevel >= vlevel {
|
| 150 |
+
fmt.Fprintf(os.Stderr, s, a...)
|
| 151 |
+
fmt.Fprintf(os.Stderr, "\n")
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
func (r *CovDataReader) warn(s string, a ...any) {
|
| 156 |
+
fmt.Fprintf(os.Stderr, "warning: ")
|
| 157 |
+
fmt.Fprintf(os.Stderr, s, a...)
|
| 158 |
+
fmt.Fprintf(os.Stderr, "\n")
|
| 159 |
+
if (r.flags & PanicOnWarning) != 0 {
|
| 160 |
+
panic("unexpected warning")
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
func (r *CovDataReader) fatal(s string, a ...any) error {
|
| 165 |
+
if r.err != nil {
|
| 166 |
+
return nil
|
| 167 |
+
}
|
| 168 |
+
errstr := "error: " + fmt.Sprintf(s, a...) + "\n"
|
| 169 |
+
if (r.flags & PanicOnError) != 0 {
|
| 170 |
+
fmt.Fprintf(os.Stderr, "%s", errstr)
|
| 171 |
+
panic("fatal error")
|
| 172 |
+
}
|
| 173 |
+
r.err = fmt.Errorf("%s", errstr)
|
| 174 |
+
return r.err
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
// visitPod examines a coverage data 'pod', that is, a meta-data file and
|
| 178 |
+
// zero or more counter data files that refer to that meta-data file.
|
| 179 |
+
func (r *CovDataReader) visitPod(p pods.Pod) error {
|
| 180 |
+
r.verb(1, "visiting pod: metafile %s with %d counter files",
|
| 181 |
+
p.MetaFile, len(p.CounterDataFiles))
|
| 182 |
+
r.vis.BeginPod(p)
|
| 183 |
+
|
| 184 |
+
// Open meta-file
|
| 185 |
+
f, err := os.Open(p.MetaFile)
|
| 186 |
+
if err != nil {
|
| 187 |
+
return r.fatal("unable to open meta-file %s", p.MetaFile)
|
| 188 |
+
}
|
| 189 |
+
defer f.Close()
|
| 190 |
+
br := bio.NewReader(f)
|
| 191 |
+
fi, err := f.Stat()
|
| 192 |
+
if err != nil {
|
| 193 |
+
return r.fatal("unable to stat metafile %s: %v", p.MetaFile, err)
|
| 194 |
+
}
|
| 195 |
+
fileView := br.SliceRO(uint64(fi.Size()))
|
| 196 |
+
br.MustSeek(0, io.SeekStart)
|
| 197 |
+
|
| 198 |
+
r.verb(1, "fileView for pod is length %d", len(fileView))
|
| 199 |
+
|
| 200 |
+
var mfr *decodemeta.CoverageMetaFileReader
|
| 201 |
+
mfr, err = decodemeta.NewCoverageMetaFileReader(f, fileView)
|
| 202 |
+
if err != nil {
|
| 203 |
+
return r.fatal("decoding meta-file %s: %s", p.MetaFile, err)
|
| 204 |
+
}
|
| 205 |
+
r.vis.VisitMetaDataFile(p.MetaFile, mfr)
|
| 206 |
+
|
| 207 |
+
processCounterDataFile := func(cdf string, k int) error {
|
| 208 |
+
cf, err := os.Open(cdf)
|
| 209 |
+
if err != nil {
|
| 210 |
+
return r.fatal("opening counter data file %s: %s", cdf, err)
|
| 211 |
+
}
|
| 212 |
+
defer cf.Close()
|
| 213 |
+
var mr *MReader
|
| 214 |
+
mr, err = NewMreader(cf)
|
| 215 |
+
if err != nil {
|
| 216 |
+
return r.fatal("creating reader for counter data file %s: %s", cdf, err)
|
| 217 |
+
}
|
| 218 |
+
var cdr *decodecounter.CounterDataReader
|
| 219 |
+
cdr, err = decodecounter.NewCounterDataReader(cdf, mr)
|
| 220 |
+
if err != nil {
|
| 221 |
+
return r.fatal("reading counter data file %s: %s", cdf, err)
|
| 222 |
+
}
|
| 223 |
+
r.vis.BeginCounterDataFile(cdf, cdr, p.Origins[k])
|
| 224 |
+
var data decodecounter.FuncPayload
|
| 225 |
+
for {
|
| 226 |
+
ok, err := cdr.NextFunc(&data)
|
| 227 |
+
if err != nil {
|
| 228 |
+
return r.fatal("reading counter data file %s: %v", cdf, err)
|
| 229 |
+
}
|
| 230 |
+
if !ok {
|
| 231 |
+
break
|
| 232 |
+
}
|
| 233 |
+
r.vis.VisitFuncCounterData(data)
|
| 234 |
+
}
|
| 235 |
+
r.vis.EndCounterDataFile(cdf, cdr, p.Origins[k])
|
| 236 |
+
return nil
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
// Read counter data files.
|
| 240 |
+
for k, cdf := range p.CounterDataFiles {
|
| 241 |
+
if err := processCounterDataFile(cdf, k); err != nil {
|
| 242 |
+
return err
|
| 243 |
+
}
|
| 244 |
+
}
|
| 245 |
+
r.vis.EndCounters()
|
| 246 |
+
|
| 247 |
+
// NB: packages in the meta-file will be in dependency order (basically
|
| 248 |
+
// the order in which init files execute). Do we want an additional sort
|
| 249 |
+
// pass here, say by packagepath?
|
| 250 |
+
np := uint32(mfr.NumPackages())
|
| 251 |
+
payload := []byte{}
|
| 252 |
+
for pkIdx := uint32(0); pkIdx < np; pkIdx++ {
|
| 253 |
+
var pd *decodemeta.CoverageMetaDataDecoder
|
| 254 |
+
pd, payload, err = mfr.GetPackageDecoder(pkIdx, payload)
|
| 255 |
+
if err != nil {
|
| 256 |
+
return r.fatal("reading pkg %d from meta-file %s: %s", pkIdx, p.MetaFile, err)
|
| 257 |
+
}
|
| 258 |
+
r.processPackage(p.MetaFile, pd, pkIdx)
|
| 259 |
+
}
|
| 260 |
+
r.vis.EndPod(p)
|
| 261 |
+
|
| 262 |
+
return nil
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
func (r *CovDataReader) processPackage(mfname string, pd *decodemeta.CoverageMetaDataDecoder, pkgIdx uint32) error {
|
| 266 |
+
if r.matchpkg != nil {
|
| 267 |
+
if !r.matchpkg(pd.PackagePath()) {
|
| 268 |
+
return nil
|
| 269 |
+
}
|
| 270 |
+
}
|
| 271 |
+
r.vis.BeginPackage(pd, pkgIdx)
|
| 272 |
+
nf := pd.NumFuncs()
|
| 273 |
+
var fd coverage.FuncDesc
|
| 274 |
+
for fidx := uint32(0); fidx < nf; fidx++ {
|
| 275 |
+
if err := pd.ReadFunc(fidx, &fd); err != nil {
|
| 276 |
+
return r.fatal("reading meta-data file %s: %v", mfname, err)
|
| 277 |
+
}
|
| 278 |
+
r.vis.VisitFunc(pkgIdx, fidx, &fd)
|
| 279 |
+
}
|
| 280 |
+
r.vis.EndPackage(pd, pkgIdx)
|
| 281 |
+
return nil
|
| 282 |
+
}
|
go/src/cmd/internal/disasm/disasm.go
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2014 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 disasm provides disassembly routines.
|
| 6 |
+
//
|
| 7 |
+
// It is broken out from cmd/internal/objfile so tools that don't need
|
| 8 |
+
// disassembling don't need to depend on x/arch disassembler code.
|
| 9 |
+
package disasm
|
| 10 |
+
|
| 11 |
+
import (
|
| 12 |
+
"bufio"
|
| 13 |
+
"bytes"
|
| 14 |
+
"container/list"
|
| 15 |
+
"encoding/binary"
|
| 16 |
+
"fmt"
|
| 17 |
+
"io"
|
| 18 |
+
"os"
|
| 19 |
+
"path/filepath"
|
| 20 |
+
"regexp"
|
| 21 |
+
"sort"
|
| 22 |
+
"strings"
|
| 23 |
+
"text/tabwriter"
|
| 24 |
+
|
| 25 |
+
"cmd/internal/objfile"
|
| 26 |
+
"cmd/internal/src"
|
| 27 |
+
|
| 28 |
+
"golang.org/x/arch/arm/armasm"
|
| 29 |
+
"golang.org/x/arch/arm64/arm64asm"
|
| 30 |
+
"golang.org/x/arch/loong64/loong64asm"
|
| 31 |
+
"golang.org/x/arch/ppc64/ppc64asm"
|
| 32 |
+
"golang.org/x/arch/riscv64/riscv64asm"
|
| 33 |
+
"golang.org/x/arch/s390x/s390xasm"
|
| 34 |
+
"golang.org/x/arch/x86/x86asm"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
// Disasm is a disassembler for a given File.
|
| 38 |
+
type Disasm struct {
|
| 39 |
+
syms []objfile.Sym // symbols in file, sorted by address
|
| 40 |
+
pcln objfile.Liner // pcln table
|
| 41 |
+
text []byte // bytes of text segment (actual instructions)
|
| 42 |
+
textStart uint64 // start PC of text
|
| 43 |
+
textEnd uint64 // end PC of text
|
| 44 |
+
goarch string // GOARCH string
|
| 45 |
+
disasm disasmFunc // disassembler function for goarch
|
| 46 |
+
byteOrder binary.ByteOrder // byte order for goarch
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// DisasmForFile returns a disassembler for the file f.
|
| 50 |
+
func DisasmForFile(f *objfile.File) (*Disasm, error) {
|
| 51 |
+
return disasmForEntry(f.Entries()[0])
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
func disasmForEntry(e *objfile.Entry) (*Disasm, error) {
|
| 55 |
+
syms, err := e.Symbols()
|
| 56 |
+
if err != nil {
|
| 57 |
+
return nil, err
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
pcln, err := e.PCLineTable()
|
| 61 |
+
if err != nil {
|
| 62 |
+
return nil, err
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
textStart, textBytes, err := e.Text()
|
| 66 |
+
if err != nil {
|
| 67 |
+
return nil, err
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
goarch := e.GOARCH()
|
| 71 |
+
disasm := disasms[goarch]
|
| 72 |
+
byteOrder := byteOrders[goarch]
|
| 73 |
+
if disasm == nil || byteOrder == nil {
|
| 74 |
+
return nil, fmt.Errorf("unsupported architecture %q", goarch)
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// Filter out section symbols, overwriting syms in place.
|
| 78 |
+
keep := syms[:0]
|
| 79 |
+
for _, sym := range syms {
|
| 80 |
+
switch sym.Name {
|
| 81 |
+
case "runtime.text", "text", "_text", "runtime.etext", "etext", "_etext":
|
| 82 |
+
// drop
|
| 83 |
+
default:
|
| 84 |
+
keep = append(keep, sym)
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
syms = keep
|
| 88 |
+
d := &Disasm{
|
| 89 |
+
syms: syms,
|
| 90 |
+
pcln: pcln,
|
| 91 |
+
text: textBytes,
|
| 92 |
+
textStart: textStart,
|
| 93 |
+
textEnd: textStart + uint64(len(textBytes)),
|
| 94 |
+
goarch: goarch,
|
| 95 |
+
disasm: disasm,
|
| 96 |
+
byteOrder: byteOrder,
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
return d, nil
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// lookup finds the symbol name containing addr.
|
| 103 |
+
func (d *Disasm) lookup(addr uint64) (name string, base uint64) {
|
| 104 |
+
i := sort.Search(len(d.syms), func(i int) bool { return addr < d.syms[i].Addr })
|
| 105 |
+
if i > 0 {
|
| 106 |
+
s := d.syms[i-1]
|
| 107 |
+
if s.Addr != 0 && s.Addr <= addr && addr < s.Addr+uint64(s.Size) {
|
| 108 |
+
return s.Name, s.Addr
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
return "", 0
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
// base returns the final element in the path.
|
| 115 |
+
// It works on both Windows and Unix paths,
|
| 116 |
+
// regardless of host operating system.
|
| 117 |
+
func base(path string) string {
|
| 118 |
+
path = path[strings.LastIndex(path, "/")+1:]
|
| 119 |
+
path = path[strings.LastIndex(path, `\`)+1:]
|
| 120 |
+
return path
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// CachedFile contains the content of a file split into lines.
|
| 124 |
+
type CachedFile struct {
|
| 125 |
+
FileName string
|
| 126 |
+
Lines [][]byte
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// FileCache is a simple LRU cache of file contents.
|
| 130 |
+
type FileCache struct {
|
| 131 |
+
files *list.List
|
| 132 |
+
maxLen int
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
// NewFileCache returns a FileCache which can contain up to maxLen cached file contents.
|
| 136 |
+
func NewFileCache(maxLen int) *FileCache {
|
| 137 |
+
return &FileCache{
|
| 138 |
+
files: list.New(),
|
| 139 |
+
maxLen: maxLen,
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
// Line returns the source code line for the given file and line number.
|
| 144 |
+
// If the file is not already cached, reads it, inserts it into the cache,
|
| 145 |
+
// and removes the least recently used file if necessary.
|
| 146 |
+
// If the file is in cache, it is moved to the front of the list.
|
| 147 |
+
func (fc *FileCache) Line(filename string, line int) ([]byte, error) {
|
| 148 |
+
if filepath.Ext(filename) != ".go" {
|
| 149 |
+
return nil, nil
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
// Clean filenames returned by src.Pos.SymFilename()
|
| 153 |
+
// or src.PosBase.SymFilename() removing
|
| 154 |
+
// the leading src.FileSymPrefix.
|
| 155 |
+
filename = strings.TrimPrefix(filename, src.FileSymPrefix)
|
| 156 |
+
|
| 157 |
+
// Expand literal "$GOROOT" rewritten by obj.AbsFile()
|
| 158 |
+
filename = filepath.Clean(os.ExpandEnv(filename))
|
| 159 |
+
|
| 160 |
+
var cf *CachedFile
|
| 161 |
+
var e *list.Element
|
| 162 |
+
|
| 163 |
+
for e = fc.files.Front(); e != nil; e = e.Next() {
|
| 164 |
+
cf = e.Value.(*CachedFile)
|
| 165 |
+
if cf.FileName == filename {
|
| 166 |
+
break
|
| 167 |
+
}
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
if e == nil {
|
| 171 |
+
content, err := os.ReadFile(filename)
|
| 172 |
+
if err != nil {
|
| 173 |
+
return nil, err
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
cf = &CachedFile{
|
| 177 |
+
FileName: filename,
|
| 178 |
+
Lines: bytes.Split(content, []byte{'\n'}),
|
| 179 |
+
}
|
| 180 |
+
fc.files.PushFront(cf)
|
| 181 |
+
|
| 182 |
+
if fc.files.Len() >= fc.maxLen {
|
| 183 |
+
fc.files.Remove(fc.files.Back())
|
| 184 |
+
}
|
| 185 |
+
} else {
|
| 186 |
+
fc.files.MoveToFront(e)
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
// because //line directives can be out-of-range. (#36683)
|
| 190 |
+
if line-1 >= len(cf.Lines) || line-1 < 0 {
|
| 191 |
+
return nil, nil
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
return cf.Lines[line-1], nil
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
// Print prints a disassembly of the file to w.
|
| 198 |
+
// If filter is non-nil, the disassembly only includes functions with names matching filter.
|
| 199 |
+
// If printCode is true, the disassembly includes corresponding source lines.
|
| 200 |
+
// The disassembly only includes functions that overlap the range [start, end).
|
| 201 |
+
func (d *Disasm) Print(w io.Writer, filter *regexp.Regexp, start, end uint64, printCode bool, gnuAsm bool) {
|
| 202 |
+
if start < d.textStart {
|
| 203 |
+
start = d.textStart
|
| 204 |
+
}
|
| 205 |
+
if end > d.textEnd {
|
| 206 |
+
end = d.textEnd
|
| 207 |
+
}
|
| 208 |
+
printed := false
|
| 209 |
+
bw := bufio.NewWriter(w)
|
| 210 |
+
|
| 211 |
+
var fc *FileCache
|
| 212 |
+
if printCode {
|
| 213 |
+
fc = NewFileCache(8)
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
tw := tabwriter.NewWriter(bw, 18, 8, 1, '\t', tabwriter.StripEscape)
|
| 217 |
+
for _, sym := range d.syms {
|
| 218 |
+
symStart := sym.Addr
|
| 219 |
+
symEnd := sym.Addr + uint64(sym.Size)
|
| 220 |
+
relocs := sym.Relocs
|
| 221 |
+
if sym.Code != 'T' && sym.Code != 't' ||
|
| 222 |
+
symStart < d.textStart ||
|
| 223 |
+
symEnd <= start || end <= symStart ||
|
| 224 |
+
filter != nil && !filter.MatchString(sym.Name) {
|
| 225 |
+
continue
|
| 226 |
+
}
|
| 227 |
+
if printed {
|
| 228 |
+
fmt.Fprintf(bw, "\n")
|
| 229 |
+
}
|
| 230 |
+
printed = true
|
| 231 |
+
|
| 232 |
+
file, _, _ := d.pcln.PCToLine(sym.Addr)
|
| 233 |
+
fmt.Fprintf(bw, "TEXT %s(SB) %s\n", sym.Name, file)
|
| 234 |
+
|
| 235 |
+
if symEnd > end {
|
| 236 |
+
symEnd = end
|
| 237 |
+
}
|
| 238 |
+
code := d.text[:end-d.textStart]
|
| 239 |
+
|
| 240 |
+
var lastFile string
|
| 241 |
+
var lastLine int
|
| 242 |
+
|
| 243 |
+
d.Decode(symStart, symEnd, relocs, gnuAsm, func(pc, size uint64, file string, line int, text string) {
|
| 244 |
+
i := pc - d.textStart
|
| 245 |
+
|
| 246 |
+
if printCode {
|
| 247 |
+
if file != lastFile || line != lastLine {
|
| 248 |
+
if srcLine, err := fc.Line(file, line); err == nil {
|
| 249 |
+
fmt.Fprintf(tw, "%s%s%s\n", []byte{tabwriter.Escape}, srcLine, []byte{tabwriter.Escape})
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
lastFile, lastLine = file, line
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
fmt.Fprintf(tw, " %#x\t", pc)
|
| 256 |
+
} else {
|
| 257 |
+
fmt.Fprintf(tw, " %s:%d\t%#x\t", base(file), line, pc)
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
if size%4 != 0 || d.goarch == "386" || d.goarch == "amd64" {
|
| 261 |
+
// Print instruction as bytes.
|
| 262 |
+
fmt.Fprintf(tw, "%x", code[i:i+size])
|
| 263 |
+
} else {
|
| 264 |
+
// Print instruction as 32-bit words.
|
| 265 |
+
for j := uint64(0); j < size; j += 4 {
|
| 266 |
+
if j > 0 {
|
| 267 |
+
fmt.Fprintf(tw, " ")
|
| 268 |
+
}
|
| 269 |
+
fmt.Fprintf(tw, "%08x", d.byteOrder.Uint32(code[i+j:]))
|
| 270 |
+
}
|
| 271 |
+
}
|
| 272 |
+
fmt.Fprintf(tw, "\t%s\t\n", text)
|
| 273 |
+
})
|
| 274 |
+
tw.Flush()
|
| 275 |
+
}
|
| 276 |
+
bw.Flush()
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
// Decode disassembles the text segment range [start, end), calling f for each instruction.
|
| 280 |
+
func (d *Disasm) Decode(start, end uint64, relocs []objfile.Reloc, gnuAsm bool, f func(pc, size uint64, file string, line int, text string)) {
|
| 281 |
+
if start < d.textStart {
|
| 282 |
+
start = d.textStart
|
| 283 |
+
}
|
| 284 |
+
if end > d.textEnd {
|
| 285 |
+
end = d.textEnd
|
| 286 |
+
}
|
| 287 |
+
code := d.text[:end-d.textStart]
|
| 288 |
+
lookup := d.lookup
|
| 289 |
+
for pc := start; pc < end; {
|
| 290 |
+
i := pc - d.textStart
|
| 291 |
+
text, size := d.disasm(code[i:], pc, lookup, d.byteOrder, gnuAsm)
|
| 292 |
+
file, line, _ := d.pcln.PCToLine(pc)
|
| 293 |
+
sep := "\t"
|
| 294 |
+
for len(relocs) > 0 && relocs[0].Addr < i+uint64(size) {
|
| 295 |
+
text += sep + relocs[0].Stringer.String(pc-start)
|
| 296 |
+
sep = " "
|
| 297 |
+
relocs = relocs[1:]
|
| 298 |
+
}
|
| 299 |
+
f(pc, uint64(size), file, line, text)
|
| 300 |
+
pc += uint64(size)
|
| 301 |
+
}
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
type lookupFunc = func(addr uint64) (sym string, base uint64)
|
| 305 |
+
type disasmFunc func(code []byte, pc uint64, lookup lookupFunc, ord binary.ByteOrder, _ bool) (text string, size int)
|
| 306 |
+
|
| 307 |
+
func disasm_386(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder, gnuAsm bool) (string, int) {
|
| 308 |
+
return disasm_x86(code, pc, lookup, 32, gnuAsm)
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
func disasm_amd64(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder, gnuAsm bool) (string, int) {
|
| 312 |
+
return disasm_x86(code, pc, lookup, 64, gnuAsm)
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
func disasm_x86(code []byte, pc uint64, lookup lookupFunc, arch int, gnuAsm bool) (string, int) {
|
| 316 |
+
inst, err := x86asm.Decode(code, arch)
|
| 317 |
+
var text string
|
| 318 |
+
size := inst.Len
|
| 319 |
+
if err != nil || size == 0 || inst.Op == 0 {
|
| 320 |
+
size = 1
|
| 321 |
+
text = "?"
|
| 322 |
+
} else {
|
| 323 |
+
if gnuAsm {
|
| 324 |
+
text = fmt.Sprintf("%-36s // %s", x86asm.GoSyntax(inst, pc, lookup), x86asm.GNUSyntax(inst, pc, nil))
|
| 325 |
+
} else {
|
| 326 |
+
text = x86asm.GoSyntax(inst, pc, lookup)
|
| 327 |
+
}
|
| 328 |
+
}
|
| 329 |
+
return text, size
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
type textReader struct {
|
| 333 |
+
code []byte
|
| 334 |
+
pc uint64
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
func (r textReader) ReadAt(data []byte, off int64) (n int, err error) {
|
| 338 |
+
if off < 0 || uint64(off) < r.pc {
|
| 339 |
+
return 0, io.EOF
|
| 340 |
+
}
|
| 341 |
+
d := uint64(off) - r.pc
|
| 342 |
+
if d >= uint64(len(r.code)) {
|
| 343 |
+
return 0, io.EOF
|
| 344 |
+
}
|
| 345 |
+
n = copy(data, r.code[d:])
|
| 346 |
+
if n < len(data) {
|
| 347 |
+
err = io.ErrUnexpectedEOF
|
| 348 |
+
}
|
| 349 |
+
return
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
func disasm_arm(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder, gnuAsm bool) (string, int) {
|
| 353 |
+
inst, err := armasm.Decode(code, armasm.ModeARM)
|
| 354 |
+
var text string
|
| 355 |
+
size := inst.Len
|
| 356 |
+
if err != nil || size == 0 || inst.Op == 0 {
|
| 357 |
+
size = 4
|
| 358 |
+
text = "?"
|
| 359 |
+
} else if gnuAsm {
|
| 360 |
+
text = fmt.Sprintf("%-36s // %s", armasm.GoSyntax(inst, pc, lookup, textReader{code, pc}), armasm.GNUSyntax(inst))
|
| 361 |
+
} else {
|
| 362 |
+
text = armasm.GoSyntax(inst, pc, lookup, textReader{code, pc})
|
| 363 |
+
}
|
| 364 |
+
return text, size
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
func disasm_arm64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.ByteOrder, gnuAsm bool) (string, int) {
|
| 368 |
+
inst, err := arm64asm.Decode(code)
|
| 369 |
+
var text string
|
| 370 |
+
if err != nil || inst.Op == 0 {
|
| 371 |
+
text = "?"
|
| 372 |
+
} else if gnuAsm {
|
| 373 |
+
text = fmt.Sprintf("%-36s // %s", arm64asm.GoSyntax(inst, pc, lookup, textReader{code, pc}), arm64asm.GNUSyntax(inst))
|
| 374 |
+
} else {
|
| 375 |
+
text = arm64asm.GoSyntax(inst, pc, lookup, textReader{code, pc})
|
| 376 |
+
}
|
| 377 |
+
return text, 4
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
func disasm_loong64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.ByteOrder, gnuAsm bool) (string, int) {
|
| 381 |
+
inst, err := loong64asm.Decode(code)
|
| 382 |
+
var text string
|
| 383 |
+
if err != nil || inst.Op == 0 {
|
| 384 |
+
text = "?"
|
| 385 |
+
} else if gnuAsm {
|
| 386 |
+
text = fmt.Sprintf("%-36s // %s", loong64asm.GoSyntax(inst, pc, lookup), loong64asm.GNUSyntax(inst))
|
| 387 |
+
} else {
|
| 388 |
+
text = loong64asm.GoSyntax(inst, pc, lookup)
|
| 389 |
+
}
|
| 390 |
+
return text, 4
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
func disasm_ppc64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.ByteOrder, gnuAsm bool) (string, int) {
|
| 394 |
+
inst, err := ppc64asm.Decode(code, byteOrder)
|
| 395 |
+
var text string
|
| 396 |
+
size := inst.Len
|
| 397 |
+
if err != nil || size == 0 {
|
| 398 |
+
size = 4
|
| 399 |
+
text = "?"
|
| 400 |
+
} else {
|
| 401 |
+
if gnuAsm {
|
| 402 |
+
text = fmt.Sprintf("%-36s // %s", ppc64asm.GoSyntax(inst, pc, lookup), ppc64asm.GNUSyntax(inst, pc))
|
| 403 |
+
} else {
|
| 404 |
+
text = ppc64asm.GoSyntax(inst, pc, lookup)
|
| 405 |
+
}
|
| 406 |
+
}
|
| 407 |
+
return text, size
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
func disasm_riscv64(code []byte, pc uint64, lookup lookupFunc, byteOrder binary.ByteOrder, gnuAsm bool) (string, int) {
|
| 411 |
+
inst, err := riscv64asm.Decode(code)
|
| 412 |
+
var text string
|
| 413 |
+
size := inst.Len
|
| 414 |
+
if err != nil || inst.Op == 0 {
|
| 415 |
+
size = 2
|
| 416 |
+
text = "?"
|
| 417 |
+
} else if gnuAsm {
|
| 418 |
+
text = fmt.Sprintf("%-36s // %s", riscv64asm.GoSyntax(inst, pc, lookup, textReader{code, pc}), riscv64asm.GNUSyntax(inst))
|
| 419 |
+
} else {
|
| 420 |
+
text = riscv64asm.GoSyntax(inst, pc, lookup, textReader{code, pc})
|
| 421 |
+
}
|
| 422 |
+
return text, size
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
func disasm_s390x(code []byte, pc uint64, lookup lookupFunc, _ binary.ByteOrder, gnuAsm bool) (string, int) {
|
| 426 |
+
inst, err := s390xasm.Decode(code)
|
| 427 |
+
var text string
|
| 428 |
+
size := inst.Len
|
| 429 |
+
if err != nil || size == 0 || inst.Op == 0 {
|
| 430 |
+
size = 2
|
| 431 |
+
text = "?"
|
| 432 |
+
} else {
|
| 433 |
+
if gnuAsm {
|
| 434 |
+
text = fmt.Sprintf("%-36s // %s", s390xasm.GoSyntax(inst, pc, lookup), s390xasm.GNUSyntax(inst, pc))
|
| 435 |
+
} else {
|
| 436 |
+
text = s390xasm.GoSyntax(inst, pc, lookup)
|
| 437 |
+
}
|
| 438 |
+
}
|
| 439 |
+
return text, size
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
var disasms = map[string]disasmFunc{
|
| 443 |
+
"386": disasm_386,
|
| 444 |
+
"amd64": disasm_amd64,
|
| 445 |
+
"arm": disasm_arm,
|
| 446 |
+
"arm64": disasm_arm64,
|
| 447 |
+
"loong64": disasm_loong64,
|
| 448 |
+
"ppc64": disasm_ppc64,
|
| 449 |
+
"ppc64le": disasm_ppc64,
|
| 450 |
+
"riscv64": disasm_riscv64,
|
| 451 |
+
"s390x": disasm_s390x,
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
var byteOrders = map[string]binary.ByteOrder{
|
| 455 |
+
"386": binary.LittleEndian,
|
| 456 |
+
"amd64": binary.LittleEndian,
|
| 457 |
+
"arm": binary.LittleEndian,
|
| 458 |
+
"arm64": binary.LittleEndian,
|
| 459 |
+
"loong64": binary.LittleEndian,
|
| 460 |
+
"ppc64": binary.BigEndian,
|
| 461 |
+
"ppc64le": binary.LittleEndian,
|
| 462 |
+
"riscv64": binary.LittleEndian,
|
| 463 |
+
"s390x": binary.BigEndian,
|
| 464 |
+
}
|
go/src/cmd/internal/dwarf/dwarf.go
ADDED
|
@@ -0,0 +1,1695 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2016 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 dwarf generates DWARF debugging information.
|
| 6 |
+
// DWARF generation is split between the compiler and the linker,
|
| 7 |
+
// this package contains the shared code.
|
| 8 |
+
package dwarf
|
| 9 |
+
|
| 10 |
+
import (
|
| 11 |
+
"bytes"
|
| 12 |
+
"cmd/internal/src"
|
| 13 |
+
"cmp"
|
| 14 |
+
"errors"
|
| 15 |
+
"fmt"
|
| 16 |
+
"internal/buildcfg"
|
| 17 |
+
"os/exec"
|
| 18 |
+
"slices"
|
| 19 |
+
"strconv"
|
| 20 |
+
"strings"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
// InfoPrefix is the prefix for all the symbols containing DWARF info entries.
|
| 24 |
+
const InfoPrefix = "go:info."
|
| 25 |
+
|
| 26 |
+
// ConstInfoPrefix is the prefix for all symbols containing DWARF info
|
| 27 |
+
// entries that contain constants.
|
| 28 |
+
const ConstInfoPrefix = "go:constinfo."
|
| 29 |
+
|
| 30 |
+
// CUInfoPrefix is the prefix for symbols containing information to
|
| 31 |
+
// populate the DWARF compilation unit info entries.
|
| 32 |
+
const CUInfoPrefix = "go:cuinfo."
|
| 33 |
+
|
| 34 |
+
// Used to form the symbol name assigned to the DWARF "abstract subprogram"
|
| 35 |
+
// info entry for a function
|
| 36 |
+
const AbstractFuncSuffix = "$abstract"
|
| 37 |
+
|
| 38 |
+
// Controls logging/debugging for selected aspects of DWARF subprogram
|
| 39 |
+
// generation (functions, scopes).
|
| 40 |
+
var logDwarf bool
|
| 41 |
+
|
| 42 |
+
// Sym represents a symbol.
|
| 43 |
+
type Sym any
|
| 44 |
+
|
| 45 |
+
// A Var represents a local variable or a function parameter.
|
| 46 |
+
type Var struct {
|
| 47 |
+
Name string
|
| 48 |
+
Tag int // Either DW_TAG_variable or DW_TAG_formal_parameter
|
| 49 |
+
WithLoclist bool
|
| 50 |
+
IsReturnValue bool
|
| 51 |
+
IsInlFormal bool
|
| 52 |
+
DictIndex uint16 // index of the dictionary entry describing the type of this variable
|
| 53 |
+
StackOffset int32
|
| 54 |
+
// This package can't use the ssa package, so it can't mention ssa.FuncDebug,
|
| 55 |
+
// so indirect through a closure.
|
| 56 |
+
PutLocationList func(listSym, startPC Sym)
|
| 57 |
+
Scope int32
|
| 58 |
+
Type Sym
|
| 59 |
+
DeclFile string
|
| 60 |
+
DeclLine uint
|
| 61 |
+
DeclCol uint
|
| 62 |
+
InlIndex int32 // subtract 1 to form real index into InlTree
|
| 63 |
+
ChildIndex int32 // child DIE index in abstract function
|
| 64 |
+
IsInAbstract bool // variable exists in abstract function
|
| 65 |
+
ClosureOffset int64 // if non-zero this is the offset of this variable in the closure struct
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// A Scope represents a lexical scope. All variables declared within a
|
| 69 |
+
// scope will only be visible to instructions covered by the scope.
|
| 70 |
+
// Lexical scopes are contiguous in source files but can end up being
|
| 71 |
+
// compiled to discontiguous blocks of instructions in the executable.
|
| 72 |
+
// The Ranges field lists all the blocks of instructions that belong
|
| 73 |
+
// in this scope.
|
| 74 |
+
type Scope struct {
|
| 75 |
+
Parent int32
|
| 76 |
+
Ranges []Range
|
| 77 |
+
Vars []*Var
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
// A Range represents a half-open interval [Start, End).
|
| 81 |
+
type Range struct {
|
| 82 |
+
Start, End int64
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// This container is used by the PutFunc* variants below when
|
| 86 |
+
// creating the DWARF subprogram DIE(s) for a function.
|
| 87 |
+
type FnState struct {
|
| 88 |
+
Name string
|
| 89 |
+
Info Sym
|
| 90 |
+
Loc Sym
|
| 91 |
+
Ranges Sym
|
| 92 |
+
Absfn Sym
|
| 93 |
+
StartPC Sym
|
| 94 |
+
StartPos src.Pos
|
| 95 |
+
Size int64
|
| 96 |
+
External bool
|
| 97 |
+
Scopes []Scope
|
| 98 |
+
InlCalls InlCalls
|
| 99 |
+
UseBASEntries bool
|
| 100 |
+
|
| 101 |
+
dictIndexToOffset []int64
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
func EnableLogging(doit bool) {
|
| 105 |
+
logDwarf = doit
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
// MergeRanges creates a new range list by merging the ranges from
|
| 109 |
+
// its two arguments, then returns the new list.
|
| 110 |
+
func MergeRanges(in1, in2 []Range) []Range {
|
| 111 |
+
out := make([]Range, 0, len(in1)+len(in2))
|
| 112 |
+
i, j := 0, 0
|
| 113 |
+
for {
|
| 114 |
+
var cur Range
|
| 115 |
+
if i < len(in2) && j < len(in1) {
|
| 116 |
+
if in2[i].Start < in1[j].Start {
|
| 117 |
+
cur = in2[i]
|
| 118 |
+
i++
|
| 119 |
+
} else {
|
| 120 |
+
cur = in1[j]
|
| 121 |
+
j++
|
| 122 |
+
}
|
| 123 |
+
} else if i < len(in2) {
|
| 124 |
+
cur = in2[i]
|
| 125 |
+
i++
|
| 126 |
+
} else if j < len(in1) {
|
| 127 |
+
cur = in1[j]
|
| 128 |
+
j++
|
| 129 |
+
} else {
|
| 130 |
+
break
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
if n := len(out); n > 0 && cur.Start <= out[n-1].End {
|
| 134 |
+
out[n-1].End = cur.End
|
| 135 |
+
} else {
|
| 136 |
+
out = append(out, cur)
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
return out
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
// UnifyRanges merges the ranges from 'c' into the list of ranges for 's'.
|
| 144 |
+
func (s *Scope) UnifyRanges(c *Scope) {
|
| 145 |
+
s.Ranges = MergeRanges(s.Ranges, c.Ranges)
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
// AppendRange adds r to s, if r is non-empty.
|
| 149 |
+
// If possible, it extends the last Range in s.Ranges; if not, it creates a new one.
|
| 150 |
+
func (s *Scope) AppendRange(r Range) {
|
| 151 |
+
if r.End <= r.Start {
|
| 152 |
+
return
|
| 153 |
+
}
|
| 154 |
+
i := len(s.Ranges)
|
| 155 |
+
if i > 0 && s.Ranges[i-1].End == r.Start {
|
| 156 |
+
s.Ranges[i-1].End = r.End
|
| 157 |
+
return
|
| 158 |
+
}
|
| 159 |
+
s.Ranges = append(s.Ranges, r)
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
type InlCalls struct {
|
| 163 |
+
Calls []InlCall
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
type InlCall struct {
|
| 167 |
+
// index into ctx.InlTree describing the call inlined here
|
| 168 |
+
InlIndex int
|
| 169 |
+
|
| 170 |
+
// Position of the inlined call site.
|
| 171 |
+
CallPos src.Pos
|
| 172 |
+
|
| 173 |
+
// Dwarf abstract subroutine symbol (really *obj.LSym).
|
| 174 |
+
AbsFunSym Sym
|
| 175 |
+
|
| 176 |
+
// Indices of child inlines within Calls array above.
|
| 177 |
+
Children []int
|
| 178 |
+
|
| 179 |
+
// entries in this list are PAUTO's created by the inliner to
|
| 180 |
+
// capture the promoted formals and locals of the inlined callee.
|
| 181 |
+
InlVars []*Var
|
| 182 |
+
|
| 183 |
+
// PC ranges for this inlined call.
|
| 184 |
+
Ranges []Range
|
| 185 |
+
|
| 186 |
+
// Root call (not a child of some other call).
|
| 187 |
+
Root bool
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
// A Context specifies how to add data to a Sym.
|
| 191 |
+
type Context interface {
|
| 192 |
+
PtrSize() int
|
| 193 |
+
Size(s Sym) int64
|
| 194 |
+
AddInt(s Sym, size int, i int64)
|
| 195 |
+
AddBytes(s Sym, b []byte)
|
| 196 |
+
AddAddress(s Sym, t any, ofs int64)
|
| 197 |
+
AddCURelativeAddress(s Sym, t any, ofs int64)
|
| 198 |
+
AddSectionOffset(s Sym, size int, t any, ofs int64)
|
| 199 |
+
AddDWARFAddrSectionOffset(s Sym, t any, ofs int64)
|
| 200 |
+
AddIndirectTextRef(s Sym, t any)
|
| 201 |
+
CurrentOffset(s Sym) int64
|
| 202 |
+
RecordDclReference(from Sym, to Sym, dclIdx int, inlIndex int)
|
| 203 |
+
RecordChildDieOffsets(s Sym, vars []*Var, offsets []int32)
|
| 204 |
+
AddString(s Sym, v string)
|
| 205 |
+
Logf(format string, args ...any)
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
// AppendUleb128 appends v to b using DWARF's unsigned LEB128 encoding.
|
| 209 |
+
func AppendUleb128(b []byte, v uint64) []byte {
|
| 210 |
+
for {
|
| 211 |
+
c := uint8(v & 0x7f)
|
| 212 |
+
v >>= 7
|
| 213 |
+
if v != 0 {
|
| 214 |
+
c |= 0x80
|
| 215 |
+
}
|
| 216 |
+
b = append(b, c)
|
| 217 |
+
if c&0x80 == 0 {
|
| 218 |
+
break
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
return b
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
// AppendSleb128 appends v to b using DWARF's signed LEB128 encoding.
|
| 225 |
+
func AppendSleb128(b []byte, v int64) []byte {
|
| 226 |
+
for {
|
| 227 |
+
c := uint8(v & 0x7f)
|
| 228 |
+
s := uint8(v & 0x40)
|
| 229 |
+
v >>= 7
|
| 230 |
+
if (v != -1 || s == 0) && (v != 0 || s != 0) {
|
| 231 |
+
c |= 0x80
|
| 232 |
+
}
|
| 233 |
+
b = append(b, c)
|
| 234 |
+
if c&0x80 == 0 {
|
| 235 |
+
break
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
return b
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
// sevenbits contains all unsigned seven bit numbers, indexed by their value.
|
| 242 |
+
var sevenbits = [...]byte{
|
| 243 |
+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
| 244 |
+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
|
| 245 |
+
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
|
| 246 |
+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
|
| 247 |
+
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
|
| 248 |
+
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
|
| 249 |
+
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
|
| 250 |
+
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
// sevenBitU returns the unsigned LEB128 encoding of v if v is seven bits and nil otherwise.
|
| 254 |
+
// The contents of the returned slice must not be modified.
|
| 255 |
+
func sevenBitU(v int64) []byte {
|
| 256 |
+
if uint64(v) < uint64(len(sevenbits)) {
|
| 257 |
+
return sevenbits[v : v+1]
|
| 258 |
+
}
|
| 259 |
+
return nil
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
// sevenBitS returns the signed LEB128 encoding of v if v is seven bits and nil otherwise.
|
| 263 |
+
// The contents of the returned slice must not be modified.
|
| 264 |
+
func sevenBitS(v int64) []byte {
|
| 265 |
+
if uint64(v) <= 63 {
|
| 266 |
+
return sevenbits[v : v+1]
|
| 267 |
+
}
|
| 268 |
+
if uint64(-v) <= 64 {
|
| 269 |
+
return sevenbits[128+v : 128+v+1]
|
| 270 |
+
}
|
| 271 |
+
return nil
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// Uleb128put appends v to s using DWARF's unsigned LEB128 encoding.
|
| 275 |
+
func Uleb128put(ctxt Context, s Sym, v int64) {
|
| 276 |
+
b := sevenBitU(v)
|
| 277 |
+
if b == nil {
|
| 278 |
+
var encbuf [20]byte
|
| 279 |
+
b = AppendUleb128(encbuf[:0], uint64(v))
|
| 280 |
+
}
|
| 281 |
+
ctxt.AddBytes(s, b)
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
// Sleb128put appends v to s using DWARF's signed LEB128 encoding.
|
| 285 |
+
func Sleb128put(ctxt Context, s Sym, v int64) {
|
| 286 |
+
b := sevenBitS(v)
|
| 287 |
+
if b == nil {
|
| 288 |
+
var encbuf [20]byte
|
| 289 |
+
b = AppendSleb128(encbuf[:0], v)
|
| 290 |
+
}
|
| 291 |
+
ctxt.AddBytes(s, b)
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
/*
|
| 295 |
+
* Defining Abbrevs. This is hardcoded on a per-platform basis (that is,
|
| 296 |
+
* each platform will see a fixed abbrev table for all objects); the number
|
| 297 |
+
* of abbrev entries is fairly small (compared to C++ objects). The DWARF
|
| 298 |
+
* spec places no restriction on the ordering of attributes in the
|
| 299 |
+
* Abbrevs and DIEs, and we will always write them out in the order
|
| 300 |
+
* of declaration in the abbrev.
|
| 301 |
+
*/
|
| 302 |
+
type dwAttrForm struct {
|
| 303 |
+
attr uint16
|
| 304 |
+
form uint8
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
// Go-specific type attributes.
|
| 308 |
+
const (
|
| 309 |
+
DW_AT_go_kind = 0x2900
|
| 310 |
+
DW_AT_go_key = 0x2901
|
| 311 |
+
DW_AT_go_elem = 0x2902
|
| 312 |
+
// Attribute for DW_TAG_member of a struct type.
|
| 313 |
+
// Nonzero value indicates the struct field is an embedded field.
|
| 314 |
+
DW_AT_go_embedded_field = 0x2903
|
| 315 |
+
DW_AT_go_runtime_type = 0x2904
|
| 316 |
+
|
| 317 |
+
DW_AT_go_package_name = 0x2905 // Attribute for DW_TAG_compile_unit
|
| 318 |
+
DW_AT_go_dict_index = 0x2906 // Attribute for DW_TAG_typedef_type, index of the dictionary entry describing the real type of this type shape
|
| 319 |
+
DW_AT_go_closure_offset = 0x2907 // Attribute for DW_TAG_variable, offset in the closure struct where this captured variable resides
|
| 320 |
+
|
| 321 |
+
DW_AT_internal_location = 253 // params and locals; not emitted
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
// Index into the abbrevs table below.
|
| 325 |
+
const (
|
| 326 |
+
DW_ABRV_NULL = iota
|
| 327 |
+
DW_ABRV_COMPUNIT
|
| 328 |
+
DW_ABRV_COMPUNIT_TEXTLESS
|
| 329 |
+
DW_ABRV_FUNCTION
|
| 330 |
+
DW_ABRV_WRAPPER
|
| 331 |
+
DW_ABRV_FUNCTION_ABSTRACT
|
| 332 |
+
DW_ABRV_FUNCTION_CONCRETE
|
| 333 |
+
DW_ABRV_WRAPPER_CONCRETE
|
| 334 |
+
DW_ABRV_INLINED_SUBROUTINE
|
| 335 |
+
DW_ABRV_INLINED_SUBROUTINE_RANGES
|
| 336 |
+
DW_ABRV_VARIABLE
|
| 337 |
+
DW_ABRV_INT_CONSTANT
|
| 338 |
+
DW_ABRV_LEXICAL_BLOCK_RANGES
|
| 339 |
+
DW_ABRV_LEXICAL_BLOCK_SIMPLE
|
| 340 |
+
DW_ABRV_STRUCTFIELD
|
| 341 |
+
DW_ABRV_FUNCTYPEPARAM
|
| 342 |
+
DW_ABRV_FUNCTYPEOUTPARAM
|
| 343 |
+
DW_ABRV_DOTDOTDOT
|
| 344 |
+
DW_ABRV_ARRAYRANGE
|
| 345 |
+
DW_ABRV_NULLTYPE
|
| 346 |
+
DW_ABRV_BASETYPE
|
| 347 |
+
DW_ABRV_ARRAYTYPE
|
| 348 |
+
DW_ABRV_CHANTYPE
|
| 349 |
+
DW_ABRV_FUNCTYPE
|
| 350 |
+
DW_ABRV_IFACETYPE
|
| 351 |
+
DW_ABRV_MAPTYPE
|
| 352 |
+
DW_ABRV_PTRTYPE
|
| 353 |
+
DW_ABRV_BARE_PTRTYPE // only for void*, no DW_AT_type attr to please gdb 6.
|
| 354 |
+
DW_ABRV_SLICETYPE
|
| 355 |
+
DW_ABRV_STRINGTYPE
|
| 356 |
+
DW_ABRV_STRUCTTYPE
|
| 357 |
+
DW_ABRV_TYPEDECL
|
| 358 |
+
DW_ABRV_DICT_INDEX
|
| 359 |
+
DW_ABRV_PUTVAR_START
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
type dwAbbrev struct {
|
| 363 |
+
tag uint8
|
| 364 |
+
children uint8
|
| 365 |
+
attr []dwAttrForm
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
var abbrevsFinalized bool
|
| 369 |
+
|
| 370 |
+
// expandPseudoForm takes an input DW_FORM_xxx value and translates it
|
| 371 |
+
// into a version- and platform-appropriate concrete form. Existing
|
| 372 |
+
// concrete/real DW_FORM values are left untouched. For the moment the
|
| 373 |
+
// only platform-specific pseudo-form is DW_FORM_udata_pseudo, which
|
| 374 |
+
// gets expanded to DW_FORM_data4 on Darwin and DW_FORM_udata
|
| 375 |
+
// everywhere else. See issue #31459 for more context. Then we have a
|
| 376 |
+
// pair of pseudo-forms for lo and hi PC attributes, which are
|
| 377 |
+
// expanded differently depending on whether we're generating DWARF
|
| 378 |
+
// version 4 or 5.
|
| 379 |
+
func expandPseudoForm(form uint8) uint8 {
|
| 380 |
+
switch form {
|
| 381 |
+
case DW_FORM_udata_pseudo:
|
| 382 |
+
expandedForm := DW_FORM_udata
|
| 383 |
+
if buildcfg.GOOS == "darwin" || buildcfg.GOOS == "ios" {
|
| 384 |
+
expandedForm = DW_FORM_data4
|
| 385 |
+
}
|
| 386 |
+
return uint8(expandedForm)
|
| 387 |
+
case DW_FORM_lo_pc_pseudo:
|
| 388 |
+
if buildcfg.Experiment.Dwarf5 {
|
| 389 |
+
return DW_FORM_addrx
|
| 390 |
+
}
|
| 391 |
+
return DW_FORM_addr
|
| 392 |
+
case DW_FORM_hi_pc_pseudo:
|
| 393 |
+
if buildcfg.Experiment.Dwarf5 {
|
| 394 |
+
return DW_FORM_udata
|
| 395 |
+
}
|
| 396 |
+
return DW_FORM_addr
|
| 397 |
+
default:
|
| 398 |
+
return form
|
| 399 |
+
}
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
// Abbrevs returns the finalized abbrev array for the platform,
|
| 403 |
+
// expanding any DW_FORM pseudo-ops to real values.
|
| 404 |
+
func Abbrevs() []dwAbbrev {
|
| 405 |
+
if abbrevsFinalized {
|
| 406 |
+
return abbrevs
|
| 407 |
+
}
|
| 408 |
+
abbrevs = append(abbrevs, putvarAbbrevs...)
|
| 409 |
+
for i := 1; i < len(abbrevs); i++ {
|
| 410 |
+
for j := 0; j < len(abbrevs[i].attr); j++ {
|
| 411 |
+
abbrevs[i].attr[j].form = expandPseudoForm(abbrevs[i].attr[j].form)
|
| 412 |
+
}
|
| 413 |
+
}
|
| 414 |
+
if buildcfg.Experiment.Dwarf5 {
|
| 415 |
+
// Tack on a new DW_AT_addr_base attribute to the compunit DIE,
|
| 416 |
+
// which will point to the offset in the .debug_addr section
|
| 417 |
+
// containing entries for this comp unit (this attr gets
|
| 418 |
+
// fixed up in the linker).
|
| 419 |
+
for i := 1; i < len(abbrevs); i++ {
|
| 420 |
+
haveLo := false
|
| 421 |
+
for j := 0; j < len(abbrevs[i].attr); j++ {
|
| 422 |
+
if abbrevs[i].attr[j].attr == DW_AT_low_pc {
|
| 423 |
+
haveLo = true
|
| 424 |
+
}
|
| 425 |
+
}
|
| 426 |
+
if abbrevs[i].tag == DW_TAG_compile_unit && haveLo {
|
| 427 |
+
abbrevs[i].attr = append(abbrevs[i].attr,
|
| 428 |
+
dwAttrForm{DW_AT_addr_base, DW_FORM_sec_offset})
|
| 429 |
+
}
|
| 430 |
+
}
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
abbrevsFinalized = true
|
| 434 |
+
return abbrevs
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
// abbrevs is a raw table of abbrev entries; it needs to be post-processed
|
| 438 |
+
// by the Abbrevs() function above prior to being consumed, to expand
|
| 439 |
+
// the 'pseudo-form' entries below to real DWARF form values.
|
| 440 |
+
|
| 441 |
+
var abbrevs = []dwAbbrev{
|
| 442 |
+
/* The mandatory DW_ABRV_NULL entry. */
|
| 443 |
+
{0, 0, []dwAttrForm{}},
|
| 444 |
+
|
| 445 |
+
/* COMPUNIT */
|
| 446 |
+
{
|
| 447 |
+
DW_TAG_compile_unit,
|
| 448 |
+
DW_CHILDREN_yes,
|
| 449 |
+
[]dwAttrForm{
|
| 450 |
+
{DW_AT_name, DW_FORM_string},
|
| 451 |
+
{DW_AT_language, DW_FORM_data1},
|
| 452 |
+
{DW_AT_stmt_list, DW_FORM_sec_offset},
|
| 453 |
+
{DW_AT_low_pc, DW_FORM_addr},
|
| 454 |
+
{DW_AT_ranges, DW_FORM_sec_offset},
|
| 455 |
+
{DW_AT_comp_dir, DW_FORM_string},
|
| 456 |
+
{DW_AT_producer, DW_FORM_string},
|
| 457 |
+
{DW_AT_go_package_name, DW_FORM_string},
|
| 458 |
+
// NB: DWARF5 adds DW_AT_addr_base here.
|
| 459 |
+
},
|
| 460 |
+
},
|
| 461 |
+
|
| 462 |
+
/* COMPUNIT_TEXTLESS */
|
| 463 |
+
{
|
| 464 |
+
DW_TAG_compile_unit,
|
| 465 |
+
DW_CHILDREN_yes,
|
| 466 |
+
[]dwAttrForm{
|
| 467 |
+
{DW_AT_name, DW_FORM_string},
|
| 468 |
+
{DW_AT_language, DW_FORM_data1},
|
| 469 |
+
{DW_AT_comp_dir, DW_FORM_string},
|
| 470 |
+
{DW_AT_producer, DW_FORM_string},
|
| 471 |
+
{DW_AT_go_package_name, DW_FORM_string},
|
| 472 |
+
},
|
| 473 |
+
},
|
| 474 |
+
|
| 475 |
+
/* FUNCTION */
|
| 476 |
+
{
|
| 477 |
+
DW_TAG_subprogram,
|
| 478 |
+
DW_CHILDREN_yes,
|
| 479 |
+
[]dwAttrForm{
|
| 480 |
+
{DW_AT_name, DW_FORM_string},
|
| 481 |
+
{DW_AT_low_pc, DW_FORM_lo_pc_pseudo},
|
| 482 |
+
{DW_AT_high_pc, DW_FORM_hi_pc_pseudo},
|
| 483 |
+
{DW_AT_frame_base, DW_FORM_block1},
|
| 484 |
+
{DW_AT_decl_file, DW_FORM_data4},
|
| 485 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 486 |
+
{DW_AT_external, DW_FORM_flag},
|
| 487 |
+
},
|
| 488 |
+
},
|
| 489 |
+
|
| 490 |
+
/* WRAPPER */
|
| 491 |
+
{
|
| 492 |
+
DW_TAG_subprogram,
|
| 493 |
+
DW_CHILDREN_yes,
|
| 494 |
+
[]dwAttrForm{
|
| 495 |
+
{DW_AT_name, DW_FORM_string},
|
| 496 |
+
{DW_AT_low_pc, DW_FORM_lo_pc_pseudo},
|
| 497 |
+
{DW_AT_high_pc, DW_FORM_hi_pc_pseudo},
|
| 498 |
+
{DW_AT_frame_base, DW_FORM_block1},
|
| 499 |
+
{DW_AT_trampoline, DW_FORM_flag},
|
| 500 |
+
},
|
| 501 |
+
},
|
| 502 |
+
|
| 503 |
+
/* FUNCTION_ABSTRACT */
|
| 504 |
+
{
|
| 505 |
+
DW_TAG_subprogram,
|
| 506 |
+
DW_CHILDREN_yes,
|
| 507 |
+
[]dwAttrForm{
|
| 508 |
+
{DW_AT_name, DW_FORM_string},
|
| 509 |
+
{DW_AT_inline, DW_FORM_data1},
|
| 510 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 511 |
+
{DW_AT_external, DW_FORM_flag},
|
| 512 |
+
},
|
| 513 |
+
},
|
| 514 |
+
|
| 515 |
+
/* FUNCTION_CONCRETE */
|
| 516 |
+
{
|
| 517 |
+
DW_TAG_subprogram,
|
| 518 |
+
DW_CHILDREN_yes,
|
| 519 |
+
[]dwAttrForm{
|
| 520 |
+
{DW_AT_abstract_origin, DW_FORM_ref_addr},
|
| 521 |
+
{DW_AT_low_pc, DW_FORM_lo_pc_pseudo},
|
| 522 |
+
{DW_AT_high_pc, DW_FORM_hi_pc_pseudo},
|
| 523 |
+
{DW_AT_frame_base, DW_FORM_block1},
|
| 524 |
+
},
|
| 525 |
+
},
|
| 526 |
+
|
| 527 |
+
/* WRAPPER_CONCRETE */
|
| 528 |
+
{
|
| 529 |
+
DW_TAG_subprogram,
|
| 530 |
+
DW_CHILDREN_yes,
|
| 531 |
+
[]dwAttrForm{
|
| 532 |
+
{DW_AT_abstract_origin, DW_FORM_ref_addr},
|
| 533 |
+
{DW_AT_low_pc, DW_FORM_lo_pc_pseudo},
|
| 534 |
+
{DW_AT_high_pc, DW_FORM_hi_pc_pseudo},
|
| 535 |
+
{DW_AT_frame_base, DW_FORM_block1},
|
| 536 |
+
{DW_AT_trampoline, DW_FORM_flag},
|
| 537 |
+
},
|
| 538 |
+
},
|
| 539 |
+
|
| 540 |
+
/* INLINED_SUBROUTINE */
|
| 541 |
+
{
|
| 542 |
+
DW_TAG_inlined_subroutine,
|
| 543 |
+
DW_CHILDREN_yes,
|
| 544 |
+
[]dwAttrForm{
|
| 545 |
+
{DW_AT_abstract_origin, DW_FORM_ref_addr},
|
| 546 |
+
{DW_AT_low_pc, DW_FORM_lo_pc_pseudo},
|
| 547 |
+
{DW_AT_high_pc, DW_FORM_hi_pc_pseudo},
|
| 548 |
+
{DW_AT_call_file, DW_FORM_data4},
|
| 549 |
+
{DW_AT_call_line, DW_FORM_udata_pseudo}, // pseudo-form
|
| 550 |
+
},
|
| 551 |
+
},
|
| 552 |
+
|
| 553 |
+
/* INLINED_SUBROUTINE_RANGES */
|
| 554 |
+
{
|
| 555 |
+
DW_TAG_inlined_subroutine,
|
| 556 |
+
DW_CHILDREN_yes,
|
| 557 |
+
[]dwAttrForm{
|
| 558 |
+
{DW_AT_abstract_origin, DW_FORM_ref_addr},
|
| 559 |
+
{DW_AT_ranges, DW_FORM_sec_offset},
|
| 560 |
+
{DW_AT_call_file, DW_FORM_data4},
|
| 561 |
+
{DW_AT_call_line, DW_FORM_udata_pseudo}, // pseudo-form
|
| 562 |
+
},
|
| 563 |
+
},
|
| 564 |
+
|
| 565 |
+
/* VARIABLE */
|
| 566 |
+
{
|
| 567 |
+
DW_TAG_variable,
|
| 568 |
+
DW_CHILDREN_no,
|
| 569 |
+
[]dwAttrForm{
|
| 570 |
+
{DW_AT_name, DW_FORM_string},
|
| 571 |
+
{DW_AT_location, DW_FORM_block1},
|
| 572 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 573 |
+
{DW_AT_external, DW_FORM_flag},
|
| 574 |
+
},
|
| 575 |
+
},
|
| 576 |
+
|
| 577 |
+
/* INT CONSTANT */
|
| 578 |
+
{
|
| 579 |
+
DW_TAG_constant,
|
| 580 |
+
DW_CHILDREN_no,
|
| 581 |
+
[]dwAttrForm{
|
| 582 |
+
{DW_AT_name, DW_FORM_string},
|
| 583 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 584 |
+
{DW_AT_const_value, DW_FORM_sdata},
|
| 585 |
+
},
|
| 586 |
+
},
|
| 587 |
+
|
| 588 |
+
/* LEXICAL_BLOCK_RANGES */
|
| 589 |
+
{
|
| 590 |
+
DW_TAG_lexical_block,
|
| 591 |
+
DW_CHILDREN_yes,
|
| 592 |
+
[]dwAttrForm{
|
| 593 |
+
{DW_AT_ranges, DW_FORM_sec_offset},
|
| 594 |
+
},
|
| 595 |
+
},
|
| 596 |
+
|
| 597 |
+
/* LEXICAL_BLOCK_SIMPLE */
|
| 598 |
+
{
|
| 599 |
+
DW_TAG_lexical_block,
|
| 600 |
+
DW_CHILDREN_yes,
|
| 601 |
+
[]dwAttrForm{
|
| 602 |
+
// Note: unused if we are generating DWARF 5, we
|
| 603 |
+
// use the ranges form even if there is a singleton range.
|
| 604 |
+
{DW_AT_low_pc, DW_FORM_addr},
|
| 605 |
+
{DW_AT_high_pc, DW_FORM_addr},
|
| 606 |
+
},
|
| 607 |
+
},
|
| 608 |
+
|
| 609 |
+
/* STRUCTFIELD */
|
| 610 |
+
{
|
| 611 |
+
DW_TAG_member,
|
| 612 |
+
DW_CHILDREN_no,
|
| 613 |
+
// This abbrev is special-cased by the linker (unlike other DIEs
|
| 614 |
+
// we don't want a loader.Sym created for this DIE).
|
| 615 |
+
[]dwAttrForm{
|
| 616 |
+
{DW_AT_name, DW_FORM_string},
|
| 617 |
+
{DW_AT_data_member_location, DW_FORM_udata},
|
| 618 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 619 |
+
{DW_AT_go_embedded_field, DW_FORM_flag},
|
| 620 |
+
},
|
| 621 |
+
},
|
| 622 |
+
|
| 623 |
+
/* FUNCTYPEPARAM */
|
| 624 |
+
{
|
| 625 |
+
DW_TAG_formal_parameter,
|
| 626 |
+
DW_CHILDREN_no,
|
| 627 |
+
|
| 628 |
+
// No name!
|
| 629 |
+
// This abbrev is special-cased by the linker (unlike other DIEs
|
| 630 |
+
// we don't want a loader.Sym created for this DIE).
|
| 631 |
+
[]dwAttrForm{
|
| 632 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 633 |
+
},
|
| 634 |
+
},
|
| 635 |
+
|
| 636 |
+
/* FUNCTYPEOUTPARAM */
|
| 637 |
+
{
|
| 638 |
+
DW_TAG_formal_parameter,
|
| 639 |
+
DW_CHILDREN_no,
|
| 640 |
+
|
| 641 |
+
// No name!
|
| 642 |
+
// This abbrev is special-cased by the linker (unlike other DIEs
|
| 643 |
+
// we don't want a loader.Sym created for this DIE).
|
| 644 |
+
[]dwAttrForm{
|
| 645 |
+
{DW_AT_variable_parameter, DW_FORM_flag},
|
| 646 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 647 |
+
},
|
| 648 |
+
},
|
| 649 |
+
|
| 650 |
+
/* DOTDOTDOT */
|
| 651 |
+
{
|
| 652 |
+
DW_TAG_unspecified_parameters,
|
| 653 |
+
DW_CHILDREN_no,
|
| 654 |
+
// No name.
|
| 655 |
+
// This abbrev is special-cased by the linker (unlike other DIEs
|
| 656 |
+
// we don't want a loader.Sym created for this DIE).
|
| 657 |
+
[]dwAttrForm{},
|
| 658 |
+
},
|
| 659 |
+
|
| 660 |
+
/* ARRAYRANGE */
|
| 661 |
+
{
|
| 662 |
+
DW_TAG_subrange_type,
|
| 663 |
+
DW_CHILDREN_no,
|
| 664 |
+
|
| 665 |
+
// No name!
|
| 666 |
+
// This abbrev is special-cased by the linker (unlike other DIEs
|
| 667 |
+
// we don't want a loader.Sym created for this DIE).
|
| 668 |
+
[]dwAttrForm{
|
| 669 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 670 |
+
{DW_AT_count, DW_FORM_udata},
|
| 671 |
+
},
|
| 672 |
+
},
|
| 673 |
+
|
| 674 |
+
// Below here are the types considered public by ispubtype
|
| 675 |
+
/* NULLTYPE */
|
| 676 |
+
{
|
| 677 |
+
DW_TAG_unspecified_type,
|
| 678 |
+
DW_CHILDREN_no,
|
| 679 |
+
[]dwAttrForm{
|
| 680 |
+
{DW_AT_name, DW_FORM_string},
|
| 681 |
+
},
|
| 682 |
+
},
|
| 683 |
+
|
| 684 |
+
/* BASETYPE */
|
| 685 |
+
{
|
| 686 |
+
DW_TAG_base_type,
|
| 687 |
+
DW_CHILDREN_no,
|
| 688 |
+
[]dwAttrForm{
|
| 689 |
+
{DW_AT_name, DW_FORM_string},
|
| 690 |
+
{DW_AT_encoding, DW_FORM_data1},
|
| 691 |
+
{DW_AT_byte_size, DW_FORM_data1},
|
| 692 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 693 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 694 |
+
},
|
| 695 |
+
},
|
| 696 |
+
|
| 697 |
+
/* ARRAYTYPE */
|
| 698 |
+
// child is subrange with upper bound
|
| 699 |
+
{
|
| 700 |
+
DW_TAG_array_type,
|
| 701 |
+
DW_CHILDREN_yes,
|
| 702 |
+
[]dwAttrForm{
|
| 703 |
+
{DW_AT_name, DW_FORM_string},
|
| 704 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 705 |
+
{DW_AT_byte_size, DW_FORM_udata},
|
| 706 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 707 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 708 |
+
},
|
| 709 |
+
},
|
| 710 |
+
|
| 711 |
+
/* CHANTYPE */
|
| 712 |
+
{
|
| 713 |
+
DW_TAG_typedef,
|
| 714 |
+
DW_CHILDREN_no,
|
| 715 |
+
[]dwAttrForm{
|
| 716 |
+
{DW_AT_name, DW_FORM_string},
|
| 717 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 718 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 719 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 720 |
+
{DW_AT_go_elem, DW_FORM_ref_addr},
|
| 721 |
+
},
|
| 722 |
+
},
|
| 723 |
+
|
| 724 |
+
/* FUNCTYPE */
|
| 725 |
+
{
|
| 726 |
+
DW_TAG_subroutine_type,
|
| 727 |
+
DW_CHILDREN_yes,
|
| 728 |
+
[]dwAttrForm{
|
| 729 |
+
{DW_AT_name, DW_FORM_string},
|
| 730 |
+
{DW_AT_byte_size, DW_FORM_udata},
|
| 731 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 732 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 733 |
+
},
|
| 734 |
+
},
|
| 735 |
+
|
| 736 |
+
/* IFACETYPE */
|
| 737 |
+
{
|
| 738 |
+
DW_TAG_typedef,
|
| 739 |
+
DW_CHILDREN_yes,
|
| 740 |
+
[]dwAttrForm{
|
| 741 |
+
{DW_AT_name, DW_FORM_string},
|
| 742 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 743 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 744 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 745 |
+
},
|
| 746 |
+
},
|
| 747 |
+
|
| 748 |
+
/* MAPTYPE */
|
| 749 |
+
{
|
| 750 |
+
DW_TAG_typedef,
|
| 751 |
+
DW_CHILDREN_no,
|
| 752 |
+
[]dwAttrForm{
|
| 753 |
+
{DW_AT_name, DW_FORM_string},
|
| 754 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 755 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 756 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 757 |
+
{DW_AT_go_key, DW_FORM_ref_addr},
|
| 758 |
+
{DW_AT_go_elem, DW_FORM_ref_addr},
|
| 759 |
+
},
|
| 760 |
+
},
|
| 761 |
+
|
| 762 |
+
/* PTRTYPE */
|
| 763 |
+
{
|
| 764 |
+
DW_TAG_pointer_type,
|
| 765 |
+
DW_CHILDREN_no,
|
| 766 |
+
[]dwAttrForm{
|
| 767 |
+
{DW_AT_name, DW_FORM_string},
|
| 768 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 769 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 770 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 771 |
+
},
|
| 772 |
+
},
|
| 773 |
+
|
| 774 |
+
/* BARE_PTRTYPE */
|
| 775 |
+
{
|
| 776 |
+
DW_TAG_pointer_type,
|
| 777 |
+
DW_CHILDREN_no,
|
| 778 |
+
[]dwAttrForm{
|
| 779 |
+
{DW_AT_name, DW_FORM_string},
|
| 780 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 781 |
+
},
|
| 782 |
+
},
|
| 783 |
+
|
| 784 |
+
/* SLICETYPE */
|
| 785 |
+
{
|
| 786 |
+
DW_TAG_structure_type,
|
| 787 |
+
DW_CHILDREN_yes,
|
| 788 |
+
[]dwAttrForm{
|
| 789 |
+
{DW_AT_name, DW_FORM_string},
|
| 790 |
+
{DW_AT_byte_size, DW_FORM_udata},
|
| 791 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 792 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 793 |
+
{DW_AT_go_elem, DW_FORM_ref_addr},
|
| 794 |
+
},
|
| 795 |
+
},
|
| 796 |
+
|
| 797 |
+
/* STRINGTYPE */
|
| 798 |
+
{
|
| 799 |
+
DW_TAG_structure_type,
|
| 800 |
+
DW_CHILDREN_yes,
|
| 801 |
+
[]dwAttrForm{
|
| 802 |
+
{DW_AT_name, DW_FORM_string},
|
| 803 |
+
{DW_AT_byte_size, DW_FORM_udata},
|
| 804 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 805 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 806 |
+
},
|
| 807 |
+
},
|
| 808 |
+
|
| 809 |
+
/* STRUCTTYPE */
|
| 810 |
+
{
|
| 811 |
+
DW_TAG_structure_type,
|
| 812 |
+
DW_CHILDREN_yes,
|
| 813 |
+
[]dwAttrForm{
|
| 814 |
+
{DW_AT_name, DW_FORM_string},
|
| 815 |
+
{DW_AT_byte_size, DW_FORM_udata},
|
| 816 |
+
{DW_AT_go_kind, DW_FORM_data1},
|
| 817 |
+
{DW_AT_go_runtime_type, DW_FORM_addr},
|
| 818 |
+
},
|
| 819 |
+
},
|
| 820 |
+
|
| 821 |
+
/* TYPEDECL */
|
| 822 |
+
{
|
| 823 |
+
DW_TAG_typedef,
|
| 824 |
+
DW_CHILDREN_no,
|
| 825 |
+
[]dwAttrForm{
|
| 826 |
+
{DW_AT_name, DW_FORM_string},
|
| 827 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 828 |
+
},
|
| 829 |
+
},
|
| 830 |
+
|
| 831 |
+
/* DICT_INDEX */
|
| 832 |
+
{
|
| 833 |
+
DW_TAG_typedef,
|
| 834 |
+
DW_CHILDREN_no,
|
| 835 |
+
[]dwAttrForm{
|
| 836 |
+
{DW_AT_name, DW_FORM_string},
|
| 837 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 838 |
+
{DW_AT_go_dict_index, DW_FORM_udata},
|
| 839 |
+
},
|
| 840 |
+
},
|
| 841 |
+
}
|
| 842 |
+
|
| 843 |
+
// GetAbbrev returns the contents of the .debug_abbrev section.
|
| 844 |
+
func GetAbbrev() []byte {
|
| 845 |
+
abbrevs := Abbrevs()
|
| 846 |
+
var buf []byte
|
| 847 |
+
for i := 1; i < len(abbrevs); i++ {
|
| 848 |
+
// See section 7.5.3
|
| 849 |
+
buf = AppendUleb128(buf, uint64(i))
|
| 850 |
+
buf = AppendUleb128(buf, uint64(abbrevs[i].tag))
|
| 851 |
+
buf = append(buf, abbrevs[i].children)
|
| 852 |
+
for _, f := range abbrevs[i].attr {
|
| 853 |
+
buf = AppendUleb128(buf, uint64(f.attr))
|
| 854 |
+
buf = AppendUleb128(buf, uint64(f.form))
|
| 855 |
+
}
|
| 856 |
+
buf = append(buf, 0, 0)
|
| 857 |
+
}
|
| 858 |
+
return append(buf, 0)
|
| 859 |
+
}
|
| 860 |
+
|
| 861 |
+
/*
|
| 862 |
+
* Debugging Information Entries and their attributes.
|
| 863 |
+
*/
|
| 864 |
+
|
| 865 |
+
// DWAttr represents an attribute of a DWDie.
|
| 866 |
+
//
|
| 867 |
+
// For DW_CLS_string and _block, value should contain the length, and
|
| 868 |
+
// data the data, for _reference, value is 0 and data is a DWDie* to
|
| 869 |
+
// the referenced instance, for all others, value is the whole thing
|
| 870 |
+
// and data is null.
|
| 871 |
+
type DWAttr struct {
|
| 872 |
+
Link *DWAttr
|
| 873 |
+
Atr uint16 // DW_AT_
|
| 874 |
+
Cls uint8 // DW_CLS_
|
| 875 |
+
Value int64
|
| 876 |
+
Data any
|
| 877 |
+
}
|
| 878 |
+
|
| 879 |
+
// DWDie represents a DWARF debug info entry.
|
| 880 |
+
type DWDie struct {
|
| 881 |
+
Abbrev int
|
| 882 |
+
Link *DWDie
|
| 883 |
+
Child *DWDie
|
| 884 |
+
Attr *DWAttr
|
| 885 |
+
Sym Sym
|
| 886 |
+
}
|
| 887 |
+
|
| 888 |
+
func putattr(ctxt Context, s Sym, abbrev int, form int, cls int, value int64, data any) error {
|
| 889 |
+
switch form {
|
| 890 |
+
case DW_FORM_addr: // address
|
| 891 |
+
// Allow nil addresses for DW_AT_go_runtime_type.
|
| 892 |
+
if data == nil && value == 0 {
|
| 893 |
+
ctxt.AddInt(s, ctxt.PtrSize(), 0)
|
| 894 |
+
break
|
| 895 |
+
}
|
| 896 |
+
if cls == DW_CLS_GO_TYPEREF {
|
| 897 |
+
ctxt.AddSectionOffset(s, ctxt.PtrSize(), data, value)
|
| 898 |
+
break
|
| 899 |
+
}
|
| 900 |
+
ctxt.AddAddress(s, data, value)
|
| 901 |
+
|
| 902 |
+
case DW_FORM_block1: // block
|
| 903 |
+
if cls == DW_CLS_ADDRESS {
|
| 904 |
+
ctxt.AddInt(s, 1, int64(1+ctxt.PtrSize()))
|
| 905 |
+
ctxt.AddInt(s, 1, DW_OP_addr)
|
| 906 |
+
ctxt.AddAddress(s, data, 0)
|
| 907 |
+
break
|
| 908 |
+
}
|
| 909 |
+
|
| 910 |
+
value &= 0xff
|
| 911 |
+
ctxt.AddInt(s, 1, value)
|
| 912 |
+
p := data.([]byte)[:value]
|
| 913 |
+
ctxt.AddBytes(s, p)
|
| 914 |
+
|
| 915 |
+
case DW_FORM_block2: // block
|
| 916 |
+
value &= 0xffff
|
| 917 |
+
|
| 918 |
+
ctxt.AddInt(s, 2, value)
|
| 919 |
+
p := data.([]byte)[:value]
|
| 920 |
+
ctxt.AddBytes(s, p)
|
| 921 |
+
|
| 922 |
+
case DW_FORM_block4: // block
|
| 923 |
+
value &= 0xffffffff
|
| 924 |
+
|
| 925 |
+
ctxt.AddInt(s, 4, value)
|
| 926 |
+
p := data.([]byte)[:value]
|
| 927 |
+
ctxt.AddBytes(s, p)
|
| 928 |
+
|
| 929 |
+
case DW_FORM_block: // block
|
| 930 |
+
Uleb128put(ctxt, s, value)
|
| 931 |
+
|
| 932 |
+
p := data.([]byte)[:value]
|
| 933 |
+
ctxt.AddBytes(s, p)
|
| 934 |
+
|
| 935 |
+
case DW_FORM_data1: // constant
|
| 936 |
+
ctxt.AddInt(s, 1, value)
|
| 937 |
+
|
| 938 |
+
case DW_FORM_data2: // constant
|
| 939 |
+
ctxt.AddInt(s, 2, value)
|
| 940 |
+
|
| 941 |
+
case DW_FORM_data4: // constant, {line,loclist,mac,rangelist}ptr
|
| 942 |
+
if cls == DW_CLS_PTR { // DW_AT_stmt_list and DW_AT_ranges
|
| 943 |
+
ctxt.AddDWARFAddrSectionOffset(s, data, value)
|
| 944 |
+
break
|
| 945 |
+
}
|
| 946 |
+
ctxt.AddInt(s, 4, value)
|
| 947 |
+
|
| 948 |
+
case DW_FORM_data8: // constant, {line,loclist,mac,rangelist}ptr
|
| 949 |
+
ctxt.AddInt(s, 8, value)
|
| 950 |
+
|
| 951 |
+
case DW_FORM_sdata: // constant
|
| 952 |
+
Sleb128put(ctxt, s, value)
|
| 953 |
+
|
| 954 |
+
case DW_FORM_udata: // constant
|
| 955 |
+
Uleb128put(ctxt, s, value)
|
| 956 |
+
|
| 957 |
+
case DW_FORM_string: // string
|
| 958 |
+
str := data.(string)
|
| 959 |
+
ctxt.AddString(s, str)
|
| 960 |
+
// TODO(ribrdb): verify padded strings are never used and remove this
|
| 961 |
+
for i := int64(len(str)); i < value; i++ {
|
| 962 |
+
ctxt.AddInt(s, 1, 0)
|
| 963 |
+
}
|
| 964 |
+
|
| 965 |
+
case DW_FORM_flag: // flag
|
| 966 |
+
if value != 0 {
|
| 967 |
+
ctxt.AddInt(s, 1, 1)
|
| 968 |
+
} else {
|
| 969 |
+
ctxt.AddInt(s, 1, 0)
|
| 970 |
+
}
|
| 971 |
+
|
| 972 |
+
// As of DWARF 3 the ref_addr is always 32 bits, unless emitting a large
|
| 973 |
+
// (> 4 GB of debug info aka "64-bit") unit, which we don't implement.
|
| 974 |
+
case DW_FORM_ref_addr: // reference to a DIE in the .info section
|
| 975 |
+
fallthrough
|
| 976 |
+
case DW_FORM_sec_offset: // offset into a DWARF section other than .info
|
| 977 |
+
if data == nil {
|
| 978 |
+
return fmt.Errorf("dwarf: null reference in %d", abbrev)
|
| 979 |
+
}
|
| 980 |
+
ctxt.AddDWARFAddrSectionOffset(s, data, value)
|
| 981 |
+
|
| 982 |
+
case DW_FORM_addrx: // index into .debug_addr section
|
| 983 |
+
ctxt.AddIndirectTextRef(s, data)
|
| 984 |
+
|
| 985 |
+
case DW_FORM_ref1, // reference within the compilation unit
|
| 986 |
+
DW_FORM_ref2, // reference
|
| 987 |
+
DW_FORM_ref4, // reference
|
| 988 |
+
DW_FORM_ref8, // reference
|
| 989 |
+
DW_FORM_ref_udata, // reference
|
| 990 |
+
|
| 991 |
+
DW_FORM_strp, // string
|
| 992 |
+
DW_FORM_indirect: // (see Section 7.5.3)
|
| 993 |
+
fallthrough
|
| 994 |
+
default:
|
| 995 |
+
return fmt.Errorf("dwarf: unsupported attribute form %d / class %d", form, cls)
|
| 996 |
+
}
|
| 997 |
+
return nil
|
| 998 |
+
}
|
| 999 |
+
|
| 1000 |
+
// PutAttrs writes the attributes for a DIE to symbol 's'.
|
| 1001 |
+
//
|
| 1002 |
+
// Note that we can (and do) add arbitrary attributes to a DIE, but
|
| 1003 |
+
// only the ones actually listed in the Abbrev will be written out.
|
| 1004 |
+
func PutAttrs(ctxt Context, s Sym, abbrev int, attr *DWAttr) {
|
| 1005 |
+
abbrevs := Abbrevs()
|
| 1006 |
+
Outer:
|
| 1007 |
+
for _, f := range abbrevs[abbrev].attr {
|
| 1008 |
+
for ap := attr; ap != nil; ap = ap.Link {
|
| 1009 |
+
if ap.Atr == f.attr {
|
| 1010 |
+
putattr(ctxt, s, abbrev, int(f.form), int(ap.Cls), ap.Value, ap.Data)
|
| 1011 |
+
continue Outer
|
| 1012 |
+
}
|
| 1013 |
+
}
|
| 1014 |
+
|
| 1015 |
+
putattr(ctxt, s, abbrev, int(f.form), 0, 0, nil)
|
| 1016 |
+
}
|
| 1017 |
+
}
|
| 1018 |
+
|
| 1019 |
+
// HasChildren reports whether 'die' uses an abbrev that supports children.
|
| 1020 |
+
func HasChildren(die *DWDie) bool {
|
| 1021 |
+
abbrevs := Abbrevs()
|
| 1022 |
+
return abbrevs[die.Abbrev].children != 0
|
| 1023 |
+
}
|
| 1024 |
+
|
| 1025 |
+
// PutIntConst writes a DIE for an integer constant
|
| 1026 |
+
func PutIntConst(ctxt Context, info, typ Sym, name string, val int64) {
|
| 1027 |
+
Uleb128put(ctxt, info, DW_ABRV_INT_CONSTANT)
|
| 1028 |
+
putattr(ctxt, info, DW_ABRV_INT_CONSTANT, DW_FORM_string, DW_CLS_STRING, int64(len(name)), name)
|
| 1029 |
+
putattr(ctxt, info, DW_ABRV_INT_CONSTANT, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, typ)
|
| 1030 |
+
putattr(ctxt, info, DW_ABRV_INT_CONSTANT, DW_FORM_sdata, DW_CLS_CONSTANT, val, nil)
|
| 1031 |
+
}
|
| 1032 |
+
|
| 1033 |
+
// PutGlobal writes a DIE for a global variable.
|
| 1034 |
+
func PutGlobal(ctxt Context, info, typ, gvar Sym, name string) {
|
| 1035 |
+
Uleb128put(ctxt, info, DW_ABRV_VARIABLE)
|
| 1036 |
+
putattr(ctxt, info, DW_ABRV_VARIABLE, DW_FORM_string, DW_CLS_STRING, int64(len(name)), name)
|
| 1037 |
+
putattr(ctxt, info, DW_ABRV_VARIABLE, DW_FORM_block1, DW_CLS_ADDRESS, 0, gvar)
|
| 1038 |
+
putattr(ctxt, info, DW_ABRV_VARIABLE, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, typ)
|
| 1039 |
+
putattr(ctxt, info, DW_ABRV_VARIABLE, DW_FORM_flag, DW_CLS_FLAG, 1, nil)
|
| 1040 |
+
}
|
| 1041 |
+
|
| 1042 |
+
// PutBasedRanges writes a range table to sym. All addresses in ranges are
|
| 1043 |
+
// relative to some base address, which must be arranged by the caller
|
| 1044 |
+
// (e.g., with a DW_AT_low_pc attribute, or in a BASE-prefixed range).
|
| 1045 |
+
func PutBasedRanges(ctxt Context, sym Sym, ranges []Range) {
|
| 1046 |
+
ps := ctxt.PtrSize()
|
| 1047 |
+
// Write ranges.
|
| 1048 |
+
for _, r := range ranges {
|
| 1049 |
+
ctxt.AddInt(sym, ps, r.Start)
|
| 1050 |
+
ctxt.AddInt(sym, ps, r.End)
|
| 1051 |
+
}
|
| 1052 |
+
// Write trailer.
|
| 1053 |
+
ctxt.AddInt(sym, ps, 0)
|
| 1054 |
+
ctxt.AddInt(sym, ps, 0)
|
| 1055 |
+
}
|
| 1056 |
+
|
| 1057 |
+
// PutRngListRanges writes a DWARF5-style set of rangelist entries to sym,
|
| 1058 |
+
// using base as a starting/base address.
|
| 1059 |
+
func PutRngListRanges(ctxt Context, sym Sym, base Sym, ranges []Range) {
|
| 1060 |
+
addULEB128 := func(v int64) {
|
| 1061 |
+
b := sevenBitU(v)
|
| 1062 |
+
if b == nil {
|
| 1063 |
+
var encbuf [20]byte
|
| 1064 |
+
b = AppendUleb128(encbuf[:0], uint64(v))
|
| 1065 |
+
}
|
| 1066 |
+
ctxt.AddBytes(sym, b)
|
| 1067 |
+
}
|
| 1068 |
+
// First entry is base address.
|
| 1069 |
+
ctxt.AddInt(sym, 1, DW_RLE_base_addressx)
|
| 1070 |
+
ctxt.AddIndirectTextRef(sym, base)
|
| 1071 |
+
// Remaining entries are .debug_rnglist offset pairs
|
| 1072 |
+
for _, r := range ranges {
|
| 1073 |
+
ctxt.AddInt(sym, 1, DW_RLE_offset_pair)
|
| 1074 |
+
addULEB128(r.Start)
|
| 1075 |
+
addULEB128(r.End)
|
| 1076 |
+
}
|
| 1077 |
+
// Terminator to mark end of list
|
| 1078 |
+
ctxt.AddInt(sym, 1, DW_RLE_end_of_list)
|
| 1079 |
+
}
|
| 1080 |
+
|
| 1081 |
+
// PutRanges writes a range table to s.Ranges.
|
| 1082 |
+
// All addresses in ranges are relative to s.base.
|
| 1083 |
+
func (s *FnState) PutRanges(ctxt Context, ranges []Range) {
|
| 1084 |
+
ps := ctxt.PtrSize()
|
| 1085 |
+
sym, base := s.Ranges, s.StartPC
|
| 1086 |
+
|
| 1087 |
+
if buildcfg.Experiment.Dwarf5 {
|
| 1088 |
+
PutRngListRanges(ctxt, sym, base, ranges)
|
| 1089 |
+
return
|
| 1090 |
+
}
|
| 1091 |
+
|
| 1092 |
+
if s.UseBASEntries {
|
| 1093 |
+
// Using a Base Address Selection Entry reduces the number of relocations, but
|
| 1094 |
+
// this is not done on macOS because it is not supported by dsymutil/dwarfdump/lldb
|
| 1095 |
+
ctxt.AddInt(sym, ps, -1)
|
| 1096 |
+
ctxt.AddAddress(sym, base, 0)
|
| 1097 |
+
PutBasedRanges(ctxt, sym, ranges)
|
| 1098 |
+
return
|
| 1099 |
+
}
|
| 1100 |
+
|
| 1101 |
+
// Write ranges full of relocations
|
| 1102 |
+
for _, r := range ranges {
|
| 1103 |
+
ctxt.AddCURelativeAddress(sym, base, r.Start)
|
| 1104 |
+
ctxt.AddCURelativeAddress(sym, base, r.End)
|
| 1105 |
+
}
|
| 1106 |
+
// Write trailer.
|
| 1107 |
+
ctxt.AddInt(sym, ps, 0)
|
| 1108 |
+
ctxt.AddInt(sym, ps, 0)
|
| 1109 |
+
}
|
| 1110 |
+
|
| 1111 |
+
// Return TRUE if the inlined call in the specified slot is empty,
|
| 1112 |
+
// meaning it has a zero-length range (no instructions), and all
|
| 1113 |
+
// of its children are empty.
|
| 1114 |
+
func isEmptyInlinedCall(slot int, calls *InlCalls) bool {
|
| 1115 |
+
ic := &calls.Calls[slot]
|
| 1116 |
+
if ic.InlIndex == -2 {
|
| 1117 |
+
return true
|
| 1118 |
+
}
|
| 1119 |
+
live := false
|
| 1120 |
+
for _, k := range ic.Children {
|
| 1121 |
+
if !isEmptyInlinedCall(k, calls) {
|
| 1122 |
+
live = true
|
| 1123 |
+
}
|
| 1124 |
+
}
|
| 1125 |
+
if len(ic.Ranges) > 0 {
|
| 1126 |
+
live = true
|
| 1127 |
+
}
|
| 1128 |
+
if !live {
|
| 1129 |
+
ic.InlIndex = -2
|
| 1130 |
+
}
|
| 1131 |
+
return !live
|
| 1132 |
+
}
|
| 1133 |
+
|
| 1134 |
+
// Slot -1: return top-level inlines.
|
| 1135 |
+
// Slot >= 0: return children of that slot.
|
| 1136 |
+
func inlChildren(slot int, calls *InlCalls) []int {
|
| 1137 |
+
var kids []int
|
| 1138 |
+
if slot != -1 {
|
| 1139 |
+
for _, k := range calls.Calls[slot].Children {
|
| 1140 |
+
if !isEmptyInlinedCall(k, calls) {
|
| 1141 |
+
kids = append(kids, k)
|
| 1142 |
+
}
|
| 1143 |
+
}
|
| 1144 |
+
} else {
|
| 1145 |
+
for k := 0; k < len(calls.Calls); k += 1 {
|
| 1146 |
+
if calls.Calls[k].Root && !isEmptyInlinedCall(k, calls) {
|
| 1147 |
+
kids = append(kids, k)
|
| 1148 |
+
}
|
| 1149 |
+
}
|
| 1150 |
+
}
|
| 1151 |
+
return kids
|
| 1152 |
+
}
|
| 1153 |
+
|
| 1154 |
+
func inlinedVarTable(inlcalls *InlCalls) map[*Var]bool {
|
| 1155 |
+
vars := make(map[*Var]bool)
|
| 1156 |
+
for _, ic := range inlcalls.Calls {
|
| 1157 |
+
for _, v := range ic.InlVars {
|
| 1158 |
+
vars[v] = true
|
| 1159 |
+
}
|
| 1160 |
+
}
|
| 1161 |
+
return vars
|
| 1162 |
+
}
|
| 1163 |
+
|
| 1164 |
+
// The s.Scopes slice contains variables were originally part of the
|
| 1165 |
+
// function being emitted, as well as variables that were imported
|
| 1166 |
+
// from various callee functions during the inlining process. This
|
| 1167 |
+
// function prunes out any variables from the latter category (since
|
| 1168 |
+
// they will be emitted as part of DWARF inlined_subroutine DIEs) and
|
| 1169 |
+
// then generates scopes for vars in the former category.
|
| 1170 |
+
func putPrunedScopes(ctxt Context, s *FnState, fnabbrev int) error {
|
| 1171 |
+
if len(s.Scopes) == 0 {
|
| 1172 |
+
return nil
|
| 1173 |
+
}
|
| 1174 |
+
scopes := make([]Scope, len(s.Scopes), len(s.Scopes))
|
| 1175 |
+
pvars := inlinedVarTable(&s.InlCalls)
|
| 1176 |
+
for k, s := range s.Scopes {
|
| 1177 |
+
var pruned Scope = Scope{Parent: s.Parent, Ranges: s.Ranges}
|
| 1178 |
+
for i := 0; i < len(s.Vars); i++ {
|
| 1179 |
+
_, found := pvars[s.Vars[i]]
|
| 1180 |
+
if !found {
|
| 1181 |
+
pruned.Vars = append(pruned.Vars, s.Vars[i])
|
| 1182 |
+
}
|
| 1183 |
+
}
|
| 1184 |
+
slices.SortFunc(pruned.Vars, byChildIndexCmp)
|
| 1185 |
+
scopes[k] = pruned
|
| 1186 |
+
}
|
| 1187 |
+
|
| 1188 |
+
s.dictIndexToOffset = putparamtypes(ctxt, s, scopes, fnabbrev)
|
| 1189 |
+
|
| 1190 |
+
var encbuf [20]byte
|
| 1191 |
+
if putscope(ctxt, s, scopes, 0, fnabbrev, encbuf[:0]) < int32(len(scopes)) {
|
| 1192 |
+
return errors.New("multiple toplevel scopes")
|
| 1193 |
+
}
|
| 1194 |
+
return nil
|
| 1195 |
+
}
|
| 1196 |
+
|
| 1197 |
+
// Emit DWARF attributes and child DIEs for an 'abstract' subprogram.
|
| 1198 |
+
// The abstract subprogram DIE for a function contains its
|
| 1199 |
+
// location-independent attributes (name, type, etc). Other instances
|
| 1200 |
+
// of the function (any inlined copy of it, or the single out-of-line
|
| 1201 |
+
// 'concrete' instance) will contain a pointer back to this abstract
|
| 1202 |
+
// DIE (as a space-saving measure, so that name/type etc doesn't have
|
| 1203 |
+
// to be repeated for each inlined copy).
|
| 1204 |
+
func PutAbstractFunc(ctxt Context, s *FnState) error {
|
| 1205 |
+
if logDwarf {
|
| 1206 |
+
ctxt.Logf("PutAbstractFunc(%v)\n", s.Absfn)
|
| 1207 |
+
}
|
| 1208 |
+
|
| 1209 |
+
abbrev := DW_ABRV_FUNCTION_ABSTRACT
|
| 1210 |
+
Uleb128put(ctxt, s.Absfn, int64(abbrev))
|
| 1211 |
+
|
| 1212 |
+
fullname := s.Name
|
| 1213 |
+
if strings.HasPrefix(s.Name, `"".`) {
|
| 1214 |
+
return fmt.Errorf("unqualified symbol name: %v", s.Name)
|
| 1215 |
+
}
|
| 1216 |
+
putattr(ctxt, s.Absfn, abbrev, DW_FORM_string, DW_CLS_STRING, int64(len(fullname)), fullname)
|
| 1217 |
+
|
| 1218 |
+
// DW_AT_inlined value
|
| 1219 |
+
putattr(ctxt, s.Absfn, abbrev, DW_FORM_data1, DW_CLS_CONSTANT, int64(DW_INL_inlined), nil)
|
| 1220 |
+
|
| 1221 |
+
// TODO(mdempsky): Shouldn't we write out StartPos.FileIndex() too?
|
| 1222 |
+
putattr(ctxt, s.Absfn, abbrev, DW_FORM_udata, DW_CLS_CONSTANT, int64(s.StartPos.RelLine()), nil)
|
| 1223 |
+
|
| 1224 |
+
var ev int64
|
| 1225 |
+
if s.External {
|
| 1226 |
+
ev = 1
|
| 1227 |
+
}
|
| 1228 |
+
putattr(ctxt, s.Absfn, abbrev, DW_FORM_flag, DW_CLS_FLAG, ev, 0)
|
| 1229 |
+
|
| 1230 |
+
// Child variables (may be empty)
|
| 1231 |
+
var flattened []*Var
|
| 1232 |
+
|
| 1233 |
+
// This slice will hold the offset in bytes for each child var DIE
|
| 1234 |
+
// with respect to the start of the parent subprogram DIE.
|
| 1235 |
+
var offsets []int32
|
| 1236 |
+
|
| 1237 |
+
// Scopes/vars
|
| 1238 |
+
if len(s.Scopes) > 0 {
|
| 1239 |
+
// For abstract subprogram DIEs we want to flatten out scope info:
|
| 1240 |
+
// lexical scope DIEs contain range and/or hi/lo PC attributes,
|
| 1241 |
+
// which we explicitly don't want for the abstract subprogram DIE.
|
| 1242 |
+
pvars := inlinedVarTable(&s.InlCalls)
|
| 1243 |
+
for _, scope := range s.Scopes {
|
| 1244 |
+
for i := 0; i < len(scope.Vars); i++ {
|
| 1245 |
+
_, found := pvars[scope.Vars[i]]
|
| 1246 |
+
if found || !scope.Vars[i].IsInAbstract {
|
| 1247 |
+
continue
|
| 1248 |
+
}
|
| 1249 |
+
flattened = append(flattened, scope.Vars[i])
|
| 1250 |
+
}
|
| 1251 |
+
}
|
| 1252 |
+
if len(flattened) > 0 {
|
| 1253 |
+
slices.SortFunc(flattened, byChildIndexCmp)
|
| 1254 |
+
|
| 1255 |
+
if logDwarf {
|
| 1256 |
+
ctxt.Logf("putAbstractScope(%v): vars:", s.Info)
|
| 1257 |
+
for i, v := range flattened {
|
| 1258 |
+
ctxt.Logf(" %d:%s", i, v.Name)
|
| 1259 |
+
}
|
| 1260 |
+
ctxt.Logf("\n")
|
| 1261 |
+
}
|
| 1262 |
+
|
| 1263 |
+
// This slice will hold the offset in bytes for each child
|
| 1264 |
+
// variable DIE with respect to the start of the parent
|
| 1265 |
+
// subprogram DIE.
|
| 1266 |
+
for _, v := range flattened {
|
| 1267 |
+
offsets = append(offsets, int32(ctxt.CurrentOffset(s.Absfn)))
|
| 1268 |
+
putAbstractVar(ctxt, s.Absfn, v)
|
| 1269 |
+
}
|
| 1270 |
+
}
|
| 1271 |
+
}
|
| 1272 |
+
ctxt.RecordChildDieOffsets(s.Absfn, flattened, offsets)
|
| 1273 |
+
|
| 1274 |
+
Uleb128put(ctxt, s.Absfn, 0)
|
| 1275 |
+
return nil
|
| 1276 |
+
}
|
| 1277 |
+
|
| 1278 |
+
// dwarfFileIndex returns the DWARF file index value for the file associated
|
| 1279 |
+
// with pos.
|
| 1280 |
+
func dwarfFileIndex(pos src.Pos) int64 {
|
| 1281 |
+
return int64(1 + pos.FileIndex())
|
| 1282 |
+
}
|
| 1283 |
+
|
| 1284 |
+
// Emit DWARF attributes and child DIEs for an inlined subroutine. The
|
| 1285 |
+
// first attribute of an inlined subroutine DIE is a reference back to
|
| 1286 |
+
// its corresponding 'abstract' DIE (containing location-independent
|
| 1287 |
+
// attributes such as name, type, etc). Inlined subroutine DIEs can
|
| 1288 |
+
// have other inlined subroutine DIEs as children.
|
| 1289 |
+
func putInlinedFunc(ctxt Context, s *FnState, callIdx int) error {
|
| 1290 |
+
ic := s.InlCalls.Calls[callIdx]
|
| 1291 |
+
callee := ic.AbsFunSym
|
| 1292 |
+
|
| 1293 |
+
// For DWARF 5, we always use the ranges form of the abbrev, since
|
| 1294 |
+
// it is more compact than using explicit hi/lo PC attrs. See
|
| 1295 |
+
// issue #72821 for more on why this makes sense.
|
| 1296 |
+
abbrev := DW_ABRV_INLINED_SUBROUTINE_RANGES
|
| 1297 |
+
if len(ic.Ranges) == 1 && !buildcfg.Experiment.Dwarf5 {
|
| 1298 |
+
abbrev = DW_ABRV_INLINED_SUBROUTINE
|
| 1299 |
+
}
|
| 1300 |
+
Uleb128put(ctxt, s.Info, int64(abbrev))
|
| 1301 |
+
|
| 1302 |
+
if logDwarf {
|
| 1303 |
+
ctxt.Logf("putInlinedFunc(callee=%v,abbrev=%d)\n", callee, abbrev)
|
| 1304 |
+
}
|
| 1305 |
+
|
| 1306 |
+
// Abstract origin.
|
| 1307 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, callee)
|
| 1308 |
+
|
| 1309 |
+
if abbrev == DW_ABRV_INLINED_SUBROUTINE_RANGES {
|
| 1310 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_sec_offset, DW_CLS_PTR, ctxt.Size(s.Ranges), s.Ranges)
|
| 1311 |
+
s.PutRanges(ctxt, ic.Ranges)
|
| 1312 |
+
} else {
|
| 1313 |
+
st := ic.Ranges[0].Start
|
| 1314 |
+
en := ic.Ranges[0].End
|
| 1315 |
+
emitHiLoPc(ctxt, abbrev, s, st, en)
|
| 1316 |
+
}
|
| 1317 |
+
|
| 1318 |
+
// Emit call file, line attrs.
|
| 1319 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_data4, DW_CLS_CONSTANT, dwarfFileIndex(ic.CallPos), nil)
|
| 1320 |
+
form := int(expandPseudoForm(DW_FORM_udata_pseudo))
|
| 1321 |
+
putattr(ctxt, s.Info, abbrev, form, DW_CLS_CONSTANT, int64(ic.CallPos.RelLine()), nil)
|
| 1322 |
+
|
| 1323 |
+
// Variables associated with this inlined routine instance.
|
| 1324 |
+
vars := ic.InlVars
|
| 1325 |
+
slices.SortFunc(vars, byChildIndexCmp)
|
| 1326 |
+
inlIndex := ic.InlIndex
|
| 1327 |
+
var encbuf [20]byte
|
| 1328 |
+
for _, v := range vars {
|
| 1329 |
+
if !v.IsInAbstract {
|
| 1330 |
+
continue
|
| 1331 |
+
}
|
| 1332 |
+
putvar(ctxt, s, v, callee, abbrev, inlIndex, encbuf[:0])
|
| 1333 |
+
}
|
| 1334 |
+
|
| 1335 |
+
// Children of this inline.
|
| 1336 |
+
for _, sib := range inlChildren(callIdx, &s.InlCalls) {
|
| 1337 |
+
err := putInlinedFunc(ctxt, s, sib)
|
| 1338 |
+
if err != nil {
|
| 1339 |
+
return err
|
| 1340 |
+
}
|
| 1341 |
+
}
|
| 1342 |
+
|
| 1343 |
+
Uleb128put(ctxt, s.Info, 0)
|
| 1344 |
+
return nil
|
| 1345 |
+
}
|
| 1346 |
+
|
| 1347 |
+
func emitHiLoPc(ctxt Context, abbrev int, fns *FnState, st int64, en int64) {
|
| 1348 |
+
if buildcfg.Experiment.Dwarf5 {
|
| 1349 |
+
putattr(ctxt, fns.Info, abbrev, DW_FORM_addrx, DW_CLS_CONSTANT, st, fns.StartPC)
|
| 1350 |
+
putattr(ctxt, fns.Info, abbrev, DW_FORM_udata, DW_CLS_CONSTANT, en, 0)
|
| 1351 |
+
} else {
|
| 1352 |
+
putattr(ctxt, fns.Info, abbrev, DW_FORM_addr, DW_CLS_ADDRESS, st, fns.StartPC)
|
| 1353 |
+
putattr(ctxt, fns.Info, abbrev, DW_FORM_addr, DW_CLS_ADDRESS, en, fns.StartPC)
|
| 1354 |
+
}
|
| 1355 |
+
}
|
| 1356 |
+
|
| 1357 |
+
// Emit DWARF attributes and child DIEs for a 'concrete' subprogram,
|
| 1358 |
+
// meaning the out-of-line copy of a function that was inlined at some
|
| 1359 |
+
// point during the compilation of its containing package. The first
|
| 1360 |
+
// attribute for a concrete DIE is a reference to the 'abstract' DIE
|
| 1361 |
+
// for the function (which holds location-independent attributes such
|
| 1362 |
+
// as name, type), then the remainder of the attributes are specific
|
| 1363 |
+
// to this instance (location, frame base, etc).
|
| 1364 |
+
func PutConcreteFunc(ctxt Context, s *FnState, isWrapper bool, fncount int) error {
|
| 1365 |
+
if logDwarf {
|
| 1366 |
+
ctxt.Logf("PutConcreteFunc(%v)\n", s.Info)
|
| 1367 |
+
}
|
| 1368 |
+
abbrev := DW_ABRV_FUNCTION_CONCRETE
|
| 1369 |
+
if isWrapper {
|
| 1370 |
+
abbrev = DW_ABRV_WRAPPER_CONCRETE
|
| 1371 |
+
}
|
| 1372 |
+
Uleb128put(ctxt, s.Info, int64(abbrev))
|
| 1373 |
+
|
| 1374 |
+
// Abstract origin.
|
| 1375 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, s.Absfn)
|
| 1376 |
+
|
| 1377 |
+
// Start/end PC.
|
| 1378 |
+
emitHiLoPc(ctxt, abbrev, s, 0, s.Size)
|
| 1379 |
+
|
| 1380 |
+
// cfa / frame base
|
| 1381 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_block1, DW_CLS_BLOCK, 1, []byte{DW_OP_call_frame_cfa})
|
| 1382 |
+
|
| 1383 |
+
if isWrapper {
|
| 1384 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_flag, DW_CLS_FLAG, int64(1), 0)
|
| 1385 |
+
}
|
| 1386 |
+
|
| 1387 |
+
// Scopes
|
| 1388 |
+
if err := putPrunedScopes(ctxt, s, abbrev); err != nil {
|
| 1389 |
+
return err
|
| 1390 |
+
}
|
| 1391 |
+
|
| 1392 |
+
// Inlined subroutines.
|
| 1393 |
+
for _, sib := range inlChildren(-1, &s.InlCalls) {
|
| 1394 |
+
err := putInlinedFunc(ctxt, s, sib)
|
| 1395 |
+
if err != nil {
|
| 1396 |
+
return err
|
| 1397 |
+
}
|
| 1398 |
+
}
|
| 1399 |
+
|
| 1400 |
+
Uleb128put(ctxt, s.Info, 0)
|
| 1401 |
+
return nil
|
| 1402 |
+
}
|
| 1403 |
+
|
| 1404 |
+
// Emit DWARF attributes and child DIEs for a subprogram. Here
|
| 1405 |
+
// 'default' implies that the function in question was not inlined
|
| 1406 |
+
// when its containing package was compiled (hence there is no need to
|
| 1407 |
+
// emit an abstract version for it to use as a base for inlined
|
| 1408 |
+
// routine records).
|
| 1409 |
+
func PutDefaultFunc(ctxt Context, s *FnState, isWrapper bool) error {
|
| 1410 |
+
if logDwarf {
|
| 1411 |
+
ctxt.Logf("PutDefaultFunc(%v)\n", s.Info)
|
| 1412 |
+
}
|
| 1413 |
+
abbrev := DW_ABRV_FUNCTION
|
| 1414 |
+
if isWrapper {
|
| 1415 |
+
abbrev = DW_ABRV_WRAPPER
|
| 1416 |
+
}
|
| 1417 |
+
Uleb128put(ctxt, s.Info, int64(abbrev))
|
| 1418 |
+
|
| 1419 |
+
name := s.Name
|
| 1420 |
+
if strings.HasPrefix(name, `"".`) {
|
| 1421 |
+
return fmt.Errorf("unqualified symbol name: %v", name)
|
| 1422 |
+
}
|
| 1423 |
+
|
| 1424 |
+
putattr(ctxt, s.Info, DW_ABRV_FUNCTION, DW_FORM_string, DW_CLS_STRING, int64(len(name)), name)
|
| 1425 |
+
emitHiLoPc(ctxt, abbrev, s, 0, s.Size)
|
| 1426 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_block1, DW_CLS_BLOCK, 1, []byte{DW_OP_call_frame_cfa})
|
| 1427 |
+
if isWrapper {
|
| 1428 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_flag, DW_CLS_FLAG, int64(1), 0)
|
| 1429 |
+
} else {
|
| 1430 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_data4, DW_CLS_CONSTANT, dwarfFileIndex(s.StartPos), nil)
|
| 1431 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_udata, DW_CLS_CONSTANT, int64(s.StartPos.RelLine()), nil)
|
| 1432 |
+
|
| 1433 |
+
var ev int64
|
| 1434 |
+
if s.External {
|
| 1435 |
+
ev = 1
|
| 1436 |
+
}
|
| 1437 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_flag, DW_CLS_FLAG, ev, 0)
|
| 1438 |
+
}
|
| 1439 |
+
|
| 1440 |
+
// Scopes
|
| 1441 |
+
if err := putPrunedScopes(ctxt, s, abbrev); err != nil {
|
| 1442 |
+
return err
|
| 1443 |
+
}
|
| 1444 |
+
|
| 1445 |
+
// Inlined subroutines.
|
| 1446 |
+
for _, sib := range inlChildren(-1, &s.InlCalls) {
|
| 1447 |
+
err := putInlinedFunc(ctxt, s, sib)
|
| 1448 |
+
if err != nil {
|
| 1449 |
+
return err
|
| 1450 |
+
}
|
| 1451 |
+
}
|
| 1452 |
+
|
| 1453 |
+
Uleb128put(ctxt, s.Info, 0)
|
| 1454 |
+
return nil
|
| 1455 |
+
}
|
| 1456 |
+
|
| 1457 |
+
// putparamtypes writes typedef DIEs for any parametric types that are used by this function.
|
| 1458 |
+
func putparamtypes(ctxt Context, s *FnState, scopes []Scope, fnabbrev int) []int64 {
|
| 1459 |
+
if fnabbrev == DW_ABRV_FUNCTION_CONCRETE {
|
| 1460 |
+
return nil
|
| 1461 |
+
}
|
| 1462 |
+
|
| 1463 |
+
maxDictIndex := uint16(0)
|
| 1464 |
+
|
| 1465 |
+
for i := range scopes {
|
| 1466 |
+
for _, v := range scopes[i].Vars {
|
| 1467 |
+
if v.DictIndex > maxDictIndex {
|
| 1468 |
+
maxDictIndex = v.DictIndex
|
| 1469 |
+
}
|
| 1470 |
+
}
|
| 1471 |
+
}
|
| 1472 |
+
|
| 1473 |
+
if maxDictIndex == 0 {
|
| 1474 |
+
return nil
|
| 1475 |
+
}
|
| 1476 |
+
|
| 1477 |
+
dictIndexToOffset := make([]int64, maxDictIndex)
|
| 1478 |
+
|
| 1479 |
+
for i := range scopes {
|
| 1480 |
+
for _, v := range scopes[i].Vars {
|
| 1481 |
+
if v.DictIndex == 0 || dictIndexToOffset[v.DictIndex-1] != 0 {
|
| 1482 |
+
continue
|
| 1483 |
+
}
|
| 1484 |
+
|
| 1485 |
+
dictIndexToOffset[v.DictIndex-1] = ctxt.CurrentOffset(s.Info)
|
| 1486 |
+
|
| 1487 |
+
Uleb128put(ctxt, s.Info, int64(DW_ABRV_DICT_INDEX))
|
| 1488 |
+
n := fmt.Sprintf(".param%d", v.DictIndex-1)
|
| 1489 |
+
putattr(ctxt, s.Info, DW_ABRV_DICT_INDEX, DW_FORM_string, DW_CLS_STRING, int64(len(n)), n)
|
| 1490 |
+
putattr(ctxt, s.Info, DW_ABRV_DICT_INDEX, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, v.Type)
|
| 1491 |
+
putattr(ctxt, s.Info, DW_ABRV_DICT_INDEX, DW_FORM_udata, DW_CLS_CONSTANT, int64(v.DictIndex-1), nil)
|
| 1492 |
+
}
|
| 1493 |
+
}
|
| 1494 |
+
|
| 1495 |
+
return dictIndexToOffset
|
| 1496 |
+
}
|
| 1497 |
+
|
| 1498 |
+
func putscope(ctxt Context, s *FnState, scopes []Scope, curscope int32, fnabbrev int, encbuf []byte) int32 {
|
| 1499 |
+
|
| 1500 |
+
if logDwarf {
|
| 1501 |
+
ctxt.Logf("putscope(%v,%d): vars:", s.Info, curscope)
|
| 1502 |
+
for i, v := range scopes[curscope].Vars {
|
| 1503 |
+
ctxt.Logf(" %d:%d:%s", i, v.ChildIndex, v.Name)
|
| 1504 |
+
}
|
| 1505 |
+
ctxt.Logf("\n")
|
| 1506 |
+
}
|
| 1507 |
+
|
| 1508 |
+
for _, v := range scopes[curscope].Vars {
|
| 1509 |
+
putvar(ctxt, s, v, s.Absfn, fnabbrev, -1, encbuf)
|
| 1510 |
+
}
|
| 1511 |
+
this := curscope
|
| 1512 |
+
curscope++
|
| 1513 |
+
for curscope < int32(len(scopes)) {
|
| 1514 |
+
scope := scopes[curscope]
|
| 1515 |
+
if scope.Parent != this {
|
| 1516 |
+
return curscope
|
| 1517 |
+
}
|
| 1518 |
+
|
| 1519 |
+
if len(scopes[curscope].Vars) == 0 {
|
| 1520 |
+
curscope = putscope(ctxt, s, scopes, curscope, fnabbrev, encbuf)
|
| 1521 |
+
continue
|
| 1522 |
+
}
|
| 1523 |
+
|
| 1524 |
+
// For DWARF 5, we always use the ranges form of the abbrev, since
|
| 1525 |
+
// it is more compact than using explicit hi/lo PC attrs. See
|
| 1526 |
+
// issue #72821 for more on why this makes sense.
|
| 1527 |
+
if len(scope.Ranges) == 1 && !buildcfg.Experiment.Dwarf5 {
|
| 1528 |
+
Uleb128put(ctxt, s.Info, DW_ABRV_LEXICAL_BLOCK_SIMPLE)
|
| 1529 |
+
putattr(ctxt, s.Info, DW_ABRV_LEXICAL_BLOCK_SIMPLE, DW_FORM_addr, DW_CLS_ADDRESS, scope.Ranges[0].Start, s.StartPC)
|
| 1530 |
+
putattr(ctxt, s.Info, DW_ABRV_LEXICAL_BLOCK_SIMPLE, DW_FORM_addr, DW_CLS_ADDRESS, scope.Ranges[0].End, s.StartPC)
|
| 1531 |
+
} else {
|
| 1532 |
+
Uleb128put(ctxt, s.Info, DW_ABRV_LEXICAL_BLOCK_RANGES)
|
| 1533 |
+
putattr(ctxt, s.Info, DW_ABRV_LEXICAL_BLOCK_RANGES, DW_FORM_sec_offset, DW_CLS_PTR, ctxt.Size(s.Ranges), s.Ranges)
|
| 1534 |
+
|
| 1535 |
+
s.PutRanges(ctxt, scope.Ranges)
|
| 1536 |
+
}
|
| 1537 |
+
|
| 1538 |
+
curscope = putscope(ctxt, s, scopes, curscope, fnabbrev, encbuf)
|
| 1539 |
+
|
| 1540 |
+
Uleb128put(ctxt, s.Info, 0)
|
| 1541 |
+
}
|
| 1542 |
+
return curscope
|
| 1543 |
+
}
|
| 1544 |
+
|
| 1545 |
+
func concreteVar(fnabbrev int, v *Var) bool {
|
| 1546 |
+
concrete := true
|
| 1547 |
+
switch fnabbrev {
|
| 1548 |
+
case DW_ABRV_FUNCTION, DW_ABRV_WRAPPER:
|
| 1549 |
+
concrete = false
|
| 1550 |
+
case DW_ABRV_FUNCTION_CONCRETE, DW_ABRV_WRAPPER_CONCRETE:
|
| 1551 |
+
// If we're emitting a concrete subprogram DIE and the variable
|
| 1552 |
+
// in question is not part of the corresponding abstract function DIE,
|
| 1553 |
+
// then use the default (non-concrete) abbrev for this param.
|
| 1554 |
+
if !v.IsInAbstract {
|
| 1555 |
+
concrete = false
|
| 1556 |
+
}
|
| 1557 |
+
case DW_ABRV_INLINED_SUBROUTINE, DW_ABRV_INLINED_SUBROUTINE_RANGES:
|
| 1558 |
+
default:
|
| 1559 |
+
panic("should never happen")
|
| 1560 |
+
}
|
| 1561 |
+
return concrete
|
| 1562 |
+
}
|
| 1563 |
+
|
| 1564 |
+
// Emit DWARF attributes for a variable belonging to an 'abstract' subprogram.
|
| 1565 |
+
func putAbstractVar(ctxt Context, info Sym, v *Var) {
|
| 1566 |
+
// The contents of this functions are used to generate putAbstractVarAbbrev automatically, see TestPutVarAbbrevGenerator.
|
| 1567 |
+
abbrev := putAbstractVarAbbrev(v)
|
| 1568 |
+
Uleb128put(ctxt, info, int64(abbrev))
|
| 1569 |
+
putattr(ctxt, info, abbrev, DW_FORM_string, DW_CLS_STRING, int64(len(v.Name)), v.Name) // DW_AT_name
|
| 1570 |
+
|
| 1571 |
+
// Isreturn attribute if this is a param
|
| 1572 |
+
if v.Tag == DW_TAG_formal_parameter {
|
| 1573 |
+
var isReturn int64
|
| 1574 |
+
if v.IsReturnValue {
|
| 1575 |
+
isReturn = 1
|
| 1576 |
+
}
|
| 1577 |
+
putattr(ctxt, info, abbrev, DW_FORM_flag, DW_CLS_FLAG, isReturn, nil) // DW_AT_variable_parameter
|
| 1578 |
+
}
|
| 1579 |
+
|
| 1580 |
+
// Line
|
| 1581 |
+
if v.Tag == DW_TAG_variable {
|
| 1582 |
+
// See issue 23374 for more on why decl line is skipped for abs params.
|
| 1583 |
+
putattr(ctxt, info, abbrev, DW_FORM_udata, DW_CLS_CONSTANT, int64(v.DeclLine), nil) // DW_AT_decl_line
|
| 1584 |
+
}
|
| 1585 |
+
|
| 1586 |
+
// Type
|
| 1587 |
+
putattr(ctxt, info, abbrev, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, v.Type) // DW_AT_type
|
| 1588 |
+
|
| 1589 |
+
// Var has no children => no terminator
|
| 1590 |
+
}
|
| 1591 |
+
|
| 1592 |
+
func putvar(ctxt Context, s *FnState, v *Var, absfn Sym, fnabbrev, inlIndex int, encbuf []byte) {
|
| 1593 |
+
// The contents of this functions are used to generate putvarAbbrev automatically, see TestPutVarAbbrevGenerator.
|
| 1594 |
+
concrete := concreteVar(fnabbrev, v)
|
| 1595 |
+
hasParametricType := !concrete && (v.DictIndex > 0 && s.dictIndexToOffset != nil && s.dictIndexToOffset[v.DictIndex-1] != 0)
|
| 1596 |
+
withLoclist := v.WithLoclist && v.PutLocationList != nil
|
| 1597 |
+
|
| 1598 |
+
abbrev := putvarAbbrev(v, concrete, withLoclist)
|
| 1599 |
+
Uleb128put(ctxt, s.Info, int64(abbrev))
|
| 1600 |
+
|
| 1601 |
+
// Abstract origin for concrete / inlined case
|
| 1602 |
+
if concrete {
|
| 1603 |
+
// Here we are making a reference to a child DIE of an abstract
|
| 1604 |
+
// function subprogram DIE. The child DIE has no LSym, so instead
|
| 1605 |
+
// after the call to 'putattr' below we make a call to register
|
| 1606 |
+
// the child DIE reference.
|
| 1607 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, absfn) // DW_AT_abstract_origin
|
| 1608 |
+
ctxt.RecordDclReference(s.Info, absfn, int(v.ChildIndex), inlIndex)
|
| 1609 |
+
} else {
|
| 1610 |
+
// Var name, line for abstract and default cases
|
| 1611 |
+
n := v.Name
|
| 1612 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_string, DW_CLS_STRING, int64(len(n)), n) // DW_AT_name
|
| 1613 |
+
if v.Tag == DW_TAG_formal_parameter {
|
| 1614 |
+
var isReturn int64
|
| 1615 |
+
if v.IsReturnValue {
|
| 1616 |
+
isReturn = 1
|
| 1617 |
+
}
|
| 1618 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_flag, DW_CLS_FLAG, isReturn, nil) // DW_AT_variable_parameter
|
| 1619 |
+
}
|
| 1620 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_udata, DW_CLS_CONSTANT, int64(v.DeclLine), nil) // DW_AT_decl_line
|
| 1621 |
+
if hasParametricType {
|
| 1622 |
+
// If the type of this variable is parametric use the entry emitted by putparamtypes
|
| 1623 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_ref_addr, DW_CLS_REFERENCE, s.dictIndexToOffset[v.DictIndex-1], s.Info) // DW_AT_type
|
| 1624 |
+
} else {
|
| 1625 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_ref_addr, DW_CLS_REFERENCE, 0, v.Type) // DW_AT_type
|
| 1626 |
+
}
|
| 1627 |
+
|
| 1628 |
+
if v.ClosureOffset > 0 {
|
| 1629 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_udata, DW_CLS_CONSTANT, v.ClosureOffset, nil) // DW_AT_go_closure_offset
|
| 1630 |
+
}
|
| 1631 |
+
}
|
| 1632 |
+
|
| 1633 |
+
if withLoclist {
|
| 1634 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_sec_offset, DW_CLS_PTR, ctxt.Size(s.Loc), s.Loc) // DW_AT_location
|
| 1635 |
+
v.PutLocationList(s.Loc, s.StartPC)
|
| 1636 |
+
} else {
|
| 1637 |
+
loc := encbuf[:0]
|
| 1638 |
+
switch {
|
| 1639 |
+
case v.WithLoclist:
|
| 1640 |
+
break // no location
|
| 1641 |
+
case v.StackOffset == 0:
|
| 1642 |
+
loc = append(loc, DW_OP_call_frame_cfa)
|
| 1643 |
+
default:
|
| 1644 |
+
loc = append(loc, DW_OP_fbreg)
|
| 1645 |
+
loc = AppendSleb128(loc, int64(v.StackOffset))
|
| 1646 |
+
}
|
| 1647 |
+
putattr(ctxt, s.Info, abbrev, DW_FORM_block1, DW_CLS_BLOCK, int64(len(loc)), loc) // DW_AT_location
|
| 1648 |
+
}
|
| 1649 |
+
|
| 1650 |
+
// Var has no children => no terminator
|
| 1651 |
+
}
|
| 1652 |
+
|
| 1653 |
+
// byChildIndexCmp compares two *dwarf.Var by child index.
|
| 1654 |
+
func byChildIndexCmp(a, b *Var) int { return cmp.Compare(a.ChildIndex, b.ChildIndex) }
|
| 1655 |
+
|
| 1656 |
+
// IsDWARFEnabledOnAIXLd returns true if DWARF is possible on the
|
| 1657 |
+
// current extld.
|
| 1658 |
+
// AIX ld doesn't support DWARF with -bnoobjreorder with version
|
| 1659 |
+
// prior to 7.2.2.
|
| 1660 |
+
func IsDWARFEnabledOnAIXLd(extld []string) (bool, error) {
|
| 1661 |
+
name, args := extld[0], extld[1:]
|
| 1662 |
+
args = append(args, "-Wl,-V")
|
| 1663 |
+
out, err := exec.Command(name, args...).CombinedOutput()
|
| 1664 |
+
if err != nil {
|
| 1665 |
+
// The normal output should display ld version and
|
| 1666 |
+
// then fails because ".main" is not defined:
|
| 1667 |
+
// ld: 0711-317 ERROR: Undefined symbol: .main
|
| 1668 |
+
if !bytes.Contains(out, []byte("0711-317")) {
|
| 1669 |
+
return false, fmt.Errorf("%s -Wl,-V failed: %v\n%s", extld, err, out)
|
| 1670 |
+
}
|
| 1671 |
+
}
|
| 1672 |
+
// gcc -Wl,-V output should be:
|
| 1673 |
+
// /usr/bin/ld: LD X.X.X(date)
|
| 1674 |
+
// ...
|
| 1675 |
+
out = bytes.TrimPrefix(out, []byte("/usr/bin/ld: LD "))
|
| 1676 |
+
vers := string(bytes.Split(out, []byte("("))[0])
|
| 1677 |
+
subvers := strings.Split(vers, ".")
|
| 1678 |
+
if len(subvers) != 3 {
|
| 1679 |
+
return false, fmt.Errorf("cannot parse %s -Wl,-V (%s): %v\n", extld, out, err)
|
| 1680 |
+
}
|
| 1681 |
+
if v, err := strconv.Atoi(subvers[0]); err != nil || v < 7 {
|
| 1682 |
+
return false, nil
|
| 1683 |
+
} else if v > 7 {
|
| 1684 |
+
return true, nil
|
| 1685 |
+
}
|
| 1686 |
+
if v, err := strconv.Atoi(subvers[1]); err != nil || v < 2 {
|
| 1687 |
+
return false, nil
|
| 1688 |
+
} else if v > 2 {
|
| 1689 |
+
return true, nil
|
| 1690 |
+
}
|
| 1691 |
+
if v, err := strconv.Atoi(subvers[2]); err != nil || v < 2 {
|
| 1692 |
+
return false, nil
|
| 1693 |
+
}
|
| 1694 |
+
return true, nil
|
| 1695 |
+
}
|
go/src/cmd/internal/dwarf/dwarf_defs.go
ADDED
|
@@ -0,0 +1,550 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2010 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 dwarf
|
| 6 |
+
|
| 7 |
+
// Cut, pasted, tr-and-awk'ed from tables in
|
| 8 |
+
// http://dwarfstd.org/doc/Dwarf3.pdf
|
| 9 |
+
|
| 10 |
+
// Table 18
|
| 11 |
+
const (
|
| 12 |
+
DW_TAG_array_type = 0x01
|
| 13 |
+
DW_TAG_class_type = 0x02
|
| 14 |
+
DW_TAG_entry_point = 0x03
|
| 15 |
+
DW_TAG_enumeration_type = 0x04
|
| 16 |
+
DW_TAG_formal_parameter = 0x05
|
| 17 |
+
DW_TAG_imported_declaration = 0x08
|
| 18 |
+
DW_TAG_label = 0x0a
|
| 19 |
+
DW_TAG_lexical_block = 0x0b
|
| 20 |
+
DW_TAG_member = 0x0d
|
| 21 |
+
DW_TAG_pointer_type = 0x0f
|
| 22 |
+
DW_TAG_reference_type = 0x10
|
| 23 |
+
DW_TAG_compile_unit = 0x11
|
| 24 |
+
DW_TAG_string_type = 0x12
|
| 25 |
+
DW_TAG_structure_type = 0x13
|
| 26 |
+
DW_TAG_subroutine_type = 0x15
|
| 27 |
+
DW_TAG_typedef = 0x16
|
| 28 |
+
DW_TAG_union_type = 0x17
|
| 29 |
+
DW_TAG_unspecified_parameters = 0x18
|
| 30 |
+
DW_TAG_variant = 0x19
|
| 31 |
+
DW_TAG_common_block = 0x1a
|
| 32 |
+
DW_TAG_common_inclusion = 0x1b
|
| 33 |
+
DW_TAG_inheritance = 0x1c
|
| 34 |
+
DW_TAG_inlined_subroutine = 0x1d
|
| 35 |
+
DW_TAG_module = 0x1e
|
| 36 |
+
DW_TAG_ptr_to_member_type = 0x1f
|
| 37 |
+
DW_TAG_set_type = 0x20
|
| 38 |
+
DW_TAG_subrange_type = 0x21
|
| 39 |
+
DW_TAG_with_stmt = 0x22
|
| 40 |
+
DW_TAG_access_declaration = 0x23
|
| 41 |
+
DW_TAG_base_type = 0x24
|
| 42 |
+
DW_TAG_catch_block = 0x25
|
| 43 |
+
DW_TAG_const_type = 0x26
|
| 44 |
+
DW_TAG_constant = 0x27
|
| 45 |
+
DW_TAG_enumerator = 0x28
|
| 46 |
+
DW_TAG_file_type = 0x29
|
| 47 |
+
DW_TAG_friend = 0x2a
|
| 48 |
+
DW_TAG_namelist = 0x2b
|
| 49 |
+
DW_TAG_namelist_item = 0x2c
|
| 50 |
+
DW_TAG_packed_type = 0x2d
|
| 51 |
+
DW_TAG_subprogram = 0x2e
|
| 52 |
+
DW_TAG_template_type_parameter = 0x2f
|
| 53 |
+
DW_TAG_template_value_parameter = 0x30
|
| 54 |
+
DW_TAG_thrown_type = 0x31
|
| 55 |
+
DW_TAG_try_block = 0x32
|
| 56 |
+
DW_TAG_variant_part = 0x33
|
| 57 |
+
DW_TAG_variable = 0x34
|
| 58 |
+
DW_TAG_volatile_type = 0x35
|
| 59 |
+
// Dwarf3
|
| 60 |
+
DW_TAG_dwarf_procedure = 0x36
|
| 61 |
+
DW_TAG_restrict_type = 0x37
|
| 62 |
+
DW_TAG_interface_type = 0x38
|
| 63 |
+
DW_TAG_namespace = 0x39
|
| 64 |
+
DW_TAG_imported_module = 0x3a
|
| 65 |
+
DW_TAG_unspecified_type = 0x3b
|
| 66 |
+
DW_TAG_partial_unit = 0x3c
|
| 67 |
+
DW_TAG_imported_unit = 0x3d
|
| 68 |
+
DW_TAG_condition = 0x3f
|
| 69 |
+
DW_TAG_shared_type = 0x40
|
| 70 |
+
// Dwarf4
|
| 71 |
+
DW_TAG_type_unit = 0x41
|
| 72 |
+
DW_TAG_rvalue_reference_type = 0x42
|
| 73 |
+
DW_TAG_template_alias = 0x43
|
| 74 |
+
|
| 75 |
+
// User defined
|
| 76 |
+
DW_TAG_lo_user = 0x4080
|
| 77 |
+
DW_TAG_hi_user = 0xffff
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
// Table 19
|
| 81 |
+
const (
|
| 82 |
+
DW_CHILDREN_no = 0x00
|
| 83 |
+
DW_CHILDREN_yes = 0x01
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
// Not from the spec, but logically belongs here
|
| 87 |
+
const (
|
| 88 |
+
DW_CLS_ADDRESS = 0x01 + iota
|
| 89 |
+
DW_CLS_BLOCK
|
| 90 |
+
DW_CLS_CONSTANT
|
| 91 |
+
DW_CLS_FLAG
|
| 92 |
+
DW_CLS_PTR // lineptr, loclistptr, macptr, rangelistptr
|
| 93 |
+
DW_CLS_REFERENCE
|
| 94 |
+
DW_CLS_ADDRLOC
|
| 95 |
+
DW_CLS_STRING
|
| 96 |
+
|
| 97 |
+
// Go-specific internal hackery.
|
| 98 |
+
DW_CLS_GO_TYPEREF
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
// Table 20
|
| 102 |
+
const (
|
| 103 |
+
DW_AT_sibling = 0x01 // reference
|
| 104 |
+
DW_AT_location = 0x02 // block, loclistptr
|
| 105 |
+
DW_AT_name = 0x03 // string
|
| 106 |
+
DW_AT_ordering = 0x09 // constant
|
| 107 |
+
DW_AT_byte_size = 0x0b // block, constant, reference
|
| 108 |
+
DW_AT_bit_offset = 0x0c // block, constant, reference
|
| 109 |
+
DW_AT_bit_size = 0x0d // block, constant, reference
|
| 110 |
+
DW_AT_stmt_list = 0x10 // lineptr
|
| 111 |
+
DW_AT_low_pc = 0x11 // address
|
| 112 |
+
DW_AT_high_pc = 0x12 // address
|
| 113 |
+
DW_AT_language = 0x13 // constant
|
| 114 |
+
DW_AT_discr = 0x15 // reference
|
| 115 |
+
DW_AT_discr_value = 0x16 // constant
|
| 116 |
+
DW_AT_visibility = 0x17 // constant
|
| 117 |
+
DW_AT_import = 0x18 // reference
|
| 118 |
+
DW_AT_string_length = 0x19 // block, loclistptr
|
| 119 |
+
DW_AT_common_reference = 0x1a // reference
|
| 120 |
+
DW_AT_comp_dir = 0x1b // string
|
| 121 |
+
DW_AT_const_value = 0x1c // block, constant, string
|
| 122 |
+
DW_AT_containing_type = 0x1d // reference
|
| 123 |
+
DW_AT_default_value = 0x1e // reference
|
| 124 |
+
DW_AT_inline = 0x20 // constant
|
| 125 |
+
DW_AT_is_optional = 0x21 // flag
|
| 126 |
+
DW_AT_lower_bound = 0x22 // block, constant, reference
|
| 127 |
+
DW_AT_producer = 0x25 // string
|
| 128 |
+
DW_AT_prototyped = 0x27 // flag
|
| 129 |
+
DW_AT_return_addr = 0x2a // block, loclistptr
|
| 130 |
+
DW_AT_start_scope = 0x2c // constant
|
| 131 |
+
DW_AT_bit_stride = 0x2e // constant
|
| 132 |
+
DW_AT_upper_bound = 0x2f // block, constant, reference
|
| 133 |
+
DW_AT_abstract_origin = 0x31 // reference
|
| 134 |
+
DW_AT_accessibility = 0x32 // constant
|
| 135 |
+
DW_AT_address_class = 0x33 // constant
|
| 136 |
+
DW_AT_artificial = 0x34 // flag
|
| 137 |
+
DW_AT_base_types = 0x35 // reference
|
| 138 |
+
DW_AT_calling_convention = 0x36 // constant
|
| 139 |
+
DW_AT_count = 0x37 // block, constant, reference
|
| 140 |
+
DW_AT_data_member_location = 0x38 // block, constant, loclistptr
|
| 141 |
+
DW_AT_decl_column = 0x39 // constant
|
| 142 |
+
DW_AT_decl_file = 0x3a // constant
|
| 143 |
+
DW_AT_decl_line = 0x3b // constant
|
| 144 |
+
DW_AT_declaration = 0x3c // flag
|
| 145 |
+
DW_AT_discr_list = 0x3d // block
|
| 146 |
+
DW_AT_encoding = 0x3e // constant
|
| 147 |
+
DW_AT_external = 0x3f // flag
|
| 148 |
+
DW_AT_frame_base = 0x40 // block, loclistptr
|
| 149 |
+
DW_AT_friend = 0x41 // reference
|
| 150 |
+
DW_AT_identifier_case = 0x42 // constant
|
| 151 |
+
DW_AT_macro_info = 0x43 // macptr
|
| 152 |
+
DW_AT_namelist_item = 0x44 // block
|
| 153 |
+
DW_AT_priority = 0x45 // reference
|
| 154 |
+
DW_AT_segment = 0x46 // block, loclistptr
|
| 155 |
+
DW_AT_specification = 0x47 // reference
|
| 156 |
+
DW_AT_static_link = 0x48 // block, loclistptr
|
| 157 |
+
DW_AT_type = 0x49 // reference
|
| 158 |
+
DW_AT_use_location = 0x4a // block, loclistptr
|
| 159 |
+
DW_AT_variable_parameter = 0x4b // flag
|
| 160 |
+
DW_AT_virtuality = 0x4c // constant
|
| 161 |
+
DW_AT_vtable_elem_location = 0x4d // block, loclistptr
|
| 162 |
+
// Dwarf3
|
| 163 |
+
DW_AT_allocated = 0x4e // block, constant, reference
|
| 164 |
+
DW_AT_associated = 0x4f // block, constant, reference
|
| 165 |
+
DW_AT_data_location = 0x50 // block
|
| 166 |
+
DW_AT_byte_stride = 0x51 // block, constant, reference
|
| 167 |
+
DW_AT_entry_pc = 0x52 // address
|
| 168 |
+
DW_AT_use_UTF8 = 0x53 // flag
|
| 169 |
+
DW_AT_extension = 0x54 // reference
|
| 170 |
+
DW_AT_ranges = 0x55 // rangelistptr
|
| 171 |
+
DW_AT_trampoline = 0x56 // address, flag, reference, string
|
| 172 |
+
DW_AT_call_column = 0x57 // constant
|
| 173 |
+
DW_AT_call_file = 0x58 // constant
|
| 174 |
+
DW_AT_call_line = 0x59 // constant
|
| 175 |
+
DW_AT_description = 0x5a // string
|
| 176 |
+
DW_AT_binary_scale = 0x5b // constant
|
| 177 |
+
DW_AT_decimal_scale = 0x5c // constant
|
| 178 |
+
DW_AT_small = 0x5d // reference
|
| 179 |
+
DW_AT_decimal_sign = 0x5e // constant
|
| 180 |
+
DW_AT_digit_count = 0x5f // constant
|
| 181 |
+
DW_AT_picture_string = 0x60 // string
|
| 182 |
+
DW_AT_mutable = 0x61 // flag
|
| 183 |
+
DW_AT_threads_scaled = 0x62 // flag
|
| 184 |
+
DW_AT_explicit = 0x63 // flag
|
| 185 |
+
DW_AT_object_pointer = 0x64 // reference
|
| 186 |
+
DW_AT_endianity = 0x65 // constant
|
| 187 |
+
DW_AT_elemental = 0x66 // flag
|
| 188 |
+
DW_AT_pure = 0x67 // flag
|
| 189 |
+
DW_AT_recursive = 0x68 // flag
|
| 190 |
+
DW_AT_addr_base = 0x73 // addrptr
|
| 191 |
+
|
| 192 |
+
DW_AT_lo_user = 0x2000 // ---
|
| 193 |
+
DW_AT_hi_user = 0x3fff // ---
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
// Table 21
|
| 197 |
+
const (
|
| 198 |
+
DW_FORM_addr = 0x01 // address
|
| 199 |
+
DW_FORM_block2 = 0x03 // block
|
| 200 |
+
DW_FORM_block4 = 0x04 // block
|
| 201 |
+
DW_FORM_data2 = 0x05 // constant
|
| 202 |
+
DW_FORM_data4 = 0x06 // constant, lineptr, loclistptr, macptr, rangelistptr
|
| 203 |
+
DW_FORM_data8 = 0x07 // constant, lineptr, loclistptr, macptr, rangelistptr
|
| 204 |
+
DW_FORM_string = 0x08 // string
|
| 205 |
+
DW_FORM_block = 0x09 // block
|
| 206 |
+
DW_FORM_block1 = 0x0a // block
|
| 207 |
+
DW_FORM_data1 = 0x0b // constant
|
| 208 |
+
DW_FORM_flag = 0x0c // flag
|
| 209 |
+
DW_FORM_sdata = 0x0d // constant
|
| 210 |
+
DW_FORM_strp = 0x0e // string
|
| 211 |
+
DW_FORM_udata = 0x0f // constant
|
| 212 |
+
DW_FORM_ref_addr = 0x10 // reference
|
| 213 |
+
DW_FORM_ref1 = 0x11 // reference
|
| 214 |
+
DW_FORM_ref2 = 0x12 // reference
|
| 215 |
+
DW_FORM_ref4 = 0x13 // reference
|
| 216 |
+
DW_FORM_ref8 = 0x14 // reference
|
| 217 |
+
DW_FORM_ref_udata = 0x15 // reference
|
| 218 |
+
DW_FORM_indirect = 0x16 // (see Section 7.5.3)
|
| 219 |
+
// Dwarf4
|
| 220 |
+
DW_FORM_sec_offset = 0x17 // lineptr, loclistptr, macptr, rangelistptr
|
| 221 |
+
DW_FORM_exprloc = 0x18 // exprloc
|
| 222 |
+
DW_FORM_flag_present = 0x19 // flag
|
| 223 |
+
// Dwarf5
|
| 224 |
+
DW_FORM_addrx = 0x1b
|
| 225 |
+
// Pseudo-form: expanded to data4 on IOS, udata elsewhere.
|
| 226 |
+
DW_FORM_udata_pseudo = 0x99
|
| 227 |
+
// Pseudo-form: expands to DW_FORM_addrx in DWARF5, DW_FORM_addr in DWARF4
|
| 228 |
+
DW_FORM_lo_pc_pseudo = 0x9a
|
| 229 |
+
// Pseudo-form: expands to DW_FORM_udata in DWARF5, DW_FORM_addr in DWARF4
|
| 230 |
+
DW_FORM_hi_pc_pseudo = 0x9b
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
// Table 24 (#operands, notes)
|
| 234 |
+
const (
|
| 235 |
+
DW_OP_addr = 0x03 // 1 constant address (size target specific)
|
| 236 |
+
DW_OP_deref = 0x06 // 0
|
| 237 |
+
DW_OP_const1u = 0x08 // 1 1-byte constant
|
| 238 |
+
DW_OP_const1s = 0x09 // 1 1-byte constant
|
| 239 |
+
DW_OP_const2u = 0x0a // 1 2-byte constant
|
| 240 |
+
DW_OP_const2s = 0x0b // 1 2-byte constant
|
| 241 |
+
DW_OP_const4u = 0x0c // 1 4-byte constant
|
| 242 |
+
DW_OP_const4s = 0x0d // 1 4-byte constant
|
| 243 |
+
DW_OP_const8u = 0x0e // 1 8-byte constant
|
| 244 |
+
DW_OP_const8s = 0x0f // 1 8-byte constant
|
| 245 |
+
DW_OP_constu = 0x10 // 1 ULEB128 constant
|
| 246 |
+
DW_OP_consts = 0x11 // 1 SLEB128 constant
|
| 247 |
+
DW_OP_dup = 0x12 // 0
|
| 248 |
+
DW_OP_drop = 0x13 // 0
|
| 249 |
+
DW_OP_over = 0x14 // 0
|
| 250 |
+
DW_OP_pick = 0x15 // 1 1-byte stack index
|
| 251 |
+
DW_OP_swap = 0x16 // 0
|
| 252 |
+
DW_OP_rot = 0x17 // 0
|
| 253 |
+
DW_OP_xderef = 0x18 // 0
|
| 254 |
+
DW_OP_abs = 0x19 // 0
|
| 255 |
+
DW_OP_and = 0x1a // 0
|
| 256 |
+
DW_OP_div = 0x1b // 0
|
| 257 |
+
DW_OP_minus = 0x1c // 0
|
| 258 |
+
DW_OP_mod = 0x1d // 0
|
| 259 |
+
DW_OP_mul = 0x1e // 0
|
| 260 |
+
DW_OP_neg = 0x1f // 0
|
| 261 |
+
DW_OP_not = 0x20 // 0
|
| 262 |
+
DW_OP_or = 0x21 // 0
|
| 263 |
+
DW_OP_plus = 0x22 // 0
|
| 264 |
+
DW_OP_plus_uconst = 0x23 // 1 ULEB128 addend
|
| 265 |
+
DW_OP_shl = 0x24 // 0
|
| 266 |
+
DW_OP_shr = 0x25 // 0
|
| 267 |
+
DW_OP_shra = 0x26 // 0
|
| 268 |
+
DW_OP_xor = 0x27 // 0
|
| 269 |
+
DW_OP_skip = 0x2f // 1 signed 2-byte constant
|
| 270 |
+
DW_OP_bra = 0x28 // 1 signed 2-byte constant
|
| 271 |
+
DW_OP_eq = 0x29 // 0
|
| 272 |
+
DW_OP_ge = 0x2a // 0
|
| 273 |
+
DW_OP_gt = 0x2b // 0
|
| 274 |
+
DW_OP_le = 0x2c // 0
|
| 275 |
+
DW_OP_lt = 0x2d // 0
|
| 276 |
+
DW_OP_ne = 0x2e // 0
|
| 277 |
+
DW_OP_lit0 = 0x30 // 0 ...
|
| 278 |
+
DW_OP_lit31 = 0x4f // 0 literals 0..31 = (DW_OP_lit0 + literal)
|
| 279 |
+
DW_OP_reg0 = 0x50 // 0 ..
|
| 280 |
+
DW_OP_reg31 = 0x6f // 0 reg 0..31 = (DW_OP_reg0 + regnum)
|
| 281 |
+
DW_OP_breg0 = 0x70 // 1 ...
|
| 282 |
+
DW_OP_breg31 = 0x8f // 1 SLEB128 offset base register 0..31 = (DW_OP_breg0 + regnum)
|
| 283 |
+
DW_OP_regx = 0x90 // 1 ULEB128 register
|
| 284 |
+
DW_OP_fbreg = 0x91 // 1 SLEB128 offset
|
| 285 |
+
DW_OP_bregx = 0x92 // 2 ULEB128 register followed by SLEB128 offset
|
| 286 |
+
DW_OP_piece = 0x93 // 1 ULEB128 size of piece addressed
|
| 287 |
+
DW_OP_deref_size = 0x94 // 1 1-byte size of data retrieved
|
| 288 |
+
DW_OP_xderef_size = 0x95 // 1 1-byte size of data retrieved
|
| 289 |
+
DW_OP_nop = 0x96 // 0
|
| 290 |
+
DW_OP_push_object_address = 0x97 // 0
|
| 291 |
+
DW_OP_call2 = 0x98 // 1 2-byte offset of DIE
|
| 292 |
+
DW_OP_call4 = 0x99 // 1 4-byte offset of DIE
|
| 293 |
+
DW_OP_call_ref = 0x9a // 1 4- or 8-byte offset of DIE
|
| 294 |
+
DW_OP_form_tls_address = 0x9b // 0
|
| 295 |
+
DW_OP_call_frame_cfa = 0x9c // 0
|
| 296 |
+
DW_OP_bit_piece = 0x9d // 2
|
| 297 |
+
DW_OP_lo_user = 0xe0
|
| 298 |
+
DW_OP_hi_user = 0xff
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
// Table 25
|
| 302 |
+
const (
|
| 303 |
+
DW_ATE_address = 0x01
|
| 304 |
+
DW_ATE_boolean = 0x02
|
| 305 |
+
DW_ATE_complex_float = 0x03
|
| 306 |
+
DW_ATE_float = 0x04
|
| 307 |
+
DW_ATE_signed = 0x05
|
| 308 |
+
DW_ATE_signed_char = 0x06
|
| 309 |
+
DW_ATE_unsigned = 0x07
|
| 310 |
+
DW_ATE_unsigned_char = 0x08
|
| 311 |
+
DW_ATE_imaginary_float = 0x09
|
| 312 |
+
DW_ATE_packed_decimal = 0x0a
|
| 313 |
+
DW_ATE_numeric_string = 0x0b
|
| 314 |
+
DW_ATE_edited = 0x0c
|
| 315 |
+
DW_ATE_signed_fixed = 0x0d
|
| 316 |
+
DW_ATE_unsigned_fixed = 0x0e
|
| 317 |
+
DW_ATE_decimal_float = 0x0f
|
| 318 |
+
DW_ATE_lo_user = 0x80
|
| 319 |
+
DW_ATE_hi_user = 0xff
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
// Table 26
|
| 323 |
+
const (
|
| 324 |
+
DW_DS_unsigned = 0x01
|
| 325 |
+
DW_DS_leading_overpunch = 0x02
|
| 326 |
+
DW_DS_trailing_overpunch = 0x03
|
| 327 |
+
DW_DS_leading_separate = 0x04
|
| 328 |
+
DW_DS_trailing_separate = 0x05
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
// Table 27
|
| 332 |
+
const (
|
| 333 |
+
DW_END_default = 0x00
|
| 334 |
+
DW_END_big = 0x01
|
| 335 |
+
DW_END_little = 0x02
|
| 336 |
+
DW_END_lo_user = 0x40
|
| 337 |
+
DW_END_hi_user = 0xff
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
// Table 28
|
| 341 |
+
const (
|
| 342 |
+
DW_ACCESS_public = 0x01
|
| 343 |
+
DW_ACCESS_protected = 0x02
|
| 344 |
+
DW_ACCESS_private = 0x03
|
| 345 |
+
)
|
| 346 |
+
|
| 347 |
+
// Table 29
|
| 348 |
+
const (
|
| 349 |
+
DW_VIS_local = 0x01
|
| 350 |
+
DW_VIS_exported = 0x02
|
| 351 |
+
DW_VIS_qualified = 0x03
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
// Table 30
|
| 355 |
+
const (
|
| 356 |
+
DW_VIRTUALITY_none = 0x00
|
| 357 |
+
DW_VIRTUALITY_virtual = 0x01
|
| 358 |
+
DW_VIRTUALITY_pure_virtual = 0x02
|
| 359 |
+
)
|
| 360 |
+
|
| 361 |
+
// Table 31
|
| 362 |
+
const (
|
| 363 |
+
DW_LANG_C89 = 0x0001
|
| 364 |
+
DW_LANG_C = 0x0002
|
| 365 |
+
DW_LANG_Ada83 = 0x0003
|
| 366 |
+
DW_LANG_C_plus_plus = 0x0004
|
| 367 |
+
DW_LANG_Cobol74 = 0x0005
|
| 368 |
+
DW_LANG_Cobol85 = 0x0006
|
| 369 |
+
DW_LANG_Fortran77 = 0x0007
|
| 370 |
+
DW_LANG_Fortran90 = 0x0008
|
| 371 |
+
DW_LANG_Pascal83 = 0x0009
|
| 372 |
+
DW_LANG_Modula2 = 0x000a
|
| 373 |
+
// Dwarf3
|
| 374 |
+
DW_LANG_Java = 0x000b
|
| 375 |
+
DW_LANG_C99 = 0x000c
|
| 376 |
+
DW_LANG_Ada95 = 0x000d
|
| 377 |
+
DW_LANG_Fortran95 = 0x000e
|
| 378 |
+
DW_LANG_PLI = 0x000f
|
| 379 |
+
DW_LANG_ObjC = 0x0010
|
| 380 |
+
DW_LANG_ObjC_plus_plus = 0x0011
|
| 381 |
+
DW_LANG_UPC = 0x0012
|
| 382 |
+
DW_LANG_D = 0x0013
|
| 383 |
+
// Dwarf4
|
| 384 |
+
DW_LANG_Python = 0x0014
|
| 385 |
+
// Dwarf5
|
| 386 |
+
DW_LANG_Go = 0x0016
|
| 387 |
+
|
| 388 |
+
DW_LANG_lo_user = 0x8000
|
| 389 |
+
DW_LANG_hi_user = 0xffff
|
| 390 |
+
)
|
| 391 |
+
|
| 392 |
+
// Table 32
|
| 393 |
+
const (
|
| 394 |
+
DW_ID_case_sensitive = 0x00
|
| 395 |
+
DW_ID_up_case = 0x01
|
| 396 |
+
DW_ID_down_case = 0x02
|
| 397 |
+
DW_ID_case_insensitive = 0x03
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
// Table 33
|
| 401 |
+
const (
|
| 402 |
+
DW_CC_normal = 0x01
|
| 403 |
+
DW_CC_program = 0x02
|
| 404 |
+
DW_CC_nocall = 0x03
|
| 405 |
+
DW_CC_lo_user = 0x40
|
| 406 |
+
DW_CC_hi_user = 0xff
|
| 407 |
+
)
|
| 408 |
+
|
| 409 |
+
// Table 34
|
| 410 |
+
const (
|
| 411 |
+
DW_INL_not_inlined = 0x00
|
| 412 |
+
DW_INL_inlined = 0x01
|
| 413 |
+
DW_INL_declared_not_inlined = 0x02
|
| 414 |
+
DW_INL_declared_inlined = 0x03
|
| 415 |
+
)
|
| 416 |
+
|
| 417 |
+
// Table 35
|
| 418 |
+
const (
|
| 419 |
+
DW_ORD_row_major = 0x00
|
| 420 |
+
DW_ORD_col_major = 0x01
|
| 421 |
+
)
|
| 422 |
+
|
| 423 |
+
// Table 36
|
| 424 |
+
const (
|
| 425 |
+
DW_DSC_label = 0x00
|
| 426 |
+
DW_DSC_range = 0x01
|
| 427 |
+
)
|
| 428 |
+
|
| 429 |
+
// Table 37
|
| 430 |
+
const (
|
| 431 |
+
DW_LNS_copy = 0x01
|
| 432 |
+
DW_LNS_advance_pc = 0x02
|
| 433 |
+
DW_LNS_advance_line = 0x03
|
| 434 |
+
DW_LNS_set_file = 0x04
|
| 435 |
+
DW_LNS_set_column = 0x05
|
| 436 |
+
DW_LNS_negate_stmt = 0x06
|
| 437 |
+
DW_LNS_set_basic_block = 0x07
|
| 438 |
+
DW_LNS_const_add_pc = 0x08
|
| 439 |
+
DW_LNS_fixed_advance_pc = 0x09
|
| 440 |
+
// Dwarf3
|
| 441 |
+
DW_LNS_set_prologue_end = 0x0a
|
| 442 |
+
DW_LNS_set_epilogue_begin = 0x0b
|
| 443 |
+
DW_LNS_set_isa = 0x0c
|
| 444 |
+
)
|
| 445 |
+
|
| 446 |
+
// Table 38
|
| 447 |
+
const (
|
| 448 |
+
DW_LNE_end_sequence = 0x01
|
| 449 |
+
DW_LNE_set_address = 0x02
|
| 450 |
+
DW_LNE_define_file = 0x03
|
| 451 |
+
DW_LNE_lo_user = 0x80
|
| 452 |
+
DW_LNE_hi_user = 0xff
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
// Table 7.25 (DWARF version 5), containing the encodings for the
|
| 456 |
+
// .debug_rnglists entry formats.
|
| 457 |
+
const (
|
| 458 |
+
DW_RLE_end_of_list = 0x0
|
| 459 |
+
DW_RLE_base_addressx = 0x1
|
| 460 |
+
DW_RLE_startx_endx = 0x2
|
| 461 |
+
DW_RLE_startx_length = 0x3
|
| 462 |
+
DW_RLE_offset_pair = 0x4
|
| 463 |
+
DW_RLE_base_address = 0x5
|
| 464 |
+
DW_RLE_start_end = 0x6
|
| 465 |
+
DW_RLE_start_length = 0x7
|
| 466 |
+
)
|
| 467 |
+
|
| 468 |
+
// Table 7.10 (DWARF version 5), containing the encodings for the
|
| 469 |
+
// .debug_loclists entry formats.
|
| 470 |
+
const (
|
| 471 |
+
DW_LLE_end_of_list = 0x0
|
| 472 |
+
DW_LLE_base_addressx = 0x1
|
| 473 |
+
DW_LLE_startx_endx = 0x2
|
| 474 |
+
DW_LLE_startx_length = 0x3
|
| 475 |
+
DW_LLE_offset_pair = 0x4
|
| 476 |
+
DW_LLE_default_location = 0x5
|
| 477 |
+
DW_LLE_base_address = 0x6
|
| 478 |
+
DW_LLE_start_end = 0x7
|
| 479 |
+
DW_LLE_start_length = 0x8
|
| 480 |
+
)
|
| 481 |
+
|
| 482 |
+
// Table 7.27 (DWARF version 5), containing the encodings for the
|
| 483 |
+
// line number header entry formats.
|
| 484 |
+
const (
|
| 485 |
+
DW_LNCT_path = 0x01
|
| 486 |
+
DW_LNCT_directory_index = 0x02
|
| 487 |
+
DW_LNCT_timestamp = 0x03
|
| 488 |
+
DW_LNCT_size = 0x04
|
| 489 |
+
DW_LNCT_md5 = 0x05
|
| 490 |
+
DW_LNCT_lo_user = 0x2000
|
| 491 |
+
DW_LNCT_hi_user = 0x3fff
|
| 492 |
+
)
|
| 493 |
+
|
| 494 |
+
// Table 7.2 (DWARF version 5), dwarf unit type encodings.
|
| 495 |
+
const (
|
| 496 |
+
DW_UT_compile = 0x01
|
| 497 |
+
DW_UT_type = 0x02
|
| 498 |
+
DW_UT_partial = 0x03
|
| 499 |
+
DW_UT_skeleton = 0x04
|
| 500 |
+
DW_UT_split_compile = 0x05
|
| 501 |
+
DW_UT_split_type = 0x06
|
| 502 |
+
DW_UT_lo_user = 0x80
|
| 503 |
+
DW_UT_hi_user = 0xff
|
| 504 |
+
)
|
| 505 |
+
|
| 506 |
+
// Table 39
|
| 507 |
+
const (
|
| 508 |
+
DW_MACINFO_define = 0x01
|
| 509 |
+
DW_MACINFO_undef = 0x02
|
| 510 |
+
DW_MACINFO_start_file = 0x03
|
| 511 |
+
DW_MACINFO_end_file = 0x04
|
| 512 |
+
DW_MACINFO_vendor_ext = 0xff
|
| 513 |
+
)
|
| 514 |
+
|
| 515 |
+
// Table 40.
|
| 516 |
+
const (
|
| 517 |
+
// operand,...
|
| 518 |
+
DW_CFA_nop = 0x00
|
| 519 |
+
DW_CFA_set_loc = 0x01 // address
|
| 520 |
+
DW_CFA_advance_loc1 = 0x02 // 1-byte delta
|
| 521 |
+
DW_CFA_advance_loc2 = 0x03 // 2-byte delta
|
| 522 |
+
DW_CFA_advance_loc4 = 0x04 // 4-byte delta
|
| 523 |
+
DW_CFA_offset_extended = 0x05 // ULEB128 register, ULEB128 offset
|
| 524 |
+
DW_CFA_restore_extended = 0x06 // ULEB128 register
|
| 525 |
+
DW_CFA_undefined = 0x07 // ULEB128 register
|
| 526 |
+
DW_CFA_same_value = 0x08 // ULEB128 register
|
| 527 |
+
DW_CFA_register = 0x09 // ULEB128 register, ULEB128 register
|
| 528 |
+
DW_CFA_remember_state = 0x0a
|
| 529 |
+
DW_CFA_restore_state = 0x0b
|
| 530 |
+
|
| 531 |
+
DW_CFA_def_cfa = 0x0c // ULEB128 register, ULEB128 offset
|
| 532 |
+
DW_CFA_def_cfa_register = 0x0d // ULEB128 register
|
| 533 |
+
DW_CFA_def_cfa_offset = 0x0e // ULEB128 offset
|
| 534 |
+
DW_CFA_def_cfa_expression = 0x0f // BLOCK
|
| 535 |
+
DW_CFA_expression = 0x10 // ULEB128 register, BLOCK
|
| 536 |
+
DW_CFA_offset_extended_sf = 0x11 // ULEB128 register, SLEB128 offset
|
| 537 |
+
DW_CFA_def_cfa_sf = 0x12 // ULEB128 register, SLEB128 offset
|
| 538 |
+
DW_CFA_def_cfa_offset_sf = 0x13 // SLEB128 offset
|
| 539 |
+
DW_CFA_val_offset = 0x14 // ULEB128, ULEB128
|
| 540 |
+
DW_CFA_val_offset_sf = 0x15 // ULEB128, SLEB128
|
| 541 |
+
DW_CFA_val_expression = 0x16 // ULEB128, BLOCK
|
| 542 |
+
|
| 543 |
+
DW_CFA_lo_user = 0x1c
|
| 544 |
+
DW_CFA_hi_user = 0x3f
|
| 545 |
+
|
| 546 |
+
// Opcodes that take an addend operand.
|
| 547 |
+
DW_CFA_advance_loc = 0x1 << 6 // +delta
|
| 548 |
+
DW_CFA_offset = 0x2 << 6 // +register (ULEB128 offset)
|
| 549 |
+
DW_CFA_restore = 0x3 << 6 // +register
|
| 550 |
+
)
|
go/src/cmd/internal/dwarf/dwarf_test.go
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 dwarf
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"reflect"
|
| 9 |
+
"testing"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
func TestSevenBitEnc128(t *testing.T) {
|
| 13 |
+
t.Run("unsigned", func(t *testing.T) {
|
| 14 |
+
for v := int64(-255); v < 255; v++ {
|
| 15 |
+
s := sevenBitU(v)
|
| 16 |
+
if s == nil {
|
| 17 |
+
continue
|
| 18 |
+
}
|
| 19 |
+
b := AppendUleb128(nil, uint64(v))
|
| 20 |
+
if !reflect.DeepEqual(b, s) {
|
| 21 |
+
t.Errorf("sevenBitU(%d) = %v but AppendUleb128(%d) = %v", v, s, v, b)
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
})
|
| 25 |
+
|
| 26 |
+
t.Run("signed", func(t *testing.T) {
|
| 27 |
+
for v := int64(-255); v < 255; v++ {
|
| 28 |
+
s := sevenBitS(v)
|
| 29 |
+
if s == nil {
|
| 30 |
+
continue
|
| 31 |
+
}
|
| 32 |
+
b := AppendSleb128(nil, v)
|
| 33 |
+
if !reflect.DeepEqual(b, s) {
|
| 34 |
+
t.Errorf("sevenBitS(%d) = %v but AppendSleb128(%d) = %v", v, s, v, b)
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
})
|
| 38 |
+
}
|
go/src/cmd/internal/dwarf/putvarabbrevgen.go
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Code generated by TestPutVarAbbrevGenerator. DO NOT EDIT.
|
| 2 |
+
// Regenerate using go test -run TestPutVarAbbrevGenerator -generate instead.
|
| 3 |
+
|
| 4 |
+
package dwarf
|
| 5 |
+
|
| 6 |
+
var putvarAbbrevs = []dwAbbrev{
|
| 7 |
+
{
|
| 8 |
+
DW_TAG_variable,
|
| 9 |
+
DW_CHILDREN_no,
|
| 10 |
+
[]dwAttrForm{
|
| 11 |
+
{DW_AT_name, DW_FORM_string},
|
| 12 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 13 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 14 |
+
},
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
DW_TAG_formal_parameter,
|
| 18 |
+
DW_CHILDREN_no,
|
| 19 |
+
[]dwAttrForm{
|
| 20 |
+
{DW_AT_name, DW_FORM_string},
|
| 21 |
+
{DW_AT_variable_parameter, DW_FORM_flag},
|
| 22 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 23 |
+
},
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
DW_TAG_variable,
|
| 27 |
+
DW_CHILDREN_no,
|
| 28 |
+
[]dwAttrForm{
|
| 29 |
+
{DW_AT_abstract_origin, DW_FORM_ref_addr},
|
| 30 |
+
{DW_AT_location, DW_FORM_sec_offset},
|
| 31 |
+
},
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
DW_TAG_variable,
|
| 35 |
+
DW_CHILDREN_no,
|
| 36 |
+
[]dwAttrForm{
|
| 37 |
+
{DW_AT_abstract_origin, DW_FORM_ref_addr},
|
| 38 |
+
{DW_AT_location, DW_FORM_block1},
|
| 39 |
+
},
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
DW_TAG_variable,
|
| 43 |
+
DW_CHILDREN_no,
|
| 44 |
+
[]dwAttrForm{
|
| 45 |
+
{DW_AT_name, DW_FORM_string},
|
| 46 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 47 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 48 |
+
{DW_AT_go_closure_offset, DW_FORM_udata},
|
| 49 |
+
{DW_AT_location, DW_FORM_sec_offset},
|
| 50 |
+
},
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
DW_TAG_variable,
|
| 54 |
+
DW_CHILDREN_no,
|
| 55 |
+
[]dwAttrForm{
|
| 56 |
+
{DW_AT_name, DW_FORM_string},
|
| 57 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 58 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 59 |
+
{DW_AT_go_closure_offset, DW_FORM_udata},
|
| 60 |
+
{DW_AT_location, DW_FORM_block1},
|
| 61 |
+
},
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
DW_TAG_variable,
|
| 65 |
+
DW_CHILDREN_no,
|
| 66 |
+
[]dwAttrForm{
|
| 67 |
+
{DW_AT_name, DW_FORM_string},
|
| 68 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 69 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 70 |
+
{DW_AT_location, DW_FORM_sec_offset},
|
| 71 |
+
},
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
DW_TAG_variable,
|
| 75 |
+
DW_CHILDREN_no,
|
| 76 |
+
[]dwAttrForm{
|
| 77 |
+
{DW_AT_name, DW_FORM_string},
|
| 78 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 79 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 80 |
+
{DW_AT_location, DW_FORM_block1},
|
| 81 |
+
},
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
DW_TAG_formal_parameter,
|
| 85 |
+
DW_CHILDREN_no,
|
| 86 |
+
[]dwAttrForm{
|
| 87 |
+
{DW_AT_abstract_origin, DW_FORM_ref_addr},
|
| 88 |
+
{DW_AT_location, DW_FORM_sec_offset},
|
| 89 |
+
},
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
DW_TAG_formal_parameter,
|
| 93 |
+
DW_CHILDREN_no,
|
| 94 |
+
[]dwAttrForm{
|
| 95 |
+
{DW_AT_abstract_origin, DW_FORM_ref_addr},
|
| 96 |
+
{DW_AT_location, DW_FORM_block1},
|
| 97 |
+
},
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
DW_TAG_formal_parameter,
|
| 101 |
+
DW_CHILDREN_no,
|
| 102 |
+
[]dwAttrForm{
|
| 103 |
+
{DW_AT_name, DW_FORM_string},
|
| 104 |
+
{DW_AT_variable_parameter, DW_FORM_flag},
|
| 105 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 106 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 107 |
+
{DW_AT_go_closure_offset, DW_FORM_udata},
|
| 108 |
+
{DW_AT_location, DW_FORM_sec_offset},
|
| 109 |
+
},
|
| 110 |
+
},
|
| 111 |
+
{
|
| 112 |
+
DW_TAG_formal_parameter,
|
| 113 |
+
DW_CHILDREN_no,
|
| 114 |
+
[]dwAttrForm{
|
| 115 |
+
{DW_AT_name, DW_FORM_string},
|
| 116 |
+
{DW_AT_variable_parameter, DW_FORM_flag},
|
| 117 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 118 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 119 |
+
{DW_AT_go_closure_offset, DW_FORM_udata},
|
| 120 |
+
{DW_AT_location, DW_FORM_block1},
|
| 121 |
+
},
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
DW_TAG_formal_parameter,
|
| 125 |
+
DW_CHILDREN_no,
|
| 126 |
+
[]dwAttrForm{
|
| 127 |
+
{DW_AT_name, DW_FORM_string},
|
| 128 |
+
{DW_AT_variable_parameter, DW_FORM_flag},
|
| 129 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 130 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 131 |
+
{DW_AT_location, DW_FORM_sec_offset},
|
| 132 |
+
},
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
DW_TAG_formal_parameter,
|
| 136 |
+
DW_CHILDREN_no,
|
| 137 |
+
[]dwAttrForm{
|
| 138 |
+
{DW_AT_name, DW_FORM_string},
|
| 139 |
+
{DW_AT_variable_parameter, DW_FORM_flag},
|
| 140 |
+
{DW_AT_decl_line, DW_FORM_udata},
|
| 141 |
+
{DW_AT_type, DW_FORM_ref_addr},
|
| 142 |
+
{DW_AT_location, DW_FORM_block1},
|
| 143 |
+
},
|
| 144 |
+
},
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
func putAbstractVarAbbrev(v *Var) int {
|
| 148 |
+
if v.Tag == DW_TAG_variable {
|
| 149 |
+
return DW_ABRV_PUTVAR_START + 0
|
| 150 |
+
} else {
|
| 151 |
+
return DW_ABRV_PUTVAR_START + 1
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
func putvarAbbrev(v *Var, concrete, withLoclist bool) int {
|
| 156 |
+
if v.Tag == DW_TAG_variable {
|
| 157 |
+
if concrete {
|
| 158 |
+
if withLoclist {
|
| 159 |
+
return DW_ABRV_PUTVAR_START + 2
|
| 160 |
+
} else {
|
| 161 |
+
return DW_ABRV_PUTVAR_START + 3
|
| 162 |
+
}
|
| 163 |
+
} else {
|
| 164 |
+
if v.ClosureOffset > 0 {
|
| 165 |
+
if withLoclist {
|
| 166 |
+
return DW_ABRV_PUTVAR_START + 4
|
| 167 |
+
} else {
|
| 168 |
+
return DW_ABRV_PUTVAR_START + 5
|
| 169 |
+
}
|
| 170 |
+
} else {
|
| 171 |
+
if withLoclist {
|
| 172 |
+
return DW_ABRV_PUTVAR_START + 6
|
| 173 |
+
} else {
|
| 174 |
+
return DW_ABRV_PUTVAR_START + 7
|
| 175 |
+
}
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
} else {
|
| 179 |
+
if concrete {
|
| 180 |
+
if withLoclist {
|
| 181 |
+
return DW_ABRV_PUTVAR_START + 8
|
| 182 |
+
} else {
|
| 183 |
+
return DW_ABRV_PUTVAR_START + 9
|
| 184 |
+
}
|
| 185 |
+
} else {
|
| 186 |
+
if v.ClosureOffset > 0 {
|
| 187 |
+
if withLoclist {
|
| 188 |
+
return DW_ABRV_PUTVAR_START + 10
|
| 189 |
+
} else {
|
| 190 |
+
return DW_ABRV_PUTVAR_START + 11
|
| 191 |
+
}
|
| 192 |
+
} else {
|
| 193 |
+
if withLoclist {
|
| 194 |
+
return DW_ABRV_PUTVAR_START + 12
|
| 195 |
+
} else {
|
| 196 |
+
return DW_ABRV_PUTVAR_START + 13
|
| 197 |
+
}
|
| 198 |
+
}
|
| 199 |
+
}
|
| 200 |
+
}
|
| 201 |
+
}
|
go/src/cmd/internal/dwarf/putvarabbrevgen_test.go
ADD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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2024 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 dwarf
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"bytes"
|
| 9 |
+
"flag"
|
| 10 |
+
"fmt"
|
| 11 |
+
"go/ast"
|
| 12 |
+
"go/format"
|
| 13 |
+
"go/parser"
|
| 14 |
+
"go/printer"
|
| 15 |
+
"go/token"
|
| 16 |
+
"os"
|
| 17 |
+
"strconv"
|
| 18 |
+
"strings"
|
| 19 |
+
"testing"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
const pvagenfile = "./putvarabbrevgen.go"
|
| 23 |
+
|
| 24 |
+
var pvaDoGenerate bool
|
| 25 |
+
|
| 26 |
+
func TestMain(m *testing.M) {
|
| 27 |
+
flag.BoolVar(&pvaDoGenerate, "generate", false, "regenerates "+pvagenfile)
|
| 28 |
+
flag.Parse()
|
| 29 |
+
os.Exit(m.Run())
|
| 30 |
+
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
// TestPutVarAbbrevGenerator checks that putvarabbrevgen.go is kept in sync
|
| 34 |
+
// with the contents of functions putvar and putAbstractVar. If test flag -generate
|
| 35 |
+
// is specified the file is regenerated instead.
|
| 36 |
+
//
|
| 37 |
+
// The block of code in putvar and putAbstractVar that picks the correct
|
| 38 |
+
// abbrev is also generated automatically by this function by looking at all
|
| 39 |
+
// the possible paths in their CFG and the order in which putattr is called.
|
| 40 |
+
//
|
| 41 |
+
// There are some restrictions on how putattr can be used in putvar and
|
| 42 |
+
// putAbstractVar:
|
| 43 |
+
//
|
| 44 |
+
// 1. it shouldn't appear inside a for or switch statements
|
| 45 |
+
// 2. it can appear within any number of nested if/else statements but the
|
| 46 |
+
// conditionals must not change after putvarAbbrev/putAbstractVarAbbrev
|
| 47 |
+
// are called
|
| 48 |
+
// 3. the form argument of putattr must be a compile time constant
|
| 49 |
+
// 4. each putattr call must be followed by a comment containing the name of
|
| 50 |
+
// the attribute it is setting
|
| 51 |
+
//
|
| 52 |
+
// TestPutVarAbbrevGenerator will fail if (1) or (4) are not respected and
|
| 53 |
+
// the generated code will not compile if (3) is violated. Violating (2)
|
| 54 |
+
// will result in code silently wrong code (which will usually be detected
|
| 55 |
+
// by one of the tests that parse debug_info).
|
| 56 |
+
func TestPutVarAbbrevGenerator(t *testing.T) {
|
| 57 |
+
spvagenfile := pvagenerate(t)
|
| 58 |
+
|
| 59 |
+
if pvaDoGenerate {
|
| 60 |
+
err := os.WriteFile(pvagenfile, []byte(spvagenfile), 0660)
|
| 61 |
+
if err != nil {
|
| 62 |
+
t.Fatal(err)
|
| 63 |
+
}
|
| 64 |
+
return
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
slurp := func(name string) string {
|
| 68 |
+
out, err := os.ReadFile(name)
|
| 69 |
+
if err != nil {
|
| 70 |
+
t.Fatal(err)
|
| 71 |
+
}
|
| 72 |
+
return string(out)
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
if spvagenfile != slurp(pvagenfile) {
|
| 76 |
+
t.Error(pvagenfile + " is out of date")
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
func pvagenerate(t *testing.T) string {
|
| 82 |
+
var fset token.FileSet
|
| 83 |
+
f, err := parser.ParseFile(&fset, "./dwarf.go", nil, parser.ParseComments)
|
| 84 |
+
if err != nil {
|
| 85 |
+
t.Fatal(err)
|
| 86 |
+
}
|
| 87 |
+
cm := ast.NewCommentMap(&fset, f, f.Comments)
|
| 88 |
+
abbrevs := make(map[string]int)
|
| 89 |
+
funcs := map[string]ast.Stmt{}
|
| 90 |
+
for _, decl := range f.Decls {
|
| 91 |
+
decl, ok := decl.(*ast.FuncDecl)
|
| 92 |
+
if !ok || decl.Body == nil {
|
| 93 |
+
continue
|
| 94 |
+
}
|
| 95 |
+
if decl.Name.Name == "putvar" || decl.Name.Name == "putAbstractVar" {
|
| 96 |
+
// construct the simplified CFG
|
| 97 |
+
pvagraph, _ := pvacfgbody(t, &fset, cm, decl.Body.List)
|
| 98 |
+
funcs[decl.Name.Name+"Abbrev"] = pvacfgvisit(pvagraph, abbrevs)
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
abbrevslice := make([]string, len(abbrevs))
|
| 102 |
+
for abbrev, n := range abbrevs {
|
| 103 |
+
abbrevslice[n] = abbrev
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
buf := new(bytes.Buffer)
|
| 107 |
+
fmt.Fprint(buf, `// Code generated by TestPutVarAbbrevGenerator. DO NOT EDIT.
|
| 108 |
+
// Regenerate using go test -run TestPutVarAbbrevGenerator -generate instead.
|
| 109 |
+
|
| 110 |
+
package dwarf
|
| 111 |
+
|
| 112 |
+
var putvarAbbrevs = []dwAbbrev{
|
| 113 |
+
`)
|
| 114 |
+
|
| 115 |
+
for _, abbrev := range abbrevslice {
|
| 116 |
+
fmt.Fprint(buf, abbrev+",\n")
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
fmt.Fprint(buf, "\n}\n\n")
|
| 120 |
+
|
| 121 |
+
fmt.Fprint(buf, "func putAbstractVarAbbrev(v *Var) int {\n")
|
| 122 |
+
format.Node(buf, &token.FileSet{}, funcs["putAbstractVarAbbrev"])
|
| 123 |
+
fmt.Fprint(buf, "}\n\n")
|
| 124 |
+
|
| 125 |
+
fmt.Fprint(buf, "func putvarAbbrev(v *Var, concrete, withLoclist bool) int {\n")
|
| 126 |
+
format.Node(buf, &token.FileSet{}, funcs["putvarAbbrev"])
|
| 127 |
+
fmt.Fprint(buf, "}\n")
|
| 128 |
+
|
| 129 |
+
out, err := format.Source(buf.Bytes())
|
| 130 |
+
if err != nil {
|
| 131 |
+
t.Log(string(buf.Bytes()))
|
| 132 |
+
t.Fatal(err)
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
return string(out)
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
type pvacfgnode struct {
|
| 139 |
+
attr, form string
|
| 140 |
+
|
| 141 |
+
cond ast.Expr
|
| 142 |
+
then, els *pvacfgnode
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
// pvacfgbody generates a simplified CFG for a slice of statements,
|
| 146 |
+
// containing only calls to putattr and the if statements affecting them.
|
| 147 |
+
func pvacfgbody(t *testing.T, fset *token.FileSet, cm ast.CommentMap, body []ast.Stmt) (start, end *pvacfgnode) {
|
| 148 |
+
add := func(n *pvacfgnode) {
|
| 149 |
+
if start == nil || end == nil {
|
| 150 |
+
start = n
|
| 151 |
+
end = n
|
| 152 |
+
} else {
|
| 153 |
+
end.then = n
|
| 154 |
+
end = n
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
for _, stmt := range body {
|
| 158 |
+
switch stmt := stmt.(type) {
|
| 159 |
+
case *ast.ExprStmt:
|
| 160 |
+
if x, _ := stmt.X.(*ast.CallExpr); x != nil {
|
| 161 |
+
funstr := exprToString(x.Fun)
|
| 162 |
+
if funstr == "putattr" {
|
| 163 |
+
form, _ := x.Args[3].(*ast.Ident)
|
| 164 |
+
if form == nil {
|
| 165 |
+
t.Fatalf("%s invalid use of putattr", fset.Position(x.Pos()))
|
| 166 |
+
}
|
| 167 |
+
cmt := findLineComment(cm, stmt)
|
| 168 |
+
if cmt == nil {
|
| 169 |
+
t.Fatalf("%s invalid use of putattr (no comment containing the attribute name)", fset.Position(x.Pos()))
|
| 170 |
+
}
|
| 171 |
+
add(&pvacfgnode{attr: strings.TrimSpace(cmt.Text[2:]), form: form.Name})
|
| 172 |
+
}
|
| 173 |
+
}
|
| 174 |
+
case *ast.IfStmt:
|
| 175 |
+
ifStart, ifEnd := pvacfgif(t, fset, cm, stmt)
|
| 176 |
+
if ifStart != nil {
|
| 177 |
+
add(ifStart)
|
| 178 |
+
end = ifEnd
|
| 179 |
+
}
|
| 180 |
+
default:
|
| 181 |
+
// check that nothing under this contains a putattr call
|
| 182 |
+
ast.Inspect(stmt, func(n ast.Node) bool {
|
| 183 |
+
if call, _ := n.(*ast.CallExpr); call != nil {
|
| 184 |
+
if exprToString(call.Fun) == "putattr" {
|
| 185 |
+
t.Fatalf("%s use of putattr in unsupported block", fset.Position(call.Pos()))
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
return true
|
| 189 |
+
})
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
return start, end
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
func pvacfgif(t *testing.T, fset *token.FileSet, cm ast.CommentMap, ifstmt *ast.IfStmt) (start, end *pvacfgnode) {
|
| 196 |
+
thenStart, thenEnd := pvacfgbody(t, fset, cm, ifstmt.Body.List)
|
| 197 |
+
var elseStart, elseEnd *pvacfgnode
|
| 198 |
+
if ifstmt.Else != nil {
|
| 199 |
+
switch els := ifstmt.Else.(type) {
|
| 200 |
+
case *ast.IfStmt:
|
| 201 |
+
elseStart, elseEnd = pvacfgif(t, fset, cm, els)
|
| 202 |
+
case *ast.BlockStmt:
|
| 203 |
+
elseStart, elseEnd = pvacfgbody(t, fset, cm, els.List)
|
| 204 |
+
default:
|
| 205 |
+
t.Fatalf("%s: unexpected statement %T", fset.Position(els.Pos()), els)
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
if thenStart != nil && elseStart != nil && thenStart == thenEnd && elseStart == elseEnd && thenStart.form == elseStart.form && thenStart.attr == elseStart.attr {
|
| 210 |
+
return thenStart, thenEnd
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
if thenStart != nil || elseStart != nil {
|
| 214 |
+
start = &pvacfgnode{cond: ifstmt.Cond}
|
| 215 |
+
end = &pvacfgnode{}
|
| 216 |
+
if thenStart != nil {
|
| 217 |
+
start.then = thenStart
|
| 218 |
+
thenEnd.then = end
|
| 219 |
+
} else {
|
| 220 |
+
start.then = end
|
| 221 |
+
}
|
| 222 |
+
if elseStart != nil {
|
| 223 |
+
start.els = elseStart
|
| 224 |
+
elseEnd.then = end
|
| 225 |
+
} else {
|
| 226 |
+
start.els = end
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
+
return start, end
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
func exprToString(t ast.Expr) string {
|
| 233 |
+
var buf bytes.Buffer
|
| 234 |
+
printer.Fprint(&buf, token.NewFileSet(), t)
|
| 235 |
+
return buf.String()
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
// findLineComment finds the line comment for statement stmt.
|
| 239 |
+
func findLineComment(cm ast.CommentMap, stmt *ast.ExprStmt) *ast.Comment {
|
| 240 |
+
var r *ast.Comment
|
| 241 |
+
for _, cmtg := range cm[stmt] {
|
| 242 |
+
for _, cmt := range cmtg.List {
|
| 243 |
+
if cmt.Slash > stmt.Pos() {
|
| 244 |
+
if r != nil {
|
| 245 |
+
return nil
|
| 246 |
+
}
|
| 247 |
+
r = cmt
|
| 248 |
+
}
|
| 249 |
+
}
|
| 250 |
+
}
|
| 251 |
+
return r
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
// pvacfgvisit visits the CFG depth first, populates abbrevs with all
|
| 255 |
+
// possible dwAbbrev definitions and returns a tree of if/else statements
|
| 256 |
+
// that picks the correct abbrev.
|
| 257 |
+
func pvacfgvisit(pvacfg *pvacfgnode, abbrevs map[string]int) ast.Stmt {
|
| 258 |
+
r := &ast.IfStmt{Cond: &ast.BinaryExpr{
|
| 259 |
+
Op: token.EQL,
|
| 260 |
+
X: &ast.SelectorExpr{X: &ast.Ident{Name: "v"}, Sel: &ast.Ident{Name: "Tag"}},
|
| 261 |
+
Y: &ast.Ident{Name: "DW_TAG_variable"}}}
|
| 262 |
+
r.Body = &ast.BlockStmt{List: []ast.Stmt{
|
| 263 |
+
pvacfgvisitnode(pvacfg, "DW_TAG_variable", []*pvacfgnode{}, abbrevs),
|
| 264 |
+
}}
|
| 265 |
+
r.Else = &ast.BlockStmt{List: []ast.Stmt{
|
| 266 |
+
pvacfgvisitnode(pvacfg, "DW_TAG_formal_parameter", []*pvacfgnode{}, abbrevs),
|
| 267 |
+
}}
|
| 268 |
+
return r
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
func pvacfgvisitnode(pvacfg *pvacfgnode, tag string, path []*pvacfgnode, abbrevs map[string]int) ast.Stmt {
|
| 272 |
+
if pvacfg == nil {
|
| 273 |
+
abbrev := toabbrev(tag, path)
|
| 274 |
+
if _, ok := abbrevs[abbrev]; !ok {
|
| 275 |
+
abbrevs[abbrev] = len(abbrevs)
|
| 276 |
+
}
|
| 277 |
+
return &ast.ReturnStmt{
|
| 278 |
+
Results: []ast.Expr{&ast.BinaryExpr{
|
| 279 |
+
Op: token.ADD,
|
| 280 |
+
X: &ast.Ident{Name: "DW_ABRV_PUTVAR_START"},
|
| 281 |
+
Y: &ast.BasicLit{Kind: token.INT, Value: strconv.Itoa(abbrevs[abbrev])}}}}
|
| 282 |
+
}
|
| 283 |
+
if pvacfg.attr != "" {
|
| 284 |
+
return pvacfgvisitnode(pvacfg.then, tag, append(path, pvacfg), abbrevs)
|
| 285 |
+
} else if pvacfg.cond != nil {
|
| 286 |
+
if bx, _ := pvacfg.cond.(*ast.BinaryExpr); bx != nil && bx.Op == token.EQL && exprToString(bx.X) == "v.Tag" {
|
| 287 |
+
// this condition is "v.Tag == Xxx", check the value of 'tag'
|
| 288 |
+
y := exprToString(bx.Y)
|
| 289 |
+
if y == tag {
|
| 290 |
+
return pvacfgvisitnode(pvacfg.then, tag, path, abbrevs)
|
| 291 |
+
} else {
|
| 292 |
+
return pvacfgvisitnode(pvacfg.els, tag, path, abbrevs)
|
| 293 |
+
}
|
| 294 |
+
} else {
|
| 295 |
+
r := &ast.IfStmt{Cond: pvacfg.cond}
|
| 296 |
+
r.Body = &ast.BlockStmt{List: []ast.Stmt{pvacfgvisitnode(pvacfg.then, tag, path, abbrevs)}}
|
| 297 |
+
r.Else = &ast.BlockStmt{List: []ast.Stmt{pvacfgvisitnode(pvacfg.els, tag, path, abbrevs)}}
|
| 298 |
+
return r
|
| 299 |
+
}
|
| 300 |
+
} else {
|
| 301 |
+
return pvacfgvisitnode(pvacfg.then, tag, path, abbrevs)
|
| 302 |
+
}
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
func toabbrev(tag string, path []*pvacfgnode) string {
|
| 306 |
+
buf := new(bytes.Buffer)
|
| 307 |
+
fmt.Fprintf(buf, "{\n%s,\nDW_CHILDREN_no,\n[]dwAttrForm{\n", tag)
|
| 308 |
+
for _, node := range path {
|
| 309 |
+
if node.cond == nil {
|
| 310 |
+
fmt.Fprintf(buf, "{%s, %s},\n", node.attr, node.form)
|
| 311 |
+
|
| 312 |
+
}
|
| 313 |
+
}
|
| 314 |
+
fmt.Fprint(buf, "},\n}")
|
| 315 |
+
return buf.String()
|
| 316 |
+
}
|
go/src/cmd/internal/edit/edit.go
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 edit implements buffered position-based editing of byte slices.
|
| 6 |
+
package edit
|
| 7 |
+
|
| 8 |
+
import (
|
| 9 |
+
"fmt"
|
| 10 |
+
"sort"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
// A Buffer is a queue of edits to apply to a given byte slice.
|
| 14 |
+
type Buffer struct {
|
| 15 |
+
old []byte
|
| 16 |
+
q edits
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// An edit records a single text modification: change the bytes in [start,end) to new.
|
| 20 |
+
type edit struct {
|
| 21 |
+
start int
|
| 22 |
+
end int
|
| 23 |
+
new string
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// An edits is a list of edits that is sortable by start offset, breaking ties by end offset.
|
| 27 |
+
type edits []edit
|
| 28 |
+
|
| 29 |
+
func (x edits) Len() int { return len(x) }
|
| 30 |
+
func (x edits) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
| 31 |
+
func (x edits) Less(i, j int) bool {
|
| 32 |
+
if x[i].start != x[j].start {
|
| 33 |
+
return x[i].start < x[j].start
|
| 34 |
+
}
|
| 35 |
+
return x[i].end < x[j].end
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// NewBuffer returns a new buffer to accumulate changes to an initial data slice.
|
| 39 |
+
// The returned buffer maintains a reference to the data, so the caller must ensure
|
| 40 |
+
// the data is not modified until after the Buffer is done being used.
|
| 41 |
+
func NewBuffer(data []byte) *Buffer {
|
| 42 |
+
return &Buffer{old: data}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
func (b *Buffer) Insert(pos int, new string) {
|
| 46 |
+
if pos < 0 || pos > len(b.old) {
|
| 47 |
+
panic("invalid edit position")
|
| 48 |
+
}
|
| 49 |
+
b.q = append(b.q, edit{pos, pos, new})
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
func (b *Buffer) Delete(start, end int) {
|
| 53 |
+
if end < start || start < 0 || end > len(b.old) {
|
| 54 |
+
panic("invalid edit position")
|
| 55 |
+
}
|
| 56 |
+
b.q = append(b.q, edit{start, end, ""})
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
func (b *Buffer) Replace(start, end int, new string) {
|
| 60 |
+
if end < start || start < 0 || end > len(b.old) {
|
| 61 |
+
panic("invalid edit position")
|
| 62 |
+
}
|
| 63 |
+
b.q = append(b.q, edit{start, end, new})
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// Bytes returns a new byte slice containing the original data
|
| 67 |
+
// with the queued edits applied.
|
| 68 |
+
func (b *Buffer) Bytes() []byte {
|
| 69 |
+
// Sort edits by starting position and then by ending position.
|
| 70 |
+
// Breaking ties by ending position allows insertions at point x
|
| 71 |
+
// to be applied before a replacement of the text at [x, y).
|
| 72 |
+
sort.Stable(b.q)
|
| 73 |
+
|
| 74 |
+
var new []byte
|
| 75 |
+
offset := 0
|
| 76 |
+
for i, e := range b.q {
|
| 77 |
+
if e.start < offset {
|
| 78 |
+
e0 := b.q[i-1]
|
| 79 |
+
panic(fmt.Sprintf("overlapping edits: [%d,%d)->%q, [%d,%d)->%q", e0.start, e0.end, e0.new, e.start, e.end, e.new))
|
| 80 |
+
}
|
| 81 |
+
new = append(new, b.old[offset:e.start]...)
|
| 82 |
+
offset = e.end
|
| 83 |
+
new = append(new, e.new...)
|
| 84 |
+
}
|
| 85 |
+
new = append(new, b.old[offset:]...)
|
| 86 |
+
return new
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// String returns a string containing the original data
|
| 90 |
+
// with the queued edits applied.
|
| 91 |
+
func (b *Buffer) String() string {
|
| 92 |
+
return string(b.Bytes())
|
| 93 |
+
}
|
go/src/cmd/internal/edit/edit_test.go
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2017 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 edit
|
| 6 |
+
|
| 7 |
+
import "testing"
|
| 8 |
+
|
| 9 |
+
func TestEdit(t *testing.T) {
|
| 10 |
+
b := NewBuffer([]byte("0123456789"))
|
| 11 |
+
b.Insert(8, ",7½,")
|
| 12 |
+
b.Replace(9, 10, "the-end")
|
| 13 |
+
b.Insert(10, "!")
|
| 14 |
+
b.Insert(4, "3.14,")
|
| 15 |
+
b.Insert(4, "π,")
|
| 16 |
+
b.Insert(4, "3.15,")
|
| 17 |
+
b.Replace(3, 4, "three,")
|
| 18 |
+
want := "012three,3.14,π,3.15,4567,7½,8the-end!"
|
| 19 |
+
|
| 20 |
+
s := b.String()
|
| 21 |
+
if s != want {
|
| 22 |
+
t.Errorf("b.String() = %q, want %q", s, want)
|
| 23 |
+
}
|
| 24 |
+
sb := b.Bytes()
|
| 25 |
+
if string(sb) != want {
|
| 26 |
+
t.Errorf("b.Bytes() = %q, want %q", sb, want)
|
| 27 |
+
}
|
| 28 |
+
}
|
go/src/cmd/internal/fuzztest/script_test.go
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 fuzztest
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"cmd/internal/script/scripttest"
|
| 9 |
+
"flag"
|
| 10 |
+
"internal/testenv"
|
| 11 |
+
"testing"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
//go:generate go test cmd/internal/fuzztest -v -run=TestScript/README --fixreadme
|
| 15 |
+
|
| 16 |
+
var fixReadme = flag.Bool("fixreadme", false, "if true, update README for script tests")
|
| 17 |
+
|
| 18 |
+
func TestScript(t *testing.T) {
|
| 19 |
+
testenv.MustHaveGoBuild(t)
|
| 20 |
+
testenv.SkipIfShortAndSlow(t)
|
| 21 |
+
scripttest.RunToolScriptTest(t, nil, "testdata/script", *fixReadme)
|
| 22 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/README
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
| 1 |
+
This file is generated by 'go generate'. DO NOT EDIT.
|
| 2 |
+
|
| 3 |
+
This directory holds test scripts *.txt run during 'go test cmd/<toolname>'.
|
| 4 |
+
To run a specific script foo.txt
|
| 5 |
+
|
| 6 |
+
go test cmd/<toolname> -run=Script/^foo$
|
| 7 |
+
|
| 8 |
+
In general script files should have short names: a few words,
|
| 9 |
+
not whole sentences.
|
| 10 |
+
The first word should be the general category of behavior being tested,
|
| 11 |
+
often the name of a go subcommand (build, link, compile, ...) or concept (vendor, pattern).
|
| 12 |
+
|
| 13 |
+
Each script is a text archive (go doc internal/txtar).
|
| 14 |
+
The script begins with an actual command script to run
|
| 15 |
+
followed by the content of zero or more supporting files to
|
| 16 |
+
create in the script's temporary file system before it starts executing.
|
| 17 |
+
|
| 18 |
+
As an example, run_hello.txt says:
|
| 19 |
+
|
| 20 |
+
# hello world
|
| 21 |
+
go run hello.go
|
| 22 |
+
stderr 'hello world'
|
| 23 |
+
! stdout .
|
| 24 |
+
|
| 25 |
+
-- hello.go --
|
| 26 |
+
package main
|
| 27 |
+
func main() { println("hello world") }
|
| 28 |
+
|
| 29 |
+
Each script runs in a fresh temporary work directory tree, available to scripts as $WORK.
|
| 30 |
+
Scripts also have access to other environment variables, including:
|
| 31 |
+
|
| 32 |
+
GOARCH=<target GOARCH>
|
| 33 |
+
GOOS=<target GOOS>
|
| 34 |
+
TMPDIR=$WORK/tmp
|
| 35 |
+
devnull=<value of os.DevNull>
|
| 36 |
+
goversion=<current Go version; for example, 1.12>
|
| 37 |
+
|
| 38 |
+
On Plan 9, the variables $path and $home are set instead of $PATH and $HOME.
|
| 39 |
+
On Windows, the variables $USERPROFILE and $TMP are set instead of
|
| 40 |
+
$HOME and $TMPDIR.
|
| 41 |
+
|
| 42 |
+
The lines at the top of the script are a sequence of commands to be executed by
|
| 43 |
+
a small script engine configured in .../cmd/internal/script/scripttest/run.go (not the system shell).
|
| 44 |
+
|
| 45 |
+
Each line of a script is parsed into a sequence of space-separated command
|
| 46 |
+
words, with environment variable expansion within each word and # marking
|
| 47 |
+
an end-of-line comment. Additional variables named ':' and '/' are expanded
|
| 48 |
+
within script arguments (expanding to the value of os.PathListSeparator and
|
| 49 |
+
os.PathSeparator respectively) but are not inherited in subprocess environments.
|
| 50 |
+
|
| 51 |
+
Adding single quotes around text keeps spaces in that text from being treated
|
| 52 |
+
as word separators and also disables environment variable expansion. Inside a
|
| 53 |
+
single-quoted block of text, a repeated single quote indicates a literal single
|
| 54 |
+
quote, as in:
|
| 55 |
+
|
| 56 |
+
'Don''t communicate by sharing memory.'
|
| 57 |
+
|
| 58 |
+
A line beginning with # is a comment and conventionally explains what is being
|
| 59 |
+
done or tested at the start of a new section of the script.
|
| 60 |
+
|
| 61 |
+
Commands are executed one at a time, and errors are checked for each command;
|
| 62 |
+
if any command fails unexpectedly, no subsequent commands in the script are
|
| 63 |
+
executed. The command prefix ! indicates that the command on the rest of the
|
| 64 |
+
line (typically go or a matching predicate) must fail instead of succeeding.
|
| 65 |
+
The command prefix ? indicates that the command may or may not succeed, but the
|
| 66 |
+
script should continue regardless.
|
| 67 |
+
|
| 68 |
+
The command prefix [cond] indicates that the command on the rest of the line
|
| 69 |
+
should only run when the condition is satisfied.
|
| 70 |
+
|
| 71 |
+
A condition can be negated: [!root] means to run the rest of the line only if
|
| 72 |
+
the user is not root. Multiple conditions may be given for a single command,
|
| 73 |
+
for example, '[linux] [amd64] skip'. The command will run if all conditions are
|
| 74 |
+
satisfied.
|
| 75 |
+
|
| 76 |
+
When TestScript runs a script and the script fails, by default TestScript shows
|
| 77 |
+
the execution of the most recent phase of the script (since the last # comment)
|
| 78 |
+
and only shows the # comments for earlier phases.
|
| 79 |
+
|
| 80 |
+
Note also that in reported output, the actual name of the per-script temporary directory
|
| 81 |
+
has been consistently replaced with the literal string $WORK.
|
| 82 |
+
|
| 83 |
+
The available commands are:
|
| 84 |
+
cat files...
|
| 85 |
+
concatenate files and print to the script's stdout buffer
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
cc args...
|
| 89 |
+
run the platform C compiler
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
cd dir
|
| 93 |
+
change the working directory
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
chmod perm paths...
|
| 97 |
+
change file mode bits
|
| 98 |
+
|
| 99 |
+
Changes the permissions of the named files or directories to
|
| 100 |
+
be equal to perm.
|
| 101 |
+
Only numerical permissions are supported.
|
| 102 |
+
|
| 103 |
+
cmp [-q] file1 file2
|
| 104 |
+
compare files for differences
|
| 105 |
+
|
| 106 |
+
By convention, file1 is the actual data and file2 is the
|
| 107 |
+
expected data.
|
| 108 |
+
The command succeeds if the file contents are identical.
|
| 109 |
+
File1 can be 'stdout' or 'stderr' to compare the stdout or
|
| 110 |
+
stderr buffer from the most recent command.
|
| 111 |
+
|
| 112 |
+
cmpenv [-q] file1 file2
|
| 113 |
+
compare files for differences, with environment expansion
|
| 114 |
+
|
| 115 |
+
By convention, file1 is the actual data and file2 is the
|
| 116 |
+
expected data.
|
| 117 |
+
The command succeeds if the file contents are identical
|
| 118 |
+
after substituting variables from the script environment.
|
| 119 |
+
File1 can be 'stdout' or 'stderr' to compare the script's
|
| 120 |
+
stdout or stderr buffer.
|
| 121 |
+
|
| 122 |
+
cp src... dst
|
| 123 |
+
copy files to a target file or directory
|
| 124 |
+
|
| 125 |
+
src can include 'stdout' or 'stderr' to copy from the
|
| 126 |
+
script's stdout or stderr buffer.
|
| 127 |
+
|
| 128 |
+
echo string...
|
| 129 |
+
display a line of text
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
env [key[=value]...]
|
| 133 |
+
set or log the values of environment variables
|
| 134 |
+
|
| 135 |
+
With no arguments, print the script environment to the log.
|
| 136 |
+
Otherwise, add the listed key=value pairs to the environment
|
| 137 |
+
or print the listed keys.
|
| 138 |
+
|
| 139 |
+
exec program [args...] [&]
|
| 140 |
+
run an executable program with arguments
|
| 141 |
+
|
| 142 |
+
Note that 'exec' does not terminate the script (unlike Unix
|
| 143 |
+
shells).
|
| 144 |
+
|
| 145 |
+
exists [-readonly] [-exec] file...
|
| 146 |
+
check that files exist
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
go [args...] [&]
|
| 150 |
+
run the 'go' program provided by the script host
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
grep [-count=N] [-q] 'pattern' file
|
| 154 |
+
find lines in a file that match a pattern
|
| 155 |
+
|
| 156 |
+
The command succeeds if at least one match (or the exact
|
| 157 |
+
count, if given) is found.
|
| 158 |
+
The -q flag suppresses printing of matches.
|
| 159 |
+
|
| 160 |
+
help [-v] name...
|
| 161 |
+
log help text for commands and conditions
|
| 162 |
+
|
| 163 |
+
To display help for a specific condition, enclose it in
|
| 164 |
+
brackets: 'help [amd64]'.
|
| 165 |
+
To display complete documentation when listing all commands,
|
| 166 |
+
pass the -v flag.
|
| 167 |
+
|
| 168 |
+
mkdir path...
|
| 169 |
+
create directories, if they do not already exist
|
| 170 |
+
|
| 171 |
+
Unlike Unix mkdir, parent directories are always created if
|
| 172 |
+
needed.
|
| 173 |
+
|
| 174 |
+
mv old new
|
| 175 |
+
rename a file or directory to a new path
|
| 176 |
+
|
| 177 |
+
OS-specific restrictions may apply when old and new are in
|
| 178 |
+
different directories.
|
| 179 |
+
|
| 180 |
+
replace [old new]... file
|
| 181 |
+
replace strings in a file
|
| 182 |
+
|
| 183 |
+
The 'old' and 'new' arguments are unquoted as if in quoted
|
| 184 |
+
Go strings.
|
| 185 |
+
|
| 186 |
+
rm path...
|
| 187 |
+
remove a file or directory
|
| 188 |
+
|
| 189 |
+
If the path is a directory, its contents are removed
|
| 190 |
+
recursively.
|
| 191 |
+
|
| 192 |
+
skip [msg]
|
| 193 |
+
skip the current test
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
sleep duration [&]
|
| 197 |
+
sleep for a specified duration
|
| 198 |
+
|
| 199 |
+
The duration must be given as a Go time.Duration string.
|
| 200 |
+
|
| 201 |
+
stderr [-count=N] [-q] 'pattern' file
|
| 202 |
+
find lines in the stderr buffer that match a pattern
|
| 203 |
+
|
| 204 |
+
The command succeeds if at least one match (or the exact
|
| 205 |
+
count, if given) is found.
|
| 206 |
+
The -q flag suppresses printing of matches.
|
| 207 |
+
|
| 208 |
+
stdout [-count=N] [-q] 'pattern' file
|
| 209 |
+
find lines in the stdout buffer that match a pattern
|
| 210 |
+
|
| 211 |
+
The command succeeds if at least one match (or the exact
|
| 212 |
+
count, if given) is found.
|
| 213 |
+
The -q flag suppresses printing of matches.
|
| 214 |
+
|
| 215 |
+
stop [msg]
|
| 216 |
+
stop execution of the script
|
| 217 |
+
|
| 218 |
+
The message is written to the script log, but no error is
|
| 219 |
+
reported from the script engine.
|
| 220 |
+
|
| 221 |
+
symlink path -> target
|
| 222 |
+
create a symlink
|
| 223 |
+
|
| 224 |
+
Creates path as a symlink to target.
|
| 225 |
+
The '->' token (like in 'ls -l' output on Unix) is required.
|
| 226 |
+
|
| 227 |
+
wait
|
| 228 |
+
wait for completion of background commands
|
| 229 |
+
|
| 230 |
+
Waits for all background commands to complete.
|
| 231 |
+
The output (and any error) from each command is printed to
|
| 232 |
+
the log in the order in which the commands were started.
|
| 233 |
+
After the call to 'wait', the script's stdout and stderr
|
| 234 |
+
buffers contain the concatenation of the background
|
| 235 |
+
commands' outputs.
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
The available conditions are:
|
| 240 |
+
[GOARCH:*]
|
| 241 |
+
runtime.GOARCH == <suffix>
|
| 242 |
+
[GODEBUG:*]
|
| 243 |
+
GODEBUG contains <suffix>
|
| 244 |
+
[GOEXPERIMENT:*]
|
| 245 |
+
GOEXPERIMENT <suffix> is enabled
|
| 246 |
+
[GOOS:*]
|
| 247 |
+
runtime.GOOS == <suffix>
|
| 248 |
+
[asan]
|
| 249 |
+
GOOS/GOARCH supports -asan
|
| 250 |
+
[buildmode:*]
|
| 251 |
+
go supports -buildmode=<suffix>
|
| 252 |
+
[cgo]
|
| 253 |
+
host CGO_ENABLED
|
| 254 |
+
[cgolinkext]
|
| 255 |
+
platform requires external linking for cgo
|
| 256 |
+
[compiler:*]
|
| 257 |
+
runtime.Compiler == <suffix>
|
| 258 |
+
[cross]
|
| 259 |
+
cmd/go GOOS/GOARCH != GOHOSTOS/GOHOSTARCH
|
| 260 |
+
[exec:*]
|
| 261 |
+
<suffix> names an executable in the test binary's PATH
|
| 262 |
+
[fuzz]
|
| 263 |
+
GOOS/GOARCH supports -fuzz
|
| 264 |
+
[fuzz-instrumented]
|
| 265 |
+
GOOS/GOARCH supports -fuzz with instrumentation
|
| 266 |
+
[go-builder]
|
| 267 |
+
GO_BUILDER_NAME is non-empty
|
| 268 |
+
[link]
|
| 269 |
+
testenv.HasLink()
|
| 270 |
+
[msan]
|
| 271 |
+
GOOS/GOARCH supports -msan
|
| 272 |
+
[mustlinkext]
|
| 273 |
+
platform always requires external linking
|
| 274 |
+
[pielinkext]
|
| 275 |
+
platform requires external linking for PIE
|
| 276 |
+
[race]
|
| 277 |
+
GOOS/GOARCH supports -race
|
| 278 |
+
[root]
|
| 279 |
+
os.Geteuid() == 0
|
| 280 |
+
[short]
|
| 281 |
+
testing.Short()
|
| 282 |
+
[symlink]
|
| 283 |
+
testenv.HasSymlink()
|
| 284 |
+
[verbose]
|
| 285 |
+
testing.Verbose()
|
| 286 |
+
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz.txt
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[short] skip
|
| 3 |
+
env GOCACHE=$WORK/cache
|
| 4 |
+
|
| 5 |
+
# This test uses -vet=off to suppress vet, as vet's "tests" analyzer would
|
| 6 |
+
# otherwise statically report the problems we are trying to observe dynamically.
|
| 7 |
+
|
| 8 |
+
# Test that running a fuzz target that returns without failing or calling
|
| 9 |
+
# f.Fuzz fails and causes a non-zero exit status.
|
| 10 |
+
! go test -vet=off noop_fuzz_test.go
|
| 11 |
+
! stdout ^ok
|
| 12 |
+
stdout FAIL
|
| 13 |
+
|
| 14 |
+
# Test that fuzzing a fuzz target that returns without failing or calling
|
| 15 |
+
# f.Fuzz fails and causes a non-zero exit status.
|
| 16 |
+
! go test -vet=off -fuzz=Fuzz -fuzztime=1x noop_fuzz_test.go
|
| 17 |
+
! stdout ^ok
|
| 18 |
+
stdout FAIL
|
| 19 |
+
|
| 20 |
+
# Test that calling f.Error in a fuzz target causes a non-zero exit status.
|
| 21 |
+
! go test -vet=off -fuzz=Fuzz -fuzztime=1x error_fuzz_test.go
|
| 22 |
+
! stdout ^ok
|
| 23 |
+
stdout FAIL
|
| 24 |
+
|
| 25 |
+
# Test that calling f.Fatal in a fuzz target causes a non-zero exit status.
|
| 26 |
+
! go test -vet=off fatal_fuzz_test.go
|
| 27 |
+
! stdout ^ok
|
| 28 |
+
stdout FAIL
|
| 29 |
+
|
| 30 |
+
# Test that successful test exits cleanly.
|
| 31 |
+
go test -vet=off success_fuzz_test.go
|
| 32 |
+
stdout ^ok
|
| 33 |
+
! stdout FAIL
|
| 34 |
+
|
| 35 |
+
# Test that successful fuzzing exits cleanly.
|
| 36 |
+
go test -vet=off -fuzz=Fuzz -fuzztime=1x success_fuzz_test.go
|
| 37 |
+
stdout ok
|
| 38 |
+
! stdout FAIL
|
| 39 |
+
|
| 40 |
+
# Test that calling f.Fatal while fuzzing causes a non-zero exit status.
|
| 41 |
+
! go test -vet=off -fuzz=Fuzz -fuzztime=1x fatal_fuzz_test.go
|
| 42 |
+
! stdout ^ok
|
| 43 |
+
stdout FAIL
|
| 44 |
+
|
| 45 |
+
# Test error with seed corpus in f.Fuzz
|
| 46 |
+
! go test -vet=off -run Fuzz_error -vet=off fuzz_add_test.go
|
| 47 |
+
! stdout ^ok
|
| 48 |
+
stdout FAIL
|
| 49 |
+
stdout 'error here'
|
| 50 |
+
|
| 51 |
+
[short] stop
|
| 52 |
+
|
| 53 |
+
# Test that calling panic(nil) in a fuzz target causes a non-zero exit status.
|
| 54 |
+
! go test -vet=off panic_fuzz_test.go
|
| 55 |
+
! stdout ^ok
|
| 56 |
+
stdout FAIL
|
| 57 |
+
|
| 58 |
+
# Test that skipped test exits cleanly.
|
| 59 |
+
go test -vet=off skipped_fuzz_test.go
|
| 60 |
+
stdout ok
|
| 61 |
+
! stdout FAIL
|
| 62 |
+
|
| 63 |
+
# Test that f.Fatal within f.Fuzz panics
|
| 64 |
+
! go test -vet=off fatal_fuzz_fn_fuzz_test.go
|
| 65 |
+
! stdout ^ok
|
| 66 |
+
! stdout 'fatal here'
|
| 67 |
+
stdout FAIL
|
| 68 |
+
stdout 'fuzz target'
|
| 69 |
+
|
| 70 |
+
# Test that f.Error within f.Fuzz panics
|
| 71 |
+
! go test -vet=off error_fuzz_fn_fuzz_test.go
|
| 72 |
+
! stdout ^ok
|
| 73 |
+
! stdout 'error here'
|
| 74 |
+
stdout FAIL
|
| 75 |
+
stdout 'fuzz target'
|
| 76 |
+
|
| 77 |
+
# Test that f.Fail within f.Fuzz panics
|
| 78 |
+
! go test -vet=off fail_fuzz_fn_fuzz_test.go
|
| 79 |
+
! stdout ^ok
|
| 80 |
+
stdout FAIL
|
| 81 |
+
stdout 'fuzz target'
|
| 82 |
+
|
| 83 |
+
# Test that f.Skip within f.Fuzz panics
|
| 84 |
+
! go test -vet=off skip_fuzz_fn_fuzz_test.go
|
| 85 |
+
! stdout ^ok
|
| 86 |
+
! stdout 'skip here'
|
| 87 |
+
stdout FAIL
|
| 88 |
+
stdout 'fuzz target'
|
| 89 |
+
|
| 90 |
+
# Test that f.Skipped within f.Fuzz panics
|
| 91 |
+
! go test -vet=off skipped_fuzz_fn_fuzz_test.go
|
| 92 |
+
! stdout ^ok
|
| 93 |
+
! stdout 'f.Skipped is'
|
| 94 |
+
stdout FAIL
|
| 95 |
+
stdout 'fuzz target'
|
| 96 |
+
stdout 't.Skipped is false'
|
| 97 |
+
|
| 98 |
+
# Test that runtime.Goexit within the fuzz function is an error.
|
| 99 |
+
! go test -vet=off goexit_fuzz_fn_fuzz_test.go
|
| 100 |
+
! stdout ^ok
|
| 101 |
+
stdout FAIL
|
| 102 |
+
|
| 103 |
+
# Test that a call to f.Fatal after the Fuzz func is executed.
|
| 104 |
+
! go test -vet=off fatal_after_fuzz_func_fuzz_test.go
|
| 105 |
+
! stdout ok
|
| 106 |
+
stdout FAIL
|
| 107 |
+
|
| 108 |
+
# Test that missing *T in f.Fuzz causes a non-zero exit status.
|
| 109 |
+
! go test -vet=off incomplete_fuzz_call_fuzz_test.go
|
| 110 |
+
! stdout ^ok
|
| 111 |
+
stdout FAIL
|
| 112 |
+
|
| 113 |
+
# Test that a panic in the Cleanup func is executed.
|
| 114 |
+
! go test -vet=off cleanup_fuzz_test.go
|
| 115 |
+
! stdout ^ok
|
| 116 |
+
stdout FAIL
|
| 117 |
+
stdout 'failed some precondition'
|
| 118 |
+
|
| 119 |
+
# Test success with seed corpus in f.Fuzz
|
| 120 |
+
go test -vet=off -run Fuzz_pass -vet=off fuzz_add_test.go
|
| 121 |
+
stdout ok
|
| 122 |
+
! stdout FAIL
|
| 123 |
+
! stdout 'off by one error'
|
| 124 |
+
|
| 125 |
+
# Test fatal with seed corpus in f.Fuzz
|
| 126 |
+
! go test -vet=off -run Fuzz_fatal -vet=off fuzz_add_test.go
|
| 127 |
+
! stdout ^ok
|
| 128 |
+
stdout FAIL
|
| 129 |
+
stdout 'fatal here'
|
| 130 |
+
|
| 131 |
+
# Test panic with seed corpus in f.Fuzz
|
| 132 |
+
! go test -vet=off -run Fuzz_panic -vet=off fuzz_add_test.go
|
| 133 |
+
! stdout ^ok
|
| 134 |
+
stdout FAIL
|
| 135 |
+
stdout 'off by one error'
|
| 136 |
+
|
| 137 |
+
# Test panic(nil) with seed corpus in f.Fuzz
|
| 138 |
+
! go test -vet=off -run Fuzz_nilPanic -vet=off fuzz_add_test.go
|
| 139 |
+
! stdout ^ok
|
| 140 |
+
stdout FAIL
|
| 141 |
+
|
| 142 |
+
# Test panic with unsupported seed corpus
|
| 143 |
+
! go test -vet=off -run Fuzz_unsupported -vet=off fuzz_add_test.go
|
| 144 |
+
! stdout ^ok
|
| 145 |
+
stdout FAIL
|
| 146 |
+
|
| 147 |
+
# Test panic with different number of args to f.Add
|
| 148 |
+
! go test -vet=off -run Fuzz_addDifferentNumber -vet=off fuzz_add_test.go
|
| 149 |
+
! stdout ^ok
|
| 150 |
+
stdout FAIL
|
| 151 |
+
|
| 152 |
+
# Test panic with different type of args to f.Add
|
| 153 |
+
! go test -vet=off -run Fuzz_addDifferentType -vet=off fuzz_add_test.go
|
| 154 |
+
! stdout ^ok
|
| 155 |
+
stdout FAIL
|
| 156 |
+
|
| 157 |
+
# Test that the wrong type given with f.Add will fail.
|
| 158 |
+
! go test -vet=off -run Fuzz_wrongType -vet=off fuzz_add_test.go
|
| 159 |
+
! stdout ^ok
|
| 160 |
+
stdout '\[string int\], want \[\[\]uint8 int8\]'
|
| 161 |
+
stdout FAIL
|
| 162 |
+
|
| 163 |
+
# Test fatal with testdata seed corpus
|
| 164 |
+
! go test -vet=off -run Fuzz_fail corpustesting/fuzz_testdata_corpus_test.go
|
| 165 |
+
! stdout ^ok
|
| 166 |
+
stdout FAIL
|
| 167 |
+
stdout 'fatal here'
|
| 168 |
+
|
| 169 |
+
# Test pass with testdata seed corpus
|
| 170 |
+
go test -vet=off -run Fuzz_pass corpustesting/fuzz_testdata_corpus_test.go
|
| 171 |
+
stdout ok
|
| 172 |
+
! stdout FAIL
|
| 173 |
+
! stdout 'fatal here'
|
| 174 |
+
|
| 175 |
+
# Test pass with testdata and f.Add seed corpus
|
| 176 |
+
go test -vet=off -run Fuzz_passString corpustesting/fuzz_testdata_corpus_test.go
|
| 177 |
+
stdout ok
|
| 178 |
+
! stdout FAIL
|
| 179 |
+
|
| 180 |
+
# Fuzzing pass with testdata and f.Add seed corpus (skip running tests first)
|
| 181 |
+
go test -vet=off -run=None -fuzz=Fuzz_passString corpustesting/fuzz_testdata_corpus_test.go -fuzztime=10x
|
| 182 |
+
stdout ok
|
| 183 |
+
! stdout FAIL
|
| 184 |
+
|
| 185 |
+
# Fuzzing pass with testdata and f.Add seed corpus
|
| 186 |
+
go test -vet=off -run=Fuzz_passString -fuzz=Fuzz_passString corpustesting/fuzz_testdata_corpus_test.go -fuzztime=10x
|
| 187 |
+
stdout ok
|
| 188 |
+
! stdout FAIL
|
| 189 |
+
|
| 190 |
+
# Test panic with malformed seed corpus
|
| 191 |
+
! go test -vet=off -run Fuzz_fail corpustesting/fuzz_testdata_corpus_test.go
|
| 192 |
+
! stdout ^ok
|
| 193 |
+
stdout FAIL
|
| 194 |
+
|
| 195 |
+
# Test pass with file in other nested testdata directory
|
| 196 |
+
go test -vet=off -run Fuzz_inNestedDir corpustesting/fuzz_testdata_corpus_test.go
|
| 197 |
+
stdout ok
|
| 198 |
+
! stdout FAIL
|
| 199 |
+
! stdout 'fatal here'
|
| 200 |
+
|
| 201 |
+
# Test fails with file containing wrong type
|
| 202 |
+
! go test -vet=off -run Fuzz_wrongType corpustesting/fuzz_testdata_corpus_test.go
|
| 203 |
+
! stdout ^ok
|
| 204 |
+
stdout FAIL
|
| 205 |
+
|
| 206 |
+
-- noop_fuzz_test.go --
|
| 207 |
+
package noop_fuzz
|
| 208 |
+
|
| 209 |
+
import "testing"
|
| 210 |
+
|
| 211 |
+
func Fuzz(f *testing.F) {}
|
| 212 |
+
|
| 213 |
+
-- error_fuzz_test.go --
|
| 214 |
+
package error_fuzz
|
| 215 |
+
|
| 216 |
+
import "testing"
|
| 217 |
+
|
| 218 |
+
func Fuzz(f *testing.F) {
|
| 219 |
+
f.Error("error in target")
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
-- fatal_fuzz_test.go --
|
| 223 |
+
package fatal_fuzz
|
| 224 |
+
|
| 225 |
+
import "testing"
|
| 226 |
+
|
| 227 |
+
func Fuzz(f *testing.F) {
|
| 228 |
+
f.Fatal("fatal in target")
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
-- panic_fuzz_test.go --
|
| 232 |
+
package panic_fuzz
|
| 233 |
+
|
| 234 |
+
import "testing"
|
| 235 |
+
|
| 236 |
+
func Fuzz_panic(f *testing.F) {
|
| 237 |
+
panic(nil)
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
-- success_fuzz_test.go --
|
| 241 |
+
package success_fuzz
|
| 242 |
+
|
| 243 |
+
import "testing"
|
| 244 |
+
|
| 245 |
+
func Fuzz(f *testing.F) {
|
| 246 |
+
f.Fuzz(func (*testing.T, []byte) {})
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
-- skipped_fuzz_test.go --
|
| 250 |
+
package skipped_fuzz
|
| 251 |
+
|
| 252 |
+
import "testing"
|
| 253 |
+
|
| 254 |
+
func Fuzz(f *testing.F) {
|
| 255 |
+
f.Skip()
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
-- fatal_fuzz_fn_fuzz_test.go --
|
| 259 |
+
package fatal_fuzz_fn_fuzz
|
| 260 |
+
|
| 261 |
+
import "testing"
|
| 262 |
+
|
| 263 |
+
func Fuzz(f *testing.F) {
|
| 264 |
+
f.Add([]byte("aa"))
|
| 265 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 266 |
+
f.Fatal("fatal here")
|
| 267 |
+
})
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
-- error_fuzz_fn_fuzz_test.go --
|
| 271 |
+
package error_fuzz_fn_fuzz
|
| 272 |
+
|
| 273 |
+
import "testing"
|
| 274 |
+
|
| 275 |
+
func Fuzz(f *testing.F) {
|
| 276 |
+
f.Add([]byte("aa"))
|
| 277 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 278 |
+
f.Error("error here")
|
| 279 |
+
})
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
-- fail_fuzz_fn_fuzz_test.go --
|
| 283 |
+
package skip_fuzz_fn_fuzz
|
| 284 |
+
|
| 285 |
+
import "testing"
|
| 286 |
+
|
| 287 |
+
func Fuzz(f *testing.F) {
|
| 288 |
+
f.Add([]byte("aa"))
|
| 289 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 290 |
+
f.Fail()
|
| 291 |
+
})
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
-- skip_fuzz_fn_fuzz_test.go --
|
| 295 |
+
package skip_fuzz_fn_fuzz
|
| 296 |
+
|
| 297 |
+
import "testing"
|
| 298 |
+
|
| 299 |
+
func Fuzz(f *testing.F) {
|
| 300 |
+
f.Add([]byte("aa"))
|
| 301 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 302 |
+
f.Skip("skip here")
|
| 303 |
+
})
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
-- skipped_fuzz_fn_fuzz_test.go --
|
| 307 |
+
package skipped_fuzz_fn_fuzz
|
| 308 |
+
|
| 309 |
+
import "testing"
|
| 310 |
+
|
| 311 |
+
func Fuzz(f *testing.F) {
|
| 312 |
+
f.Add([]byte("aa"))
|
| 313 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 314 |
+
t.Logf("t.Skipped is %t\n", t.Skipped())
|
| 315 |
+
t.Logf("f.Skipped is %t\n", f.Skipped())
|
| 316 |
+
})
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
-- goexit_fuzz_fn_fuzz_test.go --
|
| 320 |
+
package goexit_fuzz_fn_fuzz
|
| 321 |
+
|
| 322 |
+
import "testing"
|
| 323 |
+
|
| 324 |
+
func Fuzz(f *testing.F) {
|
| 325 |
+
f.Add([]byte("aa"))
|
| 326 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 327 |
+
runtime.Goexit()
|
| 328 |
+
})
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
-- fatal_after_fuzz_func_fuzz_test.go --
|
| 332 |
+
package fatal_after_fuzz_func_fuzz
|
| 333 |
+
|
| 334 |
+
import "testing"
|
| 335 |
+
|
| 336 |
+
func Fuzz(f *testing.F) {
|
| 337 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 338 |
+
// no-op
|
| 339 |
+
})
|
| 340 |
+
f.Fatal("this shouldn't be called")
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
-- incomplete_fuzz_call_fuzz_test.go --
|
| 344 |
+
package incomplete_fuzz_call_fuzz
|
| 345 |
+
|
| 346 |
+
import "testing"
|
| 347 |
+
|
| 348 |
+
func Fuzz(f *testing.F) {
|
| 349 |
+
f.Fuzz(func(b []byte) {
|
| 350 |
+
// this is missing *testing.T as the first param, so should panic
|
| 351 |
+
})
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
-- cleanup_fuzz_test.go --
|
| 355 |
+
package cleanup_fuzz_test
|
| 356 |
+
|
| 357 |
+
import "testing"
|
| 358 |
+
|
| 359 |
+
func Fuzz(f *testing.F) {
|
| 360 |
+
f.Cleanup(func() {
|
| 361 |
+
panic("failed some precondition")
|
| 362 |
+
})
|
| 363 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 364 |
+
// no-op
|
| 365 |
+
})
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
-- fuzz_add_test.go --
|
| 369 |
+
package fuzz_add
|
| 370 |
+
|
| 371 |
+
import "testing"
|
| 372 |
+
|
| 373 |
+
func add(f *testing.F) {
|
| 374 |
+
f.Helper()
|
| 375 |
+
f.Add([]byte("123"))
|
| 376 |
+
f.Add([]byte("12345"))
|
| 377 |
+
f.Add([]byte(""))
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
func Fuzz_pass(f *testing.F) {
|
| 381 |
+
add(f)
|
| 382 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 383 |
+
if len(b) == -1 {
|
| 384 |
+
t.Fatal("fatal here") // will not be executed
|
| 385 |
+
}
|
| 386 |
+
})
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
func Fuzz_error(f *testing.F) {
|
| 390 |
+
add(f)
|
| 391 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 392 |
+
if len(b) == 3 {
|
| 393 |
+
t.Error("error here")
|
| 394 |
+
}
|
| 395 |
+
})
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
func Fuzz_fatal(f *testing.F) {
|
| 399 |
+
add(f)
|
| 400 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 401 |
+
if len(b) == 0 {
|
| 402 |
+
t.Fatal("fatal here")
|
| 403 |
+
}
|
| 404 |
+
})
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
func Fuzz_panic(f *testing.F) {
|
| 408 |
+
add(f)
|
| 409 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 410 |
+
if len(b) == 5 {
|
| 411 |
+
panic("off by one error")
|
| 412 |
+
}
|
| 413 |
+
})
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
func Fuzz_nilPanic(f *testing.F) {
|
| 417 |
+
add(f)
|
| 418 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 419 |
+
if len(b) == 3 {
|
| 420 |
+
panic(nil)
|
| 421 |
+
}
|
| 422 |
+
})
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
func Fuzz_unsupported(f *testing.F) {
|
| 426 |
+
m := make(map[string]bool)
|
| 427 |
+
f.Add(m)
|
| 428 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
func Fuzz_addDifferentNumber(f *testing.F) {
|
| 432 |
+
f.Add([]byte("a"))
|
| 433 |
+
f.Add([]byte("a"), []byte("b"))
|
| 434 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
func Fuzz_addDifferentType(f *testing.F) {
|
| 438 |
+
f.Add(false)
|
| 439 |
+
f.Add(1234)
|
| 440 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
func Fuzz_wrongType(f *testing.F) {
|
| 444 |
+
f.Add("hello", 50)
|
| 445 |
+
f.Fuzz(func(*testing.T, []byte, int8) {})
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
-- corpustesting/fuzz_testdata_corpus_test.go --
|
| 449 |
+
package fuzz_testdata_corpus
|
| 450 |
+
|
| 451 |
+
import "testing"
|
| 452 |
+
|
| 453 |
+
func fuzzFn(f *testing.F) {
|
| 454 |
+
f.Helper()
|
| 455 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 456 |
+
if string(b) == "12345" {
|
| 457 |
+
t.Fatal("fatal here")
|
| 458 |
+
}
|
| 459 |
+
})
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
func Fuzz_fail(f *testing.F) {
|
| 463 |
+
fuzzFn(f)
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
func Fuzz_pass(f *testing.F) {
|
| 467 |
+
fuzzFn(f)
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
func Fuzz_passString(f *testing.F) {
|
| 471 |
+
f.Add("some seed corpus")
|
| 472 |
+
f.Fuzz(func(*testing.T, string) {})
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
func Fuzz_panic(f *testing.F) {
|
| 476 |
+
f.Fuzz(func(t *testing.T, b []byte) {})
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
func Fuzz_inNestedDir(f *testing.F) {
|
| 480 |
+
f.Fuzz(func(t *testing.T, b []byte) {})
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
func Fuzz_wrongType(f *testing.F) {
|
| 484 |
+
f.Fuzz(func(t *testing.T, b []byte) {})
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
-- corpustesting/testdata/fuzz/Fuzz_fail/1 --
|
| 488 |
+
go test fuzz v1
|
| 489 |
+
[]byte("12345")
|
| 490 |
+
-- corpustesting/testdata/fuzz/Fuzz_pass/1 --
|
| 491 |
+
go test fuzz v1
|
| 492 |
+
[]byte("00000")
|
| 493 |
+
-- corpustesting/testdata/fuzz/Fuzz_passString/1 --
|
| 494 |
+
go test fuzz v1
|
| 495 |
+
string("hello")
|
| 496 |
+
-- corpustesting/testdata/fuzz/Fuzz_panic/1 --
|
| 497 |
+
malformed
|
| 498 |
+
-- corpustesting/testdata/fuzz/Fuzz_inNestedDir/anotherdir/1 --
|
| 499 |
+
go test fuzz v1
|
| 500 |
+
[]byte("12345")
|
| 501 |
+
-- corpustesting/testdata/fuzz/Fuzz_wrongType/1 --
|
| 502 |
+
go test fuzz v1
|
| 503 |
+
int("00000")
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_cache.txt
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz-instrumented] skip
|
| 2 |
+
|
| 3 |
+
[short] skip
|
| 4 |
+
env GOCACHE=$WORK/cache
|
| 5 |
+
|
| 6 |
+
# Fuzz cache should not exist after a regular test run.
|
| 7 |
+
go test .
|
| 8 |
+
exists $GOCACHE
|
| 9 |
+
! exists $GOCACHE/fuzz
|
| 10 |
+
|
| 11 |
+
# Fuzzing should write interesting values to the cache.
|
| 12 |
+
go test -fuzz=FuzzY -fuzztime=100x .
|
| 13 |
+
go run ./contains_files $GOCACHE/fuzz/example.com/y/FuzzY
|
| 14 |
+
|
| 15 |
+
# 'go clean -cache' should not delete the fuzz cache.
|
| 16 |
+
go clean -cache
|
| 17 |
+
exists $GOCACHE/fuzz
|
| 18 |
+
|
| 19 |
+
# 'go clean -fuzzcache' should delete the fuzz cache but not the build cache.
|
| 20 |
+
go build -x ./empty
|
| 21 |
+
stderr '(compile|gccgo)( |\.exe).*empty.go'
|
| 22 |
+
go clean -fuzzcache
|
| 23 |
+
! exists $GOCACHE/fuzz
|
| 24 |
+
go build -x ./empty
|
| 25 |
+
! stderr '(compile|gccgo)( |\.exe).*empty.go'
|
| 26 |
+
|
| 27 |
+
# Fuzzing indicates that one new interesting value was found with an empty
|
| 28 |
+
# corpus, and the total size of the cache is now 1.
|
| 29 |
+
go clean -fuzzcache
|
| 30 |
+
go test -fuzz=FuzzEmpty -fuzztime=10000x .
|
| 31 |
+
stdout 'new interesting: 1'
|
| 32 |
+
stdout 'total: 1'
|
| 33 |
+
|
| 34 |
+
# Fuzzing again with a small fuzztime does not find any other interesting
|
| 35 |
+
# values but still indicates that the cache size is 1.
|
| 36 |
+
go test -fuzz=FuzzEmpty -fuzztime=2x .
|
| 37 |
+
stdout 'new interesting: 0'
|
| 38 |
+
stdout 'total: 1'
|
| 39 |
+
|
| 40 |
+
! go clean -fuzzcache example.com/y
|
| 41 |
+
stderr 'go: clean -fuzzcache cannot be used with package arguments'
|
| 42 |
+
|
| 43 |
+
-- go.mod --
|
| 44 |
+
module example.com/y
|
| 45 |
+
|
| 46 |
+
go 1.16
|
| 47 |
+
-- y_test.go --
|
| 48 |
+
package y
|
| 49 |
+
|
| 50 |
+
import (
|
| 51 |
+
"io"
|
| 52 |
+
"testing"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
func FuzzEmpty(f *testing.F) {
|
| 56 |
+
f.Fuzz(func (*testing.T, []byte) {})
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
func FuzzY(f *testing.F) {
|
| 60 |
+
f.Add([]byte("y"))
|
| 61 |
+
f.Fuzz(func(t *testing.T, b []byte) { Y(io.Discard, b) })
|
| 62 |
+
}
|
| 63 |
+
-- y.go --
|
| 64 |
+
package y
|
| 65 |
+
|
| 66 |
+
import (
|
| 67 |
+
"bytes"
|
| 68 |
+
"io"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
func Y(w io.Writer, b []byte) {
|
| 72 |
+
if !bytes.Equal(b, []byte("y")) {
|
| 73 |
+
w.Write([]byte("not equal"))
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
-- empty/empty.go --
|
| 77 |
+
package empty
|
| 78 |
+
-- contains_files/contains_files.go --
|
| 79 |
+
package main
|
| 80 |
+
|
| 81 |
+
import (
|
| 82 |
+
"fmt"
|
| 83 |
+
"path/filepath"
|
| 84 |
+
"io/ioutil"
|
| 85 |
+
"os"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
func main() {
|
| 89 |
+
infos, err := ioutil.ReadDir(filepath.Clean(os.Args[1]))
|
| 90 |
+
if err != nil {
|
| 91 |
+
fmt.Fprintln(os.Stderr, err)
|
| 92 |
+
os.Exit(1)
|
| 93 |
+
}
|
| 94 |
+
if len(infos) == 0 {
|
| 95 |
+
os.Exit(1)
|
| 96 |
+
}
|
| 97 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_cgo.txt
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[!cgo] skip
|
| 3 |
+
[short] skip
|
| 4 |
+
env GOCACHE=$WORK/cache
|
| 5 |
+
|
| 6 |
+
# Test that fuzzing works with cgo (issue 65169)
|
| 7 |
+
|
| 8 |
+
go test -fuzz=. -fuzztime=1x
|
| 9 |
+
stdout ok
|
| 10 |
+
! stdout FAIL
|
| 11 |
+
|
| 12 |
+
-- go.mod --
|
| 13 |
+
module example.com/p
|
| 14 |
+
|
| 15 |
+
go 1.20
|
| 16 |
+
-- c.go --
|
| 17 |
+
package p
|
| 18 |
+
|
| 19 |
+
import "C"
|
| 20 |
+
-- c_test.go --
|
| 21 |
+
package p
|
| 22 |
+
|
| 23 |
+
import "testing"
|
| 24 |
+
|
| 25 |
+
func Fuzz(f *testing.F) {
|
| 26 |
+
f.Add(0)
|
| 27 |
+
f.Fuzz(func(t *testing.T, x int) {})
|
| 28 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_chatty.txt
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[short] skip
|
| 3 |
+
env GOCACHE=$WORK/cache
|
| 4 |
+
|
| 5 |
+
# Run chatty fuzz targets with an error.
|
| 6 |
+
! go test -v chatty_error_fuzz_test.go
|
| 7 |
+
! stdout '^ok'
|
| 8 |
+
stdout 'FAIL'
|
| 9 |
+
stdout 'error in target'
|
| 10 |
+
|
| 11 |
+
# Run chatty fuzz targets with a fatal.
|
| 12 |
+
! go test -v chatty_fatal_fuzz_test.go
|
| 13 |
+
! stdout '^ok'
|
| 14 |
+
stdout 'FAIL'
|
| 15 |
+
stdout 'fatal in target'
|
| 16 |
+
|
| 17 |
+
# Run chatty fuzz target with a panic
|
| 18 |
+
! go test -v chatty_panic_fuzz_test.go
|
| 19 |
+
! stdout ^ok
|
| 20 |
+
stdout FAIL
|
| 21 |
+
stdout 'this is bad'
|
| 22 |
+
|
| 23 |
+
# Run skipped chatty fuzz targets.
|
| 24 |
+
go test -v chatty_skipped_fuzz_test.go
|
| 25 |
+
stdout ok
|
| 26 |
+
stdout SKIP
|
| 27 |
+
! stdout FAIL
|
| 28 |
+
|
| 29 |
+
# Run successful chatty fuzz targets.
|
| 30 |
+
go test -v chatty_fuzz_test.go
|
| 31 |
+
stdout ok
|
| 32 |
+
stdout PASS
|
| 33 |
+
stdout 'all good here'
|
| 34 |
+
! stdout FAIL
|
| 35 |
+
|
| 36 |
+
# Fuzz successful chatty fuzz target that includes a separate unit test.
|
| 37 |
+
go test -v chatty_with_test_fuzz_test.go -fuzz=Fuzz -fuzztime=1x
|
| 38 |
+
stdout ok
|
| 39 |
+
stdout PASS
|
| 40 |
+
! stdout FAIL
|
| 41 |
+
stdout -count=1 'all good here'
|
| 42 |
+
# Verify that the unit test is only run once.
|
| 43 |
+
stdout -count=1 'logged foo'
|
| 44 |
+
|
| 45 |
+
-- chatty_error_fuzz_test.go --
|
| 46 |
+
package chatty_error_fuzz
|
| 47 |
+
|
| 48 |
+
import "testing"
|
| 49 |
+
|
| 50 |
+
func Fuzz(f *testing.F) {
|
| 51 |
+
f.Error("error in target")
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
-- chatty_fatal_fuzz_test.go --
|
| 55 |
+
package chatty_fatal_fuzz
|
| 56 |
+
|
| 57 |
+
import "testing"
|
| 58 |
+
|
| 59 |
+
func Fuzz(f *testing.F) {
|
| 60 |
+
f.Fatal("fatal in target")
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
-- chatty_panic_fuzz_test.go --
|
| 64 |
+
package chatty_panic_fuzz
|
| 65 |
+
|
| 66 |
+
import "testing"
|
| 67 |
+
|
| 68 |
+
func Fuzz(f *testing.F) {
|
| 69 |
+
panic("this is bad")
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
-- chatty_skipped_fuzz_test.go --
|
| 73 |
+
package chatty_skipped_fuzz
|
| 74 |
+
|
| 75 |
+
import "testing"
|
| 76 |
+
|
| 77 |
+
func Fuzz(f *testing.F) {
|
| 78 |
+
f.Skip()
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
-- chatty_fuzz_test.go --
|
| 82 |
+
package chatty_fuzz
|
| 83 |
+
|
| 84 |
+
import "testing"
|
| 85 |
+
|
| 86 |
+
func Fuzz(f *testing.F) {
|
| 87 |
+
f.Log("all good here")
|
| 88 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
-- chatty_with_test_fuzz_test.go --
|
| 92 |
+
package chatty_with_test_fuzz
|
| 93 |
+
|
| 94 |
+
import "testing"
|
| 95 |
+
|
| 96 |
+
func TestFoo(t *testing.T) {
|
| 97 |
+
t.Log("logged foo")
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
func Fuzz(f *testing.F) {
|
| 101 |
+
f.Log("all good here")
|
| 102 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 103 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_cleanup.txt
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[short] skip
|
| 3 |
+
env GOCACHE=$WORK/cache
|
| 4 |
+
|
| 5 |
+
# Cleanup should run after F.Skip.
|
| 6 |
+
go test -run=FuzzTargetSkip
|
| 7 |
+
stdout cleanup
|
| 8 |
+
|
| 9 |
+
# Cleanup should run after F.Fatal.
|
| 10 |
+
! go test -run=FuzzTargetFatal
|
| 11 |
+
stdout cleanup
|
| 12 |
+
|
| 13 |
+
# Cleanup should run after an unexpected runtime.Goexit.
|
| 14 |
+
! go test -run=FuzzTargetGoexit
|
| 15 |
+
stdout cleanup
|
| 16 |
+
|
| 17 |
+
# Cleanup should run after panic.
|
| 18 |
+
! go test -run=FuzzTargetPanic
|
| 19 |
+
stdout cleanup
|
| 20 |
+
|
| 21 |
+
# Cleanup should run in fuzz function on seed corpus.
|
| 22 |
+
go test -v -run=FuzzFunction
|
| 23 |
+
stdout '(?s)inner.*outer'
|
| 24 |
+
|
| 25 |
+
# TODO(jayconrod): test cleanup while fuzzing. For now, the worker process's
|
| 26 |
+
# stdout and stderr is connected to the coordinator's, but it should eventually
|
| 27 |
+
# be connected to os.DevNull, so we wouldn't see t.Log output.
|
| 28 |
+
|
| 29 |
+
-- go.mod --
|
| 30 |
+
module cleanup
|
| 31 |
+
|
| 32 |
+
go 1.15
|
| 33 |
+
-- cleanup_test.go --
|
| 34 |
+
package cleanup
|
| 35 |
+
|
| 36 |
+
import (
|
| 37 |
+
"runtime"
|
| 38 |
+
"testing"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
func FuzzTargetSkip(f *testing.F) {
|
| 42 |
+
f.Cleanup(func() { f.Log("cleanup") })
|
| 43 |
+
f.Skip()
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
func FuzzTargetFatal(f *testing.F) {
|
| 47 |
+
f.Cleanup(func() { f.Log("cleanup") })
|
| 48 |
+
f.Fatal()
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
func FuzzTargetGoexit(f *testing.F) {
|
| 52 |
+
f.Cleanup(func() { f.Log("cleanup") })
|
| 53 |
+
runtime.Goexit()
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
func FuzzTargetPanic(f *testing.F) {
|
| 57 |
+
f.Cleanup(func() { f.Log("cleanup") })
|
| 58 |
+
panic("oh no")
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
func FuzzFunction(f *testing.F) {
|
| 62 |
+
f.Add([]byte{0})
|
| 63 |
+
f.Cleanup(func() { f.Log("outer") })
|
| 64 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 65 |
+
t.Cleanup(func() { t.Logf("inner") })
|
| 66 |
+
})
|
| 67 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_context.txt
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[short] skip
|
| 3 |
+
env GOCACHE=$WORK/cache
|
| 4 |
+
|
| 5 |
+
# Test fuzz.Context.
|
| 6 |
+
go test -vet=off context_fuzz_test.go
|
| 7 |
+
stdout ^ok
|
| 8 |
+
! stdout FAIL
|
| 9 |
+
|
| 10 |
+
go test -vet=off -fuzz=Fuzz -fuzztime=1x context_fuzz_test.go
|
| 11 |
+
stdout ok
|
| 12 |
+
! stdout FAIL
|
| 13 |
+
|
| 14 |
+
-- context_fuzz_test.go --
|
| 15 |
+
package context_fuzz
|
| 16 |
+
|
| 17 |
+
import (
|
| 18 |
+
"context"
|
| 19 |
+
"errors"
|
| 20 |
+
"testing"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
func Fuzz(f *testing.F) {
|
| 24 |
+
ctx := f.Context()
|
| 25 |
+
if err := ctx.Err(); err != nil {
|
| 26 |
+
f.Fatalf("expected non-canceled context, got %v", err)
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
f.Fuzz(func(t *testing.T, data []byte) {
|
| 30 |
+
innerCtx := t.Context()
|
| 31 |
+
if err := innerCtx.Err(); err != nil {
|
| 32 |
+
t.Fatalf("expected inner test to not inherit canceled context, got %v", err)
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
t.Cleanup(func() {
|
| 36 |
+
if !errors.Is(innerCtx.Err(), context.Canceled) {
|
| 37 |
+
t.Fatal("expected context of inner test to be canceled after its fuzz function finished")
|
| 38 |
+
}
|
| 39 |
+
})
|
| 40 |
+
})
|
| 41 |
+
|
| 42 |
+
f.Cleanup(func() {
|
| 43 |
+
if !errors.Is(ctx.Err(), context.Canceled) {
|
| 44 |
+
f.Fatal("expected context canceled before cleanup")
|
| 45 |
+
}
|
| 46 |
+
})
|
| 47 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_cov.txt
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Test that coverage instrumentation is working. Without the instrumentation
|
| 2 |
+
# it is _extremely_ unlikely that the fuzzer would produce this particular
|
| 3 |
+
# input in any reasonable amount of time.
|
| 4 |
+
|
| 5 |
+
[short] skip
|
| 6 |
+
[!fuzz-instrumented] skip
|
| 7 |
+
env GOCACHE=$WORK/cache
|
| 8 |
+
|
| 9 |
+
# TODO(#51484): enabled debugging info to help diagnose a deadlock in the fuzzer
|
| 10 |
+
env GODEBUG=fuzzdebug=1
|
| 11 |
+
! go test -fuzz=FuzzCov -v
|
| 12 |
+
! stderr 'cov instrumentation working'
|
| 13 |
+
|
| 14 |
+
-- go.mod --
|
| 15 |
+
module test
|
| 16 |
+
|
| 17 |
+
-- cov_test.go --
|
| 18 |
+
package cov
|
| 19 |
+
|
| 20 |
+
import "testing"
|
| 21 |
+
|
| 22 |
+
func FuzzCov(f *testing.F) {
|
| 23 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 24 |
+
if len(b) == 8 &&
|
| 25 |
+
b[0] == 'h' &&
|
| 26 |
+
b[1] == 'e' &&
|
| 27 |
+
b[2] == 'l' &&
|
| 28 |
+
b[3] == 'l' &&
|
| 29 |
+
b[4] == 'o' &&
|
| 30 |
+
b[5] == ' ' &&
|
| 31 |
+
b[6] == ':' &&
|
| 32 |
+
b[7] == ')' {
|
| 33 |
+
panic("cov instrumentation working")
|
| 34 |
+
}
|
| 35 |
+
})
|
| 36 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_deadline.txt
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[short] skip
|
| 3 |
+
env GOCACHE=$WORK/cache
|
| 4 |
+
|
| 5 |
+
# Warm up the build cache with GOMAXPROCS unrestricted.
|
| 6 |
+
go test -c -o $devnull
|
| 7 |
+
|
| 8 |
+
# For the fuzzing phase, we reduce GOMAXPROCS to avoid consuming too many
|
| 9 |
+
# resources during the test. Ideally this would just free up resources to run
|
| 10 |
+
# other parallel tests more quickly, but unfortunately it is actually necessary
|
| 11 |
+
# in some 32-bit environments to prevent the fuzzing engine from running out of
|
| 12 |
+
# address space (see https://go.dev/issue/65434).
|
| 13 |
+
env GOMAXPROCS=2
|
| 14 |
+
|
| 15 |
+
# The fuzz function should be able to detect whether -timeout
|
| 16 |
+
# was set with T.Deadline. Note there is no F.Deadline, and
|
| 17 |
+
# there is no timeout while fuzzing, even if -fuzztime is set.
|
| 18 |
+
go test -run=FuzzDeadline -wantdeadline=true # -timeout defaults to 10m
|
| 19 |
+
go test -run=FuzzDeadline -timeout=0 -wantdeadline=false
|
| 20 |
+
! go test -run=FuzzDeadline -timeout=1s -wantdeadline=false
|
| 21 |
+
go test -run=FuzzDeadline -timeout=1s -wantdeadline=true
|
| 22 |
+
go test -fuzz=FuzzDeadline -timeout=0 -fuzztime=1s -wantdeadline=false
|
| 23 |
+
go test -fuzz=FuzzDeadline -timeout=0 -fuzztime=100x -wantdeadline=false
|
| 24 |
+
|
| 25 |
+
-- go.mod --
|
| 26 |
+
module fuzz
|
| 27 |
+
|
| 28 |
+
go 1.16
|
| 29 |
+
-- fuzz_deadline_test.go --
|
| 30 |
+
package fuzz_test
|
| 31 |
+
|
| 32 |
+
import (
|
| 33 |
+
"flag"
|
| 34 |
+
"testing"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
var wantDeadline = flag.Bool("wantdeadline", false, "whether the test should have a deadline")
|
| 38 |
+
|
| 39 |
+
func FuzzDeadline(f *testing.F) {
|
| 40 |
+
f.Add("run once")
|
| 41 |
+
f.Fuzz(func (t *testing.T, _ string) {
|
| 42 |
+
if _, hasDeadline := t.Deadline(); hasDeadline != *wantDeadline {
|
| 43 |
+
t.Fatalf("function got %v; want %v", hasDeadline, *wantDeadline)
|
| 44 |
+
}
|
| 45 |
+
})
|
| 46 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_dup_cache.txt
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[short] skip
|
| 3 |
+
env GOCACHE=$WORK/cache
|
| 4 |
+
|
| 5 |
+
# This test checks that cached corpus loading properly handles duplicate entries (this can
|
| 6 |
+
# happen when a f.Add value has a duplicate entry in the cached corpus.) Duplicate entries
|
| 7 |
+
# should be discarded, and the rest of the cache should be loaded as normal.
|
| 8 |
+
|
| 9 |
+
env GOCACHE=$WORK/cache
|
| 10 |
+
env GODEBUG=fuzzdebug=1
|
| 11 |
+
|
| 12 |
+
mkdir -p $GOCACHE/fuzz/fuzztest/FuzzTarget
|
| 13 |
+
go run ./populate $GOCACHE/fuzz/fuzztest/FuzzTarget
|
| 14 |
+
|
| 15 |
+
go test -fuzz=FuzzTarget -fuzztime=10x .
|
| 16 |
+
stdout 'entries: 5'
|
| 17 |
+
|
| 18 |
+
-- go.mod --
|
| 19 |
+
module fuzztest
|
| 20 |
+
|
| 21 |
+
go 1.17
|
| 22 |
+
|
| 23 |
+
-- fuzz_test.go --
|
| 24 |
+
package fuzz
|
| 25 |
+
|
| 26 |
+
import "testing"
|
| 27 |
+
|
| 28 |
+
func FuzzTarget(f *testing.F) {
|
| 29 |
+
f.Add(int(0))
|
| 30 |
+
f.Fuzz(func(t *testing.T, _ int) {})
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
-- populate/main.go --
|
| 34 |
+
package main
|
| 35 |
+
|
| 36 |
+
import (
|
| 37 |
+
"path/filepath"
|
| 38 |
+
"fmt"
|
| 39 |
+
"os"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
func main() {
|
| 43 |
+
for i := 0; i < 10; i++ {
|
| 44 |
+
b := byte(0)
|
| 45 |
+
if i > 5 {
|
| 46 |
+
b = byte(i)
|
| 47 |
+
}
|
| 48 |
+
tmpl := "go test fuzz v1\nint(%d)\n"
|
| 49 |
+
if err := os.WriteFile(filepath.Join(os.Args[1], fmt.Sprint(i)), []byte(fmt.Sprintf(tmpl, b)), 0777); err != nil {
|
| 50 |
+
panic(err)
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_err_deadlock.txt
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[short] skip
|
| 2 |
+
[!fuzz-instrumented] skip
|
| 3 |
+
|
| 4 |
+
env GOCACHE=$WORK/cache
|
| 5 |
+
! go test -fuzz=FuzzDead -v
|
| 6 |
+
# This is a somewhat inexact check, but since we don't prefix the error with anything
|
| 7 |
+
# and as the error suffix is platform dependent, this is the best we can do. In the
|
| 8 |
+
# deadlock failure case, the test will just deadlock and timeout anyway, so it should
|
| 9 |
+
# be clear that that failure mode is different.
|
| 10 |
+
stdout 'open'
|
| 11 |
+
|
| 12 |
+
-- go.mod --
|
| 13 |
+
module test
|
| 14 |
+
|
| 15 |
+
-- cov_test.go --
|
| 16 |
+
package dead
|
| 17 |
+
|
| 18 |
+
import (
|
| 19 |
+
"os"
|
| 20 |
+
"path/filepath"
|
| 21 |
+
"testing"
|
| 22 |
+
"time"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
func FuzzDead(f *testing.F) {
|
| 26 |
+
go func() {
|
| 27 |
+
c := filepath.Join(os.Getenv("GOCACHE"), "fuzz", "test", "FuzzDead")
|
| 28 |
+
t := time.NewTicker(time.Second)
|
| 29 |
+
for range t.C {
|
| 30 |
+
files, _ := os.ReadDir(c)
|
| 31 |
+
if len(files) > 0 {
|
| 32 |
+
os.RemoveAll(c)
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
}()
|
| 36 |
+
|
| 37 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 38 |
+
if len(b) == 8 &&
|
| 39 |
+
b[0] == 'h' &&
|
| 40 |
+
b[1] == 'e' &&
|
| 41 |
+
b[2] == 'l' &&
|
| 42 |
+
b[3] == 'l' &&
|
| 43 |
+
b[4] == 'o' &&
|
| 44 |
+
b[5] == ' ' &&
|
| 45 |
+
b[6] == ':' &&
|
| 46 |
+
b[7] == ')' {
|
| 47 |
+
return
|
| 48 |
+
}
|
| 49 |
+
})
|
| 50 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_fuzztime.txt
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
skip # a 5s timeout is never going to be reliable (go.dev/issue/72140)
|
| 2 |
+
|
| 3 |
+
[!fuzz] skip
|
| 4 |
+
[short] skip
|
| 5 |
+
env GOCACHE=$WORK/cache
|
| 6 |
+
|
| 7 |
+
# There are no seed values, so 'go test' should finish quickly.
|
| 8 |
+
go test
|
| 9 |
+
|
| 10 |
+
# For the fuzzing phase, we reduce GOMAXPROCS to avoid consuming too many
|
| 11 |
+
# resources during the test. Ideally this would just free up resources to run
|
| 12 |
+
# other parallel tests more quickly, but unfortunately it is actually necessary
|
| 13 |
+
# in some 32-bit environments to prevent the fuzzing engine from running out of
|
| 14 |
+
# address space (see https://go.dev/issue/65434).
|
| 15 |
+
env GOMAXPROCS=2
|
| 16 |
+
|
| 17 |
+
# Fuzzing should exit 0 after fuzztime, even if timeout is short.
|
| 18 |
+
go test -timeout=3s -fuzz=FuzzFast -fuzztime=5s
|
| 19 |
+
|
| 20 |
+
# We should see the same behavior when invoking the test binary directly.
|
| 21 |
+
go test -c
|
| 22 |
+
exec ./fuzz.test$GOEXE -test.timeout=3s -test.fuzz=FuzzFast -test.fuzztime=5s -test.parallel=1 -test.fuzzcachedir=$WORK/cache
|
| 23 |
+
|
| 24 |
+
# Timeout should not cause inputs to be written as crashers.
|
| 25 |
+
! exists testdata/fuzz
|
| 26 |
+
|
| 27 |
+
# When we use fuzztime with an "x" suffix, it runs a specific number of times.
|
| 28 |
+
# This fuzz function creates a file with a unique name ($pid.$count) on each
|
| 29 |
+
# run. We count the files to find the number of runs.
|
| 30 |
+
mkdir count
|
| 31 |
+
go test -fuzz=FuzzTestCount -fuzztime=1000x -fuzzminimizetime=1x
|
| 32 |
+
go run check_file_count.go count 1000
|
| 33 |
+
|
| 34 |
+
# When we use fuzzminimizetime with an "x" suffix, it runs a specific number of
|
| 35 |
+
# times while minimizing. This fuzz function creates a file with a unique name
|
| 36 |
+
# ($pid.$count) on each run once the first crash has been found. That means that
|
| 37 |
+
# there should be one file for each execution of the fuzz function during
|
| 38 |
+
# minimization, so we count these to determine how many times minimization was
|
| 39 |
+
# run.
|
| 40 |
+
mkdir minimizecount
|
| 41 |
+
! go test -fuzz=FuzzMinimizeCount -fuzzminimizetime=3x -parallel=1
|
| 42 |
+
go run check_file_count.go minimizecount 3
|
| 43 |
+
|
| 44 |
+
-- go.mod --
|
| 45 |
+
module fuzz
|
| 46 |
+
|
| 47 |
+
go 1.16
|
| 48 |
+
-- fuzz_fast_test.go --
|
| 49 |
+
package fuzz_test
|
| 50 |
+
|
| 51 |
+
import "testing"
|
| 52 |
+
|
| 53 |
+
func FuzzFast(f *testing.F) {
|
| 54 |
+
f.Fuzz(func (*testing.T, []byte) {})
|
| 55 |
+
}
|
| 56 |
+
-- fuzz_count_test.go --
|
| 57 |
+
package fuzz
|
| 58 |
+
|
| 59 |
+
import (
|
| 60 |
+
"fmt"
|
| 61 |
+
"os"
|
| 62 |
+
"testing"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
func FuzzTestCount(f *testing.F) {
|
| 66 |
+
pid := os.Getpid()
|
| 67 |
+
n := 0
|
| 68 |
+
f.Fuzz(func(t *testing.T, _ []byte) {
|
| 69 |
+
name := fmt.Sprintf("count/%v.%d", pid, n)
|
| 70 |
+
if err := os.WriteFile(name, nil, 0666); err != nil {
|
| 71 |
+
t.Fatal(err)
|
| 72 |
+
}
|
| 73 |
+
n++
|
| 74 |
+
})
|
| 75 |
+
}
|
| 76 |
+
-- fuzz_minimize_count_test.go --
|
| 77 |
+
package fuzz
|
| 78 |
+
|
| 79 |
+
import (
|
| 80 |
+
"bytes"
|
| 81 |
+
"fmt"
|
| 82 |
+
"os"
|
| 83 |
+
"testing"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
func FuzzMinimizeCount(f *testing.F) {
|
| 87 |
+
pid := os.Getpid()
|
| 88 |
+
n := 0
|
| 89 |
+
seed := bytes.Repeat([]byte("a"), 357)
|
| 90 |
+
f.Add(seed)
|
| 91 |
+
crashFound := false
|
| 92 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 93 |
+
if crashFound {
|
| 94 |
+
name := fmt.Sprintf("minimizecount/%v.%d", pid, n)
|
| 95 |
+
if err := os.WriteFile(name, nil, 0666); err != nil {
|
| 96 |
+
t.Fatal(err)
|
| 97 |
+
}
|
| 98 |
+
n++
|
| 99 |
+
}
|
| 100 |
+
if !bytes.Equal(b, seed) { // this should happen right away
|
| 101 |
+
crashFound = true
|
| 102 |
+
t.Error("minimize this!")
|
| 103 |
+
}
|
| 104 |
+
})
|
| 105 |
+
}
|
| 106 |
+
-- check_file_count.go --
|
| 107 |
+
// +build ignore
|
| 108 |
+
|
| 109 |
+
package main
|
| 110 |
+
|
| 111 |
+
import (
|
| 112 |
+
"fmt"
|
| 113 |
+
"os"
|
| 114 |
+
"strconv"
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
func main() {
|
| 118 |
+
dir, err := os.ReadDir(os.Args[1])
|
| 119 |
+
if err != nil {
|
| 120 |
+
fmt.Fprintln(os.Stderr, err)
|
| 121 |
+
os.Exit(1)
|
| 122 |
+
}
|
| 123 |
+
got := len(dir)
|
| 124 |
+
want, _ := strconv.Atoi(os.Args[2])
|
| 125 |
+
if got != want {
|
| 126 |
+
fmt.Fprintf(os.Stderr, "got %d files; want %d\n", got, want)
|
| 127 |
+
os.Exit(1)
|
| 128 |
+
}
|
| 129 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_io_error.txt
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Test that when the coordinator experiences an I/O error communicating
|
| 2 |
+
# with a worker, the coordinator stops the worker and reports the error.
|
| 3 |
+
# The coordinator should not record a crasher.
|
| 4 |
+
#
|
| 5 |
+
# We simulate an I/O error in the test by writing garbage to fuzz_out.
|
| 6 |
+
# This is unlikely, but possible. It's difficult to simulate interruptions
|
| 7 |
+
# due to ^C and EOF errors which are more common. We don't report those.
|
| 8 |
+
[short] skip
|
| 9 |
+
[!fuzz] skip
|
| 10 |
+
env GOCACHE=$WORK/cache
|
| 11 |
+
|
| 12 |
+
# If the I/O error occurs before F.Fuzz is called, the coordinator should
|
| 13 |
+
# stop the worker and say that.
|
| 14 |
+
! go test -fuzz=FuzzClosePipeBefore -parallel=1
|
| 15 |
+
stdout '\s*fuzzing process terminated without fuzzing:'
|
| 16 |
+
! stdout 'communicating with fuzzing process'
|
| 17 |
+
! exists testdata
|
| 18 |
+
|
| 19 |
+
# If the I/O error occurs after F.Fuzz is called (unlikely), just exit.
|
| 20 |
+
# It's hard to distinguish this case from the worker being interrupted by ^C
|
| 21 |
+
# or exiting with status 0 (which it should do when interrupted by ^C).
|
| 22 |
+
! go test -fuzz=FuzzClosePipeAfter -parallel=1
|
| 23 |
+
stdout '^\s*communicating with fuzzing process: invalid character ''!'' looking for beginning of value$'
|
| 24 |
+
! exists testdata
|
| 25 |
+
|
| 26 |
+
-- go.mod --
|
| 27 |
+
module test
|
| 28 |
+
|
| 29 |
+
go 1.17
|
| 30 |
+
-- io_error_test.go --
|
| 31 |
+
package io_error
|
| 32 |
+
|
| 33 |
+
import (
|
| 34 |
+
"flag"
|
| 35 |
+
"testing"
|
| 36 |
+
"time"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
func isWorker() bool {
|
| 40 |
+
f := flag.Lookup("test.fuzzworker")
|
| 41 |
+
if f == nil {
|
| 42 |
+
return false
|
| 43 |
+
}
|
| 44 |
+
get, ok := f.Value.(flag.Getter)
|
| 45 |
+
if !ok {
|
| 46 |
+
return false
|
| 47 |
+
}
|
| 48 |
+
return get.Get() == interface{}(true)
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
func FuzzClosePipeBefore(f *testing.F) {
|
| 52 |
+
if isWorker() {
|
| 53 |
+
sendGarbageToCoordinator(f)
|
| 54 |
+
time.Sleep(3600 * time.Second) // pause until coordinator terminates the process
|
| 55 |
+
}
|
| 56 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
func FuzzClosePipeAfter(f *testing.F) {
|
| 60 |
+
f.Fuzz(func(t *testing.T, _ []byte) {
|
| 61 |
+
if isWorker() {
|
| 62 |
+
sendGarbageToCoordinator(t)
|
| 63 |
+
time.Sleep(3600 * time.Second) // pause until coordinator terminates the process
|
| 64 |
+
}
|
| 65 |
+
})
|
| 66 |
+
}
|
| 67 |
+
-- io_error_windows_test.go --
|
| 68 |
+
package io_error
|
| 69 |
+
|
| 70 |
+
import (
|
| 71 |
+
"fmt"
|
| 72 |
+
"os"
|
| 73 |
+
"testing"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
func sendGarbageToCoordinator(tb testing.TB) {
|
| 77 |
+
v := os.Getenv("GO_TEST_FUZZ_WORKER_HANDLES")
|
| 78 |
+
var fuzzInFD, fuzzOutFD uintptr
|
| 79 |
+
if _, err := fmt.Sscanf(v, "%x,%x", &fuzzInFD, &fuzzOutFD); err != nil {
|
| 80 |
+
tb.Fatalf("parsing GO_TEST_FUZZ_WORKER_HANDLES: %v", err)
|
| 81 |
+
}
|
| 82 |
+
f := os.NewFile(fuzzOutFD, "fuzz_out")
|
| 83 |
+
if _, err := f.Write([]byte("!!")); err != nil {
|
| 84 |
+
tb.Fatalf("writing fuzz_out: %v", err)
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
-- io_error_notwindows_test.go --
|
| 88 |
+
// +build !windows
|
| 89 |
+
|
| 90 |
+
package io_error
|
| 91 |
+
|
| 92 |
+
import (
|
| 93 |
+
"os"
|
| 94 |
+
"testing"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
func sendGarbageToCoordinator(tb testing.TB) {
|
| 98 |
+
f := os.NewFile(4, "fuzz_out")
|
| 99 |
+
if _, err := f.Write([]byte("!!")); err != nil {
|
| 100 |
+
tb.Fatalf("writing fuzz_out: %v", err)
|
| 101 |
+
}
|
| 102 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_limit_dup_entry.txt
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[short] skip
|
| 3 |
+
env GOCACHE=$WORK/cache
|
| 4 |
+
|
| 5 |
+
# FuzzA attempts to cause the mutator to create duplicate inputs that generate
|
| 6 |
+
# new coverage. Previously this would trigger a corner case when the fuzzer
|
| 7 |
+
# had an execution limit, causing it to deadlock and sit in the coordinator
|
| 8 |
+
# loop indefinitely, failing to exit once the limit had been exhausted.
|
| 9 |
+
|
| 10 |
+
go test -fuzz=FuzzA -fuzztime=100x -parallel=1
|
| 11 |
+
|
| 12 |
+
-- go.mod --
|
| 13 |
+
module m
|
| 14 |
+
|
| 15 |
+
go 1.16
|
| 16 |
+
-- fuzz_test.go --
|
| 17 |
+
package fuzz_test
|
| 18 |
+
|
| 19 |
+
import (
|
| 20 |
+
"fmt"
|
| 21 |
+
"testing"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
func FuzzA(f *testing.F) {
|
| 25 |
+
f.Add([]byte("seed"))
|
| 26 |
+
i := 0
|
| 27 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 28 |
+
i++
|
| 29 |
+
if string(b) == "seed" {
|
| 30 |
+
if i == 0 {
|
| 31 |
+
fmt.Println("a")
|
| 32 |
+
} else if i > 1 {
|
| 33 |
+
fmt.Println("b")
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
})
|
| 37 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_match.txt
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[short] skip
|
| 3 |
+
env GOCACHE=$WORK/cache
|
| 4 |
+
|
| 5 |
+
# Matches only fuzz targets to test.
|
| 6 |
+
go test standalone_fuzz_test.go
|
| 7 |
+
! stdout '^ok.*\[no tests to run\]'
|
| 8 |
+
stdout '^ok'
|
| 9 |
+
|
| 10 |
+
# Matches only for fuzzing.
|
| 11 |
+
go test -fuzz Fuzz -fuzztime 1x standalone_fuzz_test.go
|
| 12 |
+
! stdout '^ok.*\[no tests to run\]'
|
| 13 |
+
stdout '^ok'
|
| 14 |
+
|
| 15 |
+
# Matches none for fuzzing but will run the fuzz target as a test.
|
| 16 |
+
go test -fuzz ThisWillNotMatch -fuzztime 1x standalone_fuzz_test.go
|
| 17 |
+
! stdout '^ok.*no tests to run'
|
| 18 |
+
stdout '^ok'
|
| 19 |
+
stdout 'no fuzz tests to fuzz'
|
| 20 |
+
|
| 21 |
+
[short] stop
|
| 22 |
+
|
| 23 |
+
# Matches only fuzz targets to test with -run.
|
| 24 |
+
go test -run Fuzz standalone_fuzz_test.go
|
| 25 |
+
! stdout '^ok.*\[no tests to run\]'
|
| 26 |
+
stdout '^ok'
|
| 27 |
+
|
| 28 |
+
# Matches no fuzz targets.
|
| 29 |
+
go test -run ThisWillNotMatch standalone_fuzz_test.go
|
| 30 |
+
stdout '^ok.*no tests to run'
|
| 31 |
+
! stdout 'no fuzz tests to fuzz'
|
| 32 |
+
|
| 33 |
+
-- standalone_fuzz_test.go --
|
| 34 |
+
package standalone_fuzz
|
| 35 |
+
|
| 36 |
+
import "testing"
|
| 37 |
+
|
| 38 |
+
func Fuzz(f *testing.F) {
|
| 39 |
+
f.Fuzz(func (*testing.T, []byte) {})
|
| 40 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_minimize.txt
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
[short] skip
|
| 3 |
+
|
| 4 |
+
# We clean the fuzz cache during this test. Don't clean the user's cache.
|
| 5 |
+
env GOCACHE=$WORK/gocache
|
| 6 |
+
|
| 7 |
+
# Test that fuzzminimizetime cannot be negative seconds
|
| 8 |
+
! go test -fuzz=FuzzMinimizerRecoverable -run=FuzzMinimizerRecoverable -fuzztime=10000x -fuzzminimizetime=-1ms .
|
| 9 |
+
! stdout '^ok'
|
| 10 |
+
! stdout 'contains a non-zero byte'
|
| 11 |
+
stdout 'invalid duration'
|
| 12 |
+
stdout FAIL
|
| 13 |
+
|
| 14 |
+
# Test that fuzzminimizetime cannot be negative times
|
| 15 |
+
! go test -fuzz=FuzzMinimizerRecoverable -run=FuzzMinimizerRecoverable -fuzztime=10000x -fuzzminimizetime=-1x .
|
| 16 |
+
! stdout '^ok'
|
| 17 |
+
! stdout 'contains a non-zero byte'
|
| 18 |
+
stdout 'invalid count'
|
| 19 |
+
stdout FAIL
|
| 20 |
+
|
| 21 |
+
# Test that fuzzminimizetime can be zero seconds, and minimization is disabled
|
| 22 |
+
! go test -fuzz=FuzzMinimizeZeroDurationSet -run=FuzzMinimizeZeroDurationSet -fuzztime=10000x -fuzzminimizetime=0s .
|
| 23 |
+
! stdout '^ok'
|
| 24 |
+
! stdout 'minimizing'
|
| 25 |
+
stdout 'there was an Error'
|
| 26 |
+
stdout FAIL
|
| 27 |
+
|
| 28 |
+
# Test that fuzzminimizetime can be zero times, and minimization is disabled
|
| 29 |
+
! go test -fuzz=FuzzMinimizeZeroLimitSet -run=FuzzMinimizeZeroLimitSet -fuzztime=10000x -fuzzminimizetime=0x .
|
| 30 |
+
! stdout '^ok'
|
| 31 |
+
! stdout 'minimizing'
|
| 32 |
+
stdout -count=1 'there was an Error'
|
| 33 |
+
stdout FAIL
|
| 34 |
+
|
| 35 |
+
# Test that minimization is working for recoverable errors.
|
| 36 |
+
! go test -fuzz=FuzzMinimizerRecoverable -run=FuzzMinimizerRecoverable -fuzztime=10000x .
|
| 37 |
+
! stdout '^ok'
|
| 38 |
+
stdout 'got the minimum size!'
|
| 39 |
+
# The error message that was printed should be for the one written to testdata.
|
| 40 |
+
stdout 'contains a non-zero byte of length 50'
|
| 41 |
+
stdout FAIL
|
| 42 |
+
|
| 43 |
+
# Check that the bytes written to testdata are of length 50 (the minimum size)
|
| 44 |
+
go run ./check_testdata FuzzMinimizerRecoverable 50
|
| 45 |
+
|
| 46 |
+
# Test that re-running the minimized value causes a crash.
|
| 47 |
+
! go test -run=FuzzMinimizerRecoverable .
|
| 48 |
+
rm testdata
|
| 49 |
+
|
| 50 |
+
# Test that minimization is working for recoverable errors. Run it with -v this
|
| 51 |
+
# time to ensure the command line output still looks right.
|
| 52 |
+
! go test -v -fuzz=FuzzMinimizerRecoverable -run=FuzzMinimizerRecoverable -fuzztime=10000x .
|
| 53 |
+
! stdout '^ok'
|
| 54 |
+
stdout 'got the minimum size!'
|
| 55 |
+
# The error message that was printed should be for the one written to testdata.
|
| 56 |
+
stdout 'contains a non-zero byte of length 50'
|
| 57 |
+
stdout FAIL
|
| 58 |
+
|
| 59 |
+
# Check that the bytes written to testdata are of length 50 (the minimum size)
|
| 60 |
+
go run ./check_testdata FuzzMinimizerRecoverable 50
|
| 61 |
+
|
| 62 |
+
# Test that re-running the minimized value causes a crash.
|
| 63 |
+
! go test -run=FuzzMinimizerRecoverable .
|
| 64 |
+
rm testdata
|
| 65 |
+
|
| 66 |
+
# Test that minimization doesn't run for non-recoverable errors.
|
| 67 |
+
! go test -fuzz=FuzzMinimizerNonrecoverable -run=FuzzMinimizerNonrecoverable -fuzztime=10000x .
|
| 68 |
+
! stdout '^ok'
|
| 69 |
+
! stdout 'minimizing'
|
| 70 |
+
stdout -count=1 '^\s+fuzzing process hung or terminated unexpectedly: exit status 99'
|
| 71 |
+
stdout FAIL
|
| 72 |
+
|
| 73 |
+
# Check that re-running the value causes a crash.
|
| 74 |
+
! go test -run=FuzzMinimizerNonrecoverable .
|
| 75 |
+
rm testdata
|
| 76 |
+
|
| 77 |
+
# Clear the fuzzing cache. There may already be minimized inputs that would
|
| 78 |
+
# interfere with the next stage of the test.
|
| 79 |
+
go clean -fuzzcache
|
| 80 |
+
|
| 81 |
+
# Test that minimization can be cancelled by fuzzminimizetime and the latest
|
| 82 |
+
# crash will still be logged and written to testdata.
|
| 83 |
+
! go test -fuzz=FuzzMinimizerRecoverable -run=FuzzMinimizerRecoverable -fuzztime=100x -fuzzminimizetime=1x .
|
| 84 |
+
! stdout '^ok'
|
| 85 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzMinimizerRecoverable[/\\]'
|
| 86 |
+
! stdout 'got the minimum size!' # it shouldn't have had enough time to minimize it
|
| 87 |
+
stdout FAIL
|
| 88 |
+
|
| 89 |
+
# Test that re-running the unminimized value causes a crash.
|
| 90 |
+
! go test -run=FuzzMinimizerRecoverable .
|
| 91 |
+
|
| 92 |
+
# TODO(jayconrod,katiehockman): add a test which verifies that the right bytes
|
| 93 |
+
# are written to testdata in the case of an interrupt during minimization.
|
| 94 |
+
|
| 95 |
+
-- go.mod --
|
| 96 |
+
module example.com/y
|
| 97 |
+
|
| 98 |
+
go 1.16
|
| 99 |
+
-- y_test.go --
|
| 100 |
+
package y
|
| 101 |
+
|
| 102 |
+
import (
|
| 103 |
+
"os"
|
| 104 |
+
"testing"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
func FuzzMinimizeZeroDurationSet(f *testing.F) {
|
| 108 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 109 |
+
if len(b) > 5 {
|
| 110 |
+
t.Errorf("there was an Error")
|
| 111 |
+
}
|
| 112 |
+
})
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
func FuzzMinimizeZeroLimitSet(f *testing.F) {
|
| 116 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 117 |
+
if len(b) > 5 {
|
| 118 |
+
t.Errorf("there was an Error")
|
| 119 |
+
}
|
| 120 |
+
})
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
func FuzzMinimizerRecoverable(f *testing.F) {
|
| 124 |
+
f.Add(make([]byte, 100))
|
| 125 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 126 |
+
if len(b) < 50 {
|
| 127 |
+
// Make sure that b is large enough that it can be minimized
|
| 128 |
+
return
|
| 129 |
+
}
|
| 130 |
+
// Given the randomness of the mutations, this should allow the
|
| 131 |
+
// minimizer to trim down the value a bit.
|
| 132 |
+
for _, n := range b {
|
| 133 |
+
if n != 0 {
|
| 134 |
+
if len(b) == 50 {
|
| 135 |
+
t.Log("got the minimum size!")
|
| 136 |
+
}
|
| 137 |
+
t.Fatalf("contains a non-zero byte of length %d", len(b))
|
| 138 |
+
}
|
| 139 |
+
}
|
| 140 |
+
})
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
func FuzzMinimizerNonrecoverable(f *testing.F) {
|
| 144 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 145 |
+
os.Exit(99)
|
| 146 |
+
})
|
| 147 |
+
}
|
| 148 |
+
-- empty/empty.go --
|
| 149 |
+
package empty
|
| 150 |
+
-- check_testdata/check_testdata.go --
|
| 151 |
+
package main
|
| 152 |
+
|
| 153 |
+
import (
|
| 154 |
+
"bytes"
|
| 155 |
+
"fmt"
|
| 156 |
+
"io/ioutil"
|
| 157 |
+
"os"
|
| 158 |
+
"path/filepath"
|
| 159 |
+
"strconv"
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
func main() {
|
| 163 |
+
target := os.Args[1]
|
| 164 |
+
numBytes, err := strconv.Atoi(os.Args[2])
|
| 165 |
+
if err != nil {
|
| 166 |
+
fmt.Fprintln(os.Stderr, err)
|
| 167 |
+
os.Exit(1)
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// Open the file in testdata (there should only be one)
|
| 171 |
+
dir := fmt.Sprintf("testdata/fuzz/%s", target)
|
| 172 |
+
files, err := ioutil.ReadDir(dir)
|
| 173 |
+
if err != nil {
|
| 174 |
+
fmt.Fprintln(os.Stderr, err)
|
| 175 |
+
os.Exit(1)
|
| 176 |
+
}
|
| 177 |
+
if len(files) != 1 {
|
| 178 |
+
fmt.Fprintf(os.Stderr, "expected one file, got %d", len(files))
|
| 179 |
+
os.Exit(1)
|
| 180 |
+
}
|
| 181 |
+
got, err := ioutil.ReadFile(filepath.Join(dir, files[0].Name()))
|
| 182 |
+
if err != nil {
|
| 183 |
+
fmt.Fprintln(os.Stderr, err)
|
| 184 |
+
os.Exit(1)
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
// Trim the newline at the end of the file
|
| 188 |
+
got = bytes.TrimSpace(got)
|
| 189 |
+
|
| 190 |
+
// Make sure that there were exactly 100 bytes written to the corpus entry
|
| 191 |
+
prefix := []byte("[]byte(")
|
| 192 |
+
i := bytes.Index(got, prefix)
|
| 193 |
+
gotBytes := got[i+len(prefix) : len(got)-1]
|
| 194 |
+
s, err := strconv.Unquote(string(gotBytes))
|
| 195 |
+
if err != nil {
|
| 196 |
+
fmt.Fprintln(os.Stderr, err)
|
| 197 |
+
os.Exit(1)
|
| 198 |
+
}
|
| 199 |
+
if want, got := numBytes, len(s); want != got {
|
| 200 |
+
fmt.Fprintf(os.Stderr, "want %d bytes, got %d\n", want, got)
|
| 201 |
+
os.Exit(1)
|
| 202 |
+
}
|
| 203 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_minimize_dirty_cov.txt
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Test that minimization doesn't use dirty coverage snapshots when it
|
| 2 |
+
# is unable to actually minimize the input. We do this by checking that
|
| 3 |
+
# an expected value appears in the cache. If a dirty coverage map is used
|
| 4 |
+
# (i.e. the coverage map generated during the last minimization step,
|
| 5 |
+
# rather than the map provided with the initial input) then this value
|
| 6 |
+
# is unlikely to appear in the cache, since the map generated during
|
| 7 |
+
# the last minimization step should not increase the coverage.
|
| 8 |
+
|
| 9 |
+
[short] skip
|
| 10 |
+
[!fuzz-instrumented] skip
|
| 11 |
+
|
| 12 |
+
env GOCACHE=$WORK/gocache
|
| 13 |
+
go test -fuzz=FuzzCovMin -fuzztime=500000x -test.fuzzcachedir=$GOCACHE/fuzz
|
| 14 |
+
go run check_file/main.go $GOCACHE/fuzz/FuzzCovMin ab
|
| 15 |
+
|
| 16 |
+
-- go.mod --
|
| 17 |
+
module test
|
| 18 |
+
|
| 19 |
+
-- covmin_test.go --
|
| 20 |
+
package covmin
|
| 21 |
+
|
| 22 |
+
import "testing"
|
| 23 |
+
|
| 24 |
+
func FuzzCovMin(f *testing.F) {
|
| 25 |
+
f.Add([]byte("aa"))
|
| 26 |
+
f.Fuzz(func(t *testing.T, data []byte) {
|
| 27 |
+
if len(data) == 2 && data[0] == 'a' && data[1] == 'b' {
|
| 28 |
+
return
|
| 29 |
+
}
|
| 30 |
+
})
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
-- check_file/main.go --
|
| 34 |
+
package main
|
| 35 |
+
|
| 36 |
+
import (
|
| 37 |
+
"bytes"
|
| 38 |
+
"fmt"
|
| 39 |
+
"os"
|
| 40 |
+
"path/filepath"
|
| 41 |
+
"regexp"
|
| 42 |
+
"strconv"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
func checkFile(name, expected string) (bool, error) {
|
| 46 |
+
data, err := os.ReadFile(name)
|
| 47 |
+
if err != nil {
|
| 48 |
+
return false, err
|
| 49 |
+
}
|
| 50 |
+
for _, line := range bytes.Split(data, []byte("\n")) {
|
| 51 |
+
m := valRe.FindSubmatch(line)
|
| 52 |
+
if m == nil {
|
| 53 |
+
continue
|
| 54 |
+
}
|
| 55 |
+
fmt.Println(strconv.Unquote(string(m[1])))
|
| 56 |
+
if s, err := strconv.Unquote(string(m[1])); err != nil {
|
| 57 |
+
return false, err
|
| 58 |
+
} else if s == expected {
|
| 59 |
+
return true, nil
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
return false, nil
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
var valRe = regexp.MustCompile(`^\[\]byte\(([^)]+)\)$`)
|
| 66 |
+
|
| 67 |
+
func main() {
|
| 68 |
+
dir, expected := os.Args[1], os.Args[2]
|
| 69 |
+
ents, err := os.ReadDir(dir)
|
| 70 |
+
if err != nil {
|
| 71 |
+
fmt.Fprintln(os.Stderr, err)
|
| 72 |
+
os.Exit(1)
|
| 73 |
+
}
|
| 74 |
+
for _, ent := range ents {
|
| 75 |
+
name := filepath.Join(dir, ent.Name())
|
| 76 |
+
if good, err := checkFile(name, expected); err != nil {
|
| 77 |
+
fmt.Fprintln(os.Stderr, err)
|
| 78 |
+
os.Exit(1)
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[short] skip
|
| 2 |
+
[!fuzz-instrumented] skip
|
| 3 |
+
|
| 4 |
+
# Test that when an interesting value is discovered (one that expands coverage),
|
| 5 |
+
# the fuzzing engine minimizes it before writing it to the cache.
|
| 6 |
+
#
|
| 7 |
+
# The program below starts with a seed value of length 100, but more coverage
|
| 8 |
+
# will be found for any value other than the seed. We should end with a value
|
| 9 |
+
# in the cache of length 1 (the minimizer currently does not produce empty
|
| 10 |
+
# strings). check_cache.go confirms that.
|
| 11 |
+
#
|
| 12 |
+
# We would like to verify that ALL values in the cache were minimized to a
|
| 13 |
+
# length of 1, but this isn't always possible when new coverage is found in
|
| 14 |
+
# functions called by testing or internal/fuzz in the background.
|
| 15 |
+
|
| 16 |
+
go test -c -fuzz=. # Build using shared build cache for speed.
|
| 17 |
+
env GOCACHE=$WORK/gocache
|
| 18 |
+
exec ./fuzz.test$GOEXE -test.fuzzcachedir=$GOCACHE/fuzz -test.fuzz=FuzzMinCache -test.fuzztime=1000x
|
| 19 |
+
go run check_cache/check_cache.go $GOCACHE/fuzz/FuzzMinCache
|
| 20 |
+
|
| 21 |
+
# Test that minimization occurs for a crash that appears while minimizing a
|
| 22 |
+
# newly found interesting input. There must be only one worker for this test to
|
| 23 |
+
# be flaky like we want.
|
| 24 |
+
! exec ./fuzz.test$GOEXE -test.fuzzcachedir=$GOCACHE/fuzz -test.fuzz=FuzzMinimizerCrashInMinimization -test.run=^$ -test.fuzztime=10000x -test.parallel=1
|
| 25 |
+
! stdout '^ok'
|
| 26 |
+
stdout -count=1 'got the minimum size!'
|
| 27 |
+
stdout -count=1 'bad input'
|
| 28 |
+
stdout FAIL
|
| 29 |
+
# Check that the input written to testdata will reproduce the error, and is the
|
| 30 |
+
# smallest possible.
|
| 31 |
+
go run check_testdata/check_testdata.go FuzzMinimizerCrashInMinimization 1
|
| 32 |
+
|
| 33 |
+
# Test that a nonrecoverable error that occurs while minimizing an interesting
|
| 34 |
+
# input is reported correctly.
|
| 35 |
+
! exec ./fuzz.test$GOEXE -test.fuzzcachedir=$GOCACHE/fuzz -test.fuzz=FuzzMinimizerNonrecoverableCrashInMinimization -test.run=^$ -test.fuzztime=10000x -test.parallel=1
|
| 36 |
+
! stdout '^ok'
|
| 37 |
+
stdout -count=1 'fuzzing process hung or terminated unexpectedly while minimizing'
|
| 38 |
+
stdout -count=1 'EOF'
|
| 39 |
+
stdout FAIL
|
| 40 |
+
# Check that the input written to testdata will reproduce the error.
|
| 41 |
+
go run check_testdata/check_testdata.go FuzzMinimizerNonrecoverableCrashInMinimization 1
|
| 42 |
+
|
| 43 |
+
-- go.mod --
|
| 44 |
+
module fuzz
|
| 45 |
+
|
| 46 |
+
go 1.17
|
| 47 |
+
-- y.go --
|
| 48 |
+
package fuzz
|
| 49 |
+
|
| 50 |
+
import (
|
| 51 |
+
"bytes"
|
| 52 |
+
"io"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
func Y(w io.Writer, s string) {
|
| 56 |
+
if !bytes.Equal([]byte(s), []byte("y")) {
|
| 57 |
+
w.Write([]byte("not equal"))
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
-- fuzz_test.go --
|
| 61 |
+
package fuzz
|
| 62 |
+
|
| 63 |
+
import (
|
| 64 |
+
"bytes"
|
| 65 |
+
"os"
|
| 66 |
+
"testing"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
func FuzzMinimizerCrashInMinimization(f *testing.F) {
|
| 70 |
+
seed := bytes.Repeat([]byte{255}, 100)
|
| 71 |
+
f.Add(seed)
|
| 72 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 73 |
+
if bytes.Equal(seed, b) {
|
| 74 |
+
return
|
| 75 |
+
}
|
| 76 |
+
t.Error("bad input")
|
| 77 |
+
if len(b) == 1 {
|
| 78 |
+
t.Error("got the minimum size!")
|
| 79 |
+
}
|
| 80 |
+
})
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
var fuzzing bool
|
| 84 |
+
|
| 85 |
+
func FuzzMinimizerNonrecoverableCrashInMinimization(f *testing.F) {
|
| 86 |
+
seed := bytes.Repeat([]byte{255}, 100)
|
| 87 |
+
f.Add(seed)
|
| 88 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 89 |
+
if bytes.Equal(seed, b) {
|
| 90 |
+
return
|
| 91 |
+
} else if len(b) == 1 {
|
| 92 |
+
os.Exit(1)
|
| 93 |
+
}
|
| 94 |
+
})
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
func FuzzMinCache(f *testing.F) {
|
| 98 |
+
seed := bytes.Repeat([]byte("a"), 20)
|
| 99 |
+
f.Add(seed)
|
| 100 |
+
f.Fuzz(func(t *testing.T, buf []byte) {
|
| 101 |
+
if bytes.Equal(buf, seed) {
|
| 102 |
+
return
|
| 103 |
+
}
|
| 104 |
+
})
|
| 105 |
+
}
|
| 106 |
+
-- check_testdata/check_testdata.go --
|
| 107 |
+
//go:build ignore
|
| 108 |
+
// +build ignore
|
| 109 |
+
|
| 110 |
+
// check_testdata.go checks that the string written
|
| 111 |
+
// is not longer than the provided length.
|
| 112 |
+
package main
|
| 113 |
+
|
| 114 |
+
import (
|
| 115 |
+
"bytes"
|
| 116 |
+
"fmt"
|
| 117 |
+
"io/ioutil"
|
| 118 |
+
"os"
|
| 119 |
+
"path/filepath"
|
| 120 |
+
"regexp"
|
| 121 |
+
"strconv"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
func main() {
|
| 125 |
+
wantLen, err := strconv.Atoi(os.Args[2])
|
| 126 |
+
if err != nil {
|
| 127 |
+
fmt.Fprintln(os.Stderr, err)
|
| 128 |
+
os.Exit(1)
|
| 129 |
+
}
|
| 130 |
+
testName := os.Args[1]
|
| 131 |
+
dir := filepath.Join("testdata/fuzz", testName)
|
| 132 |
+
|
| 133 |
+
files, err := ioutil.ReadDir(dir)
|
| 134 |
+
if err != nil {
|
| 135 |
+
fmt.Fprintln(os.Stderr, err)
|
| 136 |
+
os.Exit(1)
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
if len(files) == 0 {
|
| 140 |
+
fmt.Fprintf(os.Stderr, "expect at least one failure to be written to testdata\n")
|
| 141 |
+
os.Exit(1)
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
for _, f := range files {
|
| 145 |
+
data, err := ioutil.ReadFile(filepath.Join(dir, f.Name()))
|
| 146 |
+
if err != nil {
|
| 147 |
+
panic(err)
|
| 148 |
+
}
|
| 149 |
+
var containsVal bool
|
| 150 |
+
for _, line := range bytes.Split(data, []byte("\n")) {
|
| 151 |
+
m := valRe.FindSubmatch(line)
|
| 152 |
+
if m == nil {
|
| 153 |
+
continue
|
| 154 |
+
}
|
| 155 |
+
containsVal = true
|
| 156 |
+
s, err := strconv.Unquote(string(m[1]))
|
| 157 |
+
if err != nil {
|
| 158 |
+
panic(err)
|
| 159 |
+
}
|
| 160 |
+
if len(s) != wantLen {
|
| 161 |
+
fmt.Fprintf(os.Stderr, "expect length %d, got %d (%q)\n", wantLen, len(s), line)
|
| 162 |
+
os.Exit(1)
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
if !containsVal {
|
| 166 |
+
fmt.Fprintln(os.Stderr, "corpus file contained no values")
|
| 167 |
+
os.Exit(1)
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
var valRe = regexp.MustCompile(`^\[\]byte\(([^)]+)\)$`)
|
| 173 |
+
|
| 174 |
+
-- check_cache/check_cache.go --
|
| 175 |
+
//go:build ignore
|
| 176 |
+
// +build ignore
|
| 177 |
+
|
| 178 |
+
// check_cache.go checks that each file in the cached corpus has a []byte
|
| 179 |
+
// of length at most 1. This verifies that at least one cached input is minimized.
|
| 180 |
+
package main
|
| 181 |
+
|
| 182 |
+
import (
|
| 183 |
+
"bytes"
|
| 184 |
+
"fmt"
|
| 185 |
+
"os"
|
| 186 |
+
"path/filepath"
|
| 187 |
+
"regexp"
|
| 188 |
+
"strconv"
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
func main() {
|
| 192 |
+
dir := os.Args[1]
|
| 193 |
+
ents, err := os.ReadDir(dir)
|
| 194 |
+
if err != nil {
|
| 195 |
+
fmt.Fprintln(os.Stderr, err)
|
| 196 |
+
os.Exit(1)
|
| 197 |
+
}
|
| 198 |
+
for _, ent := range ents {
|
| 199 |
+
name := filepath.Join(dir, ent.Name())
|
| 200 |
+
if good, err := checkCacheFile(name); err != nil {
|
| 201 |
+
fmt.Fprintln(os.Stderr, err)
|
| 202 |
+
os.Exit(1)
|
| 203 |
+
} else if good {
|
| 204 |
+
os.Exit(0)
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
fmt.Fprintln(os.Stderr, "no cached inputs were minimized")
|
| 208 |
+
os.Exit(1)
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
func checkCacheFile(name string) (good bool, err error) {
|
| 212 |
+
data, err := os.ReadFile(name)
|
| 213 |
+
if err != nil {
|
| 214 |
+
return false, err
|
| 215 |
+
}
|
| 216 |
+
for _, line := range bytes.Split(data, []byte("\n")) {
|
| 217 |
+
m := valRe.FindSubmatch(line)
|
| 218 |
+
if m == nil {
|
| 219 |
+
continue
|
| 220 |
+
}
|
| 221 |
+
if s, err := strconv.Unquote(string(m[1])); err != nil {
|
| 222 |
+
return false, err
|
| 223 |
+
} else if len(s) <= 1 {
|
| 224 |
+
return true, nil
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
return false, nil
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
var valRe = regexp.MustCompile(`^\[\]byte\(([^)]+)\)$`)
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_multiple.txt
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This test checks that 'go test' prints a reasonable error when fuzzing is
|
| 2 |
+
# enabled, and multiple package or multiple fuzz targets match.
|
| 3 |
+
# TODO(#46312): support fuzzing multiple targets in multiple packages.
|
| 4 |
+
|
| 5 |
+
[!fuzz] skip
|
| 6 |
+
[short] skip
|
| 7 |
+
env GOCACHE=$WORK/cache
|
| 8 |
+
|
| 9 |
+
# With fuzzing disabled, multiple targets can be tested.
|
| 10 |
+
go test ./...
|
| 11 |
+
|
| 12 |
+
# With fuzzing enabled, at most one package may be tested,
|
| 13 |
+
# even if only one package contains fuzz targets.
|
| 14 |
+
! go test -fuzz=. ./...
|
| 15 |
+
stderr '^cannot use -fuzz flag with multiple packages$'
|
| 16 |
+
! go test -fuzz=. ./zero ./one
|
| 17 |
+
stderr '^cannot use -fuzz flag with multiple packages$'
|
| 18 |
+
go test -fuzz=. -fuzztime=1x ./one
|
| 19 |
+
|
| 20 |
+
# With fuzzing enabled, at most one target in the same package may match.
|
| 21 |
+
! go test -fuzz=. ./two
|
| 22 |
+
stdout '^testing: will not fuzz, -fuzz matches more than one fuzz test: \[FuzzOne FuzzTwo\]$'
|
| 23 |
+
go test -fuzz=FuzzTwo -fuzztime=1x ./two
|
| 24 |
+
|
| 25 |
+
-- go.mod --
|
| 26 |
+
module fuzz
|
| 27 |
+
|
| 28 |
+
go 1.18
|
| 29 |
+
-- zero/zero.go --
|
| 30 |
+
package zero
|
| 31 |
+
-- one/one_test.go --
|
| 32 |
+
package one
|
| 33 |
+
|
| 34 |
+
import "testing"
|
| 35 |
+
|
| 36 |
+
func FuzzOne(f *testing.F) {
|
| 37 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 38 |
+
}
|
| 39 |
+
-- two/two_test.go --
|
| 40 |
+
package two
|
| 41 |
+
|
| 42 |
+
import "testing"
|
| 43 |
+
|
| 44 |
+
func FuzzOne(f *testing.F) {
|
| 45 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
func FuzzTwo(f *testing.F) {
|
| 49 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 50 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_mutate_crash.txt
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
|
| 3 |
+
# Tests that a crash caused by a mutator-discovered input writes the bad input
|
| 4 |
+
# to testdata, and fails+reports correctly. This tests the end-to-end behavior
|
| 5 |
+
# of the mutator finding a crash while fuzzing, adding it as a regression test
|
| 6 |
+
# to the seed corpus in testdata, and failing the next time the test is run.
|
| 7 |
+
|
| 8 |
+
[short] skip
|
| 9 |
+
env GOCACHE=$WORK/cache
|
| 10 |
+
|
| 11 |
+
# Running the seed corpus for all of the targets should pass the first
|
| 12 |
+
# time, since nothing in the seed corpus will cause a crash.
|
| 13 |
+
go test
|
| 14 |
+
|
| 15 |
+
# Running the fuzzer should find a crashing input quickly.
|
| 16 |
+
! go test -fuzz=FuzzWithBug -fuzztime=100x -fuzzminimizetime=1000x
|
| 17 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzWithBug[/\\]'
|
| 18 |
+
stdout 'this input caused a crash!'
|
| 19 |
+
go run check_testdata.go FuzzWithBug
|
| 20 |
+
|
| 21 |
+
# Now, the failing bytes should have been added to the seed corpus for
|
| 22 |
+
# the target, and should fail when run without fuzzing.
|
| 23 |
+
! go test
|
| 24 |
+
stdout 'FuzzWithBug/[a-f0-9]{16}'
|
| 25 |
+
stdout 'this input caused a crash!'
|
| 26 |
+
|
| 27 |
+
! go test -run=FuzzWithNilPanic -fuzz=FuzzWithNilPanic -fuzztime=100x -fuzzminimizetime=1000x
|
| 28 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzWithNilPanic[/\\]'
|
| 29 |
+
stdout 'panic called with nil argument|test executed panic.nil. or runtime.Goexit'
|
| 30 |
+
go run check_testdata.go FuzzWithNilPanic
|
| 31 |
+
|
| 32 |
+
! go test -run=FuzzWithGoexit -fuzz=FuzzWithGoexit -fuzztime=100x -fuzzminimizetime=1000x
|
| 33 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzWithGoexit[/\\]'
|
| 34 |
+
stdout 'runtime.Goexit'
|
| 35 |
+
go run check_testdata.go FuzzWithGoexit
|
| 36 |
+
|
| 37 |
+
! go test -run=FuzzWithFail -fuzz=FuzzWithFail -fuzztime=100x -fuzzminimizetime=1000x
|
| 38 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzWithFail[/\\]'
|
| 39 |
+
go run check_testdata.go FuzzWithFail
|
| 40 |
+
|
| 41 |
+
! go test -run=FuzzWithLogFail -fuzz=FuzzWithLogFail -fuzztime=100x -fuzzminimizetime=1000x
|
| 42 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzWithLogFail[/\\]'
|
| 43 |
+
stdout 'logged something'
|
| 44 |
+
go run check_testdata.go FuzzWithLogFail
|
| 45 |
+
|
| 46 |
+
! go test -run=FuzzWithErrorf -fuzz=FuzzWithErrorf -fuzztime=100x -fuzzminimizetime=1000x
|
| 47 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzWithErrorf[/\\]'
|
| 48 |
+
stdout 'errorf was called here'
|
| 49 |
+
go run check_testdata.go FuzzWithErrorf
|
| 50 |
+
|
| 51 |
+
! go test -run=FuzzWithFatalf -fuzz=FuzzWithFatalf -fuzztime=100x -fuzzminimizetime=1000x
|
| 52 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzWithFatalf[/\\]'
|
| 53 |
+
stdout 'fatalf was called here'
|
| 54 |
+
go run check_testdata.go FuzzWithFatalf
|
| 55 |
+
|
| 56 |
+
! go test -run=FuzzWithBadExit -fuzz=FuzzWithBadExit -fuzztime=100x -fuzzminimizetime=1000x
|
| 57 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzWithBadExit[/\\]'
|
| 58 |
+
stdout '^\s+fuzzing process hung or terminated unexpectedly: exit status'
|
| 59 |
+
go run check_testdata.go FuzzWithBadExit
|
| 60 |
+
|
| 61 |
+
! go test -run=FuzzDeadlock -fuzz=FuzzDeadlock -fuzztime=100x -fuzzminimizetime=0x
|
| 62 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzDeadlock[/\\]'
|
| 63 |
+
stdout '^\s+fuzzing process hung or terminated unexpectedly: exit status'
|
| 64 |
+
go run check_testdata.go FuzzDeadlock
|
| 65 |
+
|
| 66 |
+
# Running the fuzzer should find a crashing input quickly for fuzzing two types.
|
| 67 |
+
! go test -run=FuzzWithTwoTypes -fuzz=FuzzWithTwoTypes -fuzztime=100x -fuzzminimizetime=1000x
|
| 68 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzWithTwoTypes[/\\]'
|
| 69 |
+
stdout 'these inputs caused a crash!'
|
| 70 |
+
go run check_testdata.go FuzzWithTwoTypes
|
| 71 |
+
|
| 72 |
+
# Running the fuzzer should find a crashing input quickly for an integer.
|
| 73 |
+
! go test -run=FuzzInt -fuzz=FuzzInt -fuzztime=100x -fuzzminimizetime=1000x
|
| 74 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzInt[/\\]'
|
| 75 |
+
stdout 'this input caused a crash!'
|
| 76 |
+
go run check_testdata.go FuzzInt
|
| 77 |
+
|
| 78 |
+
! go test -run=FuzzUint -fuzz=FuzzUint -fuzztime=100x -fuzzminimizetime=1000x
|
| 79 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzUint[/\\]'
|
| 80 |
+
stdout 'this input caused a crash!'
|
| 81 |
+
go run check_testdata.go FuzzUint
|
| 82 |
+
|
| 83 |
+
# Running the fuzzer should find a crashing input quickly for a bool.
|
| 84 |
+
! go test -run=FuzzBool -fuzz=FuzzBool -fuzztime=100x -fuzzminimizetime=1000x
|
| 85 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzBool[/\\]'
|
| 86 |
+
stdout 'this input caused a crash!'
|
| 87 |
+
go run check_testdata.go FuzzBool
|
| 88 |
+
|
| 89 |
+
# Running the fuzzer should find a crashing input quickly for a float.
|
| 90 |
+
! go test -run=FuzzFloat -fuzz=FuzzFloat -fuzztime=100x -fuzzminimizetime=1000x
|
| 91 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzFloat[/\\]'
|
| 92 |
+
stdout 'this input caused a crash!'
|
| 93 |
+
go run check_testdata.go FuzzFloat
|
| 94 |
+
|
| 95 |
+
# Running the fuzzer should find a crashing input quickly for a byte.
|
| 96 |
+
! go test -run=FuzzByte -fuzz=FuzzByte -fuzztime=100x -fuzzminimizetime=1000x
|
| 97 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzByte[/\\]'
|
| 98 |
+
stdout 'this input caused a crash!'
|
| 99 |
+
go run check_testdata.go FuzzByte
|
| 100 |
+
|
| 101 |
+
# Running the fuzzer should find a crashing input quickly for a rune.
|
| 102 |
+
! go test -run=FuzzRune -fuzz=FuzzRune -fuzztime=100x -fuzzminimizetime=1000x
|
| 103 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzRune[/\\]'
|
| 104 |
+
stdout 'this input caused a crash!'
|
| 105 |
+
go run check_testdata.go FuzzRune
|
| 106 |
+
|
| 107 |
+
# Running the fuzzer should find a crashing input quickly for a string.
|
| 108 |
+
! go test -run=FuzzString -fuzz=FuzzString -fuzztime=100x -fuzzminimizetime=1000x
|
| 109 |
+
stdout 'testdata[/\\]fuzz[/\\]FuzzString[/\\]'
|
| 110 |
+
stdout 'this input caused a crash!'
|
| 111 |
+
go run check_testdata.go FuzzString
|
| 112 |
+
|
| 113 |
+
-- go.mod --
|
| 114 |
+
module m
|
| 115 |
+
|
| 116 |
+
go 1.16
|
| 117 |
+
-- fuzz_crash_test.go --
|
| 118 |
+
package fuzz_crash
|
| 119 |
+
|
| 120 |
+
import (
|
| 121 |
+
"os"
|
| 122 |
+
"runtime"
|
| 123 |
+
"testing"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
func FuzzWithBug(f *testing.F) {
|
| 127 |
+
f.Add([]byte("aa"))
|
| 128 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 129 |
+
if string(b) != "aa" {
|
| 130 |
+
panic("this input caused a crash!")
|
| 131 |
+
}
|
| 132 |
+
})
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
func FuzzWithNilPanic(f *testing.F) {
|
| 136 |
+
f.Add([]byte("aa"))
|
| 137 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 138 |
+
if string(b) != "aa" {
|
| 139 |
+
panic(nil)
|
| 140 |
+
}
|
| 141 |
+
})
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
func FuzzWithGoexit(f *testing.F) {
|
| 145 |
+
f.Add([]byte("aa"))
|
| 146 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 147 |
+
if string(b) != "aa" {
|
| 148 |
+
runtime.Goexit()
|
| 149 |
+
}
|
| 150 |
+
})
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
func FuzzWithFail(f *testing.F) {
|
| 154 |
+
f.Add([]byte("aa"))
|
| 155 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 156 |
+
if string(b) != "aa" {
|
| 157 |
+
t.Fail()
|
| 158 |
+
}
|
| 159 |
+
})
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
func FuzzWithLogFail(f *testing.F) {
|
| 163 |
+
f.Add([]byte("aa"))
|
| 164 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 165 |
+
if string(b) != "aa" {
|
| 166 |
+
t.Log("logged something")
|
| 167 |
+
t.Fail()
|
| 168 |
+
}
|
| 169 |
+
})
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
func FuzzWithErrorf(f *testing.F) {
|
| 173 |
+
f.Add([]byte("aa"))
|
| 174 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 175 |
+
if string(b) != "aa" {
|
| 176 |
+
t.Errorf("errorf was called here")
|
| 177 |
+
}
|
| 178 |
+
})
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
func FuzzWithFatalf(f *testing.F) {
|
| 182 |
+
f.Add([]byte("aa"))
|
| 183 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 184 |
+
if string(b) != "aa" {
|
| 185 |
+
t.Fatalf("fatalf was called here")
|
| 186 |
+
}
|
| 187 |
+
})
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
func FuzzWithBadExit(f *testing.F) {
|
| 191 |
+
f.Add([]byte("aa"))
|
| 192 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 193 |
+
if string(b) != "aa" {
|
| 194 |
+
os.Exit(1)
|
| 195 |
+
}
|
| 196 |
+
})
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
func FuzzDeadlock(f *testing.F) {
|
| 200 |
+
f.Add(int(0))
|
| 201 |
+
f.Fuzz(func(t *testing.T, n int) {
|
| 202 |
+
if n != 0 {
|
| 203 |
+
select {}
|
| 204 |
+
}
|
| 205 |
+
})
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
func FuzzWithTwoTypes(f *testing.F) {
|
| 209 |
+
f.Fuzz(func(t *testing.T, a, b []byte) {
|
| 210 |
+
if len(a) > 0 && len(b) > 0 {
|
| 211 |
+
panic("these inputs caused a crash!")
|
| 212 |
+
}
|
| 213 |
+
})
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
func FuzzInt(f *testing.F) {
|
| 217 |
+
f.Add(0)
|
| 218 |
+
f.Fuzz(func(t *testing.T, a int) {
|
| 219 |
+
if a != 0 {
|
| 220 |
+
panic("this input caused a crash!")
|
| 221 |
+
}
|
| 222 |
+
})
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
func FuzzUint(f *testing.F) {
|
| 226 |
+
f.Add(uint(0))
|
| 227 |
+
f.Fuzz(func(t *testing.T, a uint) {
|
| 228 |
+
if a != 0 {
|
| 229 |
+
panic("this input caused a crash!")
|
| 230 |
+
}
|
| 231 |
+
})
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
func FuzzBool(f *testing.F) {
|
| 235 |
+
f.Add(false)
|
| 236 |
+
f.Fuzz(func(t *testing.T, a bool) {
|
| 237 |
+
if a {
|
| 238 |
+
panic("this input caused a crash!")
|
| 239 |
+
}
|
| 240 |
+
})
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
func FuzzFloat(f *testing.F) {
|
| 244 |
+
f.Fuzz(func(t *testing.T, a float64) {
|
| 245 |
+
if a != 0 {
|
| 246 |
+
panic("this input caused a crash!")
|
| 247 |
+
}
|
| 248 |
+
})
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
func FuzzByte(f *testing.F) {
|
| 252 |
+
f.Add(byte(0))
|
| 253 |
+
f.Fuzz(func(t *testing.T, a byte) {
|
| 254 |
+
if a != 0 {
|
| 255 |
+
panic("this input caused a crash!")
|
| 256 |
+
}
|
| 257 |
+
})
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
func FuzzRune(f *testing.F) {
|
| 261 |
+
f.Add(rune(0))
|
| 262 |
+
f.Fuzz(func(t *testing.T, a rune) {
|
| 263 |
+
if a != 0 {
|
| 264 |
+
panic("this input caused a crash!")
|
| 265 |
+
}
|
| 266 |
+
})
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
func FuzzString(f *testing.F) {
|
| 270 |
+
f.Add("")
|
| 271 |
+
f.Fuzz(func(t *testing.T, a string) {
|
| 272 |
+
if a != "" {
|
| 273 |
+
panic("this input caused a crash!")
|
| 274 |
+
}
|
| 275 |
+
})
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
-- check_testdata.go --
|
| 279 |
+
// +build ignore
|
| 280 |
+
|
| 281 |
+
package main
|
| 282 |
+
|
| 283 |
+
import (
|
| 284 |
+
"bytes"
|
| 285 |
+
"crypto/sha256"
|
| 286 |
+
"fmt"
|
| 287 |
+
"io/ioutil"
|
| 288 |
+
"os"
|
| 289 |
+
"path/filepath"
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
func main() {
|
| 293 |
+
target := os.Args[1]
|
| 294 |
+
dir := filepath.Join("testdata/fuzz", target)
|
| 295 |
+
|
| 296 |
+
files, err := ioutil.ReadDir(dir)
|
| 297 |
+
if err != nil {
|
| 298 |
+
fmt.Fprintln(os.Stderr, err)
|
| 299 |
+
os.Exit(1)
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
if len(files) == 0 {
|
| 303 |
+
fmt.Fprintf(os.Stderr, "expect at least one new mutation to be written to testdata\n")
|
| 304 |
+
os.Exit(1)
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
fname := files[0].Name()
|
| 308 |
+
contents, err := ioutil.ReadFile(filepath.Join(dir, fname))
|
| 309 |
+
if err != nil {
|
| 310 |
+
fmt.Fprintln(os.Stderr, err)
|
| 311 |
+
os.Exit(1)
|
| 312 |
+
}
|
| 313 |
+
if bytes.Equal(contents, []byte("aa")) {
|
| 314 |
+
fmt.Fprintf(os.Stderr, "newly written testdata entry was not mutated\n")
|
| 315 |
+
os.Exit(1)
|
| 316 |
+
}
|
| 317 |
+
// The hash of the bytes in the file should match the filename.
|
| 318 |
+
h := []byte(fmt.Sprintf("%x", sha256.Sum256(contents)))
|
| 319 |
+
if !bytes.HasPrefix(h, []byte(fname)) {
|
| 320 |
+
fmt.Fprintf(os.Stderr, "hash of bytes %q does not match filename %q\n", h, fname)
|
| 321 |
+
os.Exit(1)
|
| 322 |
+
}
|
| 323 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_mutate_fail.txt
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
|
| 3 |
+
# Check that if a worker does not call F.Fuzz or calls F.Fail first,
|
| 4 |
+
# 'go test' exits non-zero and no crasher is recorded.
|
| 5 |
+
|
| 6 |
+
[short] skip
|
| 7 |
+
env GOCACHE=$WORK/cache
|
| 8 |
+
|
| 9 |
+
! go test -fuzz=FuzzReturn
|
| 10 |
+
! exists testdata
|
| 11 |
+
|
| 12 |
+
! go test -fuzz=FuzzSkip
|
| 13 |
+
! exists testdata
|
| 14 |
+
|
| 15 |
+
! go test -fuzz=FuzzFail
|
| 16 |
+
! exists testdata
|
| 17 |
+
|
| 18 |
+
! go test -fuzz=FuzzPanic
|
| 19 |
+
! exists testdata
|
| 20 |
+
|
| 21 |
+
! go test -fuzz=FuzzNilPanic
|
| 22 |
+
! exists testdata
|
| 23 |
+
|
| 24 |
+
! go test -fuzz=FuzzGoexit
|
| 25 |
+
! exists testdata
|
| 26 |
+
|
| 27 |
+
! go test -fuzz=FuzzExit
|
| 28 |
+
! exists testdata
|
| 29 |
+
|
| 30 |
+
-- go.mod --
|
| 31 |
+
module m
|
| 32 |
+
|
| 33 |
+
go 1.17
|
| 34 |
+
-- fuzz_fail_test.go --
|
| 35 |
+
package fuzz_fail
|
| 36 |
+
|
| 37 |
+
import (
|
| 38 |
+
"flag"
|
| 39 |
+
"os"
|
| 40 |
+
"runtime"
|
| 41 |
+
"testing"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
func isWorker() bool {
|
| 45 |
+
f := flag.Lookup("test.fuzzworker")
|
| 46 |
+
if f == nil {
|
| 47 |
+
return false
|
| 48 |
+
}
|
| 49 |
+
get, ok := f.Value.(flag.Getter)
|
| 50 |
+
if !ok {
|
| 51 |
+
return false
|
| 52 |
+
}
|
| 53 |
+
return get.Get() == interface{}(true)
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
func FuzzReturn(f *testing.F) {
|
| 57 |
+
if isWorker() {
|
| 58 |
+
return
|
| 59 |
+
}
|
| 60 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
func FuzzSkip(f *testing.F) {
|
| 64 |
+
if isWorker() {
|
| 65 |
+
f.Skip()
|
| 66 |
+
}
|
| 67 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
func FuzzFail(f *testing.F) {
|
| 71 |
+
if isWorker() {
|
| 72 |
+
f.Fail()
|
| 73 |
+
}
|
| 74 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
func FuzzPanic(f *testing.F) {
|
| 78 |
+
if isWorker() {
|
| 79 |
+
panic("nope")
|
| 80 |
+
}
|
| 81 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
func FuzzNilPanic(f *testing.F) {
|
| 85 |
+
if isWorker() {
|
| 86 |
+
panic(nil)
|
| 87 |
+
}
|
| 88 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
func FuzzGoexit(f *testing.F) {
|
| 92 |
+
if isWorker() {
|
| 93 |
+
runtime.Goexit()
|
| 94 |
+
}
|
| 95 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
func FuzzExit(f *testing.F) {
|
| 99 |
+
if isWorker() {
|
| 100 |
+
os.Exit(99)
|
| 101 |
+
}
|
| 102 |
+
f.Fuzz(func(*testing.T, []byte) {})
|
| 103 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_mutator.txt
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[!fuzz] skip
|
| 2 |
+
|
| 3 |
+
# Test basic fuzzing mutator behavior.
|
| 4 |
+
#
|
| 5 |
+
# fuzz_test.go has two fuzz targets (FuzzA, FuzzB) which both add a seed value.
|
| 6 |
+
# Each fuzz function writes the input to a log file. The coordinator and worker
|
| 7 |
+
# use separate log files. check_logs.go verifies that the coordinator only
|
| 8 |
+
# tests seed values and the worker tests mutated values on the fuzz target.
|
| 9 |
+
|
| 10 |
+
[short] skip
|
| 11 |
+
env GOCACHE=$WORK/cache
|
| 12 |
+
|
| 13 |
+
go test -fuzz=FuzzA -fuzztime=100x -parallel=1 -log=fuzz
|
| 14 |
+
go run check_logs.go fuzz fuzz.worker
|
| 15 |
+
|
| 16 |
+
# TODO(b/181800488): remove -parallel=1, here and below. For now, when a
|
| 17 |
+
# crash is found, all workers keep running, wasting resources and reducing
|
| 18 |
+
# the number of executions available to the minimizer, increasing flakiness.
|
| 19 |
+
|
| 20 |
+
# Test that the mutator is good enough to find several unique mutations.
|
| 21 |
+
! go test -fuzz=FuzzMutator -parallel=1 -fuzztime=100x mutator_test.go
|
| 22 |
+
! stdout '^ok'
|
| 23 |
+
stdout FAIL
|
| 24 |
+
stdout 'mutator found enough unique mutations'
|
| 25 |
+
|
| 26 |
+
-- go.mod --
|
| 27 |
+
module m
|
| 28 |
+
|
| 29 |
+
go 1.16
|
| 30 |
+
-- fuzz_test.go --
|
| 31 |
+
package fuzz_test
|
| 32 |
+
|
| 33 |
+
import (
|
| 34 |
+
"flag"
|
| 35 |
+
"fmt"
|
| 36 |
+
"os"
|
| 37 |
+
"testing"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
var (
|
| 41 |
+
logPath = flag.String("log", "", "path to log file")
|
| 42 |
+
logFile *os.File
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
func TestMain(m *testing.M) {
|
| 46 |
+
flag.Parse()
|
| 47 |
+
var err error
|
| 48 |
+
logFile, err = os.OpenFile(*logPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
|
| 49 |
+
if os.IsExist(err) {
|
| 50 |
+
*logPath += ".worker"
|
| 51 |
+
logFile, err = os.OpenFile(*logPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
|
| 52 |
+
}
|
| 53 |
+
if err != nil {
|
| 54 |
+
fmt.Fprintln(os.Stderr, err)
|
| 55 |
+
os.Exit(1)
|
| 56 |
+
}
|
| 57 |
+
os.Exit(m.Run())
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
func FuzzA(f *testing.F) {
|
| 61 |
+
f.Add([]byte("seed"))
|
| 62 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 63 |
+
fmt.Fprintf(logFile, "FuzzA %q\n", b)
|
| 64 |
+
})
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
func FuzzB(f *testing.F) {
|
| 68 |
+
f.Add([]byte("seed"))
|
| 69 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 70 |
+
fmt.Fprintf(logFile, "FuzzB %q\n", b)
|
| 71 |
+
})
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
-- check_logs.go --
|
| 75 |
+
// +build ignore
|
| 76 |
+
|
| 77 |
+
package main
|
| 78 |
+
|
| 79 |
+
import (
|
| 80 |
+
"bufio"
|
| 81 |
+
"bytes"
|
| 82 |
+
"fmt"
|
| 83 |
+
"io"
|
| 84 |
+
"os"
|
| 85 |
+
"strings"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
func main() {
|
| 89 |
+
coordPath, workerPath := os.Args[1], os.Args[2]
|
| 90 |
+
|
| 91 |
+
coordLog, err := os.Open(coordPath)
|
| 92 |
+
if err != nil {
|
| 93 |
+
fmt.Fprintln(os.Stderr, err)
|
| 94 |
+
os.Exit(1)
|
| 95 |
+
}
|
| 96 |
+
defer coordLog.Close()
|
| 97 |
+
if err := checkCoordLog(coordLog); err != nil {
|
| 98 |
+
fmt.Fprintln(os.Stderr, err)
|
| 99 |
+
os.Exit(1)
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
workerLog, err := os.Open(workerPath)
|
| 103 |
+
if err != nil {
|
| 104 |
+
fmt.Fprintln(os.Stderr, err)
|
| 105 |
+
os.Exit(1)
|
| 106 |
+
}
|
| 107 |
+
defer workerLog.Close()
|
| 108 |
+
if err := checkWorkerLog(workerLog); err != nil {
|
| 109 |
+
fmt.Fprintln(os.Stderr, err)
|
| 110 |
+
os.Exit(1)
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
func checkCoordLog(r io.Reader) error {
|
| 115 |
+
b, err := io.ReadAll(r)
|
| 116 |
+
if err != nil {
|
| 117 |
+
return err
|
| 118 |
+
}
|
| 119 |
+
if string(bytes.TrimSpace(b)) != `FuzzB "seed"` {
|
| 120 |
+
return fmt.Errorf("coordinator: did not test FuzzB seed")
|
| 121 |
+
}
|
| 122 |
+
return nil
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
func checkWorkerLog(r io.Reader) error {
|
| 126 |
+
scan := bufio.NewScanner(r)
|
| 127 |
+
var sawAMutant bool
|
| 128 |
+
for scan.Scan() {
|
| 129 |
+
line := scan.Text()
|
| 130 |
+
if !strings.HasPrefix(line, "FuzzA ") {
|
| 131 |
+
return fmt.Errorf("worker: tested something other than target: %s", line)
|
| 132 |
+
}
|
| 133 |
+
if strings.TrimPrefix(line, "FuzzA ") != `"seed"` {
|
| 134 |
+
sawAMutant = true
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
if err := scan.Err(); err != nil && err != bufio.ErrTooLong {
|
| 138 |
+
return err
|
| 139 |
+
}
|
| 140 |
+
if !sawAMutant {
|
| 141 |
+
return fmt.Errorf("worker: did not test any mutants")
|
| 142 |
+
}
|
| 143 |
+
return nil
|
| 144 |
+
}
|
| 145 |
+
-- mutator_test.go --
|
| 146 |
+
package fuzz_test
|
| 147 |
+
|
| 148 |
+
import (
|
| 149 |
+
"testing"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
// TODO(katiehockman): re-work this test once we have a better fuzzing engine
|
| 153 |
+
// (ie. more mutations, and compiler instrumentation)
|
| 154 |
+
func FuzzMutator(f *testing.F) {
|
| 155 |
+
// TODO(katiehockman): simplify this once we can dedupe crashes (e.g.
|
| 156 |
+
// replace map with calls to panic, and simply count the number of crashes
|
| 157 |
+
// that were added to testdata)
|
| 158 |
+
crashes := make(map[string]bool)
|
| 159 |
+
// No seed corpus initiated
|
| 160 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 161 |
+
crashes[string(b)] = true
|
| 162 |
+
if len(crashes) >= 10 {
|
| 163 |
+
panic("mutator found enough unique mutations")
|
| 164 |
+
}
|
| 165 |
+
})
|
| 166 |
+
}
|
go/src/cmd/internal/fuzztest/testdata/script/test_fuzz_mutator_repeat.txt
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TODO(jayconrod): support shared memory on more platforms.
|
| 2 |
+
[!GOOS:darwin] [!GOOS:linux] [!GOOS:windows] skip
|
| 3 |
+
|
| 4 |
+
# Verify that the fuzzing engine records the actual crashing input, even when
|
| 5 |
+
# a worker process terminates without communicating the crashing input back
|
| 6 |
+
# to the coordinator.
|
| 7 |
+
|
| 8 |
+
[short] skip
|
| 9 |
+
env GOCACHE=$WORK/cache
|
| 10 |
+
|
| 11 |
+
# Start fuzzing. The worker crashes after 100 iterations.
|
| 12 |
+
# The fuzz function writes the crashing input to "want" before exiting.
|
| 13 |
+
# The fuzzing engine reconstructs the crashing input and saves it to testdata.
|
| 14 |
+
! exists want
|
| 15 |
+
! go test -fuzz=. -parallel=1 -fuzztime=110x -fuzzminimizetime=10x -v
|
| 16 |
+
stdout '^\s+fuzzing process hung or terminated unexpectedly: exit status'
|
| 17 |
+
stdout 'Failing input written to testdata'
|
| 18 |
+
|
| 19 |
+
# Run the fuzz target without fuzzing. The fuzz function is called with the
|
| 20 |
+
# crashing input in testdata. The test passes if that input is identical to
|
| 21 |
+
# the one saved in "want".
|
| 22 |
+
exists want
|
| 23 |
+
go test -want=want
|
| 24 |
+
|
| 25 |
+
-- go.mod --
|
| 26 |
+
module fuzz
|
| 27 |
+
|
| 28 |
+
go 1.17
|
| 29 |
+
-- fuzz_test.go --
|
| 30 |
+
package fuzz
|
| 31 |
+
|
| 32 |
+
import (
|
| 33 |
+
"bytes"
|
| 34 |
+
"flag"
|
| 35 |
+
"os"
|
| 36 |
+
"testing"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
var wantFlag = flag.String("want", "", "file containing previous crashing input")
|
| 40 |
+
|
| 41 |
+
func FuzzRepeat(f *testing.F) {
|
| 42 |
+
i := 0
|
| 43 |
+
f.Fuzz(func(t *testing.T, b []byte) {
|
| 44 |
+
i++
|
| 45 |
+
if i == 100 {
|
| 46 |
+
f, err := os.OpenFile("want", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
|
| 47 |
+
if err != nil {
|
| 48 |
+
// Couldn't create the file. Return without crashing, and try
|
| 49 |
+
// again.
|
| 50 |
+
i--
|
| 51 |
+
t.Skip(err)
|
| 52 |
+
}
|
| 53 |
+
if _, err := f.Write(b); err != nil {
|
| 54 |
+
// We already created the file, so if we failed to write it
|
| 55 |
+
// there's not much we can do. The test will fail anyway, but
|
| 56 |
+
// at least make sure the error is logged to stdout.
|
| 57 |
+
t.Fatal(err)
|
| 58 |
+
}
|
| 59 |
+
if err := f.Close(); err != nil {
|
| 60 |
+
t.Fatal(err)
|
| 61 |
+
}
|
| 62 |
+
os.Exit(1) // crash without communicating
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
if *wantFlag != "" {
|
| 66 |
+
want, err := os.ReadFile(*wantFlag)
|
| 67 |
+
if err != nil {
|
| 68 |
+
t.Fatal(err)
|
| 69 |
+
}
|
| 70 |
+
if !bytes.Equal(want, b) {
|
| 71 |
+
t.Fatalf("inputs are not equal!\n got: %q\nwant:%q", b, want)
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
})
|
| 75 |
+
}
|