1 /*
2 * Copyright (c) 2021 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
16 #ifndef PANDA_VERIF_CONFIG_LOAD_H_
17 #define PANDA_VERIF_CONFIG_LOAD_H_
18
19 #include "config_load.h"
20
21 #include "verification/debug/config/config_parse.h"
22 #include "verification/debug/config/config_process.h"
23 #include "verification/debug/context/context.h"
24 #include "verification/debug/handlers/config_handlers.h"
25 #include "verification/debug/breakpoint/breakpoint_private.h"
26 #include "verification/debug/allowlist/allowlist_private.h"
27
28 #if !PANDA_TARGET_WINDOWS
29 #include "securec.h"
30 #endif
31 #include "utils/logger.h"
32 #include "libpandabase/os/file.h"
33
34 #include "default_config.h"
35
36 namespace {
37
ProcessConfigFile(const char * text)38 bool ProcessConfigFile(const char *text)
39 {
40 panda::verifier::debug::RegisterConfigHandlerBreakpoints();
41 panda::verifier::debug::RegisterConfigHandlerAllowlist();
42 panda::verifier::debug::RegisterConfigHandlerOptions();
43 panda::verifier::debug::RegisterConfigHandlerMethodOptions();
44 panda::verifier::debug::RegisterConfigHandlerMethodGroups();
45
46 panda::verifier::config::Section config;
47
48 bool result = panda::verifier::config::ParseConfig(text, config) && panda::verifier::config::ProcessConfig(config);
49 if (result) {
50 LOG(DEBUG, VERIFIER) << "Verifier debug configuration: \n" << config.Image();
51 panda::verifier::debug::SetDefaultMethodOptions();
52 }
53
54 return result;
55 }
56
57 } // namespace
58
59 namespace panda::verifier::config {
60
LoadConfig(std::string_view filename)61 bool LoadConfig(std::string_view filename)
62 {
63 using panda::os::file::Mode;
64 using panda::os::file::Open;
65
66 bool result = false;
67
68 if (filename == "default") {
69 result = ProcessConfigFile(G_VERIFIER_DEBUG_DEFAULT_CONFIG);
70 } else {
71 do {
72 auto file = Open(filename, Mode::READONLY);
73 if (!file.IsValid()) {
74 break;
75 }
76 auto size = file.GetFileSize();
77 if (!size.HasValue()) {
78 file.Close();
79 break;
80 }
81
82 char *text = new (std::nothrow) char[1 + *size];
83 if (text == nullptr) {
84 file.Close();
85 break;
86 }
87
88 (void)memset_s(text, 1 + *size, 0x00, 1 + *size);
89 if (!file.ReadAll(text, *size)) {
90 file.Close();
91 delete[] text;
92 break;
93 }
94 text[*size] = 0; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
95 file.Close();
96 result = ProcessConfigFile(text);
97 delete[] text;
98 } while (false);
99 }
100
101 if (!result) {
102 LOG(DEBUG, VERIFIER) << "Failed to load verifier debug config file '" << filename << "'";
103 }
104
105 return result;
106 }
107
MethodIdCalculationHandler(uint32_t class_hash,uint32_t hash,uintptr_t id)108 void MethodIdCalculationHandler(uint32_t class_hash, uint32_t hash, uintptr_t id)
109 {
110 debug::BreakpointMethodIdCalculationHandler(class_hash, hash, id);
111 debug::AllowlistMethodIdCalculationHandler(class_hash, hash, id);
112 }
113
114 } // namespace panda::verifier::config
115
116 #endif //! PANDA_VERIF_CONFIG_LOAD_H_
117