• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "verification/public_internal.h"
17 #include "verification/config/context/context.h"
18 #include "verification/config/process/config_process.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 ark::verifier::debug {
31 
32 using ark::parser::Action;
33 using ark::parser::Parser;
34 using ark::verifier::config::Section;
35 
WhitelistMethodParser()36 const auto &WhitelistMethodParser()
37 {
38     struct Whitelist;
39 
40     using ark::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::OfCharset(" \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::OfCharset(!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 
RegisterConfigHandlerWhitelistSectionHandler(Config * config,const struct Section & s)60 static bool RegisterConfigHandlerWhitelistSectionHandler(Config *config, const struct Section &s)
61 {
62     WhitelistKind kind;
63     if (s.name == "class") {
64         kind = WhitelistKind::CLASS;
65     } else if (s.name == "method") {
66         kind = WhitelistKind::METHOD;
67     } else if (s.name == "method_call") {
68         kind = WhitelistKind::METHOD_CALL;
69     } else {
70         LOG(DEBUG, VERIFIER) << "Wrong debug verifier whitelist section: '" << s.name << "'";
71         return false;
72     }
73     for (const auto &i : s.items) {
74         PandaString c;
75         const char *start = i.c_str();
76         const char *end = i.c_str() + i.length();  // NOLINT
77         if (!WhitelistMethodParser()(c, start, end)) {
78             LOG(DEBUG, VERIFIER) << "Wrong whitelist line: '" << i << "'";
79             return false;
80         }
81         if (!c.empty()) {
82             if (kind == WhitelistKind::CLASS) {
83                 LOG(DEBUG, VERIFIER) << "Added to whitelist config '" << s.name << "' methods from class " << c;
84             } else {
85                 LOG(DEBUG, VERIFIER) << "Added to whitelist config '" << s.name << "' methods named " << c;
86             }
87             config->debugCfg.AddWhitelistMethodConfig(kind, c);
88         }
89     }
90     return true;
91 }
92 
RegisterConfigHandlerWhitelist(Config * dcfg)93 void RegisterConfigHandlerWhitelist(Config *dcfg)
94 {
95     static const auto CONFIG_DEBUG_WHITELIST_VERIFIER = [](Config *config, const Section &section) {
96         for (const auto &s : section.sections) {
97             if (!RegisterConfigHandlerWhitelistSectionHandler(config, s)) {
98                 return false;
99             }
100         }
101         return true;
102     };
103 
104     config::RegisterConfigHandler(dcfg, "config.debug.whitelist.verifier", CONFIG_DEBUG_WHITELIST_VERIFIER);
105 }
106 
107 }  // namespace ark::verifier::debug
108