Skip to content

Commit 6b7b6b5

Browse files
committed
compiler: accept cgo_import_dynamic directives only in column 1
The gc compiler rejects a directive that shares a line with code. It accepts an indented directive on a line of its own. TinyGo uses a simple column 1 check, which is more strict for indented directives. Add tests for the trailing, function line, and indented cases.
1 parent e7e0c65 commit 6b7b6b5

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎compiler/compiler_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,18 @@ func TestDarwinCgoImportDynamic(t *testing.T) {
402402
if strings.Contains(ir, "@bad_remote") {
403403
t.Error("a declaration was created for the remote symbol of a non-uintptr trampoline global")
404404
}
405+
if !strings.Contains(ir, "load i64, ptr @main.libc_misplaced_trampoline_addr") {
406+
t.Error("a directive that shares a line with code was honored")
407+
}
408+
if strings.Contains(ir, "@misplaced_remote") {
409+
t.Error("a declaration was created for the remote symbol of a misplaced directive")
410+
}
411+
if strings.Contains(ir, "@indented_remote") {
412+
t.Error("an indented directive was honored")
413+
}
414+
if strings.Contains(ir, "@brace_remote") {
415+
t.Error("a directive after code on a function line was honored")
416+
}
405417
}
406418

407419
// normalizeIR canonicalizes LLVM-version-specific IR spellings for comparison

‎compiler/symbol.go‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,13 +779,19 @@ func (c *compilerContext) loadASTComments(pkg *loader.Package) {
779779
for _, group := range file.Comments {
780780
for _, comment := range group.List {
781781
parts := strings.Fields(comment.Text)
782-
if len(parts) >= 2 && parts[0] == "//go:cgo_import_dynamic" {
783-
local, remote := parts[1], parts[1]
784-
if len(parts) >= 3 {
785-
remote = parts[2]
786-
}
787-
c.cgoImportDynamic[local] = remote
782+
if len(parts) < 2 || parts[0] != "//go:cgo_import_dynamic" {
783+
continue
784+
}
785+
// Accept a directive only in column 1. The gc compiler also
786+
// accepts an indented directive on a line of its own.
787+
if c.program.Fset.Position(comment.Slash).Column != 1 {
788+
continue
789+
}
790+
local, remote := parts[1], parts[1]
791+
if len(parts) >= 3 {
792+
remote = parts[2]
788793
}
794+
c.cgoImportDynamic[local] = remote
789795
}
790796
}
791797

‎compiler/testdata/cgo-import-dynamic.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var libc_fcntl_trampoline_addr uintptr
88
var libc_nolib_trampoline_addr uintptr
99
var libc_self_trampoline_addr uintptr
1010
var libc_badtype_trampoline_addr uint32
11+
var libc_misplaced_trampoline_addr uintptr //go:cgo_import_dynamic libc_misplaced misplaced_remote "/usr/lib/libSystem.B.dylib"
12+
var libc_indented_trampoline_addr uintptr
13+
var libc_brace_trampoline_addr uintptr
1114

1215
//go:cgo_import_dynamic libc_test remote$INODE64 "/usr/lib/libSystem.B.dylib"
1316
//go:cgo_import_dynamic libc_ioctl ioctl "/usr/lib/libSystem.B.dylib"
@@ -49,3 +52,16 @@ func loadImportedSelfAddress() uintptr {
4952
func loadImportedBadTypeAddress() uint32 {
5053
return libc_badtype_trampoline_addr
5154
}
55+
56+
func loadMisplacedAddress() uintptr {
57+
return libc_misplaced_trampoline_addr
58+
}
59+
60+
func loadIndentedAddress() uintptr {
61+
//go:cgo_import_dynamic libc_indented indented_remote "/usr/lib/libSystem.B.dylib"
62+
return libc_indented_trampoline_addr
63+
}
64+
65+
func loadBraceAddress() uintptr { //go:cgo_import_dynamic libc_brace brace_remote "/usr/lib/libSystem.B.dylib"
66+
return libc_brace_trampoline_addr
67+
}

0 commit comments

Comments
 (0)