solana_program_entrypoint/lib.rs
1//! The Rust-based BPF program entrypoint supported by the latest BPF loader.
2//!
3//! For more information see the [`bpf_loader`] module.
4//!
5//! [`bpf_loader`]: crate::bpf_loader
6
7extern crate alloc;
8use {
9 alloc::vec::Vec,
10 solana_account_info::AccountInfo,
11 solana_pubkey::Pubkey,
12 std::{
13 alloc::Layout,
14 cell::RefCell,
15 mem::{size_of, MaybeUninit},
16 ptr::null_mut,
17 rc::Rc,
18 slice::{from_raw_parts, from_raw_parts_mut},
19 },
20};
21// need to re-export msg for custom_heap_default macro
22pub use {
23 solana_account_info::MAX_PERMITTED_DATA_INCREASE, solana_msg::msg as __msg,
24 solana_program_error::ProgramResult,
25};
26
27/// User implemented function to process an instruction
28///
29/// program_id: Program ID of the currently executing program accounts: Accounts
30/// passed as part of the instruction instruction_data: Instruction data
31pub type ProcessInstruction =
32 fn(program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult;
33
34/// Programs indicate success with a return value of 0
35pub const SUCCESS: u64 = 0;
36
37/// Start address of the memory region used for program heap.
38pub const HEAP_START_ADDRESS: u64 = 0x300000000;
39/// Length of the heap memory region used for program heap.
40pub const HEAP_LENGTH: usize = 32 * 1024;
41
42/// Value used to indicate that a serialized account is not a duplicate
43pub const NON_DUP_MARKER: u8 = u8::MAX;
44
45/// Declare the program entrypoint and set up global handlers.
46///
47/// This macro emits the common boilerplate necessary to begin program
48/// execution, calling a provided function to process the program instruction
49/// supplied by the runtime, and reporting its result to the runtime.
50///
51/// It also sets up a [global allocator] and [panic handler], using the
52/// [`custom_heap_default`] and [`custom_panic_default`] macros.
53///
54/// [`custom_heap_default`]: crate::custom_heap_default
55/// [`custom_panic_default`]: crate::custom_panic_default
56///
57/// [global allocator]: https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html
58/// [panic handler]: https://doc.rust-lang.org/nomicon/panic-handler.html
59///
60/// The argument is the name of a function with this type signature:
61///
62/// ```ignore
63/// fn process_instruction(
64/// program_id: &Pubkey, // Public key of the account the program was loaded into
65/// accounts: &[AccountInfo], // All accounts required to process the instruction
66/// instruction_data: &[u8], // Serialized instruction-specific data
67/// ) -> ProgramResult;
68/// ```
69///
70/// # Cargo features
71///
72/// This macro emits symbols and definitions that may only be defined once
73/// globally. As such, if linked to other Rust crates it will cause compiler
74/// errors. To avoid this, it is common for Solana programs to define an
75/// optional [Cargo feature] called `no-entrypoint`, and use it to conditionally
76/// disable the `entrypoint` macro invocation, as well as the
77/// `process_instruction` function. See a typical pattern for this in the
78/// example below.
79///
80/// [Cargo feature]: https://doc.rust-lang.org/cargo/reference/features.html
81///
82/// The code emitted by this macro can be customized by adding cargo features
83/// _to your own crate_ (the one that calls this macro) and enabling them:
84///
85/// - If the `custom-heap` feature is defined then the macro will not set up the
86/// global allocator, allowing `entrypoint` to be used with your own
87/// allocator. See documentation for the [`custom_heap_default`] macro for
88/// details of customizing the global allocator.
89///
90/// - If the `custom-panic` feature is defined then the macro will not define a
91/// panic handler, allowing `entrypoint` to be used with your own panic
92/// handler. See documentation for the [`custom_panic_default`] macro for
93/// details of customizing the panic handler.
94///
95/// # Examples
96///
97/// Defining an entrypoint and making it conditional on the `no-entrypoint`
98/// feature. Although the `entrypoint` module is written inline in this example,
99/// it is common to put it into its own file.
100///
101/// ```no_run
102/// #[cfg(not(feature = "no-entrypoint"))]
103/// pub mod entrypoint {
104///
105/// use solana_account_info::AccountInfo;
106/// use solana_program_entrypoint::entrypoint;
107/// use solana_program_entrypoint::ProgramResult;
108/// use solana_msg::msg;
109/// use solana_pubkey::Pubkey;
110///
111/// entrypoint!(process_instruction);
112///
113/// pub fn process_instruction(
114/// program_id: &Pubkey,
115/// accounts: &[AccountInfo],
116/// instruction_data: &[u8],
117/// ) -> ProgramResult {
118/// msg!("Hello world");
119///
120/// Ok(())
121/// }
122///
123/// }
124/// ```
125#[macro_export]
126macro_rules! entrypoint {
127 ($process_instruction:ident) => {
128 /// # Safety
129 #[no_mangle]
130 pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
131 let (program_id, accounts, instruction_data) = unsafe { $crate::deserialize(input) };
132 match $process_instruction(program_id, &accounts, instruction_data) {
133 Ok(()) => $crate::SUCCESS,
134 Err(error) => error.into(),
135 }
136 }
137 $crate::custom_heap_default!();
138 $crate::custom_panic_default!();
139 };
140}
141
142/// Declare the program entrypoint and set up global handlers.
143///
144/// This is similar to the `entrypoint!` macro, except that it does not perform
145/// any dynamic allocations, and instead writes the input accounts into a pre-
146/// allocated array.
147///
148/// This version reduces compute unit usage by 20-30 compute units per unique
149/// account in the instruction. It may become the default option in a future
150/// release.
151///
152/// For more information about how the program entrypoint behaves and what it
153/// does, please see the documentation for [`entrypoint!`].
154///
155/// NOTE: This entrypoint has a hard-coded limit of 64 input accounts.
156#[macro_export]
157macro_rules! entrypoint_no_alloc {
158 ($process_instruction:ident) => {
159 /// # Safety
160 #[no_mangle]
161 pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
162 use std::mem::MaybeUninit;
163 // Clippy complains about this because a `const` with interior
164 // mutability `RefCell` should use `static` instead to make it
165 // clear that it can change.
166 // In our case, however, we want to create an array of `AccountInfo`s,
167 // and the only way to do it is through a `const` expression, and
168 // we don't expect to mutate the internals of this `const` type.
169 #[allow(clippy::declare_interior_mutable_const)]
170 const UNINIT_ACCOUNT_INFO: MaybeUninit<AccountInfo> =
171 MaybeUninit::<AccountInfo>::uninit();
172 const MAX_ACCOUNT_INFOS: usize = 64;
173 let mut accounts = [UNINIT_ACCOUNT_INFO; MAX_ACCOUNT_INFOS];
174 let (program_id, num_accounts, instruction_data) =
175 unsafe { $crate::deserialize_into(input, &mut accounts) };
176 // Use `slice_assume_init_ref` once it's stabilized
177 let accounts = &*(&accounts[..num_accounts] as *const [MaybeUninit<AccountInfo<'_>>]
178 as *const [AccountInfo<'_>]);
179
180 #[inline(never)]
181 fn call_program(program_id: &Pubkey, accounts: &[AccountInfo], data: &[u8]) -> u64 {
182 match $process_instruction(program_id, accounts, data) {
183 Ok(()) => $crate::SUCCESS,
184 Err(error) => error.into(),
185 }
186 }
187
188 call_program(&program_id, accounts, &instruction_data)
189 }
190 $crate::custom_heap_default!();
191 $crate::custom_panic_default!();
192 };
193}
194
195/// Define the default global allocator.
196///
197/// The default global allocator is enabled only if the calling crate has not
198/// disabled it using [Cargo features] as described below. It is only defined
199/// for [BPF] targets.
200///
201/// [Cargo features]: https://doc.rust-lang.org/cargo/reference/features.html
202/// [BPF]: https://solana.com/docs/programs/faq#berkeley-packet-filter-bpf
203///
204/// # Cargo features
205///
206/// A crate that calls this macro can provide its own custom heap
207/// implementation, or allow others to provide their own custom heap
208/// implementation, by adding a `custom-heap` feature to its `Cargo.toml`. After
209/// enabling the feature, one may define their own [global allocator] in the
210/// standard way.
211///
212/// [global allocator]: https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html
213///
214#[macro_export]
215macro_rules! custom_heap_default {
216 () => {
217 #[cfg(all(not(feature = "custom-heap"), target_os = "solana"))]
218 #[global_allocator]
219 static A: $crate::BumpAllocator = $crate::BumpAllocator {
220 start: $crate::HEAP_START_ADDRESS as usize,
221 len: $crate::HEAP_LENGTH,
222 };
223 };
224}
225
226/// Define the default global panic handler.
227///
228/// This must be used if the [`entrypoint`] macro is not used, and no other
229/// panic handler has been defined; otherwise compilation will fail with a
230/// missing `custom_panic` symbol.
231///
232/// The default global allocator is enabled only if the calling crate has not
233/// disabled it using [Cargo features] as described below. It is only defined
234/// for [BPF] targets.
235///
236/// [Cargo features]: https://doc.rust-lang.org/cargo/reference/features.html
237/// [BPF]: https://solana.com/docs/programs/faq#berkeley-packet-filter-bpf
238///
239/// # Cargo features
240///
241/// A crate that calls this macro can provide its own custom panic handler, or
242/// allow others to provide their own custom panic handler, by adding a
243/// `custom-panic` feature to its `Cargo.toml`. After enabling the feature, one
244/// may define their own panic handler.
245///
246/// A good way to reduce the final size of the program is to provide a
247/// `custom_panic` implementation that does nothing. Doing so will cut ~25kb
248/// from a noop program. That number goes down the more the programs pulls in
249/// Rust's standard library for other purposes.
250///
251/// # Defining a panic handler for Solana
252///
253/// _The mechanism for defining a Solana panic handler is different [from most
254/// Rust programs][rpanic]._
255///
256/// [rpanic]: https://doc.rust-lang.org/nomicon/panic-handler.html
257///
258/// To define a panic handler one must define a `custom_panic` function
259/// with the `#[no_mangle]` attribute, as below:
260///
261/// ```ignore
262/// #[cfg(all(feature = "custom-panic", target_os = "solana"))]
263/// #[no_mangle]
264/// fn custom_panic(info: &core::panic::PanicInfo<'_>) {
265/// $crate::msg!("{}", info);
266/// }
267/// ```
268///
269/// The above is how Solana defines the default panic handler.
270#[macro_export]
271macro_rules! custom_panic_default {
272 () => {
273 #[cfg(all(not(feature = "custom-panic"), target_os = "solana"))]
274 #[no_mangle]
275 fn custom_panic(info: &core::panic::PanicInfo<'_>) {
276 // Full panic reporting
277 $crate::__msg!("{}", info);
278 }
279 };
280}
281
282/// The bump allocator used as the default rust heap when running programs.
283pub struct BumpAllocator {
284 pub start: usize,
285 pub len: usize,
286}
287/// Integer arithmetic in this global allocator implementation is safe when
288/// operating on the prescribed `HEAP_START_ADDRESS` and `HEAP_LENGTH`. Any
289/// other use may overflow and is thus unsupported and at one's own risk.
290#[allow(clippy::arithmetic_side_effects)]
291unsafe impl std::alloc::GlobalAlloc for BumpAllocator {
292 #[inline]
293 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
294 let pos_ptr = self.start as *mut usize;
295
296 let mut pos = *pos_ptr;
297 if pos == 0 {
298 // First time, set starting position
299 pos = self.start + self.len;
300 }
301 pos = pos.saturating_sub(layout.size());
302 pos &= !(layout.align().wrapping_sub(1));
303 if pos < self.start + size_of::<*mut u8>() {
304 return null_mut();
305 }
306 *pos_ptr = pos;
307 pos as *mut u8
308 }
309 #[inline]
310 unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
311 // I'm a bump allocator, I don't free
312 }
313}
314
315/// `assert_eq(std::mem::align_of::<u128>(), 8)` is true for BPF but not for some host machines
316pub const BPF_ALIGN_OF_U128: usize = 8;
317
318#[allow(clippy::arithmetic_side_effects)]
319#[inline(always)] // this reduces CU usage
320unsafe fn deserialize_instruction_data<'a>(input: *mut u8, mut offset: usize) -> (&'a [u8], usize) {
321 #[allow(clippy::cast_ptr_alignment)]
322 let instruction_data_len = *(input.add(offset) as *const u64) as usize;
323 offset += size_of::<u64>();
324
325 let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };
326 offset += instruction_data_len;
327
328 (instruction_data, offset)
329}
330
331#[allow(clippy::arithmetic_side_effects)]
332#[inline(always)] // this reduces CU usage by half!
333unsafe fn deserialize_account_info<'a>(
334 input: *mut u8,
335 mut offset: usize,
336) -> (AccountInfo<'a>, usize) {
337 #[allow(clippy::cast_ptr_alignment)]
338 let is_signer = *(input.add(offset) as *const u8) != 0;
339 offset += size_of::<u8>();
340
341 #[allow(clippy::cast_ptr_alignment)]
342 let is_writable = *(input.add(offset) as *const u8) != 0;
343 offset += size_of::<u8>();
344
345 #[allow(clippy::cast_ptr_alignment)]
346 let executable = *(input.add(offset) as *const u8) != 0;
347 offset += size_of::<u8>();
348
349 // The original data length is stored here because these 4 bytes were
350 // originally only used for padding and served as a good location to
351 // track the original size of the account data in a compatible way.
352 let original_data_len_offset = offset;
353 offset += size_of::<u32>();
354
355 let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
356 offset += size_of::<Pubkey>();
357
358 let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
359 offset += size_of::<Pubkey>();
360
361 #[allow(clippy::cast_ptr_alignment)]
362 let lamports = Rc::new(RefCell::new(&mut *(input.add(offset) as *mut u64)));
363 offset += size_of::<u64>();
364
365 #[allow(clippy::cast_ptr_alignment)]
366 let data_len = *(input.add(offset) as *const u64) as usize;
367 offset += size_of::<u64>();
368
369 // Store the original data length for detecting invalid reallocations and
370 // requires that MAX_PERMITTED_DATA_LENGTH fits in a u32
371 *(input.add(original_data_len_offset) as *mut u32) = data_len as u32;
372
373 let data = Rc::new(RefCell::new({
374 from_raw_parts_mut(input.add(offset), data_len)
375 }));
376 offset += data_len + MAX_PERMITTED_DATA_INCREASE;
377 offset += (offset as *const u8).align_offset(BPF_ALIGN_OF_U128); // padding
378
379 #[allow(clippy::cast_ptr_alignment)]
380 let rent_epoch = *(input.add(offset) as *const u64);
381 offset += size_of::<u64>();
382
383 (
384 AccountInfo {
385 key,
386 is_signer,
387 is_writable,
388 lamports,
389 data,
390 owner,
391 executable,
392 rent_epoch,
393 },
394 offset,
395 )
396}
397
398/// Deserialize the input arguments
399///
400/// The integer arithmetic in this method is safe when called on a buffer that was
401/// serialized by runtime. Use with buffers serialized otherwise is unsupported and
402/// done at one's own risk.
403///
404/// # Safety
405#[allow(clippy::arithmetic_side_effects)]
406pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a>>, &'a [u8]) {
407 let mut offset: usize = 0;
408
409 // Number of accounts present
410
411 #[allow(clippy::cast_ptr_alignment)]
412 let num_accounts = *(input.add(offset) as *const u64) as usize;
413 offset += size_of::<u64>();
414
415 // Account Infos
416
417 let mut accounts = Vec::with_capacity(num_accounts);
418 for _ in 0..num_accounts {
419 let dup_info = *(input.add(offset) as *const u8);
420 offset += size_of::<u8>();
421 if dup_info == NON_DUP_MARKER {
422 let (account_info, new_offset) = deserialize_account_info(input, offset);
423 offset = new_offset;
424 accounts.push(account_info);
425 } else {
426 offset += 7; // padding
427
428 // Duplicate account, clone the original
429 accounts.push(accounts[dup_info as usize].clone());
430 }
431 }
432
433 // Instruction data
434
435 let (instruction_data, new_offset) = deserialize_instruction_data(input, offset);
436 offset = new_offset;
437
438 // Program Id
439
440 let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
441
442 (program_id, accounts, instruction_data)
443}
444
445/// Deserialize the input arguments
446///
447/// Differs from `deserialize` by writing the account infos into an uninitialized
448/// slice, which provides better performance, roughly 30 CUs per unique account
449/// provided to the instruction.
450///
451/// Panics if the input slice is not large enough.
452///
453/// The integer arithmetic in this method is safe when called on a buffer that was
454/// serialized by runtime. Use with buffers serialized otherwise is unsupported and
455/// done at one's own risk.
456///
457/// # Safety
458#[allow(clippy::arithmetic_side_effects)]
459pub unsafe fn deserialize_into<'a>(
460 input: *mut u8,
461 accounts: &mut [MaybeUninit<AccountInfo<'a>>],
462) -> (&'a Pubkey, usize, &'a [u8]) {
463 let mut offset: usize = 0;
464
465 // Number of accounts present
466
467 #[allow(clippy::cast_ptr_alignment)]
468 let num_accounts = *(input.add(offset) as *const u64) as usize;
469 offset += size_of::<u64>();
470
471 if num_accounts > accounts.len() {
472 panic!(
473 "{} accounts provided, but only {} are supported",
474 num_accounts,
475 accounts.len()
476 );
477 }
478
479 // Account Infos
480
481 for i in 0..num_accounts {
482 let dup_info = *(input.add(offset) as *const u8);
483 offset += size_of::<u8>();
484 if dup_info == NON_DUP_MARKER {
485 let (account_info, new_offset) = deserialize_account_info(input, offset);
486 offset = new_offset;
487 accounts[i].write(account_info);
488 } else {
489 offset += 7; // padding
490
491 // Duplicate account, clone the original
492 accounts[i].write(accounts[dup_info as usize].assume_init_ref().clone());
493 }
494 }
495
496 // Instruction data
497
498 let (instruction_data, new_offset) = deserialize_instruction_data(input, offset);
499 offset = new_offset;
500
501 // Program Id
502
503 let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
504
505 (program_id, num_accounts, instruction_data)
506}
507
508#[cfg(test)]
509mod test {
510 use {super::*, std::alloc::GlobalAlloc};
511
512 #[test]
513 fn test_bump_allocator() {
514 // alloc the entire
515 {
516 let heap = [0u8; 128];
517 let allocator = BumpAllocator {
518 start: heap.as_ptr() as *const _ as usize,
519 len: heap.len(),
520 };
521 for i in 0..128 - size_of::<*mut u8>() {
522 let ptr = unsafe {
523 allocator.alloc(Layout::from_size_align(1, size_of::<u8>()).unwrap())
524 };
525 assert_eq!(
526 ptr as *const _ as usize,
527 heap.as_ptr() as *const _ as usize + heap.len() - 1 - i
528 );
529 }
530 assert_eq!(null_mut(), unsafe {
531 allocator.alloc(Layout::from_size_align(1, 1).unwrap())
532 });
533 }
534 // check alignment
535 {
536 let heap = [0u8; 128];
537 let allocator = BumpAllocator {
538 start: heap.as_ptr() as *const _ as usize,
539 len: heap.len(),
540 };
541 let ptr =
542 unsafe { allocator.alloc(Layout::from_size_align(1, size_of::<u8>()).unwrap()) };
543 assert_eq!(0, ptr.align_offset(size_of::<u8>()));
544 let ptr =
545 unsafe { allocator.alloc(Layout::from_size_align(1, size_of::<u16>()).unwrap()) };
546 assert_eq!(0, ptr.align_offset(size_of::<u16>()));
547 let ptr =
548 unsafe { allocator.alloc(Layout::from_size_align(1, size_of::<u32>()).unwrap()) };
549 assert_eq!(0, ptr.align_offset(size_of::<u32>()));
550 let ptr =
551 unsafe { allocator.alloc(Layout::from_size_align(1, size_of::<u64>()).unwrap()) };
552 assert_eq!(0, ptr.align_offset(size_of::<u64>()));
553 let ptr =
554 unsafe { allocator.alloc(Layout::from_size_align(1, size_of::<u128>()).unwrap()) };
555 assert_eq!(0, ptr.align_offset(size_of::<u128>()));
556 let ptr = unsafe { allocator.alloc(Layout::from_size_align(1, 64).unwrap()) };
557 assert_eq!(0, ptr.align_offset(64));
558 }
559 // alloc entire block (minus the pos ptr)
560 {
561 let heap = [0u8; 128];
562 let allocator = BumpAllocator {
563 start: heap.as_ptr() as *const _ as usize,
564 len: heap.len(),
565 };
566 let ptr =
567 unsafe { allocator.alloc(Layout::from_size_align(120, size_of::<u8>()).unwrap()) };
568 assert_ne!(ptr, null_mut());
569 assert_eq!(0, ptr.align_offset(size_of::<u64>()));
570 }
571 }
572}