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

sys: add type safe Pointer wrapper #1670

Merged
merged 1 commit into from
Feb 24, 2025
Merged

Conversation

lmb
Copy link
Collaborator

@lmb lmb commented Feb 6, 2025

Add TypedPointer which is a type safe wrapper around Pointer. This relieves users of the generated bindings from knowing which types are expected. This should make it less likely that we cause a hard to debug panic or crash due to the kernel overwriting runtime memory.

sys.Pointer still exists because there are some cases where we legitimately don't know what type to point at, for example the link info APIs. Map APIs are also unchanged for now because they don't benefit much from converting to TypedPointer[byte].

@github-actions github-actions bot added the breaking-change Changes exported API label Feb 6, 2025
@lmb lmb force-pushed the sys-generic-pointer branch from d6fc75b to 1fc7a4f Compare February 6, 2025 11:55
@lmb
Copy link
Collaborator Author

lmb commented Feb 6, 2025

The breaking change is due to replacing ebpf.MapID and ProgramID with a type alias.

@kolyshkin
Copy link
Contributor

I like the approach!

It will be better to separate the actual overrun fix from the new type system.

I've also updated the fix in #1660 to not break the existing API nor return truncated pointers, PTAL @lmb

@lmb lmb force-pushed the sys-generic-pointer branch from 1fc7a4f to ae08243 Compare February 12, 2025 10:09
@lmb lmb marked this pull request as ready for review February 12, 2025 10:09
@lmb lmb requested review from dylandreimerink, mmat11 and a team as code owners February 12, 2025 10:09
@lmb lmb force-pushed the sys-generic-pointer branch 2 times, most recently from c11a8e0 to 427cfcd Compare February 14, 2025 10:06
Copy link
Member

@dylandreimerink dylandreimerink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this! More type safety more better

@ti-mo
Copy link
Collaborator

ti-mo commented Feb 17, 2025

@lmb Looks like this implements #914?

@ti-mo ti-mo linked an issue Feb 17, 2025 that may be closed by this pull request
ti-mo
ti-mo previously requested changes Feb 17, 2025
Copy link
Collaborator

@ti-mo ti-mo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this allow us to unexport sys.Pointer? Ie. if it needs to be used outside of the package, it needs to be wrapped in an (Unsafe/Typed/String/XYZ)Pointer. Then we make sure UAPI no longer takes any sys.Pointers.

I still see a bunch of sys.Pointer uses in the map API; I'd argue those should all be sys.UnsafePointers instead, to be converted to typed pointers before returning.

Add TypedPointer which is a type safe wrapper around Pointer. This
relieves users of the generated bindings from knowing which types
are expected. This should make it less likely that we cause a hard
to debug panic or crash due to the kernel overwriting runtime memory.

sys.Pointer still exists because there are some cases where we
legitimately don't know what type to point at, for example the link info
APIs. Map APIs are also unchanged for now because they don't benefit
much from converting to TypedPointer[byte].

The trick to include "_ [0]*T" in TypedPointer is taken from atomic.Pointer
in the Go stdlib.

Signed-off-by: Lorenz Bauer <[email protected]>
@lmb lmb force-pushed the sys-generic-pointer branch from 427cfcd to fde0d85 Compare February 24, 2025 11:29
@lmb
Copy link
Collaborator Author

lmb commented Feb 24, 2025

I still see a bunch of sys.Pointer uses in the map API; I'd argue those should all be sys.UnsafePointers instead, to be converted to typed pointers before returning.

Discussed face to face: UnsafePointer is a constructor, not a type. We still need sys.Pointer to account for endianness differences on 32 bit arches.

@lmb lmb requested a review from ti-mo February 24, 2025 11:30
@lmb lmb dismissed ti-mo’s stale review February 24, 2025 19:48

Discussed f2f

@lmb lmb merged commit 1debf0b into cilium:main Feb 24, 2025
18 checks passed
@lmb lmb deleted the sys-generic-pointer branch February 24, 2025 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Changes exported API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proposal: sys: make UAPI wrappers type safe(er)
4 participants