• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "faultlog_sanitizer.h"
16 
17 #include "constants.h"
18 #include "file_util.h"
19 #include "hiview_logger.h"
20 #include "hisysevent.h"
21 #include "string_util.h"
22 #include "faultlog_util.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_LABEL(0xD002D11, "Faultlogger");
27 
ReportSanitizerToAppEvent(std::shared_ptr<SysEvent> sysEvent) const28 void FaultLogSanitizer::ReportSanitizerToAppEvent(std::shared_ptr<SysEvent> sysEvent) const
29 {
30     std::string summary = StringUtil::UnescapeJsonStringValue(sysEvent->GetEventValue(FaultKey::SUMMARY));
31     HIVIEW_LOGD("ReportSanitizerAppEvent:summary:%{public}s.", summary.c_str());
32 
33     Json::Value params;
34     params["time"] = sysEvent->happenTime_;
35     auto reason = sysEvent->GetEventValue(FaultKey::REASON);
36     params["type"] = reason;
37     if (reason.find("FDSAN") != std::string::npos) {
38         params["type"] = "FDSAN";
39         HIVIEW_LOGI("info reason: %{public}s, set sysEvent reason FDSAN", reason.c_str());
40     }
41     Json::Value externalLog(Json::arrayValue);
42     std::string logPath = sysEvent->GetEventValue(FaultKey::LOG_PATH);
43     if (!logPath.empty()) {
44         externalLog.append(logPath);
45     }
46     params["external_log"] = externalLog;
47     params["bundle_version"] = sysEvent->GetEventValue(FaultKey::MODULE_VERSION);
48     params["bundle_name"] = sysEvent->GetEventValue(FaultKey::MODULE_NAME);
49     params["pid"] = sysEvent->GetPid();
50     params["uid"] = sysEvent->GetUid();
51     std::string paramsStr = Json::FastWriter().write(params);
52     HIVIEW_LOGD("ReportSanitizerAppEvent: uid:%{public}d, json:%{public}s.",
53         sysEvent->GetUid(), paramsStr.c_str());
54     EventPublish::GetInstance().PushEvent(sysEvent->GetUid(), "ADDRESS_SANITIZER",
55         HiSysEvent::EventType::FAULT, paramsStr);
56 }
57 
ReportToAppEvent(std::shared_ptr<SysEvent> sysEvent,const FaultLogInfo & info) const58 bool FaultLogSanitizer::ReportToAppEvent(std::shared_ptr<SysEvent> sysEvent, const FaultLogInfo& info) const
59 {
60     if (!info.reportToAppEvent || !sysEvent) {
61         return false;
62     }
63     ReportSanitizerToAppEvent(sysEvent);
64     return true;
65 }
66 
FaultLogSanitizer()67 FaultLogSanitizer::FaultLogSanitizer()
68 {
69     faultType_ = FaultLogType::ADDR_SANITIZER;
70 }
71 
GetFaultModule(SysEvent & sysEvent) const72 std::string FaultLogSanitizer::GetFaultModule(SysEvent& sysEvent) const
73 {
74     return sysEvent.GetEventValue(FaultKey::MODULE_NAME);
75 }
76 
ParseSanitizerEasyEvent(SysEvent & sysEvent) const77 void FaultLogSanitizer::ParseSanitizerEasyEvent(SysEvent& sysEvent) const
78 {
79     std::string data = sysEvent.GetEventValue("DATA");
80     if (data.empty()) {
81         HIVIEW_LOGW("Sanitizer receive empty hiSysEventEasy");
82         return;
83     }
84     size_t start = 0;
85     while (start < data.size()) {
86         size_t end = data.find(';', start);
87         if (end == std::string::npos) {
88             end = data.size();
89         }
90         size_t pos = data.find(':', start);
91         if (pos != std::string::npos && pos > start && pos < end) {
92             std::string key = data.substr(start, pos - start);
93             if (key == "SUMMARY") {
94                 std::string value = data.substr(pos + 1);
95                 sysEvent.SetEventValue(key, value);
96                 break;
97             }
98             std::string value = data.substr(pos + 1, end - pos - 1);
99             sysEvent.SetEventValue(key, value);
100         }
101         start = end + 1;
102     }
103     sysEvent.SetEventValue("DATA", "");
104 }
105 
FillSpecificFaultLogInfo(SysEvent & sysEvent,FaultLogInfo & info) const106 void FaultLogSanitizer::FillSpecificFaultLogInfo(SysEvent& sysEvent, FaultLogInfo& info) const
107 {
108     if (info.reason.find("FDSAN") != std::string::npos) {
109         info.pid = sysEvent.GetEventIntValue("PID");
110         info.time = sysEvent.GetEventIntValue("HAPPEN_TIME");
111         info.reportToAppEvent = true;
112         info.dumpLogToFaultlogger = true;
113         info.logPath = GetDebugSignalTempLogName(info);
114         info.summary = "";
115         info.sanitizerType = "FDSAN";
116     } else if (info.reason.find("DEBUG SIGNAL") != std::string::npos) {
117         info.pid = sysEvent.GetEventIntValue(FaultKey::MODULE_PID);
118         info.time = sysEvent.GetEventIntValue(FaultKey::HAPPEN_TIME);
119         info.reportToAppEvent = false;
120         info.dumpLogToFaultlogger = false;
121         info.logPath = GetDebugSignalTempLogName(info);
122     } else {
123         ParseSanitizerEasyEvent(sysEvent);
124         info.module = sysEvent.GetEventValue(FaultKey::MODULE_NAME);
125         info.sanitizerType = sysEvent.GetEventValue(FaultKey::FAULT_TYPE);
126         info.reason = sysEvent.GetEventValue(FaultKey::REASON);
127         info.logPath = GetSanitizerTempLogName(info.pid, sysEvent.GetEventValue(FaultKey::HAPPEN_TIME));
128         info.summary = "";
129     }
130 }
131 } // namespace HiviewDFX
132 } // namespace OHOS
133