Skip to content

Commit 090bb3e

Browse files
committed
Use sol_panic_
1 parent 04166f9 commit 090bb3e

File tree

3 files changed

+12
-67
lines changed

3 files changed

+12
-67
lines changed

sdk/define-syscall/src/definitions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ define_syscall!(fn sol_remaining_compute_units() -> u64);
3131
define_syscall!(fn sol_alt_bn128_compression(op: u64, input: *const u8, input_size: u64, result: *mut u8) -> u64);
3232
define_syscall!(fn sol_get_sysvar(sysvar_id_addr: *const u8, result: *mut u8, offset: u64, length: u64) -> u64);
3333
define_syscall!(fn sol_get_epoch_stake(vote_address: *const u8) -> u64);
34+
define_syscall!(fn sol_panic_(filename: *const u8, filename_len: u64, line: u64, column: u64));
3435

3536
// these are to be deprecated once they are superceded by sol_get_sysvar
3637
define_syscall!(fn sol_get_clock_sysvar(addr: *mut u8) -> u64);

sdk/program-entrypoint/src/lib.rs

Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -296,77 +296,21 @@ macro_rules! custom_panic_space_efficient {
296296
#[cfg(all(not(feature = "custom-panic"), target_os = "solana"))]
297297
#[no_mangle]
298298
fn custom_panic(info: &core::panic::PanicInfo<'_>) {
299-
#[inline(never)]
300-
unsafe fn write_num(mut num: u32, dst: *mut u8) -> usize {
301-
let mut buf: [u8; 10] = [0; 10];
302-
let init_ptr = buf.as_mut_ptr().add(11);
303-
let mut write_ptr = init_ptr;
304-
while num > 0 {
305-
write_ptr = write_ptr.sub(1);
306-
// SAFETY: The pointer will always be within *buf and *buf+10
307-
*write_ptr = (num % 10) as u8 + 48;
308-
num /= 10;
309-
}
310-
*dst = 58;
311-
let dst = dst.add(1);
312-
let len = init_ptr.offset_from(write_ptr) as usize;
313-
// SAFETY: The copy length will never be greater than 10.
314-
std::ptr::copy_nonoverlapping(write_ptr, dst, len);
315-
len + 1
316-
}
317-
318-
if let Some(loc) = info.location() {
319-
const MAX_LEN: usize = 200;
320-
let filename = loc.file().as_bytes();
321-
// MAX_LEN for filename + 3 (ellipsis string) + 11 (line number + separator)
322-
// + 11 (column number + separator) + 13 (panic string) + 15 extra bytes
323-
let mut msg_line: [u8; MAX_LEN + 53] = [32; MAX_LEN + 53];
324-
325-
let panic_str = "Panicked at: ".as_bytes(); // 13 bytes
326-
let dst = msg_line.as_mut_ptr();
299+
if let Some(Some(mm)) = info.message().map(|mes| mes.as_str()) {
300+
let mes = mm.as_bytes();
327301
unsafe {
328-
let src = panic_str.as_ptr();
329-
// SAFETY: Destination is larger than the panic string.
330-
std::ptr::copy_nonoverlapping(src, dst, panic_str.len());
331-
332-
let mut dst = dst.add(panic_str.len());
333-
let mut src = filename.as_ptr();
334-
let mut copy_len = filename.len();
335-
if copy_len > MAX_LEN {
336-
let truncated_str = "...".as_bytes(); // 3 bytes
337-
// SAFETY: Destination is larger than the truncated string
338-
std::ptr::copy_nonoverlapping(
339-
truncated_str.as_ptr(),
340-
dst,
341-
truncated_str.len(),
342-
);
343-
dst = dst.add(truncated_str.len());
344-
src = src.add(copy_len - MAX_LEN);
345-
copy_len = MAX_LEN;
346-
}
347-
348-
// SAFETY: The destination is always larger than the filename string.
349-
std::ptr::copy_nonoverlapping(src, dst, copy_len);
350-
351-
let dst = dst.add(copy_len);
352-
// SAFETY: `write_num` never writes more than 11 bytes
353-
let written_bytes = write_num(loc.line(), dst);
354-
let dst = dst.add(written_bytes);
355-
// SAFETY: `write_num` never writes more than 11 bytes
356-
let written_bytes = write_num(loc.column(), dst);
357-
358-
let line_len = dst.add(written_bytes).offset_from(msg_line.as_ptr()) as usize;
359-
// SAFETY: The line length is properly offseted from number of
360-
// written bytes
361-
solana_program::syscalls::sol_log_(msg_line.as_ptr(), line_len as u64);
302+
solana_program::syscalls::sol_log_(mes.as_ptr(), mes.len() as u64);
362303
}
363304
}
364305

365-
if let Some(Some(mm)) = info.message().map(|mes| mes.as_str()) {
366-
let mes = mm.as_bytes();
367-
// SAFETY: We assumed the panic message is well formed.
306+
if let Some(loc) = info.location() {
368307
unsafe {
369-
solana_program::syscalls::sol_log_(mes.as_ptr(), mes.len() as u64);
308+
solana_program::syscalls::sol_panic_(
309+
loc.file().as_ptr(),
310+
loc.file().len() as u64,
311+
loc.line() as u64,
312+
loc.column() as u64,
313+
);
370314
}
371315
}
372316
}

sdk/program/src/syscalls/definitions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use solana_define_syscall::definitions::{
1111
sol_curve_group_op, sol_curve_multiscalar_mul, sol_curve_pairing_map, sol_curve_validate_point,
1212
sol_get_clock_sysvar, sol_get_epoch_rewards_sysvar, sol_get_epoch_schedule_sysvar,
1313
sol_get_epoch_stake, sol_get_fees_sysvar, sol_get_last_restart_slot, sol_get_rent_sysvar,
14-
sol_get_sysvar, sol_keccak256, sol_remaining_compute_units,
14+
sol_get_sysvar, sol_keccak256, sol_panic_, sol_remaining_compute_units,
1515
};
1616
#[cfg(target_feature = "static-syscalls")]
1717
pub use solana_define_syscall::sys_hash;

0 commit comments

Comments
 (0)