Skip to content
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
9 changes: 8 additions & 1 deletion program/src/processor/initialize.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::mem::{align_of, size_of};

use pinocchio::{
cpi::{Seed, Signer},
error::ProgramError,
Expand Down Expand Up @@ -215,8 +217,13 @@ struct Initialize {
pub data_source: u8,
}

// Enforces 1-byte alignment for the struct.
const _: () = {
assert!(align_of::<Initialize>() == 1);
};

impl Initialize {
const LEN: usize = core::mem::size_of::<Self>();
const LEN: usize = size_of::<Self>();

/// # Safety
///
Expand Down
9 changes: 8 additions & 1 deletion program/src/processor/set_data.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::mem::{align_of, size_of};

use pinocchio::{account::AccountView, error::ProgramError, ProgramResult, Resize};

use crate::state::{
Expand Down Expand Up @@ -164,8 +166,13 @@ struct SetData {
// - `&[u8]`: remaining data
}

// Enforces 1-byte alignment for the struct.
const _: () = {
assert!(align_of::<SetData>() == 1);
};

impl SetData {
const LEN: usize = core::mem::size_of::<Self>();
const LEN: usize = size_of::<Self>();

/// # Safety
///
Expand Down
1 change: 0 additions & 1 deletion program/src/processor/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ pub fn write(accounts: &mut [AccountView], instruction_data: &[u8]) -> ProgramRe
}

/// Instruction data expected by the `Write` instruction.
#[repr(C)]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is not needed.

struct Write<'a> {
/// Offset to write to.
offset: &'a [u8; 4],
Expand Down
7 changes: 7 additions & 0 deletions program/src/state/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::mem::align_of;

use pinocchio::{
account::{AccountView, Ref},
error::ProgramError,
Expand Down Expand Up @@ -41,6 +43,11 @@ pub struct Buffer {
_padding: [u8; 14],
}

// Enforces 1-byte alignment for the struct.
const _: () = {
assert!(align_of::<Buffer>() == 1);
};

impl Buffer {
/// The minimum size of a `Buffer` (`96` bytes).
pub const LEN: usize = core::mem::size_of::<Buffer>();
Expand Down
12 changes: 11 additions & 1 deletion program/src/state/data.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::{mem::align_of, str::from_utf8};

use pinocchio::{error::ProgramError, Address};

use super::{DataSource, ZeroableOption};
Expand All @@ -23,12 +25,15 @@ impl<'a> Data<'a> {
Ok(match data_source {
DataSource::Direct => Data::Direct(DirectData(bytes)),
DataSource::Url => Data::Url(UrlData(
core::str::from_utf8(bytes).map_err(|_| ProgramError::InvalidArgument)?,
from_utf8(bytes).map_err(|_| ProgramError::InvalidArgument)?,
)),
DataSource::External => {
if bytes.len() < ExternalData::LEN {
return Err(ProgramError::InvalidArgument);
}
if !(bytes.as_ptr() as usize).is_multiple_of(align_of::<ExternalData>()) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ExternalData is the only struct that has an alignment different than 1.

return Err(ProgramError::InvalidArgument);
}
// SAFETY: `bytes` was validated to have the expected length
// to hold a `Data::External` reference.
Data::External(unsafe { &*(bytes.as_ptr() as *const ExternalData) })
Expand Down Expand Up @@ -67,6 +72,11 @@ pub struct ExternalData {
pub length: ZeroableOption<u32>,
}

// Enforces 4-byte alignment for the `ExternalData` struct.
const _: () = {
assert!(align_of::<ExternalData>() == 4);
};

impl ExternalData {
pub const LEN: usize = core::mem::size_of::<ExternalData>();
}
7 changes: 7 additions & 0 deletions program/src/state/header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::mem::align_of;

use pinocchio::{
account::{AccountView, Ref},
error::ProgramError,
Expand Down Expand Up @@ -56,6 +58,11 @@ pub struct Header {
_padding: [u8; 5],
}

// Enforces 1-byte alignment for the struct.
const _: () = {
assert!(align_of::<Header>() == 1);
};

impl Header {
/// Length of the header (96 bytes).
pub const LEN: usize = core::mem::size_of::<Header>();
Expand Down
22 changes: 18 additions & 4 deletions program/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ pub mod buffer;
pub mod data;
pub mod header;

use pinocchio::{address::ADDRESS_BYTES, error::ProgramError, Address, ProgramResult};
use core::mem::size_of;

use data::{Data, ExternalData};
use header::Header;
use pinocchio::{address::ADDRESS_BYTES, error::ProgramError, Address, ProgramResult};

use crate::error::ProgramMetadataError;
use crate::{
error::ProgramMetadataError,
state::{
buffer::Buffer,
data::{Data, ExternalData},
header::Header,
},
};

// Make sure both `Header` and `Buffer` structs have the same size.
const _: () = {
assert!(
size_of::<Header>() == size_of::<Buffer>(),
"Header and Buffer should be the same size"
);
};

/// The length of the seed used to derive the metadata account address.
pub const SEED_LEN: usize = 16;
Expand Down
Loading