aarch64: Move and update the definition of MTE_ENABLED
[glibc.git] / elf / tst-pldd.c
blob210ca4d24b8338abf5d06c10555fb9aa419473b6
1 /* Basic tests for pldd program.
2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdint.h>
24 #include <stdbool.h>
26 #include <array_length.h>
27 #include <gnu/lib-names.h>
29 #include <support/subprocess.h>
30 #include <support/capture_subprocess.h>
31 #include <support/check.h>
32 #include <support/support.h>
33 #include <support/xptrace.h>
34 #include <support/xunistd.h>
35 #include <sys/mman.h>
36 #include <errno.h>
37 #include <signal.h>
39 static void
40 target_process (void *arg)
42 pause ();
45 static void
46 pldd_process (void *arg)
48 pid_t *target_pid_ptr = (pid_t *) arg;
50 /* Create a copy of current test to check with pldd. As the
51 target_process is a child of this pldd_process, pldd is also able
52 to attach to target_process if YAMA is configured to 1 =
53 "restricted ptrace". */
54 struct support_subprocess target = support_subprocess (target_process, NULL);
56 /* Store the pid of target-process as do_test needs it in order to
57 e.g. terminate it at end of the test. */
58 *target_pid_ptr = target.pid;
60 /* Three digits per byte plus null terminator. */
61 char pid[3 * sizeof (uint32_t) + 1];
62 snprintf (pid, array_length (pid), "%d", target.pid);
64 char *prog = xasprintf ("%s/pldd", support_bindir_prefix);
66 /* Run pldd and use the pid of target_process as argument. */
67 execve (prog, (char *const []) { (char *) prog, pid, NULL },
68 (char *const []) { NULL });
70 FAIL_EXIT1 ("Returned from execve: errno=%d=%m\n", errno);
73 /* The test runs in a container because pldd does not support tracing
74 a binary started by the loader iself (as with testrun.sh). */
76 static bool
77 in_str_list (const char *libname, const char *const strlist[])
79 for (const char *const *str = strlist; *str != NULL; str++)
80 if (strcmp (libname, *str) == 0)
81 return true;
82 return false;
85 static int
86 do_test (void)
88 /* Check if our subprocess can be debugged with ptrace. */
90 int ptrace_scope = support_ptrace_scope ();
91 if (ptrace_scope >= 2)
92 FAIL_UNSUPPORTED ("/proc/sys/kernel/yama/ptrace_scope >= 2");
95 pid_t *target_pid_ptr = (pid_t *) xmmap (NULL, sizeof (pid_t),
96 PROT_READ | PROT_WRITE,
97 MAP_SHARED | MAP_ANONYMOUS, -1);
99 /* Run 'pldd' on test subprocess which will be created in pldd_process.
100 The pid of the subprocess will be written to target_pid_ptr. */
101 struct support_capture_subprocess pldd;
102 pldd = support_capture_subprocess (pldd_process, target_pid_ptr);
103 support_capture_subprocess_check (&pldd, "pldd", 0, sc_allow_stdout);
105 /* Check 'pldd' output. The test is expected to be linked against only
106 loader and libc. */
108 pid_t pid;
109 char buffer[512];
110 #define STRINPUT(size) "%" # size "s"
112 FILE *out = fmemopen (pldd.out.buffer, pldd.out.length, "r");
113 TEST_VERIFY (out != NULL);
115 /* First line is in the form of <pid>: <full path of executable> */
116 TEST_COMPARE (fscanf (out, "%u: " STRINPUT (512), &pid, buffer), 2);
118 TEST_COMPARE (pid, *target_pid_ptr);
119 TEST_COMPARE (strcmp (basename (buffer), "tst-pldd"), 0);
121 /* It expects only one loader and libc loaded by the program. */
122 bool interpreter_found = false, libc_found = false;
123 while (fgets (buffer, array_length (buffer), out) != NULL)
125 /* Ignore vDSO. */
126 if (buffer[0] != '/')
127 continue;
129 /* Remove newline so baseline (buffer) can compare against the
130 LD_SO and LIBC_SO macros unmodified. */
131 if (buffer[strlen(buffer)-1] == '\n')
132 buffer[strlen(buffer)-1] = '\0';
134 const char *libname = basename (buffer);
136 /* It checks for default names in case of build configure with
137 --enable-hardcoded-path-in-tests (BZ #24506). */
138 if (in_str_list (libname,
139 (const char *const []) { "ld.so", LD_SO, NULL }))
141 TEST_COMPARE (interpreter_found, false);
142 interpreter_found = true;
143 continue;
146 if (in_str_list (libname,
147 (const char *const []) { "libc.so", LIBC_SO, NULL }))
149 TEST_COMPARE (libc_found, false);
150 libc_found = true;
151 continue;
154 TEST_COMPARE (interpreter_found, true);
155 TEST_COMPARE (libc_found, true);
157 fclose (out);
160 support_capture_subprocess_free (&pldd);
161 if (kill (*target_pid_ptr, SIGKILL) != 0)
162 FAIL_EXIT1 ("Unable to kill target_process: errno=%d=%m\n", errno);
163 xmunmap (target_pid_ptr, sizeof (pid_t));
165 return 0;
168 #include <support/test-driver.c>