1 /**
2 * Copyright (c) 2021-2024 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/public.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
28 #if !PANDA_TARGET_WINDOWS
29 #include "securec.h"
30 #endif
31 #include "utils/logger.h"
32 #include "libpandabase/os/file.h"
33
34 namespace {
35
ProcessConfigFile(ark::verifier::Config * cfg,const char * text)36 bool ProcessConfigFile(ark::verifier::Config *cfg, const char *text)
37 {
38 ark::verifier::debug::RegisterConfigHandlerBreakpoints(cfg);
39 ark::verifier::debug::RegisterConfigHandlerWhitelist(cfg);
40 ark::verifier::debug::RegisterConfigHandlerOptions(cfg);
41 ark::verifier::debug::RegisterConfigHandlerMethodOptions(cfg);
42 ark::verifier::debug::RegisterConfigHandlerMethodGroups(cfg);
43
44 ark::verifier::config::Section section;
45
46 bool result =
47 ark::verifier::config::ParseConfig(text, section) && ark::verifier::config::ProcessConfig(cfg, section);
48 if (result) {
49 LOG(DEBUG, VERIFIER) << "Verifier debug configuration: \n" << section.Image();
50 ark::verifier::debug::SetDefaultMethodOptions(cfg);
51 }
52
53 return result;
54 }
55
56 } // namespace
57
58 namespace ark::verifier::config {
59
LoadConfig(Config * cfg,std::string_view filename)60 bool LoadConfig(Config *cfg, std::string_view filename)
61 {
62 using ark::os::file::Mode;
63 using ark::os::file::Open;
64
65 bool result = false;
66
67 if (filename == "default") {
68 result = ProcessConfigFile(cfg, VERIFIER_DEBUG_DEFAULT_CONFIG);
69 } else {
70 do {
71 auto file = Open(filename, Mode::READONLY);
72 if (!file.IsValid()) {
73 break;
74 }
75 auto size = file.GetFileSize();
76 if (!size.HasValue()) {
77 file.Close();
78 break;
79 }
80 char *text = new char[1 + *size];
81 memset_s(text, 1 + *size, 0x00, 1 + *size);
82 if (!file.ReadAll(text, *size)) {
83 file.Close();
84 delete[] text;
85 break;
86 }
87 text[*size] = 0; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
88 file.Close();
89 result = ProcessConfigFile(cfg, text);
90 delete[] text;
91 } while (false);
92 }
93
94 if (!result) {
95 LOG(DEBUG, VERIFIER) << "Failed to load verifier debug config file '" << filename << "'";
96 }
97
98 return result;
99 }
100
101 } // namespace ark::verifier::config
102
103 #endif // PANDA_VERIF_CONFIG_LOAD_H_
104