fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosReduce amount of cdata by caching ffi.new valuesfaed443

v2.1.6
6.5 KiB281 linesraw
1local ffi = require("ffi")
2local log = require("log")
3
4local format = string.format
5local sub = string.sub
6
7local cast = ffi.cast
8local cdef = ffi.cdef
9local load = ffi.load
10local metatype = ffi.metatype
11local new = ffi.new
12local typeof = ffi.typeof
13local sizeof = ffi.sizeof
14local string = ffi.string
15local errno = ffi.errno
16
17local lshift = bit.lshift
18local rshift = bit.rshift
19local bswap = bit.bswap
20local band = bit.band
21local bor = bit.bor
22
23local libc = ffi.C
24local libcap = load("cap")
25
26cdef [[
27char *strerror(int errnum);
28
29typedef signed int pid_t;
30
31typedef struct {
32	pid_t pid;
33	unsigned __int64  dolphin_base_addr;
34	unsigned __int64  dolphin_addr_size;
35} MEMORY_STRUCT;
36
37typedef struct {
38	void *iov_base;	/* pointer to start of buffer */
39	size_t iov_len;	/* size of buffer in bytes */
40} iovec;
41
42int process_vm_readv(pid_t pid,
43						 iovec *local_iov,
44						 unsigned long liovcnt,
45						 iovec *remote_iov,
46						 unsigned long riovcnt,
47						 unsigned long flags);
48
49int process_vm_writev(pid_t pid,
50						  iovec *local_iov,
51						  unsigned long liovcnt,
52						  iovec *remote_iov,
53						  unsigned long riovcnt,
54						  unsigned long flags);
55
56typedef unsigned short ino_t; 	   /* i-node number */
57typedef unsigned long  off_t;	   /* offset within a file */
58
59typedef struct {
60	ino_t          d_ino;       /* Inode number */
61	off_t          d_off;       /* Not an offset; see below */
62	unsigned short d_reclen;    /* Length of this record */
63	unsigned char  d_type;      /* Type of file; not supported
64								  by all filesystem types */
65	char           name[256];   /* Null-terminated filename */
66} dirent;
67typedef struct __dirstream DIR;
68
69int access(const char *path, int amode);
70DIR *opendir(const char *name);
71int closedir(DIR *dirp);
72dirent *readdir(DIR *dirp);
73
74typedef enum {
75    CAP_EFFECTIVE=0,                      /* Specifies the effective flag */
76    CAP_PERMITTED=1,                      /* Specifies the permitted flag */
77    CAP_INHERITABLE=2                     /* Specifies the inheritable flag */
78} cap_flag_t;
79
80typedef enum {
81    CAP_CLEAR=0,                            /* The flag is cleared/disabled */
82    CAP_SET=1                               /* The flag is set/enabled */
83} cap_flag_value_t;
84
85typedef void* cap_t;
86typedef int cap_value_t;
87
88cap_t cap_get_proc(void);
89int cap_free(void *);
90int cap_set_proc(cap_t cap_p);
91int cap_set_flag(cap_t, cap_flag_t, int, const cap_value_t *, cap_flag_value_t);
92]]
93
94local CAP_SYS_PTRACE = 19
95
96local MEMORY = {}
97MEMORY.__index = MEMORY
98MEMORY.init = metatype("MEMORY_STRUCT", MEMORY)
99
100function MEMORY:hasPermissions()
101	local cap = libcap.cap_get_proc()
102
103	if cap == nil then
104		return false
105	end
106
107	local flag =  new("cap_value_t[1]")
108	flag[0] = CAP_SYS_PTRACE
109
110	if libcap.cap_set_flag(cap, libcap.CAP_EFFECTIVE, 1, flag, libcap.CAP_SET) == -1 then
111		libcap.cap_free(cap)
112		return false
113	end
114
115	if libcap.cap_set_proc(cap) ~= 0 then
116		libcap.cap_free(cap)
117		return false
118	end
119
120	libcap.cap_free(cap)
121	return true
122end
123
124
125local valid_process_names = {
126	["AppRun"] = true,
127	["AppRun.wrapped"] = true,
128	["dolphin-emu"] = true,
129	["dolphin-emu-qt2"] = true,
130	["dolphin-emu-wx"] = true,
131	["launch-fm"] = true,
132	["slippi-r18-netplay"] = true,
133	["slippi-r16-netplay"] = true,
134	["slippi-r11-netplay"] = true,
135	["slippi-r10-netplay"] = true,
136}
137
138function MEMORY:findprocess()
139	if self:hasProcess() then return false end
140
141	local dir = libc.opendir("/proc/")
142
143	if dir == nil then
144		local message = string(libc.strerror(errno()))
145		error("error opening directory /proc/: " .. message)
146	else
147		local entry
148		while true do
149			entry = libc.readdir(dir)
150			if entry == nil then break end -- end of list
151			local pid = string(entry.name)
152			local f = io.open("/proc/" .. pid .. "/comm")
153			if f then
154				local line = f:read("*line")
155				if valid_process_names[line] then
156					self.pid = tonumber(pid)
157					log.debug("Found dolphin-emu process: /proc/%d", self.pid)
158				end
159				f:close()
160			end
161		end
162		libc.closedir(dir)
163	end
164
165	return self:hasProcess()
166end
167
168function MEMORY:isProcessActive()
169	if self.pid then
170		return libc.access("/proc/" .. self.pid, 0) == 0
171	end
172	return false
173end
174
175function MEMORY:hasProcess()
176	return self.pid ~= 0
177end
178
179function MEMORY:clearGamecubeRAMOffset()
180	self.dolphin_base_addr = 0
181end
182
183function MEMORY:hasGamecubeRAMOffset()
184	return self.dolphin_base_addr ~= 0
185end
186
187function MEMORY:getGamecubeRAMOffset()
188	return tonumber(self.dolphin_base_addr)
189end
190
191function MEMORY:getGamecubeRAMSize()
192	return tonumber(self.dolphin_addr_size)
193end
194
195function MEMORY:close()
196	if self:hasProcess() then
197		self.pid = 0
198		self.dolphin_base_addr = 0
199	end
200end
201
202function MEMORY:__gc()
203	self:close()
204end
205
206function MEMORY:findGamecubeRAMOffset()
207	if self:hasProcess() then
208		local f = io.open("/proc/" .. self.pid .. "/maps")
209		if f then
210			--log.debug("Searching /proc/%d/maps for gamecube address space", self.pid)
211			local line
212			while true do
213				line = f:read("*line")
214				if not line then break end -- EOF
215				if #line > 74 then
216					if sub(line, 74, 74 + 18) == "/dev/shm/dolphinmem" or sub(line, 74, 74 + 19) == "/dev/shm/dolphin-emu" then
217						local startAddr, endAddr = line:match("^(%x-)%-(%x-)%s")
218						if startAddr and endAddr then
219							-- Convert hex values to number
220							startAddr = tonumber(startAddr, 16)
221							endAddr = tonumber(endAddr, 16)
222							if (endAddr - startAddr) >= 0x2000000 then
223								self.dolphin_base_addr = startAddr
224								self.dolphin_addr_size = endAddr - startAddr
225								f:close()
226								return true
227							end
228						end
229					end
230				end
231			end
232			f:close()
233		end
234	end
235
236	return false
237end
238
239local localvec = new("iovec[1]")
240local remotevec = new("iovec[1]")
241
242function MEMORY:read(addr, output, size)
243	local ramaddr = self.dolphin_base_addr + (addr % 0x80000000)	
244
245	localvec[0].iov_base = output
246	localvec[0].iov_len = size
247
248	remotevec[0].iov_base = cast("void*", ramaddr)
249	remotevec[0].iov_len = size
250
251	local read = libc.process_vm_readv(self.pid, localvec, 1, remotevec, 1, 0)
252
253	if read ~= size then
254		local message = libc.strerror(errno())
255		log.warn("[MEMORY] Failed reading from address [%08X] %s", addr, message)
256		self:close()
257	end
258
259	return read == size
260end
261
262function MEMORY:write(addr, input, size)
263	local ramaddr = self.dolphin_base_addr + (addr % 0x80000000)	
264
265	localvec[0].iov_base = input
266	localvec[0].iov_len = size
267
268	remotevec[0].iov_base = cast("void*", ramaddr)
269	remotevec[0].iov_len = size
270
271	local read = libc.process_vm_writev(self.pid, localvec, 1, remotevec, 1, 0)
272
273	if read ~= size then
274		log.warn("Failed writing process memory..")
275		self:close()
276	end
277
278	return read == size
279end
280
281return MEMORY