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 "libabckit/src/wrappers/abcfile_wrapper.h"
17
18 #include "libpandafile/bytecode_instruction.h"
19 #include "libpandafile/class_data_accessor.h"
20 #include "libpandafile/code_data_accessor.h"
21 #include "libpandafile/code_data_accessor-inl.h"
22 #include "libpandafile/field_data_accessor.h"
23 #include "libpandafile/file.h"
24 #include "libpandafile/file_items.h"
25 #include "libpandafile/method_data_accessor.h"
26 #include "libpandafile/method_data_accessor-inl.h"
27 #include "libpandafile/proto_data_accessor.h"
28 #include "libpandafile/proto_data_accessor-inl.h"
29 #include "libpandafile/type_helper.h"
30
31 namespace libabckit {
32 // CC-OFFNXT(WordsTool.95) sensitive word conflict
33 // NOLINTNEXTLINE(google-build-using-namespace)
34 using namespace panda;
35
~FileWrapper()36 FileWrapper::~FileWrapper()
37 {
38 delete reinterpret_cast<const panda_file::File *>(abcFile_);
39 }
40
MethodCast(void * method)41 static panda_file::File::EntityId MethodCast(void *method)
42 {
43 return panda_file::File::EntityId(reinterpret_cast<uintptr_t>(method));
44 }
45
ResolveOffsetByIndex(void * parentMethod,uint16_t index) const46 uint32_t FileWrapper::ResolveOffsetByIndex(void *parentMethod, uint16_t index) const
47 {
48 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
49 return pf->ResolveOffsetByIndex(MethodCast(parentMethod), index).GetOffset();
50 }
51
GetMethodTotalArgumentsCount(void * method) const52 size_t FileWrapper::GetMethodTotalArgumentsCount(void *method) const
53 {
54 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
55 panda_file::MethodDataAccessor mda(*pf, MethodCast(method));
56
57 ASSERT(!mda.IsExternal());
58 panda_file::CodeDataAccessor cda(*pf, mda.GetCodeId().value());
59
60 return cda.GetNumArgs();
61 }
62
GetMethodArgumentsCount(void * caller,uint32_t id) const63 size_t FileWrapper::GetMethodArgumentsCount([[maybe_unused]] void *caller, uint32_t id) const
64 {
65 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
66 panda_file::MethodDataAccessor mda(*pf, panda_file::File::EntityId(id));
67 panda_file::ProtoDataAccessor pda(*pf, mda.GetProtoId());
68
69 return pda.GetNumArgs();
70 }
71
GetMethodRegistersCount(void * method) const72 size_t FileWrapper::GetMethodRegistersCount(void *method) const
73 {
74 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
75 panda_file::MethodDataAccessor mda(*pf, MethodCast(method));
76 ASSERT(!mda.IsExternal());
77 panda_file::CodeDataAccessor cda(*pf, mda.GetCodeId().value());
78 return cda.GetNumVregs();
79 }
80
GetMethodCode(void * method) const81 const uint8_t *FileWrapper::GetMethodCode(void *method) const
82 {
83 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
84 panda_file::MethodDataAccessor mda(*pf, MethodCast(method));
85 ASSERT(!mda.IsExternal());
86 panda_file::CodeDataAccessor cda(*pf, mda.GetCodeId().value());
87 return cda.GetInstructions();
88 }
89
GetMethodCodeSize(void * method) const90 size_t FileWrapper::GetMethodCodeSize(void *method) const
91 {
92 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
93 panda_file::MethodDataAccessor mda(*pf, MethodCast(method));
94
95 ASSERT(!mda.IsExternal());
96 panda_file::CodeDataAccessor cda(*pf, mda.GetCodeId().value());
97
98 return cda.GetCodeSize();
99 }
100
GetMethodSourceLanguage(void * method) const101 uint8_t FileWrapper::GetMethodSourceLanguage(void *method) const
102 {
103 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
104 panda_file::MethodDataAccessor mda(*pf, MethodCast(method));
105
106 ASSERT(!mda.IsExternal());
107
108 auto sourceLang = mda.GetSourceLang();
109 ASSERT(sourceLang.has_value());
110
111 return static_cast<uint8_t>(sourceLang.value());
112 }
113
GetClassIdForMethod(void * method) const114 size_t FileWrapper::GetClassIdForMethod(void *method) const
115 {
116 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
117 panda_file::MethodDataAccessor mda(*pf, MethodCast(method));
118 return static_cast<size_t>(mda.GetClassId().GetOffset());
119 }
120
GetClassNameFromMethod(void * method) const121 std::string FileWrapper::GetClassNameFromMethod(void *method) const
122 {
123 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
124 panda_file::MethodDataAccessor mda(*pf, MethodCast(method));
125 auto stringData = pf->GetStringData(mda.GetClassId());
126 return std::string(reinterpret_cast<const char *>(stringData.data));
127 }
128
GetMethodName(void * method) const129 std::string FileWrapper::GetMethodName(void *method) const
130 {
131 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
132 panda_file::MethodDataAccessor mda(*pf, MethodCast(method));
133 auto stringData = pf->GetStringData(mda.GetNameId());
134 return std::string(reinterpret_cast<const char *>(stringData.data));
135 }
136
EnumerateTryBlocks(void * method,const std::function<void (void *)> & cb) const137 void FileWrapper::EnumerateTryBlocks(void *method, const std::function<void(void *)> &cb) const
138 {
139 auto *pf = reinterpret_cast<const panda_file::File *>(abcFile_);
140 panda_file::MethodDataAccessor mda(*pf, MethodCast(method));
141 panda_file::CodeDataAccessor cda(*pf, mda.GetCodeId().value());
142
143 cda.EnumerateTryBlocks([&cb](panda_file::CodeDataAccessor::TryBlock &tryBlock) {
144 cb(reinterpret_cast<void *>(&tryBlock));
145 return true;
146 });
147 }
148
GetTryBlockBoundaries(void * tryBlock) const149 std::pair<int, int> FileWrapper::GetTryBlockBoundaries(void *tryBlock) const
150 {
151 auto *tryBlockP = reinterpret_cast<panda_file::CodeDataAccessor::TryBlock *>(tryBlock);
152 return {tryBlockP->GetStartPc(), tryBlockP->GetStartPc() + tryBlockP->GetLength()};
153 }
154
EnumerateCatchBlocksForTryBlock(void * tryBlock,const std::function<void (void *)> & cb) const155 void FileWrapper::EnumerateCatchBlocksForTryBlock(void *tryBlock, const std::function<void(void *)> &cb) const
156 {
157 auto *tryBlockP = reinterpret_cast<panda_file::CodeDataAccessor::TryBlock *>(tryBlock);
158 tryBlockP->EnumerateCatchBlocks([&cb](panda_file::CodeDataAccessor::CatchBlock &catchBlock) {
159 cb(reinterpret_cast<void *>(&catchBlock));
160 return true;
161 });
162 }
163
GetCatchBlockHandlerPc(void * catchBlock) const164 uint32_t FileWrapper::GetCatchBlockHandlerPc(void *catchBlock) const
165 {
166 auto *tryBlockP = reinterpret_cast<panda_file::CodeDataAccessor::CatchBlock *>(catchBlock);
167 return tryBlockP->GetHandlerPc();
168 }
169
170 } // namespace libabckit
171