Skip to content

Commit 941338e

Browse files
committed
chore: add more syscall wrappers
1 parent 76195e3 commit 941338e

File tree

8 files changed

+117
-17
lines changed

8 files changed

+117
-17
lines changed

attic/ashmem_utils.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define ASHMEM_PURGE_ALL_CACHES _IO(__ASHMEMIOC, 10)
2121
#define ASHMEM_NAME_LEN 256
2222

23+
__attribute__((noinline))
2324
EXPORT ssize_t ashmem_dev_get_size_region(int fd) {
2425
int NR_ioctl = __NR_ioctl;
2526
auto res = (ssize_t) syscall_ext(NR_ioctl, fd, ASHMEM_GET_SIZE, 0, 0, 0, 0);

attic/common_macros.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
#include <cstdint>
77
#include <cstddef>
88

9+
#define NO_MANGLE extern "C"
910
#define EXPORT extern "C" __attribute__((visibility("default")))
11+
#define NO_RETURN [[noreturn]]
1012

1113
#else
1214

1315
#include <stdint.h>
1416
#include <stddef.h>
1517

18+
#define NO_MANGLE
1619
#define EXPORT __attribute__((visibility("default")))
20+
#define NO_RETURN __attribute__((noreturn))
1721

1822
#endif
1923

attic/fake_mmap64.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
#include "fake_fstat64.h"
1313
#include "clear_cache.h"
1414

15-
template<typename ResT>
16-
static inline void assert_syscall_success(ResT res) {
17-
if (errno_of(res) != 0) [[unlikely]] {
18-
__builtin_trap();
19-
}
20-
}
21-
2215
EXPORT void* fake_mmap64(void* const addr, const size_t length, const int prot,
2316
const int flags, const int fd, const uint64_t offset) {
2417
const auto* info = (HookInfo*) get_hook_info();

attic/hook_info.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ EXPORT volatile HookInfo* get_hook_info() {
77
return &sHookInfo;
88
}
99

10+
__attribute__((noinline))
11+
EXPORT const void* get_current_pc() {
12+
return __builtin_return_address(0);
13+
}

attic/hook_info.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ struct HookInfo {
2424

2525
volatile HookInfo* get_hook_info();
2626

27+
const void* get_current_pc();
28+
2729
#ifdef __cplusplus
2830
}
2931
#endif

attic/syscall_ext.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,23 @@ static inline uintptr_t align_down(uintptr_t ptr, size_t alignment) {
5555
return ((uintptr_t) ptr & ~(alignment - 1u));
5656
}
5757

58+
#ifdef __cplusplus
59+
60+
static inline void* align_up(void* ptr, size_t alignment) {
61+
return reinterpret_cast<void*>(align_up(reinterpret_cast<uintptr_t>(ptr), alignment));
62+
}
63+
64+
static inline void* align_down(void* ptr, size_t alignment) {
65+
return reinterpret_cast<void*>(align_down(reinterpret_cast<uintptr_t>(ptr), alignment));
66+
}
67+
68+
template<typename ResT>
69+
static inline void assert_syscall_success(ResT res) {
70+
if (errno_of(res) != 0) [[unlikely]] {
71+
__builtin_trap();
72+
}
73+
}
74+
75+
#endif
76+
5877
#endif //LIBCORE_SHELLCODE_SYSCALL_EXT_H

attic/syscall_wrapper.cc

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
#include "fake_fstat64.h"
77
#include "syscall_ext.h"
88

9-
EXPORT int lsw_mprotect(void* addr, size_t len, int prot) {
9+
NO_MANGLE int lsw_mprotect(void* addr, size_t len, int prot) {
1010
const int NR_mprotect = __NR_mprotect;
1111
return (int) syscall_ext(NR_mprotect, (uintptr_t) addr, len, prot, 0, 0, 0);
1212
}
1313

14-
EXPORT ssize_t lsw_pread64(int fd, void* buf, size_t count, uint64_t offset) {
14+
NO_MANGLE ssize_t lsw_pread64(int fd, void* buf, size_t count, uint64_t offset) {
1515
const int NR_pread64 = __NR_pread64;
1616
#if defined(__LP64__)
1717
return (ssize_t) syscall_ext(NR_pread64, fd, (uintptr_t) buf, count, offset, 0, 0);
@@ -27,7 +27,7 @@ EXPORT ssize_t lsw_pread64(int fd, void* buf, size_t count, uint64_t offset) {
2727
#endif
2828
}
2929

30-
EXPORT void* lsw_mmap64(void* addr, size_t length, int prot, int flags, int fd, uint64_t offset) {
30+
NO_MANGLE void* lsw_mmap64(void* addr, size_t length, int prot, int flags, int fd, uint64_t offset) {
3131
uintptr_t res;
3232
#if defined(__LP64__)
3333
res = syscall_ext(__NR_mmap, (uintptr_t) addr, length, prot, flags, fd, offset);
@@ -41,7 +41,7 @@ EXPORT void* lsw_mmap64(void* addr, size_t length, int prot, int flags, int fd,
4141
return (void*) res;
4242
}
4343

44-
EXPORT int lsw_fstatfs64(int fd, kernel_statfs64_compat* buf) {
44+
NO_MANGLE int lsw_fstatfs64(int fd, kernel_statfs64_compat* buf) {
4545
#if defined(__LP64__)
4646
const int NR_fstatfs = __NR_fstatfs;
4747
#else
@@ -50,7 +50,60 @@ EXPORT int lsw_fstatfs64(int fd, kernel_statfs64_compat* buf) {
5050
return (int) syscall_ext(NR_fstatfs, fd, (uintptr_t) buf, 0, 0, 0, 0);
5151
}
5252

53-
EXPORT int lsw_munmap(void* addr, size_t len) {
53+
NO_MANGLE int lsw_munmap(void* addr, size_t len) {
5454
const int NR_munmap = __NR_munmap;
5555
return (int) syscall_ext(NR_munmap, (uintptr_t) addr, len, 0, 0, 0, 0);
5656
}
57+
58+
NO_MANGLE ssize_t lsw_read(int fd, void* buf, size_t nbytes) {
59+
const int NR_read = __NR_read;
60+
return (ssize_t) syscall_ext(NR_read, fd, (uintptr_t) buf, nbytes, 0, 0, 0);
61+
}
62+
63+
NO_MANGLE ssize_t lsw_write(int fd, const void* buf, size_t nbytes) {
64+
const int NR_write = __NR_write;
65+
return (ssize_t) syscall_ext(NR_write, fd, (uintptr_t) buf, nbytes, 0, 0, 0);
66+
}
67+
68+
NO_MANGLE int lsw_openat(int dirfd, const char* pathname, int flags, mode_t mode) {
69+
const int NR_openat = __NR_openat;
70+
return (int) syscall_ext(NR_openat, dirfd, (uintptr_t) pathname, flags, mode, 0, 0);
71+
}
72+
73+
NO_MANGLE int lsw_fstatat64(int dirfd, const char* pathname, kernel_stat64_compat* buf, int flags) {
74+
#if defined(__LP64__)
75+
const int NR_fstatat = __NR_newfstatat;
76+
#else
77+
const int NR_fstatat = __NR_fstatat64;
78+
#endif
79+
return (int) syscall_ext(NR_fstatat, dirfd, (uintptr_t) pathname, (uintptr_t) buf, flags, 0, 0);
80+
}
81+
82+
NO_MANGLE int lsw_stat64(const char* pathname, kernel_stat64_compat* buf) {
83+
return lsw_fstatat64(AT_FDCWD, pathname, buf, 0);
84+
}
85+
86+
NO_MANGLE int lsw_fstat64(int fd, kernel_stat64_compat* buf) {
87+
#if defined(__LP64__)
88+
const int NR_fstat = __NR_fstat;
89+
#else
90+
const int NR_fstat = __NR_fstat64;
91+
#endif
92+
return (int) syscall_ext(NR_fstat, fd, (uintptr_t) buf, 0, 0, 0, 0);
93+
}
94+
95+
NO_MANGLE int lsw_close(int fd) {
96+
const int NR_close = __NR_close;
97+
return (int) syscall_ext(NR_close, fd, 0, 0, 0, 0, 0);
98+
}
99+
100+
NO_MANGLE int lsw_ioctl(int fd, unsigned long request, void* arg) {
101+
const int NR_ioctl = __NR_ioctl;
102+
return (int) syscall_ext(NR_ioctl, fd, request, (uintptr_t) arg, 0, 0, 0);
103+
}
104+
105+
NO_MANGLE NO_RETURN void lsw_exit_group(int status) {
106+
const int NR_exit_group = __NR_exit_group;
107+
syscall_ext(NR_exit_group, status, 0, 0, 0, 0, 0);
108+
__builtin_unreachable();
109+
}

attic/syscall_wrapper.h

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,41 @@
1111
#ifdef __LP64__
1212

1313
typedef struct kernel_statfs kernel_statfs64_compat;
14+
typedef struct kernel_stat kernel_stat64_compat;
1415

1516
#else
1617

1718
typedef struct kernel_statfs64 kernel_statfs64_compat;
19+
typedef struct kernel_stat64 kernel_stat64_compat;
1820

1921
#endif
2022

21-
EXPORT ssize_t lsw_pread64(int fd, void* buf, size_t count, uint64_t offset);
22-
EXPORT int lsw_mprotect(void* addr, size_t len, int prot);
23-
EXPORT void* lsw_mmap64(void* addr, size_t len, int prot, int flags, int fd, uint64_t offset);
24-
EXPORT int lsw_fstatfs64(int fd, kernel_statfs64_compat* buf);
25-
EXPORT int lsw_munmap(void* addr, size_t len);
23+
NO_MANGLE ssize_t lsw_pread64(int fd, void* buf, size_t count, uint64_t offset);
24+
NO_MANGLE int lsw_mprotect(void* addr, size_t len, int prot);
25+
NO_MANGLE void* lsw_mmap64(void* addr, size_t len, int prot, int flags, int fd, uint64_t offset);
26+
NO_MANGLE int lsw_fstatfs64(int fd, kernel_statfs64_compat* buf);
27+
NO_MANGLE int lsw_munmap(void* addr, size_t len);
28+
NO_MANGLE ssize_t lsw_read(int fd, void* buf, size_t nbytes);
29+
NO_MANGLE ssize_t lsw_write(int fd, const void* buf, size_t nbytes);
30+
NO_MANGLE int lsw_openat(int dirfd, const char* pathname, int flags, mode_t mode);
31+
NO_MANGLE int lsw_stat64(const char* pathname, kernel_stat64_compat* buf);
32+
NO_MANGLE int lsw_fstatat64(int dirfd, const char* pathname, kernel_stat64_compat* buf, int flags);
33+
NO_MANGLE int lsw_fstat64(int fd, kernel_stat64_compat* buf);
34+
NO_MANGLE int lsw_close(int fd);
35+
NO_MANGLE int lsw_ioctl(int fd, unsigned long request, void* arg);
36+
NO_MANGLE NO_RETURN void lsw_exit_group(int status);
37+
38+
#ifdef __cplusplus
39+
40+
// ioctl overloads
41+
static inline int lsw_ioctl(int fd, unsigned long request, unsigned long arg) {
42+
return lsw_ioctl(fd, request, reinterpret_cast<void*>(arg));
43+
}
44+
45+
static inline int lsw_ioctl(int fd, unsigned long request) {
46+
return lsw_ioctl(fd, request, nullptr);
47+
}
48+
49+
#endif
2650

2751
#endif //LIBCORE_SHELLCODE_SYSCALL_WRAPPERS_H

0 commit comments

Comments
 (0)