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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
/* See LICENSE file for copyright and license details. */
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunsafe-buffer-usage" /* completely broken */
# pragma clang diagnostic ignored "-Wpadded" /* don't care */
# pragma clang diagnostic ignored "-Wdisabled-macro-expansion" /* glibc issue */
# pragma clang diagnostic ignored "-Wc11-extensions" /* glibc issue */
# pragma clang diagnostic ignored "-Wpre-c11-compat" /* glibc issue */
# pragma clang diagnostic ignored "-Wunknown-warning-option" /* ignoring -Wsuggest-attribute=const|pure */
# pragma clang diagnostic ignored "-Wimplicit-void-ptr-cast" /* C++ warning, and we are in internal files */
# pragma clang diagnostic ignored "-Wc++-keyword" /* C++ warning, and we are in internal files */
# pragma clang diagnostic ignored "-Wc++-unterminated-string-initialization" /* Stupid C++ warning, and we are in internal files */
# pragma clang diagnostic ignored "-Wcovered-switch-default" /* harmful warning */
#endif
#if defined(__GNUC__)
# pragma GCC diagnostic ignored "-Winline"
#endif
#include "librecrypt.h"
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#if defined(__GNUC__)
# define HIDDEN __attribute__((__visibility__("hidden")))
# define CONST __attribute__((__const__))
# define PURE __attribute__((__pure__))
#else
# define HIDDEN
# define CONST
# define PURE
#endif
#define NONSTRING
#if defined(__GNUC__)
# if __GNUC__ >= 8 || defined(__clang__)
# undef NONSTRING
# define NONSTRING __attribute__((__nonstring__))
# endif
#endif
#if defined(POSIX_CLOSE_RESTART) && !defined(__linux__)
# define close(fd) posix_close(fd, 0)
#endif
/**
* Used for literal commas in macro calls
*/
#define COMMA ,
/**
* Get the number of elements in an array
*
* @param ARRAY:nondecayed-array The array
* @return :size_t The number of elements in `ARRAY`
*/
#define ELEMSOF(ARRAY) (sizeof(ARRAY) / sizeof(*(ARRAY)))
/**
* Select the minimum of two values
*/
#define MIN(A, B) ((A) < (B) ? (A) : (B))
/**
* Select the maximum of two values
*/
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#include "algorithms.h"
/**
* Which function `librecrypt_hash_` shall behave as
*/
enum action {
/**
* Used for `librecrypt_hash_binary`
*
* Output is binary and hash result-only
*/
BINARY_HASH,
/**
* Used for `librecrypt_hash`
*
* Output is ASCII and hash result-only
*/
ASCII_HASH,
/**
* Used for `librecrypt_hash`
*
* Output is ASCII and contains algorithm configurations
*/
ASCII_CRYPT
};
/**
* Pepper for a hash algorithm
*/
struct pepper {
/**
* The binary pepper
*/
const void *data;
/**
* The number of bytes in `.data`
*/
size_t len;
};
/**
* Structure used for doing truncated text concatenation
*
* This structure is laid out so that `{buf, size, 0u}` will
* properly initialise the state to use a buffer and have
* it's size properly set, and have the resulting length
* initialised to zero. This will not change.
*/
struct concat_state {
/**
* Where the function shall write to
*
* Update to an offset of it by the called function.
* Shall be set to the beginning of the output buffer
* before the first function call.
*/
char *buf;
/**
* The (remaining) size of the output buffer
*
* This is decremented by the called function
* Shall be set to the size of the output buffer
* before the first function call.
*/
size_t size;
/**
* The length the constructed text would
* have had if it wasn't truncated
*
* This length does not include NUL termination
* which is added by every function
*
* This is increased by the called function.
* Should be set to 0 before the first function call.
*
* After the last concatenation call, this field
* is read to get the final size before truncation
*/
size_t len;
};
/**
* The real type of `LIBRECRYPT_CONTEXT`
*/
struct librecrypt_context {
/**
* Application-defined data
*/
void *user_data;
/**
* Application-provided hash function implementations
*/
const struct librecrypt_algorithm *algos;
/**
* The number of elements in `.algos`
*/
size_t nalgos;
/**
* Per hash algorithm peppers
*/
struct pepper peppers[LIBRECRYPT_HASH_ALGORITHM_END];
/* TODO we probably don't want slots allocated for
* algorithms that have been disabled */
};
/**
* Check if an `struct algorithm *` is the `END_OF_ALGORITHMS`
* at the end of `librecrypt_algorithms_`
*/
#define IS_END_OF_ALGORITHMS(A) (!(A)->is_algorithm)
/**
* Used at the end of `librecrypt_algorithms_`
*/
#define END_OF_ALGORITHMS\
{\
.is_algorithm = NULL,\
.hash = NULL,\
.test_supported = NULL,\
.make_settings = NULL,\
.encoding_lut = NULL,\
.decoding_lut = NULL,\
.hash_size = 0u,\
.flexible_hash_size = 0,\
.strict_pad = 0,\
.pad = '\0'\
}
/**
* Create a concatenation of `ALPHABET` repeated
* for times; used to convert a base-64 alphabet
* to an encoding lookup table
*
* @param ALPHABET:string-literal A 64-character string
* @return :string-literal A 256-character string
*
* @seealso NONSTRING
*/
#define MAKE_ENCODING_LUT(ALPHABET) ALPHABET ALPHABET ALPHABET ALPHABET
/**
* Use in algorithms's base-64 decoding lookup tables
* in entries matching the pad character and invalid
* characters
*/
#define XX 0xFFu
/**
* The list of all supported algorithms (those not disabled
* at compile-time)
*
* They are ordered by how preferable they are as the
* default algorithm, with the most preferable first
*
* The list is terminated by `END_OF_ALGORITHMS`,
* which can be checked using `IS_END_OF_ALGORITHMS`
*/
extern struct librecrypt_algorithm librecrypt_algorithms_[];
/**
* This just points to memset(3), but the pointer is volalite
* so that the compiler cannot assume that, and therefore the
* call cannot be optimised away
*/
extern void *(*volatile librecrypt_explicit_memset_____)(void *, int, size_t); /* librecrypt_wipe.c */
/**
* The is a pointer to a function that doesn't do anything,
* however the pointer is volatile so that the function
* cannot assume that and cannot optimise away the calculating
* of the input value
*/
extern void (*volatile librecrypt_explicit_____)(unsigned char); /* librecrypt_equal_binary.c */
/**
* This function implements `librecrypt_hash_binary`, `librecrypt_hash`,
* and `librecrypt_test_supported`, depending on the value of `action`,
* see documentation of those functions for more default information
*
* @param out_buffer Output buffer for the hash result
* @param size The number bytes the function may write to `out_buffer`
* @param phrase The password to hash, may contain NUL bytes
* @param len The number of bytes in `phrase`
* @param settings The password hash configuration string,
* may contain resulting hash, which will be ignored
* @param ctx Library configuration
* @param action The function this function shall implement
* @return The number of bytes that would have been written to `out_buffer`
* if `size` was sufficiently large, excluding a terminating
* NUL byte (not present if `action == BINARY_HASH`);
* -1 on failure
*
* This function is MT-Safe but AS-Unsafe
*/
LIBRECRYPT_WRITE_MEM__(1, 2) LIBRECRYPT_READ_MEM__(3, 4)
LIBRECRYPT_READ_STR__(6) LIBRECRYPT_NONNULL_I__(5) LIBRECRYPT_WUR__ HIDDEN
ssize_t librecrypt_hash_(char *restrict out_buffer, size_t size, const char *phrase, size_t len,
const char *settings, LIBRECRYPT_CONTEXT *ctx, enum action action);
/**
* Default random number generator, used if no other
* is specified
*
* Generates up to `n`, or {SSIZE_MAX} if `n` is
* greater than {SSIZE_MAX}, random bytes
*
* @param out Output buffer for the random bytes
* @param n The maximum number of bytes to generate
* @param user Not used
* @return The number of generated bytes;
* will always be positive
*
* This function cannot fail, however it will ignore
* any `EINTR`; if `EINTR` is encountered `errno` will
* be set to `EINTR` upon return, otherwise `errno`
* will remain unmodifed
*
* This function is MT-Safe but AS-Unsafe
*/
LIBRECRYPT_WRITE_MEM__(1, 2) LIBRECRYPT_WUR__ HIDDEN
ssize_t librecrypt_rng_(void *out, size_t n, void *user);
/**
* Generate a specific number of random bytes
*
* @param out Output buffer for the random bytes
* @param n The number of bytes to generate
* @param rng The random number generator to use,
* `librecrypt_rng_` will be used if `NULL`,
* see `librecrypt_rng_` details, but not
* that it may return -1 on failure and has
* no restrictions on modifying `errno`
* @param user Passed as is to `*rng` as its third argument,
* so that it can be used for user-defined
* purposes
* @return 0 on success, -1 on failure
*
* When `rng` is non-`NULL`, this function inherits any
* MT-Unsafe and AS-Unsafe properties from `*rng`, being
* is MT-Safe and AS-Safe as a baseline; however when
* `rng` is `NULL`, this function is MT-Safe but AS-Unsafe
*/
LIBRECRYPT_WRITE_MEM__(1, 2) LIBRECRYPT_WUR__ HIDDEN
int librecrypt_fill_with_random_(void *out, size_t n, ssize_t (*rng)(void *out, size_t n, void *user), void *user);
/**
* Find the first algorithm specified by a password has string
*
* @param settings The password has string
* @param len The number of bytes in `settings`
* @param ctx Library configuration
* @return Pointer to the algorithm information,
* `NULL` if not found
*
* This function does not modify `errno`, but the
* caller should usually set `errno` to `ENOSYS`
* if this function returns `NULL`
*
* This function is MT-Safe And AS-Safe
*/
LIBRECRYPT_READ_MEM__(1, 2) LIBRECRYPT_NONNULL_1__ LIBRECRYPT_WUR__ HIDDEN
const struct librecrypt_algorithm *librecrypt_find_first_algorithm_(const char *settings, size_t len,
LIBRECRYPT_CONTEXT *ctx);
/**
* Sets the pepper for a hash algorithm
*
* @param ctx The library configuration object
* @param algo The hash algorithm to apply the pepper to
* @param len Pepper size to test support for, or 0 to not
* test (0 is always supported for algorithms that
* support pepper as it means no pepper)
* @return Pointer to the pepper configuration for
* `algo` in `ctx`; `NULL` on failure
*
* @throws ENOSYS The hash algorithm `algo` is either not
* recognised or was disabled at compile-time
* @throws ENOSUP The hash algorithm `algo` does not support
* peppers; the application is instead adviced
* to, itself, append or prepend the pepper
* to the password
* @throws EINVAL The size of the pepper is unsupported
* for the hash algorithm `algo`
*/
LIBRECRYPT_NONNULL_1__ LIBRECRYPT_WUR__ HIDDEN
struct pepper *librecrypt_get_pepper_(LIBRECRYPT_CONTEXT *ctx, enum librecrypt_hash_algorithm algo, size_t len);
/**
* Create a gap in a buffer to write data to
*
* @param state The buffer to modify, see `struct concat_state` for more information
* @param len The desired gap size
* @return The number of bytes available to write to; will be at most `len`
*
* The function will compare `len` against the remaining
* buffer size and return how much of that is available
* for writing to. At the same time, buffer position and
* remainging size is update as if that much data was
* written, `state->len` in increased by `len`, and the
* buffer is NUL terminated. The caller must retrieve
* the current buffer position, `state->buf` _before_ calling
* this function, so that it knows where the write data to.
*
* Example:
* void copy(struct concat_state *state, const char *text, size_t len)
* {
* char *buf = state->buf;
* size_t n = librecrypt_concat_void_(state, len);
* if (n) memcpy(buf, text, n);
* }
*/
LIBRECRYPT_NONNULL_1__ LIBRECRYPT_WUR__ HIDDEN
inline size_t
librecrypt_concat_void_(struct concat_state *state, size_t len)
{
size_t n = state->size ? MIN(state->size - 1u, len) : 0u;
if (len > SIZE_MAX - state->len)
abort(); /* TODO not covered */
state->len += len;
if (n) {
state->buf = &state->buf[n];
state->size -= n;
}
if (state->size)
state->buf[0u] = '\0';
return n;
}
/**
* Concatenate two strings
*
* Truncation is done to ensure the resulting string
* does not overrun it's buffer but is still NUL
* terminated (assuming the buffer is not zero-sized)
*
* @param state The buffer to write to, see `struct concat_state` for more information
* @param text The text to copy to the buffer
* @param len The length of `text`, in bytes
*/
LIBRECRYPT_NONNULL_1__ LIBRECRYPT_READ_MEM__(2, 3) HIDDEN
inline void
librecrypt_concat_mem_(struct concat_state *state, const char *text, size_t len)
{
char *buf = state->buf;
size_t n = librecrypt_concat_void_(state, len);
if (n)
memcpy(buf, text, n);
}
/**
* Concatenate two strings
*
* Truncation is done to ensure the resulting string
* does not overrun it's buffer but is still NUL
* terminated (assuming the buffer is not zero-sized)
*
* @param state The buffer to write to, see `struct concat_state` for more information
* @param text The text to copy to the buffer
*/
LIBRECRYPT_NONNULL__ LIBRECRYPT_READ_STR__(2) HIDDEN
inline void
librecrypt_concat_str_(struct concat_state *state, const char *text)
{
librecrypt_concat_mem_(state, text, strlen(text));
}
/**
* Concatenate an unsigned integer onto a string
*
* The integer will be formated in decimal without
* redundant noughts
*
* Truncation is done to ensure the resulting string
* does not overrun it's buffer but is still NUL
* terminated (assuming the buffer is not zero-sized)
*
* @param state The buffer to write to, see `struct concat_state` for more information
* @param value The integer to write to the buffer
*/
LIBRECRYPT_NONNULL_1__ HIDDEN
void librecrypt_concat_uint_(struct concat_state *state, uintmax_t value);
/**
* Concatenate a single chracter onto a string
*
* NB! NUL termination will not be added
*
* Truncation is done to ensure the resulting string
* does not overrun it's buffer but is still has room
* for NUL terminnation (assuming the buffer is not
* zero-sized)
*
* @param state The buffer to write to, see `struct concat_state` for more information
* @param c The character to append
*/
LIBRECRYPT_NONNULL_1__ HIDDEN
inline void
librecrypt_concat_char_no_nul_(struct concat_state *state, char c)
{
if (state->len == SIZE_MAX)
abort(); /* TODO not covered */
state->len += 1u;
if (state->size > 1u) {
*state->buf++ = c;
state->size -= 1u;
}
}
/**
* Adjust a `struct concat_state` has been modified by
* another function
*
* This function will not by it self modify the buffer
* in any way. It will not add NUL-termination. This
* function will only update `*state` to reflect that
* some function wanted to write `len` bytes (but may
* have written less due to the buffer being to small).
*
* @param state The buffer to write to, see `struct concat_state` for more information
* @param len the length of the concatenated data sans NUL-termination
* @return Normally 0, -1 if `state->len` would overflow
*
* The proper error code when this function returns -1 is
* usually EOVERFLOW, however this function does not modify errno
*
* It is unspecified whether `state->buf` and `state->size` will
* be updated when -1 is returned. It is also unspecified whether
* `state->len` will be updated when -1 is returned.
*
* Precondition: `state->buf != NULL`
*/
LIBRECRYPT_NONNULL_1__ LIBRECRYPT_WUR__ HIDDEN
inline int
librecrypt_post_concat_adjust_(struct concat_state *state, size_t len)
{
size_t n = state->size ? MIN(state->size - 1u, len) : 0u;
state->buf = &state->buf[n];
state->size -= n;
if (state->len > SIZE_MAX - len)
return -1;
state->len += len;
return 0;
}
/**
* Get the base64-encoding length for some size of data
*
* @param raw_len The size of the data
* @param padded Whether the base64-encoding shall be padded to a multiple of 4 bytes
* @param ascii_len_out Output parameter for the length of the base64-encoding
* @return Normally 1, 0 if the value for `*ascii_len_out` could not be represented
*
* The proper error code when this function returns 0 is
* usually EOVERFLOW, however this function does not modify errno
*/
LIBRECRYPT_WUR__ LIBRECRYPT_NONNULL__ HIDDEN
inline int
librecrypt_raw_len_to_base64_len_(size_t raw_len, int padded, size_t *ascii_len_out)
{
size_t q = raw_len / 3u;
size_t r = raw_len % 3u;
if (r) {
if (padded)
r = 4u; /* padding to for bytes */
else
r += 1u; /* 3n+m bytes: 4n+m+1 chars, unless m=0 */
}
if (q > (SIZE_MAX - r) / 4u)
return 0;
*ascii_len_out = q * 4u + r;
return 1;
}
/**
* Get the size of some data based on the length of its base64-encoding
*
* @param ascii_len The length of the data's unpadded base64-encoding
* @param raw_len_out Output parameter for the size of the data
* @return Normally 1, 0 if `ascii_len` doesn't have proper
* for a base64-encoding
*
* The proper error code when this function returns 0 is
* usually EINVAL, however this function does not modify errno
*/
LIBRECRYPT_WUR__ LIBRECRYPT_NONNULL__ HIDDEN
inline int
librecrypt_base64_len_to_raw_len_(size_t ascii_len, size_t *raw_len_out)
{
size_t q = ascii_len / 4u;
size_t r = ascii_len % 4u;
if (r == 1u)
return 0;
*raw_len_out = q * 3u;
if (r)
*raw_len_out += r - 1u;
return 1;
}
/**
* Check whether a base64-encoding is padded to a multiple
* of 4 characters, without being excessively padded
*
* @param unpadded_len The length of the base64-encoding sans it's padding
* @param padded_len The length of the base64-encoding including any present padding
* @return Whether the base64-encoding is appropriately padded
*
* Precondition: `padded_len >= unpadded_len`
* Precondition: librecrypt_base64_len_to_raw_len_(unpadded_len, _) != 0 (i.e. `unpadded_len % 4u != 1u`)
*/
LIBRECRYPT_WUR__ CONST HIDDEN
inline int
librecrypt_is_base64_properly_padded_(size_t unpadded_len, size_t padded_len)
{
if (padded_len % 4u)
return 0;
if (padded_len - unpadded_len >= 4u)
return 0;
return 1;
}
#ifdef TEST
# include "libtest/libtest.h"
# ifdef __linux__
# include <sys/prctl.h>
# endif
# include <sys/resource.h>
# include <sys/types.h>
# include <sys/uio.h>
# include <sys/wait.h>
# include <setjmp.h>
# include <signal.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# define SET_UP_ALARM()\
do {\
unsigned int alarm_time__ = alarm(10u);\
if (alarm_time__ > 10u)\
alarm(alarm_time__);\
} while (0)
# if defined(PR_SET_DUMPABLE)
# define INIT_TEST_ABORT()\
do {\
struct rlimit rl__;\
rl__.rlim_cur = 0;\
rl__.rlim_max = 0;\
(void) setrlimit(RLIMIT_CORE, &rl__);\
(void) prctl(PR_SET_DUMPABLE, 0);\
EXPECT_ABORT(abort());\
} while (0)
# else
# define INIT_TEST_ABORT()\
do {\
struct rlimit rl__;\
rl__.rlim_cur = 0;\
rl__.rlim_max = 0;\
(void) setrlimit(RLIMIT_CORE, &rl__);\
EXPECT_ABORT(abort());\
} while (0)
# endif
# define INIT_RESOURCE_TEST()\
do {\
libtest_start_tracking();\
libtest_force_zero_on_alloc(1);\
libtest_expect_zeroed_on_free(1);\
} while (0)
# define STOP_RESOURCE_TEST()\
do {\
libtest_stop_tracking();\
libtest_force_zero_on_alloc(0);\
libtest_expect_zeroed_on_free(0);\
EXPECT(libtest_check_no_leaks());\
} while (0)
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
# include <stdatomic.h>
# define MEMFENCE() atomic_thread_fence(memory_order_seq_cst)
#elif defined(_MSC_VER)
# include <intrin.h>
# define MEMFENCE() _ReadWriteBarrier()
#else
# define MEMFENCE() __asm__ volatile("" ::: "memory")
#endif
# define EXPECT__(EXPR, HOW, RETEXTRACT, RETEXPECT)\
do {\
pid_t pid__;\
int status__;\
pid__ = fork();\
EXPECT(pid__ != -1);\
if (pid__ == 0) {\
(EXPR);\
_exit(0);\
}\
EXPECT(waitpid(pid__, &status__, 0) == pid__);\
EXPECT(HOW(status__));\
EXPECT(RETEXTRACT(status__) == RETEXPECT);\
} while (0)
# define EXPECT_ABORT(EXPR)\
do {\
EXPECT__(EXPR, WIFSIGNALED, WTERMSIG, SIGABRT);\
} while (0)
# define EXPECT(EXPR)\
do {\
MEMFENCE();\
if (!(EXPR)) {\
int test_expect_saved_errno__ = errno;\
libtest_expect_zeroed_on_free(0);\
libtest_stop_tracking();\
fprintf(stderr, "Failure at %s:%i: %s (errno = %i)\n",\
__FILE__, __LINE__, #EXPR, test_expect_saved_errno__);\
libtest_dump_stack(NULL, "\t");\
exit(1);\
}\
} while (0)
# define assert(EXPR)\
do {\
MEMFENCE();\
if (!(EXPR)) {\
libtest_expect_zeroed_on_free(0);\
libtest_stop_tracking();\
fprintf(stderr, "Assertion failure at %s:%i: %s\n",\
__FILE__, __LINE__, #EXPR);\
libtest_dump_stack(NULL, "\t");\
exit(2);\
}\
} while (0)
# define CANARY_FILL(BUF) CANARY_C_FILL(99, BUF)
# define CANARY_CHECK(BUF, OFF) CANARY_C_CHECK(99, BUF, OFF)
# define CANARY_X_CHECK(BUF, OFF1, OFF2) CANARY_XC_CHECK(99, BUF, OFF1, OFF2)
# define CANARY_C_FILL(C, BUF)\
memset((BUF), (C), sizeof(BUF))
# define CANARY_C_CHECK(C, BUF, OFF)\
do {\
size_t canary_i__;\
for (canary_i__ = (OFF); canary_i__ < sizeof(BUF); canary_i__++)\
EXPECT(((unsigned char *)(BUF))[canary_i__] == (unsigned char)(C));\
} while (0)
# define CANARY_XC_CHECK(C, BUF, OFF1, OFF2)\
CANARY_XCC_CHECK(0, C, BUF, OFF1, OFF2)
# define CANARY_XCC_CHECK(C1, C2, BUF, OFF1, OFF2)\
do {\
if ((OFF2) > (OFF1))\
CANARY_C_CHECK((C2), (BUF), (OFF2));\
memset(&(BUF)[(OFF1)], (C1), sizeof(BUF) - (OFF1));\
CANARY_C_CHECK((C1), (BUF), (OFF1));\
} while (0)
#endif
|