gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/fpsig_nested.cc (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // This program verifies that application floating point state is restored
    16  // correctly after a signal handler returns. It also verifies that this works
    17  // with nested signals.
    18  #include <sys/time.h>
    19  
    20  #include "gtest/gtest.h"
    21  #include "test/util/test_util.h"
    22  #include "test/util/thread_util.h"
    23  
    24  namespace gvisor {
    25  namespace testing {
    26  
    27  namespace {
    28  
    29  #ifdef __x86_64__
    30  #define GET_XMM(__var, __xmm) \
    31    asm volatile("movq %%" #__xmm ", %0" : "=r"(__var))
    32  #define SET_XMM(__var, __xmm) asm volatile("movq %0, %%" #__xmm : : "r"(__var))
    33  #define GET_FP0(__var) GET_XMM(__var, xmm0)
    34  #define SET_FP0(__var) SET_XMM(__var, xmm0)
    35  #elif __aarch64__
    36  #define __stringify_1(x...) #x
    37  #define __stringify(x...) __stringify_1(x)
    38  #define GET_FPREG(var, regname) \
    39    asm volatile("str " __stringify(regname) ", %0" : "=m"(var))
    40  #define SET_FPREG(var, regname) \
    41    asm volatile("ldr " __stringify(regname) ", %0" : "=m"(var))
    42  #define GET_FP0(var) GET_FPREG(var, d0)
    43  #define SET_FP0(var) SET_FPREG(var, d0)
    44  #endif
    45  
    46  int pid;
    47  int tid;
    48  
    49  volatile uint64_t entryxmm[2] = {~0UL, ~0UL};
    50  volatile uint64_t exitxmm[2];
    51  
    52  void sigusr2(int s, siginfo_t* siginfo, void* _uc) {
    53    uint64_t val = SIGUSR2;
    54  
    55    // Record the value of %xmm0 on entry and then clobber it.
    56    GET_FP0(entryxmm[1]);
    57    SET_FP0(val);
    58    GET_FP0(exitxmm[1]);
    59  }
    60  
    61  void sigusr1(int s, siginfo_t* siginfo, void* _uc) {
    62    uint64_t val = SIGUSR1;
    63    int64_t ret;
    64  
    65    // Record the value of %xmm0 on entry and then clobber it.
    66    GET_FP0(entryxmm[0]);
    67    SET_FP0(val);
    68  
    69    // Send a SIGUSR2 to ourself. The signal mask is configured such that
    70    // the SIGUSR2 handler will run before this handler returns.
    71  #ifdef __x86_64__
    72    asm volatile("syscall;"
    73                 : "=a"(ret)
    74                 : "a"(__NR_tgkill), "D"(pid), "S"(tid), "d"(SIGUSR2)
    75                 :  // Clobbered by syscall.
    76                 "rcx", "r11");
    77  #elif __aarch64__
    78    register uint64_t x8 __asm__("x8") = __NR_tgkill;
    79    register uint64_t x0 __asm__("x0") = pid;
    80    register uint64_t x1 __asm__("x1") = tid;
    81    register uint64_t x2 __asm__("x2") = SIGUSR2;
    82    asm volatile("svc #0\n" : "=r"(x0) : "r"(x0), "r"(x1), "r"(x2), "r"(x8) :);
    83    ret = x0;
    84  #endif
    85    EXPECT_EQ(ret, 0);
    86  
    87    // Record value of %xmm0 again to verify that the nested signal handler
    88    // does not clobber it.
    89    GET_FP0(exitxmm[0]);
    90  }
    91  
    92  TEST(FPSigTest, NestedSignals) {
    93    pid = getpid();
    94    tid = gettid();
    95  
    96    struct sigaction sa = {};
    97    sigemptyset(&sa.sa_mask);
    98    sa.sa_flags = SA_SIGINFO;
    99    sa.sa_sigaction = sigusr1;
   100    ASSERT_THAT(sigaction(SIGUSR1, &sa, nullptr), SyscallSucceeds());
   101  
   102    sa.sa_sigaction = sigusr2;
   103    ASSERT_THAT(sigaction(SIGUSR2, &sa, nullptr), SyscallSucceeds());
   104  
   105    // The amd64 ABI specifies that the XMM register set is caller-saved. This
   106    // implies that if there is any function call between SET_XMM and GET_XMM the
   107    // compiler might save/restore xmm0 implicitly. This defeats the entire
   108    // purpose of the test which is to verify that fpstate is restored by
   109    // sigreturn(2).
   110    //
   111    // This is the reason why 'tgkill(getpid(), gettid(), SIGUSR1)' is implemented
   112    // in inline assembly below.
   113    //
   114    // If the OS is broken and registers are clobbered by the signal, using tgkill
   115    // to signal the current thread ensures that this is the clobbered thread.
   116  
   117    uint64_t expected = 0xdeadbeeffacefeed;
   118    SET_FP0(expected);
   119  
   120    int64_t ret;
   121  #ifdef __x86_64__
   122    asm volatile("syscall;"
   123                 : "=a"(ret)
   124                 : "a"(__NR_tgkill), "D"(pid), "S"(tid), "d"(SIGUSR1)
   125                 :  // Clobbered by syscall.
   126                 "rcx", "r11");
   127  #elif __aarch64__
   128    register uint64_t x8 __asm__("x8") = __NR_tgkill;
   129    register uint64_t x0 __asm__("x0") = pid;
   130    register uint64_t x1 __asm__("x1") = tid;
   131    register uint64_t x2 __asm__("x2") = SIGUSR1;
   132    asm volatile("svc #0\n" : "=r"(x0) : "r"(x0), "r"(x1), "r"(x2), "r"(x8) :);
   133    ret = x0;
   134  #endif
   135    EXPECT_EQ(ret, 0);
   136  
   137    uint64_t got;
   138    GET_FP0(got);
   139  
   140    //
   141    // The checks below verifies the following:
   142    // - signal handlers must called with a clean fpu state.
   143    // - sigreturn(2) must restore fpstate of the interrupted context.
   144    //
   145    EXPECT_EQ(expected, got);
   146  #ifdef __x86_64__
   147    EXPECT_EQ(entryxmm[0], 0);
   148    EXPECT_EQ(entryxmm[1], 0);
   149  #endif
   150    EXPECT_EQ(exitxmm[0], SIGUSR1);
   151    EXPECT_EQ(exitxmm[1], SIGUSR2);
   152  }
   153  
   154  }  // namespace
   155  
   156  }  // namespace testing
   157  }  // namespace gvisor