forked from picolibc/picolibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-memset.c
212 lines (179 loc) · 5.14 KB
/
test-memset.c
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
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright © 2021 Keith Packard
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>
#ifdef __arm__
void *__aeabi_memset(void *dest, size_t n, int c);
void *__aeabi_memset4(void *dest, size_t n, int c);
void *__aeabi_memset8(void *dest, size_t n, int c);
void __aeabi_memclr(void *dest, size_t n);
void __aeabi_memclr4(void *dest, size_t n);
void __aeabi_memclr8(void *dest, size_t n);
#endif
static uint8_t crc_table[256];
static uint8_t
crc8(uint8_t c)
{
int i;
for (i = 0; i < 8; i++) {
if (c & 1)
c = 0x31 ^ (c >> 1);
else
c >>= 1;
}
return c;
}
static void
init_crc(void)
{
int i;
for (i = 0; i < 256; i++)
crc_table[i] = crc8((uint8_t) i);
}
static char
expect(size_t pos)
{
unsigned int i;
uint8_t c = 0xff;
for (i = 0; i < sizeof(size_t); i++)
c ^= crc_table[(uint8_t) (pos >> (i * 8))];
return (char) c;
}
static size_t
check(char *label, void *buf, size_t size, size_t start, size_t end, int c)
{
size_t p;
size_t error = 0;
char *b = buf;
for (p = 0; p < size; p++) {
char g = b[p];
char e = c;
if (p < start || end <= p)
e = expect(p);
if (g != e) {
fprintf(stderr, "%s(%zu, %zu):%zu expect 0x%02x got 0x%02x\n",
label, start, end, p, (uint8_t) e, (uint8_t) g);
error++;
}
}
return error;
}
static size_t
randrange(size_t max)
{
size_t pot;
size_t rnd;
for (pot = 1; pot && pot < max; pot <<= 1)
;
for (;;) {
rnd = random() & (pot - 1);
if (rnd < max)
return rnd;
}
}
#define MAX_BUF 1024
#define NEND 64
#define NSTART 64
static char buf[MAX_BUF];
static void
fill(void)
{
size_t p;
for (p = 0; p < MAX_BUF; p++)
buf[p] = expect(p);
}
static size_t
test(size_t start, size_t end, int c)
{
size_t error = 0;
char *b = buf;
fill();
memset(b + start, c, end - start);
error += check("memset", buf, MAX_BUF, start, end, c);
fill();
bzero(b + start, end - start);
error += check("bzero", buf, MAX_BUF, start, end, 0);
#ifdef __arm__
fill();
__aeabi_memset(b + start, end - start, c);
error += check("__aeabi_memset", buf, MAX_BUF, start, end, c);
fill();
__aeabi_memclr(b + start, end - start);
error += check("__aeabi_memclr", buf, MAX_BUF, start, end, 0);
start &= ~3;
end &= ~3;
fill();
__aeabi_memset4(b + start, end - start, c);
error += check("__aeabi_memset4", buf, MAX_BUF, start, end, c);
fill();
__aeabi_memclr4(b + start, end - start);
error += check("__aeabi_memclr4", buf, MAX_BUF, start, end, 0);
start &= ~7;
end &= ~7;
fill();
__aeabi_memset8(b + start, end - start, c);
error += check("__aeabi_memset8", buf, MAX_BUF, start, end, c);
fill();
__aeabi_memclr8(b + start, end - start);
error += check("__aeabi_memclr8", buf, MAX_BUF, start, end, 0);
#endif
return error;
}
int
main(void)
{
size_t error;
size_t end;
size_t start;
int nend;
int nstart;
int ret = 0;
init_crc();
for (nend = 0; nend < NEND; nend++) {
end = randrange(MAX_BUF + 1);
for (nstart = 0; nstart < NSTART; nstart++) {
start = randrange(end + 1);
int c = randrange(INT_MAX);
error = test(start, end, c);
if (error)
ret = 1;
}
}
return ret;
}