diff --git a/Makefile b/Makefile index 7d69c475..39bdfc04 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ SRCS = mongoose.c test/unit_test.c test/packed_fs.c HDRS = $(wildcard src/*.h) $(wildcard mip/*.h) -DEFS ?= -DMG_MAX_HTTP_HEADERS=7 -DMG_ENABLE_LINES -DMG_ENABLE_PACKED_FS=1 -DMG_ENABLE_SSI=1 +PACKED ?= 1 +DEFS ?= -DMG_MAX_HTTP_HEADERS=7 -DMG_ENABLE_LINES -DMG_ENABLE_PACKED_FS=$(PACKED) -DMG_ENABLE_SSI=1 C_WARN ?= -Wmissing-prototypes -Wstrict-prototypes WARN ?= -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion -fno-common -Wconversion -Wundef $(C_WARN) OPTS ?= -O3 -g3 -VALGRIND_OPTS ?= -O0 -g3 INCS ?= -Isrc -I. SSL ?= MBEDTLS CWD ?= $(realpath $(CURDIR)) @@ -19,7 +19,7 @@ PREFIX ?= /usr/local VERSION ?= $(shell cut -d'"' -f2 src/version.h) COMMON_CFLAGS ?= $(WARN) $(INCS) $(DEFS) -DMG_ENABLE_IPV6=$(IPV6) $(TFLAGS) CFLAGS ?= $(OPTS) $(ASAN) $(COMMON_CFLAGS) -VALGRIND_CFLAGS ?= $(VALGRIND_OPTS) $(COMMON_CFLAGS) +VALGRIND_CFLAGS ?= $(OPTS) $(COMMON_CFLAGS) VALGRIND_RUN ?= valgrind --tool=memcheck --gen-suppressions=all --leak-check=full --show-leak-kinds=all --leak-resolution=high --track-origins=yes --error-exitcode=1 --exit-on-first-error=yes .PHONY: examples test valgrind @@ -28,6 +28,7 @@ MBEDTLS ?= /usr/local CFLAGS += -DMG_ENABLE_MBEDTLS=1 -I$(MBEDTLS)/include -I/usr/include LDFLAGS ?= -L$(MBEDTLS)/lib -lmbedtls -lmbedcrypto -lmbedx509 endif + ifeq "$(SSL)" "OPENSSL" OPENSSL ?= /usr/local CFLAGS += -DMG_ENABLE_OPENSSL=1 -I$(OPENSSL)/include @@ -36,6 +37,12 @@ endif all: mg_prefix unamalgamated unpacked test test++ arm examples vc98 vc17 vc22 mingw mingw++ linux linux++ fuzz +mip_test: PACKED=0 +mip_test: DEFS += -DMG_ENABLE_SOCKET=0 -DMG_ENABLE_MIP=1 +mip_test: test/mip_test.c mongoose.c mongoose.h Makefile + $(CC) test/mip_test.c $(CFLAGS) $(LDFLAGS) -g -o $@ + ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@ + examples: @for X in $(EXAMPLES); do test -f $$X/Makefile || continue; $(MAKE) -C $$X example || exit 1; done @@ -43,8 +50,8 @@ test/packed_fs.c: Makefile src/ssi.h test/fuzz.c test/data/a.txt $(CC) $(CFLAGS) test/pack.c -o pack $(RUN) ./pack Makefile src/ssi.h test/fuzz.c test/data/a.txt test/data/range.txt > $@ -DIR ?= test/data/ -OUT ?= fs_packed.c +DIR ?= test/data +OUT ?= packed_fs.c mkfs: $(CC) $(CFLAGS) test/pack.c -o pack $(RUN) ./pack -s $(DIR) `find $(DIR) -type f` > $(OUT) @@ -99,9 +106,6 @@ valgrind_unit_test: Makefile mongoose.h $(SRCS) valgrind: valgrind_unit_test $(VALGRIND_RUN) ./valgrind_unit_test -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: DEFS += -DMG_ENABLE_FILE=0 -DMG_ENABLE_MIP=1 -DMG_ARCH=MG_ARCH_NEWLIB arm: mongoose.h $(SRCS) $(DOCKER) mdashnet/armgcc arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test -nostartfiles --specs nosys.specs -e 0 diff --git a/mip/mip.c b/mip/mip.c index 0483c7b9..2fefc28e 100644 --- a/mip/mip.c +++ b/mip/mip.c @@ -161,15 +161,15 @@ struct pkt { static void q_copyin(struct queue *q, const uint8_t *buf, size_t len, size_t head) { - size_t i = 0, left = q->len - head; - for (; i < len && i < left; i++) q->buf[head + i] = buf[i]; - for (; i < len; i++) q->buf[i - left] = buf[i]; + size_t left = q->len - head; + memcpy(&q->buf[head], buf, left < len ? left : len); + if (left < len) memcpy(q->buf, &buf[left], len - left); } static void q_copyout(struct queue *q, uint8_t *buf, size_t len, size_t tail) { - size_t i = 0, left = q->len - tail; - for (; i < len && i < left; i++) buf[i] = q->buf[tail + i]; - for (; i < len; i++) buf[i] = q->buf[i - left]; + size_t left = q->len - tail; + memcpy(buf, &q->buf[tail], left < len ? left : len); + if (left < len) memcpy(&buf[left], q->buf, len - left); } static bool q_write(struct queue *q, const void *buf, size_t len) { diff --git a/mongoose.c b/mongoose.c index 94a95e02..cbe0bc80 100644 --- a/mongoose.c +++ b/mongoose.c @@ -5064,6 +5064,8 @@ void mg_tls_free(struct mg_connection *c) { bool mg_sock_would_block(void); bool mg_sock_conn_reset(void); +#if MG_ENABLE_MIP +#else static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { struct mg_connection *c = (struct mg_connection *) ctx; int fd = (int) (size_t) c->fd; @@ -5089,11 +5091,15 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) { } return n; } +#endif void mg_tls_handshake(struct mg_connection *c) { struct mg_tls *tls = (struct mg_tls *) c->tls; int rc; +#if MG_ENABLE_MIP +#else mbedtls_ssl_set_bio(&tls->ssl, c, mg_net_send, mg_net_recv, 0); +#endif rc = mbedtls_ssl_handshake(&tls->ssl); if (rc == 0) { // Success MG_DEBUG(("%lu success", c->id)); @@ -6444,15 +6450,15 @@ struct pkt { static void q_copyin(struct queue *q, const uint8_t *buf, size_t len, size_t head) { - size_t i = 0, left = q->len - head; - for (; i < len && i < left; i++) q->buf[head + i] = buf[i]; - for (; i < len; i++) q->buf[i - left] = buf[i]; + size_t left = q->len - head; + memcpy(&q->buf[head], buf, left < len ? left : len); + if (left < len) memcpy(q->buf, &buf[left], len - left); } static void q_copyout(struct queue *q, uint8_t *buf, size_t len, size_t tail) { - size_t i = 0, left = q->len - tail; - for (; i < len && i < left; i++) buf[i] = q->buf[tail + i]; - for (; i < len; i++) buf[i] = q->buf[i - left]; + size_t left = q->len - tail; + memcpy(buf, &q->buf[tail], left < len ? left : len); + if (left < len) memcpy(&buf[left], q->buf, len - left); } static bool q_write(struct queue *q, const void *buf, size_t len) { diff --git a/src/tls_mbed.c b/src/tls_mbed.c index cc77385f..2d8c8219 100644 --- a/src/tls_mbed.c +++ b/src/tls_mbed.c @@ -27,6 +27,8 @@ void mg_tls_free(struct mg_connection *c) { bool mg_sock_would_block(void); bool mg_sock_conn_reset(void); +#if MG_ENABLE_MIP +#else static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { struct mg_connection *c = (struct mg_connection *) ctx; int fd = (int) (size_t) c->fd; @@ -52,11 +54,15 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) { } return n; } +#endif void mg_tls_handshake(struct mg_connection *c) { struct mg_tls *tls = (struct mg_tls *) c->tls; int rc; +#if MG_ENABLE_MIP +#else mbedtls_ssl_set_bio(&tls->ssl, c, mg_net_send, mg_net_recv, 0); +#endif rc = mbedtls_ssl_handshake(&tls->ssl); if (rc == 0) { // Success MG_DEBUG(("%lu success", c->id)); diff --git a/test/mip_test.c b/test/mip_test.c new file mode 100644 index 00000000..2de65eb3 --- /dev/null +++ b/test/mip_test.c @@ -0,0 +1,33 @@ +#include +#include "mongoose.c" + +static void test_queue(void) { + uint8_t buf[sizeof(size_t) + 5]; + uint16_t val = 1234; + struct queue q = {buf, sizeof(buf), 0, 0}; + + // Write to an empty queue, and read back + assert(q_avail(&q) == 0); + assert(q_write(&q, &val, sizeof(val)) == true); + assert(q_avail(&q) == sizeof(val)); + assert(q.head > q.tail); + val = 0; + assert(q_read(&q, &val) == sizeof(val)); + assert(val == 1234); + assert(q_avail(&q) == 0); + + // Second write - wrap over the buffer boundary + assert(q_write(&q, &val, sizeof(val)) == true); + assert(q_avail(&q) == sizeof(val)); + assert(q.head < q.tail); + val = 0; + assert(q_read(&q, &val) == sizeof(val)); + assert(val == 1234); + assert(q_avail(&q) == 0); +} + +int main(void) { + test_queue(); + printf("SUCCESS\n"); + return 0; +}