diff --git a/components/lwip/port/esp32/freertos/sys_arch.c b/components/lwip/port/esp32/freertos/sys_arch.c index c2b44b4a11..867e414d29 100644 --- a/components/lwip/port/esp32/freertos/sys_arch.c +++ b/components/lwip/port/esp32/freertos/sys_arch.c @@ -37,10 +37,15 @@ #include "lwip/def.h" #include "lwip/sys.h" #include "lwip/mem.h" -#include "arch/sys_arch.h" #include "lwip/stats.h" +#include "arch/sys_arch.h" +#include "arch/vfs_lwip.h" +#ifdef __linux__ +#include "esp32_mock.h" +#else // __linux__ #include "esp_log.h" #include "esp_compiler.h" +#endif // __linux__ static const char* TAG = "lwip_arch"; diff --git a/components/lwip/port/esp32/include/arch/sys_arch.h b/components/lwip/port/esp32/include/arch/sys_arch.h index 2c5c89961e..b4fb53a834 100644 --- a/components/lwip/port/esp32/include/arch/sys_arch.h +++ b/components/lwip/port/esp32/include/arch/sys_arch.h @@ -33,11 +33,14 @@ #ifndef __SYS_ARCH_H__ #define __SYS_ARCH_H__ +#ifdef __linux__ +#include "esp32_mock.h" +#else #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/queue.h" #include "freertos/semphr.h" -#include "arch/vfs_lwip.h" +#endif // __linux__ #ifdef __cplusplus extern "C" { diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index 48a13b2a60..af45cffecb 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -8,17 +8,22 @@ #ifndef LWIP_HDR_ESP_LWIPOPTS_H #define LWIP_HDR_ESP_LWIPOPTS_H -#include +#include "sdkconfig.h" +#include // For memcpy +#include // For malloc/free #include #include #include #include #include -#include "esp_task.h" -#include "esp_random.h" -#include "sdkconfig.h" #include "netif/dhcp_state.h" #include "sntp/sntp_get_set_time.h" +#ifdef __linux__ +#include "esp32_mock.h" +#else +#include "esp_task.h" +#include "esp_random.h" +#endif // __linux__ /* diff --git a/components/lwip/test_afl_host/Makefile b/components/lwip/test_afl_host/Makefile index baf179a7e1..3dfac00899 100644 --- a/components/lwip/test_afl_host/Makefile +++ b/components/lwip/test_afl_host/Makefile @@ -1,35 +1,72 @@ -COMPONENTS_DIR=../.. -COMPILER_ICLUDE_DIR=$(shell echo `which xtensa-esp32-elf-gcc | xargs dirname | xargs dirname`/xtensa-esp32-elf) -CFLAGS=-std=gnu99 -Og -ggdb -ffunction-sections -fdata-sections -nostdlib -Wall -Werror=all -Wno-int-to-pointer-cast -Wno-error=unused-function -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-macro-redefined -Wno-constant-conversion -Wno-incompatible-pointer-types-discards-qualifiers -Wno-typedef-redefinition -Wno-incompatible-pointer-types -Wextra \ --Wno-unused-parameter -Wno-sign-compare -Wno-address -Wno-unused-variable -DESP_PLATFORM -D IDF_VER=\"v3.1\" -MMD -MP -DWITH_POSIX -DLWIP_NO_CTYPE_H=1 -INC_DIRS=-I . -I ./build/config -I $(COMPONENTS_DIR)/newlib/platform_include -I $(COMPONENTS_DIR)/newlib/include -I $(COMPONENTS_DIR)/driver/include -I $(COMPONENTS_DIR)/esp32/include -I $(COMPONENTS_DIR)/ethernet/include -I $(COMPONENTS_DIR)/freertos/esp_additions/include -I $(COMPONENTS_DIR)/freertos/esp_additions/include/freertos -I $(COMPONENTS_DIR)/freertos/FreeRTOS-Kernel/include -I $(COMPONENTS_DIR)/heap/include -I $(COMPONENTS_DIR)/lwip/lwip/src/include -I $(COMPONENTS_DIR)/lwip/include/apps -I $(COMPONENTS_DIR)/lwip/lwip/src/include/netif -I $(COMPONENTS_DIR)/lwip/lwip/src/include/posix -I $(COMPONENTS_DIR)/lwip/port/esp32/include -I $(COMPONENTS_DIR)/lwip/lwip/src/include/posix -I $(COMPONENTS_DIR)/lwip/include/apps/ping -I $(COMPONENTS_DIR)/lwip/include/apps/sntp -I $(COMPONENTS_DIR)/soc/esp32/include -I $(COMPONENTS_DIR)/soc/include -I $(COMPONENTS_DIR)/esp_rom/include -I $(COMPONENTS_DIR)/esp_common/include -I $(COMPONENTS_DIR)/esp_hw_support/include -I $(COMPONENTS_DIR)/xtensa/include -I $(COMPONENTS_DIR)/xtensa/esp32/include -I $(COMPONENTS_DIR)/esp_wifi/include -I $(COMPONENTS_DIR)/esp_event/include -I $(COMPONENTS_DIR)/freertos/FreeRTOS-Kernel/portable/xtensa/include -I $(COMPONENTS_DIR)/esp_system/include -I $(COMPONENTS_DIR)/esp_timer/include -I $(COMPONENTS_DIR)/soc/include -I $(COMPONENTS_DIR)/soc/include -I $(COMPONENTS_DIR)/soc/src/esp32/include -I $(COMPONENTS_DIR)/soc/esp32/include -I $(COMPONENTS_DIR)/esp_netif/include -I $(COMPONENTS_DIR)/esp_eth/include -I $(COMPONENTS_DIR)/esp_netif/lwip -I $(COMPONENTS_DIR)/hal/include -I $(COMPONENTS_DIR)/hal/esp32/include -I $(COMPILER_ICLUDE_DIR)/include -I $(COMPONENTS_DIR)/bootloader_support/include +LWIP_COMPONENT_DIR=../ + +CFLAGS=-D IDF_VER=\"v3.1\" \ + -DESP_PLATFORM \ + -DLWIP_NO_CTYPE_H=1 \ + -DWITH_POSIX \ + -fdata-sections \ + -ffunction-sections \ + -ggdb \ + -MMD \ + -MP \ + -nostdlib \ + -Og \ + -std=gnu99 \ + -Wall \ + -Werror=all \ + -Wextra \ + -Wno-address \ + -Wno-constant-conversion \ + -Wno-error=deprecated-declarations \ + -Wno-error=unused-function \ + -Wno-error=unused-variable \ + -Wno-incompatible-pointer-types \ + -Wno-incompatible-pointer-types-discards-qualifiers \ + -Wno-int-to-pointer-cast \ + -Wno-macro-redefined \ + -Wno-sign-compare \ + -Wno-typedef-redefinition \ + -Wno-unused-parameter \ + -Wno-unused-variable + +INC_DIRS=-I . \ + -I ./build/config \ + -I $(LWIP_COMPONENT_DIR)/include/apps \ + -I $(LWIP_COMPONENT_DIR)/include/apps/ping \ + -I $(LWIP_COMPONENT_DIR)/include/apps/sntp \ + -I $(LWIP_COMPONENT_DIR)/lwip/src/include \ + -I $(LWIP_COMPONENT_DIR)/lwip/src/include/netif \ + -I $(LWIP_COMPONENT_DIR)/lwip/src/include/posix \ + -I $(LWIP_COMPONENT_DIR)/lwip/src/include/posix \ + -I $(LWIP_COMPONENT_DIR)/port/esp32/include + TEST_NAME=test FUZZ=afl-fuzz GEN_CFG=generate_config LD=$(CC) ifeq ($(MODE),dhcp_client) - DEPENDENCY_INJECTION=-include dhcp_di.h - OBJECTS=dhcp.o def.o network_mock.o test_dhcp_client.o - SAMPLE_PACKETS=in_dhcp_client + DEPENDENCY_INJECTION=-include dhcp_di.h + OBJECTS=dhcp.o def.o esp32_mock.o test_dhcp_client.o + SAMPLE_PACKETS=in_dhcp_client else ifeq ($(MODE),dhcp_server) - DEPENDENCY_INJECTION=-include dhcpserver_di.h - OBJECTS=dhcpserver.o def.o test_dhcp_server.o network_mock.o esp_netif_loopback_mock.o - SAMPLE_PACKETS=in_dhcp_server + DEPENDENCY_INJECTION=-include dhcpserver_di.h + OBJECTS=dhcpserver.o def.o esp32_mock.o test_dhcp_server.o + SAMPLE_PACKETS=in_dhcp_server else ifeq ($(MODE),dns) - CFLAGS+=-DNOT_MOCK_DNS - DEPENDENCY_INJECTION=-include dns_di.h - OBJECTS=dns.o def.o test_dns.o network_mock.o - SAMPLE_PACKETS=in_dns + CFLAGS+=-DNOT_MOCK_DNS + DEPENDENCY_INJECTION=-include dns_di.h + OBJECTS=dns.o def.o esp32_mock.o test_dns.o + SAMPLE_PACKETS=in_dns else - $(error Please specify MODE: dhcp_server, dhcp_client, dns) + $(error Please specify MODE: dhcp_server, dhcp_client, dns) endif ifeq ($(INSTR),off) - CC=gcc - CFLAGS+=-DINSTR_IS_OFF - TEST_NAME=test_sim + CC=gcc + CFLAGS+=-DINSTR_IS_OFF + TEST_NAME=test_sim else - CC=afl-clang-fast + CC=afl-clang-fast endif CFLAGS+=$(INC_DIRS) diff --git a/components/lwip/test_afl_host/network_mock.c b/components/lwip/test_afl_host/esp32_mock.c similarity index 75% rename from components/lwip/test_afl_host/network_mock.c rename to components/lwip/test_afl_host/esp32_mock.c index 7b77197c92..9a7c38a5cd 100644 --- a/components/lwip/test_afl_host/network_mock.c +++ b/components/lwip/test_afl_host/esp32_mock.c @@ -1,86 +1,96 @@ -#include "no_warn_host.h" +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include +#include +#ifndef NOT_MOCK_DNS +#include "lwip/dns.h" +#endif +#include "lwip/etharp.h" +#include "lwip/mem.h" +#include "lwip/netif.h" #include "lwip/opt.h" #include "lwip/pbuf.h" -#include "lwip/udp.h" -#include "esp_netif.h" #include "lwip/timeouts.h" -#include +#include "lwip/udp.h" +#include "lwip/timeouts.h" +#include "esp32_mock.h" +#include "no_warn_host.h" +#define ESP_OK 0 + +/* ---------------------------------------------------- Variables ------------------------------------------------------ + * + * ------------------------------------------------------------------------------------------------------------------ */ + +// -------------------- LWIP Globals ----------------------- + +// ip_addr.h const ip_addr_t ip_addr_any; const ip_addr_t ip_addr_broadcast; const ip_addr_t ip_addr_any_type; +// ip.h struct ip_globals ip_data; +// netif.h struct netif *netif_list; -struct udp_pcb mock_pcb; + +// ---------------- AFL Host Test Globals ------------------ + +// dns_di.h uint32_t g_random_numbers[8] = {0}; uint32_t g_random_numbers_cnt = 0; +// ----------------------- Locals -------------------------- -struct pbuf* pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset) +struct udp_pcb mock_pcb; + +/* ---------------------------------------------------- LWIP Mock ------------------------------------------------------ + * + * ------------------------------------------------------------------------------------------------------------------ */ + +// --------------------- lwip/dns.h ------------------------ + +#ifndef NOT_MOCK_DNS +void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver) { - u16_t offset_left = in_offset; - struct pbuf* q = in; +} +#endif - /* get the correct pbuf */ - while ((q != NULL) && (q->len <= offset_left)) { - offset_left -= q->len; - q = q->next; - } - if (out_offset != NULL) { - *out_offset = offset_left; - } - return q; +// -------------------- lwip/etharp.h ---------------------- + +err_t etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q) +{ + return ESP_OK; } -int pbuf_try_get_at(const struct pbuf* p, u16_t offset) -{ - u16_t q_idx; - struct pbuf* q = pbuf_skip(p, offset, &q_idx); +// --------------------- lwip/mem.h ------------------------ - /* return requested data if pbuf is OK */ - if ((q != NULL) && (q->len > q_idx)) { - return ((u8_t*)q->payload)[q_idx]; - } - return -1; +void * mem_malloc(mem_size_t size) +{ + return malloc(size); } -void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data) +void * mem_calloc(size_t nr, mem_size_t size) { - u16_t q_idx; - struct pbuf* q = pbuf_skip(p, offset, &q_idx); - - /* write requested data if pbuf is OK */ - if ((q != NULL) && (q->len > q_idx)) { - ((u8_t*)q->payload)[q_idx] = data; - } + return calloc(nr, size); } -u8_t pbuf_get_at(const struct pbuf* p, u16_t offset) +void mem_free(void *rmem) { - u16_t q_idx; - struct pbuf* q = pbuf_skip(p, offset, &q_idx); - - /* return requested data if pbuf is OK */ - if ((q != NULL) && (q->len > q_idx)) { - return ((u8_t*)q->payload)[q_idx]; - } - return 0; + free(rmem); } -err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len) +// -------------------- lwip/netif.h ----------------------- + +void netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, + const ip4_addr_t *gw) { - return ERR_OK; } -err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset) -{ - return ERR_OK; -} - -struct udp_pcb * udp_new_ip_type(u8_t type) -{ - return &mock_pcb; -} +// --------------------- lwip/pbuf.h ----------------------- struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type) { @@ -94,6 +104,18 @@ struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type) return p; } +void pbuf_realloc(struct pbuf *p, u16_t size) +{ + if (p != NULL) + { + uint8_t *buf = malloc(size); + free(p->payload); + p->payload = buf; + p->len = size; + p->tot_len = size; + } +} + u8_t pbuf_free(struct pbuf *p) { if (p) { @@ -107,71 +129,6 @@ u8_t pbuf_free(struct pbuf *p) return 1; } -err_t udp_sendto(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port) -{ - return ESP_OK; -} - -void udp_remove(struct udp_pcb *pcb) -{ - if (pcb == NULL) - { - free(pcb); - } -} - -struct udp_pcb *udp_new(void) -{ - return malloc(sizeof(struct udp_pcb)); -} - -err_t udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) -{ - return ESP_OK; -} - -void udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg) -{ -} - -void udp_disconnect(struct udp_pcb *pcb) -{ -} - -#ifndef NOT_MOCK_DNS -void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver) -{ -} -#endif - -uint32_t esp_random(void) -{ - // Preparation for injecting favorable random numbers - return g_random_numbers[g_random_numbers_cnt++ % 8]; -} - -err_t etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q) -{ - return ESP_OK; -} - -void netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, - const ip4_addr_t *gw) -{ -} - -void pbuf_realloc(struct pbuf *p, u16_t size) -{ - if (p != NULL) - { - uint8_t *buf = malloc(size); - free(p->payload); - p->payload = buf; - p->len = size; - p->tot_len = size; - } -} - u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset) { struct pbuf *p; @@ -209,11 +166,120 @@ u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t return copied_total; } +err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len) +{ + return ERR_OK; +} + +err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset) +{ + return ERR_OK; +} + +struct pbuf* pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset) +{ + u16_t offset_left = in_offset; + struct pbuf* q = in; + + /* get the correct pbuf */ + while ((q != NULL) && (q->len <= offset_left)) { + offset_left -= q->len; + q = q->next; + } + if (out_offset != NULL) { + *out_offset = offset_left; + } + return q; +} + +u8_t pbuf_get_at(const struct pbuf* p, u16_t offset) +{ + u16_t q_idx; + struct pbuf* q = pbuf_skip(p, offset, &q_idx); + + /* return requested data if pbuf is OK */ + if ((q != NULL) && (q->len > q_idx)) { + return ((u8_t*)q->payload)[q_idx]; + } + return 0; +} + +int pbuf_try_get_at(const struct pbuf* p, u16_t offset) +{ + u16_t q_idx; + struct pbuf* q = pbuf_skip(p, offset, &q_idx); + + /* return requested data if pbuf is OK */ + if ((q != NULL) && (q->len > q_idx)) { + return ((u8_t*)q->payload)[q_idx]; + } + return -1; +} + +void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data) +{ + u16_t q_idx; + struct pbuf* q = pbuf_skip(p, offset, &q_idx); + + /* write requested data if pbuf is OK */ + if ((q != NULL) && (q->len > q_idx)) { + ((u8_t*)q->payload)[q_idx] = data; + } +} + +// ------------------- lwip/timeouts.h --------------------- + +void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg) +{ +} + +void sys_untimeout(sys_timeout_handler handler, void *arg) +{ +} + +// --------------------- lwip/udp.h ------------------------ + +struct udp_pcb *udp_new(void) +{ + return malloc(sizeof(struct udp_pcb)); +} + +struct udp_pcb * udp_new_ip_type(u8_t type) +{ + return &mock_pcb; +} + +void udp_remove(struct udp_pcb *pcb) +{ + if (pcb == NULL) + { + free(pcb); + } +} + +err_t udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) +{ + return ESP_OK; +} + err_t udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) { return ESP_OK; } +err_t udp_sendto(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port) +{ + return ESP_OK; +} + +void udp_disconnect(struct udp_pcb *pcb) +{ +} + +void udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg) +{ +} + err_t udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif) { return ESP_OK; @@ -224,25 +290,14 @@ err_t udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *ds return ESP_OK; } -void * mem_malloc(mem_size_t size) -{ - return malloc(size); -} +/* ------------------------------------------------- ESP32 Port Mock --------------------------------------------------- + * + * ------------------------------------------------------------------------------------------------------------------ */ -void * mem_calloc(size_t nr, mem_size_t size) -{ - return calloc(nr, size); -} +// --------------------- lwipopts.h ------------------------ -void mem_free(void *rmem) -{ - free(rmem); -} - -void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg) -{ -} - -void sys_untimeout(sys_timeout_handler handler, void *arg) +uint32_t esp_random(void) { + // Preparation for injecting favorable random numbers + return g_random_numbers[g_random_numbers_cnt++ % 8]; } diff --git a/components/lwip/test_afl_host/esp32_mock.h b/components/lwip/test_afl_host/esp32_mock.h new file mode 100644 index 0000000000..d384acd013 --- /dev/null +++ b/components/lwip/test_afl_host/esp32_mock.h @@ -0,0 +1,35 @@ +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ESP32_MOCK_H_ +#define _ESP32_MOCK_H_ + +#include +#include + +/* ------------------------------------------------- ESP32 Port Mock --------------------------------------------------- + * + * ------------------------------------------------------------------------------------------------------------------ */ + +// --------------------- lwipopts.h ------------------------ + +#define ESP_TASK_TCPIP_STACK +#define ESP_TASK_TCPIP_PRIO + +uint32_t esp_random(void); + +// --------------------- sys_arch.h ------------------------ + +// Required to get linux assert.h to work ??? +#define __ASSERT_FUNC __ASSERT_FUNCTION + +typedef void * SemaphoreHandle_t; +typedef void * TaskHandle_t; +typedef void * QueueHandle_t; + +#define vTaskDelay(ms) usleep((m)*0) + +#endif // _ESP32_MOCK_H_ diff --git a/components/lwip/test_afl_host/esp_netif_loopback_mock.c b/components/lwip/test_afl_host/esp_netif_loopback_mock.c deleted file mode 100644 index a86b64fd55..0000000000 --- a/components/lwip/test_afl_host/esp_netif_loopback_mock.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include "esp_netif_lwip_internal.h" - -esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info) -{ - return ESP_ERR_NOT_SUPPORTED; -} - -esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status) -{ - return ESP_ERR_NOT_SUPPORTED; -} - -const char *esp_netif_get_ifkey(esp_netif_t *esp_netif) -{ - return esp_netif->if_key; -} - -esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6) -{ - return ESP_ERR_NOT_SUPPORTED; -}