1 /**
2 * Copyright (c) 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 "guard_context.h"
17
18 #include "utils/logger.h"
19
20 #include "generator/order_name_generator.h"
21 #include "guard_args_parser.h"
22 #include "util/assert_util.h"
23
24 namespace {
25 constexpr std::string_view TAG = "[Guard_Context]";
26 // The reserved fields here are to ensure basic confusion operation in scenarios where the fronted is not configured
27 const std::set<std::string> RESERVED_NAMES = {"prototype", "default", "*default*"};
28 /* When the path is split, there will be/ xxx.ts, Even some paths can be written as/ sss/../../xx.ts, This is a folder
29 * name that needs to be preserved */
30 const std::set<std::string> RESERVED_FILE_NAMES = {".", ".."};
31 } // namespace
32
GetInstance()33 std::shared_ptr<panda::guard::GuardContext> panda::guard::GuardContext::GetInstance()
34 {
35 static auto instance = std::make_shared<GuardContext>();
36 return instance;
37 }
38
Init(int argc,const char ** argv)39 void panda::guard::GuardContext::Init(int argc, const char **argv)
40 {
41 GuardArgsParser parser;
42 PANDA_GUARD_ASSERT_PRINT(!parser.Parse(argc, argv), TAG, ErrorCode::COMMAND_PARAMS_PARSE_ERROR,
43 "obfuscate command param parse failed");
44
45 debugMode_ = parser.IsDebugMode();
46 LOG(INFO, PANDAGUARD) << TAG << "[param parse success], config file:" << parser.GetConfigFilePath();
47
48 options_ = std::make_shared<GuardOptions>();
49 options_->Load(parser.GetConfigFilePath());
50 LOG(INFO, PANDAGUARD) << TAG << "[load config file success]";
51
52 nameCache_ = std::make_shared<NameCache>(options_);
53 std::string applyNameCachePath =
54 options_->GetApplyNameCache().empty() ? options_->GetDefaultNameCachePath() : options_->GetApplyNameCache();
55 nameCache_->Load(applyNameCachePath);
56
57 nameMapping_ = std::make_shared<NameMapping>(nameCache_, options_);
58 nameMapping_->AddNameMapping(RESERVED_NAMES);
59 nameMapping_->AddFileNameMapping(RESERVED_FILE_NAMES);
60 LOG(INFO, PANDAGUARD) << TAG << "[name mapping init success]";
61 }
62
Finalize()63 void panda::guard::GuardContext::Finalize()
64 {
65 graphContext_->Finalize();
66 }
67
GetGuardOptions() const68 const std::shared_ptr<panda::guard::GuardOptions> &panda::guard::GuardContext::GetGuardOptions() const
69 {
70 return options_;
71 }
72
GetNameCache() const73 const std::shared_ptr<panda::guard::NameCache> &panda::guard::GuardContext::GetNameCache() const
74 {
75 return nameCache_;
76 }
77
GetNameMapping() const78 const std::shared_ptr<panda::guard::NameMapping> &panda::guard::GuardContext::GetNameMapping() const
79 {
80 return nameMapping_;
81 }
82
CreateGraphContext(const panda_file::File & file)83 void panda::guard::GuardContext::CreateGraphContext(const panda_file::File &file)
84 {
85 graphContext_ = std::make_shared<GraphContext>(file);
86 graphContext_->Init();
87 }
88
GetGraphContext() const89 const std::shared_ptr<panda::guard::GraphContext> &panda::guard::GuardContext::GetGraphContext() const
90 {
91 return graphContext_;
92 }
93
IsDebugMode() const94 bool panda::guard::GuardContext::IsDebugMode() const
95 {
96 return debugMode_;
97 }
98