Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 4d4dddc

Browse files
authored
Update to nightly rustc to 2023-04-19 (#31381)
* Update to nightly rustc to 2023-04-19 * pray... * Not enough pray.. * skip check... * hope merciful shellcheck * ci hack... * Restore ci/test-checks.sh * Restore debug_assertions with proper workaround... * small cleanup * seems this isn't needed? * Make the workaround more robust... * Remove now-resolved clippy exception
1 parent bbd8be6 commit 4d4dddc

File tree

4 files changed

+62
-26
lines changed

4 files changed

+62
-26
lines changed

ci/rust-version.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fi
2929
if [[ -n $RUST_NIGHTLY_VERSION ]]; then
3030
nightly_version="$RUST_NIGHTLY_VERSION"
3131
else
32-
nightly_version=2023-03-04
32+
nightly_version=2023-04-19
3333
fi
3434

3535

ci/test-checks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fi
7575

7676
_ ci/order-crates-for-publishing.py
7777

78-
nightly_clippy_allows=()
78+
nightly_clippy_allows=(--allow=clippy::redundant_clone)
7979

8080
# run nightly clippy for `sdk/` as there's a moderate amount of nightly-only code there
8181
_ scripts/cargo-for-all-lock-files.sh -- "+${rust_nightly}" clippy --workspace --all-targets --features dummy-for-ci-check -- \

programs/bpf_loader/src/serialization.rs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ mod tests {
563563
},
564564
std::{
565565
cell::RefCell,
566+
mem::transmute,
566567
rc::Rc,
567568
slice::{self, from_raw_parts, from_raw_parts_mut},
568569
},
@@ -994,58 +995,94 @@ mod tests {
994995
}
995996

996997
// the old bpf_loader in-program deserializer bpf_loader::id()
997-
#[allow(clippy::type_complexity)]
998+
#[deny(unsafe_op_in_unsafe_fn)]
998999
pub unsafe fn deserialize_unaligned<'a>(
9991000
input: *mut u8,
10001001
) -> (&'a Pubkey, Vec<AccountInfo<'a>>, &'a [u8]) {
1002+
// this boring boilerplate struct is needed until inline const...
1003+
struct Ptr<T>(std::marker::PhantomData<T>);
1004+
impl<T> Ptr<T> {
1005+
const COULD_BE_UNALIGNED: bool = std::mem::align_of::<T>() > 1;
1006+
1007+
#[inline(always)]
1008+
fn read_possibly_unaligned(input: *mut u8, offset: usize) -> T {
1009+
unsafe {
1010+
let src = input.add(offset) as *const T;
1011+
if Self::COULD_BE_UNALIGNED {
1012+
src.read_unaligned()
1013+
} else {
1014+
src.read()
1015+
}
1016+
}
1017+
}
1018+
1019+
// rustc inserts debug_assert! for misaligned pointer dereferences when
1020+
// deserializing, starting from [1]. so, use std::mem::transmute as the last resort
1021+
// while preventing clippy from complaining to suggest not to use it.
1022+
// [1]: https://github.com/rust-lang/rust/commit/22a7a19f9333bc1fcba97ce444a3515cb5fb33e6
1023+
// as for the ub nature of the misaligned pointer dereference, this is
1024+
// acceptable in this code, given that this is cfg(test) and it's cared only with
1025+
// x86-64 and the target only incurs some performance penalty, not like segfaults
1026+
// in other targets.
1027+
#[inline(always)]
1028+
fn ref_possibly_unaligned<'a>(input: *mut u8, offset: usize) -> &'a T {
1029+
#[allow(clippy::transmute_ptr_to_ref)]
1030+
unsafe {
1031+
transmute(input.add(offset) as *const T)
1032+
}
1033+
}
1034+
1035+
// See ref_possibly_unaligned's comment
1036+
#[inline(always)]
1037+
fn mut_possibly_unaligned<'a>(input: *mut u8, offset: usize) -> &'a mut T {
1038+
#[allow(clippy::transmute_ptr_to_ref)]
1039+
unsafe {
1040+
transmute(input.add(offset) as *mut T)
1041+
}
1042+
}
1043+
}
1044+
10011045
let mut offset: usize = 0;
10021046

10031047
// number of accounts present
10041048

1005-
#[allow(clippy::cast_ptr_alignment)]
1006-
let num_accounts = *(input.add(offset) as *const u64) as usize;
1049+
let num_accounts = Ptr::<u64>::read_possibly_unaligned(input, offset) as usize;
10071050
offset += size_of::<u64>();
10081051

10091052
// account Infos
10101053

10111054
let mut accounts = Vec::with_capacity(num_accounts);
10121055
for _ in 0..num_accounts {
1013-
let dup_info = *(input.add(offset) as *const u8);
1056+
let dup_info = Ptr::<u8>::read_possibly_unaligned(input, offset);
10141057
offset += size_of::<u8>();
10151058
if dup_info == NON_DUP_MARKER {
1016-
#[allow(clippy::cast_ptr_alignment)]
1017-
let is_signer = *(input.add(offset) as *const u8) != 0;
1059+
let is_signer = Ptr::<u8>::read_possibly_unaligned(input, offset) != 0;
10181060
offset += size_of::<u8>();
10191061

1020-
#[allow(clippy::cast_ptr_alignment)]
1021-
let is_writable = *(input.add(offset) as *const u8) != 0;
1062+
let is_writable = Ptr::<u8>::read_possibly_unaligned(input, offset) != 0;
10221063
offset += size_of::<u8>();
10231064

1024-
let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
1065+
let key = Ptr::<Pubkey>::ref_possibly_unaligned(input, offset);
10251066
offset += size_of::<Pubkey>();
10261067

1027-
#[allow(clippy::cast_ptr_alignment)]
1028-
let lamports = Rc::new(RefCell::new(&mut *(input.add(offset) as *mut u64)));
1068+
let lamports = Rc::new(RefCell::new(Ptr::mut_possibly_unaligned(input, offset)));
10291069
offset += size_of::<u64>();
10301070

1031-
#[allow(clippy::cast_ptr_alignment)]
1032-
let data_len = *(input.add(offset) as *const u64) as usize;
1071+
let data_len = Ptr::<u64>::read_possibly_unaligned(input, offset) as usize;
10331072
offset += size_of::<u64>();
10341073

1035-
let data = Rc::new(RefCell::new({
1074+
let data = Rc::new(RefCell::new(unsafe {
10361075
from_raw_parts_mut(input.add(offset), data_len)
10371076
}));
10381077
offset += data_len;
10391078

1040-
let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
1079+
let owner: &Pubkey = Ptr::<Pubkey>::ref_possibly_unaligned(input, offset);
10411080
offset += size_of::<Pubkey>();
10421081

1043-
#[allow(clippy::cast_ptr_alignment)]
1044-
let executable = *(input.add(offset) as *const u8) != 0;
1082+
let executable = Ptr::<u8>::read_possibly_unaligned(input, offset) != 0;
10451083
offset += size_of::<u8>();
10461084

1047-
#[allow(clippy::cast_ptr_alignment)]
1048-
let rent_epoch = *(input.add(offset) as *const u64);
1085+
let rent_epoch = Ptr::<u64>::read_possibly_unaligned(input, offset);
10491086
offset += size_of::<u64>();
10501087

10511088
accounts.push(AccountInfo {
@@ -1066,16 +1103,15 @@ mod tests {
10661103

10671104
// instruction data
10681105

1069-
#[allow(clippy::cast_ptr_alignment)]
1070-
let instruction_data_len = *(input.add(offset) as *const u64) as usize;
1106+
let instruction_data_len = Ptr::<u64>::read_possibly_unaligned(input, offset) as usize;
10711107
offset += size_of::<u64>();
10721108

1073-
let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };
1109+
let instruction_data = unsafe { from_raw_parts(input.add(offset), instruction_data_len) };
10741110
offset += instruction_data_len;
10751111

10761112
// program Id
10771113

1078-
let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
1114+
let program_id = Ptr::<Pubkey>::ref_possibly_unaligned(input, offset);
10791115

10801116
(program_id, accounts, instruction_data)
10811117
}

zk-token-sdk/src/zk_token_proof_instruction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
///! Instructions provided by the ZkToken Proof program
1+
//! Instructions provided by the ZkToken Proof program
22
pub use crate::instruction::*;
33
use {
44
bytemuck::bytes_of,

0 commit comments

Comments
 (0)