|
| 1 | +/* |
| 2 | + * Copyright(c) The Maintainers of Nanvix. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | + |
| 6 | +/* |
| 7 | + * dlfcn-init-runpath-c: Tests for `.init_array` / `.fini_array` |
| 8 | + * constructor and destructor invocation, and for DT_RUNPATH-driven |
| 9 | + * dependency search. |
| 10 | + * |
| 11 | + * Test 1 (constructor): dlopen libctor.so, dlsym `ctor_ran`, and |
| 12 | + * confirm the constructor sentinel was written. |
| 13 | + * |
| 14 | + * Test 2 (destructor): dlclose libctor.so and confirm the destructor |
| 15 | + * wrote its sentinel into the main executable's `g_dtor_ran` global. |
| 16 | + * `g_dtor_ran` must be exported via -rdynamic so the loader's global |
| 17 | + * symbol table can satisfy the `extern volatile int g_dtor_ran;` |
| 18 | + * reference in the library. |
| 19 | + * |
| 20 | + * Test 3 (DT_RUNPATH): dlopen lib/libparent.so, which has |
| 21 | + * DT_NEEDED=libchild.so and DT_RUNPATH=lib/subdir/. The loader must |
| 22 | + * probe DT_RUNPATH before the default `lib/` directory; otherwise |
| 23 | + * libchild.so is not located. |
| 24 | + */ |
| 25 | + |
| 26 | +#include <dlfcn.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <string.h> |
| 29 | +#include <unistd.h> |
| 30 | + |
| 31 | +/* Witness for libctor.so's destructor — defined here so it survives the |
| 32 | + * library being unloaded. Marked volatile to keep the optimiser away. |
| 33 | + */ |
| 34 | +volatile int g_dtor_ran = 0; |
| 35 | + |
| 36 | +static int tests_passed = 0; |
| 37 | +static int tests_failed = 0; |
| 38 | + |
| 39 | +static void pass(const char *name) |
| 40 | +{ |
| 41 | + printf(" PASS: %s\n", name); |
| 42 | + fflush(stdout); |
| 43 | + tests_passed++; |
| 44 | +} |
| 45 | + |
| 46 | +static void fail(const char *name, const char *reason) |
| 47 | +{ |
| 48 | + printf(" FAIL: %s (%s)\n", name, reason); |
| 49 | + fflush(stdout); |
| 50 | + tests_failed++; |
| 51 | +} |
| 52 | + |
| 53 | +/* |
| 54 | + * Test 1: Constructor in `.init_array` must run before dlopen returns. |
| 55 | + */ |
| 56 | +static void test_init_array(void) |
| 57 | +{ |
| 58 | + void *h = dlopen("lib/libctor.so", RTLD_NOW); |
| 59 | + if (h == NULL) { |
| 60 | + fail("init_array fires on dlopen", dlerror()); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + volatile int *ctor_ran = (volatile int *)dlsym(h, "ctor_ran"); |
| 65 | + if (ctor_ran == NULL) { |
| 66 | + fail("init_array fires on dlopen", "ctor_ran symbol missing"); |
| 67 | + dlclose(h); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (*ctor_ran != 0xC70A) { |
| 72 | + fail("init_array fires on dlopen", "constructor sentinel not set"); |
| 73 | + dlclose(h); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + /* Sanity check: ordinary symbol resolution still works after init. */ |
| 78 | + int (*fn)(void) = NULL; |
| 79 | + *(void **)(&fn) = dlsym(h, "ctor_value"); |
| 80 | + if (fn == NULL || fn() != 42) { |
| 81 | + fail("init_array fires on dlopen", "ctor_value() wrong"); |
| 82 | + dlclose(h); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + /* Reset the dtor witness so test_fini_array measures a fresh signal. */ |
| 87 | + g_dtor_ran = 0; |
| 88 | + dlclose(h); |
| 89 | + |
| 90 | + pass("init_array fires on dlopen"); |
| 91 | + |
| 92 | + /* Bridge directly into the destructor test while the witness is fresh. */ |
| 93 | + if (g_dtor_ran != 0xD70A) { |
| 94 | + fail("fini_array fires on dlclose", "destructor sentinel not set"); |
| 95 | + return; |
| 96 | + } |
| 97 | + pass("fini_array fires on dlclose"); |
| 98 | +} |
| 99 | + |
| 100 | +/* |
| 101 | + * Test 3: DT_RUNPATH must be consulted when resolving DT_NEEDED bare |
| 102 | + * names. libparent.so depends on libchild.so but libchild.so only |
| 103 | + * exists under lib/subdir/, which is libparent's DT_RUNPATH. |
| 104 | + */ |
| 105 | +static void test_dt_runpath(void) |
| 106 | +{ |
| 107 | + void *h = dlopen("lib/libparent.so", RTLD_NOW); |
| 108 | + if (h == NULL) { |
| 109 | + fail("DT_RUNPATH dependency search", dlerror()); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + int (*fn)(int) = NULL; |
| 114 | + *(void **)(&fn) = dlsym(h, "parent_value"); |
| 115 | + if (fn == NULL || fn(5) != 24) { |
| 116 | + /* parent_value(5) = child_value(5) * 2 = (5 + 7) * 2 = 24 */ |
| 117 | + fail("DT_RUNPATH dependency search", "parent_value() wrong"); |
| 118 | + dlclose(h); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + dlclose(h); |
| 123 | + pass("DT_RUNPATH dependency search"); |
| 124 | +} |
| 125 | + |
| 126 | +int main(int argc, const char *argv[]) |
| 127 | +{ |
| 128 | + (void)argc; |
| 129 | + (void)argv; |
| 130 | + |
| 131 | + printf("=== dlfcn init_array + DT_RUNPATH tests ===\n"); |
| 132 | + fflush(stdout); |
| 133 | + |
| 134 | + test_init_array(); |
| 135 | + test_dt_runpath(); |
| 136 | + |
| 137 | + printf("\n%d passed, %d failed\n", tests_passed, tests_failed); |
| 138 | + fflush(stdout); |
| 139 | + |
| 140 | + if (tests_failed == 0) { |
| 141 | + const char *magic = "ok"; |
| 142 | + write(STDOUT_FILENO, magic, 3); |
| 143 | + } |
| 144 | + |
| 145 | + return tests_failed > 0 ? 1 : 0; |
| 146 | +} |
0 commit comments