Update librseq to co-exist with glibc rseq integration
[librseq.git] / src / rseq.c
1 // SPDX-License-Identifier: LGPL-2.1-only
2 /*
3 * rseq.c
4 *
5 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 */
17 #ifndef _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20 #include <errno.h>
21 #include <sched.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <syscall.h>
27 #include <assert.h>
28 #include <signal.h>
29 #include <limits.h>
30 #include <dlfcn.h>
31
32 #include <rseq/rseq.h>
33
34 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
35
36 static const int *libc_rseq_offset_p;
37 static const unsigned int *libc_rseq_size_p;
38 static const unsigned int *libc_rseq_flags_p;
39
40 /* Offset from the thread pointer to the rseq area. */
41 int rseq_offset;
42
43 /* Size of the registered rseq area. 0 if the registration was
44 unsuccessful. */
45 unsigned int rseq_size = -1U;
46
47 /* Flags used during rseq registration. */
48 unsigned int rseq_flags;
49
50 static int rseq_ownership;
51
52 static
53 __thread struct rseq __rseq_abi __attribute__((tls_model("initial-exec"))) = {
54 .cpu_id = RSEQ_CPU_ID_UNINITIALIZED,
55 };
56
57 static int sys_rseq(struct rseq *rseq_abi, uint32_t rseq_len,
58 int flags, uint32_t sig)
59 {
60 return syscall(__NR_rseq, rseq_abi, rseq_len, flags, sig);
61 }
62
63 int rseq_available(void)
64 {
65 int rc;
66
67 rc = sys_rseq(NULL, 0, 0, 0);
68 if (rc != -1)
69 abort();
70 switch (errno) {
71 case ENOSYS:
72 return 0;
73 case EINVAL:
74 return 1;
75 default:
76 abort();
77 }
78 }
79
80 int rseq_register_current_thread(void)
81 {
82 int rc;
83
84 if (!rseq_ownership) {
85 /* Treat libc's ownership as a successful registration. */
86 return 0;
87 }
88 rc = sys_rseq(&__rseq_abi, sizeof(struct rseq), 0, RSEQ_SIG);
89 if (rc)
90 return -1;
91 assert(rseq_current_cpu_raw() >= 0);
92 return 0;
93 }
94
95 int rseq_unregister_current_thread(void)
96 {
97 int rc;
98
99 if (!rseq_ownership) {
100 /* Treat libc's ownership as a successful unregistration. */
101 return 0;
102 }
103 rc = sys_rseq(&__rseq_abi, sizeof(struct rseq), RSEQ_FLAG_UNREGISTER, RSEQ_SIG);
104 if (rc)
105 return -1;
106 return 0;
107 }
108
109 static __attribute__((constructor))
110 void rseq_init(void)
111 {
112 libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
113 libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
114 libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");
115 if (libc_rseq_size_p && libc_rseq_offset_p && libc_rseq_flags_p) {
116 /* rseq registration owned by glibc */
117 rseq_offset = *libc_rseq_offset_p;
118 rseq_size = *libc_rseq_size_p;
119 rseq_flags = *libc_rseq_flags_p;
120 return;
121 }
122 if (!rseq_available())
123 return;
124 rseq_ownership = 1;
125 rseq_offset = (void *)&__rseq_abi - rseq_thread_pointer();
126 rseq_size = sizeof(struct rseq);
127 rseq_flags = 0;
128 }
129
130 static __attribute__((destructor))
131 void rseq_exit(void)
132 {
133 if (!rseq_ownership)
134 return;
135 rseq_offset = 0;
136 rseq_size = -1U;
137 rseq_ownership = 0;
138 }
139
140 int32_t rseq_fallback_current_cpu(void)
141 {
142 int32_t cpu;
143
144 cpu = sched_getcpu();
145 if (cpu < 0) {
146 perror("sched_getcpu()");
147 abort();
148 }
149 return cpu;
150 }
This page took 0.032505 seconds and 4 git commands to generate.