Blame elf/tst-pldd.c

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