Skip to content

Commit 707de26

Browse files
committed
Update align pointer macro
1 parent 5b45a92 commit 707de26

File tree

1 file changed

+9
-3
lines changed
  • sdk/pinocchio/src/entrypoint

1 file changed

+9
-3
lines changed

sdk/pinocchio/src/entrypoint/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use alloc::BumpAllocator;
1313
use core::{
1414
cmp::min,
1515
mem::{size_of, MaybeUninit},
16+
ptr::with_exposed_provenance_mut,
1617
slice::from_raw_parts,
1718
};
1819

@@ -195,9 +196,14 @@ macro_rules! program_entrypoint {
195196
/// Align a pointer to the BPF alignment of [`u128`].
196197
macro_rules! align_pointer {
197198
($ptr:ident) => {
198-
// integer-to-pointer cast: the resulting pointer will have the same provenance as
199-
// the original pointer and it follows the alignment requirement for the input.
200-
$ptr.with_addr((($ptr.addr() + (BPF_ALIGN_OF_U128 - 1)) & !(BPF_ALIGN_OF_U128 - 1)))
199+
// Integer-to-pointer cast: first compute the aligned address as a `usize`,
200+
// since this is more CU-efficient than using `ptr::align_offset()` or the
201+
// strict provenance API (e.g., `ptr::with_addr()`). Then cast the result
202+
// back to a pointer. The resulting pointer is guaranteed to be valid
203+
// becauseit follows the layout serialized by the runtime.
204+
with_exposed_provenance_mut(
205+
($ptr.expose_provenance() + (BPF_ALIGN_OF_U128 - 1)) & !(BPF_ALIGN_OF_U128 - 1),
206+
)
201207
};
202208
}
203209

0 commit comments

Comments
 (0)