diff --git a/common/cutil/dfx_cutil.c b/common/cutil/dfx_cutil.c index 67995de86a0609016642ed1656bb0d09b6ffe30b..3a36b13db422d23cdbc544d857a3407c1251b66a 100644 --- a/common/cutil/dfx_cutil.c +++ b/common/cutil/dfx_cutil.c @@ -22,9 +22,7 @@ #include #include #include -#ifndef DFX_SIGNAL_LIBC #include -#endif #include #include #include "dfx_define.h" @@ -64,11 +62,7 @@ bool GetThreadName(char* buffer, size_t bufferSz) bool GetThreadNameByTid(int32_t tid, char* buffer, size_t bufferSz) { char threadNamePath[NAME_BUF_LEN] = { 0 }; -#ifdef DFX_SIGNAL_LIBC - if (snprintf(threadNamePath, sizeof(threadNamePath), "/proc/%d/comm", tid) <= 0) { -#else if (snprintf_s(threadNamePath, sizeof(threadNamePath), sizeof(threadNamePath) - 1, "/proc/%d/comm", tid) <= 0) { -#endif return false; } return ReadStringFromFile(threadNamePath, buffer, bufferSz); diff --git a/interfaces/innerkits/signal_handler/dfx_signal_handler.c b/interfaces/innerkits/signal_handler/dfx_signal_handler.c index c658bd4bc4c62be58494d622ab4085ae28c94e60..6f5e7f402cc112f8a1fe9111dc807c010ab2de3e 100644 --- a/interfaces/innerkits/signal_handler/dfx_signal_handler.c +++ b/interfaces/innerkits/signal_handler/dfx_signal_handler.c @@ -240,7 +240,7 @@ static enum ProcessDumpType GetDumpType(int signo, siginfo_t *si) static bool FillDumpRequest(int signo, siginfo_t *si, void *context) { - memset(&g_request, 0, sizeof(g_request)); + (void)memset_s(&g_request, sizeof(g_request), 0, sizeof(g_request)); g_request.type = GetDumpType(signo, si); g_request.pid = GetRealPid(); g_request.nsPid = syscall(SYS_getpid); @@ -249,17 +249,18 @@ static bool FillDumpRequest(int signo, siginfo_t *si, void *context) g_request.reserved = 0; g_request.timeStamp = GetTimeMilliseconds(); g_request.fdTableAddr = (uint64_t)fdsan_get_fd_table(); - memcpy(g_request.appRunningId, g_appRunningId, sizeof(g_request.appRunningId)); + if (memcpy_s(g_request.appRunningId, sizeof(g_request.appRunningId), + g_appRunningId, sizeof(g_appRunningId)) != EOK) { + DFXLOGE("FillDumpRequest appRunningId memcpy fail!"); + } if (!IsDumpSignal(signo) && g_GetStackIdFunc!= NULL) { g_request.stackId = g_GetStackIdFunc(); DFXLOGI("g_GetStackIdFunc %{private}p.", (void*)g_request.stackId); } - GetThreadNameByTid(g_request.tid, g_request.threadName, sizeof(g_request.threadName)); GetProcessName(g_request.processName, sizeof(g_request.processName)); - - memcpy(&(g_request.siginfo), si, sizeof(siginfo_t)); - memcpy(&(g_request.context), context, sizeof(ucontext_t)); + (void)memcpy_s(&(g_request.siginfo), sizeof(siginfo_t), si, sizeof(siginfo_t)); + (void)memcpy_s(&(g_request.context), sizeof(ucontext_t), context, sizeof(ucontext_t)); bool ret = true; switch (signo) { @@ -368,8 +369,8 @@ static void DFX_SignalHandler(int signo, siginfo_t *si, void *context) static void InstallSigActionHandler(int signo) { struct sigaction action; - memset(&action, 0, sizeof(action)); - memset(&g_oldSigactionList, 0, sizeof(g_oldSigactionList)); + (void)memset_s(&action, sizeof(action), 0, sizeof(action)); + (void)memset_s(&g_oldSigactionList, sizeof(g_oldSigactionList), 0, sizeof(g_oldSigactionList)); sigfillset(&action.sa_mask); action.sa_sigaction = DFX_SignalHandler; action.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; @@ -418,13 +419,11 @@ void DFX_InstallSignalHandler(void) int DFX_SetAppRunningUniqueId(const char* appRunningId, size_t len) { - size_t appRunningIdMaxLen = sizeof(g_appRunningId); - if (appRunningId == NULL || appRunningIdMaxLen <= len) { + (void)memset_s(g_appRunningId, sizeof(g_appRunningId), 0, sizeof(g_appRunningId)); + if (memcpy_s(g_appRunningId, sizeof(g_appRunningId) - 1, appRunningId, len) != EOK) { DFXLOGE("param error. appRunningId is NULL or length overflow"); return -1; } - memset(g_appRunningId, 0, appRunningIdMaxLen); - memcpy(g_appRunningId, appRunningId, len); return 0; } diff --git a/interfaces/innerkits/signal_handler/dfx_signalhandler_exception.c b/interfaces/innerkits/signal_handler/dfx_signalhandler_exception.c index 0ad19138a26aee0b0c7bf59bd4678c93aab1071c..2c7d00be45fbde2c494c64f9b985036ae71e0ffc 100644 --- a/interfaces/innerkits/signal_handler/dfx_signalhandler_exception.c +++ b/interfaces/innerkits/signal_handler/dfx_signalhandler_exception.c @@ -20,18 +20,14 @@ #include #include #include +#include #include #include "dfx_define.h" #include "dfx_exception.h" #include "errno.h" #include "string.h" - -#ifndef DFX_SIGNAL_LIBC #include "dfx_log.h" -#else -#include "musl_log.h" -#endif #ifdef LOG_DOMAIN #undef LOG_DOMAIN @@ -70,9 +66,11 @@ static int ConnectSocket(const char* path, const int timeout) } } struct sockaddr_un server; - (void)memset(&server, 0, sizeof(server)); + (void)memset_s(&server, sizeof(server), 0, sizeof(server)); server.sun_family = AF_LOCAL; - (void)strncpy(server.sun_path, path, sizeof(server.sun_path) - 1); + if (strcpy_s(server.sun_path, sizeof(server.sun_path), path) != EOK) { + DFXLOGE("server sun_path strcpy fail."); + } int len = sizeof(server.sun_family) + strlen(server.sun_path); int connected = OHOS_TEMP_FAILURE_RETRY(connect(fd, (struct sockaddr*)(&server), len)); if (connected < 0) { diff --git a/interfaces/innerkits/signal_handler/include/musl_log.h b/interfaces/innerkits/signal_handler/include/musl_log.h deleted file mode 100644 index 6d8f3f724d7efb0c3e76631c0482f2121fc0b4de..0000000000000000000000000000000000000000 --- a/interfaces/innerkits/signal_handler/include/musl_log.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. - * 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. - */ -#ifndef DFX_MUSL_LOG_H -#define DFX_MUSL_LOG_H - -#include -#include -#include - -#include "dfx_log_define.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef ENABLE_SIGHAND_MUSL_LOG - -/* Log type */ -typedef enum { - /* min log type */ - LOG_TYPE_MIN = 0, - /* Used by app log. */ - LOG_APP = 0, - /* Log to kmsg, only used by init phase. */ - LOG_INIT = 1, - /* Used by core service, framework. */ - LOG_CORE = 3, - /* Used by kmsg log. */ - LOG_KMSG = 4, - /* max log type */ - LOG_TYPE_MAX -} LogType; - -/* Log level */ -typedef enum { - /* min log level */ - LOG_LEVEL_MIN = 0, - /* Designates lower priority log. */ - LOG_DEBUG = 3, - /* Designates useful information. */ - LOG_INFO = 4, - /* Designates hazardous situations. */ - LOG_WARN = 5, - /* Designates very serious errors. */ - LOG_ERROR = 6, - /* Designates major fatal anomaly. */ - LOG_FATAL = 7, - /* max log level */ - LOG_LEVEL_MAX, -} LogLevel; - -__attribute__ ((visibility("hidden"))) int MuslHiLogPrinter( - LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...); - -// replace the old interface, and delete the old interface after the replacement is complete -#define DFXMUSL_PRINT(prio, ...) MuslHiLogPrinter(LOG_CORE, prio, LOG_DOMAIN, LOG_TAG, __VA_ARGS__) - -#define DFXLOGD(...) DFXMUSL_PRINT(LOG_DEBUG, __VA_ARGS__) -#define DFXLOGI(...) DFXMUSL_PRINT(LOG_INFO, __VA_ARGS__) -#define DFXLOGW(...) DFXMUSL_PRINT(LOG_WARN, __VA_ARGS__) -#define DFXLOGE(...) DFXMUSL_PRINT(LOG_ERROR, __VA_ARGS__) -#define DFXLOGF(...) DFXMUSL_PRINT(LOG_FATAL, __VA_ARGS__) - -#else - -// replace the old interface, and delete the old interface after the replacement is complete -#define DFXLOGD(...) -#define DFXLOGI(...) -#define DFXLOGW(...) -#define DFXLOGE(...) -#define DFXLOGF(...) - -#endif - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/interfaces/innerkits/signal_handler/musl_log.c b/interfaces/innerkits/signal_handler/musl_log.c deleted file mode 100644 index 6dea858d021729f2e671a13e10f17408837130ee..0000000000000000000000000000000000000000 --- a/interfaces/innerkits/signal_handler/musl_log.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * 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. - */ - -#include "musl_log.h" - -#include "stdbool.h" - -#ifdef ENABLE_SIGHAND_MUSL_LOG -__attribute__ ((visibility("hidden"))) int MuslHiLogPrinter( - LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) -{ - int ret; - va_list ap; - va_start(ap, fmt); - ret = HiLogAdapterPrintArgs(type, level, domain, tag, fmt, ap); - va_end(ap); - return ret; -} -#endif \ No newline at end of file