QNX IPC: Samples - Only Open SharedMemLib Once
authorAngela Stegmaier <angelabaker@ti.com>
Fri, 3 May 2013 14:09:33 +0000 (09:09 -0500)
committerAngela Stegmaier <angelabaker@ti.com>
Sat, 4 May 2013 15:48:55 +0000 (10:48 -0500)
A race condition in the rpmsg-rpc stress test could
result in one thread trying to access a NULL pointer
as there was no protection around the call to
initialize the sharedmemallocator library functions.

To avoid the race condition, move the initialization
of the sharedmemallocator functions to the main function,
to happen only once.

Signed-off-by: Angela Stegmaier <angelabaker@ti.com>
qnx/src/ipc3x_dev/ti/syslink/samples/hlos/rpmsg-rpc-stress/usr/tests_rpc_stress.c

index 23ca57baca4b0faa9deaecdc943c81043fab67e8..f79438dc836690644e1cbebf59babfaa157ae0e6 100644 (file)
@@ -235,54 +235,46 @@ void * test_exec_call(void * arg)
                 function->num_translations = 0;
                 break;
             case FXN_IDX_FXNADD3:
-                if (init_sharedmem_funcs() < 0)
+                if ((*sharedmem_alloc)(sizeof(fxn_add3_args), &buf) < 0) {
                     test_status = -1;
+                }
                 else {
-                    if ((*sharedmem_alloc)(sizeof(fxn_add3_args), &buf) < 0) {
-                        test_status = -1;
-                    }
-                    else {
-                        ptr = (fxn_add3_args *)(buf.vir_addr);
-                        ((fxn_add3_args *)ptr)->a = i;
-                        ((fxn_add3_args *)ptr)->b = i+1;
-                        ((fxn_add3_args *)ptr)->c = i+2;
-                        function->num_params = 1;
-                        function->params[0].type = RPPC_PARAM_TYPE_PTR;
-                        function->params[0].size = sizeof(fxn_add3_args);
-                        function->params[0].data = (size_t)ptr;
-                        function->params[0].base = (size_t)ptr;
-                        function->num_translations = 0;
-                    }
+                    ptr = (fxn_add3_args *)(buf.vir_addr);
+                    ((fxn_add3_args *)ptr)->a = i;
+                    ((fxn_add3_args *)ptr)->b = i+1;
+                    ((fxn_add3_args *)ptr)->c = i+2;
+                    function->num_params = 1;
+                    function->params[0].type = RPPC_PARAM_TYPE_PTR;
+                    function->params[0].size = sizeof(fxn_add3_args);
+                    function->params[0].data = (size_t)ptr;
+                    function->params[0].base = (size_t)ptr;
+                    function->num_translations = 0;
                 }
                 break;
             case FXN_IDX_FXNADDX:
-                if (init_sharedmem_funcs() < 0)
+                if ((*sharedmem_alloc)(sizeof(fxn_addx_args), &buf) < 0) {
                     test_status = -1;
+                }
+                else if ((*sharedmem_alloc)(sizeof(int) * 3, &buf2) < 0) {
+                    test_status = -1;
+                }
                 else {
-                    if ((*sharedmem_alloc)(sizeof(fxn_addx_args), &buf) < 0) {
-                        test_status = -1;
-                    }
-                    else if ((*sharedmem_alloc)(sizeof(int) * 3, &buf2) < 0) {
-                        test_status = -1;
-                    }
-                    else {
-                        ptr = (fxn_addx_args *)(buf.vir_addr);
-                        ptr2 = (int *)(buf2.vir_addr);
-                        ((fxn_addx_args *)ptr)->num = 3;
-                        ((fxn_addx_args *)ptr)->array = ptr2;
-                        ((int *)ptr2)[0] = i;
-                        ((int *)ptr2)[1] = i+1;
-                        ((int *)ptr2)[2] = i+2;
-                        function->num_params = 1;
-                        function->params[0].type = RPPC_PARAM_TYPE_PTR;
-                        function->params[0].size = sizeof(fxn_addx_args);
-                        function->params[0].data = (size_t)ptr;
-                        function->params[0].base = (size_t)ptr;
-                        function->num_translations = 1;
-                        function->translations[0].index = 0;
-                        function->translations[0].offset = (int)&(((fxn_addx_args *)ptr)->array) - (int)ptr;
-                        function->translations[0].base = ((fxn_addx_args *)ptr)->array;
-                    }
+                    ptr = (fxn_addx_args *)(buf.vir_addr);
+                    ptr2 = (int *)(buf2.vir_addr);
+                    ((fxn_addx_args *)ptr)->num = 3;
+                    ((fxn_addx_args *)ptr)->array = ptr2;
+                    ((int *)ptr2)[0] = i;
+                    ((int *)ptr2)[1] = i+1;
+                    ((int *)ptr2)[2] = i+2;
+                    function->num_params = 1;
+                    function->params[0].type = RPPC_PARAM_TYPE_PTR;
+                    function->params[0].size = sizeof(fxn_addx_args);
+                    function->params[0].data = (size_t)ptr;
+                    function->params[0].base = (size_t)ptr;
+                    function->num_translations = 1;
+                    function->translations[0].index = 0;
+                    function->translations[0].offset = (int)&(((fxn_addx_args *)ptr)->array) - (int)ptr;
+                    function->translations[0].base = ((fxn_addx_args *)ptr)->array;
                 }
                 break;
         }
@@ -1022,6 +1014,13 @@ int main(int argc, char *argv[])
     }
     testFunc = func_idx;
 
+    if (func_idx == FXN_IDX_FXNADD3 || func_idx == FXN_IDX_FXNADDX) {
+        if (init_sharedmem_funcs() < 0) {
+            printf("failure: shmemallocator library must be present for this test function\n");
+            return 1;
+        }
+    }
+
     switch (test_id) {
         case 1:
             /* multiple threads each with an RPMSG-RPC ServiceMgr instance */