Blame elf/tst-dlopenfail.c

Packit Bot f2790b
/* Test dlopen rollback after failures involving NODELETE objects (bug 20839).
Packit Bot f2790b
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Bot f2790b
   This file is part of the GNU C Library.
Packit Bot f2790b
Packit Bot f2790b
   The GNU C Library is free software; you can redistribute it and/or
Packit Bot f2790b
   modify it under the terms of the GNU Lesser General Public
Packit Bot f2790b
   License as published by the Free Software Foundation; either
Packit Bot f2790b
   version 2.1 of the License, or (at your option) any later version.
Packit Bot f2790b
Packit Bot f2790b
   The GNU C Library is distributed in the hope that it will be useful,
Packit Bot f2790b
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot f2790b
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot f2790b
   Lesser General Public License for more details.
Packit Bot f2790b
Packit Bot f2790b
   You should have received a copy of the GNU Lesser General Public
Packit Bot f2790b
   License along with the GNU C Library; if not, see
Packit Bot f2790b
   <https://www.gnu.org/licenses/>.  */
Packit Bot f2790b
Packit Bot f2790b
#include <dlfcn.h>
Packit Bot f2790b
#include <errno.h>
Packit Bot f2790b
#include <gnu/lib-names.h>
Packit Bot f2790b
#include <stddef.h>
Packit Bot f2790b
#include <stdio.h>
Packit Bot f2790b
#include <string.h>
Packit Bot f2790b
#include <support/check.h>
Packit Bot f2790b
#include <support/xdlfcn.h>
Packit Bot f2790b
Packit Bot f2790b
static int
Packit Bot f2790b
do_test (void)
Packit Bot f2790b
{
Packit Bot f2790b
  /* This test uses libpthread as the canonical NODELETE module.  If
Packit Bot f2790b
     libpthread is no longer NODELETE because it has been merged into
Packit Bot f2790b
     libc, the test needs to be updated.  */
Packit Bot f2790b
  TEST_VERIFY (dlsym (NULL, "pthread_create") == NULL);
Packit Bot f2790b
Packit Bot f2790b
  /* This is expected to fail because of the missing dependency.  */
Packit Bot f2790b
  puts ("info: attempting to load tst-dlopenfailmod1.so");
Packit Bot f2790b
  TEST_VERIFY (dlopen ("tst-dlopenfailmod1.so", RTLD_LAZY) == NULL);
Packit Bot f2790b
  const char *message = dlerror ();
Packit Bot f2790b
  TEST_COMPARE_STRING (message,
Packit Bot f2790b
                       "tst-dlopenfail-missingmod.so:"
Packit Bot f2790b
                       " cannot open shared object file:"
Packit Bot f2790b
                       " No such file or directory");
Packit Bot f2790b
Packit Bot f2790b
  /* Do not probe for the presence of libpthread at this point because
Packit Bot f2790b
     that might trigger relocation if bug 20839 is present, obscuring
Packit Bot f2790b
     a subsequent crash.  */
Packit Bot f2790b
Packit Bot f2790b
  /* This is expected to succeed.  */
Packit Bot f2790b
  puts ("info: loading tst-dlopenfailmod2.so");
Packit Bot f2790b
  void *handle = xdlopen ("tst-dlopenfailmod2.so", RTLD_NOW);
Packit Bot f2790b
  xdlclose (handle);
Packit Bot f2790b
Packit Bot f2790b
  /* libpthread should remain loaded.  */
Packit Bot f2790b
  TEST_VERIFY (dlopen (LIBPTHREAD_SO, RTLD_LAZY | RTLD_NOLOAD) != NULL);
Packit Bot f2790b
  TEST_VERIFY (dlsym (NULL, "pthread_create") == NULL);
Packit Bot f2790b
Packit Bot f2790b
  /* We can make libpthread global, and then the symbol should become
Packit Bot f2790b
     available.  */
Packit Bot f2790b
  TEST_VERIFY (dlopen (LIBPTHREAD_SO, RTLD_LAZY | RTLD_GLOBAL) != NULL);
Packit Bot f2790b
  TEST_VERIFY (dlsym (NULL, "pthread_create") != NULL);
Packit Bot f2790b
Packit Bot f2790b
  /* sem_open is sufficiently complex to depend on relocations.  */
Packit Bot f2790b
  void *(*sem_open_ptr) (const char *, int flag, ...)
Packit Bot f2790b
    = dlsym (NULL, "sem_open");
Packit Bot f2790b
  if (sem_open_ptr == NULL)
Packit Bot f2790b
    /* Hurd does not implement sem_open.  */
Packit Bot f2790b
    puts ("warning: sem_open not found, further testing not possible");
Packit Bot f2790b
  else
Packit Bot f2790b
    {
Packit Bot f2790b
      errno = 0;
Packit Bot f2790b
      TEST_VERIFY (sem_open_ptr ("/", 0) == NULL);
Packit Bot f2790b
      TEST_COMPARE (errno, EINVAL);
Packit Bot f2790b
    }
Packit Bot f2790b
Packit Bot f2790b
  return 0;
Packit Bot f2790b
}
Packit Bot f2790b
Packit Bot f2790b
#include <support/test-driver.c>