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

btf: handle signed enums and fix default signedness #781

Merged
merged 3 commits into from
Sep 6, 2022

Conversation

lmb
Copy link
Collaborator

@lmb lmb commented Sep 1, 2022

btf: handle signed enums and fix default signedness

Enums now carry signedness encoded in the KindFlag, with the default being
an unsigned enum. This breaks our assumption that enums are signed by default.

Add Enum.Signed and switch EnumValue.Value to uint64. I used uint64 instead of
uint32 since we'll likely need 64 bits to support BTF_KIND_ENUM64 in the future.

Users of bpf2go that generated enums will see their generated types changing from
signed to unsigned types.

Updates #780

btf: reject invalid structs when formatting

GoFormatter currently outputs nonsensical padding given a struct definition
like the following:

    &Struct{
        Size: 1,
        Members: []Member{
             {Name: "foo", Type: &Int{Size: 2}, Offset: 0},
        },
    }

The reason is that the member is large than the enclosing struct. Return
an error in such cases instead of formatting a bogus type.

btf: fix the output of GoFormatter for small enums

When adding support for sized enums I only adjusted the code that
writes out a type declaration, but forgot to change the output
of enum type literals. This would lead to the wrong type being used
for an enum that is used as a struct field.

Fix this by moving the code from writeTypeDecl to writeTypeLit.

@lmb
Copy link
Collaborator Author

lmb commented Sep 1, 2022

@lmb lmb force-pushed the btf-enum-signedness branch from 8f6db8f to 17b3ed3 Compare September 2, 2022 09:01
lmb added 3 commits September 5, 2022 09:35
When adding support for sized enums I only adjusted the code that
writes out a type declaration, but forgot to change the output
of enum type literals. This would lead to the wrong type being used
for an enum that is used as a struct field.

Fix this by moving the code from writeTypeDecl to writeTypeLit.
GoFormatter currently outputs nonsensical padding given a struct definition
like the following:

    &Struct{
        Size: 1,
        Members: []Member{
             {Name: "foo", Type: &Int{Size: 2}, Offset: 0},
        },
    }

The reason is that the member is large than the enclosing struct. Return
an error in such cases instead of formatting a bogus type.
Enums now carry signedness encoded in the KindFlag, with the default being
an unsigned enum. This breaks our assumption that enums are signed by default.

Add Enum.Signed and switch EnumValue.Value to uint64. I used uint64 instead of
uint32 since we'll likely need 64 bits to support BTF_KIND_ENUM64 in the future.

Users of bpf2go that generated enums will see their generated types changing from
signed to unsigned types.

Updates cilium#780
@lmb lmb force-pushed the btf-enum-signedness branch from 17b3ed3 to e8a7a73 Compare September 5, 2022 09:36
@lmb lmb merged commit daa7977 into cilium:master Sep 6, 2022
@lmb lmb deleted the btf-enum-signedness branch September 6, 2022 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant