diff --git a/.gitignore b/.gitignore index 3cc7f580..7164c083 100644 --- a/.gitignore +++ b/.gitignore @@ -78,11 +78,12 @@ cmake-build-debug # mpeltonen/sbt-idea plugin .idea_modules/ -# JIRA plugin -atlassian-ide-plugin.xml - # Crashlytics plugin (for Android Studio and IntelliJ) com_crashlytics_export_strings.xml crashlytics.properties crashlytics-build.properties -fabric.properties \ No newline at end of file +fabric.properties + +# Wasm files +*.wasm +*.wat diff --git a/Dockerfile b/Dockerfile index d2508945..cd593bfc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,12 @@ -FROM ubuntu:19.04 +FROM ubuntu:18.04 RUN apt-get update \ && apt-get install -y ca-certificates \ curl \ git \ - make \ - libtinfo5 + make -RUN curl -L https://github.com/CraneStation/wasi-sdk/releases/download/wasi-sdk-6/wasi-sdk-6.0-linux.tar.gz | tar xz --strip-components=1 -C / +RUN curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-10/wasi-sdk-10.0-linux.tar.gz | tar xz --strip-components=1 -C / VOLUME /code WORKDIR /code diff --git a/docker-compose.yml b/docker-compose.yml index c03188aa..5bd90918 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,5 +3,6 @@ services: redis: build: context: . + container_name: redis_builder volumes: - .:/code diff --git a/src/Makefile_wasm b/src/Makefile_wasm index 312eaaba..ef8802a2 100644 --- a/src/Makefile_wasm +++ b/src/Makefile_wasm @@ -1,24 +1,87 @@ TARGET = redis -CC = /opt/wasi-sdk/bin/clang -SYSROOT = /opt/wasi-sdk/share/wasi-sysroot -TARGET_TRIPLE = wasm32-unknown-wasi +CC = /bin/clang +SYSROOT = /share/wasi-sysroot +TARGET_TRIPLE = wasm32-wasi CFLAGS = -nostartfiles -fvisibility=hidden LDFLAGS = -Wl,--no-entry,--demangle,--allow-undefined -EXPORT_FUNCS = --export=allocate,--export=deallocate,--export=invoke,--export=load,--export=store,--export=redis_allocate,--export=redis_deallocate,--export=redis_invoke,--export=redis_load,--export=redis_store WRAPPER_SRC = wrapper.c -LUA =/code/deps/lua/src -REDIS_SERVER = adlist.c quicklist.c debug.c dict.c server.c sds.c zmalloc.c lzf_c.c lzf_d.c pqsort.c zipmap.c sha1.c ziplist.c release.c networking.c util.c object.c db.c t_string.c t_list.c t_set.c t_zset.c t_hash.c multi.c sort.c intset.c crc16.c endianconv.c slowlog.c scripting.c rand.c crc64.c bitops.c notify.c hyperloglog.c latency.c sparkline.c geo.c evict.c expire.c geohash.c geohash_helper.c defrag.c siphash.c rax.c t_stream.c listpack.c lolwut.c lolwut5.c ../deps/lua/src/*.c -SDK = sdk/logger.c +LUA = /code/deps/lua/src +SDK = sdk/syscalls_stubs.c + +EXPORT_FUNCS = \ + --export=allocate,$\ + --export=deallocate,$\ + --export=invoke,$\ + --export=load,$\ + --export=store,$\ + --export=redis_allocate,$\ + --export=redis_deallocate,$\ + --export=redis_invoke,$\ + --export=redis_load,$\ + --export=redis_store + +REDIS_SERVER_SRC = \ + adlist.c\ + quicklist.c\ + debug.c\ + dict.c\ + server.c\ + sds.c\ + zmalloc.c\ + lzf_c.c\ + lzf_d.c\ + pqsort.c\ + zipmap.c\ + sha1.c\ + ziplist.c\ + release.c\ + networking.c\ + util.c\ + object.c\ + db.c\ + t_string.c\ + t_list.c\ + t_set.c\ + t_zset.c\ + t_hash.c\ + multi.c\ + sort.c\ + intset.c\ + crc16.c\ + endianconv.c\ + slowlog.c\ + scripting.c\ + rand.c\ + crc64.c\ + bitops.c\ + notify.c\ + hyperloglog.c\ + latency.c\ + sparkline.c\ + geo.c\ + evict.c\ + expire.c\ + geohash.c\ + geohash_helper.c\ + defrag.c\ + siphash.c\ + rax.c\ + t_stream.c\ + listpack.c\ + lolwut.c\ + lolwut5.c\ + ../deps/lua/src/*.c .PHONY: default all clean default: $(TARGET) all: default -$(TARGET): $(SDK) $(REDIS_SERVER) $(WRAPPER_SRC) +$(TARGET): $(SDK) $(REDIS_SERVER_SRC) $(WRAPPER_SRC) + ./mkreleasehdr.sh $(CC) -O3 --sysroot=$(SYSROOT) --target=$(TARGET_TRIPLE) -I$(LUA) $(CFLAGS) $(LDFLAGS) -Wl,$(EXPORT_FUNCS) $^ -o $@.wasm .PRECIOUS: $(TARGET) clean: - -rm -f $(TARGET).wasm \ No newline at end of file + -rm -f $(TARGET).wasm $(TARGET).wat diff --git a/src/sdk/allocator.c b/src/sdk/allocator.c deleted file mode 100644 index 15e49ff5..00000000 --- a/src/sdk/allocator.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "allocator.h" -#include - -#define UNUSED(x) (void)(x) - -void *allocate(size_t size) { - return malloc(size); -} - -void deallocate(void *ptr, size_t size) { - UNUSED(size); - free(ptr); -} diff --git a/src/sdk/allocator.h b/src/sdk/allocator.h deleted file mode 100644 index ded2bebc..00000000 --- a/src/sdk/allocator.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef C_SDK_ALLOCATOR_H -#define C_SDK_ALLOCATOR_H - -#include // for size_t - -/** - * Allocates a memory region of given size. - * - * Used by Wasm VM for byte array passing. Should be exported from module. - * - * @param size a size of needed memory region. - * @return a pointer to allocated memory region. - */ -void *allocate(size_t size); - -/** - * Frees a memory region. - * - * Used by Wasm VM for freeing previous memory allocated by `allocate` function. - * Should be exported from module. - * - * @param ptr the pointer to the previously allocated memory region. - * @param size the size of the previously allocated memory region. - */ -void deallocate(void *ptr, size_t size); - -#endif //C_SDK_ALLOCATOR_H diff --git a/src/sdk/logger.c b/src/sdk/logger.c deleted file mode 100644 index 7a6f8811..00000000 --- a/src/sdk/logger.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "logger.h" - -#define __LOGGER_IMPORT(name) \ - __attribute__((__import_module__("logger"), __import_name__(#name))) - -void __write(char ch) __LOGGER_IMPORT(write); -void __flush() __LOGGER_IMPORT(flush); - -void wasm_log(const char *str, int len) { - for(int byteId = 0; byteId < len; ++byteId) { - __write(str[byteId]); - } - - __flush(); -} diff --git a/src/sdk/logger.h b/src/sdk/logger.h index f00f8355..0b93d396 100644 --- a/src/sdk/logger.h +++ b/src/sdk/logger.h @@ -1,11 +1,15 @@ -#ifndef C_SDK_LOGGER_H -#define C_SDK_LOGGER_H +#ifndef FLUENCE_C_SDK_LOGGER_H +#define FLUENCE_C_SDK_LOGGER_H + +#define __LOGGER_IMPORT(name) \ + __attribute__((__import_module__("logger"), __import_name__(#name))) /** - * Writes provided string to Wasm VM logger. + * Writes provided utf8 string to Wasm VM logger. * - * @param log_message a message that should be logged. + * @param str a pointer to the string that should be logged. + * @param len a size of the string that should be logged. */ -void wasm_log(const char *str, int len); +void log_utf8_string(const char *str, int len) __LOGGER_IMPORT(log_utf8_string); -#endif //C_SDK_LOGGER_H +#endif // FLUENCE_C_SDK_LOGGER_H diff --git a/src/sdk/syscalls_stubs.c b/src/sdk/syscalls_stubs.c new file mode 100644 index 00000000..3676aa06 --- /dev/null +++ b/src/sdk/syscalls_stubs.c @@ -0,0 +1,23 @@ +#include + +/// Some syscalls could be as the result of importing files like stdio.h that in its turn imports standart I/O +/// descriptors like stdin/stderr/stdout. Each of them is a structure contains some callbacks (e.g. +/// https://github.com/CraneStation/wasi-libc/blob/9bb4cc5c411af8453cbef69c137a4c2337714d89/libc-top-half/musl/src/stdio/stdin.c#L11) +/// These stubs could provide default implementations for syscalls. And please include this file, if you are sure +/// that these syscalls couldn't be used directly. + +size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) { + return 1; +} + +int __stdio_close(FILE *f) { + return 1; +} + +off_t __stdio_seek(FILE *_f, off_t _offset, int _value) { + return 1; +} + +size_t __stdout_write(FILE *f, const unsigned char *buf, size_t len) { + return 1; +} diff --git a/src/server.c b/src/server.c index 9e095c6f..dc4f278b 100644 --- a/src/server.c +++ b/src/server.c @@ -411,7 +411,7 @@ void serverLogRaw(int level, const char *msg) { if (!log_to_stdout) fclose(fp); if (server.syslog_enabled) syslog(syslogLevelMap[level], "%s", msg); #else - wasm_log(msg, strlen(msg)); + log_utf8_string(msg, strlen(msg)); // increment time ustime();