-
Notifications
You must be signed in to change notification settings - Fork 19k
cmd/cgo: detect incompatible C declarations #67699
Copy link
Copy link
Closed
Labels
FeatureRequestIssues asking for a new feature that does not need a proposal.Issues asking for a new feature that does not need a proposal.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.Issues related to the Go compiler and/or runtime.
Milestone
Metadata
Metadata
Assignees
Labels
FeatureRequestIssues asking for a new feature that does not need a proposal.Issues asking for a new feature that does not need a proposal.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.Issues related to the Go compiler and/or runtime.
We already detect incompatible C declarations if both declarations are in the same file. For instance,
Gives an error
conflicting types for 'f'.When the conflicting definitions are in different files, however, we don't detect it. That can lead to very tricky runtime failures. See #67670 for an example.
Here's a small reproducer:
test1.go:test2.go:When run with
go run test1.go test2.go, we get (darwin/arm64):Which means that a stack variable got partially overwritten by a cgo call.
I'm pretty sure what is happening is that at the callsite the compiler assumes
C.fhas no return value, so does not allocate space for it. But the cgo wrapper assumesC.fdoes have a return value, and generates code to movef's return value to the stack frame. That write lands on the local variablex.I'm not sure how the compiler and/or cgo decide which version of
fto use. Perhaps if they just chose consistently things would work. But probably better to detect the conflicting definitions and error out.@ianlancetaylor