fix: check if libc rseq support is registered
[librseq.git] / src / rseq.c
CommitLineData
744d0b8b 1// SPDX-License-Identifier: LGPL-2.1-only
784b0012
MD
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 */
2cbca301 17#ifndef _GNU_SOURCE
784b0012 18#define _GNU_SOURCE
2cbca301 19#endif
784b0012
MD
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>
0ceae74a 29#include <limits.h>
9698c399 30#include <dlfcn.h>
170f840b 31#include <stddef.h>
784b0012
MD
32
33#include <rseq/rseq.h>
34
170f840b 35static const ptrdiff_t *libc_rseq_offset_p;
9698c399
MD
36static const unsigned int *libc_rseq_size_p;
37static const unsigned int *libc_rseq_flags_p;
38
39/* Offset from the thread pointer to the rseq area. */
170f840b 40ptrdiff_t rseq_offset;
9698c399
MD
41
42/* Size of the registered rseq area. 0 if the registration was
43 unsuccessful. */
44unsigned int rseq_size = -1U;
45
46/* Flags used during rseq registration. */
47unsigned int rseq_flags;
48
49static int rseq_ownership;
50
51static
2d533093
MD
52__thread struct rseq_abi __rseq_abi __attribute__((tls_model("initial-exec"))) = {
53 .cpu_id = RSEQ_ABI_CPU_ID_UNINITIALIZED,
784b0012
MD
54};
55
2d533093 56static int sys_rseq(struct rseq_abi *rseq_abi, uint32_t rseq_len,
52e82b87
MD
57 int flags, uint32_t sig)
58{
59 return syscall(__NR_rseq, rseq_abi, rseq_len, flags, sig);
60}
61
62int rseq_available(void)
63{
64 int rc;
65
a5fd0584 66 rc = sys_rseq(NULL, 0, 0, 0);
52e82b87
MD
67 if (rc != -1)
68 abort();
69 switch (errno) {
70 case ENOSYS:
71 return 0;
72 case EINVAL:
73 return 1;
74 default:
75 abort();
76 }
77}
78
9698c399 79int rseq_register_current_thread(void)
784b0012 80{
9698c399 81 int rc;
784b0012 82
9698c399
MD
83 if (!rseq_ownership) {
84 /* Treat libc's ownership as a successful registration. */
85 return 0;
86 }
2d533093 87 rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), 0, RSEQ_SIG);
9698c399
MD
88 if (rc)
89 return -1;
90 assert(rseq_current_cpu_raw() >= 0);
91 return 0;
784b0012
MD
92}
93
9698c399 94int rseq_unregister_current_thread(void)
784b0012 95{
9698c399 96 int rc;
784b0012 97
9698c399
MD
98 if (!rseq_ownership) {
99 /* Treat libc's ownership as a successful unregistration. */
100 return 0;
101 }
2d533093 102 rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), RSEQ_ABI_FLAG_UNREGISTER, RSEQ_SIG);
9698c399
MD
103 if (rc)
104 return -1;
105 return 0;
784b0012
MD
106}
107
9698c399
MD
108static __attribute__((constructor))
109void rseq_init(void)
784b0012 110{
9698c399
MD
111 libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
112 libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
113 libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");
ad538a80
MJ
114 if (libc_rseq_size_p && libc_rseq_offset_p && libc_rseq_flags_p &&
115 *libc_rseq_size_p != 0) {
9698c399
MD
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;
0ceae74a 121 }
9698c399
MD
122 if (!rseq_available())
123 return;
124 rseq_ownership = 1;
125 rseq_offset = (void *)&__rseq_abi - rseq_thread_pointer();
2d533093 126 rseq_size = sizeof(struct rseq_abi);
9698c399 127 rseq_flags = 0;
784b0012
MD
128}
129
9698c399
MD
130static __attribute__((destructor))
131void rseq_exit(void)
784b0012 132{
9698c399
MD
133 if (!rseq_ownership)
134 return;
135 rseq_offset = 0;
136 rseq_size = -1U;
137 rseq_ownership = 0;
784b0012
MD
138}
139
140int32_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.05479 seconds and 4 git commands to generate.