[9182e86f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2016 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup dltest
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief Test dynamic linking
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <dlfcn.h>
|
---|
| 39 | #include <libdltest.h>
|
---|
| 40 | #include <stdbool.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 |
|
---|
| 44 | /** libdltest library handle */
|
---|
| 45 | void *handle;
|
---|
| 46 |
|
---|
| 47 | /** Test dlsym() function */
|
---|
| 48 | static bool test_dlsym(void)
|
---|
| 49 | {
|
---|
| 50 | int (*p_dl_get_constant)(void);
|
---|
| 51 |
|
---|
| 52 | printf("dlsym()... ");
|
---|
| 53 | p_dl_get_constant = dlsym(handle, "dl_get_constant");
|
---|
| 54 | if (p_dl_get_constant == NULL) {
|
---|
| 55 | printf("FAILED\n");
|
---|
| 56 | return false;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | printf("Passed\n");
|
---|
| 60 | return true;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /** Test calling function that returns a constant */
|
---|
| 64 | static bool test_dlfcn_dl_get_constant(void)
|
---|
| 65 | {
|
---|
| 66 | int (*p_dl_get_constant)(void);
|
---|
| 67 | int val;
|
---|
| 68 |
|
---|
| 69 | printf("Call dlsym/dl_get_constant...\n");
|
---|
| 70 |
|
---|
| 71 | p_dl_get_constant = dlsym(handle, "dl_get_constant");
|
---|
| 72 | if (p_dl_get_constant == NULL) {
|
---|
| 73 | printf("FAILED\n");
|
---|
| 74 | return false;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | val = p_dl_get_constant();
|
---|
| 78 |
|
---|
| 79 | printf("Got %d, expected %d... ", val, dl_constant);
|
---|
| 80 | if (val != dl_constant) {
|
---|
| 81 | printf("FAILED\n");
|
---|
| 82 | return false;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | printf("Passed\n");
|
---|
| 86 | return true;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /** Test calling a function that returns contents of a private initialized
|
---|
| 90 | * variable.
|
---|
| 91 | */
|
---|
| 92 | static bool test_dlfcn_dl_get_private_var(void)
|
---|
| 93 | {
|
---|
| 94 | int (*p_dl_get_private_var)(void);
|
---|
| 95 | int val;
|
---|
| 96 |
|
---|
| 97 | printf("Call dlsym/dl_get_private_var...\n");
|
---|
| 98 |
|
---|
| 99 | p_dl_get_private_var = dlsym(handle, "dl_get_private_var");
|
---|
| 100 | if (p_dl_get_private_var == NULL) {
|
---|
| 101 | printf("FAILED\n");
|
---|
| 102 | return false;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | val = p_dl_get_private_var();
|
---|
| 106 |
|
---|
| 107 | printf("Got %d, expected %d... ", val, dl_private_var_val);
|
---|
| 108 | if (val != dl_private_var_val) {
|
---|
| 109 | printf("FAILED\n");
|
---|
| 110 | return false;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | printf("Passed\n");
|
---|
| 114 | return true;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /** Test calling a function that returns contents of a private uninitialized
|
---|
| 118 | * variable.
|
---|
| 119 | */
|
---|
| 120 | static bool test_dlfcn_dl_get_private_uvar(void)
|
---|
| 121 | {
|
---|
| 122 | int (*p_dl_get_private_uvar)(void);
|
---|
| 123 | int val;
|
---|
| 124 |
|
---|
| 125 | printf("Call dlsym/dl_get_private_uvar...\n");
|
---|
| 126 |
|
---|
| 127 | p_dl_get_private_uvar = dlsym(handle, "dl_get_private_uvar");
|
---|
| 128 | if (p_dl_get_private_uvar == NULL) {
|
---|
| 129 | printf("FAILED\n");
|
---|
| 130 | return false;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | val = p_dl_get_private_uvar();
|
---|
| 134 |
|
---|
| 135 | printf("Got %d, expected %d... ", val, 0);
|
---|
| 136 | if (val != 0) {
|
---|
| 137 | printf("FAILED\n");
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | printf("Passed\n");
|
---|
| 142 | return true;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /** Test calling a function that returns the contents of a public initialized
|
---|
| 146 | * variable.
|
---|
| 147 | */
|
---|
| 148 | static bool test_dlfcn_dl_get_public_var(void)
|
---|
| 149 | {
|
---|
| 150 | int (*p_dl_get_public_var)(void);
|
---|
| 151 | int val;
|
---|
| 152 |
|
---|
| 153 | printf("Call dlsym/dl_get_public_var...\n");
|
---|
| 154 |
|
---|
| 155 | p_dl_get_public_var = dlsym(handle, "dl_get_public_var");
|
---|
| 156 | if (p_dl_get_public_var == NULL) {
|
---|
| 157 | printf("FAILED\n");
|
---|
| 158 | return false;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | val = p_dl_get_public_var();
|
---|
| 162 |
|
---|
| 163 | printf("Got %d, expected %d... ", val, dl_public_var_val);
|
---|
| 164 | if (val != dl_public_var_val) {
|
---|
| 165 | printf("FAILED\n");
|
---|
| 166 | return false;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | printf("Passed\n");
|
---|
| 170 | return true;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /** Test calling a function that returns the contents of a public uninitialized
|
---|
| 174 | * variable.
|
---|
| 175 | */
|
---|
| 176 | static bool test_dlfcn_dl_get_public_uvar(void)
|
---|
| 177 | {
|
---|
| 178 | int (*p_dl_get_public_uvar)(void);
|
---|
| 179 | int val;
|
---|
| 180 |
|
---|
| 181 | printf("Call dlsym/dl_get_public_uvar...\n");
|
---|
| 182 |
|
---|
| 183 | p_dl_get_public_uvar = dlsym(handle, "dl_get_public_uvar");
|
---|
| 184 | if (p_dl_get_public_uvar == NULL) {
|
---|
| 185 | printf("FAILED\n");
|
---|
| 186 | return false;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | val = p_dl_get_public_uvar();
|
---|
| 190 |
|
---|
| 191 | printf("Got %d, expected %d... ", val, 0);
|
---|
| 192 | if (val != 0) {
|
---|
| 193 | printf("FAILED\n");
|
---|
| 194 | return false;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | printf("Passed\n");
|
---|
| 198 | return true;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /** Test directly reading a public initialized variable whose address was
|
---|
| 202 | * obtained using dlsym.
|
---|
| 203 | */
|
---|
| 204 | static bool test_dlfcn_read_public_var(void)
|
---|
| 205 | {
|
---|
| 206 | int *p_dl_public_var;
|
---|
| 207 | int val;
|
---|
| 208 |
|
---|
| 209 | printf("Read dlsym/dl_public_var...\n");
|
---|
| 210 |
|
---|
| 211 | p_dl_public_var = dlsym(handle, "dl_public_var");
|
---|
| 212 | if (p_dl_public_var == NULL) {
|
---|
| 213 | printf("FAILED\n");
|
---|
| 214 | return false;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | val = *p_dl_public_var;
|
---|
| 218 |
|
---|
| 219 | printf("Got %d, expected %d... ", val, dl_public_var_val);
|
---|
| 220 | if (val != dl_public_var_val) {
|
---|
| 221 | printf("FAILED\n");
|
---|
| 222 | return false;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | printf("Passed\n");
|
---|
| 226 | return true;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /** Test directly reading a public uninitialized variable whose address was
|
---|
| 230 | * obtained using dlsym.
|
---|
| 231 | */
|
---|
| 232 | static bool test_dlfcn_read_public_uvar(void)
|
---|
| 233 | {
|
---|
| 234 | int *p_dl_public_uvar;
|
---|
| 235 | int val;
|
---|
| 236 |
|
---|
| 237 | printf("Read dlsym/dl_public_uvar...\n");
|
---|
| 238 |
|
---|
| 239 | p_dl_public_uvar = dlsym(handle, "dl_public_uvar");
|
---|
| 240 | if (p_dl_public_uvar == NULL) {
|
---|
| 241 | printf("FAILED\n");
|
---|
| 242 | return false;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | val = *p_dl_public_uvar;
|
---|
| 246 |
|
---|
| 247 | printf("Got %d, expected %d... ", val, 0);
|
---|
| 248 | if (val != 0) {
|
---|
| 249 | printf("FAILED\n");
|
---|
| 250 | return false;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | printf("Passed\n");
|
---|
| 254 | return true;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | int main(int argc, char *argv[])
|
---|
| 258 | {
|
---|
| 259 |
|
---|
| 260 | printf("Dynamic linking test\n");
|
---|
| 261 |
|
---|
| 262 | printf("dlopen()... ");
|
---|
| 263 | handle = dlopen("libdltest.so.0", 0);
|
---|
| 264 | if (handle == NULL) {
|
---|
| 265 | printf("FAILED\n");
|
---|
| 266 | return 1;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | printf("Passed\n");
|
---|
| 270 |
|
---|
| 271 | if (!test_dlsym())
|
---|
| 272 | return 1;
|
---|
| 273 |
|
---|
| 274 | if (!test_dlfcn_dl_get_constant())
|
---|
| 275 | return 1;
|
---|
| 276 |
|
---|
| 277 | if (!test_dlfcn_dl_get_private_var())
|
---|
| 278 | return 1;
|
---|
| 279 |
|
---|
| 280 | if (!test_dlfcn_dl_get_private_uvar())
|
---|
| 281 | return 1;
|
---|
| 282 |
|
---|
| 283 | if (!test_dlfcn_dl_get_public_var())
|
---|
| 284 | return 1;
|
---|
| 285 |
|
---|
| 286 | if (!test_dlfcn_dl_get_public_uvar())
|
---|
| 287 | return 1;
|
---|
| 288 |
|
---|
| 289 | if (!test_dlfcn_read_public_var())
|
---|
| 290 | return 1;
|
---|
| 291 |
|
---|
| 292 | if (!test_dlfcn_read_public_uvar())
|
---|
| 293 | return 1;
|
---|
| 294 |
|
---|
| 295 | // printf("dlclose()... ");
|
---|
| 296 | // dlclose(handle);
|
---|
| 297 | // printf("Passed\n");
|
---|
| 298 |
|
---|
| 299 | printf("All passed.\n");
|
---|
| 300 | return 0;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | /**
|
---|
| 304 | * @}
|
---|
| 305 | */
|
---|