1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! Zero-allocation logging via Solana syscalls.
/// Log a UTF-8 message to the runtime log.
#[inline(always)]
pub fn log(message: &str) {
#[cfg(target_os = "solana")]
// SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
unsafe {
crate::syscalls::sol_log_(message.as_ptr(), message.len() as u64);
}
#[cfg(not(target_os = "solana"))]
{
// No-op off-chain; test harnesses capture logs separately.
let _ = message;
}
}
/// Log five u64 values for quick debugging.
#[inline(always)]
pub fn log_64(a: u64, b: u64, c: u64, d: u64, e: u64) {
#[cfg(target_os = "solana")]
// SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
unsafe {
crate::syscalls::sol_log_64_(a, b, c, d, e);
}
#[cfg(not(target_os = "solana"))]
{
let _ = (a, b, c, d, e);
}
}
/// Log the current compute unit consumption.
#[inline(always)]
pub fn log_compute_units() {
#[cfg(target_os = "solana")]
// SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
unsafe {
crate::syscalls::sol_log_compute_units_();
}
}
/// Emit structured data segments via `sol_log_data` (for events).
#[inline(always)]
pub fn log_data(segments: &[&[u8]]) {
#[cfg(target_os = "solana")]
// SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
unsafe {
crate::syscalls::sol_log_data(segments.as_ptr() as *const u8, segments.len() as u64);
}
#[cfg(not(target_os = "solana"))]
{
let _ = segments;
}
}
/// Convenience macro for logging. Equivalent to `hopper_native::log::log(msg)`.
///
/// Usage: `msg!("Hello, {}", name);`
///
/// On BPF, this formats into a stack buffer and calls `sol_log_`.
/// For simple string literals, prefer `hopper_native::log::log("...")` directly.
#[macro_export]
macro_rules! msg {
( $literal:expr ) => {
$crate::log::log($literal)
};
( $fmt:expr, $($arg:tt)* ) => {{
// On BPF we have limited stack, so use a fixed 256-byte buffer.
// For string literals, the branch above avoids this entirely.
#[cfg(target_os = "solana")]
{
use core::fmt::Write;
let mut buf = [0u8; 256];
let mut wrapper = $crate::log::StackWriter::new(&mut buf);
let _ = write!(wrapper, $fmt, $($arg)*);
let len = wrapper.pos();
$crate::log::log(
// SAFETY: Write only produces valid UTF-8 from fmt::Display impls.
unsafe { core::str::from_utf8_unchecked(&buf[..len]) }
);
}
#[cfg(not(target_os = "solana"))]
{
let _ = ($fmt, $($arg)*);
}
}};
}
/// Stack-allocated write buffer for formatted log messages on BPF.
pub struct StackWriter<'a> {
buf: &'a mut [u8],
pos: usize,
}
impl<'a> StackWriter<'a> {
/// Create a new writer over the given buffer.
#[inline(always)]
pub fn new(buf: &'a mut [u8]) -> Self {
Self { buf, pos: 0 }
}
/// Number of bytes written.
#[inline(always)]
pub fn pos(&self) -> usize {
self.pos
}
}
impl core::fmt::Write for StackWriter<'_> {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
let bytes = s.as_bytes();
let remaining = self.buf.len() - self.pos;
let to_write = bytes.len().min(remaining);
self.buf[self.pos..self.pos + to_write].copy_from_slice(&bytes[..to_write]);
self.pos += to_write;
Ok(())
}
}