From b5d90cb1bdc1d7fa4e8a40f5f80b089f1ca39363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 20 Apr 2025 21:53:01 +0200 Subject: [PATCH] expose cgo issue 916 Seems to happen when the main package only has Go files importing "C", meaning that it has zero "pure Go" files. To avoid needing two main package Go builds for cgo.txtar, switch our main package to this scenario as it seems more interesting. While here, add a test case for a Go callback function taking a C param as that is relatively common and we had no coverage for it. This only reproduces the bug; the fix is coming separately. For #916. --- testdata/script/cgo.txtar | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/testdata/script/cgo.txtar b/testdata/script/cgo.txtar index f39c991..b57dfbc 100644 --- a/testdata/script/cgo.txtar +++ b/testdata/script/cgo.txtar @@ -1,6 +1,8 @@ [!cgo] skip 'this test requires cgo to be enabled' -exec garble build +! exec garble build +stderr 'cannot define new methods on non-local type _trieNode' +stop ! stderr 'warning' # check that the C toolchain is happy exec ./main cmp stdout main.stdout @@ -33,12 +35,18 @@ go 1.23 -- main.go -- package main +// It's important that the main package only has files importing "C", +// as that used to trigger https://github.com/burrowers/garble/issues/916. + +import "C" +import "test/main/imported" + func main() { - regularFunc() + imported.RegularFunc() cgoFunc() } --- regular_main.go -- -package main +-- imported/imported_regular.go -- +package imported import ( "fmt" @@ -46,7 +54,7 @@ import ( "runtime" ) -func regularFunc() { +func RegularFunc() { if os.Getenv("GARBLE_TEST_REVERSING") == "true" { _, filename, _, _ := runtime.Caller(0) fmt.Println("regular filename:", filename) @@ -71,7 +79,7 @@ static int privateAdd(int a, int b) { extern void goCallback(); -static void callGoCallback() { +static void callGoCallbacks() { goCallback(); separateFunction(); } @@ -89,7 +97,7 @@ func cgoFunc() { st := C.struct_portedStruct{} fmt.Println(st.PortedField == nil) - C.callGoCallback() + C.callGoCallbacks() } //export goCallback @@ -101,22 +109,31 @@ func goCallback() { fmt.Println("cgo filename:", filename) } } + +//export printString +func printString(cs *C.char) { + fmt.Println(C.GoString(cs)) +} -- separate.h -- void separateFunction(); -- separate.c -- #include "_cgo_export.h" +#include void separateFunction() { goCallback(); + printString("string from C"); } -- main.stdout -- 3 true go callback go callback +string from C -- reversed.stdout -- -regular filename: test/main/regular_main.go +regular filename: test/main/imported/imported_regular.go 3 true go callback go callback +string from C