-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: offset generated as padding #61707
Comments
In triage, our theory is that because the |
But that is defined in mach/memory_object_types.h, which is #include'd in vm_region.h. I also tried manually #include mach/memory_object_types.h in defs_darwin.go but that didn't help either. Other fields also have types from #include'd files, like |
It's an alignment problem. The The reason C is using a 4 byte alignment rather than an 8 alignment is because the struct is defined within the scope of a Hand editing the code to change the type of field to There is no really good workaround for this kind of thing. One approach is to hand write the struct in defs_darwin.go, but write the Closing this issue because I don't think there is anything to do in the Go toolchain. Please comment if you disagree. |
Thanks. This makes sense. I forgot to check the alignment. I'll send a CL to change it back to [8]byte and convert to uint64 manually. That said, would it be better for the cgo command to preserve the name |
Change https://go.dev/cl/516156 mentions this issue: |
What's happening internally is that cgo says "I can't represent this C field, so I'm just going to drop it." Then it moves to the next field, and says "this field is at offset 28, and to make that work I need to insert 8 bytes of padding." That's why we wind up with That said, sure, I think that for a case like this it would be feasible to keep the name |
The type machVMRegionBasicInfoData is generated from C type vm_region_basic_info_data_64_t, which is a packed struct with a 64-bit field at offset 20. We cannot use uint64 as the field type in the Go struct, as that will be aligned at offset 24, which does not match the C struct. Change back to [8]byte (which is what the cgo command generates), but keep the name Offset. Updates #61707. Updates #50891. Change-Id: I2932328d7f9dfe9d79cff89752666c794d4d3788 Reviewed-on: https://go-review.googlesource.com/c/go/+/516156 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com>
At the very least a warning is warranted, either as a comment in the generated code or as an error message. If there is a lot of legacy code then maybe generate the new code as a comment with a flag to switch to the new behaviour. eg. // is skipped because it cannot be represented in go as a due to alignment rules, use -flag to include as a byte array |
Or alternatively, add a -explain-alignments, that doesn't generate code, just explanations for the alignment decisions made. Either of these would have avoided the confusion here which thankfully you caught quickly, but I'd worry that similar errors are made in the broader go codebase. |
I honestly don't know how likely errors are, because |
Well, if there was even a warning about dropping the field because it
couldn’t be represented then I would have known to dig further and not
checked in broken code! The many other edits to generated code in the go
runtime suggest that generated code is a starting point rather than the
final code which undermines confidence in any such generated code. Anyway,
feel free to close this issue which hopefully will end being indexed.
Thanks for the help!
Cheers, Cos.
…On Sun, Aug 6, 2023 at 1:14 AM Ian Lance Taylor ***@***.***> wrote:
I honestly don't know how likely errors are, because -godefs is rarely
used, and the default generated code should be safe to use. The problem
only arises when somebody hand edits the generated code.
—
Reply to this email directly, view it on GitHub
<#61707 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACCMUYGV75J7TNK7OABIDC3XT3HU3ANCNFSM6AAAAAA3A4JX7I>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
I’m not being sarcastic here, I really do mean thanks for the help with
getting this pr done!
Cheers, Cos.
On Sun, Aug 6, 2023 at 10:01 AM Cosmos Nicolaou ***@***.***>
wrote:
… Well, if there was even a warning about dropping the field because it
couldn’t be represented then I would have known to dig further and not
checked in broken code! The many other edits to generated code in the go
runtime suggest that generated code is a starting point rather than the
final code which undermines confidence in any such generated code. Anyway,
feel free to close this issue which hopefully will end being indexed.
Thanks for the help!
Cheers, Cos.
On Sun, Aug 6, 2023 at 1:14 AM Ian Lance Taylor ***@***.***>
wrote:
> I honestly don't know how likely errors are, because -godefs is rarely
> used, and the default generated code should be safe to use. The problem
> only arises when somebody hand edits the generated code.
>
> —
> Reply to this email directly, view it on GitHub
> <#61707 (comment)>, or
> unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ACCMUYGV75J7TNK7OABIDC3XT3HU3ANCNFSM6AAAAAA3A4JX7I>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>
|
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, and with head.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
While developing https://go-review.googlesource.com/c/go/+/503919 it is necessary to generate the go struct corresponding to the C struct vm_region_basic_info_data_64_t. Unfortunately the generated output is incorrect forcing a hand-edit of the generated code.
To reproduce the problem use:
cd $GOROOT/src/runtime/pprof
go tool cgo -godefs defs_darwin.go > tmp-go
diff defs_darwin_arm64.go tmp-go
What did you expect to see?
The offset field in the original C struct should appear as Offset uint64 in the generated output.
What did you see instead?
The generated code contains
Pad_cgo_0 [8]byte
rather than the named field.The text was updated successfully, but these errors were encountered: