CI clippy and fmt for all (#23599)

This commit is contained in:
Jack May 2022-03-11 12:07:06 -08:00 committed by GitHub
parent d20dd21600
commit 7ee7fc6f58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 96 additions and 51 deletions

View File

@ -63,20 +63,14 @@ else
echo "Note: cargo-for-all-lock-files.sh skipped because $CI_BASE_BRANCH != $EDGE_CHANNEL" echo "Note: cargo-for-all-lock-files.sh skipped because $CI_BASE_BRANCH != $EDGE_CHANNEL"
fi fi
_ ci/order-crates-for-publishing.py _ ci/order-crates-for-publishing.py
# -Z... is needed because of clippy bug: https://github.com/rust-lang/rust-clippy/issues/4612 # -Z... is needed because of clippy bug: https://github.com/rust-lang/rust-clippy/issues/4612
# run nightly clippy for `sdk/` as there's a moderate amount of nightly-only code there # run nightly clippy for `sdk/` as there's a moderate amount of nightly-only code there
_ "$cargo" nightly clippy -Zunstable-options --workspace --all-targets -- --deny=warnings --deny=clippy::integer_arithmetic _ scripts/cargo-for-all-lock-files.sh -- nightly clippy -Zunstable-options --all-targets -- --deny=warnings --deny=clippy::integer_arithmetic
_ "$cargo" nightly fmt --all -- --check _ scripts/cargo-for-all-lock-files.sh -- nightly fmt --all -- --check
_ ci/do-audit.sh _ ci/do-audit.sh
{
cd programs/bpf
_ "$cargo" nightly clippy --all -- --deny=warnings --allow=clippy::missing_safety_doc
_ "$cargo" nightly fmt --all -- --check
}
echo --- ok echo --- ok

View File

@ -39,8 +39,7 @@ fn rerun_if_changed(files: &[&str], directories: &[&str], excludes: &[&str]) {
fn main() { fn main() {
let bpf_c = env::var("CARGO_FEATURE_BPF_C").is_ok(); let bpf_c = env::var("CARGO_FEATURE_BPF_C").is_ok();
if bpf_c { if bpf_c {
let install_dir = let install_dir = "OUT_DIR=../target/".to_string() + &env::var("PROFILE").unwrap() + "/bpf";
"OUT_DIR=../target/".to_string() + &env::var("PROFILE").unwrap() + &"/bpf".to_string();
println!("cargo:warning=(not a warning) Building C-based BPF programs"); println!("cargo:warning=(not a warning) Building C-based BPF programs");
assert!(Command::new("make") assert!(Command::new("make")
@ -56,8 +55,7 @@ fn main() {
let bpf_rust = env::var("CARGO_FEATURE_BPF_RUST").is_ok(); let bpf_rust = env::var("CARGO_FEATURE_BPF_RUST").is_ok();
if bpf_rust { if bpf_rust {
let install_dir = let install_dir = "target/".to_string() + &env::var("PROFILE").unwrap() + "/bpf";
"target/".to_string() + &env::var("PROFILE").unwrap() + &"/bpf".to_string();
let rust_programs = [ let rust_programs = [
"128bit", "128bit",

View File

@ -23,8 +23,12 @@ fn do_nested_invokes(num_nested_invokes: u64, accounts: &[AccountInfo]) -> Progr
let pre_argument_lamports = accounts[ARGUMENT_INDEX].lamports(); let pre_argument_lamports = accounts[ARGUMENT_INDEX].lamports();
let pre_invoke_argument_lamports = accounts[INVOKED_ARGUMENT_INDEX].lamports(); let pre_invoke_argument_lamports = accounts[INVOKED_ARGUMENT_INDEX].lamports();
**accounts[ARGUMENT_INDEX].lamports.borrow_mut() -= 5; {
**accounts[INVOKED_ARGUMENT_INDEX].lamports.borrow_mut() += 5; let mut lamports = (*accounts[ARGUMENT_INDEX].lamports).borrow_mut();
**lamports = (*lamports).saturating_sub(5);
let mut lamports = (*accounts[INVOKED_ARGUMENT_INDEX].lamports).borrow_mut();
**lamports = (*lamports).saturating_add(5);
}
msg!("First invoke"); msg!("First invoke");
let instruction = create_instruction( let instruction = create_instruction(
@ -42,11 +46,15 @@ fn do_nested_invokes(num_nested_invokes: u64, accounts: &[AccountInfo]) -> Progr
assert_eq!( assert_eq!(
accounts[ARGUMENT_INDEX].lamports(), accounts[ARGUMENT_INDEX].lamports(),
pre_argument_lamports - 5 + (2 * num_nested_invokes) pre_argument_lamports
.saturating_sub(5)
.saturating_add(2_u64.saturating_mul(num_nested_invokes))
); );
assert_eq!( assert_eq!(
accounts[INVOKED_ARGUMENT_INDEX].lamports(), accounts[INVOKED_ARGUMENT_INDEX].lamports(),
pre_invoke_argument_lamports + 5 - (2 * num_nested_invokes) pre_invoke_argument_lamports
.saturating_add(5)
.saturating_sub(2_u64.saturating_mul(num_nested_invokes))
); );
Ok(()) Ok(())
} }
@ -87,17 +95,23 @@ fn process_instruction(
&[&[b"You pass butter", &[bump_seed1]]], &[&[b"You pass butter", &[bump_seed1]]],
)?; )?;
assert_eq!(accounts[FROM_INDEX].lamports(), from_lamports - 42); assert_eq!(
assert_eq!(accounts[DERIVED_KEY1_INDEX].lamports(), to_lamports + 42); accounts[FROM_INDEX].lamports(),
from_lamports.saturating_sub(42)
);
assert_eq!(
accounts[DERIVED_KEY1_INDEX].lamports(),
to_lamports.saturating_add(42)
);
assert_eq!(program_id, accounts[DERIVED_KEY1_INDEX].owner); assert_eq!(program_id, accounts[DERIVED_KEY1_INDEX].owner);
assert_eq!( assert_eq!(
accounts[DERIVED_KEY1_INDEX].data_len(), accounts[DERIVED_KEY1_INDEX].data_len(),
MAX_PERMITTED_DATA_INCREASE MAX_PERMITTED_DATA_INCREASE
); );
let mut data = accounts[DERIVED_KEY1_INDEX].try_borrow_mut_data()?; let mut data = accounts[DERIVED_KEY1_INDEX].try_borrow_mut_data()?;
assert_eq!(data[MAX_PERMITTED_DATA_INCREASE - 1], 0); assert_eq!(data[MAX_PERMITTED_DATA_INCREASE.saturating_sub(1)], 0);
data[MAX_PERMITTED_DATA_INCREASE - 1] = 0x0f; data[MAX_PERMITTED_DATA_INCREASE.saturating_sub(1)] = 0x0f;
assert_eq!(data[MAX_PERMITTED_DATA_INCREASE - 1], 0x0f); assert_eq!(data[MAX_PERMITTED_DATA_INCREASE.saturating_sub(1)], 0x0f);
for i in 0..20 { for i in 0..20 {
data[i] = i as u8; data[i] = i as u8;
} }
@ -113,8 +127,14 @@ fn process_instruction(
1, 1,
); );
invoke(&instruction, accounts)?; invoke(&instruction, accounts)?;
assert_eq!(accounts[FROM_INDEX].lamports(), from_lamports - 1); assert_eq!(
assert_eq!(accounts[DERIVED_KEY1_INDEX].lamports(), to_lamports + 1); accounts[FROM_INDEX].lamports(),
from_lamports.saturating_sub(1)
);
assert_eq!(
accounts[DERIVED_KEY1_INDEX].lamports(),
to_lamports.saturating_add(1)
);
} }
msg!("Test data translation"); msg!("Test data translation");
@ -357,11 +377,17 @@ fn process_instruction(
); );
invoke(&instruction, accounts)?; invoke(&instruction, accounts)?;
assert_eq!(accounts[FROM_INDEX].lamports(), from_lamports - 1); assert_eq!(
assert_eq!(accounts[DERIVED_KEY2_INDEX].lamports(), to_lamports + 1); accounts[FROM_INDEX].lamports(),
from_lamports.saturating_sub(1)
);
assert_eq!(
accounts[DERIVED_KEY2_INDEX].lamports(),
to_lamports.saturating_add(1)
);
let data = accounts[DERIVED_KEY2_INDEX].try_borrow_mut_data()?; let data = accounts[DERIVED_KEY2_INDEX].try_borrow_mut_data()?;
assert_eq!(data[0], 0x0e); assert_eq!(data[0], 0x0e);
assert_eq!(data[MAX_PERMITTED_DATA_INCREASE - 1], 0x0f); assert_eq!(data[MAX_PERMITTED_DATA_INCREASE.saturating_sub(1)], 0x0f);
for i in 1..20 { for i in 1..20 {
assert_eq!(data[i], i as u8); assert_eq!(data[i], i as u8);
} }
@ -608,9 +634,15 @@ fn process_instruction(
// set account to executable and subtract lamports // set account to executable and subtract lamports
accounts[ARGUMENT_INDEX].executable = true; accounts[ARGUMENT_INDEX].executable = true;
**(*accounts[ARGUMENT_INDEX].lamports).borrow_mut() -= 1; {
let mut lamports = (*accounts[ARGUMENT_INDEX].lamports).borrow_mut();
**lamports = (*lamports).saturating_sub(1);
}
// add lamports to dest account // add lamports to dest account
**(*accounts[DERIVED_KEY1_INDEX].lamports).borrow_mut() += 1; {
let mut lamports = (*accounts[DERIVED_KEY1_INDEX].lamports).borrow_mut();
**lamports = (*lamports).saturating_add(1);
}
let instruction = create_instruction( let instruction = create_instruction(
*program_id, *program_id,
@ -623,7 +655,10 @@ fn process_instruction(
let _ = invoke(&instruction, &accounts); let _ = invoke(&instruction, &accounts);
// reset executable account // reset executable account
**(*accounts[ARGUMENT_INDEX].lamports).borrow_mut() += 1; {
let mut lamports = (*accounts[ARGUMENT_INDEX].lamports).borrow_mut();
**lamports = (*lamports).saturating_add(1);
}
} }
TEST_CALL_PRECOMPILE => { TEST_CALL_PRECOMPILE => {
msg!("Test calling precompiled program from cpi"); msg!("Test calling precompiled program from cpi");
@ -633,7 +668,10 @@ fn process_instruction(
} }
ADD_LAMPORTS => { ADD_LAMPORTS => {
// make sure the total balance is fine // make sure the total balance is fine
**accounts[0].lamports.borrow_mut() += 1; {
let mut lamports = (*accounts[0].lamports).borrow_mut();
**lamports = (*lamports).saturating_add(1);
}
} }
TEST_RETURN_DATA_TOO_LARGE => { TEST_RETURN_DATA_TOO_LARGE => {
set_return_data(&[1u8; 1028]); set_return_data(&[1u8; 1028]);

View File

@ -19,7 +19,7 @@ pub fn realloc(program_id: &Pubkey, address: &Pubkey, size: usize, bump: &mut u8
let mut instruction_data = vec![REALLOC, *bump]; let mut instruction_data = vec![REALLOC, *bump];
instruction_data.extend_from_slice(&size.to_le_bytes()); instruction_data.extend_from_slice(&size.to_le_bytes());
*bump += 1; *bump = bump.saturating_add(1);
Instruction::new_with_bytes( Instruction::new_with_bytes(
*program_id, *program_id,
@ -37,7 +37,7 @@ pub fn realloc_extend(
let mut instruction_data = vec![REALLOC_EXTEND, *bump]; let mut instruction_data = vec![REALLOC_EXTEND, *bump];
instruction_data.extend_from_slice(&size.to_le_bytes()); instruction_data.extend_from_slice(&size.to_le_bytes());
*bump += 1; *bump = bump.saturating_add(1);
Instruction::new_with_bytes( Instruction::new_with_bytes(
*program_id, *program_id,
@ -61,7 +61,7 @@ pub fn realloc_extend_and_fill(
]; ];
instruction_data.extend_from_slice(&size.to_le_bytes()); instruction_data.extend_from_slice(&size.to_le_bytes());
*bump += 1; *bump = bump.saturating_add(1);
Instruction::new_with_bytes( Instruction::new_with_bytes(
*program_id, *program_id,

View File

@ -36,7 +36,7 @@ fn process_instruction(
REALLOC_EXTEND => { REALLOC_EXTEND => {
let pre_len = account.data_len(); let pre_len = account.data_len();
let (bytes, _) = instruction_data[2..].split_at(std::mem::size_of::<usize>()); let (bytes, _) = instruction_data[2..].split_at(std::mem::size_of::<usize>());
let new_len = pre_len + usize::from_le_bytes(bytes.try_into().unwrap()); let new_len = pre_len.saturating_add(usize::from_le_bytes(bytes.try_into().unwrap()));
msg!("realloc extend by {}", new_len); msg!("realloc extend by {}", new_len);
account.realloc(new_len, false)?; account.realloc(new_len, false)?;
assert_eq!(new_len, account.data_len()); assert_eq!(new_len, account.data_len());
@ -45,7 +45,7 @@ fn process_instruction(
let pre_len = account.data_len(); let pre_len = account.data_len();
let fill = instruction_data[2]; let fill = instruction_data[2];
let (bytes, _) = instruction_data[4..].split_at(std::mem::size_of::<usize>()); let (bytes, _) = instruction_data[4..].split_at(std::mem::size_of::<usize>());
let new_len = pre_len + usize::from_le_bytes(bytes.try_into().unwrap()); let new_len = pre_len.saturating_add(usize::from_le_bytes(bytes.try_into().unwrap()));
msg!("realloc extend by {}", new_len); msg!("realloc extend by {}", new_len);
account.realloc(new_len, false)?; account.realloc(new_len, false)?;
assert_eq!(new_len, account.data_len()); assert_eq!(new_len, account.data_len());
@ -61,8 +61,11 @@ fn process_instruction(
REALLOC_AND_ASSIGN_TO_SELF_VIA_SYSTEM_PROGRAM => { REALLOC_AND_ASSIGN_TO_SELF_VIA_SYSTEM_PROGRAM => {
msg!("realloc and assign to self via system program"); msg!("realloc and assign to self via system program");
let pre_len = account.data_len(); let pre_len = account.data_len();
account.realloc(pre_len + MAX_PERMITTED_DATA_INCREASE, false)?; account.realloc(pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE), false)?;
assert_eq!(pre_len + MAX_PERMITTED_DATA_INCREASE, account.data_len()); assert_eq!(
pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE),
account.data_len()
);
invoke( invoke(
&system_instruction::assign(account.key, program_id), &system_instruction::assign(account.key, program_id),
accounts, accounts,
@ -77,8 +80,11 @@ fn process_instruction(
accounts, accounts,
)?; )?;
assert_eq!(account.owner, program_id); assert_eq!(account.owner, program_id);
account.realloc(pre_len + MAX_PERMITTED_DATA_INCREASE, false)?; account.realloc(pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE), false)?;
assert_eq!(account.data_len(), pre_len + MAX_PERMITTED_DATA_INCREASE); assert_eq!(
account.data_len(),
pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE)
);
} }
DEALLOC_AND_ASSIGN_TO_CALLER => { DEALLOC_AND_ASSIGN_TO_CALLER => {
msg!("dealloc and assign to caller"); msg!("dealloc and assign to caller");

View File

@ -52,7 +52,7 @@ fn process_instruction(
&realloc( &realloc(
invoke_program_id, invoke_program_id,
account.key, account.key,
MAX_PERMITTED_DATA_INCREASE + 1, MAX_PERMITTED_DATA_INCREASE.saturating_add(1),
&mut bump, &mut bump,
), ),
accounts, accounts,
@ -69,7 +69,10 @@ fn process_instruction(
), ),
accounts, accounts,
)?; )?;
assert_eq!(pre_len + MAX_PERMITTED_DATA_INCREASE, account.data_len()); assert_eq!(
pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE),
account.data_len()
);
} }
INVOKE_REALLOC_MAX_TWICE => { INVOKE_REALLOC_MAX_TWICE => {
msg!("invoke realloc max twice"); msg!("invoke realloc max twice");
@ -82,10 +85,13 @@ fn process_instruction(
), ),
accounts, accounts,
)?; )?;
let new_len = pre_len + MAX_PERMITTED_DATA_INCREASE; let new_len = pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE);
assert_eq!(new_len, account.data_len()); assert_eq!(new_len, account.data_len());
account.realloc(new_len + MAX_PERMITTED_DATA_INCREASE, false)?; account.realloc(new_len.saturating_add(MAX_PERMITTED_DATA_INCREASE), false)?;
assert_eq!(new_len + MAX_PERMITTED_DATA_INCREASE, account.data_len()); assert_eq!(
new_len.saturating_add(MAX_PERMITTED_DATA_INCREASE),
account.data_len()
);
} }
INVOKE_REALLOC_AND_ASSIGN => { INVOKE_REALLOC_AND_ASSIGN => {
msg!("invoke realloc and assign"); msg!("invoke realloc and assign");
@ -97,7 +103,10 @@ fn process_instruction(
), ),
accounts, accounts,
)?; )?;
assert_eq!(pre_len + MAX_PERMITTED_DATA_INCREASE, account.data_len()); assert_eq!(
pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE),
account.data_len()
);
assert_eq!(*account.owner, system_program::id()); assert_eq!(*account.owner, system_program::id());
} }
INVOKE_REALLOC_AND_ASSIGN_TO_SELF_VIA_SYSTEM_PROGRAM => { INVOKE_REALLOC_AND_ASSIGN_TO_SELF_VIA_SYSTEM_PROGRAM => {
@ -197,8 +206,8 @@ fn process_instruction(
accounts, accounts,
)?; )?;
assert_eq!(pre_len, accounts[1].data_len()); assert_eq!(pre_len, accounts[1].data_len());
accounts[1].realloc(pre_len + 1, false)?; accounts[1].realloc(pre_len.saturating_add(1), false)?;
assert_eq!(pre_len + 1, accounts[1].data_len()); assert_eq!(pre_len.saturating_add(1), accounts[1].data_len());
assert_eq!(accounts[1].owner, program_id); assert_eq!(accounts[1].owner, program_id);
let final_len: usize = 200; let final_len: usize = 200;
let mut new_instruction_data = vec![]; let mut new_instruction_data = vec![];
@ -221,7 +230,7 @@ fn process_instruction(
msg!("realloc zerod"); msg!("realloc zerod");
let (bytes, _) = instruction_data[2..].split_at(std::mem::size_of::<usize>()); let (bytes, _) = instruction_data[2..].split_at(std::mem::size_of::<usize>());
let pre_len = usize::from_le_bytes(bytes.try_into().unwrap()); let pre_len = usize::from_le_bytes(bytes.try_into().unwrap());
let new_len = pre_len * 2; let new_len = pre_len.saturating_mul(2);
assert_eq!(pre_len, 100); assert_eq!(pre_len, 100);
{ {
let data = account.try_borrow_mut_data()?; let data = account.try_borrow_mut_data()?;