Skip to content

Commit c42ed47

Browse files
committed
internal/refactor/inline: reject attempts to inline in cgo code
Plus a test. Change-Id: I2d5542ed2c6e0cabae740279d09ceaf0d22b8710 Reviewed-on: https://go-review.googlesource.com/c/tools/+/529877 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent 313150a commit c42ed47

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

internal/refactor/inline/callee.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"go/parser"
1515
"go/token"
1616
"go/types"
17+
"strings"
1718

1819
"golang.org/x/tools/go/ast/astutil"
1920
"golang.org/x/tools/go/types/typeutil"
@@ -322,6 +323,17 @@ func AnalyzeCallee(logf func(string, ...any), fset *token.FileSet, pkg *types.Pa
322323
return true
323324
})
324325

326+
// Reject attempts to inline cgo-generated functions.
327+
for _, obj := range freeObjs {
328+
// There are others (iconst fconst sconst fpvar macro)
329+
// but this is probably sufficient.
330+
if strings.HasPrefix(obj.Name, "_Cfunc_") ||
331+
strings.HasPrefix(obj.Name, "_Ctype_") ||
332+
strings.HasPrefix(obj.Name, "_Cvar_") {
333+
return nil, fmt.Errorf("cannot inline cgo-generated functions")
334+
}
335+
}
336+
325337
// Compact content to just the FuncDecl.
326338
//
327339
// As a space optimization, we don't retain the complete

internal/refactor/inline/inline.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ func Inline(logf func(string, ...any), caller *Caller, callee *Callee) ([]byte,
5757
return nil, fmt.Errorf("internal error: caller syntax positions are inconsistent with file content (did you forget to use FileSet.PositionFor when computing the file name?)")
5858
}
5959

60+
// TODO(adonovan): use go1.21's ast.IsGenerated.
61+
if bytes.Contains(caller.Content, []byte("// Code generated by cmd/cgo; DO NOT EDIT.")) {
62+
return nil, fmt.Errorf("cannot inline calls from files that import \"C\"")
63+
}
64+
6065
res, err := inline(logf, caller, &callee.impl)
6166
if err != nil {
6267
return nil, err
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Test that attempts to inline with caller or callee in a cgo-generated
2+
file are rejected.
3+
4+
-- go.mod --
5+
module testdata
6+
go 1.12
7+
8+
-- a/a.go --
9+
package a
10+
11+
/*
12+
static void f() {}
13+
*/
14+
import "C"
15+
16+
func a() {
17+
C.f() //@ inline(re"f", re"cannot inline cgo-generated functions")
18+
g() //@ inline(re"g", re`cannot inline calls from files that import "C"`)
19+
}
20+
21+
func g() {
22+
println()
23+
}
24+
25+
-- a/a2.go --
26+
package a
27+
28+
func b() {
29+
a() //@ inline(re"a", re"cannot inline cgo-generated functions")
30+
}
31+
32+
func c() {
33+
b() //@ inline(re"b", result)
34+
}
35+
36+
-- result --
37+
package a
38+
39+
func b() {
40+
a() //@ inline(re"a", re"cannot inline cgo-generated functions")
41+
}
42+
43+
func c() {
44+
a() //@ inline(re"b", result)
45+
}

0 commit comments

Comments
 (0)