Skip to content
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: doesn't support struct with bit fields #43261

Closed
mengzhuo opened this issue Dec 18, 2020 · 2 comments
Closed

cmd/cgo: doesn't support struct with bit fields #43261

mengzhuo opened this issue Dec 18, 2020 · 2 comments

Comments

@mengzhuo
Copy link
Contributor

What version of Go are you using (go version)?

$ go version
eaa97fbf20baffac713ed1b780f864a6fee54ab6

Does this issue reproduce with the latest release?

no

What did you do?

package main

/*
struct foo {
        int bf:4;
        int bar;
};
*/
import "C"

import (
        "unsafe"
)

func main() {
        a := [24]byte{}
        c := (*C.struct_foo)(unsafe.Pointer(&a))
        c.bar = '4'
        c.bf = 1
        print(string(a[:]))
}

What did you expect to see?

go build passed

What did you see instead?

c.bf undefined (type *_Ctype_struct_foo has no field or method bf)

This is just a demo.

In https://go-review.googlesource.com/c/go/+/252378

cgo no longer try to translate
C struct bitfields into Go struct fields, even if their size can be
represented in Go. The order in which C bitfields appear in memory
is implementation dependent, so in some cases the cgo tool produced
results that were silently incorrect.

In real life, some system api like ebpf <linux/bpf.h> do has struct with bit fields.

struct bpf_insn {
        __u8    code;           /* opcode */
        __u8    dst_reg:4;      /* dest register */
        __u8    src_reg:4;      /* source register */
        __s16   off;            /* signed offset */
        __s32   imm;            /* signed immediate constant */
};

Related:
xlab/c-for-go#109

Maybe we should reconside it?
Thanks

CC @ianlancetaylor

@ianlancetaylor ianlancetaylor changed the title cgo: compiler can't gernate struct with bit fields cmd/cgo: doesn't support struct with bit fields Dec 18, 2020
@ianlancetaylor
Copy link
Contributor

A C struct like

struct foo {
        int bf:4;
        int bar;
};

simply can't be represented as a Go struct. The cgo model has no way to handle C types that can't be represented in Go. The only mechanism we have is something like

/*
int getBf(struct foo *p) { return p->bf; }
void setBf(struct foo *p, int v) { p->bf = v; }
*/
import "C"

and then Go code can call C.getBf and C.setBf as needed.

I don't see what else we can do.

@mengzhuo
Copy link
Contributor Author

A C struct like

struct foo {
        int bf:4;
        int bar;
};

simply can't be represented as a Go struct. The cgo model has no way to handle C types that can't be represented in Go. The only mechanism we have is something like

/*
int getBf(struct foo *p) { return p->bf; }
void setBf(struct foo *p, int v) { p->bf = v; }
*/
import "C"

and then Go code can call C.getBf and C.setBf as needed.

I don't see what else we can do.

OK, Thank you for the information.

@golang golang locked and limited conversation to collaborators Dec 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants