blob: 82a564d53378f7a049debfb3fcf9a7ab992591d3 [file] [log] [blame]
Josh Gaof5693c62018-08-31 14:07:40 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#pragma once
30
31#include <sys/cdefs.h>
32#include <sys/syscall.h>
33#include <sys/types.h>
34#include <unistd.h>
35
36// An inline version of pthread_sigqueue(pthread_self(), ...), to reduce the number of
37// uninteresting stack frames at the top of a crash.
38static inline __always_inline void inline_raise(int sig, void* value = nullptr) {
39 // Protect ourselves against stale cached PID/TID values by fetching them via syscall.
40 // http://b/37769298
41 pid_t pid = syscall(__NR_getpid);
42 pid_t tid = syscall(__NR_gettid);
43 siginfo_t info = {};
44 info.si_code = SI_QUEUE;
45 info.si_pid = pid;
46 info.si_uid = getuid();
47 info.si_value.sival_ptr = value;
48
49#if defined(__arm__)
50 register long r0 __asm__("r0") = pid;
51 register long r1 __asm__("r1") = tid;
52 register long r2 __asm__("r2") = sig;
53 register long r3 __asm__("r3") = reinterpret_cast<long>(&info);
54 register long r7 __asm__("r7") = __NR_rt_tgsigqueueinfo;
55 __asm__("swi #0" : "=r"(r0) : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r7) : "memory");
56#elif defined(__aarch64__)
57 register long x0 __asm__("x0") = pid;
58 register long x1 __asm__("x1") = tid;
59 register long x2 __asm__("x2") = sig;
60 register long x3 __asm__("x3") = reinterpret_cast<long>(&info);
61 register long x8 __asm__("x8") = __NR_rt_tgsigqueueinfo;
62 __asm__("svc #0" : "=r"(x0) : "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x8) : "memory");
Elliott Hughes0e5b89c2023-08-02 16:04:25 -070063#elif defined(__riscv)
64 register long a0 __asm__("a0") = pid;
65 register long a1 __asm__("a1") = tid;
66 register long a2 __asm__("a2") = sig;
67 register long a3 __asm__("a3") = reinterpret_cast<long>(&info);
68 register long a7 __asm__("a7") = __NR_rt_tgsigqueueinfo;
69 __asm__("ecall" : "=r"(a0) : "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(a7) : "memory");
Elliott Hughes3abde062021-02-18 15:12:41 -080070#elif defined(__x86_64__)
71 register long rax __asm__("rax") = __NR_rt_tgsigqueueinfo;
72 register long rdi __asm__("rdi") = pid;
73 register long rsi __asm__("rsi") = tid;
74 register long rdx __asm__("rdx") = sig;
75 register long r10 __asm__("r10") = reinterpret_cast<long>(&info);
76 __asm__("syscall"
77 : "+r"(rax)
78 : "r"(rdi), "r"(rsi), "r"(rdx), "r"(r10)
79 : "memory", "cc", "r11", "rcx");
Josh Gaof5693c62018-08-31 14:07:40 -070080#else
Elliott Hughes3abde062021-02-18 15:12:41 -080081 // 32-bit x86 is a huge mess, so don't even bother...
Josh Gaof5693c62018-08-31 14:07:40 -070082 syscall(__NR_rt_tgsigqueueinfo, pid, tid, sig, &info);
83#endif
84}