This repository was archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.rs
194 lines (153 loc) · 5.64 KB
/
heap.rs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::{alloc::Layout,mem::size_of,ptr::null_mut,result::Result};
type ProgramResult = Result<(),ProgramError>;
/// Programs indicate success with a return value of 0
pub const SUCCESS: u64 = 0;
/// Start address of the memory region used for program heap.
pub const HEAP_START_ADDRESS: u64 = 0x300000000;
/// Length of the heap memory region used for program heap.
pub const HEAP_LENGTH: usize = 32 * 1024;
pub enum ProgramError {
DefaultError,
CalculationError
}
/// Builtin return values occupy the upper 32 bits
const BUILTIN_BIT_SHIFT: usize = 32;
macro_rules! to_builtin {
($error:expr) => {
($error as u64) << BUILTIN_BIT_SHIFT
};
}
pub const DEFAULT_ERROR:u64 = to_builtin!(1);
pub const CALCULATION_ERROR:u64 = to_builtin!(2);
impl From<ProgramError> for u64 {
fn from(error: ProgramError) -> Self {
match error {
ProgramError::DefaultError => DEFAULT_ERROR,
ProgramError::CalculationError => CALCULATION_ERROR,
}
}
}
pub struct Pubkey(pub(crate) [u8; 32]);
pub struct UtxoInfo {
pub txid: String,
pub vout: u32,
pub value: u64,
}
/// The bump allocator used as the default rust heap when running programs.
pub struct BumpAllocator {
pub start: usize,
pub len: usize,
}
#[global_allocator]
static A: BumpAllocator = BumpAllocator {
start: HEAP_START_ADDRESS as usize,
len: HEAP_LENGTH,
};
unsafe impl std::alloc::GlobalAlloc for BumpAllocator {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let pos_ptr = self.start as *mut usize;
let mut pos = *pos_ptr;
if pos == 0 {
// First time, set starting position
pos = self.start + self.len;
}
pos = pos.saturating_sub(layout.size());
pos &= !(layout.align().wrapping_sub(1));
if pos < self.start + size_of::<*mut u8>() {
return null_mut();
}
*pos_ptr = pos;
pos as *mut u8
}
#[inline]
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
// I'm a bump allocator, I don't free
}
}
/// FORMAT
/// 8 bytes unsigned number of UTXO
/// 32 byte txid
/// 4 byte vout
/// 8 byte value
/// 8 bytes of unsigned number of instruction data
/// x bytes of instruction data
/// 32 bytes of the program id
pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<UtxoInfo>, &'a [u8]) {
let mut offset: usize = 0;
let num_utxos = *(input.add(offset) as *const u64) as usize;
offset += size_of::<u64>();
// Account Infos
let mut utxos = Vec::with_capacity(num_utxos);
for _ in 0..num_utxos {
let str_len = *(input.add(offset) as *const u8) != 0;
offset += size_of::<u8>();
#[allow(clippy::cast_ptr_alignment)]
let is_writable = *(input.add(offset) as *const u8) != 0;
offset += (size_of::<u8>() * str_len);
#[allow(clippy::cast_ptr_alignment)]
let executable = *(input.add(offset) as *const u8) != 0;
offset += size_of::<u8>();
// The original data length is stored here because these 4 bytes were
// originally only used for padding and served as a good location to
// track the original size of the account data in a compatible way.
let original_data_len_offset = offset;
offset += size_of::<u32>();
let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
offset += size_of::<Pubkey>();
let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
offset += size_of::<Pubkey>();
#[allow(clippy::cast_ptr_alignment)]
let lamports = Rc::new(RefCell::new(&mut *(input.add(offset) as *mut u64)));
offset += size_of::<u64>();
#[allow(clippy::cast_ptr_alignment)]
let data_len = *(input.add(offset) as *const u64) as usize;
offset += size_of::<u64>();
// Store the original data length for detecting invalid reallocations and
// requires that MAX_PERMITTED_DATA_LENGTH fits in a u32
*(input.add(original_data_len_offset) as *mut u32) = data_len as u32;
let data = Rc::new(RefCell::new({
from_raw_parts_mut(input.add(offset), data_len)
}));
offset += data_len + MAX_PERMITTED_DATA_INCREASE;
offset += (offset as *const u8).align_offset(BPF_ALIGN_OF_U128); // padding
#[allow(clippy::cast_ptr_alignment)]
let rent_epoch = *(input.add(offset) as *const u64);
offset += size_of::<u64>();
accounts.push(UtxoInfo {
key,
is_signer,
is_writable,
lamports,
data,
owner,
executable,
rent_epoch,
});
}
// Instruction data
#[allow(clippy::cast_ptr_alignment)]
let instruction_data_len = *(input.add(offset) as *const u64) as usize;
offset += size_of::<u64>();
let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };
offset += instruction_data_len;
// Program Id
let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
(program_id, utxos, instruction_data)
}
#[no_mangle]
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
let (program_id, accounts, instruction_data) =
unsafe { deserialize(input) };
match handler(program_id, accounts, instruction_data) {
Ok(()) => SUCCESS,
Err(error) => error.into(),
}
}
fn handler(
program_id: &Pubkey,
utxos: &[UtxoInfo],
instruction_data: &[u8]
) -> ProgramResult {
Ok(())
}