Skip to content
Open
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
31 changes: 27 additions & 4 deletions lang-v2/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3290,8 +3290,14 @@ fn gen_declare_program_types(idl: &serde_json::Value) -> syn::Result<Vec<TokenSt
let docs = gen_declare_program_docs(ty_def, ident.span());
let repr = gen_declare_program_repr(ty_def, ident.span())?;
let serialization = declare_type_serialization(ty_def, ident.span())?;
let bytemuck_repr = if repr.is_none() && serialization.is_bytemuck() {
quote! { #[repr(C)] }
// Safe bytemuck defaults to repr(C). `bytemuckunsafe` matches v1
// `#[zero_copy(unsafe)]`: packed Rust layout when the IDL omits repr.
let bytemuck_repr = if repr.is_none() {
match serialization {
DeclareTypeSerialization::Bytemuck => quote! { #[repr(C)] },
DeclareTypeSerialization::BytemuckUnsafe => quote! { #[repr(Rust, packed)] },
DeclareTypeSerialization::Borsh => quote! {},
}
} else {
quote! { #repr }
};
Expand Down Expand Up @@ -3375,7 +3381,9 @@ fn gen_declare_program_types(idl: &serde_json::Value) -> syn::Result<Vec<TokenSt
.unwrap_or_default();
let pod_impls = serialization
.is_bytemuck()
.then(|| gen_declare_program_pod_impls(&ident, &generics, &fields))
.then(|| {
gen_declare_program_pod_impls(&ident, &generics, &fields, serialization)
})
.unwrap_or_default();
let impl_generics = &generics.impl_generics;
out.push(match fields {
Expand Down Expand Up @@ -3545,6 +3553,10 @@ impl DeclareTypeSerialization {
fn is_bytemuck(self) -> bool {
matches!(self, Self::Bytemuck | Self::BytemuckUnsafe)
}

fn is_bytemuck_unsafe(self) -> bool {
matches!(self, Self::BytemuckUnsafe)
}
}

fn gen_declare_program_docs(
Expand Down Expand Up @@ -4149,10 +4161,21 @@ fn gen_declare_program_pod_impls(
ident: &Ident,
generics: &DeclareTypeGenerics,
fields: &DeclareTypeFields,
serialization: DeclareTypeSerialization,
) -> TokenStream2 {
let field_types = fields.tys();
let impl_generics = &generics.impl_generics;
let ty_generics = &generics.ty_generics;
// `bytemuckunsafe` is an explicit opt-out of safe Pod derivability:
// imported layouts may include padding or non-Pod fields. Emit the
// unsafe impls without field-Pod / no-padding assertions.
if serialization.is_bytemuck_unsafe() {
return quote! {
unsafe impl #impl_generics anchor_lang::bytemuck::Pod for #ident #ty_generics {}
unsafe impl #impl_generics anchor_lang::bytemuck::Zeroable for #ident #ty_generics {}
};
}

let field_types = fields.tys();
let generic_where_clause = &generics.pod_where_clause;
let where_clause = if field_types.is_empty() {
generic_where_clause.clone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
{
"name": "UnsafeZeroCopyAccount",
"discriminator": [41, 42, 43, 44, 45, 46, 47, 48]
},
{
"name": "PaddedUnsafeAccount",
"discriminator": [51, 52, 53, 54, 55, 56, 57, 58]
}
],
"types": [
Expand Down Expand Up @@ -149,6 +153,43 @@
}
]
}
},
{
"name": "PaddedUnsafeAccount",
"serialization": "bytemuckunsafe",
"repr": {
"kind": "c"
},
"type": {
"kind": "struct",
"fields": [
{
"name": "flag",
"type": "bool"
},
{
"name": "wide",
"type": "u64"
}
]
}
},
{
"name": "PackedUnsafeAccount",
"serialization": "bytemuckunsafe",
"type": {
"kind": "struct",
"fields": [
{
"name": "tag",
"type": "u8"
},
{
"name": "wide",
"type": "u64"
}
]
}
}
]
}
32 changes: 32 additions & 0 deletions tests-v2/src/declare-program/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ fn declared_program_type_serialization_controls_account_traits() {
assert_borsh_account_wrapper_idl::<serialization::ExplicitBorshAccount>();
assert_zero_copy_account::<serialization::ZeroCopyAccount>();
assert_zero_copy_account::<serialization::UnsafeZeroCopyAccount>();
assert_zero_copy_account::<serialization::PaddedUnsafeAccount>();

fn assert_pod<T: anchor_lang::bytemuck::Pod + anchor_lang::bytemuck::Zeroable>() {}
assert_pod::<serialization::PackedUnsafeAccount>();

assert!(
<serialization::ImplicitBorshAccount as anchor_lang::IdlAccountType>::__IDL_ACCOUNT_ENTRY
Expand Down Expand Up @@ -86,6 +90,10 @@ fn declared_program_type_serialization_controls_account_traits() {
<serialization::UnsafeZeroCopyAccount as Discriminator>::DISCRIMINATOR,
&[41, 42, 43, 44, 45, 46, 47, 48]
);
assert_eq!(
<serialization::PaddedUnsafeAccount as Discriminator>::DISCRIMINATOR,
&[51, 52, 53, 54, 55, 56, 57, 58]
);

assert_eq!(
<serialization::ImplicitBorshAccount as anchor_lang::Owner>::OWNER,
Expand Down Expand Up @@ -123,6 +131,30 @@ fn declared_program_type_serialization_controls_account_traits() {
assert_eq!(&zero_bytes[..8], &0x0102_0304_0506_0708u64.to_le_bytes());
assert_eq!(&zero_bytes[8..12], &0x1112_1314u32.to_le_bytes());
assert_eq!(&zero_bytes[12..16], b"zero");

// `bytemuckunsafe` must accept layouts that fail safe Pod checks:
// non-Pod `bool` plus repr(C) padding after it, and the v1 default
// packed layout when the IDL omits repr.
assert_eq!(core::mem::size_of::<serialization::PaddedUnsafeAccount>(), 16);
assert_eq!(core::mem::align_of::<serialization::PaddedUnsafeAccount>(), 8);
let mut padded_bytes = [0u8; 16];
padded_bytes[0] = 1;
padded_bytes[8..16].copy_from_slice(&0x0102_0304_0506_0708u64.to_le_bytes());
let padded: serialization::PaddedUnsafeAccount =
anchor_lang::bytemuck::pod_read_unaligned(&padded_bytes);
assert!(padded.flag);
assert_eq!(padded.wide, 0x0102_0304_0506_0708);

assert_eq!(core::mem::size_of::<serialization::PackedUnsafeAccount>(), 9);
assert_eq!(core::mem::align_of::<serialization::PackedUnsafeAccount>(), 1);
let mut packed_bytes = [0u8; 9];
packed_bytes[0] = 7;
packed_bytes[1..9].copy_from_slice(&99u64.to_le_bytes());
let packed: serialization::PackedUnsafeAccount =
anchor_lang::bytemuck::pod_read_unaligned(&packed_bytes);
assert_eq!(packed.tag, 7);
let packed_wide = packed.wide;
assert_eq!(packed_wide, 99);
}

#[test]
Expand Down
Loading