1  /* Test disabling of rseq registration via tunable.
       2     Copyright (C) 2021-2023 Free Software Foundation, Inc.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <errno.h>
      19  #include <stdio.h>
      20  #include <support/check.h>
      21  #include <support/namespace.h>
      22  #include <support/xthread.h>
      23  #include <sysdep.h>
      24  #include <thread_pointer.h>
      25  #include <sys/rseq.h>
      26  #include <unistd.h>
      27  
      28  #ifdef RSEQ_SIG
      29  
      30  /* Check that rseq can be registered and has not been taken by glibc.  */
      31  static void
      32  check_rseq_disabled (void)
      33  {
      34    struct pthread *pd = THREAD_SELF;
      35  
      36    TEST_COMPARE (__rseq_flags, 0);
      37    TEST_VERIFY ((char *) __thread_pointer () + __rseq_offset
      38                 == (char *) &pd->rseq_area);
      39    TEST_COMPARE (__rseq_size, 0);
      40    TEST_COMPARE ((int) pd->rseq_area.cpu_id, RSEQ_CPU_ID_REGISTRATION_FAILED);
      41  
      42    int ret = syscall (__NR_rseq, &pd->rseq_area, sizeof (pd->rseq_area),
      43                       0, RSEQ_SIG);
      44    if (ret == 0)
      45      {
      46        ret = syscall (__NR_rseq, &pd->rseq_area, sizeof (pd->rseq_area),
      47                       RSEQ_FLAG_UNREGISTER, RSEQ_SIG);
      48        TEST_COMPARE (ret, 0);
      49        pd->rseq_area.cpu_id = RSEQ_CPU_ID_REGISTRATION_FAILED;
      50      }
      51    else
      52      {
      53        TEST_VERIFY (errno != -EINVAL);
      54        TEST_VERIFY (errno != -EBUSY);
      55      }
      56  }
      57  
      58  static void *
      59  thread_func (void *ignored)
      60  {
      61    check_rseq_disabled ();
      62    return NULL;
      63  }
      64  
      65  static void
      66  proc_func (void *ignored)
      67  {
      68    check_rseq_disabled ();
      69  }
      70  
      71  static int
      72  do_test (void)
      73  {
      74    puts ("info: checking main thread");
      75    check_rseq_disabled ();
      76  
      77    puts ("info: checking main thread (2)");
      78    check_rseq_disabled ();
      79  
      80    puts ("info: checking new thread");
      81    xpthread_join (xpthread_create (NULL, thread_func, NULL));
      82  
      83    puts ("info: checking subprocess");
      84    support_isolate_in_subprocess (proc_func, NULL);
      85  
      86    return 0;
      87  }
      88  #else /* !RSEQ_SIG */
      89  static int
      90  do_test (void)
      91  {
      92    FAIL_UNSUPPORTED ("glibc does not define RSEQ_SIG, skipping test");
      93  }
      94  #endif
      95  
      96  #include <support/test-driver.c>