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

Preliminary Changes for Updating Cargo #32547

Merged
merged 5 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frozen-abi/src/abi_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl<T: AbiExample> AbiExample for Box<[T]> {
impl<T: AbiExample> AbiExample for std::marker::PhantomData<T> {
fn example() -> Self {
info!("AbiExample for (PhantomData<T>): {}", type_name::<Self>());
<std::marker::PhantomData<T>>::default()
std::marker::PhantomData::<T>
}
}

Expand Down
2 changes: 1 addition & 1 deletion net-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ mod tests {
let address = IpAddr::from([
525u16, 524u16, 523u16, 522u16, 521u16, 520u16, 519u16, 518u16,
]);
let mut data = vec![0u8; IP_ECHO_SERVER_RESPONSE_LENGTH];
let mut data = [0u8; IP_ECHO_SERVER_RESPONSE_LENGTH];
bincode::serialize_into(&mut data[HEADER_LENGTH..], &address).unwrap();
let response: Result<IpEchoServerResponse, _> =
bincode::deserialize(&data[HEADER_LENGTH..]);
Expand Down
12 changes: 6 additions & 6 deletions program-runtime/src/loaded_programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,8 @@ mod tests {
let mut cache = LoadedPrograms::default();

let program1 = Pubkey::new_unique();
let program1_deployment_slots = vec![0, 10, 20];
let program1_usage_counters = vec![4, 5, 25];
let program1_deployment_slots = [0, 10, 20];
let program1_usage_counters = [4, 5, 25];
program1_deployment_slots
.iter()
.enumerate()
Expand Down Expand Up @@ -891,8 +891,8 @@ mod tests {
}

let program2 = Pubkey::new_unique();
let program2_deployment_slots = vec![5, 11];
let program2_usage_counters = vec![0, 2];
let program2_deployment_slots = [5, 11];
let program2_usage_counters = [0, 2];
program2_deployment_slots
.iter()
.enumerate()
Expand Down Expand Up @@ -924,8 +924,8 @@ mod tests {
}

let program3 = Pubkey::new_unique();
let program3_deployment_slots = vec![0, 5, 15];
let program3_usage_counters = vec![100, 3, 20];
let program3_deployment_slots = [0, 5, 15];
let program3_usage_counters = [100, 3, 20];
program3_deployment_slots
.iter()
.enumerate()
Expand Down
5 changes: 5 additions & 0 deletions sdk/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ pub fn derive_clone_zeroed(input: proc_macro::TokenStream) -> proc_macro::TokenS
let name = &item_struct.ident;
quote! {
impl Clone for #name {
// Clippy lint `incorrect_clone_impl_on_copy_type` requires that clone
// implementations on `Copy` types are simply wrappers of `Copy`.
// This is not the case here, and intentionally so because we want to
// guarantee zeroed padding.
#[allow(clippy::incorrect_clone_impl_on_copy_type)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment indicating (1) why this is needed, and (2) when it can be removed/re-evaluated?

I think it'll be helpful to have that information here in the code, in case it becomes hard to find this PR later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So this lint: https://rust-lang.github.io/rust-clippy/master/index.html#/incorrect_clone_impl_on_copy_type

If both Clone and Copy are implemented, they must agree. This is done by dereferencing self in Clone’s implementation. Anything else is incorrect.

In this case, we are "manually" implementing Clone to make sure all padding bytes are zeroed out.
Based on that description I don't think we'll ever be able to remove the allow, since it's a correct flagging of this scenario.
IIRC rust makes no guarantees for Copy in terms of padding bytes bein zero (probably not since it's just a memcpy?) - would defer to @Lichtso since he's the one who added the macro.

In anycase I will add a comment on why this allow is there/necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

We want a custom copy operation. Probably should derive both copy and clone, I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not possible to derive a custom Copy in rust as far as I am aware - it's a special trait that effectively allows types to be memcpy'd

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Re-thinking about this, I'm not sure we even want these types to be Copy. It's probably safer to only support Clone w/ our zeroed padding stuff.
Otherwise it doesn't really protect us from sending something over the network w/ non-zero padding because we could have just copied our value instead of cloning?

Copy link
Contributor

Choose a reason for hiding this comment

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

We can try unimplementing Copy for these types, but that might result in a lot of .clone() where implicit deref happened before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 I think that sounds reasonable. I'm going to add this clippy-lint allow for now, and just self-assign an issue for experimenting with removing Copy.
Without removing Copy this allow is necessary to make clippy happy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fn clone(&self) -> Self {
let mut value = std::mem::MaybeUninit::<Self>::uninit();
unsafe {
Expand Down
6 changes: 3 additions & 3 deletions sdk/program/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ mod test {
fn test_bump_allocator() {
// alloc the entire
{
let heap = vec![0u8; 128];
let heap = [0u8; 128];
let allocator = BumpAllocator {
start: heap.as_ptr() as *const _ as usize,
len: heap.len(),
Expand All @@ -398,7 +398,7 @@ mod test {
}
// check alignment
{
let heap = vec![0u8; 128];
let heap = [0u8; 128];
let allocator = BumpAllocator {
start: heap.as_ptr() as *const _ as usize,
len: heap.len(),
Expand All @@ -423,7 +423,7 @@ mod test {
}
// alloc entire block (minus the pos ptr)
{
let heap = vec![0u8; 128];
let heap = [0u8; 128];
let allocator = BumpAllocator {
start: heap.as_ptr() as *const _ as usize,
len: heap.len(),
Expand Down
4 changes: 2 additions & 2 deletions sdk/program/src/message/compiled_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ mod tests {

#[test]
fn test_try_drain_keys_found_in_lookup_table() {
let orig_keys = vec![
let orig_keys = [
Pubkey::new_unique(),
Pubkey::new_unique(),
Pubkey::new_unique(),
Expand Down Expand Up @@ -598,7 +598,7 @@ mod tests {

#[test]
fn test_try_drain_keys_found_in_lookup_table_with_empty_table() {
let original_keys = vec![
let original_keys = [
Pubkey::new_unique(),
Pubkey::new_unique(),
Pubkey::new_unique(),
Expand Down
3 changes: 1 addition & 2 deletions sdk/program/src/sysvar/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ pub struct BorrowedInstruction<'a> {
#[cfg(not(target_os = "solana"))]
bitflags! {
struct InstructionsSysvarAccountMeta: u8 {
const NONE = 0b00000000;
const IS_SIGNER = 0b00000001;
const IS_WRITABLE = 0b00000010;
}
Expand Down Expand Up @@ -126,7 +125,7 @@ fn serialize_instructions(instructions: &[BorrowedInstruction]) -> Vec<u8> {
data[start..start + 2].copy_from_slice(&start_instruction_offset.to_le_bytes());
append_u16(&mut data, instruction.accounts.len() as u16);
for account_meta in &instruction.accounts {
let mut account_meta_flags = InstructionsSysvarAccountMeta::NONE;
let mut account_meta_flags = InstructionsSysvarAccountMeta::empty();
if account_meta.is_signer {
account_meta_flags |= InstructionsSysvarAccountMeta::IS_SIGNER;
}
Expand Down