From e19271fc44f1cb7ae0f11f4f2304debab553e991 Mon Sep 17 00:00:00 2001 From: DrDoof <8052722+DrDoof@users.noreply.github.com> Date: Sun, 4 Oct 2026 20:26:51 +0200 Subject: [PATCH] test/stp: build the simulator against the firmware headers test/stp/shim held hand-written copies of the headers the STP code includes, and they had drifted: the uIP buffer was 600 bytes instead of 1556, struct machine had three fields, port_ingress_filter_get() returned a plain uint8_t. The simulator now compiles rtl837x_stp.c and rtl837x_stp_cli.c the way test/ compiles firmware sources, with the real headers and sdcc_shim.h forced in, and with the same warnings, including -Wno-pointer-sign for the string literals passed to cmd_compare(). It stays on -std=c11: in gnu11 mode glibc defines LITTLE_ENDIAN, which uip-conf.h names as the byte order, and HTONS() stops swapping. Its own print_string() and cmd_compare() take the firmware's argument types. --- test/stp/Makefile | 23 ++++++++++------ test/stp/shim/machine.h | 12 -------- test/stp/shim/rtl837x_common.h | 50 ---------------------------------- test/stp/shim/rtl837x_port.h | 11 -------- test/stp/shim/rtl837x_regs.h | 9 ------ test/stp/shim/rtl837x_sfr.h | 6 ---- test/stp/shim/uip.h | 8 ------ test/stp/stp_sim.c | 4 +-- 8 files changed, 17 insertions(+), 106 deletions(-) delete mode 100644 test/stp/shim/machine.h delete mode 100644 test/stp/shim/rtl837x_common.h delete mode 100644 test/stp/shim/rtl837x_port.h delete mode 100644 test/stp/shim/rtl837x_regs.h delete mode 100644 test/stp/shim/rtl837x_sfr.h delete mode 100644 test/stp/shim/uip.h diff --git a/test/stp/Makefile b/test/stp/Makefile index 9fda7ac2..c596cf37 100644 --- a/test/stp/Makefile +++ b/test/stp/Makefile @@ -1,7 +1,16 @@ -CC ?= cc -CFLAGS = -std=c11 -Wall -Wextra -Wno-unknown-pragmas \ - -D__xdata= -D__code= -D__banked= -D__reentrant= \ - -Ishim -I../.. +CC ?= cc +SRCROOT = ../.. +CFLAGS = -std=c11 -g -O1 -Wall -Wextra -Werror -Wno-unused-parameter -Wno-unknown-pragmas \ + -Wno-pointer-sign \ + -DRTLP_HOST_TEST \ + -fsanitize=address,undefined -fno-omit-frame-pointer \ + -include ../sdcc_shim.h -I../stub -I$(SRCROOT) -I$(SRCROOT)/httpd -I$(SRCROOT)/uip +ifeq ($(findstring clang,$(shell $(CC) --version 2>/dev/null)),) +CFLAGS += -Wno-builtin-declaration-mismatch +endif +LDFLAGS = -fsanitize=address,undefined + +SRCS = stp_sim.c $(SRCROOT)/rtl837x_stp.c $(SRCROOT)/rtl837x_stp_cli.c all: build/stp_sim ./build/stp_sim @@ -9,11 +18,9 @@ all: build/stp_sim verbose: build/stp_sim ./build/stp_sim -v -build/stp_sim: stp_sim.c ../../rtl837x_stp.c ../../rtl837x_stp_cli.c ../../rtl837x_stp.h shim/*.h +build/stp_sim: $(SRCS) $(SRCROOT)/rtl837x_stp.h mkdir -p build - cp ../../rtl837x_stp.c build/stp_under_test.c - cp ../../rtl837x_stp_cli.c build/stp_cli_under_test.c - $(CC) $(CFLAGS) -o $@ stp_sim.c build/stp_under_test.c build/stp_cli_under_test.c + $(CC) $(CFLAGS) $(SRCS) -o $@ $(LDFLAGS) clean: rm -rf build diff --git a/test/stp/shim/machine.h b/test/stp/shim/machine.h deleted file mode 100644 index bce13895..00000000 --- a/test/stp/shim/machine.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef _SHIM_MACHINE_H_ -#define _SHIM_MACHINE_H_ -#include -#define MACHINE_MIN_PORT 0 -#define MACHINE_MAX_PORT 8 -#define MACHINE_PORT_VALID(p) \ - ((uint8_t)((p) - MACHINE_MIN_PORT) <= (MACHINE_MAX_PORT - MACHINE_MIN_PORT)) -struct machine { uint8_t log_to_phys_port[9]; }; -extern const struct machine machine; -struct machine_runtime { uint8_t isRTL8373; }; -extern struct machine_runtime machine_detected; -#endif diff --git a/test/stp/shim/rtl837x_common.h b/test/stp/shim/rtl837x_common.h deleted file mode 100644 index 6f124245..00000000 --- a/test/stp/shim/rtl837x_common.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef _SHIM_RTL837X_COMMON_H_ -#define _SHIM_RTL837X_COMMON_H_ - -#include -#include -#include - -struct rtl_tag { - uint16_t tag; - uint8_t version; - uint8_t reason; - uint16_t flags; - uint16_t pmask; /* TX: port bitmask; RX: 4-bit port number */ -}; - -struct vlan_tag { - uint16_t svlan; - uint16_t vlan; -}; - -#define RTL_FRAME_TAG_ID 0x8899 -#define RTL_FRAME_TAG_VERSION 0x04 -#define RTL_FRAME_DESC_SIZE 12 -#define RTL_TAG_LEARN_DIS 0x0020 - -#define PMASK_9 0x1ff -#define PMASK_6 0x1f8 -#define PMASK_CPU 0x200 - -#define CMD_BUF_SIZE 128 -#define CPU_PORT 9 - -#define ERR_OK 0 -#define ERR_INVALID_ARGUMENT 3 - -#define HTONS(n) ((uint16_t)((((uint16_t)(n)) << 8) | (((uint16_t)(n)) >> 8))) - -extern bool stp_enabled; -void print_string(char *s); -void print_byte(uint8_t b); -void print_short(uint16_t v); -void print_long(uint32_t v); -void write_char(char c); -void itoa(uint8_t v); -void print_reg(uint16_t reg); -void reg_read_m(uint16_t addr); -void reg_write_m(uint16_t addr); -void tcpip_output(void); - -#endif diff --git a/test/stp/shim/rtl837x_port.h b/test/stp/shim/rtl837x_port.h deleted file mode 100644 index 4a33e7a3..00000000 --- a/test/stp/shim/rtl837x_port.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _SHIM_RTL837X_PORT_H_ -#define _SHIM_RTL837X_PORT_H_ -#include -#define VLAN_TAGGED 1 -int8_t vlan_get(uint16_t vlan); -uint16_t port_pvid_get(uint8_t port); -void port_l2mc_set(uint8_t mac_last, uint16_t vid, uint16_t pmask); -void port_l2_forget_port(uint8_t port); -uint16_t port_lag_members_get(uint8_t lag); -uint8_t port_ingress_filter_get(uint8_t port); -#endif diff --git a/test/stp/shim/rtl837x_regs.h b/test/stp/shim/rtl837x_regs.h deleted file mode 100644 index 16c1ab86..00000000 --- a/test/stp/shim/rtl837x_regs.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _SHIM_RTL837X_REGS_H_ -#define _SHIM_RTL837X_REGS_H_ - -#define RTL837X_MSTP_STATES 0x5310 -#define RTL837X_REG_LINKS_STS 0x63E8 -#define RTL837X_REG_LINKS 0x63f0 -#define RTL837X_REG_LINKS_89 0x63f4 - -#endif diff --git a/test/stp/shim/rtl837x_sfr.h b/test/stp/shim/rtl837x_sfr.h deleted file mode 100644 index 38560691..00000000 --- a/test/stp/shim/rtl837x_sfr.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _SHIM_RTL837X_SFR_H_ -#define _SHIM_RTL837X_SFR_H_ - -extern unsigned char sfr_data[4]; - -#endif diff --git a/test/stp/shim/uip.h b/test/stp/shim/uip.h deleted file mode 100644 index e46f8a6e..00000000 --- a/test/stp/shim/uip.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _SHIM_UIP_H_ -#define _SHIM_UIP_H_ -#include -#define UIP_CONF_BUFFER_SIZE 600 -struct uip_eth_addr { uint8_t addr[6]; }; -extern uint16_t uip_len; -void tcpip_output(void); -#endif diff --git a/test/stp/stp_sim.c b/test/stp/stp_sim.c index d69702a8..5a0faf15 100644 --- a/test/stp/stp_sim.c +++ b/test/stp/stp_sim.c @@ -29,7 +29,7 @@ uint8_t err_status; uint8_t atoi_results_u8; static int verbose; -void print_string(char *s) { if (verbose) fputs(s, stdout); } +void print_string(const char *s) { if (verbose) fputs(s, stdout); } void print_byte(uint8_t b) { if (verbose) printf("%02x", b); } void print_short(uint16_t v) { if (verbose) printf("%04x", v); } void print_long(uint32_t v) { if (verbose) printf("%08x", v); } @@ -37,7 +37,7 @@ void write_char(char c) { if (verbose) putchar(c); } void itoa(uint8_t v) { if (verbose) printf("%u", v); } void print_reg(uint16_t reg) { (void)reg; } -uint8_t cmd_compare(uint8_t start, uint8_t *cmd) +uint8_t cmd_compare(uint8_t start, const uint8_t *cmd) { if (start >= cmd_words_len) return 0;