From 3b0c43c04b3e74227467ad8411d7f9e47bef4ed6 Mon Sep 17 00:00:00 2001
From: Laurent <laurent.thomas@open-cells.com>
Date: Sun, 10 Jan 2021 21:41:22 +0100
Subject: [PATCH] fix memory allocation error

---
 common/utils/ocp_itti/intertask_interface.cpp | 55 ++++++++++---------
 openair2/ENB_APP/enb_config.c                 |  5 +-
 openair2/NETWORK_DRIVER/MESH/device.c         |  4 ++
 openair2/RRC/NR/rrc_gNB_nsa.c                 |  2 +-
 openair3/SCTP/sctp_eNB_task.c                 |  2 +-
 targets/ARCH/rfsimulator/apply_channelmod.c   |  5 +-
 targets/RT/USER/lte-enb.c                     |  3 +-
 7 files changed, 41 insertions(+), 35 deletions(-)

diff --git a/common/utils/ocp_itti/intertask_interface.cpp b/common/utils/ocp_itti/intertask_interface.cpp
index aec702f9873..964f5409fd0 100644
--- a/common/utils/ocp_itti/intertask_interface.cpp
+++ b/common/utils/ocp_itti/intertask_interface.cpp
@@ -45,7 +45,7 @@ extern "C" {
     std::vector<MessageDef *> message_queue;
     std::map<long,timer_elm_t> timer_map;
     uint64_t next_timer=UINT64_MAX;
-    struct epoll_event  *events =NULL;
+    struct epoll_event *events =NULL;
     int nb_fd_epoll=0;
     int nb_events=0;
     int epoll_fd=-1;
@@ -53,7 +53,7 @@ extern "C" {
   } task_list_t;
 
   int timer_expired(int fd);
-  task_list_t *tasks;
+  static task_list_t **tasks=NULL;
   static int nb_queues=0;
   static pthread_mutex_t lock_nb_queues;
 
@@ -124,7 +124,7 @@ extern "C" {
   }
 
   static inline int itti_send_msg_to_task_locked(task_id_t destination_task_id, instance_t destinationInstance, MessageDef *message) {
-    task_list_t *t=tasks+destination_task_id;
+    task_list_t *t=tasks[destination_task_id];
     message->ittiMsgHeader.destinationTaskId = destination_task_id;
     message->ittiMsgHeader.destinationInstance = destinationInstance;
     message->ittiMsgHeader.lte_time.frame = 0;
@@ -146,7 +146,7 @@ extern "C" {
   }
 
   int itti_send_msg_to_task(task_id_t destination_task_id, instance_t destinationInstance, MessageDef *message) {
-    task_list_t *t=&tasks[destination_task_id];
+    task_list_t *t=tasks[destination_task_id];
     pthread_mutex_lock (&t->queue_cond_lock);
     int ret=itti_send_msg_to_task_locked(destination_task_id, destinationInstance, message);
 
@@ -165,7 +165,7 @@ extern "C" {
 
   void itti_subscribe_event_fd(task_id_t task_id, int fd) {
     struct epoll_event event;
-    task_list_t *t=&tasks[task_id];
+    task_list_t *t=tasks[task_id];
     t->nb_fd_epoll++;
     t->events = (struct epoll_event *)realloc((void *)t->events,
                 t->nb_fd_epoll * sizeof(struct epoll_event));
@@ -178,7 +178,7 @@ extern "C" {
   }
 
   void itti_unsubscribe_event_fd(task_id_t task_id, int fd) {
-    task_list_t *t=&tasks[task_id];
+    task_list_t *t=tasks[task_id];
     AssertFatal (epoll_ctl(t->epoll_fd, EPOLL_CTL_DEL, fd, NULL) == 0,
                  "epoll_ctl (EPOLL_CTL_DEL) failed for task %s, fd %d: %s!\n",
                  itti_get_task_name(task_id), fd, strerror(errno));
@@ -186,7 +186,7 @@ extern "C" {
   }
 
   static inline int itti_get_events_locked(task_id_t task_id, struct epoll_event **events) {
-    task_list_t *t=&tasks[task_id];
+    task_list_t *t=tasks[task_id];
     uint64_t current_time=0;
 
     do {
@@ -266,13 +266,13 @@ extern "C" {
   }
 
   int itti_get_events(task_id_t task_id, struct epoll_event **events) {
-    pthread_mutex_lock(&tasks[task_id].queue_cond_lock);
+    pthread_mutex_lock(&tasks[task_id]->queue_cond_lock);
     return itti_get_events_locked(task_id, events);
   }
 
   void itti_receive_msg(task_id_t task_id, MessageDef **received_msg) {
     // Reception of one message, blocking caller
-    task_list_t *t=&tasks[task_id];
+    task_list_t *t=tasks[task_id];
     pthread_mutex_lock(&t->queue_cond_lock);
 
     // Weird condition to deal with crap legacy itti interface
@@ -304,7 +304,7 @@ extern "C" {
 
   void itti_poll_msg(task_id_t task_id, MessageDef **received_msg) {
     //reception of one message, non-blocking
-    task_list_t *t=&tasks[task_id];
+    task_list_t *t=tasks[task_id];
     pthread_mutex_lock(&t->queue_cond_lock);
 
     if (!t->message_queue.empty()) {
@@ -320,7 +320,7 @@ extern "C" {
   int itti_create_task(task_id_t task_id,
                        void *(*start_routine)(void *),
                        void *args_p) {
-    task_list_t *t=&tasks[task_id];
+    task_list_t *t=tasks[task_id];
     threadCreate (&t->thread, start_routine, args_p, (char *)itti_get_task_name(task_id),-1,OAI_PRIORITY_RT);
     LOG_I(TMR,"Created Posix thread %s\n",  itti_get_task_name(task_id) );
     return 0;
@@ -338,19 +338,20 @@ extern "C" {
 
   int itti_create_queue(const task_info_t *task_info) {
     pthread_mutex_lock (&lock_nb_queues);
-    int newQueue=nb_queues;
-    nb_queues++;
-    AssertFatal(realloc(tasks, nb_queues* sizeof(*tasks)),"no memory");
+    int newQueue=nb_queues++;
+    AssertFatal(tasks=(task_list_t **) realloc(tasks, nb_queues * sizeof(*tasks)),"");
+    tasks[newQueue]= new task_list_t;
     pthread_mutex_unlock (&lock_nb_queues);
     LOG_I(TMR,"Starting itti queue: %s as task %d\n", tasks_info->name, newQueue);
-    pthread_mutex_init(&tasks[newQueue].queue_cond_lock, NULL);
-    memcpy(&tasks[newQueue].admin, tasks_info, sizeof(task_info_t));
-    AssertFatal( ( tasks[newQueue].epoll_fd = epoll_create1(0) ) >=0, "");
-    AssertFatal( ( tasks[newQueue].sem_fd = eventfd(0, EFD_SEMAPHORE) ) >=0, "");
-    itti_subscribe_event_fd((task_id_t)newQueue, tasks[newQueue].sem_fd);
-
-    if (tasks[newQueue].admin.threadFunc != NULL)
-      itti_create_task((task_id_t)newQueue, tasks[newQueue].admin.threadFunc, NULL);
+    pthread_mutex_init(&tasks[newQueue]->queue_cond_lock, NULL);
+    memcpy(&tasks[newQueue]->admin, tasks_info, sizeof(task_info_t));
+    AssertFatal( ( tasks[newQueue]->epoll_fd = epoll_create1(0) ) >=0, "");
+    AssertFatal( ( tasks[newQueue]->sem_fd = eventfd(0, EFD_SEMAPHORE) ) >=0, "");
+    itti_subscribe_event_fd((task_id_t)newQueue, tasks[newQueue]->sem_fd);
+
+    if (tasks[newQueue]->admin.threadFunc != NULL)
+      itti_create_task((task_id_t)newQueue, tasks[newQueue]->admin.threadFunc, NULL);
+
     return newQueue;
   }
 
@@ -376,7 +377,7 @@ extern "C" {
     timer_type_t  type,
     void         *timer_arg,
     long         *timer_id) {
-    task_list_t *t=&tasks[task_id];
+    task_list_t *t=tasks[task_id];
 
     do {
       // set the taskid in the timer id to keep compatible with the legacy API
@@ -412,9 +413,9 @@ extern "C" {
   int timer_remove(long timer_id) {
     task_id_t task_id=(task_id_t)(timer_id&0xffff);
     int ret;
-    pthread_mutex_lock (&tasks[task_id].queue_cond_lock);
-    ret=tasks[task_id].timer_map.erase(timer_id);
-    pthread_mutex_unlock (&tasks[task_id].queue_cond_lock);
+    pthread_mutex_lock (&tasks[task_id]->queue_cond_lock);
+    ret=tasks[task_id]->timer_map.erase(timer_id);
+    pthread_mutex_unlock (&tasks[task_id]->queue_cond_lock);
 
     if (ret==1)
       return 0;
@@ -429,7 +430,7 @@ extern "C" {
   }
 
   const char *itti_get_task_name(task_id_t task_id) {
-    return tasks[task_id].admin.name;
+    return tasks[task_id]->admin.name;
   }
 
   // void for compatibility
diff --git a/openair2/ENB_APP/enb_config.c b/openair2/ENB_APP/enb_config.c
index 7a01d35d48d..b963af0f582 100644
--- a/openair2/ENB_APP/enb_config.c
+++ b/openair2/ENB_APP/enb_config.c
@@ -121,15 +121,14 @@ void RCconfig_L1(void) {
       RC.nb_L1_CC[j] = *(L1_ParamList.paramarray[j][L1_CC_IDX].uptr);
 
       if (RC.eNB[j] == NULL) {
-        RC.eNB[j]                       = (PHY_VARS_eNB **)malloc((1+MAX_NUM_CCs)*sizeof(PHY_VARS_eNB *));
+        RC.eNB[j]  = (PHY_VARS_eNB **)malloc((1+MAX_NUM_CCs)*sizeof(PHY_VARS_eNB *));
         LOG_I(PHY,"RC.eNB[%d] = %p\n",j,RC.eNB[j]);
         memset(RC.eNB[j],0,(1+MAX_NUM_CCs)*sizeof(PHY_VARS_eNB *));
       }
 
       for (i=0; i<RC.nb_L1_CC[j]; i++) {
         if (RC.eNB[j][i] == NULL) {
-          RC.eNB[j][i] = (PHY_VARS_eNB *)malloc(sizeof(PHY_VARS_eNB));
-          memset((void *)RC.eNB[j][i],0,sizeof(PHY_VARS_eNB));
+          RC.eNB[j][i] = (PHY_VARS_eNB *)calloc(1, sizeof(PHY_VARS_eNB));
           LOG_I(PHY,"RC.eNB[%d][%d] = %p\n",j,i,RC.eNB[j][i]);
           RC.eNB[j][i]->Mod_id  = j;
           RC.eNB[j][i]->CC_id   = i;
diff --git a/openair2/NETWORK_DRIVER/MESH/device.c b/openair2/NETWORK_DRIVER/MESH/device.c
index 5690b9b153c..181899cd6ec 100644
--- a/openair2/NETWORK_DRIVER/MESH/device.c
+++ b/openair2/NETWORK_DRIVER/MESH/device.c
@@ -297,7 +297,11 @@ int nas_change_mtu(struct net_device *dev, int mtu)
 }
 
 //---------------------------------------------------------------------------
+#if  LINUX_VERSION_CODE < KERNEL_VERSION(5,7,0)
 void nas_tx_timeout(struct net_device *dev)
+#else
+void nas_tx_timeout(struct net_device *dev, unsigned int x)
+#endif
 {
   //---------------------------------------------------------------------------
   // Transmitter timeout, serious problems.
diff --git a/openair2/RRC/NR/rrc_gNB_nsa.c b/openair2/RRC/NR/rrc_gNB_nsa.c
index aa9b9df8f2a..419dcc47e15 100644
--- a/openair2/RRC/NR/rrc_gNB_nsa.c
+++ b/openair2/RRC/NR/rrc_gNB_nsa.c
@@ -151,7 +151,7 @@ void rrc_add_nsa_user(gNB_RRC_INST *rrc,struct rrc_gNB_ue_context_s *ue_context_
   msg = itti_alloc_new_message(TASK_RRC_ENB, 0, X2AP_ENDC_SGNB_ADDITION_REQ_ACK);
   gtpv1u_enb_create_tunnel_req_t  create_tunnel_req;
   gtpv1u_enb_create_tunnel_resp_t create_tunnel_resp;
-  protocol_ctxt_t ctxt;
+  protocol_ctxt_t ctxt={0};
   // NR RRCReconfiguration
   AssertFatal(rrc->Nb_ue < MAX_NR_RRC_UE_CONTEXTS,"cannot add another UE\n");
   ue_context_p->ue_context.reconfig = calloc(1,sizeof(NR_RRCReconfiguration_t));
diff --git a/openair3/SCTP/sctp_eNB_task.c b/openair3/SCTP/sctp_eNB_task.c
index 0193c50e1d2..2f732443bea 100644
--- a/openair3/SCTP/sctp_eNB_task.c
+++ b/openair3/SCTP/sctp_eNB_task.c
@@ -364,7 +364,7 @@ sctp_handle_new_association_req(
     int                           sd       = 0;
     int32_t                       assoc_id = 0;
 
-    struct sctp_event_subscribe   events;
+    struct sctp_event_subscribe   events={0};
 
     struct sctp_cnx_list_elm_s   *sctp_cnx = NULL;
     enum sctp_connection_type_e   connection_type = SCTP_TYPE_CLIENT;
diff --git a/targets/ARCH/rfsimulator/apply_channelmod.c b/targets/ARCH/rfsimulator/apply_channelmod.c
index 7aa5d78bf85..3f4c0e1517e 100644
--- a/targets/ARCH/rfsimulator/apply_channelmod.c
+++ b/targets/ARCH/rfsimulator/apply_channelmod.c
@@ -102,8 +102,9 @@ void rxAddInput( struct complex16 *input_sig, struct complex16 *after_channel_si
       } //l
     }
 
-    out_ptr->r += round(rx_tmp.x*pathLossLinear + noise_per_sample*gaussZiggurat(0.0,1.0));
-    out_ptr->i += round(rx_tmp.y*pathLossLinear + noise_per_sample*gaussZiggurat(0.0,1.0));
+    // Fixme: lround(), rount(), ... is detected by valgrind as error, not found why
+    out_ptr->r += lround(rx_tmp.x*pathLossLinear + noise_per_sample*gaussZiggurat(0.0,1.0));
+    out_ptr->i += lround(rx_tmp.y*pathLossLinear + noise_per_sample*gaussZiggurat(0.0,1.0));
     out_ptr++;
   }
 
diff --git a/targets/RT/USER/lte-enb.c b/targets/RT/USER/lte-enb.c
index 1347d75fb06..53167058091 100644
--- a/targets/RT/USER/lte-enb.c
+++ b/targets/RT/USER/lte-enb.c
@@ -1239,7 +1239,8 @@ void init_eNB(int single_thread_flag,
     if (RC.eNB[inst] == NULL) RC.eNB[inst] = (PHY_VARS_eNB **) malloc(RC.nb_CC[inst]*sizeof(PHY_VARS_eNB *));
 
     for (CC_id=0; CC_id<RC.nb_L1_CC[inst]; CC_id++) {
-      if (RC.eNB[inst][CC_id] == NULL) RC.eNB[inst][CC_id] = (PHY_VARS_eNB *) malloc(sizeof(PHY_VARS_eNB));
+      if (RC.eNB[inst][CC_id] == NULL) 
+         RC.eNB[inst][CC_id] = (PHY_VARS_eNB *) calloc(1,sizeof(PHY_VARS_eNB));
 
       eNB                     = RC.eNB[inst][CC_id];
       eNB->abstraction_flag   = 0;
-- 
GitLab