Blame elf/tst-pldd.c

Packit Service d6ac51
/* Basic tests for pldd program.
Packit Service d6ac51
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service d6ac51
   This file is part of the GNU C Library.
Packit Service d6ac51
Packit Service d6ac51
   The GNU C Library is free software; you can redistribute it and/or
Packit Service d6ac51
   modify it under the terms of the GNU Lesser General Public
Packit Service d6ac51
   License as published by the Free Software Foundation; either
Packit Service d6ac51
   version 2.1 of the License, or (at your option) any later version.
Packit Service d6ac51
Packit Service d6ac51
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service d6ac51
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service d6ac51
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service d6ac51
   Lesser General Public License for more details.
Packit Service d6ac51
Packit Service d6ac51
   You should have received a copy of the GNU Lesser General Public
Packit Service d6ac51
   License along with the GNU C Library; if not, see
Packit Service d6ac51
   <http://www.gnu.org/licenses/>.  */
Packit Service d6ac51
Packit Service d6ac51
#include <stdio.h>
Packit Service d6ac51
#include <string.h>
Packit Service 498173
#include <stdlib.h>
Packit Service d6ac51
#include <unistd.h>
Packit Service d6ac51
#include <stdint.h>
Packit Service d6ac51
#include <stdbool.h>
Packit Service d6ac51
Packit Service d6ac51
#include <array_length.h>
Packit Service d6ac51
#include <gnu/lib-names.h>
Packit Service d6ac51
Packit Service d6ac51
#include <support/subprocess.h>
Packit Service d6ac51
#include <support/capture_subprocess.h>
Packit Service d6ac51
#include <support/check.h>
Packit Service 498173
#include <support/support.h>
Packit Service dd7248
#include <support/xptrace.h>
Packit Service dd7248
#include <support/xunistd.h>
Packit Service dd7248
#include <sys/mman.h>
Packit Service dd7248
#include <errno.h>
Packit Service dd7248
#include <signal.h>
Packit Service d6ac51
Packit Service d6ac51
static void
Packit Service d6ac51
target_process (void *arg)
Packit Service d6ac51
{
Packit Service d6ac51
  pause ();
Packit Service d6ac51
}
Packit Service d6ac51
Packit Service dd7248
static void
Packit Service dd7248
pldd_process (void *arg)
Packit Service dd7248
{
Packit Service dd7248
  pid_t *target_pid_ptr = (pid_t *) arg;
Packit Service dd7248
Packit Service dd7248
  /* Create a copy of current test to check with pldd.  As the
Packit Service dd7248
     target_process is a child of this pldd_process, pldd is also able
Packit Service dd7248
     to attach to target_process if YAMA is configured to 1 =
Packit Service dd7248
     "restricted ptrace".  */
Packit Service dd7248
  struct support_subprocess target = support_subprocess (target_process, NULL);
Packit Service dd7248
Packit Service dd7248
  /* Store the pid of target-process as do_test needs it in order to
Packit Service dd7248
     e.g. terminate it at end of the test.  */
Packit Service dd7248
  *target_pid_ptr = target.pid;
Packit Service dd7248
Packit Service dd7248
  /* Three digits per byte plus null terminator.  */
Packit Service dd7248
  char pid[3 * sizeof (uint32_t) + 1];
Packit Service dd7248
  snprintf (pid, array_length (pid), "%d", target.pid);
Packit Service dd7248
Packit Service dd7248
  char *prog = xasprintf ("%s/pldd", support_bindir_prefix);
Packit Service dd7248
Packit Service dd7248
  /* Run pldd and use the pid of target_process as argument.  */
Packit Service dd7248
  execve (prog, (char *const []) { (char *) prog, pid, NULL },
Packit Service dd7248
	  (char *const []) { NULL });
Packit Service dd7248
Packit Service dd7248
  FAIL_EXIT1 ("Returned from execve: errno=%d=%m\n", errno);
Packit Service dd7248
}
Packit Service dd7248
Packit Service d6ac51
/* The test runs in a container because pldd does not support tracing
Packit Service d6ac51
   a binary started by the loader iself (as with testrun.sh).  */
Packit Service d6ac51
Packit Service c0892c
static bool
Packit Service c0892c
in_str_list (const char *libname, const char *const strlist[])
Packit Service c0892c
{
Packit Service c0892c
  for (const char *const *str = strlist; *str != NULL; str++)
Packit Service c0892c
    if (strcmp (libname, *str) == 0)
Packit Service c0892c
      return true;
Packit Service c0892c
  return false;
Packit Service c0892c
}
Packit Service c0892c
Packit Service d6ac51
static int
Packit Service d6ac51
do_test (void)
Packit Service d6ac51
{
Packit Service dd7248
  /* Check if our subprocess can be debugged with ptrace.  */
Packit Service d6ac51
  {
Packit Service dd7248
    int ptrace_scope = support_ptrace_scope ();
Packit Service dd7248
    if (ptrace_scope >= 2)
Packit Service dd7248
      FAIL_UNSUPPORTED ("/proc/sys/kernel/yama/ptrace_scope >= 2");
Packit Service dd7248
  }
Packit Service d6ac51
Packit Service dd7248
  pid_t *target_pid_ptr = (pid_t *) xmmap (NULL, sizeof (pid_t),
Packit Service dd7248
					   PROT_READ | PROT_WRITE,
Packit Service dd7248
					   MAP_SHARED | MAP_ANONYMOUS, -1);
Packit Service 498173
Packit Service dd7248
  /* Run 'pldd' on test subprocess which will be created in pldd_process.
Packit Service dd7248
     The pid of the subprocess will be written to target_pid_ptr.  */
Packit Service dd7248
  struct support_capture_subprocess pldd;
Packit Service dd7248
  pldd = support_capture_subprocess (pldd_process, target_pid_ptr);
Packit Service dd7248
  support_capture_subprocess_check (&pldd, "pldd", 0, sc_allow_stdout);
Packit Service d6ac51
Packit Service d6ac51
  /* Check 'pldd' output.  The test is expected to be linked against only
Packit Service d6ac51
     loader and libc.  */
Packit Service d6ac51
  {
Packit Service d6ac51
    pid_t pid;
Packit Service d6ac51
    char buffer[512];
Packit Service d6ac51
#define STRINPUT(size) "%" # size "s"
Packit Service d6ac51
Packit Service d6ac51
    FILE *out = fmemopen (pldd.out.buffer, pldd.out.length, "r");
Packit Service d6ac51
    TEST_VERIFY (out != NULL);
Packit Service d6ac51
Packit Service d6ac51
    /* First line is in the form of <pid>: <full path of executable>  */
Packit Service d6ac51
    TEST_COMPARE (fscanf (out, "%u: " STRINPUT (512), &pid, buffer), 2);
Packit Service d6ac51
Packit Service dd7248
    TEST_COMPARE (pid, *target_pid_ptr);
Packit Service d6ac51
    TEST_COMPARE (strcmp (basename (buffer), "tst-pldd"), 0);
Packit Service d6ac51
Packit Service d6ac51
    /* It expects only one loader and libc loaded by the program.  */
Packit Service d6ac51
    bool interpreter_found = false, libc_found = false;
Packit Service d6ac51
    while (fgets (buffer, array_length (buffer), out) != NULL)
Packit Service d6ac51
      {
Packit Service dd7248
	/* Ignore vDSO.  */
Packit Service dd7248
	if (buffer[0] != '/')
Packit Service c0892c
	  continue;
Packit Service c0892c
Packit Service c0892c
	/* Remove newline so baseline (buffer) can compare against the
Packit Service c0892c
	   LD_SO and LIBC_SO macros unmodified.  */
Packit Service c0892c
	if (buffer[strlen(buffer)-1] == '\n')
Packit Service c0892c
	  buffer[strlen(buffer)-1] = '\0';
Packit Service c0892c
Packit Service c0892c
	const char *libname = basename (buffer);
Packit Service c0892c
Packit Service c0892c
	/* It checks for default names in case of build configure with
Packit Service c0892c
	   --enable-hardcoded-path-in-tests (BZ #24506).  */
Packit Service c0892c
	if (in_str_list (libname,
Packit Service c0892c
			 (const char *const []) { "ld.so", LD_SO, NULL }))
Packit Service c0892c
	  {
Packit Service c0892c
	    TEST_COMPARE (interpreter_found, false);
Packit Service c0892c
	    interpreter_found = true;
Packit Service c0892c
	    continue;
Packit Service c0892c
	  }
Packit Service c0892c
Packit Service c0892c
	if (in_str_list (libname,
Packit Service c0892c
			 (const char *const []) { "libc.so", LIBC_SO, NULL }))
Packit Service c0892c
	  {
Packit Service c0892c
	    TEST_COMPARE (libc_found, false);
Packit Service c0892c
	    libc_found = true;
Packit Service c0892c
	    continue;
Packit Service c0892c
	  }
Packit Service d6ac51
      }
Packit Service d6ac51
    TEST_COMPARE (interpreter_found, true);
Packit Service d6ac51
    TEST_COMPARE (libc_found, true);
Packit Service d6ac51
Packit Service d6ac51
    fclose (out);
Packit Service d6ac51
  }
Packit Service d6ac51
Packit Service d6ac51
  support_capture_subprocess_free (&pldd);
Packit Service dd7248
  if (kill (*target_pid_ptr, SIGKILL) != 0)
Packit Service dd7248
    FAIL_EXIT1 ("Unable to kill target_process: errno=%d=%m\n", errno);
Packit Service dd7248
  xmunmap (target_pid_ptr, sizeof (pid_t));
Packit Service d6ac51
Packit Service d6ac51
  return 0;
Packit Service d6ac51
}
Packit Service d6ac51
Packit Service d6ac51
#include <support/test-driver.c>