From 2d585474c1388f08e66f3fbc1ef8ddad1d47cc44 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Mon, 26 Jul 2021 11:00:37 +0100 Subject: [PATCH] Add riscv target, enhance arm target --- Makefile | 50 ++++++++++++++++++++++-------------------- mongoose.c | 2 +- mongoose.h | 7 ++++++ src/fs.c | 2 +- src/fs.h | 7 ++++++ test/fuzz.c | 4 ++-- test/mongoose_custom.c | 40 +++++++++++++++++++++++++++++++++ test/mongoose_custom.h | 6 +++-- 8 files changed, 88 insertions(+), 30 deletions(-) create mode 100644 test/mongoose_custom.c diff --git a/Makefile b/Makefile index 7283ab3c..d421f201 100644 --- a/Makefile +++ b/Makefile @@ -6,19 +6,15 @@ OPTS ?= -O3 -g3 INCS ?= -Isrc -I. CFLAGS ?= $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) $(EXTRA) SSL ?= MBEDTLS -CDIR ?= $(realpath $(CURDIR)) -VC98 = docker run --rm -e WINEDEBUG=-all -v $(CDIR):$(CDIR) -w $(CDIR) docker.io/mdashnet/vc98 -VC2017 = docker run --rm -e WINEDEBUG=-all -v $(CDIR):$(CDIR) -w $(CDIR) docker.io/mdashnet/vc2017 -MINGW = docker run --rm -v $(CDIR):$(CDIR) -w $(CDIR) docker.io/mdashnet/mingw -GCC = docker run --rm -v $(CDIR):$(CDIR) -w $(CDIR) mdashnet/cc2 -ARM = docker run -v $(CDIR):$(CDIR) -w $(CDIR) mdashnet/armgcc +CWD ?= $(realpath $(CURDIR)) +DOCKER ?= docker run -it --rm -e Tmp=. -e WINEDEBUG=-all -v $(CWD):$(CWD) -w $(CWD) VCFLAGS = /nologo /W3 /O2 /I. $(DEFS) $(TFLAGS) IPV6 ?= 1 ASAN_OPTIONS ?= EXAMPLES := $(wildcard examples/*) EXAMPLE_TARGET ?= example PREFIX ?= /usr/local -SOVERSION = 7.2 +SOVERSION = 7.4 .PHONY: ex test ifeq "$(SSL)" "MBEDTLS" @@ -32,7 +28,7 @@ CFLAGS += -DMG_ENABLE_OPENSSL=1 -I$(OPENSSL)/include LDFLAGS ?= -L$(OPENSSL)/lib -lssl -lcrypto endif -all: mg_prefix test test++ ex vc98 vc2017 mingw mingw++ linux linux++ infer fuzz +all: mg_prefix test test++ arm ex vc98 vc2017 mingw mingw++ linux linux++ fuzz ex: @for X in $(EXAMPLES); do $(MAKE) -C $$X $(EXAMPLE_TARGET) || break; done @@ -53,15 +49,18 @@ test++: test # Make sure we can build from an unamalgamated sources unamalgamated: $(HDRS) Makefile $(CC) src/*.c test/unit_test.c $(CFLAGS) $(LDFLAGS) -g -o unit_test -fuzz: mongoose.c mongoose.h Makefile test/fuzz.c - $(CC) mongoose.c test/fuzz.c $(CFLAGS) -DMG_ENABLE_LINES -DMG_ENABLE_LOG=0 -fsanitize=fuzzer,signed-integer-overflow,address $(LDFLAGS) -g -o fuzzer - $(DEBUGGER) ./fuzzer + +fuzzer: mongoose.c mongoose.h Makefile test/fuzz.c + clang mongoose.c test/fuzz.c $(CFLAGS) -DMG_ENABLE_LINES -DMG_ENABLE_LOG=0 -fsanitize=fuzzer,signed-integer-overflow,address $(LDFLAGS) -g -o $@ + +fuzz: fuzzer + $(RUN) ./fuzzer # make CC=/usr/local/opt/llvm\@8/bin/clang ASAN_OPTIONS=detect_leaks=1 test: CFLAGS += -DMG_ENABLE_IPV6=$(IPV6) -fsanitize=address#,undefined test: mongoose.h Makefile $(SRCS) $(CC) $(SRCS) $(CFLAGS) -coverage $(LDFLAGS) -g -o unit_test - ASAN_OPTIONS=$(ASAN_OPTIONS) $(DEBUGGER) ./unit_test + ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./unit_test coverage: test gcov -l -n *.gcno | sed '/^$$/d' | sed 'N;s/\n/ /' @@ -73,29 +72,35 @@ upload-coverage: coverage infer: infer run -- cc test/unit_test.c -c -W -Wall -Werror -Isrc -I. -O2 -DMG_ENABLE_MBEDTLS=1 -DMG_ENABLE_LINES -I/usr/local/Cellar/mbedtls/2.23.0/include -DMG_ENABLE_IPV6=1 -g -o /dev/null +arm: mongoose.h $(SRCS) + $(DOCKER) mdashnet/armgcc arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb $(SRCS) test/mongoose_custom.c -Itest -DMG_ARCH=MG_ARCH_CUSTOM $(OPTS) $(WARN) $(INCS) -DMG_MAX_HTTP_HEADERS=5 -DMG_ENABLE_LINES -DMG_ENABLE_DIRECTORY_LISTING=0 -DMG_ENABLE_SSI=1 -o unit_test -nostartfiles --specs nosys.specs -e 0 + +riscv: mongoose.h $(SRCS) + $(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) test/mongoose_custom.c -Itest -DMG_ARCH=MG_ARCH_CUSTOM $(OPTS) $(WARN) $(INCS) -DMG_MAX_HTTP_HEADERS=5 -DMG_ENABLE_LINES -DMG_ENABLE_DIRECTORY_LISTING=0 -DMG_ENABLE_SSI=1 -o unit_test + #vc98: VCFLAGS += -DMG_ENABLE_IPV6=1 vc98: Makefile mongoose.c mongoose.h test/unit_test.c - $(VC98) wine cl mongoose.c test/unit_test.c $(VCFLAGS) ws2_32.lib /Fe$@.exe - $(VC98) wine $@.exe + $(DOCKER) mdashnet/vc98 wine cl mongoose.c test/unit_test.c $(VCFLAGS) ws2_32.lib /Fe$@.exe + $(DOCKER) mdashnet/vc98 wine $@.exe #vc2017: VCFLAGS += -DMG_ENABLE_IPV6=1 vc2017: Makefile mongoose.c mongoose.h test/unit_test.c - $(VC2017) wine64 cl mongoose.c test/unit_test.c $(VCFLAGS) ws2_32.lib /Fe$@.exe - $(VC2017) wine64 $@.exe + $(DOCKER) mdashnet/vc2017 wine64 cl mongoose.c test/unit_test.c $(VCFLAGS) ws2_32.lib /Fe$@.exe + $(DOCKER) mdashnet/vc2017 wine64 $@.exe mingw: Makefile mongoose.c mongoose.h test/unit_test.c - $(MINGW) i686-w64-mingw32-gcc mongoose.c test/unit_test.c -W -Wall -Werror -I. $(DEFS) -lwsock32 -o test.exe - $(VC98) wine test.exe + $(DOCKER) mdashnet/mingw i686-w64-mingw32-gcc mongoose.c test/unit_test.c -W -Wall -Werror -I. $(DEFS) -lwsock32 -o test.exe + $(DOCKER) mdashnet/vc98 wine test.exe mingw++: Makefile mongoose.c mongoose.h test/unit_test.c - $(MINGW) i686-w64-mingw32-g++ mongoose.c test/unit_test.c -W -Wall -Werror -I. $(DEFS) -lwsock32 -o test.exe + $(DOCKER) mdashnet/mingw i686-w64-mingw32-g++ mongoose.c test/unit_test.c -W -Wall -Werror -I. $(DEFS) -lwsock32 -o test.exe # Note: for some reason, a binary built with mingw g++, fails to run #linux: CFLAGS += -DMG_ENABLE_IPV6=$(IPV6) linux: CFLAGS += -fsanitize=address,undefined linux: Makefile mongoose.c mongoose.h test/unit_test.c - $(GCC) $(CC) mongoose.c test/unit_test.c $(CFLAGS) $(LDFLAGS) -o unit_test_gcc - $(GCC) ./unit_test_gcc + $(DOCKER) mdashnet/cc2 gcc mongoose.c test/unit_test.c $(CFLAGS) $(LDFLAGS) -o unit_test_gcc + $(DOCKER) mdashnet/cc2 ./unit_test_gcc linux++: CC = g++ linux++: WARN += -Wno-missing-field-initializers @@ -114,9 +119,6 @@ install: linux-libs uninstall: rm -rf $(DESTDIR)$(PREFIX)/lib/libmongoose.a $(DESTDIR)$(PREFIX)/lib/libmongoose.so.$(SOVERSION) $(DESTDIR)$(PREFIX)/include/mongoose.h $(DESTDIR)$(PREFIX)/lib/libmongoose.so -arm: Makefile mongoose.c mongoose.h test/unit_test.c - $(ARM) arm-none-eabi-gcc mongoose.c -c -Itest -DMG_ARCH=MG_ARCH_CUSTOM $(OPTS) $(WARN) $(INCS) -DMG_MAX_HTTP_HEADERS=5 -DMG_ENABLE_LINES -DMG_ENABLE_DIRECTORY_LISTING=0 -DMG_ENABLE_SSI=1 - mongoose.c: Makefile $(wildcard src/*) (cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/private.h src/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@ diff --git a/mongoose.c b/mongoose.c index 182138d9..98805040 100644 --- a/mongoose.c +++ b/mongoose.c @@ -423,7 +423,7 @@ const char *mg_unpack(const char *path, size_t *size) { return NULL; } -#if defined(__linux__) && defined(GCC) +#if defined(MG_FOPENCOOKIE) ssize_t packed_read(void *cookie, char *buf, size_t size) { struct packed_file *fp = (struct packed_file *) cookie; if (size > fp->size - fp->pos) size = fp->size - fp->pos; diff --git a/mongoose.h b/mongoose.h index b5e229a2..2586096e 100644 --- a/mongoose.h +++ b/mongoose.h @@ -608,6 +608,13 @@ void mg_usleep(unsigned long usecs); FILE *mg_fopen_packed(const char *path, const char *mode); +#if defined(__linux__) && defined(GCC) +#define MG_ENABLE_PACKED_FS 1 +#define MG_FOPENCOOKIE +#else +#define MG_ENABLE_PACKED_FS 0 +#endif + unsigned short mg_url_port(const char *url); diff --git a/src/fs.c b/src/fs.c index 39553bdf..cae613dd 100644 --- a/src/fs.c +++ b/src/fs.c @@ -12,7 +12,7 @@ const char *mg_unpack(const char *path, size_t *size) { return NULL; } -#if defined(__linux__) && defined(GCC) +#if defined(MG_FOPENCOOKIE) ssize_t packed_read(void *cookie, char *buf, size_t size) { struct packed_file *fp = (struct packed_file *) cookie; if (size > fp->size - fp->pos) size = fp->size - fp->pos; diff --git a/src/fs.h b/src/fs.h index c062d15c..589cd72d 100644 --- a/src/fs.h +++ b/src/fs.h @@ -3,3 +3,10 @@ #include "arch.h" FILE *mg_fopen_packed(const char *path, const char *mode); + +#if defined(__linux__) && defined(GCC) +#define MG_ENABLE_PACKED_FS 1 +#define MG_FOPENCOOKIE +#else +#define MG_ENABLE_PACKED_FS 0 +#endif diff --git a/test/fuzz.c b/test/fuzz.c index 81b1a959..2eaf1d3f 100644 --- a/test/fuzz.c +++ b/test/fuzz.c @@ -30,9 +30,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { mg_sntp_parse(NULL, 0, &tv); char buf[size * 4 / 3 + 5]; // At least 4 chars and nul termination - mg_base64_decode((char *) data, size, buf); + mg_base64_decode((char *) data, (int) size, buf); mg_base64_decode(NULL, 0, buf); - mg_base64_encode(data, size, buf); + mg_base64_encode(data, (int) size, buf); mg_base64_encode(NULL, 0, buf); char *hexdump = mg_hexdump(data, size); diff --git a/test/mongoose_custom.c b/test/mongoose_custom.c new file mode 100644 index 00000000..eb564d02 --- /dev/null +++ b/test/mongoose_custom.c @@ -0,0 +1,40 @@ +#include "mongoose.h" + +int usleep(useconds_t us) { + for (useconds_t i = 0; i < us * 99; i++) asm("nop"); + return 0; +} + +int clock_gettime(clockid_t clock_id, struct timespec *tp) { + (void) clock_id; + memset(tp, 0, sizeof(*tp)); + return 0; +} + +struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, + mg_event_handler_t fn, void *fn_data) { + (void) mgr, (void) url, (void) fn, (void) fn_data; + return NULL; +} + +void mg_connect_resolved(struct mg_connection *c) { + (void) c; +} + +struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, + mg_event_handler_t fn, void *fn_data) { + (void) mgr, (void) url, (void) fn, (void) fn_data; + return NULL; +} + +void mg_mgr_poll(struct mg_mgr *mgr, int ms) { + (void) mgr, (void) ms; +} + +bool mg_send(struct mg_connection *c, const void *buf, size_t len) { + (void) c, (void) buf, (void) len; + return 0; +} + +void _fini(void) { +} diff --git a/test/mongoose_custom.h b/test/mongoose_custom.h index 282fae0f..97ddfecd 100644 --- a/test/mongoose_custom.h +++ b/test/mongoose_custom.h @@ -13,11 +13,13 @@ #include #include -int clock_gettime(clockid_t clock_id, struct timespec *tp); - #define MG_DIRSEP '/' #define MG_INT64_FMT "%lld" #undef MG_PATH_MAX #define MG_PATH_MAX 100 #undef MG_ENABLE_SOCKET #define MG_ENABLE_SOCKET 0 + +#define realpath(a, b) (a) + +int clock_gettime(clockid_t clock_id, struct timespec *tp);