Skip to content

Commit a2cdbf8

Browse files
committed
Make code worling w/ pointers in library/std/src/sys/sgx/abi/usercalls/alloc.rs nicer
- Use `.addr()` instead of `as`-cast - Use `add` instead of `offset` and remove some `as isize` casts by doing that - Remove some casts
1 parent b11bf65 commit a2cdbf8

File tree

1 file changed

+14
-14
lines changed
  • library/std/src/sys/sgx/abi/usercalls

1 file changed

+14
-14
lines changed

library/std/src/sys/sgx/abi/usercalls/alloc.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ where
316316
// | small1 | Chunk smaller than 8 bytes
317317
// +--------+
318318
fn region_as_aligned_chunks(ptr: *const u8, len: usize) -> (usize, usize, usize) {
319-
let small0_size = if ptr as usize % 8 == 0 { 0 } else { 8 - ptr as usize % 8 };
320-
let small1_size = (len - small0_size as usize) % 8;
321-
let big_size = len - small0_size as usize - small1_size as usize;
319+
let small0_size = if ptr.is_aligned_to(8) { 0 } else { 8 - ptr.addr() % 8 };
320+
let small1_size = (len - small0_size) % 8;
321+
let big_size = len - small0_size - small1_size;
322322

323323
(small0_size, big_size, small1_size)
324324
}
@@ -364,8 +364,8 @@ pub(crate) unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize)
364364
mfence
365365
lfence
366366
",
367-
val = in(reg_byte) *src.offset(off as isize),
368-
dst = in(reg) dst.offset(off as isize),
367+
val = in(reg_byte) *src.add(off),
368+
dst = in(reg) dst.add(off),
369369
seg_sel = in(reg) &mut seg_sel,
370370
options(nostack, att_syntax)
371371
);
@@ -378,8 +378,8 @@ pub(crate) unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize)
378378
assert!(is_enclave_range(src, len));
379379
assert!(is_user_range(dst, len));
380380
assert!(len < isize::MAX as usize);
381-
assert!(!(src as usize).overflowing_add(len).1);
382-
assert!(!(dst as usize).overflowing_add(len).1);
381+
assert!(!src.addr().overflowing_add(len).1);
382+
assert!(!dst.addr().overflowing_add(len).1);
383383

384384
if len < 8 {
385385
// Can't align on 8 byte boundary: copy safely byte per byte
@@ -404,17 +404,17 @@ pub(crate) unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize)
404404

405405
unsafe {
406406
// Copy small0
407-
copy_bytewise_to_userspace(src, dst, small0_size as _);
407+
copy_bytewise_to_userspace(src, dst, small0_size);
408408

409409
// Copy big
410-
let big_src = src.offset(small0_size as _);
411-
let big_dst = dst.offset(small0_size as _);
412-
copy_quadwords(big_src as _, big_dst, big_size);
410+
let big_src = src.add(small0_size);
411+
let big_dst = dst.add(small0_size);
412+
copy_quadwords(big_src, big_dst, big_size);
413413

414414
// Copy small1
415-
let small1_src = src.offset(big_size as isize + small0_size as isize);
416-
let small1_dst = dst.offset(big_size as isize + small0_size as isize);
417-
copy_bytewise_to_userspace(small1_src, small1_dst, small1_size as _);
415+
let small1_src = src.add(big_size + small0_size);
416+
let small1_dst = dst.add(big_size + small0_size);
417+
copy_bytewise_to_userspace(small1_src, small1_dst, small1_size);
418418
}
419419
}
420420
}

0 commit comments

Comments
 (0)