Skip to content

Commit d1bcb8b

Browse files
adonovangopherbot
authored andcommitted
x/tools: audit "unsafe"
This CL contains a number of small cleanups from auditing all uses of the "unsafe" package (and the reflect.Value.Unsafe* methods) in dependencies of gopls as potential causes of memory corruption. None look actually problematic. Change-Id: I443f29caf00c2d3734a31c5fa496e5296952930c Reviewed-on: https://go-review.googlesource.com/c/tools/+/725940 Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent ac87d84 commit d1bcb8b

File tree

4 files changed

+9
-16
lines changed

4 files changed

+9
-16
lines changed

internal/event/core/export.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"sync/atomic"
1010
"time"
11-
"unsafe"
1211

1312
"golang.org/x/tools/internal/event/label"
1413
)
@@ -17,23 +16,21 @@ import (
1716
// It may return a modified context and event.
1817
type Exporter func(context.Context, Event, label.Map) context.Context
1918

20-
var (
21-
exporter unsafe.Pointer
22-
)
19+
var exporter atomic.Pointer[Exporter]
2320

2421
// SetExporter sets the global exporter function that handles all events.
2522
// The exporter is called synchronously from the event call site, so it should
2623
// return quickly so as not to hold up user code.
2724
func SetExporter(e Exporter) {
28-
p := unsafe.Pointer(&e)
2925
if e == nil {
3026
// &e is always valid, and so p is always valid, but for the early abort
3127
// of ProcessEvent to be efficient it needs to make the nil check on the
3228
// pointer without having to dereference it, so we make the nil function
3329
// also a nil pointer
34-
p = nil
30+
exporter.Store(nil)
31+
} else {
32+
exporter.Store(&e)
3533
}
36-
atomic.StorePointer(&exporter, p)
3734
}
3835

3936
// deliver is called to deliver an event to the supplied exporter.
@@ -48,7 +45,7 @@ func deliver(ctx context.Context, exporter Exporter, ev Event) context.Context {
4845
// Export is called to deliver an event to the global exporter if set.
4946
func Export(ctx context.Context, ev Event) context.Context {
5047
// get the global exporter and abort early if there is not one
51-
exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
48+
exporterPtr := exporter.Load()
5249
if exporterPtr == nil {
5350
return ctx
5451
}
@@ -61,7 +58,7 @@ func Export(ctx context.Context, ev Event) context.Context {
6158
// It will fill in the time.
6259
func ExportPair(ctx context.Context, begin, end Event) (context.Context, func()) {
6360
// get the global exporter and abort early if there is not one
64-
exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
61+
exporterPtr := exporter.Load()
6562
if exporterPtr == nil {
6663
return ctx, func() {}
6764
}

internal/typesinternal/classify_call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"fmt"
99
"go/ast"
1010
"go/types"
11-
_ "unsafe"
11+
_ "unsafe" // for go:linkname hack
1212
)
1313

1414
// CallKind describes the function position of an [*ast.CallExpr].

internal/typesinternal/types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"go/token"
2424
"go/types"
2525
"reflect"
26-
"unsafe"
2726

2827
"golang.org/x/tools/go/ast/inspector"
2928
"golang.org/x/tools/internal/aliases"
@@ -40,8 +39,7 @@ func SetUsesCgo(conf *types.Config) bool {
4039
}
4140
}
4241

43-
addr := unsafe.Pointer(f.UnsafeAddr())
44-
*(*bool)(addr) = true
42+
*(*bool)(f.Addr().UnsafePointer()) = true
4543

4644
return true
4745
}

refactor/satisfy/find.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
// interface, and this fact is necessary for the package to be
99
// well-typed.
1010
//
11-
// THIS PACKAGE IS EXPERIMENTAL AND MAY CHANGE AT ANY TIME.
12-
//
13-
// It is provided only for the gopls tool. It requires well-typed inputs.
11+
// It requires well-typed inputs.
1412
package satisfy // import "golang.org/x/tools/refactor/satisfy"
1513

1614
// NOTES:

0 commit comments

Comments
 (0)