Skip to content

Commit

Permalink
mdns, dns, dhcp, dhcps: update fuzzer test to work in CI
Browse files Browse the repository at this point in the history
Closes: IDF-1861 and IDF-1990


* Original commit: espressif/esp-idf@a43c06a
  • Loading branch information
suren-gabrielyan-espressif committed May 27, 2022
1 parent 9772e49 commit e0bc60a
Show file tree
Hide file tree
Showing 10 changed files with 597 additions and 104 deletions.
8 changes: 8 additions & 0 deletions components/mdns/test_afl_fuzz_host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The following four lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

project(fuzz_test_update)

43 changes: 38 additions & 5 deletions components/mdns/test_afl_fuzz_host/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
TEST_NAME=test
FUZZ=afl-fuzz
COMPONENTS_DIR=../..
CFLAGS=-g -DHOOK_MALLOC_FAILED -DESP_EVENT_H_ -D__ESP_LOG_H__ -DMDNS_TEST_MODE \
-I. -I.. -I../include -I../private_include -include esp32_compat.h \
-I$(COMPONENTS_DIR)/tcpip_adapter/include -I$(COMPONENTS_DIR)/esp_event/include -I$(COMPONENTS_DIR)/log/include
COMPILER_ICLUDE_DIR=$(shell echo `which xtensa-esp32-elf-gcc | xargs dirname | xargs dirname`/xtensa-esp32-elf)

CFLAGS=-g -Wno-unused-value -Wno-missing-declarations -Wno-pointer-bool-conversion -Wno-macro-redefined -Wno-int-to-void-pointer-cast -DHOOK_MALLOC_FAILED -DESP_EVENT_H_ -D__ESP_LOG_H__ -DMDNS_TEST_MODE \
-I. -I.. -I../include -I../private_include -I ./build/config -include esp32_compat.h \
-I$(COMPONENTS_DIR) \
-I$(COMPONENTS_DIR)/driver/include \
-I$(COMPONENTS_DIR)/esp_common/include \
-I$(COMPONENTS_DIR)/esp_event/include \
-I$(COMPONENTS_DIR)/esp_eth/include \
-I$(COMPONENTS_DIR)/esp_netif/include \
-I$(COMPONENTS_DIR)/esp_netif/private_include \
-I$(COMPONENTS_DIR)/esp_netif/lwip \
-I$(COMPONENTS_DIR)/esp_rom/include \
-I$(COMPONENTS_DIR)/esp_system/include \
-I$(COMPONENTS_DIR)/esp_timer/include \
-I$(COMPONENTS_DIR)/esp_wifi/include \
-I$(COMPONENTS_DIR)/freertos/xtensa/include \
-I$(COMPONENTS_DIR)/freertos \
-I$(COMPONENTS_DIR)/freertos/include \
-I$(COMPONENTS_DIR)/hal/include \
-I$(COMPONENTS_DIR)/hal/esp32/include \
-I$(COMPONENTS_DIR)/heap/include \
-I$(COMPONENTS_DIR)/log/include \
-I$(COMPONENTS_DIR)/lwip/lwip/src/include \
-I$(COMPONENTS_DIR)/lwip/port/esp32/include \
-I$(COMPONENTS_DIR)/lwip/lwip/src/include/lwip/apps \
-I$(COMPONENTS_DIR)/soc/include \
-I$(COMPONENTS_DIR)/soc/soc/include \
-I$(COMPONENTS_DIR)/soc/soc/esp32/include \
-I$(COMPONENTS_DIR)/soc/src/esp32/include \
-I$(COMPONENTS_DIR)/tcpip_adapter/include \
-I$(COMPONENTS_DIR)/xtensa/include \
-I$(COMPONENTS_DIR)/xtensa/esp32/include \
-I$(COMPILER_ICLUDE_DIR)/include


MDNS_C_DEPENDENCY_INJECTION=-include mdns_di.h
ifeq ($(INSTR),off)
CC=gcc
Expand All @@ -14,7 +47,7 @@ else
endif
CPP=$(CC)
LD=$(CC)
OBJECTS=mdns.o esp32_mock.o test.o
OBJECTS=esp32_mock.o esp_netif_loopback_mock.o mdns.o test.o esp_netif_objects_mock.o

OS := $(shell uname)
ifeq ($(OS),Darwin)
Expand All @@ -32,7 +65,7 @@ all: $(TEST_NAME)

mdns.o: ../mdns.c
@echo "[CC] $<"
@$(CC) $(CFLAGS) $(MDNS_C_DEPENDENCY_INJECTION) -c $< -o $@
@$(CC) $(CFLAGS) --include mdns_mock.h $(MDNS_C_DEPENDENCY_INJECTION) -c $< -o $@

$(TEST_NAME): $(OBJECTS)
@echo "[LD] $@"
Expand Down
82 changes: 9 additions & 73 deletions components/mdns/test_afl_fuzz_host/esp32_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
#include <signal.h>
#include <sys/time.h>

#define CONFIG_MDNS_MAX_SERVICES 25

#define ERR_OK 0
#define ESP_OK 0
#define ESP_FAIL -1

#define ESP_ERR_NO_MEM 0x101
Expand All @@ -57,16 +53,24 @@
#define portMAX_DELAY 0xFFFFFFFF
#define portTICK_PERIOD_MS 1
#define ESP_LOGD(a,b)
#define ESP_LOGE(a,b,c)
#define ESP_LOGV(a,b,c,d)

#define LWIP_HDR_PBUF_H
#define __ESP_SYSTEM_H__
#define INC_TASK_H

#define xSemaphoreTake(s,d)
#define xTaskDelete(a)
#define vTaskDelete(a) free(a)
#define xSemaphoreGive(s)
#define xQueueCreateMutex(s)
#define _mdns_pcb_init(a,b) true
#define _mdns_pcb_deinit(a,b) true
#define _mdns_pcb_deinit(a,b) true
#define xSemaphoreCreateMutex() malloc(1)
#define xSemaphoreCreateBinary() malloc(1)
#define vSemaphoreDelete(s) free(s)
#define queueQUEUE_TYPE_MUTEX ( ( uint8_t ) 1U
#define xTaskCreatePinnedToCore(a,b,c,d,e,f,g) *(f) = malloc(1)
#define vTaskDelay(m) usleep((m)*0)
#define pbuf_free(p) free(p)
Expand All @@ -84,9 +88,6 @@
#define ip_2_ip6(ipaddr) (&((ipaddr)->u_addr.ip6))
#define ip_2_ip4(ipaddr) (&((ipaddr)->u_addr.ip4))

#define IPADDR_TYPE_V4 0U
#define IPADDR_TYPE_V6 6U
#define IPADDR_TYPE_ANY 46U
#define IP_SET_TYPE_VAL(ipaddr, iptype) do { (ipaddr).type = (iptype); }while(0)
#define IP_ADDR4(ipaddr,a,b,c,d) do { IP4_ADDR(ip_2_ip4(ipaddr),a,b,c,d); \
IP_SET_TYPE_VAL(*(ipaddr), IPADDR_TYPE_V4); } while(0)
Expand All @@ -102,60 +103,14 @@ typedef void * SemaphoreHandle_t;
typedef void * xQueueHandle;
typedef void * QueueHandle_t;
typedef void * TaskHandle_t;
typedef void * esp_timer_handle_t;
typedef uint32_t TickType_t;
typedef uint32_t portTickType;


typedef enum {
WIFI_MODE_NULL = 0, /**< null mode */
WIFI_MODE_STA, /**< WiFi station mode */
WIFI_MODE_AP, /**< WiFi soft-AP mode */
WIFI_MODE_APSTA, /**< WiFi station + soft-AP mode */
WIFI_MODE_MAX
} wifi_mode_t;

extern const char * WIFI_EVENT;
extern const char * IP_EVENT;
extern const char * ETH_EVENT;

typedef enum {
WIFI_EVENT_WIFI_READY = 0, /**< ESP32 WiFi ready */
WIFI_EVENT_SCAN_DONE, /**< ESP32 finish scanning AP */
WIFI_EVENT_STA_START, /**< ESP32 station start */
WIFI_EVENT_STA_STOP, /**< ESP32 station stop */
WIFI_EVENT_STA_CONNECTED, /**< ESP32 station connected to AP */
WIFI_EVENT_STA_DISCONNECTED, /**< ESP32 station disconnected from AP */
WIFI_EVENT_STA_AUTHMODE_CHANGE, /**< the auth mode of AP connected by ESP32 station changed */

WIFI_EVENT_STA_WPS_ER_SUCCESS, /**< ESP32 station wps succeeds in enrollee mode */
WIFI_EVENT_STA_WPS_ER_FAILED, /**< ESP32 station wps fails in enrollee mode */
WIFI_EVENT_STA_WPS_ER_TIMEOUT, /**< ESP32 station wps timeout in enrollee mode */
WIFI_EVENT_STA_WPS_ER_PIN, /**< ESP32 station wps pin code in enrollee mode */

WIFI_EVENT_AP_START, /**< ESP32 soft-AP start */
WIFI_EVENT_AP_STOP, /**< ESP32 soft-AP stop */
WIFI_EVENT_AP_STACONNECTED, /**< a station connected to ESP32 soft-AP */
WIFI_EVENT_AP_STADISCONNECTED, /**< a station disconnected from ESP32 soft-AP */

WIFI_EVENT_AP_PROBEREQRECVED, /**< Receive probe request packet in soft-AP interface */
} wifi_event_t;

typedef enum {
ETHERNET_EVENT_START, /**< ESP32 ethernet start */
ETHERNET_EVENT_STOP, /**< ESP32 ethernet stop */
ETHERNET_EVENT_CONNECTED, /**< ESP32 ethernet phy link up */
ETHERNET_EVENT_DISCONNECTED, /**< ESP32 ethernet phy link down */
} eth_event_t;

typedef enum {
IP_EVENT_STA_GOT_IP, /*!< ESP32 station got IP from connected AP */
IP_EVENT_STA_LOST_IP, /*!< ESP32 station lost IP and the IP is reset to 0 */
IP_EVENT_AP_STAIPASSIGNED, /*!< ESP32 soft-AP assign an IP to a connected station */
IP_EVENT_GOT_IP6, /*!< ESP32 station or ap or ethernet interface v6IP addr is preferred */
IP_EVENT_ETH_GOT_IP, /*!< ESP32 ethernet got IP from connected AP */
} ip_event_t;

/* status of DHCP client or DHCP server */
typedef enum {
TCPIP_ADAPTER_DHCP_INIT = 0, /**< DHCP client/server in initial state */
Expand All @@ -178,14 +133,6 @@ struct ip6_addr {
};
typedef struct ip6_addr ip6_addr_t;

typedef struct _ip_addr {
union {
ip6_addr_t ip6;
ip4_addr_t ip4;
} u_addr;
uint8_t type;
} ip_addr_t;

typedef struct {
ip4_addr_t ip;
ip4_addr_t netmask;
Expand All @@ -203,19 +150,8 @@ typedef struct {
ip6_addr_t ip;
} tcpip_adapter_ip6_info_t;

typedef struct {
tcpip_adapter_if_t if_index; /*!< Interface for which the event is received */
tcpip_adapter_ip6_info_t ip6_info; /*!< IPv6 address of the interface */
} ip_event_got_ip6_t;

typedef void* system_event_t;

inline esp_err_t esp_wifi_get_mode(wifi_mode_t * mode)
{
*mode = WIFI_MODE_APSTA;
return ESP_OK;
}

struct pbuf {
struct pbuf *next;
void *payload;
Expand Down
1 change: 0 additions & 1 deletion components/mdns/test_afl_fuzz_host/esp32_mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ int g_queue_send_shall_fail = 0;
int g_size = 0;

const char * WIFI_EVENT = "wifi_event";
const char * IP_EVENT = "ip_event";
const char * ETH_EVENT = "eth_event";

esp_err_t esp_event_handler_register(const char * event_base,
Expand Down
28 changes: 3 additions & 25 deletions components/mdns/test_afl_fuzz_host/esp32_mock.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
#ifndef ESP32_MOCK_H_
#define ESP32_MOCK_H_
#include "esp_timer.h"

typedef void (*esp_timer_cb_t)(void* arg);

typedef enum
{
ESP_TIMER_TASK, //!< Callback is called from timer task
} esp_timer_dispatch_t;

typedef struct
{
esp_timer_cb_t callback; //!< Function to call when timer expires
void* arg; //!< Argument to pass to the callback
esp_timer_dispatch_t dispatch_method; //!< Call the callback from task or from ISR
const char* name; //!< Timer name, used in esp_timer_dump function
} esp_timer_create_args_t;

esp_err_t esp_timer_delete(esp_timer_handle_t timer);

esp_err_t esp_timer_stop(esp_timer_handle_t timer);

esp_err_t esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period);

esp_err_t esp_timer_create(const esp_timer_create_args_t* create_args,
esp_timer_handle_t* out_handle);


// Queue mock
QueueHandle_t xQueueCreate( uint32_t uxQueueLength,
QueueHandle_t xQueueCreate( uint32_t uxQueueLength,
uint32_t uxItemSize );

void vQueueDelete( QueueHandle_t xQueue );
Expand All @@ -46,4 +24,4 @@ esp_err_t esp_event_handler_unregister(const char * event_base, int32_t event_id

#define _mdns_udp_pcb_write(tcpip_if, ip_protocol, ip, port, data, len) len

#endif /* ESP32_MOCK_H_ */
#endif /* ESP32_MOCK_H_ */
3 changes: 3 additions & 0 deletions components/mdns/test_afl_fuzz_host/esp_attr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once
#define IRAM_ATTR
#define FLAG_ATTR(TYPE)
35 changes: 35 additions & 0 deletions components/mdns/test_afl_fuzz_host/esp_netif_loopback_mock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2020 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#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;
}
71 changes: 71 additions & 0 deletions components/mdns/test_afl_fuzz_host/esp_netif_objects_mock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2020 Espressif Systems (Shanghai) CO LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "esp_netif.h"
#include "sys/queue.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "esp_netif_private.h"
#include <string.h>

//
// Purpose of this module is to provide list of esp-netif structures
// - this module has no dependency on a specific network stack (lwip)
//

struct slist_netifs_s {
esp_netif_t *netif;
SLIST_ENTRY(slist_netifs_s) next;
};

SLIST_HEAD(slisthead, slist_netifs_s) s_head = { .slh_first = NULL, };

static size_t s_esp_netif_counter = 0;

ESP_EVENT_DEFINE_BASE(IP_EVENT);

//
// List manipulation functions
//
esp_err_t esp_netif_add_to_list(esp_netif_t *netif)
{
return ESP_OK;
}


esp_err_t esp_netif_remove_from_list(esp_netif_t *netif)
{
return ESP_ERR_NOT_FOUND;
}

size_t esp_netif_get_nr_of_ifs(void)
{
return s_esp_netif_counter;
}

esp_netif_t* esp_netif_next(esp_netif_t* netif)
{
return NULL;
}

esp_netif_t* esp_netif_next_unsafe(esp_netif_t* netif)
{
return NULL;
}

esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key)
{
return NULL;
}
5 changes: 5 additions & 0 deletions components/mdns/test_afl_fuzz_host/mdns_mock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#define ERR_OK 0
#define IPADDR_TYPE_V4 0U
#define IPADDR_TYPE_V6 6U
#define IPADDR_TYPE_ANY 46U
Loading

0 comments on commit e0bc60a

Please sign in to comment.