File: memory_patcher_component.c

package info (click to toggle)
openmpi 5.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 202,312 kB
  • sloc: ansic: 612,441; makefile: 42,495; sh: 11,230; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,154; python: 1,856; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (687 lines) | stat: -rw-r--r-- 19,896 bytes parent folder | download | duplicates (2)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2009-2020 Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2013-2017 Los Alamos National Security, LLC. All rights
 *                         reserved.
 * Copyright (c) 2016-2017 Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * Copyright (c) 2016-2020 IBM Corporation.  All rights reserved.
 * Copyright (c) 2025      Triad National Security, LLC. All rights reserved.
 *
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "memory_patcher.h"

#include "opal/mca/memory/base/base.h"
#include "opal/mca/memory/base/empty.h"
#include "opal/mca/patcher/base/base.h"
#include "opal/memoryhooks/memory.h"
#include "opal/util/output.h"
#include "opal/util/show_help.h"

#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <unistd.h>
#if defined(HAVE_SYS_SYSCALL_H)
#    include <sys/syscall.h>
#endif
#if defined(HAVE_LINUX_MMAN_H)
#    include <linux/mman.h>
#endif
#if defined(HAVE_SYS_IPC_H)
#    include <sys/ipc.h>
#endif

#include "memory_patcher.h"
#undef opal_memory_changed

#if defined(SYS_shmdt) || (defined(IPCOP_shmdt) && defined(SYS_ipc))
#    define HAS_SHMDT 1
#else
#    define HAS_SHMDT 0
#endif

#if defined(SYS_shmat) || (defined(IPCOP_shmat) && defined(SYS_ipc))
#    define HAS_SHMAT 1
#else
#    define HAS_SHMAT 0
#endif

static int patcher_open(void);
static int patcher_close(void);
static int patcher_register(void);
static int patcher_query(int *);

static int mca_memory_patcher_priority;
static int was_executed_already = 0;

opal_memory_patcher_component_t mca_memory_patcher_component = {
    .super =
        {
            .memoryc_version =
                {
                    OPAL_MEMORY_BASE_VERSION_2_0_0,

                    /* Component name and version */
                    .mca_component_name = "patcher",
                    MCA_BASE_MAKE_VERSION(component, OPAL_MAJOR_VERSION, OPAL_MINOR_VERSION,
                                          OPAL_RELEASE_VERSION),

                    /* Component open and close functions */
                    .mca_open_component = patcher_open,
                    .mca_close_component = patcher_close,
                    .mca_register_component_params = patcher_register,
                },
            .memoryc_data =
                {/* The component is checkpoint ready */
                 MCA_BASE_METADATA_PARAM_CHECKPOINT},

            /* Memory framework functions. */
            .memoryc_query = patcher_query,
            .memoryc_register = opal_memory_base_component_register_empty,
            .memoryc_deregister = opal_memory_base_component_deregister_empty,
            .memoryc_set_alignment = opal_memory_base_component_set_alignment_empty,
        },

    /* Component-specific data, filled in later (compiler will 0/NULL
       it out) */
};

#if HAVE_DECL___SYSCALL && defined(HAVE___SYSCALL)
/* calling __syscall is preferred on some systems when some arguments may be 64-bit. it also
 * has the benefit of having an off_t return type */
#    define memory_patcher_syscall __syscall
#else
#    define memory_patcher_syscall syscall
#endif

/* All the hooks in this file have two levels. The first level has the OPAL_PATCHER_* macros
 * around the call to the second level. This was done because with xlc the compiler was
 * generating an access to r2 before the OPAL_PATCHER_* assembly. This was loading invalid
 * data. If this can be resolved the two levels can be joined.
 */

/*
 * Nathan's original fix described above can have the same problem reappear if the
 * interception functions inline themselves.
 */
static void *_intercept_mmap(void *start, size_t length, int prot, int flags, int fd,
                             off_t offset) __opal_attribute_noinline__;
static int _intercept_munmap(void *start, size_t length) __opal_attribute_noinline__;
#if defined(SYS_mremap)
#    if defined(__linux__)
static void *_intercept_mremap(void *start, size_t oldlen, size_t newlen, int flags,
                               void *new_address) __opal_attribute_noinline__;
#    else
static void *_intercept_mremap(void *start, size_t oldlen, void *new_address, size_t newlen,
                               int flags) __opal_attribute_noinline__;
#    endif // defined(__linux__)
#endif // defined(SYS_mremap)
static int _intercept_madvise(void *start, size_t length, int advice) __opal_attribute_noinline__;
#if defined SYS_brk
static int _intercept_brk(void *addr) __opal_attribute_noinline__;
#endif
#if defined(__linux__)
#    if HAS_SHMAT
static void *_intercept_shmat(int shmid, const void *shmaddr,
                              int shmflg) __opal_attribute_noinline__;
#    endif // HAS_SHMAT
#    if HAS_SHMDT
static int _intercept_shmdt(const void *shmaddr) __opal_attribute_noinline__;
#    endif // HAS_SHMDT
#endif // defined(__linux__)

#if defined(SYS_mmap)

#    if defined(HAVE___MMAP) && !HAVE_DECL___MMAP
/* prototype for Apple's internal mmap function */
void *__mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
#    endif

static void *(*original_mmap)(void *, size_t, int, int, int, off_t);

static void *_intercept_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
{
    void *result = 0;

    if ((flags & MAP_FIXED) && (start != NULL)) {
        opal_mem_hooks_release_hook(start, length, true);
    }

    if (!original_mmap) {
        result = (void *) (intptr_t) memory_patcher_syscall(SYS_mmap, start, length, prot, flags,
                                                            fd, offset);
    } else {
        result = original_mmap(start, length, prot, flags, fd, offset);
    }

    return result;
}

static void *intercept_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
{
    OPAL_PATCHER_BEGIN;
    void *result = _intercept_mmap(start, length, prot, flags, fd, offset);
    OPAL_PATCHER_END;
    return result;
}
#endif

#if defined(SYS_munmap)
static int (*original_munmap)(void *, size_t);

static int _intercept_munmap(void *start, size_t length)
{
    int result = 0;

    /* could be in a malloc implementation */
    opal_mem_hooks_release_hook(start, length, true);

    if (!original_munmap) {
        result = memory_patcher_syscall(SYS_munmap, start, length);
    } else {
        result = original_munmap(start, length);
    }

    return result;
}

static int intercept_munmap(void *start, size_t length)
{
    OPAL_PATCHER_BEGIN;
    int result = _intercept_munmap(start, length);
    OPAL_PATCHER_END;
    return result;
}

#endif

#if defined(SYS_mremap)

#    if defined(__linux__)
/* on linux this function has an optional extra argument but ... can not be used here because it
 * causes issues when intercepting a 4-argument mremap call */
static void *(*original_mremap)(void *, size_t, size_t, int, void *);
#    else
/* mremap has a different signature on BSD systems */
static void *(*original_mremap)(void *, size_t, void *, size_t, int);
#    endif

#    if defined(__linux__)
static void *_intercept_mremap(void *start, size_t oldlen, size_t newlen, int flags,
                               void *new_address)
#    else
static void *_intercept_mremap(void *start, size_t oldlen, void *new_address, size_t newlen,
                               int flags)
#    endif
{
    void *result = MAP_FAILED;

    if (MAP_FAILED != start && oldlen > 0) {
        opal_mem_hooks_release_hook(start, oldlen, true);
    }

#    if defined(MREMAP_FIXED)
    if (!(flags & MREMAP_FIXED)) {
        new_address = NULL;
    }
#    endif

#    if defined(__linux__)
    if (!original_mremap) {
        result = (void *) (intptr_t) memory_patcher_syscall(SYS_mremap, start, oldlen, newlen,
                                                            flags, new_address);
    } else {
        result = original_mremap(start, oldlen, newlen, flags, new_address);
    }
#    else
    if (!original_mremap) {
        result = (void *) (intptr_t) memory_patcher_syscall(SYS_mremap, start, oldlen, new_address,
                                                            newlen, flags);
    } else {
        result = original_mremap(start, oldlen, new_address, newlen, flags);
    }
#    endif

    return result;
}

#    if defined(__linux__)
static void *intercept_mremap(void *start, size_t oldlen, size_t newlen, int flags,
                              void *new_address)
{
    OPAL_PATCHER_BEGIN;
    void *result = _intercept_mremap(start, oldlen, newlen, flags, new_address);
    OPAL_PATCHER_END;
    return result;
}
#    else
static void *intercept_mremap(void *start, size_t oldlen, void *new_address, size_t newlen,
                              int flags)
{
    OPAL_PATCHER_BEGIN;
    void *result = _intercept_mremap(start, oldlen, new_address, newlen, flags);
    OPAL_PATCHER_END;
    return result;
}
#    endif

#endif

#if defined(SYS_madvise)

static int (*original_madvise)(void *, size_t, int);

static int _intercept_madvise(void *start, size_t length, int advice)
{
    int result = 0;

    if (advice == MADV_DONTNEED ||
#    ifdef MADV_FREE
        advice == MADV_FREE ||
#    endif
#    ifdef MADV_REMOVE
        advice == MADV_REMOVE ||
#    endif
        advice == POSIX_MADV_DONTNEED) {
        opal_mem_hooks_release_hook(start, length, true);
    }

    if (!original_madvise) {
        result = memory_patcher_syscall(SYS_madvise, start, length, advice);
    } else {
        result = original_madvise(start, length, advice);
    }

    return result;
}
static int intercept_madvise(void *start, size_t length, int advice)
{
    OPAL_PATCHER_BEGIN;
    int result = _intercept_madvise(start, length, advice);
    OPAL_PATCHER_END;
    return result;
}

#endif

#if defined SYS_brk

#    ifdef HAVE___CURBRK
extern void *__curbrk; /* in libc */
#    endif

static int (*original_brk)(void *);

static int _intercept_brk(void *addr)
{
    int result = 0;
    void *old_addr, *new_addr;

#    ifdef HAVE___CURBRK
    old_addr = __curbrk;
#    else
    old_addr = sbrk(0);
#    endif

    if (!original_brk) {
        /* get the current_addr */
        new_addr = (void *) (intptr_t) memory_patcher_syscall(SYS_brk, addr);

#    ifdef HAVE___CURBRK
        /*
         * Note: if we were using glibc brk/sbrk, their __curbrk would get
         * updated, but since we're going straight to the syscall, we have
         * to update __curbrk or else glibc won't see it.
         */
        __curbrk = new_addr;
#    endif
    } else {
        result = original_brk(addr);
#    ifdef HAVE___CURBRK
        new_addr = __curbrk;
#    else
        new_addr = sbrk(0);
#    endif
    }

    if (new_addr < addr) {
        errno = ENOMEM;
        result = -1;
    } else if (new_addr < old_addr) {
        opal_mem_hooks_release_hook(new_addr, (intptr_t) old_addr - (intptr_t) new_addr, true);
    }
    return result;
}

static int intercept_brk(void *addr)
{
    OPAL_PATCHER_BEGIN;
    int result = _intercept_brk(addr);
    OPAL_PATCHER_END;
    return result;
}

#endif

// These op codes used to be in bits/ipc.h but were removed in glibc in 2015
// with a comment saying they should be defined in internal headers:
// https://sourceware.org/bugzilla/show_bug.cgi?id=18560
// and when glibc uses that syscall it seems to do so from its own definitions:
// https://github.com/bminor/glibc/search?q=IPCOP_shmat&unscoped_q=IPCOP_shmat
#ifndef IPCOP_shmat
#    define IPCOP_shmat 21
#endif
#ifndef IPCOP_shmdt
#    define IPCOP_shmdt 22
#endif

#if HAS_SHMDT || HAS_SHMAT
#    if defined(__linux__)

#        include <fcntl.h>
#        include <stdio.h>
#        include <sys/shm.h>

static size_t memory_patcher_get_shm_seg_size(const void *shmaddr)
{
    unsigned long start_addr, end_addr;
    char *ptr, *newline;
    char buffer[1024];
    size_t seg_size = 0;
    int fd;

    seg_size = 0;

    fd = open("/proc/self/maps", O_RDONLY);
    if (fd < 0) {
        return 0;
    }

    for (size_t read_offset = 0;;) {
        ssize_t nread = read(fd, buffer + read_offset, sizeof(buffer) - 1 - read_offset);
        if (nread <= 0) {
            if (errno == EINTR) {
                continue;
            }

            break;
        } else {
            buffer[nread + read_offset] = '\0';
        }

        ptr = buffer;
        while ((newline = strchr(ptr, '\n')) != NULL) {
            /* 00400000-0040b000 r-xp ... \n */
            int ret = sscanf(ptr, "%lx-%lx ", &start_addr, &end_addr);
            if (ret != 2) {
                continue;
            }

            if (start_addr == (uintptr_t) shmaddr) {
                seg_size = end_addr - start_addr;
                goto out_close;
            }

            newline = strchr(ptr, '\n');
            if (newline == NULL) {
                break;
            }

            ptr = newline + 1;
        }

        read_offset = strlen(ptr);
        memmove(buffer, ptr, read_offset);
    }

out_close:
    close(fd);
    return seg_size;
}

static size_t get_shm_size(int shmid)
{
    struct shmid_ds ds;
    int ret;

    ret = shmctl(shmid, IPC_STAT, &ds);
    if (ret < 0) {
        return 0;
    }

    return ds.shm_segsz;
}
#    endif
#endif

#if HAS_SHMAT
#    if defined(__linux__)
static void *(*original_shmat)(int shmid, const void *shmaddr, int shmflg);

static void *_intercept_shmat(int shmid, const void *shmaddr, int shmflg)
{
    void *result = 0;

    size_t size = get_shm_size(shmid);

    if ((shmflg & SHM_REMAP) && (shmaddr != NULL)) {
        // I don't really know what REMAP combined with SHM_RND does, so I'll just
        // guess it remaps all the way down to the lower attach_addr, and all the
        // way up to the original shmaddr+size
        uintptr_t attach_addr = (uintptr_t) shmaddr;

        if (shmflg & SHM_RND) {
            attach_addr -= ((uintptr_t) shmaddr) % SHMLBA;
            size += ((uintptr_t) shmaddr) % SHMLBA;
        }
        opal_mem_hooks_release_hook((void *) attach_addr, size, false);
    }

    if (!original_shmat) {
#        if defined(SYS_shmat)
        result = (void *) memory_patcher_syscall(SYS_shmat, shmid, shmaddr, shmflg);
#        else // IPCOP_shmat
        unsigned long ret;
        ret = memory_patcher_syscall(SYS_ipc, IPCOP_shmat, shmid, shmflg, &shmaddr, shmaddr);
        result = (ret > -(unsigned long) SHMLBA) ? (void *) ret : (void *) shmaddr;
#        endif
    } else {
        result = original_shmat(shmid, shmaddr, shmflg);
    }

    return result;
}

static void *intercept_shmat(int shmid, const void *shmaddr, int shmflg)
{
    OPAL_PATCHER_BEGIN;
    void *result = _intercept_shmat(shmid, shmaddr, shmflg);
    OPAL_PATCHER_END;
    return result;
}
#    endif
#endif

#if HAS_SHMDT
#    if defined(__linux__)
static int (*original_shmdt)(const void *);

static int _intercept_shmdt(const void *shmaddr)
{
    int result;

    /* opal_mem_hooks_release_hook should probably be updated to take a const void *.
     * for now just cast away the const */
    opal_mem_hooks_release_hook((void *) shmaddr, memory_patcher_get_shm_seg_size(shmaddr), false);

    if (original_shmdt) {
        result = original_shmdt(shmaddr);
    } else {
#        if defined(SYS_shmdt)
        result = memory_patcher_syscall(SYS_shmdt, shmaddr);
#        else // IPCOP_shmdt
        result = memory_patcher_syscall(SYS_ipc, IPCOP_shmdt, 0, 0, 0, shmaddr);
#        endif
    }

    return result;
}

static int intercept_shmdt(const void *shmaddr)
{
    OPAL_PATCHER_BEGIN;
    int result = _intercept_shmdt(shmaddr);
    OPAL_PATCHER_END;
    return result;
}
#    endif
#endif

static int patcher_register(void)
{
    mca_memory_patcher_priority = 80;
    mca_base_component_var_register(&mca_memory_patcher_component.super.memoryc_version, "priority",
                                    "Priority of the patcher memory hook component",
                                    MCA_BASE_VAR_TYPE_INT, NULL, 0, 0, OPAL_INFO_LVL_5,
                                    MCA_BASE_VAR_SCOPE_CONSTANT, &mca_memory_patcher_priority);

    return OPAL_SUCCESS;
}

static int patcher_query(int *priority)
{
    int rc;

    rc = mca_base_framework_open(&opal_patcher_base_framework, 0);
    if (OPAL_SUCCESS != rc) {
        *priority = -1;
        return OPAL_SUCCESS;
    }

    *priority = mca_memory_patcher_priority;

    return OPAL_SUCCESS;
}

static int patcher_open(void)
{
    int rc;

    if (was_executed_already) {
        return OPAL_SUCCESS;
    }

    was_executed_already = 1;

    rc = opal_patcher_base_select();
    if (OPAL_SUCCESS != rc) {
        mca_base_framework_close(&opal_patcher_base_framework);
        return OPAL_ERR_NOT_AVAILABLE;
    }

    /* set memory hooks support level */
    opal_mem_hooks_set_support(OPAL_MEMORY_FREE_SUPPORT | OPAL_MEMORY_MUNMAP_SUPPORT);

#if defined(SYS_mmap)
    rc = opal_patcher->patch_symbol("mmap", (uintptr_t) intercept_mmap,
                                    (uintptr_t *) &original_mmap);
    if (OPAL_SUCCESS != rc) {
        goto err_patching;
    }
#endif

#if defined(SYS_munmap)
    rc = opal_patcher->patch_symbol("munmap", (uintptr_t) intercept_munmap,
                                    (uintptr_t *) &original_munmap);
    if (OPAL_SUCCESS != rc) {
        goto err_patching;
    }
#endif

#if defined(SYS_mremap)
    rc = opal_patcher->patch_symbol("mremap", (uintptr_t) intercept_mremap,
                                    (uintptr_t *) &original_mremap);
    if (OPAL_SUCCESS != rc) {
        goto err_patching;
    }
#endif

#if defined(SYS_madvise)
    rc = opal_patcher->patch_symbol("madvise", (uintptr_t) intercept_madvise,
                                    (uintptr_t *) &original_madvise);
    if (OPAL_SUCCESS != rc) {
        goto err_patching;
    }
#endif

#if HAS_SHMAT
#    if defined(__linux__)
    rc = opal_patcher->patch_symbol("shmat", (uintptr_t) intercept_shmat,
                                    (uintptr_t *) &original_shmat);
    if (OPAL_SUCCESS != rc) {
        goto err_patching;
    }
#    endif
#endif

#if defined(__linux__)
#    if HAS_SHMDT
    rc = opal_patcher->patch_symbol("shmdt", (uintptr_t) intercept_shmdt,
                                    (uintptr_t *) &original_shmdt);
    if (OPAL_SUCCESS != rc) {
        goto err_patching;
    }
#    endif
#endif

#if defined(SYS_brk)
    rc = opal_patcher->patch_symbol("brk", (uintptr_t) intercept_brk, (uintptr_t *) &original_brk);
#endif

    if (OPAL_SUCCESS != rc) {
        goto err_patching;
    }

    return OPAL_SUCCESS;

err_patching:
    /* In the case we had a problem patching, set this flag to 0 so we do not
       directly return OPAL_SUCCESS if we call patcher_open() again. */
    was_executed_already = 0;
    opal_patcher_base_restore_all();

    return rc;
}

static int patcher_close(void)
{
    mca_base_framework_close(&opal_patcher_base_framework);

    was_executed_already = 0;

    /* Note that we don't need to unpatch any symbols here; the
       patcher framework will take care of all of that for us. */
    return OPAL_SUCCESS;
}