Move bpf-sdk to sdk/bpf
This commit is contained in:
2
sdk/bpf/.gitignore
vendored
Normal file
2
sdk/bpf/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/criterion-*
|
||||
/llvm/native-*
|
85
sdk/bpf/README.md
Normal file
85
sdk/bpf/README.md
Normal file
@ -0,0 +1,85 @@
|
||||
|
||||
## Prerequisites
|
||||
|
||||
## LLVM / clang 7.0.0
|
||||
http://releases.llvm.org/download.html
|
||||
|
||||
### Linux Ubuntu 18.04 (bionic)
|
||||
```
|
||||
$ wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
|
||||
$ sudo apt-add-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-7 main"
|
||||
$ sudo apt-get update
|
||||
$ sudo apt-get install -y clang-7
|
||||
```
|
||||
|
||||
### Linux Ubuntu 16.04 (xenial)
|
||||
```
|
||||
$ wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
|
||||
$ sudo apt-add-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-7 main"
|
||||
$ sudo apt-get update
|
||||
$ sudo apt-get install -y clang-7
|
||||
```
|
||||
|
||||
### Linux Ubuntu 14.04 (trusty)
|
||||
```
|
||||
$ wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
|
||||
$ sudo apt-add-repository "deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-7 main"
|
||||
$ sudo apt-get update
|
||||
$ sudo apt-get install -y clang-7
|
||||
```
|
||||
|
||||
### macOS
|
||||
The following depends on Homebrew, instructions on how to install Homebrew are at https://brew.sh
|
||||
|
||||
Once Homebrew is installed, ensure the latest llvm is installed:
|
||||
```
|
||||
$ brew update # <- ensure your brew is up to date
|
||||
$ brew install llvm # <- should output “Warning: llvm 7.0.0 is already installed and up-to-date”
|
||||
$ brew --prefix llvm # <- should output “/usr/local/opt/llvm”
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
### Quick start
|
||||
To get started create a `makefile` containing:
|
||||
```make
|
||||
include path/to/bpf.mk
|
||||
```
|
||||
and `src/program.c` containing:
|
||||
```c
|
||||
#include <solana_sdk.h>
|
||||
|
||||
bool entrypoint(const uint8_t *input) {
|
||||
SolKeyedAccount ka[1];
|
||||
uint8_t *data;
|
||||
uint64_t data_len;
|
||||
|
||||
if (!sol_deserialize(buf, ka, SOL_ARRAY_SIZE(ka), NULL, &data, &data_len)) {
|
||||
return false;
|
||||
}
|
||||
print_params(1, ka, data, data_len);
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
Then run `make` to build `out/program.o`.
|
||||
Run `make help` for more details.
|
||||
|
||||
### Unit tests
|
||||
Built-in support for unit testing is provided by the
|
||||
[Criterion](https://criterion.readthedocs.io/en/master/index.html) test framework.
|
||||
To get started create the file `test/example.c` containing:
|
||||
```c
|
||||
#include <criterion/criterion.h>
|
||||
#include "../src/program.c"
|
||||
|
||||
Test(test_suite_name, test_case_name) {
|
||||
cr_assert(true);
|
||||
}
|
||||
```
|
||||
Then run `make test`.
|
||||
|
||||
### Limitations
|
||||
* Programs must be fully contained within a single .c file
|
||||
* No libc is available but `solana_sdk.h` provides a minimal set of
|
||||
primitives.
|
189
sdk/bpf/bpf.mk
Normal file
189
sdk/bpf/bpf.mk
Normal file
@ -0,0 +1,189 @@
|
||||
LOCAL_PATH := $(dir $(lastword $(MAKEFILE_LIST)))
|
||||
|
||||
all:
|
||||
.PHONY: help all clean
|
||||
|
||||
ifneq ($(V),1)
|
||||
_@ :=@
|
||||
endif
|
||||
|
||||
INC_DIRS ?=
|
||||
SRC_DIR ?= ./src
|
||||
TEST_DIR ?= ./test
|
||||
OUT_DIR ?= ./out
|
||||
|
||||
ifeq ($(DOCKER),1)
|
||||
LLVM_DIR = $(LOCAL_PATH)llvm/docker
|
||||
else
|
||||
LLVM_DIR = $(LOCAL_PATH)llvm/native-v0.0.1
|
||||
endif
|
||||
|
||||
ifdef LLVM_DIR
|
||||
CC := $(LLVM_DIR)/bin/clang
|
||||
CXX := $(LLVM_DIR)/bin/clang++
|
||||
LLC := $(LLVM_DIR)/bin/llc
|
||||
OBJ_DUMP := $(LLVM_DIR)/bin/llvm-objdump
|
||||
endif
|
||||
|
||||
SYSTEM_INC_DIRS := \
|
||||
$(LOCAL_PATH)inc \
|
||||
$(LLVM_DIR)lib/clang/8.0.0/include \
|
||||
|
||||
C_FLAGS := \
|
||||
-Werror \
|
||||
-O2 \
|
||||
-fno-builtin \
|
||||
-std=c17 \
|
||||
$(addprefix -isystem,$(SYSTEM_INC_DIRS)) \
|
||||
$(addprefix -I,$(INC_DIRS))
|
||||
|
||||
CXX_FLAGS := \
|
||||
$(C_FLAGS) \
|
||||
-std=c++17 \
|
||||
|
||||
BPF_C_FLAGS := \
|
||||
$(C_FLAGS) \
|
||||
-emit-llvm \
|
||||
-target bpf \
|
||||
|
||||
BPF_CXX_FLAGS := \
|
||||
$(CXX_FLAGS) \
|
||||
-emit-llvm \
|
||||
-target bpf \
|
||||
|
||||
BPF_LLC_FLAGS := \
|
||||
-march=bpf \
|
||||
-filetype=obj \
|
||||
|
||||
OBJ_DUMP_FLAGS := \
|
||||
-color \
|
||||
-source \
|
||||
-disassemble \
|
||||
|
||||
TESTFRAMEWORK_RPATH := $(abspath $(LOCAL_PATH)criterion-v2.3.2/lib)
|
||||
TESTFRAMEWORK_FLAGS := \
|
||||
-DSOL_TEST \
|
||||
-isystem $(LOCAL_PATH)criterion-v2.3.2/include \
|
||||
-L $(LOCAL_PATH)criterion-v2.3.2/lib \
|
||||
-rpath $(TESTFRAMEWORK_RPATH) \
|
||||
-lcriterion \
|
||||
|
||||
# The "-rpath" in TESTFRAMEWORK_FLAGS doesn't work in macOS so rewrite the name
|
||||
# post-link.
|
||||
# TODO: Find a better way
|
||||
MACOS_ADJUST_TEST_DYLIB := \
|
||||
$(if $(filter $(OS),Darwin),\
|
||||
$(_@)install_name_tool -change libcriterion.3.dylib $(TESTFRAMEWORK_RPATH)/libcriterion.3.dylib, \
|
||||
: \
|
||||
)
|
||||
|
||||
TEST_C_FLAGS := \
|
||||
$(C_FLAGS) \
|
||||
$(TESTFRAMEWORK_FLAGS) \
|
||||
|
||||
TEST_CXX_FLAGS := \
|
||||
$(CXX_FLAGS) \
|
||||
$(TESTFRAMEWORK_FLAGS) \
|
||||
|
||||
help:
|
||||
@echo ''
|
||||
@echo 'BPF Program makefile'
|
||||
@echo ''
|
||||
@echo 'This makefile will build BPF Programs from C or C++ source files into ELFs'
|
||||
@echo ''
|
||||
@echo 'Assumptions:'
|
||||
@echo ' - Programs are a single .c or .cc source file (may include headers)'
|
||||
@echo ' - Programs are located in the source directory: $(SRC_DIR)'
|
||||
@echo ' - Programs are named by their basename (eg. file name:foo.c/foo.cc -> program name:foo)'
|
||||
@echo ' - Tests are located in the test directory: $(TEST_DIR)'
|
||||
@echo ' - Tests are named by their basename (eg. file name:foo.c/foo.cc -> test name:test_foo)'
|
||||
@echo ' - Output files will be placed in the directory: $(OUT_DIR)'
|
||||
@echo ''
|
||||
@echo 'User settings'
|
||||
@echo ' - The following setting are overridable on the command line, default values shown:'
|
||||
@echo ' - Show commands while building: V=1'
|
||||
@echo ' V=$(V)'
|
||||
@echo ' - Use LLVM from docker: DOCKER=1'
|
||||
@echo ' DOCKER=$(DOCKER)'
|
||||
@echo ' - List of include directories:'
|
||||
@echo ' INC_DIRS=$(INC_DIRS)'
|
||||
@echo ' - List of system include directories:'
|
||||
@echo ' SYSTEM_INC_DIRS=$(SYSTEM_INC_DIRS)'
|
||||
@echo ' - Location of source files:'
|
||||
@echo ' SRC_DIR=$(SRC_DIR)'
|
||||
@echo ' - Location of test files:'
|
||||
@echo ' TEST_DIR=$(TEST_DIR)'
|
||||
@echo ' - Location to place output files:'
|
||||
@echo ' OUT_DIR=$(OUT_DIR)'
|
||||
@echo ' - Location of LLVM:'
|
||||
@echo ' LLVM_DIR=$(LLVM_DIR)'
|
||||
@echo ''
|
||||
@echo 'Usage:'
|
||||
@echo ' - make help - This help message'
|
||||
@echo ' - make all - Build all the programs'
|
||||
@echo ' - make test - Build and run all tests'
|
||||
@echo ' - make dump_<program name> - Dumps the contents of the program to stdout'
|
||||
@echo ' - make <program name> - Build a single program by name'
|
||||
@echo ''
|
||||
@echo 'Available programs:'
|
||||
$(foreach name, $(PROGRAM_NAMES), @echo ' - $(name)'$(\n))
|
||||
@echo 'Available tests:'
|
||||
$(foreach name, $(TEST_NAMES), @echo ' - $(name)'$(\n))
|
||||
@echo ''
|
||||
@echo 'Example:'
|
||||
@echo ' - Assuming a programed named foo (src/foo.c)'
|
||||
@echo ' - make foo'
|
||||
@echo ' - make dump_foo'
|
||||
@echo ''
|
||||
|
||||
.PRECIOUS: $(OUT_DIR)/%.bc
|
||||
$(OUT_DIR)/%.bc: $(SRC_DIR)/%.c
|
||||
@echo "[cc] $@ ($<)"
|
||||
$(_@)mkdir -p $(OUT_DIR)
|
||||
$(_@)$(CC) $(BPF_C_FLAGS) -o $@ -c $< -MD -MF $(@:.bc=.d)
|
||||
|
||||
$(OUT_DIR)/%.bc: $(SRC_DIR)/%.cc
|
||||
@echo "[cc] $@ ($<)"
|
||||
$(_@)mkdir -p $(OUT_DIR)
|
||||
$(_@)$(CXX) $(BPF_CXX_FLAGS) -o $@ -c $< -MD -MF $(@:.bc=.d)
|
||||
|
||||
.PRECIOUS: $(OUT_DIR)/%.o
|
||||
$(OUT_DIR)/%.o: $(OUT_DIR)/%.bc
|
||||
@echo "[llc] $@ ($<)"
|
||||
$(_@)$(LLC) $(BPF_LLC_FLAGS) -o $@ $<
|
||||
|
||||
$(OUT_DIR)/test_%: $(TEST_DIR)/%.c
|
||||
@echo "[test cc] $@ ($<)"
|
||||
$(_@)mkdir -p $(OUT_DIR)
|
||||
$(_@)$(CC) $(TEST_C_FLAGS) -o $@ $< -MD -MF $(@:=.d)
|
||||
$(_@)$(MACOS_ADJUST_TEST_DYLIB) $@
|
||||
|
||||
$(OUT_DIR)/test_%: $(TEST_DIR)/%.cc
|
||||
@echo "[test cc] $@ ($<)"
|
||||
$(_@)mkdir -p $(OUT_DIR)
|
||||
$(_@)$(CXX) $(TEST_CXX_FLAGS) -o $@ $< -MD -MF $(@:=.d)
|
||||
$(_@)$(MACOS_ADJUST_TEST_DYLIB) $@
|
||||
|
||||
-include $(wildcard $(OUT_DIR)/*.d)
|
||||
|
||||
PROGRAM_NAMES := $(notdir $(basename $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/*.cc)))
|
||||
TEST_NAMES := $(addprefix test_,$(notdir $(basename $(wildcard $(TEST_DIR)/*.c))))
|
||||
|
||||
define \n
|
||||
|
||||
|
||||
endef
|
||||
|
||||
all: $(PROGRAM_NAMES)
|
||||
|
||||
test: $(TEST_NAMES)
|
||||
$(foreach test, $(TEST_NAMES), $(OUT_DIR)/$(test)$(\n))
|
||||
|
||||
$(PROGRAM_NAMES): %: $(addprefix $(OUT_DIR)/, %.o) ;
|
||||
$(TEST_NAMES): %: $(addprefix $(OUT_DIR)/, %) ;
|
||||
|
||||
dump_%: %
|
||||
$(_@)$(OBJ_DUMP) $(OBJ_DUMP_FLAGS) $(addprefix $(OUT_DIR)/, $(addsuffix .o, $<))
|
||||
|
||||
clean:
|
||||
rm -rf $(OUT_DIR)
|
362
sdk/bpf/inc/solana_sdk.h
Normal file
362
sdk/bpf/inc/solana_sdk.h
Normal file
@ -0,0 +1,362 @@
|
||||
#pragma once
|
||||
/**
|
||||
* @brief Solana C-based BPF program utility functions and types
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Pick up static_assert if C11 or greater
|
||||
*
|
||||
* Inlined here until <assert.h> is available
|
||||
*/
|
||||
#if (defined _ISOC11_SOURCE || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L)) && !defined (__cplusplus)
|
||||
#undef static_assert
|
||||
#define static_assert _Static_assert
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Numeric types
|
||||
*/
|
||||
#ifndef __LP64__
|
||||
#error LP64 data model required
|
||||
#endif
|
||||
|
||||
typedef signed char int8_t;
|
||||
typedef unsigned char uint8_t;
|
||||
typedef signed short int16_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef signed int int32_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef signed long int int64_t;
|
||||
typedef unsigned long int uint64_t;
|
||||
typedef int64_t ssize_t;
|
||||
typedef uint64_t size_t;
|
||||
|
||||
#if defined (__cplusplus) || defined(static_assert)
|
||||
static_assert(sizeof(int8_t) == 1);
|
||||
static_assert(sizeof(uint8_t) == 1);
|
||||
static_assert(sizeof(int16_t) == 2);
|
||||
static_assert(sizeof(uint16_t) == 2);
|
||||
static_assert(sizeof(int32_t) == 4);
|
||||
static_assert(sizeof(uint32_t) == 4);
|
||||
static_assert(sizeof(int64_t) == 8);
|
||||
static_assert(sizeof(uint64_t) == 8);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* NULL
|
||||
*/
|
||||
#define NULL 0
|
||||
|
||||
/**
|
||||
* Boolean type
|
||||
*/
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Helper function that prints a string to stdout
|
||||
*/
|
||||
void sol_log(const char *);
|
||||
|
||||
/**
|
||||
* Helper function that prints a 64 bit values represented in hexadecimal
|
||||
* to stdout
|
||||
*/
|
||||
void sol_log_64(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
|
||||
|
||||
|
||||
/**
|
||||
* Prefix for all BPF functions
|
||||
*
|
||||
* This prefix should be used for functions in order to facilitate
|
||||
* interoperability with BPF representation
|
||||
*/
|
||||
#define SOL_FN_PREFIX __attribute__((always_inline)) static
|
||||
|
||||
/**
|
||||
* Size of Public key in bytes
|
||||
*/
|
||||
#define SIZE_PUBKEY 32
|
||||
|
||||
/**
|
||||
* Public key
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t x[SIZE_PUBKEY];
|
||||
} SolPubkey;
|
||||
|
||||
/**
|
||||
* Compares two public keys
|
||||
*
|
||||
* @param one First public key
|
||||
* @param two Second public key
|
||||
* @return true if the same
|
||||
*/
|
||||
SOL_FN_PREFIX bool SolPubkey_same(const SolPubkey *one, const SolPubkey *two) {
|
||||
for (int i = 0; i < sizeof(*one); i++) {
|
||||
if (one->x[i] != two->x[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keyed Account
|
||||
*/
|
||||
typedef struct {
|
||||
SolPubkey *key; /** Public key of the account */
|
||||
bool is_signer; /** Transaction was signed by this account's key */
|
||||
uint64_t *tokens; /** Number of tokens owned by this account */
|
||||
uint64_t userdata_len; /** Length of data in bytes */
|
||||
uint8_t *userdata; /** On-chain data within this account */
|
||||
SolPubkey *owner; /** Program that owns this account */
|
||||
} SolKeyedAccount;
|
||||
|
||||
/**
|
||||
* Copies memory
|
||||
*/
|
||||
SOL_FN_PREFIX void sol_memcpy(void *dst, const void *src, int len) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
*((uint8_t *)dst + i) = *((const uint8_t *)src + i);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares memory
|
||||
*/
|
||||
SOL_FN_PREFIX int sol_memcmp(const void *s1, const void *s2, int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
uint8_t diff = *((uint8_t *)s1 + i) - *((const uint8_t *)s2 + i);
|
||||
if (diff) {
|
||||
return diff;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill a byte string with a byte value
|
||||
*/
|
||||
SOL_FN_PREFIX void *sol_memset(void *b, int c, size_t len) {
|
||||
uint8_t *a = (uint8_t *) b;
|
||||
while (len > 0) {
|
||||
*a = c;
|
||||
a++;
|
||||
len--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find length of string
|
||||
*/
|
||||
SOL_FN_PREFIX size_t sol_strlen(const char *s) {
|
||||
size_t len = 0;
|
||||
while (*s) {
|
||||
len++;
|
||||
s++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the number of elements in an array
|
||||
*/
|
||||
#define SOL_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
/**
|
||||
* Panics
|
||||
*
|
||||
* Prints the line number where the panic occurred and then causes
|
||||
* the BPF VM to immediately halt execution. No accounts' userdata are updated
|
||||
*/
|
||||
#define sol_panic() _sol_panic(__LINE__)
|
||||
SOL_FN_PREFIX void _sol_panic(uint64_t line) {
|
||||
sol_log_64(0xFF, 0xFF, 0xFF, 0xFF, line);
|
||||
uint8_t *pv = (uint8_t *)1;
|
||||
*pv = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts
|
||||
*/
|
||||
#define sol_assert(expr) \
|
||||
if (!(expr)) { \
|
||||
_sol_panic(__LINE__); \
|
||||
}
|
||||
|
||||
/**
|
||||
* Information about the state of the cluster immediately before the program
|
||||
* started executing the current instruction
|
||||
*/
|
||||
typedef struct {
|
||||
uint64_t tick_height; /** Current ledger tick */
|
||||
const SolPubkey *program_id; /** program_id of the currently executing program */
|
||||
} SolClusterInfo;
|
||||
|
||||
/**
|
||||
* De-serializes the input parameters into usable types
|
||||
*
|
||||
* Use this function to deserialize the buffer passed to the program entrypoint
|
||||
* into usable types. This function does not perform copy deserialization,
|
||||
* instead it populates the pointers and lengths in SolKeyedAccount and data so
|
||||
* that any modification to tokens or account data take place on the original
|
||||
* buffer. Doing so also eliminates the need to serialize back into the buffer
|
||||
* at program end.
|
||||
*
|
||||
* @param input Source buffer containing serialized input parameters
|
||||
* @param ka Pointer to an array of SolKeyedAccount to deserialize into
|
||||
* @param ka_len Number of SolKeyedAccount entries in `ka`
|
||||
* @param ka_len_out If NULL, fill exactly `ka_len` accounts or fail.
|
||||
* If not NULL, fill up to `ka_len` accounts and return the
|
||||
* number of filled accounts in `ka_len_out`.
|
||||
* @param data On return, a pointer to the instruction data
|
||||
* @param data_len On return, the length in bytes of the instruction data
|
||||
* @param cluster_info If not NULL, fill cluster_info
|
||||
* @return Boolean true if successful
|
||||
*/
|
||||
SOL_FN_PREFIX bool sol_deserialize(
|
||||
const uint8_t *input,
|
||||
SolKeyedAccount *ka,
|
||||
uint64_t ka_len,
|
||||
uint64_t *ka_len_out,
|
||||
const uint8_t **data,
|
||||
uint64_t *data_len,
|
||||
SolClusterInfo *cluster_info
|
||||
) {
|
||||
if (ka_len_out == NULL) {
|
||||
if (ka_len != *(uint64_t *) input) {
|
||||
return false;
|
||||
}
|
||||
ka_len = *(uint64_t *) input;
|
||||
} else {
|
||||
if (ka_len > *(uint64_t *) input) {
|
||||
ka_len = *(uint64_t *) input;
|
||||
}
|
||||
*ka_len_out = ka_len;
|
||||
}
|
||||
|
||||
input += sizeof(uint64_t);
|
||||
for (int i = 0; i < ka_len; i++) {
|
||||
// key
|
||||
ka[i].is_signer = *(uint64_t *) input != 0;
|
||||
input += sizeof(uint64_t);
|
||||
ka[i].key = (SolPubkey *) input;
|
||||
input += sizeof(SolPubkey);
|
||||
|
||||
// tokens
|
||||
ka[i].tokens = (uint64_t *) input;
|
||||
input += sizeof(uint64_t);
|
||||
|
||||
// account userdata
|
||||
ka[i].userdata_len = *(uint64_t *) input;
|
||||
input += sizeof(uint64_t);
|
||||
ka[i].userdata = (uint8_t *) input;
|
||||
input += ka[i].userdata_len;
|
||||
|
||||
// owner
|
||||
ka[i].owner = (SolPubkey *) input;
|
||||
input += sizeof(SolPubkey);
|
||||
}
|
||||
|
||||
// input data
|
||||
*data_len = *(uint64_t *) input;
|
||||
input += sizeof(uint64_t);
|
||||
*data = input;
|
||||
input += *data_len;
|
||||
|
||||
if (cluster_info != NULL) {
|
||||
cluster_info->tick_height = *(uint64_t *) input;
|
||||
input += sizeof(uint64_t);
|
||||
cluster_info->program_id = (SolPubkey *) input;
|
||||
input += sizeof(SolPubkey);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging utilities
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Prints the hexadecimal representation of a public key
|
||||
*
|
||||
* @param key The public key to print
|
||||
*/
|
||||
SOL_FN_PREFIX void sol_log_key(const SolPubkey *key) {
|
||||
for (int j = 0; j < sizeof(*key); j++) {
|
||||
sol_log_64(0, 0, 0, j, key->x[j]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the hexadecimal representation of an array
|
||||
*
|
||||
* @param array The array to print
|
||||
*/
|
||||
SOL_FN_PREFIX void sol_log_array(const uint8_t *array, int len) {
|
||||
for (int j = 0; j < len; j++) {
|
||||
sol_log_64(0, 0, 0, j, array[j]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the hexadecimal representation of the program's input parameters
|
||||
*
|
||||
* @param ka A pointer to an array of SolKeyedAccount to print
|
||||
* @param ka_len Number of SolKeyedAccount to print
|
||||
* @param data A pointer to the instruction data to print
|
||||
* @param data_len The length in bytes of the instruction data
|
||||
*/
|
||||
SOL_FN_PREFIX void sol_log_params(
|
||||
const SolKeyedAccount *ka,
|
||||
uint64_t ka_len,
|
||||
const uint8_t *data,
|
||||
uint64_t data_len
|
||||
) {
|
||||
sol_log_64(0, 0, 0, 0, ka_len);
|
||||
for (int i = 0; i < ka_len; i++) {
|
||||
sol_log_64(0, 0, 0, 0, ka[i].is_signer);
|
||||
sol_log_key(ka[i].key);
|
||||
sol_log_64(0, 0, 0, 0, *ka[i].tokens);
|
||||
sol_log_array(ka[i].userdata, ka[i].userdata_len);
|
||||
sol_log_key(ka[i].owner);
|
||||
}
|
||||
sol_log_array(data, data_len);
|
||||
}
|
||||
|
||||
/**@}*/
|
||||
|
||||
/**
|
||||
* Program instruction entrypoint
|
||||
*
|
||||
* @param input Buffer of serialized input parameters. Use sol_deserialize() to decode
|
||||
* @return true if the instruction executed successfully
|
||||
*/
|
||||
bool entrypoint(const uint8_t *input);
|
||||
|
||||
|
||||
#ifdef SOL_TEST
|
||||
/**
|
||||
* Stub log functions when building tests
|
||||
*/
|
||||
#include <stdio.h>
|
||||
void sol_log(const char *s) {
|
||||
printf("sol_log: %s\n", s);
|
||||
}
|
||||
void sol_log_64(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) {
|
||||
printf("sol_log_64: %llu, %llu, %llu, %llu, %llu\n", arg1, arg2, arg3, arg4, arg5);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/**@}*/
|
2
sdk/bpf/inc/stdio.h
Normal file
2
sdk/bpf/inc/stdio.h
Normal file
@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
typedef void *FILE;
|
2
sdk/bpf/inc/stdlib.h
Normal file
2
sdk/bpf/inc/stdlib.h
Normal file
@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include <solana_sdk.h>
|
7
sdk/bpf/inc/string.h
Normal file
7
sdk/bpf/inc/string.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
#include <solana_sdk.h>
|
||||
|
||||
#define memcpy sol_memcpy
|
||||
#define memset sol_memset
|
||||
#define strlen sol_strlen
|
||||
|
1
sdk/bpf/inc/sys/param.h
Normal file
1
sdk/bpf/inc/sys/param.h
Normal file
@ -0,0 +1 @@
|
||||
#pragma once
|
1
sdk/bpf/inc/wchar.h
Normal file
1
sdk/bpf/inc/wchar.h
Normal file
@ -0,0 +1 @@
|
||||
#pragma once
|
4
sdk/bpf/llvm/docker/bin/clang
Executable file
4
sdk/bpf/llvm/docker/bin/clang
Executable file
@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -ex
|
||||
SDKPATH="$( cd "$(dirname "$0")" ; pwd -P )"/../../../..
|
||||
docker run --workdir /solana_sdk --volume $SDKPATH:/solana_sdk --rm solanalabs/llvm `basename "$0"` "$@"
|
4
sdk/bpf/llvm/docker/bin/clang++
Executable file
4
sdk/bpf/llvm/docker/bin/clang++
Executable file
@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -ex
|
||||
SDKPATH="$( cd "$(dirname "$0")" ; pwd -P )"/../../../..
|
||||
docker run --workdir /solana_sdk --volume $SDKPATH:/solana_sdk --rm solanalabs/llvm `basename "$0"` "$@"
|
4
sdk/bpf/llvm/docker/bin/llc
Executable file
4
sdk/bpf/llvm/docker/bin/llc
Executable file
@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -ex
|
||||
SDKPATH="$( cd "$(dirname "$0")" ; pwd -P )"/../../../..
|
||||
docker run --workdir /solana_sdk --volume $SDKPATH:/solana_sdk --rm solanalabs/llvm `basename "$0"` "$@"
|
4
sdk/bpf/llvm/docker/bin/llvm-objdump
Executable file
4
sdk/bpf/llvm/docker/bin/llvm-objdump
Executable file
@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -ex
|
||||
SDKPATH="$( cd "$(dirname "$0")" ; pwd -P )"/../../../..
|
||||
docker run --workdir /solana_sdk --volume $SDKPATH:/solana_sdk --rm solanalabs/llvm `basename "$0"` "$@"
|
13
sdk/bpf/llvm/docker/generate.sh
Executable file
13
sdk/bpf/llvm/docker/generate.sh
Executable file
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
read -r -d '' SCRIPT << 'EOM'
|
||||
#!/usr/bin/env bash
|
||||
set -ex
|
||||
SDKPATH="$( cd "$(dirname "$0")" ; pwd -P )"/../../../..
|
||||
docker run --workdir /solana_sdk --volume $SDKPATH:/solana_sdk --rm solanalabs/llvm `basename "$0"` "$@"
|
||||
EOM
|
||||
|
||||
echo "$SCRIPT" > bin/clang
|
||||
echo "$SCRIPT" > bin/clang++
|
||||
echo "$SCRIPT" > bin/llc
|
||||
echo "$SCRIPT" > bin/llvm-objdump
|
54
sdk/bpf/scripts/install.sh
Executable file
54
sdk/bpf/scripts/install.sh
Executable file
@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "$(dirname "$0")"/..
|
||||
|
||||
# Install Criterion for all supported platforms
|
||||
# if changing version here must also change in bpf.mk
|
||||
version=v2.3.2
|
||||
if [[ ! -d criterion-$version ]]; then
|
||||
(
|
||||
[[ ! -d criterion-$version ]] || exit 0
|
||||
set -ex
|
||||
wget https://github.com/Snaipe/Criterion/releases/download/$version/criterion-$version-osx-x86_64.tar.bz2
|
||||
wget https://github.com/Snaipe/Criterion/releases/download/$version/criterion-$version-linux-x86_64.tar.bz2
|
||||
tar jxf criterion-$version-osx-x86_64.tar.bz2
|
||||
tar jxf criterion-$version-linux-x86_64.tar.bz2
|
||||
rm -rf criterion-$version-osx-x86_64.tar.bz2 criterion-$version-linux-x86_64.tar.bz2
|
||||
|
||||
[[ ! -f criterion-$version/README.md ]]
|
||||
echo "https://github.com/Snaipe/Criterion/releases/tag/$version" > criterion-$version/README.md
|
||||
)
|
||||
# shellcheck disable=SC2181
|
||||
if [[ $? -ne 0 ]]; then
|
||||
rm -rf criterion-$version*
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Install LLVM
|
||||
# if changing version here must also change in bpf.mk
|
||||
version=v0.0.1
|
||||
if [ "$(uname)" == "Darwin" ]; then
|
||||
machine=macos
|
||||
else
|
||||
machine=linux
|
||||
fi
|
||||
if [[ ! -d llvm/native-$version ]]; then
|
||||
(
|
||||
set -ex
|
||||
mkdir -p llvm/native-$version
|
||||
cd llvm/native-$version
|
||||
wget https://github.com/solana-labs/llvm-builder/releases/download/$version/solana-llvm-$machine.tgz
|
||||
tar xzf solana-llvm-$machine.tgz
|
||||
rm -rf solana-llvm-$machine.tgz
|
||||
|
||||
[[ ! -f llvm/native-$version/README.md ]]
|
||||
echo "https://github.com/solana-labs/solana-web3.js/releases/tag/$version" > README.md
|
||||
)
|
||||
fi
|
||||
# shellcheck disable=SC2181
|
||||
if [[ $? -ne 0 ]]; then
|
||||
rm -rf llvm/native-$version
|
||||
exit 1
|
||||
fi
|
||||
|
18
sdk/bpf/scripts/package.sh
Executable file
18
sdk/bpf/scripts/package.sh
Executable file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
set -ex
|
||||
|
||||
SOLANA_ROOT="$(cd "$(dirname "$0")"/../../../../..; pwd)"
|
||||
[[ -f "$SOLANA_ROOT"/LICENSE && -d "$SOLANA_ROOT"/ci ]]
|
||||
|
||||
rm -rf bpf-sdk/
|
||||
mkdir bpf-sdk/
|
||||
|
||||
(
|
||||
"$SOLANA_ROOT"/ci/crate-version.sh
|
||||
git rev-parse HEAD
|
||||
) > bpf-sdk/version.txt
|
||||
|
||||
cp -ra "$SOLANA_ROOT"/programs/bpf/c/sdk/* bpf-sdk/
|
||||
rm -rf bpf-sdk/scripts/package.sh
|
||||
|
||||
tar jvcf bpf-sdk.tar.bz2 bpf-sdk/
|
Reference in New Issue
Block a user