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 #include "verification/config/debug_breakpoint/breakpoint_private.h"
17 #include "verification/config/process/config_process.h"
18 #include "verification/config/whitelist/whitelist_private.h"
19 #include "verification/util/parser/parser.h"
20
21 #include "runtime/include/method.h"
22 #include "runtime/include/mem/panda_string.h"
23
24 #include "utils/logger.h"
25
26 #include <string>
27 #include <cstring>
28 #include <cstdint>
29
30 namespace panda::verifier::debug {
31
32 using panda::parser::action;
33 using panda::parser::parser;
34 using panda::verifier::config::Section;
35
WhitelistMethodParser()36 const auto &WhitelistMethodParser()
37 {
38 struct Whitelist;
39
40 using panda::parser::charset;
41 using p = parser<PandaString, const char, const char *>::next<Whitelist>;
42 using p1 = p::p;
43
44 static const auto WS = p::of_charset(" \t");
45
46 static const auto METHOD_NAME_HANDLER = [](action a, PandaString &c, auto from, auto to) {
47 if (a == action::PARSED) {
48 c = PandaString {from, to};
49 }
50 return true;
51 };
52
53 static const auto METHOD_NAME = p1::of_charset(!charset {" \t,"}) |= METHOD_NAME_HANDLER; // NOLINT
54
55 static const auto WHITELIST_METHOD = ~WS >> METHOD_NAME >> ~WS >> p::end() | ~WS >> p::end();
56
57 return WHITELIST_METHOD;
58 }
59
RegisterConfigHandlerWhitelist()60 void RegisterConfigHandlerWhitelist()
61 {
62 static const auto CONFIG_DEBUG_WHITELIST_VERIFIER = [](const Section §ion) {
63 for (const auto &s : section.sections) {
64 WhitelistKind kind;
65 if (s.name == "class") {
66 kind = WhitelistKind::CLASS;
67 } else if (s.name == "method") {
68 kind = WhitelistKind::METHOD;
69 } else if (s.name == "method_call") {
70 kind = WhitelistKind::METHOD_CALL;
71 } else {
72 LOG(DEBUG, VERIFIER) << "Wrong debug verifier whitelist section: '" << s.name << "'";
73 return false;
74 }
75 for (const auto &i : s.items) {
76 PandaString c;
77 const char *start = i.c_str();
78 const char *end = i.c_str() + i.length(); // NOLINT
79 if (!WhitelistMethodParser()(c, start, end)) {
80 LOG(DEBUG, VERIFIER) << "Wrong whitelist line: '" << i << "'";
81 return false;
82 }
83 if (!c.empty()) {
84 if (kind == WhitelistKind::CLASS) {
85 LOG(DEBUG, VERIFIER) << "Added to whitelist config '" << s.name << "' methods from class " << c;
86 } else {
87 LOG(DEBUG, VERIFIER) << "Added to whitelist config '" << s.name << "' methods named " << c;
88 }
89 AddWhitelistMethodConfig(kind, c);
90 }
91 }
92 }
93 return true;
94 };
95
96 config::RegisterConfigHandler("config.debug.whitelist.verifier", CONFIG_DEBUG_WHITELIST_VERIFIER);
97 }
98
99 } // namespace panda::verifier::debug
100