Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: static C functions with the same names are not called correctly #49476

Open
hajimehoshi opened this issue Nov 9, 2021 · 5 comments
Open
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@hajimehoshi
Copy link
Member

What version of Go are you using (go version)?

$ go version
go version go1.17.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/hajimehoshi/Library/Caches/go-build"
GOENV="/Users/hajimehoshi/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/hajimehoshi/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/hajimehoshi/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/hajimehoshi/test/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/60/khbk2xqn1c5bml1byjn89dwc0000gn/T/go-build114659014=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Compiled these files:

main.go:

package main

func main() {
        foo()
        bar()
}

foo.go:

package main

// static int x() {                                                                                                                                                                                                                                                                   
//   return 0;                                                                                                                                                                                                                                                                        
// }                                                                                                                                                                                                                                                                                  
import "C"

func foo() {
        println(C.x())
}

bar.go:

package main

// static int x() {                                                                                                                                                                                                                                                                   
//   return 1;                                                                                                                                                                                                                                                                        
// }                                                                                                                                                                                                                                                                                  
import "C"

func bar() {
        println(C.x())
}

What did you expect to see?

0
1

is printed

What did you see instead?

1
1

is printed

@hajimehoshi hajimehoshi changed the title runtime/cgo: duplicated C function names causes unexpected results runtime/cgo: static C functions with the same names are not called correctly Nov 9, 2021
@hajimehoshi
Copy link
Member Author

CC @dmitshur ?

@bcmills
Copy link
Contributor

bcmills commented Nov 9, 2021

Does cgo define what it uses as a compilation unit?

(Certainly the output you observed is not correct, but it's not obvious to me whether the correct behavior is 0 1 or a compile-time error.)

@hajimehoshi
Copy link
Member Author

Does cgo define what it uses as a compilation unit?

Maybe no. At least I want a compile error rather than the current unexpected behavior.

@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 9, 2021
@bcmills bcmills added this to the Backlog milestone Nov 9, 2021
@bcmills
Copy link
Contributor

bcmills commented Nov 9, 2021

(See also #19836, #40820.)

@hajimehoshi hajimehoshi changed the title runtime/cgo: static C functions with the same names are not called correctly cmd/cgo: static C functions with the same names are not called correctly Nov 9, 2021
@ianlancetaylor
Copy link
Contributor

The correct behavior here is a compile time error. The entire package is compiled together with callbacks to the C code, so there isn't any other reasonable approach that we can take.

The fix is probably not too complicated but it's not trivial. We have to accept duplicate types and duplicate function declarations. We have to detect and reject duplicate function definitions.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Status: Triage Backlog
Development

No branches or pull requests

4 participants