1 /**
2 * Copyright (c) 2021-2022 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/config/debug_breakpoint/breakpoint_private.h"
22 #include "verification/config/default/default_config.h"
23 #include "verification/config/parse/config_parse.h"
24 #include "verification/config/process/config_process.h"
25 #include "verification/config/context/context.h"
26 #include "verification/config/handlers/config_handlers.h"
27 #include "verification/config/whitelist/whitelist_private.h"
28
29 #if !PANDA_TARGET_WINDOWS
30 #include "securec.h"
31 #endif
32 #include "utils/logger.h"
33 #include "libpandabase/os/file.h"
34
35 namespace {
36
ProcessConfigFile(const char * text)37 bool ProcessConfigFile(const char *text)
38 {
39 panda::verifier::debug::RegisterConfigHandlerBreakpoints();
40 panda::verifier::debug::RegisterConfigHandlerWhitelist();
41 panda::verifier::debug::RegisterConfigHandlerOptions();
42 panda::verifier::debug::RegisterConfigHandlerMethodOptions();
43 panda::verifier::debug::RegisterConfigHandlerMethodGroups();
44
45 panda::verifier::config::Section config;
46
47 bool result = panda::verifier::config::ParseConfig(text, config) && panda::verifier::config::ProcessConfig(config);
48
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(VerifierDebugDefaultConfig);
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 char *text = new char[1 + *size];
82 memset_s(text, 1 + *size, 0x00, 1 + *size);
83 if (!file.ReadAll(text, *size)) {
84 file.Close();
85 delete[] text;
86 break;
87 }
88 text[*size] = 0; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
89 file.Close();
90 result = ProcessConfigFile(text);
91 delete[] text;
92 } while (false);
93 }
94
95 if (!result) {
96 LOG(DEBUG, VERIFIER) << "Failed to load verifier debug config file '" << filename << "'";
97 }
98
99 return result;
100 }
101
102 } // namespace panda::verifier::config
103
104 #endif //! PANDA_VERIF_CONFIG_LOAD_H_
105