Add CAcloseSslConnectionFreeEndpoint method 60/201660/1
authorRobert Chudek <r.chudek@partner.samsung.com>
Tue, 12 Feb 2019 10:28:29 +0000 (11:28 +0100)
committerAbhishek Sansanwal <abhishek.s94@samsung.com>
Mon, 18 Mar 2019 10:27:32 +0000 (15:57 +0530)
[Model] IoTivity
[BinType] AP
[Customer] IoT-Core

[Issue#] CONPRO-1392
[Request] Robert Chudek / r.chudek
[Occurrence Version] n/a

[Problem] After calling CAcloseSslConnection there was a memory leak.
[Cause & Measure] Cause: After calling CAcloseSslConnection endpoint wasn't freed which led to memory leak.
[Checking Method] n/a

[Team] IoT Connection
[Developer] Robert Chudek / r.chudek
[Solution company] Samsung
[Change Type] bug fix

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/414
(cherry picked from 18a20af57324bc3d816a862e3aa5194712d2e237)

Change-Id: I283055bea913401c6c23953857b7ccfe5db3581b
Signed-off-by: Abhishek Sansanwal <abhishek.s94@samsung.com>
resource/csdk/connectivity/api/casecurityinterface.h
resource/csdk/connectivity/inc/ca_adapter_net_ssl.h
resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c
resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c

index c891460..117ab18 100644 (file)
@@ -324,6 +324,17 @@ CAResult_t CAinitiateSslHandshake(const CAEndpoint_t *endpoint);
 CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint);
 
 /**
+ * Close the DTLS session and free endpoint.
+ *
+ * @param[in] endpoint  information of network address;
+ *            CAcloseSslConnectionWrapper takes ownership of endpoint
+ *
+ * @retval  ::CA_STATUS_OK    Successful.
+ * @retval  ::CA_STATUS_FAILED Operation failed.
+ */
+CAResult_t CAcloseSslConnectionFreeEndpoint(CAEndpoint_t *endpoint);
+
+/**
  * Close the TLS session using UUID
  *
  * @param[in] identity  UUID of target device
index cc9e8d5..4dd2510 100644 (file)
@@ -90,6 +90,17 @@ void CAsetSslCredentialsCallback(CAgetPskCredentialsHandler credCallback);
 CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint);
 
 /**
+ * Close the DTLS session and free endpoint.
+ *
+ * @param[in] endpoint  information of network address;
+ *            CAcloseSslConnectionWrapper takes ownership of endpoint
+ *
+ * @retval  ::CA_STATUS_OK    Successful.
+ * @retval  ::CA_STATUS_FAILED Operation failed.
+ */
+CAResult_t CAcloseSslConnectionFreeEndpoint(CAEndpoint_t *endpoint);
+
+/**
  * initialize mbedTLS library and other necessary initialization.
  *
  * @return  0 on success otherwise a positive error value.
index 187d23c..2553e5b 100644 (file)
@@ -1505,6 +1505,19 @@ CAResult_t CAcloseSslConnection(const CAEndpoint_t *endpoint)
     return CA_STATUS_OK;
 }
 
+CAResult_t CAcloseSslConnectionFreeEndpoint(CAEndpoint_t *endpoint)
+{
+    OIC_LOG_V(INFO, NET_SSL_TAG, "In %s", __func__);
+    VERIFY_NON_NULL_RET(endpoint, NET_SSL_TAG, "Param endpoint is NULL" , CA_STATUS_INVALID_PARAM);
+
+    CAResult_t ret = CAcloseSslConnection(endpoint);
+    OICFree(endpoint);
+
+    OIC_LOG_V(INFO, NET_SSL_TAG, "Out %s", __func__);
+    return ret;
+}
+
+
 CAResult_t CAcloseSslConnectionUsingUuid(const uint8_t *identity, size_t idLength)
 {
     OIC_LOG_V(DEBUG, NET_SSL_TAG, "In %s", __func__);
index 78e4c12..93c8bde 100644 (file)
@@ -3194,8 +3194,54 @@ static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const cha
 #endif
 
 #ifdef __WITH_DTLS__
+#ifndef SINGLE_THREAD
+        // CAcloseSslConnection returns CAResult_t instead of void*, but the size is the same and crash shouldn't occur
+        pthread_t ccThread;
+        pthread_attr_t attr;
+        int initAttrRes = -1;
+        int pthreadCreateRes = -1;
+        int detachStatusRes = -1;
+        int memoryAllocationRes = -1;
+
+        do
+        {
+            initAttrRes = pthread_attr_init(&attr);
+            if (initAttrRes != 0)
+            {
+                break;
+            }
+            CAEndpoint_t *localEndpointCpyPtr = OICMalloc(sizeof(CAEndpoint_t));
+
+            if(NULL == localEndpointCpyPtr)
+            {
+                memoryAllocationRes = -1;
+                break;
+            }
+            else
+            {
+                memoryAllocationRes = 0;
+            }
+
+            (*localEndpointCpyPtr) = localEndpoint;
+            // this piece of code is reached on the main thread
+            // CAcloseSslConnection might wait for too long (network + mutexes) and watchdog might kill it
+            // Asynchronous call protects this function from watchdog
+            pthreadCreateRes = pthread_create(&ccThread, &attr, (void *(*)(void*))&CAcloseSslConnectionFreeEndpoint, (void*)localEndpointCpyPtr);
+
+            if (pthreadCreateRes != 0)
+            {
+                break;
+            }
+            detachStatusRes = pthread_detach(ccThread);
+        }while (0);
+
+        // regardless of CAcloseSslConnection result, the function will continue and g_connectionCallback will be called
+        OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "CAcloseSslConnection pthread_init [%d], mem_alloc [%d] pthread_create [%d], pthread_detach [%d]",
+                initAttrRes, memoryAllocationRes, pthreadCreateRes, detachStatusRes);
+#else
         CAcloseSslConnection(&localEndpoint);
 #endif
+#endif
     }
 
     if (g_connectionCallback)