1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
#ifndef __LIBCEPHFS_PROXY_HELPERS_H__
#define __LIBCEPHFS_PROXY_HELPERS_H__
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <stdint.h>
#include <stdbool.h>
#include "proxy_log.h"
#define __public __attribute__((__visibility__("default")))
#define ptr_value(_ptr) ((uint64_t)(uintptr_t)(_ptr))
#define value_ptr(_val) ((void *)(uintptr_t)(_val))
typedef struct _proxy_random {
uint64_t mask;
uint64_t factor;
uint64_t factor_inv;
uint64_t shift;
} proxy_random_t;
/* Generate a 64-bits random number different than 0. */
static inline uint64_t random_u64(void)
{
uint64_t value;
int32_t i;
do {
value = 0;
for (i = 0; i < 4; i++) {
value <<= 16;
value ^= (random() >> 8) & 0xffff;
}
} while (value == 0);
return value;
}
/* Randomly initialize the data used to scramble pointers. */
static inline void random_init(proxy_random_t *rnd)
{
uint64_t inv;
rnd->mask = random_u64();
/* Generate an odd multiplicative factor different than 1. */
do {
rnd->factor = random_u64() | 1;
} while (rnd->factor == 1);
/* Compute the inverse of 'factor' modulo 2^64. */
inv = rnd->factor & 0x3;
inv *= 0x000000012 - rnd->factor * inv;
inv *= 0x000000102 - rnd->factor * inv;
inv *= 0x000010002 - rnd->factor * inv;
inv *= 0x100000002 - rnd->factor * inv;
rnd->factor_inv = inv * (2 - rnd->factor * inv);
rnd->shift = random_u64();
}
/* Obfuscate a pointer. */
static inline uint64_t random_scramble(proxy_random_t *rnd, uint64_t value)
{
uint32_t bits;
bits = __builtin_popcountll(value);
/* rnd->shift is rotated by the amount of bits set to 1 in the original
* value, and the lowest 6 bits are extracted. This generates a
* pseudo-random number that depends on the number of bits of the
* value. */
bits = ((rnd->shift >> bits) | (rnd->shift << (64 - bits))) & 0x3f;
/* The value is rotated by the amount just computed. */
value = (value << bits) | (value >> (64 - bits));
/* The final result is masked with a random number. */
value ^= rnd->mask;
/* And multiplied by a random factor modulo 2^64. */
return value * rnd->factor;
}
/* Recover a pointer. */
static inline uint64_t random_unscramble(proxy_random_t *rnd, uint64_t value)
{
uint32_t bits;
/* Divide by the random factor (i.e. multiply by the inverse of the
* factor). */
value *= rnd->factor_inv;
/* Remove the mask. */
value ^= rnd->mask;
/* Get the number of bits the pointer was rotated. */
bits = __builtin_popcountll(value);
bits = ((rnd->shift >> bits) | (rnd->shift << (64 - bits))) & 0x3f;
/* Undo the rotation to recover the original value. */
return (value >> bits) | (value << (64 - bits));
}
static inline void *proxy_malloc(size_t size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) {
proxy_log(LOG_ERR, errno, "Failed to allocate memory");
}
return ptr;
}
static inline int32_t proxy_realloc(void **pptr, size_t size)
{
void *ptr;
ptr = realloc(*pptr, size);
if (ptr == NULL) {
return proxy_log(LOG_ERR, errno, "Failed to reallocate memory");
}
*pptr = ptr;
return 0;
}
static inline void proxy_free(void *ptr)
{
free(ptr);
}
static inline char *proxy_strdup(const char *str)
{
char *ptr;
ptr = strdup(str);
if (ptr == NULL) {
proxy_log(LOG_ERR, errno, "Failed to copy a string");
return NULL;
}
return ptr;
}
static inline int32_t proxy_mutex_init(pthread_mutex_t *mutex)
{
int32_t err;
err = pthread_mutex_init(mutex, NULL);
if (err != 0) {
return proxy_log(LOG_ERR, err, "Failed to initialize a mutex");
}
return 0;
}
static inline void proxy_mutex_lock(pthread_mutex_t *mutex)
{
int32_t err;
err = pthread_mutex_lock(mutex);
if (err != 0) {
proxy_abort(err, "Mutex cannot be acquired");
}
}
static inline void proxy_mutex_unlock(pthread_mutex_t *mutex)
{
int32_t err;
err = pthread_mutex_unlock(mutex);
if (err != 0) {
proxy_abort(err, "Mutex cannot be released");
}
}
static inline int32_t proxy_rwmutex_init(pthread_rwlock_t *mutex)
{
int32_t err;
err = pthread_rwlock_init(mutex, NULL);
if (err != 0) {
return proxy_log(LOG_ERR, err,
"Failed to initialize a rwmutex");
}
return 0;
}
static inline void proxy_rwmutex_rdlock(pthread_rwlock_t *mutex)
{
int32_t err;
err = pthread_rwlock_rdlock(mutex);
if (err != 0) {
proxy_abort(err, "RWMutex cannot be acquired for read");
}
}
static inline void proxy_rwmutex_wrlock(pthread_rwlock_t *mutex)
{
int32_t err;
err = pthread_rwlock_wrlock(mutex);
if (err != 0) {
proxy_abort(err, "RWMutex cannot be acquired for write");
}
}
static inline void proxy_rwmutex_unlock(pthread_rwlock_t *mutex)
{
int32_t err;
err = pthread_rwlock_unlock(mutex);
if (err != 0) {
proxy_abort(err, "RWMutex cannot be released");
}
}
static inline int32_t proxy_condition_init(pthread_cond_t *condition)
{
int32_t err;
err = pthread_cond_init(condition, NULL);
if (err != 0) {
return proxy_log(LOG_ERR, err,
"Failed to initialize a condition variable");
}
return 0;
}
static inline void proxy_condition_signal(pthread_cond_t *condition)
{
int32_t err;
err = pthread_cond_signal(condition);
if (err != 0) {
proxy_abort(err, "Condition variable cannot be signaled");
}
}
static inline void proxy_condition_wait(pthread_cond_t *condition,
pthread_mutex_t *mutex)
{
int32_t err;
err = pthread_cond_wait(condition, mutex);
if (err != 0) {
proxy_abort(err, "Condition variable cannot be waited");
}
}
static inline int32_t proxy_thread_create(pthread_t *tid,
void *(*start)(void *), void *arg)
{
int32_t err;
err = pthread_create(tid, NULL, start, arg);
if (err != 0) {
proxy_log(LOG_ERR, err, "Failed to create a thread");
}
return err;
}
static inline void proxy_thread_kill(pthread_t tid, int32_t signum)
{
int32_t err;
err = pthread_kill(tid, signum);
if (err != 0) {
proxy_abort(err, "Failed to send a signal to a thread");
}
}
static inline void proxy_thread_join(pthread_t tid)
{
int32_t err;
err = pthread_join(tid, NULL);
if (err != 0) {
proxy_log(LOG_ERR, err, "Unable to join a thread");
}
}
static inline int32_t proxy_signal_set(int32_t signum, struct sigaction *action,
struct sigaction *old)
{
if (sigaction(signum, action, old) < 0) {
return proxy_log(LOG_ERR, errno,
"Failed to configure a signal");
}
return 0;
}
int32_t proxy_hash(uint8_t *hash, size_t size,
int32_t (*feed)(void **, void *, int32_t), void *data);
int32_t proxy_hash_hex(char *digest, size_t size,
int32_t (*feed)(void **, void *, int32_t), void *data);
#endif
|