Resolved SDOCM00112612: Memory leak when extracting resources via Linux DTB
authorJustin Sobota <jsobota@ti.com>
Fri, 15 Aug 2014 18:03:46 +0000 (14:03 -0400)
committerJustin Sobota <jsobota@ti.com>
Fri, 15 Aug 2014 18:03:46 +0000 (14:03 -0400)
docs/ReleaseNotes_RM.doc
docs/ReleaseNotes_RM.pdf
package.xdc
src/rm_allocator.c
test/armv7/linux/rm_linux_osal.c
test/armv7/linux/rm_server_osal.c
test/k2e/c66/bios/rm_osal.c
test/k2h/c66/bios/rm_osal.c
test/k2k/c66/bios/rm_osal.c
test/k2l/c66/bios/rm_osal.c
test/src/rm_mem_test.c

index 5e1070a63b0901beb2408b3e362ff049e847f289..a1f42ce2cb1e14468fe01873b990e9772aba8d4b 100644 (file)
Binary files a/docs/ReleaseNotes_RM.doc and b/docs/ReleaseNotes_RM.doc differ
index 3beb3d7fb83e7b7eb1833e7c43768c30fab41a2b..0fdec10be3f35a9babbb8460998c82a20a8724c3 100644 (file)
Binary files a/docs/ReleaseNotes_RM.pdf and b/docs/ReleaseNotes_RM.pdf differ
index edcbdbfe4ab1ed149a48ab51e25db47816d10143..6b95a64cb1aed2aeed7f4168f18b9d51f5e20fd6 100755 (executable)
@@ -9,6 +9,6 @@
  * Copyright (C) 2012-2014, Texas Instruments, Inc.\r
  *****************************************************************************/\r
 \r
-package ti.drv.rm[02, 01, 00, 05] {\r
+package ti.drv.rm[02, 01, 00, 06] {\r
     module Settings;\r
 }\r
index e0e4db6b808d3df319ea0248e696441db38297ab..ac351569d4e56b9ce37355cbc4e0f4ee73e6aef6 100644 (file)
@@ -1225,6 +1225,7 @@ static int32_t allocatorFindLinuxResource(Rm_Handle rmHandle, const char *resour
     Rm_ResourceInfo     resourceInfo;
     uint32_t            pathOffset;
     uint32_t            pathSize;
+    char               *tempAliasPath;
     char               *spacePtr;
     int32_t             propOffset;
     int32_t             nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;
@@ -1249,19 +1250,21 @@ static int32_t allocatorFindLinuxResource(Rm_Handle rmHandle, const char *resour
         /* Reset parsing variables */
         pathOffset = 0;
         pathSize = strlen(linuxAlias->path) + 1;
+        tempAliasPath = Rm_osalMalloc(pathSize);
+        rm_strncpy(tempAliasPath, linuxAlias->path, pathSize);
         nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;
         prevDepth = RM_DTB_UTIL_STARTING_DEPTH;   
         resourceInfo.base = 0;
         resourceInfo.length = 0;
 
-        spacePtr = strpbrk(linuxAlias->path, " ");
+        spacePtr = strpbrk(tempAliasPath, " ");
         if (spacePtr) {
             *spacePtr = '\0';
         }       
         
         while(pathOffset < pathSize) {
             /* Move through DTB nodes until next alias path node found */
-            if (strcmp(linuxAlias->path + pathOffset, fdt_get_name(linuxDtb, nodeOffset, NULL))) {
+            if (strcmp(tempAliasPath + pathOffset, fdt_get_name(linuxDtb, nodeOffset, NULL))) {
                 nodeOffset = fdt_next_node(linuxDtb, nodeOffset, &depth);
 
                 if ((depth < prevDepth) || (nodeOffset == -FDT_ERR_NOTFOUND)) {
@@ -1273,8 +1276,8 @@ static int32_t allocatorFindLinuxResource(Rm_Handle rmHandle, const char *resour
             }
             else {
                 /* Found next alias path node.  Move to next node name in path string. */
-                pathOffset += (strlen(linuxAlias->path + pathOffset) + 1);
-                spacePtr = strpbrk(linuxAlias->path + pathOffset, " ");
+                pathOffset += (strlen(tempAliasPath + pathOffset) + 1);
+                spacePtr = strpbrk(tempAliasPath + pathOffset, " ");
                 if (spacePtr) {
                     *spacePtr = '\0';
                 }       
@@ -1286,9 +1289,9 @@ static int32_t allocatorFindLinuxResource(Rm_Handle rmHandle, const char *resour
                     propertyData = fdt_getprop_by_offset(linuxDtb, propOffset, 
                                                          &propertyName, &propertyLen);
 
-                    if (strcmp(linuxAlias->path + pathOffset, propertyName) == 0) {
+                    if (strcmp(tempAliasPath + pathOffset, propertyName) == 0) {
                         /* Found resource at end of alias path */
-                        pathOffset += (strlen(linuxAlias->path + pathOffset) + 1);
+                        pathOffset += (strlen(tempAliasPath + pathOffset) + 1);
                         linuxValueRange = rmDtbUtilLinuxExtractValues(propertyData, propertyLen);
                         retVal = allocatorReserveLinuxResource(rmHandle, linuxAlias, 
                                                                linuxValueRange, &opInfo);
@@ -1303,7 +1306,8 @@ static int32_t allocatorFindLinuxResource(Rm_Handle rmHandle, const char *resour
                 }
             }
         }
-
+        
+        Rm_osalFree(tempAliasPath, pathSize);
         if (retVal < RM_OK) {
             break;
         }
index 8128fbb2844200eccc2092d27ca202c8b4f54b6b..03fa8aae692b4244dc3b7f8a654a9ad115a78199 100644 (file)
@@ -6,7 +6,7 @@
  *
  *  \par
  *  ============================================================================
- *  @n   (C) Copyright 2012-2013, Texas Instruments, Inc.
+ *  @n   (C) Copyright 2012-2014, Texas Instruments, Inc.
  *
  *  Redistribution and use in source and binary forms, with or without
  *  modification, are permitted provided that the following conditions
@@ -54,6 +54,9 @@
 uint32_t rmMallocCounter = 0;
 uint32_t rmFreeCounter   = 0;
 
+int32_t rmByteAlloc = 0;
+int32_t rmByteFree = 0;
+
 /**********************************************************************
  *************************** OSAL Functions **************************
  **********************************************************************/
@@ -67,6 +70,7 @@ void *Osal_rmMalloc (uint32_t num_bytes)
 {
     /* Increment the allocation counter. */
     rmMallocCounter++;
+    rmByteAlloc += num_bytes;
 
        /* Allocate memory. */
        return calloc(1, num_bytes);
@@ -81,6 +85,7 @@ void Osal_rmFree (void *ptr, uint32_t size)
 {
     /* Increment the free counter. */
     rmFreeCounter++;
+    rmByteFree += size;
        free(ptr);
 }
 
index e8c16225ddad36a005bc37d307733b1664fbe364..35329e600104ebc4b508e7e6db18ddd6b5148912 100644 (file)
@@ -58,6 +58,9 @@
 uint32_t rmMallocCounter = 0;
 uint32_t rmFreeCounter   = 0;
 
+int32_t rmByteAlloc = 0;
+int32_t rmByteFree = 0;
+
 /**********************************************************************
  ********************** External Variables ****************************
  **********************************************************************/
@@ -78,6 +81,7 @@ void *Osal_rmMalloc (uint32_t num_bytes)
 {
     /* Increment the allocation counter. */
     rmMallocCounter++;
+    rmByteAlloc += num_bytes;
 
        /* Allocate memory. */
        return calloc(1, num_bytes);
@@ -92,6 +96,7 @@ void Osal_rmFree (void *ptr, uint32_t size)
 {
     /* Increment the free counter. */
     rmFreeCounter++;
+    rmByteFree += size;
        free(ptr);
 }
 
index c8bfc3a64fc1b5de797a18e5a12dc9255e5637db..6b8d806b0068cf96d8160570c0937340fd24ddef 100644 (file)
@@ -67,6 +67,9 @@
 uint32_t rmMallocCounter = 0;
 uint32_t rmFreeCounter   = 0;
 
+int32_t rmByteAlloc = 0;
+int32_t rmByteFree = 0;
+
 /**********************************************************************
  *************************** OSAL Functions **************************
  **********************************************************************/
@@ -82,6 +85,7 @@ void *Osal_rmMalloc (uint32_t num_bytes)
 
     /* Increment the allocation counter. */
     rmMallocCounter++;
+    rmByteAlloc += num_bytes;
 
        /* Allocate memory. */
        return Memory_alloc(NULL, num_bytes, 0, &errorBlock);
@@ -96,6 +100,7 @@ void Osal_rmFree (void *ptr, uint32_t size)
 {
     /* Increment the free counter. */
     rmFreeCounter++;
+    rmByteFree += size;
        Memory_free(NULL, ptr, size);
 }
 
index c8bfc3a64fc1b5de797a18e5a12dc9255e5637db..6b8d806b0068cf96d8160570c0937340fd24ddef 100644 (file)
@@ -67,6 +67,9 @@
 uint32_t rmMallocCounter = 0;
 uint32_t rmFreeCounter   = 0;
 
+int32_t rmByteAlloc = 0;
+int32_t rmByteFree = 0;
+
 /**********************************************************************
  *************************** OSAL Functions **************************
  **********************************************************************/
@@ -82,6 +85,7 @@ void *Osal_rmMalloc (uint32_t num_bytes)
 
     /* Increment the allocation counter. */
     rmMallocCounter++;
+    rmByteAlloc += num_bytes;
 
        /* Allocate memory. */
        return Memory_alloc(NULL, num_bytes, 0, &errorBlock);
@@ -96,6 +100,7 @@ void Osal_rmFree (void *ptr, uint32_t size)
 {
     /* Increment the free counter. */
     rmFreeCounter++;
+    rmByteFree += size;
        Memory_free(NULL, ptr, size);
 }
 
index c8bfc3a64fc1b5de797a18e5a12dc9255e5637db..6b8d806b0068cf96d8160570c0937340fd24ddef 100644 (file)
@@ -67,6 +67,9 @@
 uint32_t rmMallocCounter = 0;
 uint32_t rmFreeCounter   = 0;
 
+int32_t rmByteAlloc = 0;
+int32_t rmByteFree = 0;
+
 /**********************************************************************
  *************************** OSAL Functions **************************
  **********************************************************************/
@@ -82,6 +85,7 @@ void *Osal_rmMalloc (uint32_t num_bytes)
 
     /* Increment the allocation counter. */
     rmMallocCounter++;
+    rmByteAlloc += num_bytes;
 
        /* Allocate memory. */
        return Memory_alloc(NULL, num_bytes, 0, &errorBlock);
@@ -96,6 +100,7 @@ void Osal_rmFree (void *ptr, uint32_t size)
 {
     /* Increment the free counter. */
     rmFreeCounter++;
+    rmByteFree += size;
        Memory_free(NULL, ptr, size);
 }
 
index c8bfc3a64fc1b5de797a18e5a12dc9255e5637db..6b8d806b0068cf96d8160570c0937340fd24ddef 100644 (file)
@@ -67,6 +67,9 @@
 uint32_t rmMallocCounter = 0;
 uint32_t rmFreeCounter   = 0;
 
+int32_t rmByteAlloc = 0;
+int32_t rmByteFree = 0;
+
 /**********************************************************************
  *************************** OSAL Functions **************************
  **********************************************************************/
@@ -82,6 +85,7 @@ void *Osal_rmMalloc (uint32_t num_bytes)
 
     /* Increment the allocation counter. */
     rmMallocCounter++;
+    rmByteAlloc += num_bytes;
 
        /* Allocate memory. */
        return Memory_alloc(NULL, num_bytes, 0, &errorBlock);
@@ -96,6 +100,7 @@ void Osal_rmFree (void *ptr, uint32_t size)
 {
     /* Increment the free counter. */
     rmFreeCounter++;
+    rmByteFree += size;
        Memory_free(NULL, ptr, size);
 }
 
index da276310227ed3896487a5b82471b1445846c03f..539f4148f120047cb4d104d405d079424b00c94d 100644 (file)
@@ -104,6 +104,8 @@ extern const char rmLinuxDtb[];
 /* Alloc and free OSAL variables */
 extern uint32_t rmMallocCounter;
 extern uint32_t rmFreeCounter;
+extern int32_t rmByteAlloc;
+extern int32_t rmByteFree;
 
 extern void *Osal_rmMalloc (uint32_t num_bytes);
 extern void  Osal_rmFree (void *ptr, uint32_t size);
@@ -141,8 +143,8 @@ Rm_Handle           rmHandle = NULL;
 char                rmInstName[RM_NAME_MAX_CHARS];
 
 /* Malloc/Free tracking variables */
-uint32_t            sMalloc, mMalloc;
-uint32_t            sFree, mFree;
+uint32_t            sMalloc, mMalloc, sByteAlloc, mByteAlloc;
+uint32_t            sFree, mFree, sByteFree, mByteFree;
 
 /* RM response info queue used to store service responses received via the callback function */
 Rm_ServiceRespInfo  responseInfoQueue[MAX_QUEUED_SERVICE_RESPONSES];
@@ -167,53 +169,56 @@ volatile int8_t   isRmInitialized[128];
  **********************************************************************/
 
 /* Error checking macro */
-#define ERROR_CHECK(checkVal, resultVal, rmInstName, printMsg)                   \
-    if (resultVal != checkVal) {                                                 \
-        char errorMsgToPrint[] = printMsg;                                       \
-        System_printf("Error Core %d : %s : ", coreNum, rmInstName);             \
-        System_printf("%s with error code : %d\n", errorMsgToPrint, resultVal);  \
-        testErrors++;                                                            \
-        System_abort("Test Failure\n");                                          \
+#define ERROR_CHECK(checkVal, resultVal, rmInstName, printMsg)                  \
+    if (resultVal != checkVal) {                                                \
+        char errorMsgToPrint[] = printMsg;                                      \
+        System_printf("Error Core %d : %s : ", coreNum, rmInstName);            \
+        System_printf("%s with error code : %d\n", errorMsgToPrint, resultVal); \
+        testErrors++;                                                           \
+        System_abort("Test Failure\n");                                         \
     }
 
-#define MEM_TEST_START_STORE() sMalloc = rmMallocCounter; sFree = rmFreeCounter; 
-#define MEM_TEST_MID_STORE()   mMalloc = rmMallocCounter; mFree = rmFreeCounter; 
-#define MEM_TEST_END_PRINT(title, sApiOrFunc, mApiOrFunc, eApiOrFunc)                               \
-    do {                                                                                            \
-        char    titleMsg[] = title;                                                                 \
-        char    sApiOrFuncMsg[] = sApiOrFunc;                                                       \
-        char    mApiOrFuncMsg[] = mApiOrFunc;                                                       \
-        char    eApiOrFuncMsg[] = eApiOrFunc;                                                       \
-        int32_t mallocFreeBalance = (rmMallocCounter - sMalloc) - (rmFreeCounter - sFree);          \
-                                                                                                    \
-        System_printf ("Core %d : ---------------------------------------------------------\n",     \
-                       coreNum);                                                                    \
-        System_printf ("Core %d : %s\n", coreNum, titleMsg);                                        \
-        System_printf ("Core %d : - API/Functionality     : malloc count   |   free count -\n",     \
-                       coreNum);                                                                    \
-        System_printf ("Core %d : - %s :  %6d        |  %6d      -\n", coreNum, sApiOrFuncMsg,      \
-                       sMalloc, sFree);                                                             \
-        System_printf ("Core %d : - %s :  %6d        |  %6d      -\n", coreNum, mApiOrFuncMsg,      \
-                       mMalloc, mFree);                                                             \
-        System_printf ("Core %d : - %s :  %6d        |  %6d      -\n", coreNum,                     \
-                       eApiOrFuncMsg, rmMallocCounter, rmFreeCounter);                              \
-        if (mallocFreeBalance > 0) {                                                                \
-            System_printf ("Core %d : - FAILED - %6d unfreed mallocs                       -\n",    \
-                           coreNum, mallocFreeBalance);                                             \
-            testErrors++;                                                                           \
-        }                                                                                           \
-        else if (mallocFreeBalance < 0) {                                                           \
-            System_printf ("Core %d : - FAILED - %6d more frees than mallocs               -\n",    \
-                           coreNum, -mallocFreeBalance);                                            \
-            testErrors++;                                                                           \
-        }                                                                                           \
-        else {                                                                                      \
-            System_printf ("Core %d : - PASSED                                                -\n", \
-                           coreNum);                                                                \
-        }                                                                                           \
-        System_printf ("Core %d : ---------------------------------------------------------\n",     \
-                       coreNum);                                                                    \
-        System_printf ("\n");                                                                       \
+#define MEM_TEST_START_STORE() sMalloc = rmMallocCounter; sFree = rmFreeCounter; \
+                               sByteAlloc = rmByteAlloc;  sByteFree = rmByteFree;
+#define MEM_TEST_MID_STORE()   mMalloc = rmMallocCounter; mFree = rmFreeCounter; \
+                               mByteAlloc = rmByteAlloc;  mByteFree = rmByteFree;
+#define MEM_TEST_END_PRINT(title, sApiOrFunc, mApiOrFunc, eApiOrFunc)                                           \
+    do {                                                                                                        \
+        char    titleMsg[] = title;                                                                             \
+        char    sApiOrFuncMsg[] = sApiOrFunc;                                                                   \
+        char    mApiOrFuncMsg[] = mApiOrFunc;                                                                   \
+        char    eApiOrFuncMsg[] = eApiOrFunc;                                                                   \
+        int32_t mallocFreeBalance = (rmMallocCounter - sMalloc) - (rmFreeCounter - sFree);                      \
+        int32_t mallocByteBalance = (rmByteAlloc - sByteAlloc) - (rmByteFree - sByteFree);                      \
+                                                                                                                \
+        System_printf ("Core %d : ---------------------------------------------------------------------\n",     \
+                       coreNum);                                                                                \
+        System_printf ("Core %d : ------%s------\n", coreNum, titleMsg);                                        \
+        System_printf ("Core %d : - API/Functionality     :   malloc count (b)  |    free count (b)   -\n",     \
+                       coreNum);                                                                                \
+        System_printf ("Core %d : - %s :  %6d (%8d)  |  %6d (%8d)  -\n", coreNum, sApiOrFuncMsg,                \
+                       sMalloc, sByteAlloc, sFree, sByteFree);                                                  \
+        System_printf ("Core %d : - %s :  %6d (%8d)  |  %6d (%8d)  -\n", coreNum, mApiOrFuncMsg,                \
+                       mMalloc, mByteAlloc, mFree, mByteFree);                                                  \
+        System_printf ("Core %d : - %s :  %6d (%8d)  |  %6d (%8d)  -\n", coreNum,                               \
+                       eApiOrFuncMsg, rmMallocCounter, rmByteAlloc, rmFreeCounter, rmByteFree);                 \
+        if ((mallocFreeBalance > 0) || (mallocByteBalance >0)) {                                                \
+            System_printf ("Core %d : - FAILED - %6d (%8d) unfreed memory                         -\n",         \
+                           coreNum, mallocFreeBalance, mallocByteBalance);                                      \
+            testErrors++;                                                                                       \
+        }                                                                                                       \
+        else if ((mallocFreeBalance < 0) || (mallocByteBalance < 0)) {                                          \
+            System_printf ("Core %d : - FAILED - %6d (%8d) over freeing memory                    -\n",         \
+                           coreNum, -mallocFreeBalance, -mallocByteBalance);                                    \
+            testErrors++;                                                                                       \
+        }                                                                                                       \
+        else {                                                                                                  \
+            System_printf ("Core %d : - PASSED                                                            -\n", \
+                           coreNum);                                                                            \
+        }                                                                                                       \
+        System_printf ("Core %d : ---------------------------------------------------------------------\n",     \
+                       coreNum);                                                                                \
+        System_printf ("\n");                                                                                   \
     } while(0)
 
 
@@ -278,7 +283,7 @@ void setRmRequest(Rm_ServiceReqInfo *reqInfo, Rm_ServiceType type, const char *r
 void rmCleanupTsk(UArg arg0, UArg arg1)
 {
     int32_t rmResult;
-    int32_t finalMallocFree;    
+    int32_t finalMallocFree, finalByteFree;    
     
     /* Delete the RM test tasks */
     System_printf("Core %d: Deleting RM startup task...\n", coreNum);
@@ -311,31 +316,32 @@ void rmCleanupTsk(UArg arg0, UArg arg1)
     rmResult = Rm_delete(rmHandle, RM_TEST_TRUE);
     ERROR_CHECK(RM_OK, rmResult, rmInstName, "Delete failed");
 
-    System_printf ("Core %d : ---------------------------------------------------------\n", coreNum);
-    System_printf ("Core %d : ------------------ Example Completion -------------------\n", coreNum);
-    System_printf ("Core %d : - API/Functionality     : malloc count   |   free count -\n", coreNum);
-    System_printf ("Core %d : - Example Completion    :  %6d        |  %6d      -\n", coreNum,
-                   rmMallocCounter, rmFreeCounter);
+    System_printf ("Core %d : ---------------------------------------------------------------------\n", coreNum);
+    System_printf ("Core %d : ------------------------- Example Completion ------------------------\n", coreNum);
+    System_printf ("Core %d : - API/Functionality     :   malloc count (b)  |    free count (b)   -\n", coreNum);
+    System_printf ("Core %d : - Example Completion    :  %6d (%8d)  |  %6d (%8d)  -\n", coreNum,
+                   rmMallocCounter, rmByteAlloc, rmFreeCounter, rmByteFree);
     finalMallocFree = rmMallocCounter - rmFreeCounter;
-    if (finalMallocFree > 0) {
-        System_printf ("Core %d : - FAILED - %6d unfreed mallocs                       -\n",
-                       coreNum, finalMallocFree);
+    finalByteFree = rmByteAlloc - rmByteFree;
+    if ((finalMallocFree > 0) || (finalByteFree > 0)) {
+        System_printf ("Core %d : - FAILED - %6d (%8d) unfreed memory                         -\n",
+                       coreNum, finalMallocFree, finalByteFree);
         testErrors++;
     }
-    else if (finalMallocFree < 0) {
-        System_printf ("Core %d : - FAILED - %6d more frees than mallocs               -\n",
-                       coreNum, -finalMallocFree);
+    else if ((finalMallocFree < 0) || (finalByteFree < 0)) {
+        System_printf ("Core %d : - FAILED - %6d (%8d) over freeing memory                    -\n",
+                       coreNum, -finalMallocFree, finalByteFree);
         testErrors++;
     }
     else {
-        System_printf ("Core %d : - PASSED                                                -\n",
+        System_printf ("Core %d : - PASSED                                                            -\n",
                        coreNum);
     }    
     if (testErrors) {
-        System_printf ("Core %d : -                                                       -\n", coreNum); 
-        System_printf ("Core %d : - Test Errors: %-32d         -\n", coreNum, testErrors);
+        System_printf ("Core %d : -                                                                   -\n", coreNum); 
+        System_printf ("Core %d : - Test Errors: %-32d                     -\n", coreNum, testErrors);
     }    
-    System_printf ("Core %d : ---------------------------------------------------------\n", coreNum);
+    System_printf ("Core %d : ---------------------------------------------------------------------\n", coreNum);
     System_printf ("\n");  
 
     BIOS_exit(0);