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

CI clippy and fmt for all #23599

Merged
merged 1 commit into from
Mar 11, 2022
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
16 changes: 5 additions & 11 deletions ci/test-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,20 @@ if [[ $CI_BASE_BRANCH = "$EDGE_CHANNEL" ]]; then
exit "$check_status"
fi

# Ensure nightly and --benches
# Ensure nightly and --benches
_ scripts/cargo-for-all-lock-files.sh nightly check --locked --all-targets
else
echo "Note: cargo-for-all-lock-files.sh skipped because $CI_BASE_BRANCH != $EDGE_CHANNEL"
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
# 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

{
cd programs/bpf
_ "$cargo" nightly clippy --all -- --deny=warnings --allow=clippy::missing_safety_doc
_ "$cargo" nightly fmt --all -- --check
}
_ ci/do-audit.sh

echo --- ok
6 changes: 2 additions & 4 deletions programs/bpf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ fn rerun_if_changed(files: &[&str], directories: &[&str], excludes: &[&str]) {
fn main() {
let bpf_c = env::var("CARGO_FEATURE_BPF_C").is_ok();
if bpf_c {
let install_dir =
"OUT_DIR=../target/".to_string() + &env::var("PROFILE").unwrap() + &"/bpf".to_string();
let install_dir = "OUT_DIR=../target/".to_string() + &env::var("PROFILE").unwrap() + "/bpf";

println!("cargo:warning=(not a warning) Building C-based BPF programs");
assert!(Command::new("make")
Expand All @@ -56,8 +55,7 @@ fn main() {

let bpf_rust = env::var("CARGO_FEATURE_BPF_RUST").is_ok();
if bpf_rust {
let install_dir =
"target/".to_string() + &env::var("PROFILE").unwrap() + &"/bpf".to_string();
let install_dir = "target/".to_string() + &env::var("PROFILE").unwrap() + "/bpf";

let rust_programs = [
"128bit",
Expand Down
74 changes: 56 additions & 18 deletions programs/bpf/rust/invoke/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_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");
let instruction = create_instruction(
Expand All @@ -42,11 +46,15 @@ fn do_nested_invokes(num_nested_invokes: u64, accounts: &[AccountInfo]) -> Progr

assert_eq!(
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!(
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(())
}
Expand Down Expand Up @@ -87,17 +95,23 @@ fn process_instruction(
&[&[b"You pass butter", &[bump_seed1]]],
)?;

assert_eq!(accounts[FROM_INDEX].lamports(), from_lamports - 42);
assert_eq!(accounts[DERIVED_KEY1_INDEX].lamports(), to_lamports + 42);
assert_eq!(
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!(
accounts[DERIVED_KEY1_INDEX].data_len(),
MAX_PERMITTED_DATA_INCREASE
);
let mut data = accounts[DERIVED_KEY1_INDEX].try_borrow_mut_data()?;
assert_eq!(data[MAX_PERMITTED_DATA_INCREASE - 1], 0);
data[MAX_PERMITTED_DATA_INCREASE - 1] = 0x0f;
assert_eq!(data[MAX_PERMITTED_DATA_INCREASE - 1], 0x0f);
assert_eq!(data[MAX_PERMITTED_DATA_INCREASE.saturating_sub(1)], 0);
data[MAX_PERMITTED_DATA_INCREASE.saturating_sub(1)] = 0x0f;
assert_eq!(data[MAX_PERMITTED_DATA_INCREASE.saturating_sub(1)], 0x0f);
for i in 0..20 {
data[i] = i as u8;
}
Expand All @@ -113,8 +127,14 @@ fn process_instruction(
1,
);
invoke(&instruction, accounts)?;
assert_eq!(accounts[FROM_INDEX].lamports(), from_lamports - 1);
assert_eq!(accounts[DERIVED_KEY1_INDEX].lamports(), to_lamports + 1);
assert_eq!(
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");
Expand Down Expand Up @@ -357,11 +377,17 @@ fn process_instruction(
);
invoke(&instruction, accounts)?;

assert_eq!(accounts[FROM_INDEX].lamports(), from_lamports - 1);
assert_eq!(accounts[DERIVED_KEY2_INDEX].lamports(), to_lamports + 1);
assert_eq!(
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()?;
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 {
assert_eq!(data[i], i as u8);
}
Expand Down Expand Up @@ -608,9 +634,15 @@ fn process_instruction(

// set account to executable and subtract lamports
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
**(*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(
*program_id,
Expand All @@ -623,7 +655,10 @@ fn process_instruction(
let _ = invoke(&instruction, &accounts);

// 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 => {
msg!("Test calling precompiled program from cpi");
Expand All @@ -633,7 +668,10 @@ fn process_instruction(
}
ADD_LAMPORTS => {
// 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 => {
set_return_data(&[1u8; 1028]);
Expand Down
6 changes: 3 additions & 3 deletions programs/bpf/rust/realloc/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn realloc(program_id: &Pubkey, address: &Pubkey, size: usize, bump: &mut u8
let mut instruction_data = vec![REALLOC, *bump];
instruction_data.extend_from_slice(&size.to_le_bytes());

*bump += 1;
*bump = bump.saturating_add(1);

Instruction::new_with_bytes(
*program_id,
Expand All @@ -37,7 +37,7 @@ pub fn realloc_extend(
let mut instruction_data = vec![REALLOC_EXTEND, *bump];
instruction_data.extend_from_slice(&size.to_le_bytes());

*bump += 1;
*bump = bump.saturating_add(1);

Instruction::new_with_bytes(
*program_id,
Expand All @@ -61,7 +61,7 @@ pub fn realloc_extend_and_fill(
];
instruction_data.extend_from_slice(&size.to_le_bytes());

*bump += 1;
*bump = bump.saturating_add(1);

Instruction::new_with_bytes(
*program_id,
Expand Down
18 changes: 12 additions & 6 deletions programs/bpf/rust/realloc/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn process_instruction(
REALLOC_EXTEND => {
let pre_len = account.data_len();
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);
account.realloc(new_len, false)?;
assert_eq!(new_len, account.data_len());
Expand All @@ -45,7 +45,7 @@ fn process_instruction(
let pre_len = account.data_len();
let fill = instruction_data[2];
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);
account.realloc(new_len, false)?;
assert_eq!(new_len, account.data_len());
Expand All @@ -61,8 +61,11 @@ fn process_instruction(
REALLOC_AND_ASSIGN_TO_SELF_VIA_SYSTEM_PROGRAM => {
msg!("realloc and assign to self via system program");
let pre_len = account.data_len();
account.realloc(pre_len + MAX_PERMITTED_DATA_INCREASE, false)?;
assert_eq!(pre_len + MAX_PERMITTED_DATA_INCREASE, account.data_len());
account.realloc(pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE), false)?;
assert_eq!(
pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE),
account.data_len()
);
invoke(
&system_instruction::assign(account.key, program_id),
accounts,
Expand All @@ -77,8 +80,11 @@ fn process_instruction(
accounts,
)?;
assert_eq!(account.owner, program_id);
account.realloc(pre_len + MAX_PERMITTED_DATA_INCREASE, false)?;
assert_eq!(account.data_len(), pre_len + MAX_PERMITTED_DATA_INCREASE);
account.realloc(pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE), false)?;
assert_eq!(
account.data_len(),
pre_len.saturating_add(MAX_PERMITTED_DATA_INCREASE)
);
}
DEALLOC_AND_ASSIGN_TO_CALLER => {
msg!("dealloc and assign to caller");
Expand Down
27 changes: 18 additions & 9 deletions programs/bpf/rust/realloc_invoke/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn process_instruction(
&realloc(
invoke_program_id,
account.key,
MAX_PERMITTED_DATA_INCREASE + 1,
MAX_PERMITTED_DATA_INCREASE.saturating_add(1),
&mut bump,
),
accounts,
Expand All @@ -69,7 +69,10 @@ fn process_instruction(
),
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 => {
msg!("invoke realloc max twice");
Expand All @@ -82,10 +85,13 @@ fn process_instruction(
),
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());
account.realloc(new_len + MAX_PERMITTED_DATA_INCREASE, false)?;
assert_eq!(new_len + MAX_PERMITTED_DATA_INCREASE, account.data_len());
account.realloc(new_len.saturating_add(MAX_PERMITTED_DATA_INCREASE), false)?;
assert_eq!(
new_len.saturating_add(MAX_PERMITTED_DATA_INCREASE),
account.data_len()
);
}
INVOKE_REALLOC_AND_ASSIGN => {
msg!("invoke realloc and assign");
Expand All @@ -97,7 +103,10 @@ fn process_instruction(
),
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());
}
INVOKE_REALLOC_AND_ASSIGN_TO_SELF_VIA_SYSTEM_PROGRAM => {
Expand Down Expand Up @@ -197,8 +206,8 @@ fn process_instruction(
accounts,
)?;
assert_eq!(pre_len, accounts[1].data_len());
accounts[1].realloc(pre_len + 1, false)?;
assert_eq!(pre_len + 1, accounts[1].data_len());
accounts[1].realloc(pre_len.saturating_add(1), false)?;
assert_eq!(pre_len.saturating_add(1), accounts[1].data_len());
assert_eq!(accounts[1].owner, program_id);
let final_len: usize = 200;
let mut new_instruction_data = vec![];
Expand All @@ -221,7 +230,7 @@ fn process_instruction(
msg!("realloc zerod");
let (bytes, _) = instruction_data[2..].split_at(std::mem::size_of::<usize>());
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);
{
let data = account.try_borrow_mut_data()?;
Expand Down