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