src/wipe.c

filter: Statementstmt 100% 31 / 31 Branchbran 77.2% 17 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.5% 48 / 53 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/random.c ↑ Summary xs/abi.xs »
1 #include "symprim/config.h"
2  
3 /* memset_s is declared only when this is defined before <string.h>. */
4 #ifdef SYMPRIM_HAVE_MEMSET_S
5 #define __STDC_WANT_LIB_EXT1__ 1
6 #endif
7  
8 #include "symprim/symprim.h"
9 #include "symprim/wipe.h"
10  
11 #include <string.h>
12  
13 #if defined(_WIN32)
14 #include <windows.h>
15 #else
16 #ifdef SYMPRIM_HAVE_EXPLICIT_BZERO
17 #include <strings.h>
18 #endif
19 #ifdef SYMPRIM_HAVE_MLOCK
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24 #endif
25 #endif
26  
27 #if !defined(_WIN32) && !defined(SYMPRIM_HAVE_EXPLICIT_BZERO) \
28 && !defined(SYMPRIM_HAVE_MEMSET_S)
29 /* The compiler cannot see through a volatile pointer, so it cannot decide the
30 * store is dead and drop it. */
31 static void *(*const volatile wipe_memset)(void *, int, size_t) = memset;
32 #endif
33  
34 820101 void symprim_wipe(void *buf, size_t len)
35 {
36 /* The table looks for nothing given before this is reached, and no door
37 * hands it nothing.
38 # uncoverable condition left count:1 */
39 820101 ▶ if (!buf || !len)
Branch: 2 branches
BranchTrueFalseTotal
if (!buf || !len) 820101 0 820101
if (!buf || !len) 2 820099 820101
40 2 return;
41 #if defined(_WIN32)
42 SecureZeroMemory(buf, len);
43 #elif defined(SYMPRIM_HAVE_EXPLICIT_BZERO)
44 820099 explicit_bzero(buf, len);
45 #elif defined(SYMPRIM_HAVE_MEMSET_S)
46 memset_s(buf, len, 0, len);
47 #else
48 wipe_memset(buf, 0, len);
49 #endif
50 }
51  
52 1859 int symprim_equal(const void *a, const void *b, size_t len)
53 {
54 1859 const unsigned char *x = (const unsigned char *)a;
55 1859 const unsigned char *y = (const unsigned char *)b;
56 1859 unsigned int diff = 0;
57 size_t i;
58  
59 96531 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 94672 1859 96531
60 94672 diff |= (unsigned int)(x[i] ^ y[i]);
61 /* diff is 0..255: subtracting one borrows into bit 8 only for 0. */
62 1859 return (int)(((diff - 1U) >> 8) & 1U);
63 }
64  
65 #if !defined(_WIN32) && defined(SYMPRIM_HAVE_MLOCK)
66 /* Locks, or lets go, the whole pages that hold len bytes at buf. POSIX lets
67 * mlock refuse an address that is not the start of a page, and Haiku's
68 * does, so the system is handed the start of the first page and a whole
69 * number of them. */
70 24 static int on_whole_pages(void *buf, size_t len, int lock)
71 {
72 24 long asked = sysconf(_SC_PAGESIZE);
73 size_t page, before, span;
74 void *start;
75 int code;
76  
77 /* A system that has mlock says how long its pages are. Were one not to,
78 * a page of one byte hands on the bytes as they were given.
79 # uncoverable condition right */
80 24 ▶ page = asked > 0 ? (size_t)asked : 1;
Branch: 1 branch
BranchTrueFalseTotal
page = asked > 0 ? (size_t)asked : 1; 24 0 24
81 24 before = (size_t)((uintptr_t)buf % page);
82 /* More bytes than there are to the end of memory. */
83 24 ▶ if (len > SIZE_MAX - before - (page - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (len > SIZE_MAX - before - (page - 1)) 2 22 24
84 2 return SYMPRIM_E_NOLOCK;
85 22 start = (void *)((uintptr_t)buf - before);
86 22 span = (before + len + page - 1) / page * page;
87 22 ▶ if (lock)
Branch: 1 branch
BranchTrueFalseTotal
if (lock) 11 11 22
88 11 code = mlock(start, span);
89 else
90 11 code = munlock(start, span);
91 /* Whether the system will lock is the system's to say. Where the tests
92 * run it always has, and where it will not is not something a test can
93 * arrange without changing what the machine allows.
94 # uncoverable condition right class:ignore_covered_err */
95 22 ▶ return code == 0 ? SYMPRIM_OK : SYMPRIM_E_NOLOCK;
Branch: 1 branch
BranchTrueFalseTotal
return code == 0 ? SYMPRIM_OK : SYMPRIM_E_NOLOCK; 22 0 22
96 }
97 #endif
98  
99 13 int symprim_lock_memory(void *buf, size_t len)
100 {
101 /* The table refuses a length with nothing behind it before this is
102 * reached, and the door that tests it always holds a block.
103 # uncoverable condition left count:1 */
104 13 ▶ if (!buf || !len)
Branch: 2 branches
BranchTrueFalseTotal
if (!buf || !len) 13 0 13
if (!buf || !len) 1 12 13
105 1 return SYMPRIM_OK;
106 #if defined(_WIN32)
107 return VirtualLock(buf, len) ? SYMPRIM_OK : SYMPRIM_E_NOLOCK;
108 #elif defined(SYMPRIM_HAVE_MLOCK)
109 12 return on_whole_pages(buf, len, 1);
110 #else
111 return SYMPRIM_E_NOLOCK;
112 #endif
113 }
114  
115 13 int symprim_unlock_memory(void *buf, size_t len)
116 {
117 /* As for the lock.
118 # uncoverable condition left count:1 */
119 13 ▶ if (!buf || !len)
Branch: 2 branches
BranchTrueFalseTotal
if (!buf || !len) 13 0 13
if (!buf || !len) 1 12 13
120 1 return SYMPRIM_OK;
121 #if defined(_WIN32)
122 return VirtualUnlock(buf, len) ? SYMPRIM_OK : SYMPRIM_E_NOLOCK;
123 #elif defined(SYMPRIM_HAVE_MLOCK)
124 12 return on_whole_pages(buf, len, 0);
125 #else
126 return SYMPRIM_E_NOLOCK;
127 #endif
128 }
« src/random.c ↑ Summary xs/abi.xs »