cmd/cgo: detect incompatible C declarations #67699
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
FeatureRequest
Issues asking for a new feature that does not need a proposal.
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
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.f
has no return value, so does not allocate space for it. But the cgo wrapper assumesC.f
does 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
f
to use. Perhaps if they just chose consistently things would work. But probably better to detect the conflicting definitions and error out.@ianlancetaylor
The text was updated successfully, but these errors were encountered: