-
Notifications
You must be signed in to change notification settings - Fork 431
/
sys.c
342 lines (289 loc) · 8.68 KB
/
sys.c
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
/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2016. ALL RIGHTS RESERVED.
*
* See file LICENSE for terms.
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE /* for dladdr */
#endif
#include "sys.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <ucm/api/ucm.h>
#include <ucm/util/log.h>
#include <ucm/mmap/mmap.h>
#include <ucs/sys/math.h>
#include <linux/mman.h>
#include <sys/mman.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <dlfcn.h>
#define UCM_PROC_SELF_MAPS "/proc/self/maps"
ucm_global_config_t ucm_global_opts = {
.log_level = UCS_LOG_LEVEL_WARN,
.enable_events = 1,
.mmap_hook_mode = UCM_DEFAULT_HOOK_MODE,
.enable_malloc_hooks = 1,
.enable_malloc_reloc = 0,
.enable_cuda_reloc = 1,
.enable_dynamic_mmap_thresh = 1,
.alloc_alignment = 16,
.dlopen_process_rpath = 1
};
size_t ucm_get_page_size()
{
static long page_size = -1;
long value;
if (page_size == -1) {
value = sysconf(_SC_PAGESIZE);
if (value < 0) {
page_size = 4096;
} else {
page_size = value;
}
}
return page_size;
}
static void *ucm_sys_complete_alloc(void *ptr, size_t size)
{
*(size_t*)ptr = size;
return UCS_PTR_BYTE_OFFSET(ptr, sizeof(size_t));
}
void *ucm_sys_malloc(size_t size)
{
size_t sys_size;
void *ptr;
sys_size = ucs_align_up_pow2(size + sizeof(size_t), ucm_get_page_size());
ptr = ucm_orig_mmap(NULL, sys_size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED) {
ucm_error("mmap(size=%zu) failed: %m", sys_size);
return NULL;
}
return ucm_sys_complete_alloc(ptr, sys_size);
}
void *ucm_sys_calloc(size_t nmemb, size_t size)
{
size_t total_size = size * nmemb;
void *ptr;
ptr = ucm_sys_malloc(total_size);
if (ptr == NULL) {
return NULL;
}
memset(ptr, 0, total_size);
return ptr;
}
void ucm_sys_free(void *ptr)
{
size_t size;
if (ptr == NULL) {
return;
}
/* Do not use UCS_PTR_BYTE_OFFSET macro here due to coverity
* false positive.
* TODO: check for false positive on newer coverity. */
ptr = (char*)ptr - sizeof(size_t);
size = *(size_t*)ptr;
munmap(ptr, size);
}
void *ucm_sys_realloc(void *ptr, size_t size)
{
size_t oldsize, sys_size;
void *oldptr, *newptr;
if (ptr == NULL) {
return ucm_sys_malloc(size);
}
oldptr = UCS_PTR_BYTE_OFFSET(ptr, -sizeof(size_t));
oldsize = *(size_t*)oldptr;
sys_size = ucs_align_up_pow2(size + sizeof(size_t), ucm_get_page_size());
if (sys_size == oldsize) {
return ptr;
}
newptr = ucm_orig_mremap(oldptr, oldsize, sys_size, MREMAP_MAYMOVE);
if (newptr == MAP_FAILED) {
ucm_error("mremap(oldptr=%p oldsize=%zu, newsize=%zu) failed: %m",
oldptr, oldsize, sys_size);
return NULL;
}
return ucm_sys_complete_alloc(newptr, sys_size);
}
void ucm_parse_proc_self_maps(ucm_proc_maps_cb_t cb, void *arg)
{
static char *buffer = MAP_FAILED;
static size_t buffer_size = 32768;
static pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
ssize_t read_size, offset;
unsigned long start, end;
char prot_c[4];
int line_num;
int prot;
char *ptr, *newline;
int maps_fd;
int ret;
int n;
maps_fd = open(UCM_PROC_SELF_MAPS, O_RDONLY);
if (maps_fd < 0) {
ucm_fatal("cannot open %s for reading: %m", UCM_PROC_SELF_MAPS);
}
/* read /proc/self/maps fully into the buffer */
pthread_rwlock_wrlock(&lock);
if (buffer == MAP_FAILED) {
buffer = ucm_orig_mmap(NULL, buffer_size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (buffer == MAP_FAILED) {
ucm_fatal("failed to allocate maps buffer(size=%zu): %m", buffer_size);
}
}
offset = 0;
for (;;) {
read_size = read(maps_fd, buffer + offset, buffer_size - offset);
if (read_size < 0) {
/* error */
if (errno != EINTR) {
ucm_fatal("failed to read from %s: %m", UCM_PROC_SELF_MAPS);
}
} else if (read_size == buffer_size - offset) {
/* enlarge buffer */
buffer = ucm_orig_mremap(buffer, buffer_size, buffer_size * 2,
MREMAP_MAYMOVE);
if (buffer == MAP_FAILED) {
ucm_fatal("failed to allocate maps buffer(size=%zu)", buffer_size);
}
buffer_size *= 2;
/* read again from the beginning of the file */
ret = lseek(maps_fd, 0, SEEK_SET);
if (ret < 0) {
ucm_fatal("failed to lseek(0): %m");
}
offset = 0;
} else if (read_size == 0) {
/* finished reading */
buffer[offset] = '\0';
break;
} else {
/* more data could be available even if the buffer is not full */
offset += read_size;
}
}
pthread_rwlock_unlock(&lock);
close(maps_fd);
pthread_rwlock_rdlock(&lock);
ptr = buffer;
line_num = 1;
while ( (newline = strchr(ptr, '\n')) != NULL ) {
/* address perms offset dev inode pathname
* 00400000-0040b000 r-xp 00001a00 0a:0b 12345 /dev/mydev
*/
*newline = '\0';
ret = sscanf(ptr, "%lx-%lx %4c %*x %*x:%*x %*d %n",
&start, &end, prot_c,
/* ignore offset, dev, inode */
&n /* save number of chars before path begins */);
if (ret < 3) {
ucm_warn("failed to parse %s line %d: '%s'",
UCM_PROC_SELF_MAPS, line_num, ptr);
} else {
prot = 0;
if (prot_c[0] == 'r') {
prot |= PROT_READ;
}
if (prot_c[1] == 'w') {
prot |= PROT_WRITE;
}
if (prot_c[2] == 'x') {
prot |= PROT_EXEC;
}
if (cb(arg, (void*)start, end - start, prot, ptr + n)) {
goto out;
}
}
ptr = newline + 1;
++line_num;
}
out:
pthread_rwlock_unlock(&lock);
}
typedef struct {
const void *shmaddr;
size_t seg_size;
} ucm_get_shm_seg_size_ctx_t;
static int ucm_get_shm_seg_size_cb(void *arg, void *addr, size_t length,
int prot, const char *path)
{
ucm_get_shm_seg_size_ctx_t *ctx = arg;
if (addr == ctx->shmaddr) {
ctx->seg_size = length;
return 1;
}
return 0;
}
size_t ucm_get_shm_seg_size(const void *shmaddr)
{
ucm_get_shm_seg_size_ctx_t ctx = { shmaddr, 0 };
ucm_parse_proc_self_maps(ucm_get_shm_seg_size_cb, &ctx);
return ctx.seg_size;
}
void ucm_strerror(int eno, char *buf, size_t max)
{
#if STRERROR_R_CHAR_P
char *ret = strerror_r(eno, buf, max);
if (ret != buf) {
strncpy(buf, ret, max);
}
#else
(void)strerror_r(eno, buf, max);
#endif
}
void ucm_prevent_dl_unload()
{
Dl_info info;
void *dl;
int ret;
/* Get the path to current library by current function pointer */
(void)dlerror();
ret = dladdr(ucm_prevent_dl_unload, &info);
if (ret == 0) {
ucm_warn("could not find address of current library: %s", dlerror());
return;
}
/* Load the current library with NODELETE flag, to prevent it from being
* unloaded. This will create extra reference to the library, but also add
* NODELETE flag to the dynamic link map.
*/
(void)dlerror();
dl = dlopen(info.dli_fname, RTLD_LOCAL|RTLD_LAZY|RTLD_NODELETE);
if (dl == NULL) {
ucm_warn("failed to load '%s': %s", info.dli_fname, dlerror());
return;
}
ucm_debug("reloaded '%s' at %p with NODELETE flag", info.dli_fname, dl);
/* Now we drop our reference to the lib, and it won't be unloaded anymore */
dlclose(dl);
}
char *ucm_concat_path(char *buffer, size_t max, const char *dir, const char *file)
{
size_t len;
len = strlen(dir);
while (len && (dir[len - 1] == '/')) {
len--; /* trim closing '/' */
}
len = ucs_min(len, max);
memcpy(buffer, dir, len);
max -= len;
if (max < 2) { /* buffer is shorter than dir - copy dir only */
buffer[len - 1] = '\0';
return buffer;
}
buffer[len] = '/';
max--;
while (file[0] == '/') {
file++; /* trim beginning '/' */
}
strncpy(buffer + len + 1, file, max);
buffer[max + len] = '\0'; /* force close string */
return buffer;
}