From 72c978d66013a79e300cad1d727fb895030243df Mon Sep 17 00:00:00 2001 From: liuyifei Date: Tue, 25 Mar 2025 07:19:30 +0000 Subject: [PATCH 1/4] delete CheckHiLogdLinked Signed-off-by: liuyifei --- .../moduletest/common/hilog_base_ndk_test.cpp | 55 ------------------- 1 file changed, 55 deletions(-) diff --git a/test/moduletest/common/hilog_base_ndk_test.cpp b/test/moduletest/common/hilog_base_ndk_test.cpp index 9f91a7e..ce6b83e 100644 --- a/test/moduletest/common/hilog_base_ndk_test.cpp +++ b/test/moduletest/common/hilog_base_ndk_test.cpp @@ -146,57 +146,6 @@ bool CheckHiLogPrint(char *needToMatch) } return ret; } - -// Detects how many FDs are linked to hilogd. -int CheckHiLogdLinked() -{ - #define PROC_PATH_LENGTH (64) - int result = 0; - char procPath[PROC_PATH_LENGTH]; - int res = snprintf_s(procPath, PROC_PATH_LENGTH, PROC_PATH_LENGTH - 1, "/proc/%d/fd", getpid()); - if (res == NEGATIVE_ONE) { - printf("CheckHiLogdLinked getpid snprintf_s failed\n"); - return 0; - } - DIR *dir = opendir(procPath); - if (dir == nullptr) { - return result; - } - struct dirent *entry; - while ((entry = readdir(dir)) != nullptr) { - if (entry->d_type != DT_LNK) { - continue; - } - char fdPath[128]; - res = snprintf_s(fdPath, sizeof(fdPath), sizeof(fdPath) - 1, "%s/%s", procPath, entry->d_name); - if (res == NEGATIVE_ONE) { - printf("CheckHiLogdLinked fd search snprintf_s failed\n"); - return 0; - } - - char target[256]; - ssize_t len = readlink(fdPath, target, sizeof(target) - 1); - if (len == -1) { - continue; - } - target[len] = '\0'; - if (!strstr(target, "socket")) { - continue; - } - struct sockaddr_un addr; - socklen_t addrLen = sizeof(addr); - - // Obtains the peer address connected to the socket. - getpeername(atoi(entry->d_name), reinterpret_cast(&addr), &addrLen); - if (strstr(addr.sun_path, "hilogInput")) { - printf("FD: %s Connected to: %s\n", entry->d_name, addr.sun_path); - result++; - } - } - - closedir(dir); - return result; -} class HiLogBaseNDKTest : public testing::Test { public: @@ -353,10 +302,6 @@ HWTEST_F(HiLogBaseNDKTest, HilogBasePrintCheck, TestSize.Level1) bool result = CheckHiLogPrint(g_str[i].data()); EXPECT_EQ(result, true); } - - // Check the number of socket links to hilogInput. - int result = CheckHiLogdLinked(); - EXPECT_EQ(result, TWO); } } // namespace HiLogTest } // namespace HiviewDFX -- Gitee From ff3b4703d1ca728c36e02624fab5f9a07bc7d9ed Mon Sep 17 00:00:00 2001 From: liuyifei Date: Fri, 28 Mar 2025 11:24:37 +0800 Subject: [PATCH 2/4] enable CFI Signed-off-by: liuyifei --- frameworks/hilog_ndk/BUILD.gn | 1 + frameworks/libhilog/BUILD.gn | 6 ++++++ interfaces/native/innerkits/BUILD.gn | 2 ++ services/hilogd/BUILD.gn | 6 ++++++ services/hilogtool/BUILD.gn | 6 ++++++ 5 files changed, 21 insertions(+) diff --git a/frameworks/hilog_ndk/BUILD.gn b/frameworks/hilog_ndk/BUILD.gn index ff22b32..d69f53f 100644 --- a/frameworks/hilog_ndk/BUILD.gn +++ b/frameworks/hilog_ndk/BUILD.gn @@ -14,6 +14,7 @@ import("//build/ohos.gni") ohos_shared_library("hilog_ndk") { + branch_protector_ret = "pac_ret" sanitize = { cfi = true cfi_cross_dso = true diff --git a/frameworks/libhilog/BUILD.gn b/frameworks/libhilog/BUILD.gn index c8bbd92..02e4540 100644 --- a/frameworks/libhilog/BUILD.gn +++ b/frameworks/libhilog/BUILD.gn @@ -41,6 +41,12 @@ config("libhilog_config") { template("libhilog_source") { forward_variables_from(invoker, "*") ohos_source_set(target_name) { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } if (platform != "windows" && platform != "mac" && platform != "linux") { param_sources = [ "$param_root/properties.cpp" ] ioctl_sources = [ "$ioctl_root/log_ioctl.cpp" ] diff --git a/interfaces/native/innerkits/BUILD.gn b/interfaces/native/innerkits/BUILD.gn index 26c638b..8c43d64 100644 --- a/interfaces/native/innerkits/BUILD.gn +++ b/interfaces/native/innerkits/BUILD.gn @@ -49,6 +49,7 @@ if (is_mingw || is_mac || is_linux || is_ohos) { "system", "updater", ] + branch_protector_ret = "pac_ret" sanitize = { cfi = true cfi_cross_dso = true @@ -73,6 +74,7 @@ template("libhilog") { } } else { ohos_shared_library(target_name) { + branch_protector_ret = "pac_ret" sanitize = { cfi = true cfi_cross_dso = true diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index 9a5bbed..5ad2377 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -20,6 +20,12 @@ config("hilogd_config") { } ohos_executable("hilogd") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } sources = [ "cmd_executor.cpp", "flow_control.cpp", diff --git a/services/hilogtool/BUILD.gn b/services/hilogtool/BUILD.gn index aa7ea15..b142f96 100644 --- a/services/hilogtool/BUILD.gn +++ b/services/hilogtool/BUILD.gn @@ -20,6 +20,12 @@ config("hilog_config") { } ohos_executable("hilog") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } sources = [ "log_display.cpp", "main.cpp", -- Gitee From 22d47b6c0295f755a0dfcf0d68122d5a118a2dca Mon Sep 17 00:00:00 2001 From: liuyifei Date: Fri, 28 Mar 2025 04:04:14 +0000 Subject: [PATCH 3/4] update frameworks/libhilog/BUILD.gn. Signed-off-by: liuyifei --- frameworks/libhilog/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/libhilog/BUILD.gn b/frameworks/libhilog/BUILD.gn index 02e4540..e201517 100644 --- a/frameworks/libhilog/BUILD.gn +++ b/frameworks/libhilog/BUILD.gn @@ -42,7 +42,7 @@ template("libhilog_source") { forward_variables_from(invoker, "*") ohos_source_set(target_name) { branch_protector_ret = "pac_ret" - sanitize = { + sanitize = { cfi = true cfi_cross_dso = true debug = false -- Gitee From 6d8d5c7f24e9bd1f4b6edc188261b0afe28424f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E6=98=8C=E6=99=B6?= Date: Fri, 18 Apr 2025 17:27:39 +0800 Subject: [PATCH 4/4] add support for printing ts objects --- .vscode/settings.json | 67 +++++++++++++++++++ .../js/kits/napi/src/common/napi/n_val.cpp | 48 ++++++++++++- .../js/kits/napi/src/common/napi/n_val.h | 6 ++ .../js/kits/napi/src/common/napi/uni_header.h | 4 -- .../napi/src/hilog/src/hilog_napi_base.cpp | 9 ++- 5 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3babb71 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,67 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "strstream": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "codecvt": "cpp", + "condition_variable": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "map": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "regex": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "shared_mutex": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "typeinfo": "cpp", + "variant": "cpp" + } +} \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/n_val.cpp b/interfaces/js/kits/napi/src/common/napi/n_val.cpp index e97f317..4622dd2 100644 --- a/interfaces/js/kits/napi/src/common/napi/n_val.cpp +++ b/interfaces/js/kits/napi/src/common/napi/n_val.cpp @@ -30,13 +30,18 @@ NVal::operator bool() const } bool NVal::TypeIs(napi_valuetype expType) const +{ + return ValueTypeIs(val_, expType); +} + +bool NVal::ValueTypeIs(napi_value value, napi_valuetype expType) const { if (!*this) { return false; } napi_valuetype valueType; - napi_typeof(env_, val_, &valueType); + napi_typeof(env_, value, &valueType); if (expType != valueType) { return false; @@ -173,6 +178,47 @@ tuple NVal::ToTypedArrayInfo return make_tuple(status == napi_ok, data, length, byte_offset, type); } +string NVal::GetPrintString(napi_value value) const +{ + string str; + if(!ValueTypeIs(value, napi_string)) { + napi_value strValue = nullptr; + if (napi_coerce_to_string(env_, value, &strValue) != napi_ok) { + return str; + } + value = strValue; + } + napi_get_print_string(env_, value, str); + return str; +} + +tuple NVal::GetValObjectAsStr() const +{ + napi_value globalValue = nullptr; + auto fail = make_tuple(false, ""); + napi_status status = napi_get_global(env_, &globalValue); + if (status != napi_ok) { + return fail; + } + napi_value jsonValue = nullptr; + status = napi_get_named_property(env_, globalValue, "JSON", &jsonValue); + if (status != napi_ok) { + return fail; + } + napi_value stringifyValue = nullptr; + status = napi_get_named_property(env_, jsonValue, "stringify", &stringifyValue); + if (status != napi_ok) { + return fail; + } + napi_value transValue = nullptr; + status = napi_call_function(env_, jsonValue, stringifyValue, 1, &val_, &transValue); + if (status != napi_ok || transValue == nullptr) { + return fail; + } + string content = GetPrintString(transValue); + return make_tuple(true, content); +} + bool NVal::HasProp(string propName) const { bool res = false; diff --git a/interfaces/js/kits/napi/src/common/napi/n_val.h b/interfaces/js/kits/napi/src/common/napi/n_val.h index e2ed44d..2a0e1ab 100644 --- a/interfaces/js/kits/napi/src/common/napi/n_val.h +++ b/interfaces/js/kits/napi/src/common/napi/n_val.h @@ -41,6 +41,8 @@ public: bool TypeIs(napi_valuetype expType) const; + bool ValueTypeIs(napi_value value, napi_valuetype expType) const; + /* SHOULD ONLY BE USED FOR EXPECTED TYPE */ std::tuple, size_t> ToUTF8String() const; @@ -68,6 +70,8 @@ public: std::tuple ToTypedArrayInfo() const; + std::tuple GetValObjectAsStr() const; + /* Static helpers to create js objects */ static NVal CreateUndefined(napi_env env); @@ -109,6 +113,8 @@ public: static inline napi_property_descriptor DeclareNapiGetterSetter(const char *name, napi_callback getter, napi_callback setter); +private: + std::string GetPrintString(napi_value value) const; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/interfaces/js/kits/napi/src/common/napi/uni_header.h b/interfaces/js/kits/napi/src/common/napi/uni_header.h index d000d78..5de61d6 100644 --- a/interfaces/js/kits/napi/src/common/napi/uni_header.h +++ b/interfaces/js/kits/napi/src/common/napi/uni_header.h @@ -15,10 +15,6 @@ #ifndef INTERFACES_JS_KITS_NAPI_SRC_COMMON_NAPI_UNI_HEADER_H #define INTERFACES_JS_KITS_NAPI_SRC_COMMON_NAPI_UNI_HEADER_H -#pragma once - -#include -#else #include "napi/native_api.h" #include "napi/native_node_api.h" diff --git a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp index 4e5ca26..4de4ba4 100644 --- a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp +++ b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp @@ -95,7 +95,8 @@ void ParseLogContent(string& formatStr, vector& params, string& logCo break; case 'O': case 'o': - if (params[count].type == napi_object) { + if (params[count].type == napi_object || params[count].type == napi_function || + params[count].type == napi_undefined || params[count].type == napi_null) { ret += (priv && showPriv) ? PRIV_STR : params[count].val; } count++; @@ -223,8 +224,8 @@ napi_value HilogNapiBase::parseNapiValue(napi_env env, napi_callback_info info, if (typeStatus != napi_ok) { return nullptr; } - if (type == napi_number || type == napi_bigint || type == napi_object || - type == napi_undefined || type == napi_boolean || type == napi_null) { + if (type == napi_number || type == napi_bigint || type == napi_undefined || + type == napi_boolean || type == napi_null) { napi_value elmString; napi_status objectStatus = napi_coerce_to_string(env, element, &elmString); if (objectStatus != napi_ok) { @@ -234,6 +235,8 @@ napi_value HilogNapiBase::parseNapiValue(napi_env env, napi_callback_info info, if (!succ) { return nullptr; } + } else if (type == napi_object || type == napi_function) { + tie(succ, res.val) = NVal(env, element).GetValObjectAsStr(); } else if (type == napi_string) { tie(succ, name, ignore) = NVal(env, element).ToUTF8String(); if (!succ) { -- Gitee