Skip to content

Commit 3377b2d

Browse files
committed
Almost ported
1 parent c377075 commit 3377b2d

File tree

18 files changed

+454
-743
lines changed

18 files changed

+454
-743
lines changed

.DS_Store

2 KB
Binary file not shown.

NativeCore/.DS_Store

2 KB
Binary file not shown.

NativeCore/Dependencies/.DS_Store

6 KB
Binary file not shown.
8 KB
Binary file not shown.

NativeCore/Unix/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
//#include <sys/types.h>
22
#include <csignal>
33

4+
#if __APPLE__
5+
#include <sys/proc_info.h>
6+
#include <libproc.h>
7+
#include <mach/mach_init.h>
8+
#include <mach/mach_vm.h>
9+
#endif
410
#include "NativeCore.hpp"
511

612
extern "C" void RC_CallConv ControlRemoteProcess(RC_Pointer handle, ControlRemoteProcessAction action)
@@ -14,13 +20,6 @@ extern "C" void RC_CallConv ControlRemoteProcess(RC_Pointer handle, ControlRemot
1420
{
1521
signal = SIGCONT;
1622
}
17-
#ifdef __linux__
1823
kill(static_cast<pid_t>(reinterpret_cast<intptr_t>(handle)), signal);
19-
#elif __APPLE__
20-
task_t task;
21-
22-
task_for_pid(current_task(), (int)id, &task);
23-
return (RC_Pointer)task;
24-
#endif
2524

2625
}

NativeCore/Unix/Debugger.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
#include <sys/ptrace.h>
1+
22
#include <sys/types.h>
33
#include <sys/wait.h>
44
#include <sys/time.h>
55
#include <sys/user.h>
6+
#ifdef __linux__
7+
#include <sys/ptrace.h>
68
#include <experimental/filesystem>
7-
#include <cstddef>
9+
#endif
10+
811

912
#include "NativeCore.hpp"
1013

14+
#ifdef __linux__
1115
namespace fs = std::experimental::filesystem;
16+
#endif
1217

1318
int ualarm(unsigned int milliseconds)
1419
{
@@ -24,6 +29,7 @@ int ualarm(unsigned int milliseconds)
2429

2530
pid_t waitpid_timeout(pid_t pid, int* status, int options, int timeoutInMilliseconds, bool& timedOut)
2631
{
32+
#ifdef __linux__
2733
struct sigaction sig = {};
2834
sig.sa_flags = 0;
2935
sig.sa_handler = [](int) {};
@@ -44,6 +50,9 @@ pid_t waitpid_timeout(pid_t pid, int* status, int options, int timeoutInMillisec
4450
timedOut = false;
4551
}
4652
return res;
53+
#else
54+
return 0;
55+
#endif
4756
}
4857

4958
pid_t waitpid_timeout(int* status, int timeoutInMilliseconds, bool& timedOut)
@@ -54,25 +63,27 @@ pid_t waitpid_timeout(int* status, int timeoutInMilliseconds, bool& timedOut)
5463
extern "C" bool RC_CallConv AttachDebuggerToProcess(RC_Pointer id)
5564
{
5665
//TODO: Attach to all threads.
57-
66+
#ifdef __linux__
5867
ptrace(PTRACE_ATTACH, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);
5968

6069
waitpid(-1, nullptr, 0);
6170

6271
ptrace(PTRACE_CONT, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);
63-
72+
#endif
6473
return false;
6574
}
6675

6776
extern "C" void RC_CallConv DetachDebuggerFromProcess(RC_Pointer id)
6877
{
6978
//TODO: Detach to all threads.
70-
79+
#ifdef __linux__
7180
ptrace(PTRACE_DETACH, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);
81+
#endif
7282
}
7383

7484
extern "C" bool RC_CallConv AwaitDebugEvent(DebugEvent* evt, int timeoutInMilliseconds)
7585
{
86+
#ifdef __linux__
7687
int status;
7788
bool timedOut;
7889

@@ -167,10 +178,14 @@ extern "C" bool RC_CallConv AwaitDebugEvent(DebugEvent* evt, int timeoutInMillis
167178
}
168179

169180
return result;
181+
#else
182+
return false;
183+
#endif
170184
}
171185

172186
extern "C" void RC_CallConv HandleDebugEvent(DebugEvent* evt)
173187
{
188+
#ifdef __linux__
174189
auto tid = static_cast<pid_t>(reinterpret_cast<intptr_t>(evt->ThreadId));
175190

176191
siginfo_t si;
@@ -194,10 +209,12 @@ extern "C" void RC_CallConv HandleDebugEvent(DebugEvent* evt)
194209

195210
ptrace(PTRACE_CONT, tid, nullptr, signal);
196211
}
212+
#endif
197213
}
198214

199215
extern "C" bool RC_CallConv SetHardwareBreakpoint(RC_Pointer id, RC_Pointer address, HardwareBreakpointRegister reg, HardwareBreakpointTrigger type, HardwareBreakpointSize size, bool set)
200216
{
217+
#ifdef __linux__
201218
if (reg == HardwareBreakpointRegister::InvalidRegister)
202219
{
203220
return false;
@@ -295,6 +312,6 @@ extern "C" bool RC_CallConv SetHardwareBreakpoint(RC_Pointer id, RC_Pointer addr
295312
}
296313
}
297314
}
298-
315+
#endif
299316
return true;
300317
}

NativeCore/Unix/EnumerateProcesses.cpp

Lines changed: 91 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,57 @@
22
#include <string>
33
#include <sstream>
44
#include <fstream>
5+
#ifdef __linux__
56
#include <experimental/filesystem>
67

8+
#elif __APPLE__
9+
#include <sys/proc_info.h>
10+
#include <libproc.h>
11+
#endif
712
#include "NativeCore.hpp"
813

14+
#ifdef __linux__
915
namespace fs = std::experimental::filesystem;
16+
#endif
1017

11-
// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
12-
#define USE_CUSTOM_READ_SYMLINK
13-
14-
#ifdef USE_CUSTOM_READ_SYMLINK
15-
#include <unistd.h>
16-
17-
fs::path my_read_symlink(const fs::path& p, std::error_code& ec)
18-
{
19-
fs::path symlink_path;
20-
21-
std::string temp(64, '\0');
22-
for (;; temp.resize(temp.size() * 2))
23-
{
24-
ssize_t result;
25-
if ((result = ::readlink(p.c_str(), /*temp.data()*/ &temp[0], temp.size())) == -1)
26-
{
27-
ec.assign(errno, std::system_category());
28-
break;
29-
}
30-
else
31-
{
32-
if (result != static_cast<ssize_t>(temp.size()))
33-
{
34-
symlink_path = fs::path(std::string(temp.begin(), temp.begin() + result));
35-
36-
ec.clear();
37-
38-
break;
39-
}
40-
}
41-
}
42-
43-
return symlink_path;
44-
}
4518

19+
// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
20+
#ifdef __linux__
21+
#define USE_CUSTOM_READ_SYMLINK
22+
23+
#ifdef USE_CUSTOM_READ_SYMLINK
24+
#include <unistd.h>
25+
26+
fs::path my_read_symlink(const fs::path& p, std::error_code& ec)
27+
{
28+
fs::path symlink_path;
29+
30+
std::string temp(64, '\0');
31+
for (;; temp.resize(temp.size() * 2))
32+
{
33+
ssize_t result;
34+
if ((result = ::readlink(p.c_str(), /*temp.data()*/ &temp[0], temp.size())) == -1)
35+
{
36+
ec.assign(errno, std::system_category());
37+
break;
38+
}
39+
else
40+
{
41+
if (result != static_cast<ssize_t>(temp.size()))
42+
{
43+
symlink_path = fs::path(std::string(temp.begin(), temp.begin() + result));
44+
45+
ec.clear();
46+
47+
break;
48+
}
49+
}
50+
}
51+
52+
return symlink_path;
53+
}
54+
55+
#endif
4656
#endif
4757

4858
enum class Platform
@@ -85,7 +95,7 @@ extern "C" void RC_CallConv EnumerateProcesses(EnumerateProcessCallback callback
8595
{
8696
return;
8797
}
88-
98+
#ifdef __linux__
8999
fs::path procPath("/proc");
90100
if (fs::is_directory(procPath))
91101
{
@@ -134,4 +144,50 @@ extern "C" void RC_CallConv EnumerateProcesses(EnumerateProcessCallback callback
134144
}
135145
}
136146
}
147+
#elif __APPLE__
148+
int procCnt = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
149+
pid_t pids[1024];
150+
memset(pids, 0, sizeof pids);
151+
proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));
152+
153+
for (int i = 0; i < procCnt; i++)
154+
{
155+
if (!pids[i]) continue;
156+
char curPath[PROC_PIDPATHINFO_MAXSIZE];
157+
char curName[PROC_PIDPATHINFO_MAXSIZE];
158+
memset(curPath, 0, sizeof curPath);
159+
proc_pidpath(pids[i], curPath, sizeof curPath);
160+
int len = strlen(curPath);
161+
if (len)
162+
{
163+
int pos = len;
164+
while (pos && curPath[pos] != '/') --pos;
165+
strcpy(curName, curPath + pos + 1);
166+
167+
struct proc_bsdinfo bsd_info;
168+
int error = proc_pidinfo (pids[i], PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
169+
if (error == 0)
170+
continue;
171+
172+
auto platform = Platform::X86;
173+
174+
if (bsd_info.pbi_flags & PROC_FLAG_LP64)
175+
platform = Platform::X64;
176+
177+
#ifdef RECLASSNET64
178+
if (platform == Platform::X64)
179+
#else
180+
if (platform == Platform::X86)
181+
#endif
182+
{
183+
EnumerateProcessData data = {};
184+
data.Id = (size_t)pids[i];
185+
MultiByteToUnicode(curPath, data.Path, PATH_MAXIMUM_LENGTH);
186+
MultiByteToUnicode(curName, data.Name, PATH_MAXIMUM_LENGTH);
187+
callbackProcess(&data);
188+
}
189+
190+
}
191+
}
192+
#endif
137193
}

NativeCore/Unix/EnumerateRemoteSectionsAndModules.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ std::istream& operator >> (std::istream& s, SectionProtection& protection)
3838

3939
extern "C" void RC_CallConv EnumerateRemoteSectionsAndModules(RC_Pointer handle, EnumerateRemoteSectionsCallback callbackSection, EnumerateRemoteModulesCallback callbackModule)
4040
{
41+
#ifdef __APPLE__
42+
return;
43+
#endif
44+
4145
if (callbackSection == nullptr && callbackModule == nullptr)
4246
{
4347
return;

NativeCore/Unix/Makefile

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ INC = -I../Dependencies/distorm/include
1010
CFLAGS = -Wall -fPIC -DRECLASSNET64=1
1111
RESINC =
1212
LIBDIR =
13-
LIB = -lstdc++fs -lstdc++ -lc++experimental
14-
LDFLAGS = -shared -Wl,--no-undefined
13+
LIB = -lstdc++
14+
LDFLAGS = --shared -Wl
1515

1616
INC_DEBUG = $(INC)
1717
CFLAGS_DEBUG = $(CFLAGS) -g
@@ -91,31 +91,31 @@ $(OBJDIR_DEBUG)/CloseRemoteProcess.o: CloseRemoteProcess.cpp
9191
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c CloseRemoteProcess.cpp -o $(OBJDIR_DEBUG)/CloseRemoteProcess.o
9292

9393
$(OBJDIR_DEBUG)/decoder.o: ../Dependencies/distorm/src/decoder.c
94-
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/decoder.c -o $(OBJDIR_DEBUG)/decoder.o
94+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/decoder.c -o $(OBJDIR_DEBUG)/decoder.o
9595

9696
$(OBJDIR_DEBUG)/distorm.o: ../Dependencies/distorm/src/distorm.c
97-
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/distorm.c -o $(OBJDIR_DEBUG)/distorm.o
97+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/distorm.c -o $(OBJDIR_DEBUG)/distorm.o
9898

9999
$(OBJDIR_DEBUG)/instructions.o: ../Dependencies/distorm/src/instructions.c
100-
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/instructions.c -o $(OBJDIR_DEBUG)/instructions.o
100+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/instructions.c -o $(OBJDIR_DEBUG)/instructions.o
101101

102102
$(OBJDIR_DEBUG)/insts.o: ../Dependencies/distorm/src/insts.c
103-
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/insts.c -o $(OBJDIR_DEBUG)/insts.o
103+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/insts.c -o $(OBJDIR_DEBUG)/insts.o
104104

105105
$(OBJDIR_DEBUG)/mnemonics.o: ../Dependencies/distorm/src/mnemonics.c
106-
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/mnemonics.c -o $(OBJDIR_DEBUG)/mnemonics.o
106+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/mnemonics.c -o $(OBJDIR_DEBUG)/mnemonics.o
107107

108108
$(OBJDIR_DEBUG)/operands.o: ../Dependencies/distorm/src/operands.c
109-
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/operands.c -o $(OBJDIR_DEBUG)/operands.o
109+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/operands.c -o $(OBJDIR_DEBUG)/operands.o
110110

111111
$(OBJDIR_DEBUG)/prefix.o: ../Dependencies/distorm/src/prefix.c
112-
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/prefix.c -o $(OBJDIR_DEBUG)/prefix.o
112+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/prefix.c -o $(OBJDIR_DEBUG)/prefix.o
113113

114114
$(OBJDIR_DEBUG)/textdefs.o: ../Dependencies/distorm/src/textdefs.c
115-
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/textdefs.c -o $(OBJDIR_DEBUG)/textdefs.o
115+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/textdefs.c -o $(OBJDIR_DEBUG)/textdefs.o
116116

117117
$(OBJDIR_DEBUG)/wstring.o: ../Dependencies/distorm/src/wstring.c
118-
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/wstring.c -o $(OBJDIR_DEBUG)/wstring.o
118+
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/wstring.c -o $(OBJDIR_DEBUG)/wstring.o
119119

120120
clean_debug:
121121
rm -f $(OBJ_DEBUG) $(OUT_DEBUG)
@@ -170,31 +170,31 @@ $(OBJDIR_RELEASE)/CloseRemoteProcess.o: CloseRemoteProcess.cpp
170170
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c CloseRemoteProcess.cpp -o $(OBJDIR_RELEASE)/CloseRemoteProcess.o
171171

172172
$(OBJDIR_RELEASE)/decoder.o: ../Dependencies/distorm/src/decoder.c
173-
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/decoder.c -o $(OBJDIR_RELEASE)/decoder.o
173+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/decoder.c -o $(OBJDIR_RELEASE)/decoder.o
174174

175175
$(OBJDIR_RELEASE)/distorm.o: ../Dependencies/distorm/src/distorm.c
176-
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/distorm.c -o $(OBJDIR_RELEASE)/distorm.o
176+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/distorm.c -o $(OBJDIR_RELEASE)/distorm.o
177177

178178
$(OBJDIR_RELEASE)/instructions.o: ../Dependencies/distorm/src/instructions.c
179-
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/instructions.c -o $(OBJDIR_RELEASE)/instructions.o
179+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/instructions.c -o $(OBJDIR_RELEASE)/instructions.o
180180

181181
$(OBJDIR_RELEASE)/insts.o: ../Dependencies/distorm/src/insts.c
182-
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/insts.c -o $(OBJDIR_RELEASE)/insts.o
182+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/insts.c -o $(OBJDIR_RELEASE)/insts.o
183183

184184
$(OBJDIR_RELEASE)/mnemonics.o: ../Dependencies/distorm/src/mnemonics.c
185-
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/mnemonics.c -o $(OBJDIR_RELEASE)/mnemonics.o
185+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/mnemonics.c -o $(OBJDIR_RELEASE)/mnemonics.o
186186

187187
$(OBJDIR_RELEASE)/operands.o: ../Dependencies/distorm/src/operands.c
188-
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/operands.c -o $(OBJDIR_RELEASE)/operands.o
188+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/operands.c -o $(OBJDIR_RELEASE)/operands.o
189189

190190
$(OBJDIR_RELEASE)/prefix.o: ../Dependencies/distorm/src/prefix.c
191-
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/prefix.c -o $(OBJDIR_RELEASE)/prefix.o
191+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/prefix.c -o $(OBJDIR_RELEASE)/prefix.o
192192

193193
$(OBJDIR_RELEASE)/textdefs.o: ../Dependencies/distorm/src/textdefs.c
194-
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/textdefs.c -o $(OBJDIR_RELEASE)/textdefs.o
194+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/textdefs.c -o $(OBJDIR_RELEASE)/textdefs.o
195195

196196
$(OBJDIR_RELEASE)/wstring.o: ../Dependencies/distorm/src/wstring.c
197-
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/wstring.c -o $(OBJDIR_RELEASE)/wstring.o
197+
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/wstring.c -o $(OBJDIR_RELEASE)/wstring.o
198198

199199
clean_release:
200200
rm -f $(OBJ_RELEASE) $(OUT_RELEASE)

NativeCore/Unix/NativeCore.Unix.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
buildPhases = (
5353
);
5454
buildToolPath = /usr/bin/make;
55-
buildWorkingDirectory = /Users/h3xc0r3/Documents/GitHub/ReClass.NET/NativeCore/Unix;
55+
buildWorkingDirectory = /Users/tarek/ReClass.NET/NativeCore/Unix;
5656
dependencies = (
5757
);
5858
name = NativeCore.Unix;

0 commit comments

Comments
 (0)