Skip to content

Commit fe14962

Browse files
q66jvoisin
authored andcommitted
add initial clang support
1 parent adae76a commit fe14962

File tree

10 files changed

+157
-61
lines changed

10 files changed

+157
-61
lines changed

include/fortify-headers.h

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015-2016 Dimitris Papastamos <[email protected]>
3+
* Copyright (C) 2022 q66 <[email protected]>
34
*
45
* Permission to use, copy, modify, and/or distribute this software for any
56
* purpose with or without fee is hereby granted.
@@ -16,10 +17,30 @@
1617
#ifndef _FORTIFY_HEADERS_H
1718
#define _FORTIFY_HEADERS_H
1819

20+
#ifdef __clang__
21+
22+
/* clang uses overloads; see https://github.com/llvm/llvm-project/issues/53516 */
23+
#define _FORTIFY_POSN(n) const __attribute__((__pass_object_size__(n)))
24+
/* we can't use extern inline with overloads without making them external */
25+
#define _FORTIFY_INLINE static __inline__ \
26+
__attribute__((__always_inline__,__artificial__,__overloadable__))
27+
28+
#else /* !__clang__ */
29+
30+
#define _FORTIFY_POSN(n)
31+
#define _FORTIFY_INLINE extern __inline__ \
32+
__attribute__((__always_inline__,__gnu_inline__,__artificial__))
33+
34+
#endif /* __clang__ */
35+
36+
#define _FORTIFY_POS0 _FORTIFY_POSN(0)
37+
#define _FORTIFY_POS1 _FORTIFY_POSN(1)
38+
#define _FORTIFY_POS2 _FORTIFY_POSN(2)
39+
1940
#define _FORTIFY_STR(s) #s
2041
#define _FORTIFY_ORIG(p,fn) __typeof__(fn) __orig_##fn __asm__(_FORTIFY_STR(p) #fn)
21-
#define _FORTIFY_FN(fn) _FORTIFY_ORIG(__USER_LABEL_PREFIX__,fn); \
22-
extern __inline__ __attribute__((__always_inline__,__gnu_inline__,__artificial__))
42+
#define _FORTIFY_FNB(fn) _FORTIFY_ORIG(__USER_LABEL_PREFIX__,fn)
43+
#define _FORTIFY_FN(fn) _FORTIFY_FNB(fn); _FORTIFY_INLINE
2344

2445

2546
/* Use __builtin_dynamic_object_size with _FORTIFY_SOURCE>2, if available. */

include/poll.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015-2016 Dimitris Papastamos <[email protected]>
3+
* Copyright (C) 2022 q66 <[email protected]>
34
*
45
* Permission to use, copy, modify, and/or distribute this software for any
56
* purpose with or without fee is hereby granted.
@@ -30,7 +31,7 @@ extern "C" {
3031

3132
#undef poll
3233

33-
_FORTIFY_FN(poll) int poll(struct pollfd *__f, nfds_t __n, int __s)
34+
_FORTIFY_FN(poll) int poll(struct pollfd * _FORTIFY_POS0 __f, nfds_t __n, int __s)
3435
{
3536
size_t __b = __bos(__f, 0);
3637

@@ -41,8 +42,8 @@ _FORTIFY_FN(poll) int poll(struct pollfd *__f, nfds_t __n, int __s)
4142

4243
#if defined(_GNU_SOURCE) && !_REDIR_TIME64
4344
#undef ppoll
44-
_FORTIFY_FN(ppoll) int ppoll(struct pollfd *__f, nfds_t __n, const struct timespec *__s,
45-
const sigset_t *__m)
45+
_FORTIFY_FN(ppoll) int ppoll(struct pollfd * _FORTIFY_POS0 __f, nfds_t __n,
46+
const struct timespec *__s, const sigset_t *__m)
4647
{
4748
size_t __b = __bos(__f, 0);
4849

include/stdio.h

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015-2016 Dimitris Papastamos <[email protected]>
3+
* Copyright (C) 2022 q66 <[email protected]>
34
*
45
* Permission to use, copy, modify, and/or distribute this software for any
56
* purpose with or without fee is hereby granted.
@@ -37,7 +38,7 @@ extern "C" {
3738
#undef sprintf
3839

3940
__access(write_only, 1, 2)
40-
_FORTIFY_FN(fgets) char *fgets(char *__s, int __n, FILE *__f)
41+
_FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f)
4142
{
4243
size_t __b = __bos(__s, 0);
4344

@@ -46,7 +47,8 @@ _FORTIFY_FN(fgets) char *fgets(char *__s, int __n, FILE *__f)
4647
return __orig_fgets(__s, __n, __f);
4748
}
4849

49-
_FORTIFY_FN(fread) size_t fread(void *__d, size_t __n, size_t __m, FILE *__f)
50+
_FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n,
51+
size_t __m, FILE *__f)
5052
{
5153
size_t __b = __bos(__d, 0);
5254

@@ -57,7 +59,8 @@ _FORTIFY_FN(fread) size_t fread(void *__d, size_t __n, size_t __m, FILE *__f)
5759
return __orig_fread(__d, __n, __m, __f);
5860
}
5961

60-
_FORTIFY_FN(fwrite) size_t fwrite(const void *__d, size_t __n, size_t __m, FILE *__f)
62+
_FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n,
63+
size_t __m, FILE *__f)
6164
{
6265
size_t __b = __bos(__d, 0);
6366

@@ -68,8 +71,8 @@ _FORTIFY_FN(fwrite) size_t fwrite(const void *__d, size_t __n, size_t __m, FILE
6871
return __orig_fwrite(__d, __n, __m, __f);
6972
}
7073

71-
_FORTIFY_FN(vsnprintf) int vsnprintf(char *__s, size_t __n, const char *__f,
72-
__builtin_va_list __v)
74+
_FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n,
75+
const char *__f, __builtin_va_list __v)
7376
{
7477
size_t __b = __bos(__s, 0);
7578

@@ -78,7 +81,8 @@ _FORTIFY_FN(vsnprintf) int vsnprintf(char *__s, size_t __n, const char *__f,
7881
return __orig_vsnprintf(__s, __n, __f, __v);
7982
}
8083

81-
_FORTIFY_FN(vsprintf) int vsprintf(char *__s, const char *__f, __builtin_va_list __v)
84+
_FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f,
85+
__builtin_va_list __v)
8286
{
8387
size_t __b = __bos(__s, 0);
8488
int __r;
@@ -93,7 +97,23 @@ _FORTIFY_FN(vsprintf) int vsprintf(char *__s, const char *__f, __builtin_va_list
9397
return __r;
9498
}
9599

96-
_FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, const char *__f, ...)
100+
#if defined(__has_builtin)
101+
#if __has_builtin(__builtin_va_arg_pack)
102+
103+
/* clang is missing __builtin_va_arg_pack, so we cannot use these impls
104+
* outside of gcc; we then have a few options:
105+
*
106+
* 1) using va_start/end and implementing these functions as static inline,
107+
* with inlining never happening; that means extra symbols with internal
108+
* linkage, which is not ideal
109+
* 2) using macros; this is incompatible with c++ and since musl does not
110+
* have the __chk variants, we'd need to implement a body with intermediate
111+
* variables within the macro, which means more non-portable mess
112+
* 3) not implementing these under clang, which is what we do for now
113+
*/
114+
115+
_FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n,
116+
const char *__f, ...)
97117
{
98118
size_t __b = __bos(__s, 0);
99119

@@ -117,6 +137,9 @@ _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...)
117137
return __r;
118138
}
119139

140+
#endif /* __has_builtin(__builtin_va_arg_pack) */
141+
#endif /* defined(__has_builtin) */
142+
120143
#ifdef __cplusplus
121144
}
122145
#endif

include/stdlib.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015-2016 Dimitris Papastamos <[email protected]>
3+
* Copyright (C) 2022 q66 <[email protected]>
34
*
45
* Permission to use, copy, modify, and/or distribute this software for any
56
* purpose with or without fee is hereby granted.
@@ -35,7 +36,8 @@ __extension__
3536
extern "C" {
3637
#endif
3738

38-
#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
39+
/* FIXME clang */
40+
#if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(__clang__)
3941
#undef realpath
4042
_FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r)
4143
{

include/string.h

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015-2016 Dimitris Papastamos <[email protected]>
3+
* Copyright (C) 2022 q66 <[email protected]>
34
*
45
* Permission to use, copy, modify, and/or distribute this software for any
56
* purpose with or without fee is hereby granted.
@@ -38,7 +39,8 @@ extern "C" {
3839

3940
__access(write_only, 1)
4041
__access(read_only, 2, 3)
41-
_FORTIFY_FN(memcpy) void *memcpy(void *__od, const void *__os, size_t __n)
42+
_FORTIFY_FN(memcpy) void *memcpy(void * _FORTIFY_POS0 __od,
43+
const void * _FORTIFY_POS0 __os, size_t __n)
4244
{
4345
size_t __bd = __bos(__od, 0);
4446
size_t __bs = __bos(__os, 0);
@@ -57,7 +59,8 @@ _FORTIFY_FN(memcpy) void *memcpy(void *__od, const void *__os, size_t __n)
5759

5860
__access(write_only, 1)
5961
__access(read_only, 2, 3)
60-
_FORTIFY_FN(memmove) void *memmove(void *__d, const void *__s, size_t __n)
62+
_FORTIFY_FN(memmove) void *memmove(void * _FORTIFY_POS0 __d,
63+
const void * _FORTIFY_POS0 __s, size_t __n)
6164
{
6265
size_t __bd = __bos(__d, 0);
6366
size_t __bs = __bos(__s, 0);
@@ -68,7 +71,7 @@ _FORTIFY_FN(memmove) void *memmove(void *__d, const void *__s, size_t __n)
6871
}
6972

7073
__access(write_only, 1)
71-
_FORTIFY_FN(memset) void *memset(void *__d, int __c, size_t __n)
74+
_FORTIFY_FN(memset) void *memset(void * _FORTIFY_POS0 __d, int __c, size_t __n)
7275
{
7376
size_t __b = __bos(__d, 0);
7477

@@ -83,7 +86,7 @@ _FORTIFY_FN(memset) void *memset(void *__d, int __c, size_t __n)
8386
#undef stpcpy
8487
__access(write_only, 1)
8588
__access(read_only, 2)
86-
_FORTIFY_FN(stpcpy) char *stpcpy(char *__d, const char *__s)
89+
_FORTIFY_FN(stpcpy) char *stpcpy(char * _FORTIFY_POS0 __d, const char *__s)
8790
{
8891
size_t __n = strlen(__s) + 1;
8992

@@ -102,7 +105,8 @@ _FORTIFY_FN(stpcpy) char *stpcpy(char *__d, const char *__s)
102105
#undef stpncpy
103106
__access(write_only, 1)
104107
__access(read_only, 2, 3)
105-
_FORTIFY_FN(stpncpy) char *stpncpy(char *__d, const char *__s, size_t __n)
108+
_FORTIFY_FN(stpncpy) char *stpncpy(char * _FORTIFY_POS0 __d, const char *__s,
109+
size_t __n)
106110
{
107111
/* trap if pointers are overlapping but not if dst == src.
108112
* gcc seems to like to generate code that relies on dst == src */
@@ -119,7 +123,7 @@ _FORTIFY_FN(stpncpy) char *stpncpy(char *__d, const char *__s, size_t __n)
119123

120124
__access (read_write, 1)
121125
__access (read_only, 2)
122-
_FORTIFY_FN(strcat) char *strcat(char *__d, const char *__s)
126+
_FORTIFY_FN(strcat) char *strcat(char * _FORTIFY_POS0 __d, const char *__s)
123127
{
124128
size_t __b = __bos(__d, 0);
125129

@@ -130,7 +134,7 @@ _FORTIFY_FN(strcat) char *strcat(char *__d, const char *__s)
130134

131135
__access (write_only, 1)
132136
__access (read_only, 2)
133-
_FORTIFY_FN(strcpy) char *strcpy(char *__d, const char *__s)
137+
_FORTIFY_FN(strcpy) char *strcpy(char * _FORTIFY_POS0 __d, const char *__s)
134138
{
135139
size_t __n = strlen(__s) + 1;
136140

@@ -148,7 +152,8 @@ _FORTIFY_FN(strcpy) char *strcpy(char *__d, const char *__s)
148152

149153
__access (read_write, 1)
150154
__access (read_only, 2, 3)
151-
_FORTIFY_FN(strncat) char *strncat(char *__d, const char *__s, size_t __n)
155+
_FORTIFY_FN(strncat) char *strncat(char * _FORTIFY_POS0 __d, const char *__s,
156+
size_t __n)
152157
{
153158
size_t __b = __bos(__d, 0);
154159

@@ -165,7 +170,8 @@ _FORTIFY_FN(strncat) char *strncat(char *__d, const char *__s, size_t __n)
165170

166171
__access (write_only, 1)
167172
__access (read_only, 2, 3)
168-
_FORTIFY_FN(strncpy) char *strncpy(char *__d, const char *__s, size_t __n)
173+
_FORTIFY_FN(strncpy) char *strncpy(char * _FORTIFY_POS0 __d,
174+
const char *__s, size_t __n)
169175
{
170176
/* trap if pointers are overlapping but not if dst == src.
171177
* gcc seems to like to generate code that relies on dst == src */
@@ -183,7 +189,8 @@ _FORTIFY_FN(strncpy) char *strncpy(char *__d, const char *__s, size_t __n)
183189
#undef mempcpy
184190
__access(write_only, 1)
185191
__access(read_only, 2, 3)
186-
_FORTIFY_FN(mempcpy) void *mempcpy(void *__d, const void *__s, size_t __n)
192+
_FORTIFY_FN(mempcpy) void *mempcpy(void * _FORTIFY_POS0 __d,
193+
const void * _FORTIFY_POS0 __s, size_t __n)
187194
{
188195
size_t __bd = __bos(__d, 0);
189196
size_t __bs = __bos(__s, 0);
@@ -199,7 +206,8 @@ _FORTIFY_FN(mempcpy) void *mempcpy(void *__d, const void *__s, size_t __n)
199206
#undef strlcpy
200207
__access (read_write, 1)
201208
__access (read_only, 2, 3)
202-
_FORTIFY_FN(strlcat) size_t strlcat(char *__d, const char *__s, size_t __n)
209+
_FORTIFY_FN(strlcat) size_t strlcat(char * _FORTIFY_POS0 __d,
210+
const char *__s, size_t __n)
203211
{
204212
size_t __b = __bos(__d, 0);
205213

@@ -210,7 +218,8 @@ _FORTIFY_FN(strlcat) size_t strlcat(char *__d, const char *__s, size_t __n)
210218

211219
__access (write_only, 1)
212220
__access (read_only, 2, 3)
213-
_FORTIFY_FN(strlcpy) size_t strlcpy(char *__d, const char *__s, size_t __n)
221+
_FORTIFY_FN(strlcpy) size_t strlcpy(char * _FORTIFY_POS0 __d,
222+
const char *__s, size_t __n)
214223
{
215224
size_t __b = __bos(__d, 0);
216225

include/strings.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015-2016 Dimitris Papastamos <[email protected]>
3+
* Copyright (C) 2022 q66 <[email protected]>
34
*
45
* Permission to use, copy, modify, and/or distribute this software for any
56
* purpose with or without fee is hereby granted.
@@ -30,7 +31,8 @@ extern "C" {
3031
|| (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700)
3132
#undef bcopy
3233
#undef bzero
33-
_FORTIFY_FN(bcopy) void bcopy(const void *__s, void *__d, size_t __n)
34+
_FORTIFY_FN(bcopy) void bcopy(const void * _FORTIFY_POS0 __s,
35+
void * _FORTIFY_POS0 __d, size_t __n)
3436
{
3537
size_t __bd = __bos(__d, 0);
3638
size_t __bs = __bos(__s, 0);
@@ -40,7 +42,7 @@ _FORTIFY_FN(bcopy) void bcopy(const void *__s, void *__d, size_t __n)
4042
return __orig_bcopy(__s, __d, __n);
4143
}
4244

43-
_FORTIFY_FN(bzero) void bzero(void *__s, size_t __n)
45+
_FORTIFY_FN(bzero) void bzero(void * _FORTIFY_POS0 __s, size_t __n)
4446
{
4547
size_t __b = __bos(__s, 0);
4648

include/sys/select.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2015-2016 Dimitris Papastamos <[email protected]>
3+
* Copyright (C) 2022 q66 <[email protected]>
34
*
45
* Permission to use, copy, modify, and/or distribute this software for any
56
* purpose with or without fee is hereby granted.
@@ -27,8 +28,14 @@ __extension__
2728
extern "C" {
2829
#endif
2930

31+
#ifdef __clang__
32+
#define _FORTIFY_FD_POS0 const __attribute__((__pass_object_size__(0)))
33+
#else
34+
#define _FORTIFY_FD_POS0
35+
#endif
36+
3037
static __inline__ __attribute__((__always_inline__,__gnu_inline__,__artificial__))
31-
void __fortify_FD_CLR(int __f, fd_set *__s)
38+
void __fortify_FD_CLR(int __f, fd_set * _FORTIFY_FD_POS0 __s)
3239
{
3340
size_t __b = __bos(__s, 0);
3441

@@ -38,7 +45,7 @@ void __fortify_FD_CLR(int __f, fd_set *__s)
3845
}
3946

4047
static __inline__ __attribute__((__always_inline__,__gnu_inline__,__artificial__))
41-
void __fortify_FD_SET(int __f, fd_set *__s)
48+
void __fortify_FD_SET(int __f, fd_set * _FORTIFY_FD_POS0 __s)
4249
{
4350
size_t __b = __bos(__s, 0);
4451

@@ -47,6 +54,8 @@ void __fortify_FD_SET(int __f, fd_set *__s)
4754
FD_SET(__f, __s);
4855
}
4956

57+
#undef _FORTIFY_FD_POS0
58+
5059
#undef FD_CLR
5160
#define FD_CLR(fd, set) __fortify_FD_CLR(fd, set)
5261
#undef FD_SET

0 commit comments

Comments
 (0)