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: -godefs doesn't handle embedded struct fields correctly #5253
Comments
$ cat test.go package test /* struct A { unsigned long count; struct { long value; } data[15]; }; */ import "C" type A C.struct_A $ go tool cgo -godefs test.go // Created by cgo -godefs - DO NOT EDIT // cgo -godefs test.go package test type A struct { Count uint64 Data [15]_Ctype_struct___0 } @Mortdeus, please provide minimal and standalone test cases (like above) next time, thanks. Labels changed: added priority-later, removed priority-triage. Status changed to Accepted. |
Comment 3 by Mortdeus@gocos2d.org: I just manually wrote the them in. |
Comment 4 by Mortdeus@gocos2d.org: I just manually wrote them in. mortdeus/egles@201cb06#diff-1f1850a75cd762fd2b932808ece04736L238 |
I think this is reasonably fixable. We just need to change the handling of unnamed dwarf.StructType's to directly return a struct definition rather than indirectly via a dummy identifier and typedef. E.g., with a minor change on top of my patch for issue 8441 (mostly to make sure assigning to t.Go after the recursive call to c.Struct() is safe), I get: $ go tool cgo -godefs issue5253.go // Created by cgo -godefs - DO NOT EDIT // cgo -godefs issue5253.go package test type A struct { Count uint64 Data [15]struct { Value int64 } } There's a possible issue if C code does something silly like "typedef struct { ... } X, Y;" then C.X and C.Y would now become distinct types, whereas previously they would be treated as aliases of type __1. I don't think that's an issue in practice, but it's probably solvable if needed. There's also an exponential code size blow up from pathological inputs like: /* typedef struct foo { struct { struct { struct { int x; } a, b; } a, b; } a, b; } A, B; */ import "C" but that could be fixed by making (*typeConv).Struct() smarter when consecutive fields reference the same DWARF type. Though again, I don't expect it to be an issue in practice. |
I've put together https://golang.org/cl/122900043 to fix the issue as described in the initial bug report here. E.g., for the input provided by minux in #1, here's the output from patched cgo: <<<< $ go tool cgo -godefs test.go // Created by cgo -godefs - DO NOT EDIT // cgo -godefs test.go package test type A struct { Count uint64 Data [15]struct { Value int64 } } >>>> I'd appreciate if any users affected by this could test that this patch (applied to tip) fixes their issues with cgo -godefs, or if there are more details to address. |
This bug gets even worse when you have something like this using anonymous unions & structs:
Is there any chance that CGO will support this? |
This issue is specifically about |
I've run into this while trying to fix github.com/gen2brain/malgo, which wraps Miniaudio, which has huge structs (600 or 1200 bytes in size) with many embedded anonymous structs. They broke at some point, and trying to figure out what fields are misaligned is not fun at all. |
@mdempsky Do you want to revive https://golang.org/cl/122900043 ? |
I'd love to see this fixed. https://utcc.utoronto.ca/%7Ecks/space/blog/programming/GoCGoCompatibleStructs mentions this issue, and has a not so great workaround. |
Agreed, it would be great to see this finally fixed. |
by Mortdeus@gocos2d.org:
The text was updated successfully, but these errors were encountered: