-
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: alignment issue with int128 inside of a struct #69086
Comments
Change https://go.dev/cl/608815 mentions this issue: |
cc @golang/compiler |
Not sure if it's the same bug or another one. I noticed the same kind of problem, but this time it looks to me like a function call ABI issue when a struct containing an int128 is passed as fourth argument of a C function. go.mod
main.go
int128.h
int128.c
When
When
(Note that here, adding a padding to the struct doesn't change the issue.) |
@mwetterw Can you post the corresponding Go code for the problem passing a struct as an argument? Thanks. |
@ianlancetaylor Sorry, completely forgot that one! |
Thanks for the fix! |
@gopherbot Please backport this issue. This never worked so it is not a regression. But it seems worth fixing because without it it's quite hard to use a C struct with an |
Backport issue(s) opened: #69218 (for 1.22), #69219 (for 1.23). Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases. |
@ianlancetaylor Thanks for the fix! |
Change https://go.dev/cl/611296 mentions this issue: |
Change https://go.dev/cl/611297 mentions this issue: |
If the aligned offset isn't sufficient for the field offset, we were padding based on the aligned offset. We need to pad based on the original offset instead. Also set the Go alignment correctly for int128. We were defaulting to the maximum alignment, but since we translate int128 into an array of uint8 the correct Go alignment is 1. For #69086 Fixes #69219 Change-Id: I23ce583335c81beac2ac51f7f9336ac97ccebf09 Reviewed-on: https://go-review.googlesource.com/c/go/+/608815 Reviewed-by: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> (cherry picked from commit c209892) Reviewed-on: https://go-review.googlesource.com/c/go/+/611296 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com>
If the aligned offset isn't sufficient for the field offset, we were padding based on the aligned offset. We need to pad based on the original offset instead. Also set the Go alignment correctly for int128. We were defaulting to the maximum alignment, but since we translate int128 into an array of uint8 the correct Go alignment is 1. For #69086 Fixes #69218 Change-Id: I23ce583335c81beac2ac51f7f9336ac97ccebf09 Reviewed-on: https://go-review.googlesource.com/c/go/+/608815 Reviewed-by: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> (cherry picked from commit c209892) Reviewed-on: https://go-review.googlesource.com/c/go/+/611297 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Go version
go version go1.23.0 linux/amd64
Output of
go env
in your module/workspace:What did you do?
From Go, I passed to C a struct containing one int128 (and another field before it so that Go needs to guess the padding between the first member and that int128 member).
From C, I wanted to read what I got from Go.
go.mod
main.go
int128.h
int128.c
What did you see happen?
The result is corrupted: C doesn't receive the correct int128.
This is because Go seems to not correctly guess the padding for int128 inside of a struct.
Look how C and Go both have a different conception of that C struct:
What did you expect to see?
I expected Go to correctly guess the padding of the C struct, and to receive the correct int128 from C perspective.
C self-aligns the int128: i.e. it is aligned to a multiple of 16, which is its own size.
I would have expected to see this result:
Correct behavior can be obtained by making the C padding explicit and visible to Go:
The text was updated successfully, but these errors were encountered: