-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinylib.h
368 lines (295 loc) · 9.93 KB
/
tinylib.h
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#ifndef TINYLIB_H_
#define TINYLIB_H_
// https://git.musl-libc.org/cgit/musl/tree/arch/x86_64/syscall_arch.h
typedef unsigned long size_t;
typedef signed long ssize_t;
typedef signed long off_t;
typedef unsigned long long uint64_t;
typedef signed long long int64_t;
typedef unsigned int uint32_t;
typedef signed int int32_t;
typedef unsigned short uint16_t;
typedef signed short int16_t;
typedef unsigned char uint8_t;
typedef signed char int8_t;
#define NULL ((void *)0)
static __inline long __syscall0(long n)
{
unsigned long ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n) : "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall1(long n, long a1)
{
unsigned long ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1) : "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall2(long n, long a1, long a2)
{
unsigned long ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2)
: "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall3(long n, long a1, long a2, long a3)
{
unsigned long ret;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3) : "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall4(long n, long a1, long a2, long a3, long a4)
{
unsigned long ret;
register long r10 __asm__("r10") = a4;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3), "r"(r10): "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall5(long n, long a1, long a2, long a3, long a4, long a5)
{
unsigned long ret;
register long r10 __asm__("r10") = a4;
register long r8 __asm__("r8") = a5;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3), "r"(r10), "r"(r8) : "rcx", "r11", "memory");
return ret;
}
static __inline long __syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6)
{
unsigned long ret;
register long r10 __asm__("r10") = a4;
register long r8 __asm__("r8") = a5;
register long r9 __asm__("r9") = a6;
__asm__ __volatile__ ("syscall" : "=a"(ret) : "a"(n), "D"(a1), "S"(a2),
"d"(a3), "r"(r10), "r"(r8), "r"(r9) : "rcx", "r11", "memory");
return ret;
}
// =============================================================================
static inline ssize_t read(int fd, void *buf, size_t count) {
return (ssize_t)__syscall3(0, (long)fd, (long)buf, (long)count);
}
static inline ssize_t write(int fd, const void *buf, size_t count) {
return (ssize_t)__syscall3(1, (long)fd, (long)buf, (long)count);
}
static inline ssize_t pread64(int fd, void *buf, size_t count, off_t offset) {
return (ssize_t)__syscall4(17, (long)fd, (long)buf, (long)count, (long)offset);
}
static inline ssize_t pwrite64(int fd, const void *buf, size_t count, off_t offset) {
return (ssize_t)__syscall4(18, (long)fd, (long)buf, (long)count, (long)offset);
}
struct iovec {
void *iov_base; /* Starting address */
size_t iov_len; /* Number of bytes to transfer */
};
static inline ssize_t writev(int fd, const struct iovec *iov, int iovcnt) {
return (ssize_t)__syscall3(20, (long)fd, (long)iov, (long)iovcnt);
}
// https://git.musl-libc.org/cgit/musl/tree/include/fcntl.h
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 00000100
#define O_TRUNC 00001000
static inline int open(const char *pathname, int flags) {
return (int)__syscall2(2, (long)pathname, flags);
}
static inline int openat(int dirfd, const char *pathname, int flags) {
return (int)__syscall3(257, (long)dirfd, (long)pathname, flags);
}
static inline int close(int fd) {
return (int)__syscall1(3, (long)fd);
}
// https://git.musl-libc.org/cgit/musl/tree/include/sys/mman.h
#define MAP_SHARED 0x01
#define MAP_PRIVATE 0x02
#define MAP_ANONYMOUS 0x20
#define MAP_NORESERVE 0x4000
#define PROT_NONE 0
#define PROT_READ 1
#define PROT_WRITE 2
#define PROT_EXEC 4
static inline void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
return (void *)__syscall6(9, (long)addr, (long)length, (long)prot, (long)flags, (long)fd, (long)offset);
}
static inline int munmap(void *addr, size_t length) {
return (int)__syscall2(11, (long)addr, (long)length);
}
static inline int execve(const char *pathname, char *const argv[], char *const envp[]) {
return (int)__syscall3(59, (long)pathname, (long)argv, (long)envp);
}
static inline void exit(int status) {
__syscall1(60, (long)status);
}
static inline int ioctl(int fd, unsigned long request, unsigned long data) {
return __syscall3(16, (long)fd, (long)request, (long)data);
}
static inline int symlink(const char *target, const char *linkpath) {
return (int)__syscall2(88, (long)target, (long)linkpath);
}
#define SEEK_SET 0
static inline off_t lseek(int fd, off_t offset, int whence) {
return __syscall3(8, (long)fd, (long)offset, (long)whence);
}
#define AT_EMPTY_PATH 0x1000
static inline int execveat(int dirfd, const char *pathname, char *const argv[], char *const envp[], int flags) {
return __syscall5(322, (long)dirfd, (long)pathname, (long)argv, (long)envp, (long)flags);
}
static inline int memfd_create(const char *name, unsigned int flags) {
return __syscall2(319, (long)name, flags);
}
#define AF_ALG 38
#define SOCK_SEQPACKET 5
typedef uint32_t socklen_t;
struct sockaddr {
uint16_t sa_family;
char sa_data[];
};
struct sockaddr_alg {
uint16_t salg_family;
uint8_t salg_type[14];
uint32_t salg_feat;
uint32_t salg_mask;
uint8_t salg_name[64];
};
static inline int socket(int domain, int type, int protocol) {
return __syscall3(41, (long)domain, (long)type, (long)protocol);
}
static inline int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
return __syscall3(49, (long)sockfd, (long)addr, (long)addrlen);
}
static inline int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
return __syscall3(43, (long)sockfd, (long)addr, (long)addrlen);
}
// https://unix.superglobalmegacorp.com/Net2/newsrc/sys/ipc.h.html
#define IPC_CREAT 01000 /* create entry if key does not exist */
#define IPC_EXCL 02000 /* fail if key exists */
typedef long key_t;
static inline int shmget(key_t key, size_t size, int shmflg) {
return __syscall3(29, (long)key, (long)size, (long)shmflg);
}
static inline void *shmat(int shmid, const void *shmaddr, int shmflg) {
return (void *)__syscall3(30, (long)shmid, (long)shmaddr, (long)shmflg);
}
static inline int shmdt(const void *shmaddr) {
return __syscall1(67, (long)shmaddr);
}
typedef uint32_t uid_t;
typedef uint32_t gid_t;
static inline int setresuid(uid_t ruid, uid_t euid, uid_t suid) {
return __syscall3(117, (long)ruid, (long)euid, (long)suid);
}
static inline int setresgid(gid_t rgid, gid_t egid, gid_t sgid) {
return __syscall3(119, (long)rgid, (long)egid, (long)sgid);
}
static inline int chdir(const char *path) {
return __syscall1(80, (long)path);
}
static inline int fchdir(int fd) {
return __syscall1(81, (long)fd);
}
static inline int dup2(int oldfd, int newfd) {
return __syscall2(33, (long)oldfd, (long)newfd);
}
static inline int chroot(const char *path) {
return __syscall1(161, (long)path);
}
#define R_OK 4 /* Test for read permission. */
#define W_OK 2 /* Test for write permission. */
#define X_OK 1 /* Test for execute permission. */
#define F_OK 0 /* Test for existence. */
static inline int access(const char *pathname, int mode) {
return __syscall2(21, (long)pathname, (long)mode);
}
typedef uint32_t mode_t;
static inline int mkdir(const char *pathname, mode_t mode) {
return __syscall2(83, (long)pathname, (long)mode);
}
typedef int pid_t;
struct user_regs_struct {
unsigned long r15;
unsigned long r14;
unsigned long r13;
unsigned long r12;
unsigned long rbp;
unsigned long rbx;
unsigned long r11;
unsigned long r10;
unsigned long r9;
unsigned long r8;
unsigned long rax;
unsigned long rcx;
unsigned long rdx;
unsigned long rsi;
unsigned long rdi;
unsigned long orig_rax;
unsigned long rip;
unsigned long cs;
unsigned long eflags;
unsigned long rsp;
unsigned long ss;
unsigned long fs_base;
unsigned long gs_base;
unsigned long ds;
unsigned long es;
unsigned long fs;
unsigned long gs;
};
#define PTRACE_TRACEME 0
#define PTRACE_PEEKTEXT 1
#define PTRACE_PEEKDATA 2
#define PTRACE_PEEKUSER 3
#define PTRACE_POKETEXT 4
#define PTRACE_POKEDATA 5
#define PTRACE_POKEUSER 6
#define PTRACE_CONT 7
#define PTRACE_KILL 8
#define PTRACE_SINGLESTEP 9
#define PTRACE_GETREGS 12
#define PTRACE_SETREGS 13
#define PTRACE_GETFPREGS 14
#define PTRACE_SETFPREGS 15
#define PTRACE_ATTACH 16
#define PTRACE_DETACH 17
#define PTRACE_GETFPXREGS 18
#define PTRACE_SETFPXREGS 19
#define PTRACE_SYSCALL 24
#define PTRACE_SETOPTIONS 0x4200
#define PTRACE_GETEVENTMSG 0x4201
#define PTRACE_GETSIGINFO 0x4202
#define PTRACE_SETSIGINFO 0x4203
#define PTRACE_GETREGSET 0x4204
#define PTRACE_SETREGSET 0x4205
#define PTRACE_SEIZE 0x4206
#define PTRACE_INTERRUPT 0x4207
#define PTRACE_LISTEN 0x4208
#define PTRACE_PEEKSIGINFO 0x4209
#define PTRACE_GETSIGMASK 0x420a
#define PTRACE_SETSIGMASK 0x420b
#define PTRACE_SECCOMP_GET_FILTER 0x420c
#define PTRACE_SECCOMP_GET_METADATA 0x420d
#define PTRACE_GET_SYSCALL_INFO 0x420e
#define PTRACE_O_TRACESYSGOOD 0x00000001
#define PTRACE_O_TRACEFORK 0x00000002
#define PTRACE_O_TRACEVFORK 0x00000004
#define PTRACE_O_TRACECLONE 0x00000008
#define PTRACE_O_TRACEEXEC 0x00000010
#define PTRACE_O_TRACEVFORKDONE 0x00000020
#define PTRACE_O_TRACEEXIT 0x00000040
#define PTRACE_O_TRACESECCOMP 0x00000080
// #define PTRACE_O_MASK 0x0000007f
#define PTRACE_EVENT_FORK 1
#define PTRACE_EVENT_VFORK 2
#define PTRACE_EVENT_CLONE 3
#define PTRACE_EVENT_EXEC 4
#define PTRACE_EVENT_VFORK_DONE 5
#define PTRACE_EVENT_EXIT 6
#define PTRACE_EVENT_SECCOMP 7
static inline long ptrace(long op, pid_t pid, void *addr, void *data) {
return __syscall4(101, (long)op, (long)pid, (long)addr, (long)data);
}
static inline pid_t wait4(pid_t pid, int *wstatus, int options, struct rusage * rusage) {
return __syscall4(61, (long)pid, (long)wstatus, (long)options, (long)rusage);
}
#endif