Skip to content

Commit 97316bb

Browse files
committed
Disabled Clang builtins for mempcpy/memccpy tests. Defined C95 <wchar.h> functions since Clang can inline the functions with its own builtins
1 parent 5f36f7b commit 97316bb

File tree

4 files changed

+155
-29
lines changed

4 files changed

+155
-29
lines changed

src/libc/include/wchar.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _WCHAR_H
22
#define _WCHAR_H
33

4+
#include <cdefs.h>
5+
46
#ifndef _WCHAR_T_DEFINED
57
#define _WCHAR_T_DEFINED
68
#ifndef __cplusplus
@@ -17,4 +19,68 @@ typedef __WCHAR_TYPE__ wchar_t;
1719

1820
#define WEOF -1
1921

22+
__BEGIN_DECLS
23+
24+
wchar_t *wmemcpy(wchar_t *__restrict dest, const wchar_t *__restrict src, size_t n)
25+
__attribute__((nonnull(1, 2)));
26+
27+
wchar_t *wmemmove(wchar_t *dest, const wchar_t *src, size_t n)
28+
__attribute__((nonnull(1, 2)));
29+
30+
wchar_t *wmemset(wchar_t *s, wchar_t c, size_t n)
31+
__attribute__((nonnull(1)));
32+
33+
int wmemcmp(const wchar_t *s1, const wchar_t *s2, size_t n)
34+
__attribute__((nonnull(1, 2)));
35+
36+
wchar_t *wmemchr(const wchar_t *s, int c, size_t n)
37+
__attribute__((nonnull(1)));
38+
39+
wchar_t *wcscpy(wchar_t *__restrict dest, const wchar_t *__restrict src)
40+
__attribute__((nonnull(1, 2)));
41+
42+
wchar_t *wcsncpy(wchar_t *__restrict dest, const wchar_t *__restrict src, size_t n)
43+
__attribute__((nonnull(1, 2)));
44+
45+
wchar_t *wcscat(wchar_t *__restrict dest, const wchar_t *__restrict src)
46+
__attribute__((nonnull(1, 2)));
47+
48+
wchar_t *wcsncat(wchar_t *__restrict dest, const wchar_t *__restrict src, size_t n)
49+
__attribute__((nonnull(1, 2)));
50+
51+
wchar_t *wcschr(const wchar_t *s, int c)
52+
__attribute__((nonnull(1)));
53+
54+
wchar_t *wcsrchr(const wchar_t *s, int c)
55+
__attribute__((nonnull(1)));
56+
57+
wchar_t *wcspbrk(const wchar_t *s, const wchar_t *accept)
58+
__attribute__((nonnull(1, 2)));
59+
60+
wchar_t *wcsstr(const wchar_t *haystack, const wchar_t *needle)
61+
__attribute__((nonnull(1, 2)));
62+
63+
wchar_t *wcstok(wchar_t *__restrict s, const wchar_t *__restrict delim)
64+
__attribute__((nonnull(2)));
65+
66+
size_t wcscspn(const wchar_t *s, const wchar_t *reject)
67+
__attribute__((nonnull(1, 2)));
68+
69+
size_t wcsspn(const wchar_t *s, const wchar_t *accept)
70+
__attribute__((nonnull(1, 2)));
71+
72+
size_t wcslen(const wchar_t *s)
73+
__attribute__((nonnull(1)));
74+
75+
size_t wcsnlen(const wchar_t *s, size_t maxlen)
76+
__attribute__((nonnull(1)));
77+
78+
int wcscmp(const wchar_t *s1, const wchar_t *s2)
79+
__attribute__((nonnull(1, 2)));
80+
81+
int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
82+
__attribute__((nonnull(1, 2)));
83+
84+
__END_DECLS
85+
2086
#endif /* _WCHAR_H */

src/libc/mempcpy.src

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ _mempcpy:
1515
ld de, (iy + 6) ; Load source
1616
ex de, hl
1717
ldir
18+
ex de, hl
1819
ret

test/standalone/asprintf_fprintf/src/main.c

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,46 @@
1919
* memccpy
2020
*/
2121

22+
// prevents Clang from replacing function calls with builtins
23+
#if 1
24+
25+
void *T_memcpy(void *__restrict dest, const void *__restrict src, size_t n)
26+
__attribute__((nonnull(1, 2)));
27+
28+
void *T_memset(void *s, int c, size_t n)
29+
__attribute__((nonnull(1)));
30+
31+
int T_memcmp(const void *s1, const void *s2, size_t n)
32+
__attribute__((nonnull(1, 2)));
33+
34+
void *T_memccpy(void *__restrict dest, const void *__restrict src, int c, size_t n)
35+
__attribute__((nonnull(1, 2)));
36+
37+
void *T_mempcpy(void *__restrict dest, const void *__restrict src, size_t n)
38+
__attribute__((nonnull(1, 2)));
39+
40+
char *T_stpcpy(char *__restrict dest, const char *__restrict src)
41+
__attribute__((nonnull(1, 2)));
42+
43+
size_t T_strlen(const char *s)
44+
__attribute__((nonnull(1)));
45+
46+
int T_strcmp(const char *s1, const char *s2)
47+
__attribute__((nonnull(1, 2)));
48+
49+
#else
50+
51+
#define T_memcpy memcpy
52+
#define T_memset memset
53+
#define T_memcmp memcmp
54+
#define T_memccpy memccpy
55+
#define T_mempcpy mempcpy
56+
#define T_stpcpy stpcpy
57+
#define T_strlen strlen
58+
#define T_strcmp strcmp
59+
60+
#endif
61+
2262
static char const * const test_1 =
2363
"+123 asprintf% 076543 0x9abcd 0XFE1 0\n";
2464
static char const * const test_2 =
@@ -42,9 +82,9 @@ int ti_tests(void) {
4282
if (buf[len] != '\0') {
4383
return __LINE__;
4484
}
45-
size_t buf_len = strlen(buf);
46-
if (buf_len != strlen(test_1) || buf_len != (size_t)len) {
47-
printf("E: %zu != %zu != %d\n", strlen(test_1), buf_len, len);
85+
size_t buf_len = T_strlen(buf);
86+
if (buf_len != T_strlen(test_1) || buf_len != (size_t)len) {
87+
printf("E: %zu != %zu != %d\n", T_strlen(test_1), buf_len, len);
4888
return __LINE__;
4989
}
5090
if (pos != pos_1) {
@@ -63,23 +103,23 @@ int ti_tests(void) {
63103
return __LINE__;
64104
}
65105
int len_2 = ti_snprintf(append, sizeof(append), "%s", test_1);
66-
if (len_2 != strlen(test_1)) {
67-
printf("E: %d != %zu\n", len_2, strlen(test_1));
106+
if (len_2 != (int)T_strlen(test_1)) {
107+
printf("E: %d != %zu\n", len_2, T_strlen(test_1));
68108
return __LINE__;
69109
}
70110
char str2[128];
71111
char* end;
72-
end = stpcpy(str2, append);
73-
end = stpcpy(end, "");
74-
end = stpcpy(end, "foo");
112+
end = T_stpcpy(str2, append);
113+
end = T_stpcpy(end, "");
114+
end = T_stpcpy(end, "foo");
75115
if (*end != '\0') {
76116
return __LINE__;
77117
}
78118
if (end != &str2[pos_2]) {
79119
printf("diff %p - %p = %td\n", end, str2, (ptrdiff_t)(end - str2));
80120
return __LINE__;
81121
}
82-
int cmp2 = strcmp(str2, test_2);
122+
int cmp2 = T_strcmp(str2, test_2);
83123
if (cmp2 != 0) {
84124
printf("cmp: %d\n", cmp2);
85125
return __LINE__;
@@ -100,16 +140,16 @@ int nano_tests(void) {
100140
if (buf[len] != '\0') {
101141
return __LINE__;
102142
}
103-
size_t buf_len = strlen(buf);
104-
if (buf_len != strlen(test_1) || buf_len != (size_t)len) {
105-
printf("E: %zu != %zu != %d\n", strlen(test_1), buf_len, len);
143+
size_t buf_len = T_strlen(buf);
144+
if (buf_len != T_strlen(test_1) || buf_len != (size_t)len) {
145+
printf("E: %zu != %zu != %d\n", T_strlen(test_1), buf_len, len);
106146
return __LINE__;
107147
}
108148
if (pos != pos_1) {
109149
printf("E: %d != %d\n", pos, pos_1);
110150
return __LINE__;
111151
}
112-
int cmp = strcmp(buf, test_1);
152+
int cmp = T_strcmp(buf, test_1);
113153
if (cmp != 0) {
114154
printf("cmp: %d\n", cmp);
115155
return __LINE__;
@@ -147,7 +187,7 @@ static void get_diff_char(const char* data, const char* test) {
147187

148188
int memccpy_tests(void) {
149189
// test zero byte case
150-
void* ptr = memccpy((void*)0xC0FFEE, (void*)0x123456, 123, 0);
190+
void* ptr = T_memccpy((void*)0xC0FFEE, (void*)0x123456, 123, 0);
151191
if (ptr != NULL) {
152192
printf("%p != NULL\n", ptr);
153193
return __LINE__;
@@ -167,7 +207,7 @@ int memccpy_tests(void) {
167207

168208
for (size_t i = 0; i != sizeof terminal; ++i)
169209
{
170-
void* to = memccpy(dest, src, terminal[i], sizeof dest);
210+
void* to = T_memccpy(dest, src, terminal[i], sizeof dest);
171211

172212
fprintf(file,"Terminal '%c' (%s):\t\"", terminal[i], to ? "found" : "absent");
173213

@@ -193,7 +233,7 @@ int memccpy_tests(void) {
193233
for (size_t t = 0; t != (sizeof star_distance) / (sizeof star_distance[0]); ++t)
194234
{
195235
if (first) {
196-
first = memccpy(first, star_distance[t], ' ', last - first);
236+
first = T_memccpy(first, star_distance[t], ' ', last - first);
197237
} else {
198238
break;
199239
}
@@ -226,8 +266,8 @@ int memccpy_tests(void) {
226266
perror("Error reading from file");
227267
return __LINE__;
228268
}
229-
if (strlen(buf) != strlen(fprintf_test)) {
230-
printf("E: %zu != %zu\n", strlen(buf), strlen(fprintf_test));
269+
if (T_strlen(buf) != T_strlen(fprintf_test)) {
270+
printf("E: %zu != %zu\n", T_strlen(buf), T_strlen(fprintf_test));
231271
get_diff_char(buf, fprintf_test);
232272
return __LINE__;
233273
}
@@ -242,29 +282,29 @@ int memccpy_tests(void) {
242282

243283
int mempcpy_test(void) {
244284
// test zero byte case
245-
void* ptr = mempcpy((void*)0xC0FFEE, (void*)0x123456, 0);
285+
void* ptr = T_mempcpy((void*)0xC0FFEE, (void*)0x123456, 0);
246286
if (ptr != (void*)0xC0FFEE) {
247287
printf("%p != %p\n", ptr, (void*)0xC0FFEE);
248288
return __LINE__;
249289
}
250290
char data[192 + 1];
251-
memset(&data[ 0], 0x12, 64);
252-
memset(&data[ 64], 0x12, 64);
253-
memset(&data[128], 0x56, 64);
291+
T_memset(&data[ 0], 0x12, 64);
292+
T_memset(&data[ 64], 0x12, 64);
293+
T_memset(&data[128], 0x56, 64);
254294
char append[64 + 1];
255-
memset(append, 0x34, 64);
256-
char* res = mempcpy(&data[64], append, 64);
295+
T_memset(append, 0x34, 64);
296+
char* res = T_mempcpy(&data[64], append, 64);
257297
if (res != &data[128]) {
258-
printf("%p != %p\n", res, data);
298+
printf("%p != %p\n", res, &data[128]);
259299
return __LINE__;
260300
}
261301

262302
char truth[192 + 1];
263-
memset(&truth[ 0], 0x12, 64);
264-
memset(&truth[ 64], 0x34, 64);
265-
memset(&truth[128], 0x56, 64);
303+
T_memset(&truth[ 0], 0x12, 64);
304+
T_memset(&truth[ 64], 0x34, 64);
305+
T_memset(&truth[128], 0x56, 64);
266306

267-
int cmp = memcmp(data, truth, 192);
307+
int cmp = T_memcmp(data, truth, 192);
268308
if (cmp != 0) {
269309
printf("cmp: %d\n", cmp);
270310
return __LINE__;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
assume adl = 1
2+
3+
section .text
4+
5+
public _T_memset, _T_memcpy, _T_memcmp, _T_memccpy, _T_mempcpy
6+
public _T_strlen, _T_strcmp, _T_stpcpy
7+
8+
_T_memset := _memset
9+
_T_memcpy := _memcpy
10+
_T_memcmp := _memcmp
11+
_T_memccpy := _memccpy
12+
_T_mempcpy := _mempcpy
13+
14+
_T_strlen := _strlen
15+
_T_strcmp := _strcmp
16+
_T_stpcpy := _stpcpy
17+
18+
extern _memset, _memcpy, _memcmp, _memccpy, _mempcpy
19+
extern _strlen, _strcmp, _stpcpy

0 commit comments

Comments
 (0)