xref: /aosp_15_r20/external/llvm-libc/src/signal/linux/sigaltstack.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Linux implementation of sigaltstack -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/signal/sigaltstack.h"
10 #include "hdr/types/stack_t.h"
11 #include "src/__support/macros/config.h"
12 #include "src/errno/libc_errno.h"
13 #include "src/signal/linux/signal_utils.h"
14 
15 #include "src/__support/common.h"
16 
17 #include <sys/syscall.h>
18 
19 namespace LIBC_NAMESPACE_DECL {
20 
21 LLVM_LIBC_FUNCTION(int, sigaltstack,
22                    (const stack_t *__restrict ss, stack_t *__restrict oss)) {
23   if (ss != nullptr) {
24     unsigned not_ss_disable = ~unsigned(SS_DISABLE);
25     if ((unsigned(ss->ss_flags) & not_ss_disable) != 0) {
26       // Flags cannot have anything other than SS_DISABLE set.
27       // We do the type-casting to unsigned because the |ss_flags|
28       // field of stack_t is of type "int".
29       libc_errno = EINVAL;
30       return -1;
31     }
32     if (ss->ss_size < MINSIGSTKSZ) {
33       libc_errno = ENOMEM;
34       return -1;
35     }
36   }
37 
38   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_sigaltstack, ss, oss);
39   if (ret < 0) {
40     libc_errno = -ret;
41     return -1;
42   }
43   return 0;
44 }
45 
46 } // namespace LIBC_NAMESPACE_DECL
47