Blame elf/tst-dlopenfail.c

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