From 924d88bbf94ae4467dcc23917ad083b1caf33683 Mon Sep 17 00:00:00 2001 From: Cedric Roux Date: Tue, 17 Jan 2023 13:06:25 +0100 Subject: [PATCH 1/3] bugfix: avoid a crash when oai_exit is set There was a crash when we exit the nr softmodem with AW2S device. The array rxbase[] was surely freed() by some other thread, so let's not access it after detecting oai_exit != 0. We also need to unblock a possible reader thread. The chosen way to do may be not the best. To be checked. --- radio/ETHERNET/USERSPACE/LIB/eth_udp.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/radio/ETHERNET/USERSPACE/LIB/eth_udp.c b/radio/ETHERNET/USERSPACE/LIB/eth_udp.c index ca62ceab6c..c098d4f539 100644 --- a/radio/ETHERNET/USERSPACE/LIB/eth_udp.c +++ b/radio/ETHERNET/USERSPACE/LIB/eth_udp.c @@ -451,6 +451,11 @@ void *udp_read_thread(void *arg) { buffer,sizeof(buffer),0, (struct sockaddr *)&((eth_state_t*)device->priv)->dest_addrd, (socklen_t *)&((eth_state_t*)device->priv)->addr_len); + /* if oai_exit is 1 here, don't access the array rxbase, + * it may have been freed(), so let's break at this point + */ + if (oai_exit) + break; aid = *(uint16_t*)(&buffer[ECPRICOMMON_BYTES]); TS = *(openair0_timestamp *)(&buffer[ECPRICOMMON_BYTES+ECPRIPCID_BYTES]); // convert TS to samples, /6 for AW2S @ 30.72 Ms/s, this is converted for other sample rates in OAI application @@ -475,6 +480,11 @@ void *udp_read_thread(void *arg) { } sleep(1); } + + /* let's unblock reader (maybe not the best way to do it) */ + fhstate->first_read = 0; + fhstate->r[0] = 1; + return(0); } From 08e57ca3132726dc3cddf22de457bdaf3d4a06fa Mon Sep 17 00:00:00 2001 From: Cedric Roux Date: Tue, 17 Jan 2023 13:10:08 +0100 Subject: [PATCH 2/3] bugfix: use correct size in malloc() --- radio/ETHERNET/USERSPACE/LIB/eth_udp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radio/ETHERNET/USERSPACE/LIB/eth_udp.c b/radio/ETHERNET/USERSPACE/LIB/eth_udp.c index c098d4f539..45cc75bd4d 100644 --- a/radio/ETHERNET/USERSPACE/LIB/eth_udp.c +++ b/radio/ETHERNET/USERSPACE/LIB/eth_udp.c @@ -417,7 +417,7 @@ extern int oai_exit; void *udp_write_thread(void *arg) { udp_ctx_t *utx = (udp_ctx_t *)arg; - utx->resp = (notifiedFIFO_t*) malloc(sizeof(notifiedFIFO_elt_t)); + utx->resp = malloc(sizeof(*utx->resp)); initNotifiedFIFO(utx->resp); LOG_D(PHY,"UDP write thread started on core %d\n",sched_getcpu()); reset_meas(&utx->device->tx_fhaul); From fc7bcceca9fedb383aeadde7d2a9db1b1d1ce1b8 Mon Sep 17 00:00:00 2001 From: Cedric Roux Date: Wed, 18 Jan 2023 09:54:38 +0100 Subject: [PATCH 3/3] fix a small bug and add a log for when recvfrom() fails recvfrom() returns ssize_t, not size_t. --- radio/ETHERNET/USERSPACE/LIB/eth_udp.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/radio/ETHERNET/USERSPACE/LIB/eth_udp.c b/radio/ETHERNET/USERSPACE/LIB/eth_udp.c index 45cc75bd4d..31971f1d1a 100644 --- a/radio/ETHERNET/USERSPACE/LIB/eth_udp.c +++ b/radio/ETHERNET/USERSPACE/LIB/eth_udp.c @@ -447,10 +447,20 @@ void *udp_read_thread(void *arg) { while (oai_exit == 0) { LOG_I(PHY,"UDP read thread %d, waiting for start sampling_rate_d %d, sampling_rate_n %d\n",u->thread_id,device->sampling_rate_ratio_n,device->sampling_rate_ratio_d); while (fhstate->active > 0) { - size_t count = recvfrom(((eth_state_t*)device->priv)->sockfdd[0], - buffer,sizeof(buffer),0, - (struct sockaddr *)&((eth_state_t*)device->priv)->dest_addrd, - (socklen_t *)&((eth_state_t*)device->priv)->addr_len); + ssize_t count = recvfrom(((eth_state_t*)device->priv)->sockfdd[0], + buffer,sizeof(buffer),0, + (struct sockaddr *)&((eth_state_t*)device->priv)->dest_addrd, + (socklen_t *)&((eth_state_t*)device->priv)->addr_len); + /* log and skip processing in case of error from recvfrom */ + /* (to be refined if needed) */ + if (count == 0) { + LOG_E(PHY, "recvfrom returned 0\n"); + continue; + } + if (count < 0) { + LOG_E(PHY, "recvfrom failed (%s)\n", strerror(errno)); + continue; + } /* if oai_exit is 1 here, don't access the array rxbase, * it may have been freed(), so let's break at this point */ @@ -460,7 +470,6 @@ void *udp_read_thread(void *arg) { TS = *(openair0_timestamp *)(&buffer[ECPRICOMMON_BYTES+ECPRIPCID_BYTES]); // convert TS to samples, /6 for AW2S @ 30.72 Ms/s, this is converted for other sample rates in OAI application TS = (device->sampling_rate_ratio_n*TS)/(device->sampling_rate_ratio_d*6); - if ((int)count <= 0) continue; AssertFatal(aid < 8,"Cannot handle more than 8 antennas, got aid %d\n",aid); fhstate->r[aid]=1; if (aid==0 && first_read == 0) fhstate->TS0 = TS;