Fix race condition related to global variable deallocation on process termination 87/317187/1
authorChanggyu Choi <changyu.choi@samsung.com>
Thu, 26 Dec 2024 11:04:09 +0000 (20:04 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Thu, 26 Dec 2024 11:04:09 +0000 (20:04 +0900)
Change-Id: I5270d186eba7c5672b4a7653d36d801e146945bc
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/aul/app_com.cc

index ff921259911d000fe8d8e1b9312d86b331488c9a..92e02d392076be1c38c753f6cb4d81817e083b8a 100644 (file)
@@ -181,7 +181,12 @@ class Context {
   mutable std::recursive_mutex mutex_;
 };
 
-Context context;
+Context* context = new Context();
+
+__attribute__((destructor)) static void destructor() {
+  delete context;
+  context = nullptr;
+}
 
 int AppComCreate(const char* endpoint,
     aul_app_com_permission_h permission, app_com_cb callback, void* user_data,
@@ -191,6 +196,11 @@ int AppComCreate(const char* endpoint,
     return AUL_R_EINVAL;
   }
 
+  if (context == nullptr) {
+    _E("Invalid context");
+    return AUL_R_EINVAL;
+  }
+
   if (!aul_is_initialized()) {
     if (aul_launch_init(nullptr, nullptr) < 0)
       return AUL_R_ENOINIT;
@@ -216,7 +226,7 @@ int AppComCreate(const char* endpoint,
       .SendSimply(sync ? AUL_SOCK_NONE : AUL_SOCK_NOREPLY);
   if (ret == 0) {
     *connection = static_cast<aul_app_com_connection_h>(
-        context.AddConnection(endpoint, callback, user_data));
+        context->AddConnection(endpoint, callback, user_data));
   }
 
   return ret;
@@ -227,8 +237,10 @@ int AppComCreate(const char* endpoint,
 int app_com_recv(bundle* b) {
   if (b == nullptr)
     return -1;
+  if (context == nullptr)
+    return -1;
 
-  return context.Receive(b);
+  return context->Receive(b);
 }
 
 extern "C" API aul_app_com_permission_h aul_app_com_permission_create() {
@@ -288,6 +300,11 @@ extern "C" API int aul_app_com_join(const char* endpoint, const char* filter,
     return AUL_R_EINVAL;
   }
 
+  if (context == nullptr) {
+    _E("Invalid context");
+    return AUL_R_EINVAL;
+  }
+
   if (!aul_is_initialized()) {
     if (aul_launch_init(nullptr, nullptr) < 0)
       return AUL_R_ENOINIT;
@@ -302,7 +319,7 @@ extern "C" API int aul_app_com_join(const char* endpoint, const char* filter,
       .SendSimply();
   if (ret == 0) {
     *connection = static_cast<aul_app_com_connection_h>(
-        context.AddConnection(endpoint, callback, user_data));
+        context->AddConnection(endpoint, callback, user_data));
   }
 
   return ret;
@@ -337,17 +354,22 @@ extern "C" API int aul_app_com_leave(aul_app_com_connection_h connection) {
   if (connection == nullptr)
     return AUL_R_EINVAL;
 
+  if (context == nullptr) {
+    _E("Invalid context");
+    return AUL_R_EINVAL;
+  }
+
   auto* conn = static_cast<AppComConnection*>(connection);
-  if (!context.ExistConnection(conn)) {
+  if (!context->ExistConnection(conn)) {
     _E("Invalid parameter");
     return AUL_R_EINVAL;
   }
 
-  std::lock_guard<std::recursive_mutex> lock(context.GetMutex());
+  std::lock_guard<std::recursive_mutex> lock(context->GetMutex());
   conn->Disable();
 
   std::string endpoint = conn->GetEndpoint();
-  if (!context.ExistConnection(endpoint)) {
+  if (!context->ExistConnection(endpoint)) {
     tizen_base::Bundle b { { AUL_K_COM_ENDPOINT, endpoint } };
     int ret = AppRequest(APP_COM_LEAVE, getuid())
       .With(std::move(b))
@@ -358,8 +380,8 @@ extern "C" API int aul_app_com_leave(aul_app_com_connection_h connection) {
 
   g_idle_add_full(G_PRIORITY_HIGH, [](gpointer user_data) -> gboolean {
         auto* conn = static_cast<AppComConnection*>(user_data);
-        std::lock_guard<std::recursive_mutex> lock(context.GetMutex());
-        context.RemoveConnection(conn);
+        std::lock_guard<std::recursive_mutex> lock(context->GetMutex());
+        context->RemoveConnection(conn);
         return G_SOURCE_REMOVE;
       }, connection, nullptr);