From 96e88c43df1394824e0ffa3d2d19477359190449 Mon Sep 17 00:00:00 2001 From: Adrian Szyndela Date: Fri, 18 Jan 2019 12:36:18 +0100 Subject: [PATCH] internal: make nondestructible global static_parser This should help with destructing global variable while using it in other thread. Multi-threaded daemons often kill the main thread before killing the other threads. This patch prevents instance memory release when the main thread exits. , and thus required to avoid crash on released memory. .#0 ldp_xml_parser::ItemBuilder::~ItemBuilder (this=0xb67a980c , __in_chrg=) at src/internal/policy.cpp:510 .#1 0xb67a5de8 in ldp_xml_parser::XmlParser::~XmlParser (this=0xb67a980c , __in_chrg=) at /usr/lib/gcc/armv7l-tizen-linux-gnueabi/6.2.1/include/c++/ext/new_allocator.h:110 .#2 0xb695806c in __cxa_finalize (d=0xb67a9438) at cxa_finalize.c:83 .#3 0xb679bcea in __do_global_dtors_aux () from /lib/libdbuspolicy1.so.1 .#4 0xb6fdfa1c in _dl_fini () at dl-fini.c:235 .#5 0xb6957a44 in __run_exit_handlers (status=, listp=, run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true) at exit.c:106 .#6 0xb6957b6c in __GI_exit (status=) at exit.c:137 .#7 0xb6940640 in __libc_start_main (main=0xbefffe44, argc=-1230635008, argv=0xb6940640 <__libc_start_main+280>, init=, fini=0x7f55f69c <__libc_csu_fini>, rtld_fini=0xb6fdf7e4 <_dl_fini>, stack_end=0xbefffe44) . at libc-start.c:323 .#8 0x7f5589e0 in _start () at ../sysdeps/arm/start.S:110 Change-Id: I0cc0a2623eee688b0498fccacb8a2bc219fd3a94 Signed-off-by: Hyotaek Shim --- src/internal/global_nodestruct.hpp | 38 +++++++++++++++++++++++++++++++++++ src/internal/internal.cpp | 4 ++-- src/internal/naive_policy_checker.hpp | 13 +----------- src/internal/xml_parser.cpp | 8 ++++---- src/internal/xml_parser.hpp | 4 +++- 5 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 src/internal/global_nodestruct.hpp diff --git a/src/internal/global_nodestruct.hpp b/src/internal/global_nodestruct.hpp new file mode 100644 index 0000000..6bc7fe7 --- /dev/null +++ b/src/internal/global_nodestruct.hpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2015-2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ + +/** + * \file + * \ingroup Implementation + */ + + +#ifndef GLOBAL_NODESTRUCT_HPP +#define GLOBAL_NODESTRUCT_HPP + +/** Declare a global variable that is constructed (via gcc's __attribute__((constructor))) but never destroyed. */ +#define DCL_NODESTRUCT_GLOBAL(TYPE,NAME)\ + namespace detail_NODESTRUCT_GLOBAL { alignas(TYPE) extern uint8_t NAME[sizeof(TYPE)]; }\ + /* awkwardly written to silence gcc's -Wstrict-aliasing because _Pragma("GCC diagnostic ignored \"-Wstrict-aliasing\"") is bugged */\ + static inline TYPE &NAME() { auto p = static_cast(&detail_NODESTRUCT_GLOBAL::NAME[0]); return *reinterpret_cast(p); }\ + namespace detailInit_NODESTRUCT_GLOBAL { __attribute__((constructor)) static void NAME() { new(&::NAME()) TYPE(); } } + +/** Define a global variable that is constructed (via gcc's __attribute__((constructor))) but never destroyed. */ +#define DEF_NODESTRUCT_GLOBAL(TYPE,NAME)\ + namespace detail_NODESTRUCT_GLOBAL { alignas(TYPE) uint8_t NAME[sizeof(TYPE)]; } + + +#endif // GLOBAL_NODESTRUCT_HPP diff --git a/src/internal/internal.cpp b/src/internal/internal.cpp index 8100483..78b8dc1 100755 --- a/src/internal/internal.cpp +++ b/src/internal/internal.cpp @@ -35,7 +35,7 @@ static const char* get_str(const char* const szstr) { int __internal_init(bool bus_type, const char* const config_name) { policy_checker().clearDb(bus_type); - auto err = ldp_xml_parser::static_parser.parsePolicy(bus_type, get_str(config_name)); + auto err = static_parser().parsePolicy(bus_type, get_str(config_name)); if (tslog::enabled()) memory_dump(bus_type); return err; @@ -64,7 +64,7 @@ void __internal_init_flush_logs() void __internal_init_sup_group(bool bus_type, uid_t uid, gid_t gid) { - ldp_xml_parser::static_parser.updateGroupPolicy(bus_type, uid, gid); + static_parser().updateGroupPolicy(bus_type, uid, gid); } void __internal_enter() diff --git a/src/internal/naive_policy_checker.hpp b/src/internal/naive_policy_checker.hpp index d4379cc..c15eab0 100644 --- a/src/internal/naive_policy_checker.hpp +++ b/src/internal/naive_policy_checker.hpp @@ -25,6 +25,7 @@ #include "policy.hpp" #include "naive_policy_db.hpp" +#include "global_nodestruct.hpp" namespace ldp_xml_parser { @@ -170,18 +171,6 @@ namespace ldp_xml_parser }; } -/** Declare a global variable that is constructed (via gcc's __attribute__((constructor))) but never destroyed. */ -#define DCL_NODESTRUCT_GLOBAL(TYPE,NAME)\ - namespace detail_NODESTRUCT_GLOBAL { alignas(TYPE) extern uint8_t NAME[sizeof(TYPE)]; }\ - /* awkwardly written to silence gcc's -Wstrict-aliasing because _Pragma("GCC diagnostic ignored \"-Wstrict-aliasing\"") is bugged */\ - static inline TYPE &NAME() { auto p = static_cast(&detail_NODESTRUCT_GLOBAL::NAME[0]); return *reinterpret_cast(p); }\ - namespace detailInit_NODESTRUCT_GLOBAL { __attribute__((constructor)) static void NAME() { new(&::NAME()) TYPE(); } } - -/** Define a global variable that is constructed (via gcc's __attribute__((constructor))) but never destroyed. */ -#define DEF_NODESTRUCT_GLOBAL(TYPE,NAME)\ - namespace detail_NODESTRUCT_GLOBAL { alignas(TYPE) uint8_t NAME[sizeof(TYPE)]; } - - DCL_NODESTRUCT_GLOBAL(ldp_xml_parser::NaivePolicyChecker, policy_checker) #endif diff --git a/src/internal/xml_parser.cpp b/src/internal/xml_parser.cpp index 25cc199..03d6a13 100644 --- a/src/internal/xml_parser.cpp +++ b/src/internal/xml_parser.cpp @@ -49,7 +49,7 @@ void parseAssert(bool condition) { void start_element_handler(void *data, const char *el, const char **attr) { (void)data; - XmlParser& parser = static_parser; + XmlParser& parser = static_parser(); try { parser.elementStart(el, attr); } catch (...) { @@ -60,7 +60,7 @@ void start_element_handler(void *data, const char *el, const char **attr) { void end_element_handler(void *data, const char *el) { (void)data; - XmlParser& parser = static_parser; + XmlParser& parser = static_parser(); try { parser.elementEnd(el); } catch (...) { @@ -71,7 +71,7 @@ void end_element_handler(void *data, const char *el) { void text_handler(void *data, const char *text, int len) { (void)data; - XmlParser& parser = static_parser; + XmlParser& parser = static_parser(); try { parser.text(text, len); } catch (...) { @@ -297,4 +297,4 @@ void XmlParser::getIncludedFiles(const std::string& parent_dir, const std::strin } } -ldp_xml_parser::XmlParser ldp_xml_parser::static_parser; +DEF_NODESTRUCT_GLOBAL(ldp_xml_parser::XmlParser, static_parser); diff --git a/src/internal/xml_parser.hpp b/src/internal/xml_parser.hpp index 3cf7899..2369c61 100755 --- a/src/internal/xml_parser.hpp +++ b/src/internal/xml_parser.hpp @@ -24,6 +24,7 @@ #include #include #include "policy.hpp" +#include "global_nodestruct.hpp" namespace ldp_xml_parser { @@ -81,7 +82,8 @@ namespace ldp_xml_parser std::vector included_files; }; - extern XmlParser static_parser; } //namespace +DCL_NODESTRUCT_GLOBAL(ldp_xml_parser::XmlParser, static_parser) + #endif -- 2.7.4