Skip to content
Closed
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
1 change: 1 addition & 0 deletions sdk/define-syscall/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ define_syscall!(fn sol_remaining_compute_units() -> u64);
define_syscall!(fn sol_alt_bn128_compression(op: u64, input: *const u8, input_size: u64, result: *mut u8) -> u64);
define_syscall!(fn sol_get_sysvar(sysvar_id_addr: *const u8, result: *mut u8, offset: u64, length: u64) -> u64);
define_syscall!(fn sol_get_epoch_stake(vote_address: *const u8) -> u64);
define_syscall!(fn sol_panic_(filename: *const u8, filename_len: u64, line: u64, column: u64));

// these are to be deprecated once they are superceded by sol_get_sysvar
define_syscall!(fn sol_get_clock_sysvar(addr: *mut u8) -> u64);
Expand Down
38 changes: 38 additions & 0 deletions sdk/program-entrypoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,44 @@ macro_rules! custom_panic_default {
};
}

/// This is a more space efficient implementation of custom panic.
/// It is about 1kb in size and depends on the unstable feature `panic_info_message`,
/// which is going to be stabilized in Rust 1.84.
///
/// It works just like the default custom panic, except that dynamic generated error
/// messages are not shown. For instance, trying to access an index out of bounds of a vector
/// would give us `index Y is out of bounds for length X`. As the length is only know at runtime
/// time, this message is elided.
///
/// All messages known at compile time are correctly displayed, e.g. `called unwrap in a None`,
/// file names, line and column numbers.
#[macro_export]
macro_rules! custom_panic_space_efficient {
Copy link
Author

Choose a reason for hiding this comment

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

I'm not yet replacing the exiting custom panic, because this new one requires Rust 1.84, which is going to be the next toolchain upgrade.

() => {
#[cfg(all(not(feature = "custom-panic"), target_os = "solana"))]
Copy link

Choose a reason for hiding this comment

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

I think we need to remove the "gate" on the not(feature = "custom-panic"). Since the entrypoint! macro always uses the custom_panic_default!, the only way to "disable" the default panic handler is to enable the feature "custom-panic" on the program. This would also disable the custom_panic_space_efficient! as it is.

Removing the not(feature = "custom-panic") in this case would solve this – a program can enable the "custom-panic" feature to disable the default and then use custom_panic_space_efficient!.

Copy link
Author

Choose a reason for hiding this comment

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

My idea is that the space efficient implementation will replace the existing one, once we have Rust 1.84 or higher in the platform tools.

What do you think of this?

#[no_mangle]
fn custom_panic(info: &core::panic::PanicInfo<'_>) {
if let Some(Some(mm)) = info.message().map(|mes| mes.as_str()) {
let mes = mm.as_bytes();
unsafe {
solana_program::syscalls::sol_log_(mes.as_ptr(), mes.len() as u64);
}
}

if let Some(loc) = info.location() {
unsafe {
solana_program::syscalls::sol_panic_(
loc.file().as_ptr(),
loc.file().len() as u64,
loc.line() as u64,
loc.column() as u64,
);
}
}
}
};
}

Copy link
Author

Choose a reason for hiding this comment

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

I can add some test programs with this implementation. I'm looking for early feedback now.

/// The bump allocator used as the default rust heap when running programs.
pub struct BumpAllocator {
pub start: usize,
Expand Down
2 changes: 1 addition & 1 deletion sdk/program/src/syscalls/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use solana_define_syscall::definitions::{
sol_curve_group_op, sol_curve_multiscalar_mul, sol_curve_pairing_map, sol_curve_validate_point,
sol_get_clock_sysvar, sol_get_epoch_rewards_sysvar, sol_get_epoch_schedule_sysvar,
sol_get_epoch_stake, sol_get_fees_sysvar, sol_get_last_restart_slot, sol_get_rent_sysvar,
sol_get_sysvar, sol_keccak256, sol_remaining_compute_units,
sol_get_sysvar, sol_keccak256, sol_panic_, sol_remaining_compute_units,
};
#[cfg(target_feature = "static-syscalls")]
pub use solana_define_syscall::sys_hash;
Expand Down
Loading