-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathkernelcache.cpp
More file actions
195 lines (174 loc) · 5.66 KB
/
Copy pathkernelcache.cpp
File metadata and controls
195 lines (174 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// Created by kat on 5/21/23.
//
#include "kernelcacheapi.h"
using namespace BinaryNinja;
using namespace KernelCacheAPI;
BNKernelCacheImage ImageToApi(CacheImage image)
{
BNKernelCacheImage apiImage {};
apiImage.name = BNAllocString(image.name.c_str());
apiImage.headerFileAddress = image.headerFileAddress;
apiImage.headerVirtualAddress = image.headerVirtualAddress;
return apiImage;
}
CacheImage ImageFromApi(BNKernelCacheImage image)
{
CacheImage apiImage {};
apiImage.name = image.name;
apiImage.headerVirtualAddress = image.headerVirtualAddress;
apiImage.headerFileAddress = image.headerFileAddress;
return apiImage;
}
CacheSymbol SymbolFromApi(BNKernelCacheSymbol apiSymbol)
{
CacheSymbol symbol;
symbol.name = apiSymbol.name;
symbol.address = apiSymbol.address;
symbol.type = apiSymbol.symbolType;
return symbol;
}
std::pair<std::string, Ref<Type>> CacheSymbol::DemangledName(BinaryView &view) const
{
QualifiedName qname;
Ref<Type> outType = nullptr;
std::string shortName = name;
if (DemangleGeneric(view.GetDefaultArchitecture(), name, outType, qname, &view, true))
shortName = qname.GetString();
return {shortName, outType};
}
Ref<Symbol> CacheSymbol::GetBNSymbol(BinaryView &view) const
{
auto [shortName, _] = DemangledName(view);
return new Symbol(type, shortName, shortName, name, address, nullptr);
}
std::string KernelCacheAPI::GetSymbolTypeAsString(const BNSymbolType &type)
{
// NOTE: We currently only use the function and data symbol for cache symbols.
// update this if that changes.
switch (type)
{
case FunctionSymbol:
return "Function";
case DataSymbol:
return "Data";
default:
return "Unknown";
}
}
KernelCacheController::KernelCacheController(BNKernelCacheController *controller)
{
m_object = controller;
}
KCRef<KernelCacheController> KernelCacheController::GetController(BinaryView &view)
{
BNKernelCacheController *controller = BNGetKernelCacheController(view.GetObject());
if (controller == nullptr)
return nullptr;
return new KernelCacheController(controller);
}
bool KernelCacheController::ApplyImage(BinaryView &view, const CacheImage &image)
{
auto apiImage = ImageToApi(image);
bool result = BNKernelCacheControllerApplyImage(m_object, view.GetObject(), &apiImage);
BNKernelCacheFreeImage(apiImage);
return result;
}
bool KernelCacheController::IsImageLoaded(const CacheImage &image) const
{
auto apiImage = ImageToApi(image);
bool result = BNKernelCacheControllerIsImageLoaded(m_object, &apiImage);
BNKernelCacheFreeImage(apiImage);
return result;
}
std::optional<CacheImage> KernelCacheController::GetImageAt(uint64_t address) const
{
BNKernelCacheImage apiImage;
if (!BNKernelCacheControllerGetImageAt(m_object, address, &apiImage))
return std::nullopt;
CacheImage image = ImageFromApi(apiImage);
BNKernelCacheFreeImage(apiImage);
return image;
}
std::optional<CacheImage> KernelCacheController::GetImageContaining(uint64_t address) const
{
BNKernelCacheImage apiImage;
if (!BNKernelCacheControllerGetImageContaining(m_object, address, &apiImage))
return std::nullopt;
CacheImage image = ImageFromApi(apiImage);
BNKernelCacheFreeImage(apiImage);
return image;
}
std::optional<CacheImage> KernelCacheController::GetImageWithName(const std::string &name) const
{
BNKernelCacheImage apiImage;
if (!BNKernelCacheControllerGetImageWithName(m_object, name.c_str(), &apiImage))
return std::nullopt;
CacheImage image = ImageFromApi(apiImage);
BNKernelCacheFreeImage(apiImage);
return image;
}
std::vector<std::string> KernelCacheController::GetImageDependencies(const CacheImage &image) const
{
size_t count;
BNKernelCacheImage apiImage = ImageToApi(image);
char **dependencies = BNKernelCacheControllerGetImageDependencies(m_object, &apiImage, &count);
BNKernelCacheFreeImage(apiImage);
std::vector<std::string> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.emplace_back(dependencies[i]);
BNFreeStringList(dependencies, count);
return result;
}
std::optional<CacheSymbol> KernelCacheController::GetSymbolAt(uint64_t address) const
{
BNKernelCacheSymbol apiSymbol;
if (!BNKernelCacheControllerGetSymbolAt(m_object, address, &apiSymbol))
return std::nullopt;
CacheSymbol symbol = SymbolFromApi(apiSymbol);
BNKernelCacheFreeSymbol(apiSymbol);
return symbol;
}
std::optional<CacheSymbol> KernelCacheController::GetSymbolWithName(const std::string &name) const
{
BNKernelCacheSymbol apiSymbol;
if (!BNKernelCacheControllerGetSymbolWithName(m_object, name.c_str(), &apiSymbol))
return std::nullopt;
CacheSymbol symbol = SymbolFromApi(apiSymbol);
BNKernelCacheFreeSymbol(apiSymbol);
return symbol;
}
std::vector<CacheImage> KernelCacheController::GetImages() const
{
size_t count;
BNKernelCacheImage *images = BNKernelCacheControllerGetImages(m_object, &count);
std::vector<CacheImage> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.emplace_back(ImageFromApi(images[i]));
BNKernelCacheFreeImageList(images, count);
return result;
}
std::vector<CacheImage> KernelCacheController::GetLoadedImages() const
{
size_t count;
BNKernelCacheImage *images = BNKernelCacheControllerGetLoadedImages(m_object, &count);
std::vector<CacheImage> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.emplace_back(ImageFromApi(images[i]));
BNKernelCacheFreeImageList(images, count);
return result;
}
std::vector<CacheSymbol> KernelCacheController::GetSymbols() const
{
size_t count;
BNKernelCacheSymbol *symbols = BNKernelCacheControllerGetSymbols(m_object, &count);
std::vector<CacheSymbol> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.emplace_back(SymbolFromApi(symbols[i]));
BNKernelCacheFreeSymbolList(symbols, count);
return result;
}