-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: unaligned access warning with zig #62480
Comments
In triage, the issue appears to be that |
This generated C code is constructing a Go argument list that is going to be passed to a Go function. The Go argument list must exactly match what the Go function expects to find on the stack. For Go, for linux-arm, a This does mean that the typedef struct {
int* p0;
int64_t p1;
} __attribute__((__packed__)) _cgo_argtype; Even if the alignment of I don't see why that changes when So it seems to me that this is a bug in how zig handles |
So, the warning comes from LLVM (not zig directly itself). I suspect the relevant issue is llvm/llvm-project#55520. So using clang 14+ leads to the same warning https://c.godbolt.org/z/4cP5E9TaE. As far as I can understand it's an early warning for:
The linked issue (llvm/llvm-project#55520 (comment)) mentions:
So, the cgo generated code seems correct.
Yeah, that's what's the confusing thing. I was also under the assumption that the expected alignment for arm is 4. If it's aligned less, like the C example above, then the warning makes more sense. Maybe it's because, that the compiler doesn't know how aligned it should be -- e.g. maybe some NEON instruction expects higher alignment? I guess the solution would be to silence that warning ( |
It should be possible for cmd/cgo to write out something like
in the same place where it emits a pragma ignoring |
Change https://go.dev/cl/526915 mentions this issue: |
Change https://go.dev/cl/528935 mentions this issue: |
Clang 14+ introduced a warning when using mixed packed and unpacked structs. This can cause problems when taking an address of the unpacked struct, which may end up having a different alignment than expected. This is not a problem in cgo, which does not take pointers from the packed struct. Updated version of https://go.dev/cl/526915, which includes "-Wunknown-warning-option" for compilers that do not have the specific flag. Fixes #62480 Change-Id: I788c6604d0ed5267949f4367f148fa26d2116f51 Reviewed-on: https://go-review.googlesource.com/c/go/+/528935 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What did you do?
This is the smallest repro
Then using zig 0.11 for CC and compiling for linux/arm:
Ideally there wouldn't be a warning.
I would guess the same warning will be shown with
clang
, however the setup for cross-compile is more annoying and I didn't get it to work at the moment.Debugging
Using
go tool cgo
it shows that it ends up generating:It seems this warning was added in https://reviews.llvm.org/D116221
As far as I understand the warning is happening due to having
__attribute__((__packed__))
andtypedef struct Data
not having it. After adding__attribute__((__packed__))
to theData struct
the warning disappears.The text was updated successfully, but these errors were encountered: