-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProcess.cpp
342 lines (293 loc) · 9.06 KB
/
Process.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "Process.hpp"
#include<stdio.h>
#include<map>
#include<libtransistor/cpp/nx.hpp>
#include "libiberty/include/demangle.h"
#include "err.hpp"
#include "Ilia.hpp"
#include "DebugTypes.hpp"
#include "InterfaceSniffer.hpp"
namespace ilia {
using namespace trn;
struct ModHeader {
uint32_t magic, dynamic_off, bss_start_off, bss_end_off;
uint32_t unwind_start_off, unwind_end_off, module_object_off;
};
Process::Process(
Ilia &ilia,
uint64_t pid) :
ilia(ilia), pid(pid),
debug(ResultCode::AssertOk(svc::DebugActiveProcess(pid))),
wait_handle(
ilia.event_waiter.Add(
debug,
[this]() -> bool { HandleEvents(); return true; })) {
}
Process::Thread::Thread(Process &process, uint64_t id, uint64_t tls, uint64_t entrypoint) :
process(process),
thread_id(id),
tls(tls),
entrypoint(entrypoint) {
}
nx::ThreadContext &Process::Thread::GetContext() {
if(!is_context_valid) {
*((thread_context_t*) (&context)) = ResultCode::AssertOk(
svc::GetDebugThreadContext(process.debug, thread_id, 15));
is_context_valid = true;
}
is_context_dirty = true;
return context;
}
void Process::Thread::CommitContext() {
if(is_context_dirty) {
ResultCode::AssertOk(
svc::SetDebugThreadContext(process.debug, thread_id, (thread_context_t*) &context, 3));
is_context_dirty = false;
}
}
void Process::Thread::InvalidateContext() {
is_context_valid = false;
}
bool Process::Thread::operator<(const Thread &rhs) const {
return thread_id < rhs.thread_id;
}
Process::NSO::NSO(Process &process, uint64_t base, uint64_t size) :
process(process),
base(base),
size(size) {
}
Process::STable::STable(Process &process, std::string interface_name, uint64_t addr) :
process(process),
interface_name(interface_name),
addr(addr) {
}
Process::Trap::Trap(Process &process, std::function<void(Thread&)> cb) :
trap_addr(process.RegisterTrap(*this)),
process(process),
cb(cb) {
}
Process::Trap::~Trap() {
process.UnregisterTrap(*this);
}
void Process::Trap::Hit(Thread &t) {
cb(t);
}
std::unique_ptr<InterfaceSniffer> Process::Sniff(const char *name) {
if(!has_scanned) {
ScanSTables();
}
auto i = s_tables.find(std::string(name));
if(i == s_tables.end()) {
throw ResultError(ILIA_ERR_NO_SUCH_S_TABLE);
}
return std::make_unique<InterfaceSniffer>(ilia, i->second);
}
std::unique_ptr<InterfaceSniffer> Process::Sniff(std::string name, uint64_t offset) {
if(!has_scanned) {
ScanSTables();
}
auto i = s_tables.emplace(name, STable(*this, name, likely_aslr_base + offset)).first;
return std::make_unique<InterfaceSniffer>(ilia, i->second);
}
void Process::HandleEvents() {
trn::Result<debug_event_info_t> r;
while((r = svc::GetDebugEvent(debug))) {
nx::DebugEvent event = {};
static_assert(sizeof(event) == sizeof(*r));
memcpy((void*) &event, (void*) &(*r), sizeof(event));
switch(event.event_type) {
case nx::DebugEvent::EventType::AttachProcess: {
if(has_attached) {
throw ResultError(ILIA_ERR_INVALID_PROCESS_STATE);
}
fprintf(stderr, "attached process '%s'\n", event.attach_process.process_name);
has_attached = true;
break; }
case nx::DebugEvent::EventType::AttachThread: {
if(!has_attached) {
throw ResultError(ILIA_ERR_INVALID_PROCESS_STATE);
}
auto i = threads.find(event.attach_thread.thread_id);
if(i != threads.end()) {
throw ResultError(ILIA_ERR_INVALID_THREAD_STATE);
}
threads.emplace(
event.attach_thread.thread_id, Thread(
*this,
event.attach_thread.thread_id,
event.attach_thread.tls_pointer,
event.attach_thread.entrypoint));
fprintf(stderr, "attached thread 0x%lx\n", event.attach_thread.thread_id);
break; }
case nx::DebugEvent::EventType::ExitProcess: {
fprintf(stderr, "ERROR: exited process?\n");
break; }
case nx::DebugEvent::EventType::ExitThread: {
threads.erase(event.thread_id);
fprintf(stderr, "exited thread 0x%lx\n", event.thread_id);
break; }
case nx::DebugEvent::EventType::Exception: {
switch(event.exception.exception_type) {
case nx::DebugEvent::ExceptionType::InstructionAbort: {
uint64_t far = event.exception.fault_register;
auto i = threads.find(event.thread_id);
if(i == threads.end()) {
fprintf(stderr, "ERROR: no such thread 0x%lx\n", event.thread_id);
break;
}
size_t index = LookupTrap(far);
if(traps[index] == nullptr) {
fprintf(stderr, "ERROR: no such trap 0x%lx\n", index);
} else {
traps[index]->Hit(i->second);
}
break; }
case nx::DebugEvent::ExceptionType::DebuggerAttached: {
fprintf(stderr, "got debugger attachment exception\n");
break; }
default:
fprintf(stderr, "ERROR: unhandled exception\n");
return;
}
break; }
default:
fprintf(stderr, "ERROR: unknown debug event?\n");
return;
}
}
if(r.error().code != 0x8c01) { // no events left
throw r.error();
}
for(auto &i : threads) {
i.second.CommitContext();
i.second.InvalidateContext();
}
ResultCode::AssertOk(
svc::ContinueDebugEvent(debug, 7, nullptr, 0));
}
uint64_t Process::RegisterTrap(Trap &t) {
if(!trap_free_list.empty()) {
size_t index = trap_free_list.front();
trap_free_list.pop_front();
traps[index] = &t;
return TrapAddress(index);
} else {
uint64_t addr = TrapAddress(traps.size());
traps.push_back(&t);
return addr;
}
}
void Process::UnregisterTrap(Trap &t) {
size_t index = LookupTrap(t.trap_addr);
if(traps[index] != &t) {
throw ResultError(ILIA_ERR_INVALID_TRAP);
}
traps[index] = nullptr;
trap_free_list.push_back(index);
}
size_t Process::LookupTrap(uint64_t addr) {
if(addr < TrapBaseAddress) {
throw ResultError(ILIA_ERR_INVALID_TRAP);
}
if(addr >= TrapBaseAddress + (traps.size() * TrapSize)) {
throw ResultError(ILIA_ERR_INVALID_TRAP);
}
return (addr - TrapBaseAddress) / TrapSize;
}
uint64_t Process::TrapAddress(size_t index) {
return TrapBaseAddress + (index * TrapSize);
}
void Process::ScanSTables() {
struct NsoInfo {
union {
uint8_t build_id[0x20];
uint64_t build_id_64[4];
};
uint64_t addr;
size_t size;
};
std::vector<NsoInfo> nso_infos(16, {0, 0, 0});
uint32_t num_nsos;
if(pid >= 0x50) {
ResultCode::AssertOk(
ilia.ldr_dmnt.SendSyncRequest<2>( // GetNsoInfos
ipc::InRaw<uint64_t>(pid),
ipc::OutRaw<uint32_t>(num_nsos),
ipc::Buffer<NsoInfo, 0xA>(nso_infos)));
} else {
uint64_t addr = 0;
memory_info_t mi;
while((mi = std::get<0>(ResultCode::AssertOk(svc::QueryDebugProcessMemory(debug, addr)))).memory_type != 3) {
if((uint64_t) mi.base_addr + mi.size < addr) {
fprintf(stderr, "giving up on finding module...\n");
return;
}
addr = (uint64_t) mi.base_addr + mi.size;
}
fprintf(stderr, "found module at 0x%lx\n", addr);
nso_infos[0] = {.addr = addr};
num_nsos = 1;
}
likely_aslr_base = nso_infos[0].addr;
for(uint32_t i = 0; i < num_nsos; i++) {
NsoInfo &info = nso_infos[i];
NSO &nso = nsos.emplace_back(*this, info.addr, info.size);
uint32_t mod_offset = Read<uint32_t>(info.addr + 4);
ModHeader hdr = Read<ModHeader>(info.addr + mod_offset);
std::map<int64_t, Elf64_Dyn> dyn_map;
uint64_t dyn_addr = info.addr + mod_offset + hdr.dynamic_off;
for(Elf64_Dyn dyn; (dyn = Read<Elf64_Dyn>(dyn_addr)).d_tag != DT_NULL; dyn_addr+= sizeof(dyn)) {
dyn_map[dyn.d_tag] = dyn;
}
if(dyn_map.find(DT_STRTAB) == dyn_map.end()) {
fprintf(stderr, " couldn't find string table\n");
continue;
}
if(dyn_map.find(DT_STRSZ) == dyn_map.end()) {
fprintf(stderr, " couldn't find string table size\n");
continue;
}
RemotePointer<char> string_table = RemotePointer<char>(debug, info.addr + dyn_map[DT_STRTAB].d_val);
if(dyn_map.find(DT_SYMTAB) == dyn_map.end()) {
fprintf(stderr, " couldn't find symbol table\n");
continue;
}
if(dyn_map.find(DT_HASH) == dyn_map.end()) {
fprintf(stderr, " couldn't find hash table\n");
continue;
}
uint32_t nchain = Access<uint32_t>(info.addr + dyn_map[DT_HASH].d_val)[1];
RemotePointer<Elf64_Sym> sym_table = Access<Elf64_Sym>(info.addr + dyn_map[DT_SYMTAB].d_val);
for(uint32_t i = 0; i < nchain; i++) {
Elf64_Sym sym = sym_table[i];
if(sym.st_name != 0) {
std::string name;
size_t p = sym.st_name;
char c;
while((c = string_table[p++])) {
name.push_back(c);
}
size_t pos = name.find("s_Table");
if(pos == std::string::npos) {
continue;
}
char *demangled = cplus_demangle_v3(name.c_str(), 0);
name = demangled;
free(demangled);
static const char prefix[] = "nn::sf::cmif::server::detail::CmifProcessFunctionTableGetter<";
static const char postfix[] = ", void>::s_Table";
if(name.compare(0, sizeof(prefix)-1, prefix) == 0 &&
name.compare(name.length() - (sizeof(postfix) - 1),
sizeof(postfix) - 1, postfix) == 0) {
name = name.substr(sizeof(prefix) - 1, name.length() - sizeof(prefix) + 1 - sizeof(postfix) + 1);
fprintf(stderr, " found s_Table: %s\n", name.c_str());
s_tables.emplace(name, STable(*this, name, info.addr + sym.st_value));
} else {
fprintf(stderr, " found non-matching s_Table: %s\n", name.c_str());
}
}
}
}
has_scanned = true;
}
} // namespace ilia