[tools] go/analysis/passes/nilness: skip magic cgo functions

1 view
Skip to first unread message

Alan Donovan (Gerrit)

unread,
Jun 26, 2026, 5:42:46 PM (13 hours ago) Jun 26
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Alan Donovan has uploaded the change for review

Commit message

go/analysis/passes/nilness: skip magic cgo functions

The annotation on this function, for example, indicates
that the _cgo_runtime_cgocall function is accessing the
entire stack frame directly, so it may clobber r1 even
though the SSA representation suggests it is always nil.

//go:cgo_unsafe_args
func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
_cgo_runtime_cgocall(_cgo_a202e8d7d4fb_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
if r1 == nil {
runtime_throw("runtime: C malloc failed")
}
return
}

This is required to avoid false positives in the nilness
analysis when it starts reporting nil unsafe.Pointers after
CL 794881 lands.

For golang/go#80121
Change-Id: Ia0824f86a86e18ee6956fda492490a2faf138f12

Change diff

diff --git a/go/analysis/passes/nilness/nilness.go b/go/analysis/passes/nilness/nilness.go
index 6f35396..18a100a 100644
--- a/go/analysis/passes/nilness/nilness.go
+++ b/go/analysis/passes/nilness/nilness.go
@@ -7,6 +7,7 @@
import (
_ "embed"
"fmt"
+ "go/ast"
"go/token"
"go/types"

@@ -14,6 +15,7 @@
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/internal/analysis/analyzerutil"
+ "golang.org/x/tools/internal/astutil"
"golang.org/x/tools/internal/typeparams"
)

@@ -37,6 +39,14 @@
}

func runFunc(pass *analysis.Pass, fn *ssa.Function) {
+ // Skip cgo-generated functions annotated with
+ // //go:cgo_unsafe_args such as _cgo_cmalloc since
+ // they behave in magical ways not captured by the
+ // SSA representation, leading to false positives.
+ if hasCgoUnsafeArgs(fn) {
+ return
+ }
+
reportf := func(category string, pos token.Pos, format string, args ...any) {
// We ignore nil-checking ssa.Instructions
// that don't correspond to syntax.
@@ -495,3 +505,17 @@
}
return false
}
+
+func hasCgoUnsafeArgs(fn *ssa.Function) bool {
+ for ; fn != nil; fn = fn.Parent() {
+ if decl, ok := fn.Syntax().(*ast.FuncDecl); ok && decl != nil {
+ for _, d := range astutil.Directives(decl.Doc) {
+ if d.Tool == "go" && d.Name == "cgo_unsafe_args" {
+ return true
+ }
+ }
+ }
+
+ }
+ return false
+}
diff --git a/go/analysis/passes/nilness/testdata/src/a/a.go b/go/analysis/passes/nilness/testdata/src/a/a.go
index f7caa01..aa26e66 100644
--- a/go/analysis/passes/nilness/testdata/src/a/a.go
+++ b/go/analysis/passes/nilness/testdata/src/a/a.go
@@ -319,3 +319,20 @@
}
f.Close()
}
+
+//go:cgo_unsafe_args
+func f21(ptr *int) {
+ if ptr == nil {
+ print(*ptr) // nope: cgo_unsafe_args means there is magic afoot that SSA cannot see
+ }
+}
+
+//go:cgo_unsafe_args
+func f22(ptr *int) {
+ if ptr == nil {
+ f := func() {
+ print(*ptr) // nope: cgo_unsafe_args means there is magic afoot that SSA cannot see
+ }
+ f()
+ }
+}

Change information

Files:
  • M go/analysis/passes/nilness/nilness.go
  • M go/analysis/passes/nilness/testdata/src/a/a.go
Change size: S
Delta: 2 files changed, 41 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: Ia0824f86a86e18ee6956fda492490a2faf138f12
Gerrit-Change-Number: 794883
Gerrit-PatchSet: 1
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Jun 26, 2026, 5:43:06 PM (13 hours ago) Jun 26
to goph...@pubsubhelper.golang.org, Mark Freeman, Dominik Honnef, golang...@luci-project-accounts.iam.gserviceaccount.com, golang-co...@googlegroups.com
Attention needed from Mark Freeman

Alan Donovan voted Auto-Submit+1

Auto-Submit+1
Open in Gerrit

Related details

Attention is currently required from:
  • Mark Freeman
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: Ia0824f86a86e18ee6956fda492490a2faf138f12
Gerrit-Change-Number: 794883
Gerrit-PatchSet: 1
Gerrit-Owner: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Mark Freeman <markf...@google.com>
Gerrit-CC: Dominik Honnef <dom...@honnef.co>
Gerrit-Attention: Mark Freeman <markf...@google.com>
Gerrit-Comment-Date: Fri, 26 Jun 2026 21:43:03 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages