Fix heap-use-after-free issue
[platform/core/multimedia/mmsvc-core.git] / server / src / muse_server_ipc.c
1 /*
2  * muse-server
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: YoungHun Kim <yh8004.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "muse_core_internal.h"
23 #include "muse_server_private.h"
24
25 #define MSG_THREAD_NAME                                         "msg"
26 #define DATA_THREAD_NAME                                        "data"
27
28 #define DATA_WORKER_QDATA_MAX_SIZE                      (3840 * 2160 * 4) /* UHD BGRA8888 */
29 #define UNLIMITED_INSTANCE                                      -1
30
31 static void _ms_ipc_data_ch_init(muse_module_h m);
32 static void _ms_ipc_data_ch_deinit(muse_module_h m);
33 static void _ms_ipc_module_data_ch_cleanup(muse_module_h m);
34 static void _ms_ipc_module_msg_ch_cleanup(muse_module_h m);
35 static void _ms_ipc_module_cleanup(muse_module_h m);
36 static gboolean _ms_ipc_module_instance_creation_is_allowed(int module_idx);
37 static gboolean _ms_ipc_get_module_idx(muse_module_h m, void *jobj);
38 static gboolean _ms_ipc_dispatch_create(muse_module_h m, void *jobj);
39 static gboolean _ms_ipc_dispatch_destroy(muse_module_h m);
40 static gboolean _ms_ipc_dispatch_no_instance(muse_module_h m, void *jobj);
41 static gpointer _ms_ipc_dispatch_worker(gpointer data);
42 static gboolean _ms_ipc_data_processing(int fd, muse_recv_data_head_t *header, muse_channel_info_t *ch);
43 static gpointer _ms_ipc_data_worker(gpointer data);
44
45 static void _ms_ipc_data_ch_init(muse_module_h m)
46 {
47         muse_return_if_fail(m);
48
49         m->ch[MUSE_CHANNEL_DATA].data_queue = g_queue_new();
50         g_mutex_init(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
51         g_cond_init(&m->ch[MUSE_CHANNEL_DATA].data_cond);
52 }
53
54 static void _ms_ipc_data_ch_deinit(muse_module_h m)
55 {
56         muse_return_if_fail(m);
57
58         g_mutex_lock(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
59         g_queue_free(m->ch[MUSE_CHANNEL_DATA].data_queue);
60         m->ch[MUSE_CHANNEL_DATA].data_queue = NULL;
61         g_cond_broadcast(&m->ch[MUSE_CHANNEL_DATA].data_cond);
62         g_mutex_unlock(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
63
64         g_mutex_clear(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
65         g_cond_clear(&m->ch[MUSE_CHANNEL_DATA].data_cond);
66 }
67
68 static void _ms_ipc_module_data_ch_cleanup(muse_module_h m)
69 {
70         muse_return_if_fail(m);
71         muse_return_if_fail(m->ch[MUSE_CHANNEL_DATA].thread);
72
73         g_thread_join(m->ch[MUSE_CHANNEL_DATA].thread);
74         m->ch[MUSE_CHANNEL_DATA].thread = NULL;
75
76         _ms_ipc_data_ch_deinit(m);
77
78         LOGD("[close] [%d] MUSE_CHANNEL_DATA", m->ch[MUSE_CHANNEL_DATA].sock_fd);
79         muse_core_connection_close(m->ch[MUSE_CHANNEL_DATA].sock_fd);
80
81 }
82
83 static void _ms_ipc_module_msg_ch_cleanup(muse_module_h m)
84 {
85         muse_return_if_fail(m);
86         muse_return_if_fail(m->ch[MUSE_CHANNEL_MSG].thread);
87
88         SECURE_LOGD("[close] [%d] MUSE_CHANNEL_MSG %p", m->ch[MUSE_CHANNEL_MSG].sock_fd, m);
89         muse_core_connection_close(m->ch[MUSE_CHANNEL_MSG].sock_fd);
90
91         SECURE_LOGD("msg thread (%p) exit", m->ch[MUSE_CHANNEL_MSG].thread);
92         g_thread_unref(m->ch[MUSE_CHANNEL_MSG].thread);
93         m->ch[MUSE_CHANNEL_MSG].thread = NULL;
94 }
95
96 static void _ms_ipc_module_cleanup(muse_module_h m)
97 {
98         muse_return_if_fail(m);
99
100         _ms_ipc_module_data_ch_cleanup(m);
101
102         _ms_ipc_module_msg_ch_cleanup(m);
103
104         ms_connection_unregister(m);
105
106         g_mutex_clear(&m->dispatch_lock);
107
108         memset(m, 0, sizeof(muse_module_t));
109
110         LOGI("[module %p] EXIT pid %d handle %zd created %d", m, m->pid, m->handle, m->is_created);
111         g_free(m);
112 }
113
114 static gboolean _ms_ipc_module_instance_creation_is_allowed(int module_idx)
115 {
116         int max_instance, created_module_instance_count;
117
118         muse_return_val_if_fail(ms_check_module_idx(module_idx), FALSE);
119
120         max_instance = ms_config_get_max_instance(module_idx);
121         created_module_instance_count = muse_server_get_module_instance_count(module_idx);
122
123         if (max_instance == UNLIMITED_INSTANCE || created_module_instance_count < max_instance) {
124                 return TRUE;
125         } else {
126                 LOGW("The number (%d) of created module instance is over the value of max instance (%d)",
127                                 created_module_instance_count, max_instance);
128                 return FALSE;
129         }
130 }
131
132 static gboolean _ms_ipc_get_module_idx(muse_module_h m, void *jobj)
133 {
134         muse_return_val_if_fail(m, FALSE);
135         muse_return_val_if_fail(jobj, FALSE);
136
137         if (!muse_core_msg_object_get_value(MSG_KEY_MODULE_INDEX, jobj, MUSE_TYPE_INT, &m->idx)) {
138                 LOGE("Failed to get the value of module index");
139                 return FALSE;
140         }
141
142         return TRUE;
143 }
144
145 static gboolean _ms_ipc_dispatch_create(muse_module_h m, void *jobj)
146 {
147         int pid, dispatch_ret = MM_ERROR_NONE;
148
149         muse_return_val_if_fail(m, FALSE);
150         muse_return_val_if_fail(jobj, FALSE);
151
152         ms_module_dispatch_lock(m);
153
154         if (!_ms_ipc_get_module_idx(m, jobj)) {
155                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_DEBUG_INFO_DUMP);
156                 goto out;
157         }
158
159         m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(m->idx);
160
161         if (!_ms_ipc_module_instance_creation_is_allowed(m->idx)) {
162                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
163                 goto out;
164         }
165
166         if (muse_core_msg_object_get_value(MSG_KEY_PID, jobj, MUSE_TYPE_INT, &pid) && m->pid != pid)
167                 LOGW("connected pid [%d] msg [%d] is different", m->pid, pid);
168
169         ms_connection_register(m);
170
171         if (muse_server_is_ready())
172                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_CREATE_SERVER_ACK);
173         else
174                 LOGW("Do not send server acknowledgement because muse server is actually not ready");
175
176         _ms_ipc_data_ch_init(m);
177
178         LOGD("module fd: %d dll_handle: %p", m->ch[MUSE_CHANNEL_MSG].sock_fd, m->ch[MUSE_CHANNEL_MSG].dll_handle);
179         dispatch_ret = ms_module_dispatch(m);
180
181         if (dispatch_ret != MM_ERROR_NONE) {
182                 LOGE("create dispatch failed 0x%x, clean up module", dispatch_ret);
183                 goto out;
184         }
185
186         m->is_created = TRUE;
187
188         ms_module_dispatch_unlock(m);
189
190         LOGD("Leave");
191
192         return TRUE;
193
194 out:
195         ms_module_dispatch_unlock(m);
196
197         return FALSE;
198 }
199
200 static gboolean _ms_ipc_dispatch_destroy(muse_module_h m)
201 {
202         int dispatch_ret = MM_ERROR_NONE;
203
204         muse_return_val_if_fail(m, FALSE);
205
206         ms_module_dispatch_lock(m);
207         dispatch_ret = ms_module_dispatch(m);
208         if (dispatch_ret != MM_ERROR_NONE) {
209                 LOGE("destroy dispatch failed 0x%x", dispatch_ret);
210                 ms_module_dispatch_unlock(m);
211                 return TRUE;
212         }
213
214         ms_module_dispatch_unlock(m);
215
216         return FALSE;
217 }
218
219 static gboolean _ms_ipc_dispatch_no_instance(muse_module_h m, void *jobj)
220 {
221         muse_return_val_if_fail(m, FALSE);
222         muse_return_val_if_fail(jobj, FALSE);
223
224         ms_module_dispatch_lock(m);
225
226         if (!_ms_ipc_get_module_idx(m, jobj)) {
227                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_DEBUG_INFO_DUMP);
228                 goto out;
229         }
230
231         m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(m->idx);
232
233         if (!_ms_ipc_module_instance_creation_is_allowed(m->idx)) {
234                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
235                 goto out;
236         }
237
238         ms_connection_register(m);
239
240         _ms_ipc_data_ch_init(m);
241
242         ms_module_dispatch(m);
243
244         SECURE_LOGW("[module %p] [loaded value %d]", m, ms_module_get_loaded_dllsym(m->idx));
245
246 out:
247         ms_module_dispatch_unlock(m);
248
249         return FALSE;
250 }
251
252 static gpointer _ms_ipc_dispatch_worker(gpointer data)
253 {
254         int len, fd, i;
255         int parse_len = 0;
256         muse_module_h m = NULL;
257         gboolean attempt_to_dispatch = TRUE;
258         void *jobj = NULL;
259         char err_msg[MUSE_MSG_LEN_MAX] = {'\0',};
260
261         muse_return_val_if_fail(data, NULL);
262
263         m = (muse_module_h)data;
264
265         fd = m->ch[MUSE_CHANNEL_MSG].sock_fd;
266         m->ch[MUSE_CHANNEL_MSG].thread = g_thread_self();
267
268         LOGI("Enter %d module %p thread %p", fd, m, m->ch[MUSE_CHANNEL_MSG].thread);
269
270         while (attempt_to_dispatch) {
271                 memset(m->recv_msg, 0x00, sizeof(m->recv_msg));
272
273                 for (i = 0; i < MUSE_NUM_FD; i++)
274                         m->ch[MUSE_CHANNEL_MSG].tbm_fd[i] = -1;
275
276                 len = muse_core_msg_recv_fd(fd, m->recv_msg, MUSE_MSG_MAX_LENGTH, m->ch[MUSE_CHANNEL_MSG].tbm_fd);
277                 if (len <= 0) {
278                         strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
279                         LOGE("[%s] [%d] recv : %s (%d)", ms_config_get_host_name(m->idx), fd, err_msg, errno);
280                         ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_SHUTDOWN);
281                         attempt_to_dispatch = FALSE;
282                 }
283
284                 m->msg_offset = 0;
285
286                 while (attempt_to_dispatch && m->msg_offset < len && ms_is_server_ready()) {
287                         jobj = muse_core_msg_object_new(m->recv_msg + m->msg_offset, &parse_len, NULL);
288                         if (!jobj) {
289                                 LOGE("jobj is null");
290                                 attempt_to_dispatch = FALSE;
291                                 break;
292                         }
293
294                         if (muse_core_msg_object_get_value(MSG_KEY_API, jobj, MUSE_TYPE_INT, &m->api)) {
295                                 switch (m->api) {
296                                 case API_CREATE:
297                                         SECURE_LOGI("CREATE module %p %d", m, fd);
298                                         attempt_to_dispatch = _ms_ipc_dispatch_create(m, jobj);
299                                         break;
300                                 case API_DESTROY:
301                                         SECURE_LOGI("DESTROY module %p %d", m, fd);
302                                         attempt_to_dispatch = _ms_ipc_dispatch_destroy(m);
303                                         break;
304                                 default:
305                                         if (m->is_created) /* handle based */
306                                                 ms_module_dispatch(m);
307                                         else
308                                                 attempt_to_dispatch = _ms_ipc_dispatch_no_instance(m, jobj);
309                                         break;
310                                 }
311                         }
312                         muse_core_msg_object_free(jobj);
313                         jobj = NULL;
314                         m->msg_offset += parse_len;
315                         parse_len = len - parse_len;
316                 }
317         }
318
319         _ms_ipc_module_cleanup(m);
320
321         LOGD("worker exit");
322
323 #ifdef MUSE_GCOV_TEST
324         muse_core_gcov_flush();
325 #endif
326
327         return NULL;
328 }
329
330 static gboolean _ms_ipc_data_processing(int fd, muse_recv_data_head_t *header, muse_channel_info_t *ch)
331 {
332         char *raw_data = NULL;
333         muse_server_h ms = ms_get_instance();
334
335         muse_return_val_if_fail(ms, FALSE);
336
337         if (!(fd > 0 && header && ch)) {
338                 LOGE("invalid param %d %p %p", fd, header, ch);
339                 goto _PROCESSING_FAILED;
340         }
341
342         /* check marker */
343         if (header->marker != MUSE_DATA_HEAD) {
344                 LOGE("invalid marker 0x%x", header->marker);
345                 goto _PROCESSING_FAILED;
346         }
347
348         /* check data size */
349         if (header->size > DATA_WORKER_QDATA_MAX_SIZE) {
350                 LOGE("invalid data size %d", header->size);
351                 goto _PROCESSING_FAILED;
352         }
353
354         /* allocation data */
355         raw_data = (char *)g_try_new0(char, header->size + sizeof(muse_recv_data_head_t));
356         if (!raw_data) {
357                 LOGE("failed to alloc data %d + %zu", header->size, sizeof(muse_recv_data_head_t));
358                 goto _PROCESSING_FAILED;
359         }
360
361         /* copy header */
362         memcpy(raw_data, header, sizeof(muse_recv_data_head_t));
363
364         /* receive data */
365         if (!muse_core_msg_recv_len(fd, raw_data + sizeof(muse_recv_data_head_t), header->size)) {
366                 LOGE("receive data failed - length %d", header->size);
367                 goto _PROCESSING_FAILED;
368         }
369
370         /* push data */
371         g_mutex_lock(&ch->data_mutex);
372         g_queue_push_tail(ch->data_queue, (gpointer)raw_data);
373         g_cond_signal(&ch->data_cond);
374         g_mutex_unlock(&ch->data_mutex);
375
376         return TRUE;
377
378 _PROCESSING_FAILED:
379
380         MUSE_G_FREE(raw_data);
381
382         ms_log_process_info(ms->pid);
383
384         return FALSE;
385 }
386
387 static gpointer _ms_ipc_data_worker(gpointer data)
388 {
389         char recv_buf[MUSE_MSG_LEN_MAX] = {'\0',};
390         int fd;
391         muse_module_h m = NULL;
392         muse_channel_info_t *ch = NULL;
393
394         muse_return_val_if_fail(data, NULL);
395
396         m = (muse_module_h)data;
397         SECURE_LOGW("module : %p pid %d handle %zd created %d", m, m->pid, m->handle, m->is_created);
398
399         muse_return_val_if_fail(m->pid > 0 && m->handle && m->is_created, NULL);
400
401         fd = m->ch[MUSE_CHANNEL_DATA].sock_fd;
402         ch = &m->ch[MUSE_CHANNEL_DATA];
403
404         /* get data */
405         while (1) {
406                 if (!muse_core_msg_recv_len(fd, recv_buf, sizeof(muse_recv_data_head_t)))
407                         break;
408
409                 if (!_ms_ipc_data_processing(fd, (muse_recv_data_head_t *)recv_buf, ch)) {
410                         LOGE("ipc data processing failed");
411                         break;
412                 }
413         }
414
415         LOGW("Leave");
416
417         return NULL;
418 }
419
420 gboolean ms_ipc_create_msg_dispatch_worker(muse_module_h m)
421 {
422         GThread *thread = NULL;
423         GError *error = NULL;
424         muse_server_h ms = ms_get_instance();
425
426         ms_connection_t *connection = NULL;
427
428         LOGD("Enter %p", m);
429
430         muse_return_val_if_fail(ms, FALSE);
431         muse_return_val_if_fail(ms_is_server_ready(), FALSE);
432         muse_return_val_if_fail(muse_server_module_is_valid(m), FALSE);
433
434         connection = ms->connection;
435         muse_return_val_if_fail(connection, FALSE);
436
437         ms_connection_lock(connection);
438
439         SECURE_LOGD("[PID %d module %p] module's msg channel fd : %d", m->pid, m, m->ch[MUSE_CHANNEL_MSG].sock_fd);
440
441         thread = g_thread_try_new(MSG_THREAD_NAME, _ms_ipc_dispatch_worker, (gpointer)m, &error);
442         if (!thread) {
443                 LOGE("[module %p] thread creation failed : %s", m, error->message);
444                 g_error_free(error);
445                 ms_log_process_info(ms->pid);
446                 ms_connection_unlock(connection);
447                 return FALSE;
448         }
449
450         LOGD("Leave module %p thread %p", m, thread);
451
452         ms_connection_unlock(connection);
453
454         return TRUE;
455 }
456
457 gboolean ms_ipc_create_data_dispatch_worker(muse_module_h m)
458 {
459         GError *error = NULL;
460         muse_server_h ms = ms_get_instance();
461         ms_connection_t *connection = NULL;
462
463         LOGD("Enter %p", m);
464
465         muse_return_val_if_fail(ms, FALSE);
466         muse_return_val_if_fail(ms_is_server_ready(), FALSE);
467         muse_return_val_if_fail(muse_server_module_is_valid(m), TRUE);
468
469         connection = ms->connection;
470         muse_return_val_if_fail(connection, FALSE);
471
472         ms_connection_lock(connection);
473
474         m->ch[MUSE_CHANNEL_DATA].thread = g_thread_try_new(DATA_THREAD_NAME, _ms_ipc_data_worker, (gpointer)m, &error);
475         if (!m->ch[MUSE_CHANNEL_DATA].thread) {
476                 LOGE("thread creation failed : %s", error->message);
477                 g_error_free(error);
478                 ms_log_process_info(ms->pid);
479                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
480                 muse_core_connection_close(m->ch[MUSE_CHANNEL_MSG].sock_fd);
481                 ms_connection_unlock(connection);
482                 return FALSE;
483         }
484
485         LOGD("Leave %p", m);
486
487         ms_connection_unlock(connection);
488
489         return TRUE;
490 }