libswarm-ng

C implementation of BMT hasher, Swarmhash and Single Owner Chunk for swarm
git clone git://git.defalsify.org/libswarm-ng.git
Log | Files | Refs | Submodules | README

commit 1f4c1fd90c5b88a15ff2d68b603b7be61b398717
parent 7aa10094f23817e6b73dfdcd8991dfa6319b718a
Author: nolash <dev@holbrook.no>
Date:   Sun, 26 Sep 2021 04:40:35 +0200

Use custom memory functions for wasm

Diffstat:
MMakefile.dev | 15+++++++++++++--
Msrc/wasm/common.c | 10++++++----
Mtest/check_bmt.c | 7++-----
3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/Makefile.dev b/Makefile.dev @@ -60,6 +60,9 @@ build_check: build_base_malloc build_check_common $(CC) -I./src -o build/test/check_file build/swarm.o build/bmt.o build/endian.o build/swarmfile.o test/check_file.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon -lsecp256k1 $(CC) -I./src -o build/test/check_chunk build/chunk.o build/swarm.o build/bmt.o build/endian.o build/swarmfile.o test/check_chunk.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon -lsecp256k1 +build_check_stack: build_base build_check_common + $(CC) -I./src -o build/test/check_bmt build/swarm.o build/bmt.o build/endian.o build/swarmfile.o test/check_bmt.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon -lsecp256k1 + build_check_keystore: build_base_malloc build_keystore build_check_common $(CC) -I./src -o build/test/check_keystore build/swarm.o build/keystore.o test/check_keystore.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon @@ -75,6 +78,9 @@ build_lib: build .PHONY: test clean check_bmt: build_check + LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no CFLAGS=-DLIBSWARM_MALLOC build/test/check_bmt_malloc + +check_bmt_stack: build_check_stack LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no CFLAGS=-DLIBSWARM_MALLOC build/test/check_bmt check_chunk: build_check @@ -112,14 +118,19 @@ install: wasm_prep: mkdir -vp build/wasm -wasm: wasm_prep +wasm_base: wasm_prep #clang $(CFLAGS) $(wasm_cflags_stdlib) -static -Wl,--export="bmt_hash" -Wl,--export="bmt_hash_heap_init" -Wl,--export="bmt_hash_heap_free" -o build/wasm/swarm.wasm src/wasm.c build/lib/libswarm.a - clang $(CFLAGS) $(wasm_cflags_stdlib) -c -Wl,--export-all -Os -o build/wasm/common_o.wasm src/wasm/common.c clang $(CFLAGS) $(wasm_cflags_stdlib) -c -Wl,--export-all -Os -o build/wasm/wasm_o.wasm src/wasm/wasm.c clang $(CFLAGS) $(wasm_cflags_stdlib) -c -Wl,--export="bmt_sum" -Os -Wl,--export="bmt_init_ptr" -Wl,--export="bmt_hash" -o build/wasm/bmt_o.wasm -c src/bmt.c clang $(CFLAGS) $(wasm_cflags_stdlib) -c -Wl,--export="keccak_hash_btc" -Os -o build/wasm/swarm_o.wasm -c src/swarm.c clang $(CFLAGS) $(wasm_cflags) -c -Wl,--export="to_endian" -Os -o build/wasm/endian_o.wasm src/endian.c clang $(CFLAGS) $(wasm_cflags_stdlib) -c -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -Wl,--export="keccak_hash_heap_init" -Wl,--export="keccak_hash" -o build/wasm/keccak_o.wasm aux/keccak-tiny/keccak-tiny.c + +wasm_libc: wasm_base clang $(CFLAGS) $(wasm_cflags_stdlib) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -Wl,--export="keccak_hash_heap_init" -Wl,--export="keccak_hash_heap_free" -Wl,--export="keccak_hash" -o build/wasm/keccak.wasm build/wasm/keccak_o.wasm build/wasm/wasm_o.wasm $(wasm_libdir)/libc.a clang $(CFLAGS) $(wasm_cflags_stdlib) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -Wl,--export="bmt_hash" -Wl,--export="bmt_hash_free" -Wl,--export="bmt_hash_heap" -o build/wasm/swarm.wasm build/wasm/endian_o.wasm build/wasm/keccak_o.wasm build/wasm/bmt_o.wasm build/wasm/swarm_o.wasm build/wasm/wasm_o.wasm $(wasm_libdir)/libc.a +wasm_native: wasm_base + clang $(CFLAGS) $(wasm_cflags_stdlib) -c -Wl,--export-all -Os -o build/wasm/common_o.wasm src/wasm/common.c + clang $(CFLAGS) $(wasm_cflags_stdlib) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -Wl,--export="keccak_hash_heap_init" -Wl,--export="keccak_hash_heap_free" -Wl,--export="keccak_hash" -o build/wasm/keccak.wasm build/wasm/keccak_o.wasm build/wasm/wasm_o.wasm build/wasm/common_o.wasm + clang $(CFLAGS) $(wasm_cflags_stdlib) -D"memset_s(W,WL,V,OL)=memset(W,V,OL)" -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -Wl,--export="bmt_hash" -Wl,--export="bmt_hash_free" -Wl,--export="bmt_hash_heap" -o build/wasm/swarm.wasm build/wasm/endian_o.wasm build/wasm/keccak_o.wasm build/wasm/bmt_o.wasm build/wasm/swarm_o.wasm build/wasm/wasm_o.wasm build/wasm/common_o.wasm diff --git a/src/wasm/common.c b/src/wasm/common.c @@ -1,9 +1,9 @@ #include <stddef.h> #define MALLOC_PAGE_SIZE 1024 +#define STACK_OFFSET 1024 extern unsigned char __heap_base; -extern void dlog(int p, int v); void *malloc(size_t c) { int i; @@ -14,14 +14,13 @@ void *malloc(size_t c) { unsigned char *ptr; unsigned char *base = (unsigned char*)(&__heap_base); - offset = *((int*)(base)); + offset = *((int*)(base)) + STACK_OFFSET; n = (c / MALLOC_PAGE_SIZE) + 1; n *= MALLOC_PAGE_SIZE; ptr = base + offset; - dlog(77, (int)&c); offset += n; offset_p = (unsigned char*)&offset; for (i = 0; i < 4; i++) { @@ -45,9 +44,12 @@ void *memset(void *p, int v, size_t c) { void *memcpy(void *dst, const void *src, size_t c) { int i ; - dlog(12, c); for (i = 0; i < c; i++) { *((char*)(dst+i)) = *((char*)(src+i)); } return dst; } + +void free(void *ptr) { + return; +} diff --git a/test/check_bmt.c b/test/check_bmt.c @@ -1,7 +1,7 @@ #include <check.h> #include <stdlib.h> -#include "bmt_malloc.h" +#include "bmt.h" #include "hex.h" #include "common.h" @@ -16,7 +16,6 @@ START_TEST(check_bmt_init) { bmt_init(&bmt_context, input, input_length, 3); ck_assert_mem_eq(bmt_context.buf, data_length_bytes, sizeof(long long)); ck_assert_mem_eq(bmt_context.w_ptr, input, 3); - bmt_free(&bmt_context); } END_TEST @@ -34,7 +33,6 @@ START_TEST(check_bmt_sum_foo) { hex2bin(HASH_OF_FOO, v_chk); ck_assert_mem_eq(bmt_context.buf, v_chk, SWARM_WORD_SIZE); - bmt_free(&bmt_context); } END_TEST @@ -83,7 +81,6 @@ START_TEST(check_bmt_sum_vector) { hex2bin(vectors[i], v_chk); ck_assert_mem_eq(bmt_context.buf, v_chk, SWARM_WORD_SIZE); } - bmt_free(&bmt_context); } END_TEST @@ -95,7 +92,7 @@ Suite * common_suite(void) { s = suite_create("bmt"); tc = tcase_create("core"); tcase_add_test(tc, check_bmt_init); - //tcase_add_test(tc, check_bmt_sum_foo); + tcase_add_test(tc, check_bmt_sum_foo); tcase_add_test(tc, check_bmt_sum_vector); suite_add_tcase(s, tc);