about summary refs log tree commit diff
path: root/elf/tst-glibc-hwcaps-prepend-cache.c
blob: f3c7588624c560676da396ee389ebabe304230dc (plain) (blame)
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
/* Test that --glibc-hwcaps-prepend works, using dlopen and /etc/ld.so.cache.
   Copyright (C) 2020-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <dlfcn.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
#include <support/xdlfcn.h>
#include <support/xunistd.h>

/* Invoke /sbin/ldconfig with some error checking.  */
static void
run_ldconfig (void)
{
  char *command = xasprintf ("%s/ldconfig", support_install_rootsbindir);
  TEST_COMPARE (system (command), 0);
  free (command);
}

/* The library under test.  */
#define SONAME "libmarkermod1.so"

static int
do_test (void)
{
  if (dlopen (SONAME, RTLD_NOW) != NULL)
    FAIL_EXIT1 (SONAME " is already on the search path");

  {
    /* Install the default implementation of libmarkermod1.so.  */
    char *conf_path = xasprintf ("%s/ld.so.conf", support_sysconfdir_prefix);
    xmkdirp (support_sysconfdir_prefix, 0777);
    support_write_file_string (conf_path, "/glibc-test/lib\n");
    free (conf_path);
  }
  xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend2", 0777);
  xmkdirp ("/glibc-test/lib/glibc-hwcaps/prepend3", 0777);
  {
    char *src = xasprintf ("%s/elf/libmarkermod1-1.so", support_objdir_root);
    support_copy_file (src, "/glibc-test/lib/" SONAME);
    free (src);
  }
  run_ldconfig ();
  {
    /* The default implementation can now be loaded.  */
    void *handle = xdlopen (SONAME, RTLD_NOW);
    int (*marker1) (void) = xdlsym (handle, "marker1");
    TEST_COMPARE (marker1 (), 1);
    xdlclose (handle);
  }

  /* Add the first override to the directory that is searched last.  */
  {
    char *src = xasprintf ("%s/elf/libmarkermod1-2.so", support_objdir_root);
    support_copy_file (src, "/glibc-test/lib/glibc-hwcaps/prepend2/"
                       SONAME);
    free (src);
  }
  {
    /* This is still the first implementation.  The cache has not been
       updated.  */
    void *handle = xdlopen (SONAME, RTLD_NOW);
    int (*marker1) (void) = xdlsym (handle, "marker1");
    TEST_COMPARE (marker1 (), 1);
    xdlclose (handle);
  }
  run_ldconfig ();
  {
    /* After running ldconfig, it is the second implementation.  */
    void *handle = xdlopen (SONAME, RTLD_NOW);
    int (*marker1) (void) = xdlsym (handle, "marker1");
    TEST_COMPARE (marker1 (), 2);
    xdlclose (handle);
  }

  /* Add the second override to the directory that is searched first.  */
  {
    char *src = xasprintf ("%s/elf/libmarkermod1-3.so", support_objdir_root);
    support_copy_file (src, "/glibc-test/lib/glibc-hwcaps/prepend3/"
                       SONAME);
    free (src);
  }
  {
    /* This is still the second implementation.  */
    void *handle = xdlopen (SONAME, RTLD_NOW);
    int (*marker1) (void) = xdlsym (handle, "marker1");
    TEST_COMPARE (marker1 (), 2);
    xdlclose (handle);
  }
  run_ldconfig ();
  {
    /* After running ldconfig, it is the third implementation.  */
    void *handle = xdlopen (SONAME, RTLD_NOW);
    int (*marker1) (void) = xdlsym (handle, "marker1");
    TEST_COMPARE (marker1 (), 3);
    xdlclose (handle);
  }

  /* Remove the second override again, without running ldconfig.
     Ideally, this would revert to implementation 2.  However, in the
     current implementation, the cache returns exactly one file name
     which does not exist after unlinking, so the dlopen fails.  */
  xunlink ("/glibc-test/lib/glibc-hwcaps/prepend3/" SONAME);
  TEST_VERIFY (dlopen (SONAME, RTLD_NOW) == NULL);
  run_ldconfig ();
  {
    /* After running ldconfig, the second implementation is available
       once more.  */
    void *handle = xdlopen (SONAME, RTLD_NOW);
    int (*marker1) (void) = xdlsym (handle, "marker1");
    TEST_COMPARE (marker1 (), 2);
    xdlclose (handle);
  }

  return 0;
}

static void
prepare (int argc, char **argv)
{
  const char *no_restart = "no-restart";
  if (argc == 2 && strcmp (argv[1], no_restart) == 0)
    return;
  /* Re-execute the test with an explicit loader invocation.  */
  execl (support_objdir_elf_ldso,
         support_objdir_elf_ldso,
         "--glibc-hwcaps-prepend", "prepend3:prepend2",
         argv[0], no_restart,
         NULL);
  printf ("error: execv of %s failed: %m\n", argv[0]);
  _exit (1);
}

#define PREPARE prepare
#include <support/test-driver.c>