1 /*
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.
38 static 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");
63 #else
64   syscall(__NR_rt_tgsigqueueinfo, pid, tid, sig, &info);
65 #endif
66 }
67 
68