Skip to content

Commit 200560e

Browse files
authored
Enable tests for stdlib (hyperledger-solang#1299)
* Enable tests for stdlib Signed-off-by: Lucas Steuernagel <[email protected]>
1 parent 38d3bee commit 200560e

25 files changed

+547
-467
lines changed

.github/workflows/test.yml

+16
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,19 @@ jobs:
366366
working-directory: ./vscode
367367
- run: vsce package
368368
working-directory: ./vscode
369+
370+
std-lib-tests:
371+
name: Stdlib
372+
runs-on: solang-ubuntu-latest
373+
steps:
374+
- name: Checkout sources
375+
uses: actions/checkout@v3
376+
- name: Lints for stdlib
377+
run: |
378+
make lint
379+
working-directory: ./stdlib
380+
- name: Stdlib tests
381+
run: |
382+
make test
383+
./test
384+
working-directory: ./stdlib

build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ use std::process::Command;
55
fn main() {
66
#[cfg(feature = "llvm")]
77
{
8+
Command::new("make")
9+
.args(["-C", "stdlib"])
10+
.output()
11+
.expect("Could not build stdlib");
12+
813
// compile our linker
914
let cxxflags = Command::new("llvm-config")
1015
.args(["--cxxflags"])

docs/installing.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ And on Windows, assuming *installdir* was ``C:\Users\User\solang-llvm``:
197197
Step 2: Build Solang
198198
____________________
199199

200-
Once you have the correct LLVM version in your path, simply run:
200+
Once you have the correct LLVM version in your path, ensure you have GNU make installed and simply run:
201201

202202
.. code-block:: bash
203203
@@ -213,7 +213,7 @@ Alternative step 2: Build Solang from crates.io
213213
_______________________________________________
214214

215215
The latest Solang release is on `crates.io <https://crates.io/crates/solang>`_. Once you have the
216-
correct LLVM version in your path, simply run:
216+
correct LLVM version in your path, ensure you have GNU make installed and simply run:
217217

218218
.. code-block:: bash
219219

stdlib/.clang-format

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ReflowComments: false
2+
BasedOnStyle: Microsoft
3+
SortIncludes: Never

stdlib/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
wasm/
2+
bpf/

stdlib/Makefile

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@ CC=clang
22
BIT_INT_FLAGS=-Xclang -fexperimental-max-bitint-width=512
33
CFLAGS=$(TARGET_FLAGS) -emit-llvm -O3 -ffreestanding -fno-builtin -Wall -Wno-unused-function $(BIT_INT_FLAGS)
44

5-
bpf/%.bc wasm/%.bc: %.c
5+
bpf/%.bc: %.c
6+
$(CC) -c $(CFLAGS) $< -o $@
7+
8+
wasm/%.bc: %.c
69
$(CC) -c $(CFLAGS) $< -o $@
710

811
SOLANA=$(addprefix bpf/,solana.bc bigint.bc format.bc stdlib.bc ripemd160.bc heap.bc)
912
WASM=$(addprefix wasm/,ripemd160.bc stdlib.bc bigint.bc format.bc heap.bc)
1013

1114
all: $(SOLANA) $(WASM)
1215

16+
$(SOLANA) $(WASM): | outputs_dirs
17+
1318
$(SOLANA): TARGET_FLAGS=--target=sbf
1419
$(WASM): TARGET_FLAGS=--target=wasm32
1520

16-
bpf/solana.bc: solana.c solana_sdk.h
21+
bpf/solana.bc: solana.c solana_sdk.h | outputs_dirs
22+
23+
outputs_dirs:
24+
@mkdir -p bpf wasm
1725

1826
clean:
19-
rm -r bpf/* wasm/*
27+
rm -rf bpf wasm
28+
29+
test:
30+
clang -DTEST -DSOL_TEST -O3 -Wall solana.c stdlib.c -o test
31+
32+
lint:
33+
clang-format *.c *.h --style=file --dry-run -Werror

stdlib/bigint.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ bool __mul32_with_builtin_ovf(uint32_t left[], uint32_t right[], uint32_t out[],
120120
// Some compiler runtime builtins we need.
121121

122122
// 128 bit shift left.
123-
typedef union
124-
{
123+
typedef union {
125124
__uint128_t all;
126125
struct
127126
{
@@ -131,8 +130,7 @@ typedef union
131130
} two64;
132131

133132
// 128 bit shift left.
134-
typedef union
135-
{
133+
typedef union {
136134
__int128_t all;
137135
struct
138136
{

stdlib/bpf/bigint.bc

-24.2 KB
Binary file not shown.

stdlib/bpf/format.bc

-13 KB
Binary file not shown.

stdlib/bpf/heap.bc

-5.75 KB
Binary file not shown.

stdlib/bpf/ripemd160.bc

-9.2 KB
Binary file not shown.

stdlib/bpf/solana.bc

-30.7 KB
Binary file not shown.

stdlib/bpf/stdlib.bc

-8.11 KB
Binary file not shown.

stdlib/format.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ char *uint2dec(char *output, uint64_t val)
109109
return output;
110110
}
111111

112-
extern int udivmod128(const __uint128_t *dividend, const __uint128_t *divisor, __uint128_t *remainder, __uint128_t *quotient);
112+
extern int udivmod128(const __uint128_t *dividend, const __uint128_t *divisor, __uint128_t *remainder,
113+
__uint128_t *quotient);
113114

114115
char *uint128dec(char *output, __uint128_t val128)
115116
{
@@ -183,8 +184,7 @@ typedef unsigned _BitInt(256) uint256_t;
183184

184185
extern int udivmod256(const uint256_t *dividend, const uint256_t *divisor, uint256_t *remainder, uint256_t *quotient);
185186

186-
char *
187-
uint256dec(char *output, uint256_t *val256)
187+
char *uint256dec(char *output, uint256_t *val256)
188188
{
189189
// we want 1e19, how to declare such a constant in clang?
190190
const uint256_t n1e10 = 10000000000;
@@ -243,14 +243,14 @@ uint256dec(char *output, uint256_t *val256)
243243
static const char b58digits[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
244244

245245
// https://github.com/bitcoin/libbase58/blob/b1dd03fa8d1be4be076bb6152325c6b5cf64f678/base58.c inspired this code.
246-
void base58_encode_solana_address(uint8_t * data, uint32_t data_len, uint8_t * output, uint32_t output_len)
246+
void base58_encode_solana_address(uint8_t *data, uint32_t data_len, uint8_t *output, uint32_t output_len)
247247
{
248248
uint32_t j, carry, zero_count = 0;
249249

250250
while (zero_count < data_len && !data[zero_count])
251251
++zero_count;
252252

253-
for (uint32_t i=zero_count, high = output_len - 1; i < data_len; i++, high = j)
253+
for (uint32_t i = zero_count, high = output_len - 1; i < data_len; i++, high = j)
254254
{
255255
for (carry = data[i], j = output_len - 1; (j > high) || carry; --j)
256256
{
@@ -264,7 +264,7 @@ void base58_encode_solana_address(uint8_t * data, uint32_t data_len, uint8_t * o
264264
}
265265
}
266266

267-
for (j=0; j < output_len; j++)
267+
for (j = 0; j < output_len; j++)
268268
{
269269
output[j] = b58digits[output[j]];
270270
}

stdlib/heap.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ void __init_heap()
3333
struct chunk *first = HEAP_START;
3434
first->next = first->prev = NULL;
3535
first->allocated = false;
36-
first->length = (size_t)(__builtin_wasm_memory_size(0) * 0x10000 -
37-
(size_t)first - sizeof(struct chunk));
36+
first->length = (size_t)(__builtin_wasm_memory_size(0) * 0x10000 - (size_t)first - sizeof(struct chunk));
3837
}
3938
#else
4039
#define HEAP_START ((struct chunk *)0x300000000)

0 commit comments

Comments
 (0)