forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process-darwin.cpp
322 lines (281 loc) · 9.29 KB
/
Process-darwin.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
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek ([email protected])
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <mach-o/dyld.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
using namespace std;
#include <md5wrapper.h>
#include "MemAccess.h"
#include "Memory.h"
#include "VersionInfoFactory.h"
#include "VersionInfo.h"
#include "Error.h"
#include <string.h>
using namespace DFHack;
Process::Process(const VersionInfoFactory& known_versions) : identified(false), my_pe(0)
{
char path[1024];
char *real_path;
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0) {
real_path = realpath(path, NULL);
}
md5wrapper md5;
uint32_t length;
uint8_t first_kb [1024];
memset(first_kb, 0, sizeof(first_kb));
// get hash of the running DF process
my_md5 = md5.getHashFromFile(real_path, length, (char *) first_kb);
// create linux process, add it to the vector
auto vinfo = known_versions.getVersionInfoByMD5(my_md5);
if(vinfo)
{
my_descriptor = std::make_shared<VersionInfo>(*vinfo);
identified = true;
}
else
{
char * wd = getcwd(NULL, 0);
cerr << "Unable to retrieve version information.\n";
cerr << "File: " << real_path << endl;
cerr << "MD5: " << my_md5 << endl;
cerr << "working dir: " << wd << endl;
cerr << "length:" << length << endl;
cerr << "1KB hexdump follows:" << endl;
for(int i = 0; i < 64; i++)
{
fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
first_kb[i*16],
first_kb[i*16+1],
first_kb[i*16+2],
first_kb[i*16+3],
first_kb[i*16+4],
first_kb[i*16+5],
first_kb[i*16+6],
first_kb[i*16+7],
first_kb[i*16+8],
first_kb[i*16+9],
first_kb[i*16+10],
first_kb[i*16+11],
first_kb[i*16+12],
first_kb[i*16+13],
first_kb[i*16+14],
first_kb[i*16+15]
);
}
free(wd);
}
}
Process::~Process()
{
// Nothing to do here
}
string Process::doReadClassName (void * vptr)
{
//FIXME: BAD!!!!!
char * typeinfo = Process::readPtr(((char *)vptr - sizeof(void*)));
char * typestring = Process::readPtr(typeinfo + sizeof(void*));
string raw = readCString(typestring);
size_t start = raw.find_first_of("abcdefghijklmnopqrstuvwxyz");// trim numbers
size_t end = raw.length();
return raw.substr(start,end-start);
}
#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <mach/vm_region.h>
#include <mach/vm_statistics.h>
#include <dlfcn.h>
const char *
inheritance_strings[] = {
"SHARE", "COPY", "NONE", "DONATE_COPY",
};
const char *
behavior_strings[] = {
"DEFAULT", "RANDOM", "SEQUENTIAL", "RESQNTL", "WILLNEED", "DONTNEED",
};
void Process::getMemRanges( vector<t_memrange> & ranges )
{
static bool log_ranges = (getenv("DFHACK_LOG_MEM_RANGES") != NULL);
kern_return_t kr;
task_t the_task;
the_task = mach_task_self();
#ifdef DFHACK64
mach_vm_size_t vmsize;
mach_vm_address_t address;
vm_region_basic_info_data_64_t info;
#else
vm_size_t vmsize;
vm_address_t address;
vm_region_basic_info_data_t info;
#endif
mach_msg_type_number_t info_count;
vm_region_flavor_t flavor;
memory_object_name_t object;
kr = KERN_SUCCESS;
address = 0;
do {
#ifdef DFHACK64
flavor = VM_REGION_BASIC_INFO_64;
info_count = VM_REGION_BASIC_INFO_COUNT_64;
kr = mach_vm_region(the_task, &address, &vmsize, flavor,
(vm_region_info_64_t)&info, &info_count, &object);
#else
flavor = VM_REGION_BASIC_INFO;
info_count = VM_REGION_BASIC_INFO_COUNT;
kr = vm_region(the_task, &address, &vmsize, flavor,
(vm_region_info_t)&info, &info_count, &object);
#endif
if (kr == KERN_SUCCESS) {
if (info.reserved==1) {
address += vmsize;
continue;
}
Dl_info dlinfo;
int dlcheck;
dlcheck = dladdr((const void*)address, &dlinfo);
if (dlcheck==0) {
dlinfo.dli_fname = "";
}
t_memrange temp;
strncpy( temp.name, dlinfo.dli_fname, 1023 );
temp.name[1023] = 0;
temp.start = (void *) address;
temp.end = (void *) (address+vmsize);
temp.read = (info.protection & VM_PROT_READ);
temp.write = (info.protection & VM_PROT_WRITE);
temp.execute = (info.protection & VM_PROT_EXECUTE);
temp.shared = info.shared;
temp.valid = true;
ranges.push_back(temp);
if (log_ranges)
{
fprintf(stderr,
"%p-%p %8zuK %c%c%c/%c%c%c %11s %6s %10s uwir=%hu sub=%u dlname: %s\n",
(void*)address,
(void*)(address + vmsize),
size_t(vmsize >> 10),
(info.protection & VM_PROT_READ) ? 'r' : '-',
(info.protection & VM_PROT_WRITE) ? 'w' : '-',
(info.protection & VM_PROT_EXECUTE) ? 'x' : '-',
(info.max_protection & VM_PROT_READ) ? 'r' : '-',
(info.max_protection & VM_PROT_WRITE) ? 'w' : '-',
(info.max_protection & VM_PROT_EXECUTE) ? 'x' : '-',
inheritance_strings[info.inheritance],
(info.shared) ? "shared" : "-",
behavior_strings[info.behavior],
info.user_wired_count,
info.reserved,
dlinfo.dli_fname);
}
address += vmsize;
} else if (kr != KERN_INVALID_ADDRESS) {
/*if (the_task != MACH_PORT_NULL) {
mach_port_deallocate(mach_task_self(), the_task);
}*/
return;
}
} while (kr != KERN_INVALID_ADDRESS);
/* if (the_task != MACH_PORT_NULL) {
mach_port_deallocate(mach_task_self(), the_task);
}*/
}
uintptr_t Process::getBase()
{
return DEFAULT_BASE_ADDR; // Memory.h
}
int Process::adjustOffset(int offset, bool /*to_file*/)
{
return offset;
}
uint32_t Process::getTickCount()
{
struct timeval tp;
gettimeofday(&tp, NULL);
return (tp.tv_sec * 1000) + (tp.tv_usec / 1000);
}
string Process::getPath()
{
static string cached_path = "";
if (cached_path.size())
return cached_path;
char path[1024];
char *real_path;
uint32_t size = sizeof(path);
if (getcwd(path, size))
{
cached_path = string(path);
return cached_path;
}
if (_NSGetExecutablePath(path, &size) == 0) {
real_path = realpath(path, NULL);
}
else {
fprintf(stderr, "_NSGetExecutablePath failed!\n");
cached_path = ".";
return cached_path;
}
std::string path_string(real_path);
int last_slash = path_string.find_last_of("/");
cached_path = path_string.substr(0,last_slash);
return cached_path;
}
int Process::getPID()
{
return getpid();
}
bool Process::setPermisions(const t_memrange & range,const t_memrange &trgrange)
{
int result;
int protect=0;
if(trgrange.read)protect|=PROT_READ;
if(trgrange.write)protect|=PROT_WRITE;
if(trgrange.execute)protect|=PROT_EXEC;
result=mprotect((void *)range.start, (size_t)range.end-(size_t)range.start,protect);
return result==0;
}
// returns -1 on error
void* Process::memAlloc(const int length)
{
return mmap(0, length, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
}
int Process::memDealloc(void *ptr, const int length)
{
return munmap(ptr, length);
}
int Process::memProtect(void *ptr, const int length, const int prot)
{
int prot_native = 0;
if (prot & Process::MemProt::READ)
prot_native |= PROT_READ;
if (prot & Process::MemProt::WRITE)
prot_native |= PROT_WRITE;
if (prot & Process::MemProt::EXEC)
prot_native |= PROT_EXEC;
return mprotect(ptr, length, prot_native);
}