Zvonimir Pavlinovic submitted this change.
vulncheck/internal/gosym: refactor code to match original code
The code within the package is organized so that the slightly modified
original code is placed in files with original names. Newly added code
is placed in files starting with "additions_" prefix.
Some code within original files has small modifications. These are
wrapped with comments starting with "Addition:".
A README file is updated with the above code structure explanations.
Change-Id: I6660bc96cd922c3d3324c0a19f26c10525b889f5
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/468397
Run-TryBot: Zvonimir Pavlinovic <zpavl...@google.com>
TryBot-Result: Gopher Robot <go...@golang.org>
Reviewed-by: Jonathan Amsterdam <j...@google.com>
---
A vulncheck/internal/gosym/README
M vulncheck/internal/gosym/additions.go
M vulncheck/internal/gosym/additions_test.go
M vulncheck/internal/gosym/pclntab.go
D vulncheck/internal/gosym/sym.go
D vulncheck/internal/gosym/sym_test.go
M vulncheck/internal/gosym/symtab.go
7 files changed, 86 insertions(+), 70 deletions(-)
diff --git a/vulncheck/internal/gosym/README b/vulncheck/internal/gosym/README
new file mode 100644
index 0000000..dacb83f
--- /dev/null
+++ b/vulncheck/internal/gosym/README
@@ -0,0 +1,11 @@
+This code is a copied and slightly modified version of go/src/debug/gosym.
+
+The original code contains logic for accessing symbol tables and line numbers
+in Go binaries. The only reason why this is copied is to support inlining.
+
+Code added by vulncheck is located in files with "additions_" prefix and it
+contains logic for accessing inlining information.
+
+Within the originally named files, deleted or added logic is annotated with
+a comment starting with "Addition:". The modified logic allows the inlining
+code in "additions_*" files to access the necessary information.
diff --git a/vulncheck/internal/gosym/additions.go b/vulncheck/internal/gosym/additions.go
index f740d71..f608c15 100644
--- a/vulncheck/internal/gosym/additions.go
+++ b/vulncheck/internal/gosym/additions.go
@@ -7,11 +7,41 @@
import (
"encoding/binary"
"io"
+ "strings"
+
+ sv "golang.org/x/mod/semver"
+ "golang.org/x/vuln/internal/semver"
)
-// Additions to the original package.
+const (
+ funcSymNameGo119Lower string = "go.func.*"
+ funcSymNameGo120 string = "go:func.*"
+)
-// from cmd/internal/objabi/funcdata.go
+// FuncSymName returns symbol name for Go functions
+// used in binaries based on Go version. Supported
+// Go versions are 1.18, 1.19, and 1.20. Otherwise,
+// returns an empty string.
+func FuncSymName(goVersion string) string {
+ // Support devel goX.Y...
+ v := strings.TrimPrefix(goVersion, "devel ")
+ v = semver.GoTagToSemver(v)
+ mm := sv.MajorMinor(v)
+ if mm == "v1.18" || mm == "v1.19" {
+ return funcSymNameGo119Lower
+ } else if mm == "v1.20" {
+ return funcSymNameGo120
+ } else if v == "" && strings.HasPrefix(goVersion, "devel") {
+ // We currently don't have a direct way of mapping
+ // Go versions of the form devel <hash> to semver,
+ // so we map it to the most recent supported major
+ // Go version, which is currently go1.20.
+ return funcSymNameGo120
+ }
+ return ""
+}
+
+// Additions to the original package from cmd/internal/objabi/funcdata.go
const (
pcdata_InlTreeIndex = 2
funcdata_InlTree = 3
diff --git a/vulncheck/internal/gosym/additions_test.go b/vulncheck/internal/gosym/additions_test.go
index 123c458..b922e02 100644
--- a/vulncheck/internal/gosym/additions_test.go
+++ b/vulncheck/internal/gosym/additions_test.go
@@ -13,6 +13,24 @@
"github.com/google/go-cmp/cmp/cmpopts"
)
+func TestFuncSymName(t *testing.T) {
+ for _, test := range []struct {
+ v string
+ want string
+ }{
+ {"go1.18", "go.func.*"},
+ {"go1.19", "go.func.*"},
+ {"devel go1.19", "go.func.*"},
+ {"go1.19-pre4", "go.func.*"},
+ {"go1.20", "go:func.*"},
+ {"devel bd56cb90a72e6725e", "go:func.*"},
+ } {
+ if got := FuncSymName(test.v); got != test.want {
+ t.Errorf("got %s; want %s", got, test.want)
+ }
+ }
+}
+
func TestInlineTree(t *testing.T) {
pclinetestBinary, cleanup := dotest(t)
defer cleanup()
diff --git a/vulncheck/internal/gosym/pclntab.go b/vulncheck/internal/gosym/pclntab.go
index ebc8539..4a7955a 100644
--- a/vulncheck/internal/gosym/pclntab.go
+++ b/vulncheck/internal/gosym/pclntab.go
@@ -312,7 +312,7 @@
f.LineTable = t
f.FrameSize = int(info.deferreturn())
- // Additions.
+ // Additions:
// numFuncField is the number of (32 bit) fields in _func (src/runtime/runtime2.go)
// Note that the last 4 fields are 32 bits combined. This number is 11 for go1.20,
// 10 for earlier versions down to go1.16, and 9 before that.
@@ -483,6 +483,7 @@
if n == 0 || n > 9 {
panic("bad funcdata field")
}
+ // Addition: some code deleted here to support inlining.
off := f.fieldOffset(n)
data := f.data[off:]
return f.t.binary.Uint32(data)
diff --git a/vulncheck/internal/gosym/sym.go b/vulncheck/internal/gosym/sym.go
deleted file mode 100644
index 7d5eb20..0000000
--- a/vulncheck/internal/gosym/sym.go
+++ /dev/null
@@ -1,40 +0,0 @@
-// Copyright 2022 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package gosym
-
-import (
- "strings"
-
- sv "golang.org/x/mod/semver"
- "golang.org/x/vuln/internal/semver"
-)
-
-const (
- funcSymNameGo119Lower string = "go.func.*"
- funcSymNameGo120 string = "go:func.*"
-)
-
-// FuncSymName returns symbol name for Go functions
-// used in binaries based on Go version. Supported
-// Go versions are 1.18, 1.19, and 1.20. Otherwise,
-// returns an empty string.
-func FuncSymName(goVersion string) string {
- // Support devel goX.Y...
- v := strings.TrimPrefix(goVersion, "devel ")
- v = semver.GoTagToSemver(v)
- mm := sv.MajorMinor(v)
- if mm == "v1.18" || mm == "v1.19" {
- return funcSymNameGo119Lower
- } else if mm == "v1.20" {
- return funcSymNameGo120
- } else if v == "" && strings.HasPrefix(goVersion, "devel") {
- // We currently don't have a direct way of mapping
- // Go versions of the form devel <hash> to semver,
- // so we map it to the most recent supported major
- // Go version, which is currently go1.20.
- return funcSymNameGo120
- }
- return ""
-}
diff --git a/vulncheck/internal/gosym/sym_test.go b/vulncheck/internal/gosym/sym_test.go
deleted file mode 100644
index af9882e..0000000
--- a/vulncheck/internal/gosym/sym_test.go
+++ /dev/null
@@ -1,27 +0,0 @@
-// Copyright 2022 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package gosym
-
-import (
- "testing"
-)
-
-func TestFuncSymName(t *testing.T) {
- for _, test := range []struct {
- v string
- want string
- }{
- {"go1.18", "go.func.*"},
- {"go1.19", "go.func.*"},
- {"devel go1.19", "go.func.*"},
- {"go1.19-pre4", "go.func.*"},
- {"go1.20", "go:func.*"},
- {"devel bd56cb90a72e6725e", "go:func.*"},
- } {
- if got := FuncSymName(test.v); got != test.want {
- t.Errorf("got %s; want %s", got, test.want)
- }
- }
-}
diff --git a/vulncheck/internal/gosym/symtab.go b/vulncheck/internal/gosym/symtab.go
index 1d51a25..6281e78 100644
--- a/vulncheck/internal/gosym/symtab.go
+++ b/vulncheck/internal/gosym/symtab.go
@@ -137,6 +137,7 @@
FrameSize int
LineTable *LineTable
Obj *Obj
+ // Addition: extra data to support inlining.
inlTree
}
To view, visit change 468397. To unsubscribe, or for help writing mail filters, visit settings.