From f334ac68b6313ce4b631b2e41f7f80b1bef11831 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Tue, 2 Jan 2018 15:44:08 +0100 Subject: [PATCH] Work on task #14780: Add debug helper asserts to ensure threading/locking requirements are met Add LWIP_ASSERT_CORE_LOCKED() in several places --- src/core/ipv4/autoip.c | 3 +++ src/core/ipv4/dhcp.c | 6 ++++++ src/core/netif.c | 2 -- src/core/raw.c | 10 ++++++++++ src/core/tcp.c | 22 ++++++++++++++++++++++ src/core/tcp_in.c | 1 + src/core/tcp_out.c | 3 +++ 7 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/core/ipv4/autoip.c b/src/core/ipv4/autoip.c index a3cf9fe4..9f7139bc 100644 --- a/src/core/ipv4/autoip.c +++ b/src/core/ipv4/autoip.c @@ -105,6 +105,7 @@ static void autoip_start_probing(struct netif *netif); void autoip_set_struct(struct netif *netif, struct autoip *autoip) { + LWIP_ASSERT_CORE_LOCKED(); LWIP_ASSERT("netif != NULL", netif != NULL); LWIP_ASSERT("autoip != NULL", autoip != NULL); LWIP_ASSERT("netif already has a struct autoip set", @@ -256,6 +257,7 @@ autoip_start(struct netif *netif) struct autoip *autoip = netif_autoip_data(netif); err_t result = ERR_OK; + LWIP_ASSERT_CORE_LOCKED(); LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;); /* Set IP-Address, Netmask and Gateway to 0 to make sure that @@ -348,6 +350,7 @@ autoip_stop(struct netif *netif) { struct autoip *autoip = netif_autoip_data(netif); + LWIP_ASSERT_CORE_LOCKED(); if (autoip != NULL) { autoip->state = AUTOIP_STATE_OFF; if (ip4_addr_islinklocal(netif_ip4_addr(netif))) { diff --git a/src/core/ipv4/dhcp.c b/src/core/ipv4/dhcp.c index ebc89174..00fed0e4 100644 --- a/src/core/ipv4/dhcp.c +++ b/src/core/ipv4/dhcp.c @@ -688,6 +688,7 @@ dhcp_handle_ack(struct netif *netif, struct dhcp_msg *msg_in) void dhcp_set_struct(struct netif *netif, struct dhcp *dhcp) { + LWIP_ASSERT_CORE_LOCKED(); LWIP_ASSERT("netif != NULL", netif != NULL); LWIP_ASSERT("dhcp != NULL", dhcp != NULL); LWIP_ASSERT("netif already has a struct dhcp set", netif_dhcp_data(netif) == NULL); @@ -709,6 +710,7 @@ dhcp_set_struct(struct netif *netif, struct dhcp *dhcp) */ void dhcp_cleanup(struct netif *netif) { + LWIP_ASSERT_CORE_LOCKED(); LWIP_ASSERT("netif != NULL", netif != NULL); if (netif_dhcp_data(netif) != NULL) { @@ -736,6 +738,7 @@ dhcp_start(struct netif *netif) struct dhcp *dhcp; err_t result; + LWIP_ASSERT_CORE_LOCKED(); LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;); LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;); dhcp = netif_dhcp_data(netif); @@ -816,6 +819,7 @@ dhcp_inform(struct netif *netif) struct pbuf *p_out; u16_t options_out_len; + LWIP_ASSERT_CORE_LOCKED(); LWIP_ERROR("netif != NULL", (netif != NULL), return;); if (dhcp_inc_pcb_refcount() != ERR_OK) { /* ensure DHCP PCB is allocated */ @@ -1154,6 +1158,7 @@ dhcp_renew(struct netif *netif) struct pbuf *p_out; u16_t options_out_len; + LWIP_ASSERT_CORE_LOCKED(); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_renew()\n")); dhcp_set_state(dhcp, DHCP_STATE_RENEWING); @@ -1312,6 +1317,7 @@ dhcp_release_and_stop(struct netif *netif) struct dhcp *dhcp = netif_dhcp_data(netif); ip_addr_t server_ip_addr; + LWIP_ASSERT_CORE_LOCKED(); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_release_and_stop()\n")); if (dhcp == NULL) { return; diff --git a/src/core/netif.c b/src/core/netif.c index 16fa9511..ec08bc5d 100644 --- a/src/core/netif.c +++ b/src/core/netif.c @@ -770,8 +770,6 @@ netif_set_up(struct netif *netif) static void netif_issue_reports(struct netif *netif, u8_t report_type) { - LWIP_ASSERT_CORE_LOCKED(); - /* Only send reports when both link and admin states are up */ if (!(netif->flags & NETIF_FLAG_LINK_UP) || !(netif->flags & NETIF_FLAG_UP)) { diff --git a/src/core/raw.c b/src/core/raw.c index a6cbff42..ae5e339b 100644 --- a/src/core/raw.c +++ b/src/core/raw.c @@ -217,6 +217,7 @@ raw_input(struct pbuf *p, struct netif *inp) err_t raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr) { + LWIP_ASSERT_CORE_LOCKED(); if ((pcb == NULL) || (ipaddr == NULL)) { return ERR_VAL; } @@ -248,6 +249,7 @@ raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr) void raw_bind_netif(struct raw_pcb *pcb, const struct netif *netif) { + LWIP_ASSERT_CORE_LOCKED(); if (netif != NULL) { pcb->netif_idx = netif_get_index(netif); } else { @@ -272,6 +274,7 @@ raw_bind_netif(struct raw_pcb *pcb, const struct netif *netif) err_t raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr) { + LWIP_ASSERT_CORE_LOCKED(); if ((pcb == NULL) || (ipaddr == NULL)) { return ERR_VAL; } @@ -297,6 +300,7 @@ raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr) void raw_disconnect(struct raw_pcb *pcb) { + LWIP_ASSERT_CORE_LOCKED(); /* reset remote address association */ #if LWIP_IPV4 && LWIP_IPV6 if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { @@ -326,6 +330,7 @@ raw_disconnect(struct raw_pcb *pcb) void raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg) { + LWIP_ASSERT_CORE_LOCKED(); /* remember recv() callback and user data */ pcb->recv = recv; pcb->recv_arg = recv_arg; @@ -417,6 +422,8 @@ raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t header_size; u8_t ttl; + LWIP_ASSERT_CORE_LOCKED(); + if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { return ERR_VAL; @@ -551,6 +558,7 @@ void raw_remove(struct raw_pcb *pcb) { struct raw_pcb *pcb2; + LWIP_ASSERT_CORE_LOCKED(); /* pcb to be removed is first in list? */ if (raw_pcbs == pcb) { /* make list start at 2nd pcb */ @@ -586,6 +594,7 @@ raw_new(u8_t proto) struct raw_pcb *pcb; LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n")); + LWIP_ASSERT_CORE_LOCKED(); pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB); /* could allocate RAW PCB? */ @@ -622,6 +631,7 @@ struct raw_pcb * raw_new_ip_type(u8_t type, u8_t proto) { struct raw_pcb *pcb; + LWIP_ASSERT_CORE_LOCKED(); pcb = raw_new(proto); #if LWIP_IPV4 && LWIP_IPV6 if (pcb != NULL) { diff --git a/src/core/tcp.c b/src/core/tcp.c index e5521b8f..a9857f5e 100644 --- a/src/core/tcp.c +++ b/src/core/tcp.c @@ -265,6 +265,7 @@ void tcp_backlog_delayed(struct tcp_pcb *pcb) { LWIP_ASSERT("pcb != NULL", pcb != NULL); + LWIP_ASSERT_CORE_LOCKED(); if ((pcb->flags & TF_BACKLOGPEND) == 0) { if (pcb->listener != NULL) { pcb->listener->accepts_pending++; @@ -287,6 +288,7 @@ void tcp_backlog_accepted(struct tcp_pcb *pcb) { LWIP_ASSERT("pcb != NULL", pcb != NULL); + LWIP_ASSERT_CORE_LOCKED(); if ((pcb->flags & TF_BACKLOGPEND) != 0) { if (pcb->listener != NULL) { LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0); @@ -451,6 +453,7 @@ err_t tcp_close(struct tcp_pcb *pcb) { LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in ")); + LWIP_ASSERT_CORE_LOCKED(); tcp_debug_print_state(pcb->state); if (pcb->state != LISTEN) { @@ -477,6 +480,7 @@ tcp_close(struct tcp_pcb *pcb) err_t tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx) { + LWIP_ASSERT_CORE_LOCKED(); if (pcb->state == LISTEN) { return ERR_CONN; } @@ -527,6 +531,7 @@ tcp_abandon(struct tcp_pcb *pcb, int reset) #endif /* LWIP_CALLBACK_API */ void *errf_arg; + LWIP_ASSERT_CORE_LOCKED(); /* pcb->state LISTEN not allowed here */ LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs", pcb->state != LISTEN); @@ -622,6 +627,8 @@ tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) ip_addr_t zoned_ipaddr; #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */ + LWIP_ASSERT_CORE_LOCKED(); + #if LWIP_IPV4 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */ if (ipaddr == NULL) { @@ -712,6 +719,7 @@ tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) void tcp_bind_netif(struct tcp_pcb *pcb, const struct netif *netif) { + LWIP_ASSERT_CORE_LOCKED(); if (netif != NULL) { pcb->netif_idx = netif_get_index(netif); } else { @@ -771,6 +779,7 @@ tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err) struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog) { + LWIP_ASSERT_CORE_LOCKED(); return tcp_listen_with_backlog_and_err(pcb, backlog, NULL); } @@ -797,6 +806,7 @@ tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err) err_t res; LWIP_UNUSED_ARG(backlog); + LWIP_ASSERT_CORE_LOCKED(); LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, res = ERR_CLSD; goto done); /* already listening? */ @@ -904,6 +914,7 @@ tcp_recved(struct tcp_pcb *pcb, u16_t len) { u32_t wnd_inflation; + LWIP_ASSERT_CORE_LOCKED(); /* pcb->state LISTEN not allowed here */ LWIP_ASSERT("don't call tcp_recved for listen-pcbs", pcb->state != LISTEN); @@ -1008,6 +1019,8 @@ tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port, u32_t iss; u16_t old_local_port; + LWIP_ASSERT_CORE_LOCKED(); + if ((pcb == NULL) || (ipaddr == NULL)) { return ERR_VAL; } @@ -1569,6 +1582,7 @@ tcp_seg_free(struct tcp_seg *seg) void tcp_setprio(struct tcp_pcb *pcb, u8_t prio) { + LWIP_ASSERT_CORE_LOCKED(); pcb->prio = prio; } @@ -1752,6 +1766,8 @@ tcp_alloc(u8_t prio) { struct tcp_pcb *pcb; + LWIP_ASSERT_CORE_LOCKED(); + pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); if (pcb == NULL) { /* Try to send FIN for all pcbs stuck in TF_CLOSEPEND first */ @@ -1902,6 +1918,7 @@ tcp_new_ip_type(u8_t type) void tcp_arg(struct tcp_pcb *pcb, void *arg) { + LWIP_ASSERT_CORE_LOCKED(); /* This function is allowed to be called for both listen pcbs and connection pcbs. */ if (pcb != NULL) { @@ -1924,6 +1941,7 @@ tcp_arg(struct tcp_pcb *pcb, void *arg) void tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv) { + LWIP_ASSERT_CORE_LOCKED(); if (pcb != NULL) { LWIP_ASSERT("invalid socket state for recv callback", pcb->state != LISTEN); pcb->recv = recv; @@ -1943,6 +1961,7 @@ tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv) void tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent) { + LWIP_ASSERT_CORE_LOCKED(); if (pcb != NULL) { LWIP_ASSERT("invalid socket state for sent callback", pcb->state != LISTEN); pcb->sent = sent; @@ -1968,6 +1987,7 @@ tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent) void tcp_err(struct tcp_pcb *pcb, tcp_err_fn err) { + LWIP_ASSERT_CORE_LOCKED(); if (pcb != NULL) { LWIP_ASSERT("invalid socket state for err callback", pcb->state != LISTEN); pcb->errf = err; @@ -1986,6 +2006,7 @@ tcp_err(struct tcp_pcb *pcb, tcp_err_fn err) void tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept) { + LWIP_ASSERT_CORE_LOCKED(); if ((pcb != NULL) && (pcb->state == LISTEN)) { struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen *)pcb; lpcb->accept = accept; @@ -2014,6 +2035,7 @@ tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept) void tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval) { + LWIP_ASSERT_CORE_LOCKED(); LWIP_ASSERT("invalid socket state for poll", pcb->state != LISTEN); #if LWIP_CALLBACK_API pcb->poll = poll; diff --git a/src/core/tcp_in.c b/src/core/tcp_in.c index f9f53e5f..18fda797 100644 --- a/src/core/tcp_in.c +++ b/src/core/tcp_in.c @@ -123,6 +123,7 @@ tcp_input(struct pbuf *p, struct netif *inp) err_t err; LWIP_UNUSED_ARG(inp); + LWIP_ASSERT_CORE_LOCKED(); PERF_START; diff --git a/src/core/tcp_out.c b/src/core/tcp_out.c index 965ae862..f35720fa 100644 --- a/src/core/tcp_out.c +++ b/src/core/tcp_out.c @@ -426,6 +426,8 @@ tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags) u16_t mss_local = LWIP_MIN(pcb->mss, TCPWND_MIN16(pcb->snd_wnd_max / 2)); mss_local = mss_local ? mss_local : pcb->mss; + LWIP_ASSERT_CORE_LOCKED(); + #if LWIP_NETIF_TX_SINGLE_PBUF /* Always copy to try to create single pbufs for TX */ apiflags |= TCP_WRITE_FLAG_COPY; @@ -1133,6 +1135,7 @@ tcp_output(struct tcp_pcb *pcb) s16_t i = 0; #endif /* TCP_CWND_DEBUG */ + LWIP_ASSERT_CORE_LOCKED(); /* pcb->state LISTEN not allowed here */ LWIP_ASSERT("don't call tcp_output for listen-pcbs", pcb->state != LISTEN);