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_graph_context.h"
17
18 #include "assembly-type.h"
19 #include "mem/pool_manager.h"
20 #include "compiler/compiler_options.h"
21 #include "libpandafile/class_data_accessor.h"
22 #include "libpandafile/class_data_accessor-inl.h"
23 #include "libpandafile/file-inl.h"
24 #include "libpandafile/method_data_accessor.h"
25
26 #include "util/assert_util.h"
27
28 namespace {
29 constexpr std::string_view TAG = "[Guard_Graph]";
30 constexpr uint32_t MAX_SIZE = ~0U;
31 } // namespace
32
Init()33 void panda::guard::GraphContext::Init()
34 {
35 PoolManager::Initialize(PoolType::MALLOC);
36
37 auto records = file_.GetClasses();
38 for (auto id : records) {
39 panda_file::File::EntityId recordId {id};
40 if (file_.IsExternal(recordId)) {
41 continue;
42 }
43 std::string name = GetStringById(recordId);
44 pandasm::Type type = pandasm::Type::FromDescriptor(name);
45 std::string recordFullName = type.GetName();
46 recordNameMap_.emplace(recordFullName, id);
47 }
48
49 panda::compiler::options.SetCompilerUseSafepoint(false);
50 panda::compiler::options.SetCompilerMaxBytecodeSize(MAX_SIZE);
51 panda::compiler::options.SetCompilerMaxVregsNum(MAX_SIZE);
52
53 LOG(INFO, PANDAGUARD) << TAG << "[graph context init success]";
54 }
55
Finalize()56 void panda::guard::GraphContext::Finalize()
57 {
58 PoolManager::Finalize();
59 LOG(INFO, PANDAGUARD) << TAG << "[graph context finalize success]";
60 }
61
GetAbcFile() const62 const panda::panda_file::File &panda::guard::GraphContext::GetAbcFile() const
63 {
64 return file_;
65 }
66
FillMethodPtr(const std::string & recordName,const std::string & rawName) const67 uint32_t panda::guard::GraphContext::FillMethodPtr(const std::string &recordName, const std::string &rawName) const
68 {
69 auto it = recordNameMap_.find(recordName);
70 PANDA_GUARD_ASSERT_PRINT(it == recordNameMap_.end(), TAG, ErrorCode::GENERIC_ERROR,
71 "can not find record: " << recordName);
72
73 uint32_t methodId = 0;
74 uint32_t id = it->second;
75 panda_file::File::EntityId recordId {id};
76 panda_file::ClassDataAccessor cda {this->GetAbcFile(), recordId};
77 cda.EnumerateMethods([this, rawName, &methodId](const panda_file::MethodDataAccessor &mda) {
78 if (mda.IsExternal()) {
79 return;
80 }
81 std::string methodNameRaw = this->GetStringById(mda.GetNameId());
82 if (methodNameRaw == rawName) {
83 methodId = mda.GetMethodId().GetOffset();
84 }
85 });
86 return methodId;
87 }
88
GetStringById(const panda_file::File::EntityId & entity_id) const89 std::string panda::guard::GraphContext::GetStringById(const panda_file::File::EntityId &entity_id) const
90 {
91 panda_file::File::StringData sd = file_.GetStringData(entity_id);
92 return (reinterpret_cast<const char *>(sd.data));
93 }
94