-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsymtrap.so.c
More file actions
174 lines (143 loc) · 3.6 KB
/
libsymtrap.so.c
File metadata and controls
174 lines (143 loc) · 3.6 KB
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <dlfcn.h>
#include <execinfo.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
/*
* This library is intended to be preloaded so that it can abort at a specific
* point and generate a core dump for analysis. It needs to be manually altered
* to match specific case.
*
* Notes:
* -funwind-tables might be needed on some architectures (arm, riscv)
* for backtrace() to work
* -rdynamic can help make backtrace() output more readable
*
* Backtrace output can be further decoded with add2line:
* $ addr2line -fp -e ./elf addr
*
*/
#define DLSYM_CHECK(handle) do { \
if ((handle) != NULL) { \
/* fprintf(stdout, "libsymtrap.so: wrapping %s()\n", __func__); */ \
} else { \
fprintf(stderr, "%s\n", dlerror()); \
abort(); \
} \
} while (0)
static void do_abort(void)
{
int size;
void *buffer[64];
size = backtrace(buffer, sizeof(buffer));
fprintf(stderr, "backtrace() returned %d addresses:\n", size);
backtrace_symbols_fd(buffer, size, STDERR_FILENO);
abort();
}
#if 1
/*
* extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __THROW;
*/
int clock_gettime(clockid_t clock_id, struct timespec *tp)
{
static int (*__clock_gettime)(clockid_t, struct timespec *) = NULL;
static unsigned long count = 0;
if (__clock_gettime == NULL) {
__clock_gettime = (int (*)(clockid_t, struct timespec *))dlsym(RTLD_NEXT, "clock_gettime");
DLSYM_CHECK(__clock_gettime);
}
/*
* app under certain load generated 3~5k calls / second so the idea was
* to abort but only when file flag exists
*/
if (++count % 4000 == 0) {
int ret;
ret = access("/tmp/symtrap_clock_gettime", F_OK);
if (!ret)
do_abort();
}
return __clock_gettime(clock_id, tp);
}
#endif
#if 1
/*
* extern int ioctl (int __fd, unsigned long int __request, ...) __THROW;
*/
int ioctl(int fd, unsigned long int request, ...)
{
int ret;
va_list args;
static int (*__ioctl)(int, unsigned long int, ...) = NULL;
unsigned long int ioc;
if (__ioctl == NULL) {
__ioctl = (int (*)(int, unsigned long int, ...))dlsym(RTLD_NEXT, "ioctl");
DLSYM_CHECK(__ioctl);
}
/*
* ioctl from strace:
* ioctl(11, _IOC(0, 0x64, 0x05, 0x65), 0xb60a4bb8);
*/
ioc = _IOC(0, 0x64, 0x05, 0x65);
/* abort on matching ioctl */
if (request == ioc)
do_abort();
va_start(args, request);
ret = __ioctl(fd, request, args);
va_end(args);
return ret;
}
#endif
#if 1
/*
* match this file name
*/
#define FILE_PATH "/dev/input/event1"
/*
* extern int open (const char *__file, int __oflag, ...) __nonnull ((1));
*/
int open(const char *pathname, int flags, ...)
{
int ret;
va_list args;
static int (*__open)(const char *, int, ...) = NULL;
if (__open == NULL) {
__open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open");
DLSYM_CHECK(__open);
}
/* abort on matching file name */
if (!strcmp(pathname, FILE_PATH))
do_abort();
va_start(args, flags);
ret = __open(pathname, flags, args);
va_end(args);
return ret;
}
/*
* extern int open64 (const char *__file, int __oflag, ...) __nonnull ((1));
*/
int open64(const char *pathname, int flags, ...)
{
int ret;
va_list args;
static int (*__open64)(const char *, int, ...) = NULL;
if (__open64 == NULL) {
__open64 = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open64");
DLSYM_CHECK(__open64);
}
/* abort on matching file name */
if (!strcmp(pathname, FILE_PATH))
do_abort();
va_start(args, flags);
ret = __open64(pathname, flags, args);
va_end(args);
return ret;
}
#endif