Skip to content

Commit 41d3d15

Browse files
ianlancetaylorandybons
authored andcommitted
cmd/compile: permit go:cgo_import_dynamic anywhere
It's used on Solaris to import symbols from shared libraries, e.g., in golang.org/x/sys/unix and golang.org/x/net/internal/socket. We could use a different directive but that would require build tags in all the places that use it. Updates #23672 Updates #23749 Change-Id: I47fcf72a6d2862e304204705979c2056c2f78ec5 Reviewed-on: https://go-review.googlesource.com/94018 Run-TryBot: Andrew Bonventre <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent f2354d0 commit 41d3d15

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/cmd/compile/internal/gc/noder.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,22 @@ func (p *noder) pragma(pos src.Pos, text string) syntax.Pragma {
13461346
}
13471347
p.linknames = append(p.linknames, linkname{pos, f[1], f[2]})
13481348

1349+
case strings.HasPrefix(text, "go:cgo_import_dynamic "):
1350+
// This is permitted for general use because Solaris
1351+
// code relies on it in golang.org/x/sys/unix and others.
1352+
fields := pragmaFields(text)
1353+
if len(fields) >= 4 {
1354+
lib := strings.Trim(fields[3], `"`)
1355+
if lib != "" && !safeArg(lib) && !isCgoGeneratedFile(pos) {
1356+
p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("invalid library name %q in cgo_import_dynamic directive", lib)})
1357+
}
1358+
p.pragcgobuf += p.pragcgo(pos, text)
1359+
return pragmaValue("go:cgo_import_dynamic")
1360+
}
1361+
fallthrough
13491362
case strings.HasPrefix(text, "go:cgo_"):
1350-
// For security, we disallow //go:cgo_* directives outside cgo-generated files.
1363+
// For security, we disallow //go:cgo_* directives other
1364+
// than cgo_import_dynamic outside cgo-generated files.
13511365
// Exception: they are allowed in the standard library, for runtime and syscall.
13521366
if !isCgoGeneratedFile(pos) && !compiling_std {
13531367
p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in cgo-generated code", text)})
@@ -1383,6 +1397,18 @@ func isCgoGeneratedFile(pos src.Pos) bool {
13831397
return strings.HasPrefix(filepath.Base(filepath.Clean(pos.AbsFilename())), "_cgo_")
13841398
}
13851399

1400+
// safeArg reports whether arg is a "safe" command-line argument,
1401+
// meaning that when it appears in a command-line, it probably
1402+
// doesn't have some special meaning other than its own name.
1403+
// This is copied from SafeArg in cmd/go/internal/load/pkg.go.
1404+
func safeArg(name string) bool {
1405+
if name == "" {
1406+
return false
1407+
}
1408+
c := name[0]
1409+
return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
1410+
}
1411+
13861412
func mkname(sym *types.Sym) *Node {
13871413
n := oldname(sym)
13881414
if n.Name != nil && n.Name.Pack != nil {

src/cmd/go/internal/load/pkg.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
12061206
// GNU binutils flagfile specifiers, sometimes called "response files").
12071207
// To be conservative, we reject almost any arg beginning with non-alphanumeric ASCII.
12081208
// We accept leading . _ and / as likely in file system paths.
1209+
// There is a copy of this function in cmd/compile/internal/gc/noder.go.
12091210
func SafeArg(name string) bool {
12101211
if name == "" {
12111212
return false

0 commit comments

Comments
 (0)