This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sealevel.h
325 lines (276 loc) · 9.92 KB
/
sealevel.h
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* libsealevel is a C interface for the Sealevel virtual machine.
* This version of the library bundles the interpreter and JIT executors part of the Rust implementation of the Solana blockchain.
*
* Source code: https://github.com/solana-labs/solana
*
* ABI stability is planned, though this version makes no promises yet.
* Avoid passing objects between two different versions of this library because no internal compatibility guarantees are made.
*
* Note that, despite the Rust code under the hood, this interface allows unsafe behavior.
* The usual C rules apply library-wide: Check for null pointers, avoid aliasing, don't mix types, respect thread safety, no double frees.
* You may find additional safety remarks on each exported function.
*/
#pragma once
/* Generated with cbindgen:0.24.3 */
/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#define SEALEVEL_ERR_UNKNOWN -1
#define SEALEVEL_OK 0
#define SEALEVEL_ERR_INVALID_ELF 1
#define SEALEVEL_ERR_SYSCALL_REGISTRATION 2
#define SEALEVEL_ERR_CALL_DEPTH_EXCEEDED 3
#define SEALEVEL_ERR_EXIT_ROOT_CALL_FRAME 4
#define SEALEVEL_ERR_DIVIDE_BY_ZERO 5
#define SEALEVEL_ERR_DIVIDE_OVERFLOW 6
#define SEALEVEL_ERR_EXECUTION_OVERRUN 7
#define SEALEVEL_ERR_CALL_OUTSIDE_TEXT_SEGMENT 8
#define SEALEVEL_ERR_EXCEEDED_MAX_INSTRUCTIONS 9
#define SEALEVEL_ERR_JIT_NOT_COMPILED 10
#define SEALEVEL_ERR_INVALID_VIRTUAL_ADDRESS 11
#define SEALEVEL_ERR_INVALID_MEMORY_REGION 12
#define SEALEVEL_ERR_ACCESS_VIOLATION 13
#define SEALEVEL_ERR_STACK_ACCESS_VIOLATION 14
#define SEALEVEL_ERR_INVALID_INSTRUCTION 15
#define SEALEVEL_ERR_UNSUPPORTED_INSTRUCTION 16
#define SEALEVEL_ERR_ERR_EXHAUSTED_TEXT_SEGMENT 17
#define SEALEVEL_ERR_LIBC_INVOCATION_FAILED 18
#define SEALEVEL_ERR_VERIFIER_ERROR 19
typedef enum {
SEALEVEL_OPT_NONE,
SEALEVEL_OPT_NO_VERIFY,
SEALEVEL_OPT_MAX_CALL_DEPTH,
SEALEVEL_OPT_STACK_FRAME_SIZE,
SEALEVEL_OPT_ENABLE_STACK_FRAME_GAPS,
SEALEVEL_OPT_INSTRUCTION_METER_CHECKPOINT_DISTANCE,
SEALEVEL_OPT_ENABLE_INSTRUCTION_METER,
SEALEVEL_OPT_ENABLE_INSTRUCTION_TRACING,
SEALEVEL_OPT_ENABLE_SYMBOL_AND_SECTION_LABELS,
SEALEVEL_OPT_DISABLE_UNRESOLVED_SYMBOLS_AT_RUNTIME,
SEALEVEL_OPT_REJECT_BROKEN_ELFS,
SEALEVEL_OPT_NOOP_INSTRUCTION_RATIO,
SEALEVEL_OPT_SANITIZE_USER_PROVIDED_VALUES,
SEALEVEL_OPT_ENCRYPT_ENVIRONMENT_REGISTERS,
SEALEVEL_OPT_DISABLE_DEPRECATED_LOAD_INSTRUCTIONS,
SEALEVEL_OPT_SYSCALL_BPF_FUNCTION_HASH_COLLISION,
SEALEVEL_OPT_REJECT_CALLX_R10,
SEALEVEL_OPT_DYNAMIC_STACK_FRAMES,
SEALEVEL_OPT_ENABLE_SDIV,
SEALEVEL_OPT_OPTIMIZE_RODATA,
SEALEVEL_OPT_STATIC_SYSCALLS,
SEALEVEL_OPT_ENABLE_ELF_VADDR,
} sealevel_config_opt;
typedef enum {
SEALEVEL_SYSCALL_INVALID,
SEALEVEL_SYSCALL_ABORT,
SEALEVEL_SYSCALL_SOL_PANIC,
SEALEVEL_SYSCALL_SOL_LOG,
SEALEVEL_SYSCALL_SOL_LOG_64,
SEALEVEL_SYSCALL_SOL_LOG_COMPUTE_UNITS,
SEALEVEL_SYSCALL_SOL_LOG_PUBKEY,
SEALEVEL_SYSCALL_SOL_CREATE_PROGRAM_ADDRESS,
SEALEVEL_SYSCALL_SOL_TRY_FIND_PROGRAM_ADDRESS,
SEALEVEL_SYSCALL_SOL_SHA256,
SEALEVEL_SYSCALL_SOL_KECCAK256,
SEALEVEL_SYSCALL_SOL_SECP256K1_RECOVER,
SEALEVEL_SYSCALL_SOL_BLAKE3,
SEALEVEL_SYSCALL_SOL_CURVE_VALIDATE_POINT,
SEALEVEL_SYSCALL_SOL_CURVE_GROUP_OP,
SEALEVEL_SYSCALL_SOL_GET_CLOCK_SYSVAR,
SEALEVEL_SYSCALL_SOL_GET_EPOCH_SCHEDULE_SYSVAR,
SEALEVEL_SYSCALL_SOL_GET_FEES_SYSVAR,
SEALEVEL_SYSCALL_SOL_GET_RENT_SYSVAR,
SEALEVEL_SYSCALL_SOL_MEMCPY,
SEALEVEL_SYSCALL_SOL_MEMMOVE,
SEALEVEL_SYSCALL_SOL_MEMCMP,
SEALEVEL_SYSCALL_SOL_MEMSET,
SEALEVEL_SYSCALL_SOL_INVOKE_SIGNED_C,
SEALEVEL_SYSCALL_SOL_INVOKE_SIGNED_RUST,
SEALEVEL_SYSCALL_SOL_ALLOC_FREE,
SEALEVEL_SYSCALL_SOL_SET_RETURN_DATA,
SEALEVEL_SYSCALL_SOL_GET_RETURN_DATA,
SEALEVEL_SYSCALL_SOL_LOG_DATA,
SEALEVEL_SYSCALL_SOL_GET_PROCESSED_SIBLING_INSTRUCTION,
SEALEVEL_SYSCALL_SOL_GET_STACK_HEIGHT,
} sealevel_syscall_id;
/**
* Sealevel virtual machine config.
*/
typedef struct sealevel_config sealevel_config;
/**
* A loaded and relocated program.
*
* To execute this program, create a VM with `sealevel_vm_create`.
*/
typedef struct sealevel_executable sealevel_executable;
/**
* The invoke context holds the state of a single transaction execution.
* It tracks the execution progress (instruction being executed),
* interfaces with account data,
* and specifies the on-chain execution rules (precompiles, syscalls, sysvars).
*/
typedef struct sealevel_invoke_context sealevel_invoke_context;
typedef struct sealevel_vm sealevel_vm;
/**
* The map of syscalls provided by the virtual machine.
*/
typedef void *sealevel_syscall_registry;
typedef struct {
void *data_addr;
size_t data_size;
uint64_t vm_addr;
uint64_t vm_gap_size;
bool is_writable;
} sealevel_region;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* Creates a new Sealevel machine config.
*
* # Safety
* Call `sealevel_config_free` on the return value after you are done using it.
* Failure to do so results in a memory leak.
*/
sealevel_config *sealevel_config_new(void);
/**
* Sets a config option given the config key and exactly one value arg.
*
* # Safety
* Avoid the following undefined behavior:
* - Passing the wrong argument type as the config value (each key documents the expected value).
*/
void sealevel_config_setopt(sealevel_config *config, sealevel_config_opt key, size_t value);
/**
* Releases resources associated with a Sealevel machine config.
*
* # Safety
* Avoid the following undefined behavior:
* - Calling this function given a string that's _not_ the return value of `sealevel_config_new`.
* - Calling this function more than once on the same object (double free).
* - Using the config object after calling this function (use-after-free).
*/
void sealevel_config_free(sealevel_config *config);
/**
* Creates a new, empty syscall registry.
*
* # Safety
* Call `sealevel_syscall_registry_free` on the return value after you are done using it.
* Failure to do so results in a memory leak.
*/
sealevel_syscall_registry sealevel_syscall_registry_new(void);
/**
* Frees a syscall registry.
*
* # Safety
* Avoid the following undefined behavior:
* - Calling this function twice on the same object.
* - Using the syscall registry after calling this function.
*/
void sealevel_syscall_registry_free(sealevel_syscall_registry registry);
/**
* Registers a Solana builtin syscall.
*
* Returns TRUE when registration was successful.
*
* # Safety
* Avoid the following undefined behavior:
* - Passing a NULL pointer.
* - Passing a registry that was already freed with `sealevel_syscall_registry_free`.
* - Passing a registry that was created from another thread.
*/
bool sealevel_syscall_register_builtin(sealevel_syscall_registry registry,
sealevel_syscall_id syscall_id);
/**
* Drops an invoke context and all programs created with it. Noop given a null pointer.
*
* # Safety
* Avoid the following undefined behavior:
* - Calling this function twice on the same object.
* - Using the invoke context after calling this function.
*/
void sealevel_invoke_context_free(sealevel_invoke_context *this_);
/**
* Returns the error code of this thread's last seen error.
*/
int sealevel_errno(void);
/**
* Returns a UTF-8 string of this thread's last seen error.
*
* # Safety
* Avoid the following undefined behavior:
* - Writing to the error string
* - Reading the error string after the thread that called `sealevel_strerror` exited.
*/
const char *sealevel_strerror(void);
/**
* Loads a Sealevel program from an ELF buffer and verifies its SBF bytecode.
*
* Sets `sealevel_errno` and returns a null pointer if loading failed.
*
* Syscalls and config may be null pointers, in which case defaults are used.
* These defaults is not stable across any libsealevel versions.
*
* If a syscall registry is provided, it is consumed, and cannot be used a second time.
*
* # Safety
* Avoid the following undefined behavior:
* - Using the syscalls object parameter after calling this function (including a second call of this function).
* - Providing a config object that has been freed with `sealevel_config_free` before.
*/
sealevel_executable *sealevel_load_program(const sealevel_config *config,
sealevel_syscall_registry syscalls,
const char *data,
size_t data_len);
/**
* Compiles a program to native executable code.
*
* Sets `sealevel_errno`.
*
* # Safety
* Avoid the following undefined behavior:
* - Calling this function twice on the same program.
* - Calling this function given a null pointer or an invalid pointer.
*/
void sealevel_program_jit_compile(sealevel_executable *program);
/**
* Creates a Sealevel virtual machine and loads the given program into it.
*
* Sets `sealevel_errno` and returns a null pointer if loading failed.
*
* The given heap should be 16-byte aligned.
*
* # Safety
* Avoid the following undefined behavior:
* - Destroying the program, heap, or regions before calling sealevel_vm_destroy.
* - Sharing the same heap or memory regions across multiple threads.
*/
sealevel_vm *sealevel_vm_create(const sealevel_executable *program,
uint8_t *heap_ptr,
size_t heap_len,
const sealevel_region *regions_ptr,
int regions_count);
/**
* Release a virtual machine and its execution state.
*
* # Safety
* Avoid the following undefined behavior:
* - Invoking this function twice on the same VM
*/
void sealevel_vm_destroy(sealevel_vm *vm);
/**
* Executes a Sealevel virtual machine.
*
* # Safety
* Avoid the following undefined behavior:
* - Passing a null pointer argument
* - Invoking the same program twice
*/
uint64_t sealevel_vm_execute(sealevel_vm *vm);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus