Skip to content

Commit

Permalink
add initial clang support
Browse files Browse the repository at this point in the history
  • Loading branch information
q66 authored and jvoisin committed Jun 25, 2023
1 parent adae76a commit fe14962
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 61 deletions.
25 changes: 23 additions & 2 deletions include/fortify-headers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
* Copyright (C) 2022 q66 <q66@chimera-linux.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
Expand All @@ -16,10 +17,30 @@
#ifndef _FORTIFY_HEADERS_H
#define _FORTIFY_HEADERS_H

#ifdef __clang__

/* clang uses overloads; see https://github.com/llvm/llvm-project/issues/53516 */
#define _FORTIFY_POSN(n) const __attribute__((__pass_object_size__(n)))
/* we can't use extern inline with overloads without making them external */
#define _FORTIFY_INLINE static __inline__ \
__attribute__((__always_inline__,__artificial__,__overloadable__))

#else /* !__clang__ */

#define _FORTIFY_POSN(n)
#define _FORTIFY_INLINE extern __inline__ \
__attribute__((__always_inline__,__gnu_inline__,__artificial__))

#endif /* __clang__ */

#define _FORTIFY_POS0 _FORTIFY_POSN(0)
#define _FORTIFY_POS1 _FORTIFY_POSN(1)
#define _FORTIFY_POS2 _FORTIFY_POSN(2)

#define _FORTIFY_STR(s) #s
#define _FORTIFY_ORIG(p,fn) __typeof__(fn) __orig_##fn __asm__(_FORTIFY_STR(p) #fn)
#define _FORTIFY_FN(fn) _FORTIFY_ORIG(__USER_LABEL_PREFIX__,fn); \
extern __inline__ __attribute__((__always_inline__,__gnu_inline__,__artificial__))
#define _FORTIFY_FNB(fn) _FORTIFY_ORIG(__USER_LABEL_PREFIX__,fn)
#define _FORTIFY_FN(fn) _FORTIFY_FNB(fn); _FORTIFY_INLINE


/* Use __builtin_dynamic_object_size with _FORTIFY_SOURCE>2, if available. */
Expand Down
7 changes: 4 additions & 3 deletions include/poll.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
* Copyright (C) 2022 q66 <q66@chimera-linux.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
Expand Down Expand Up @@ -30,7 +31,7 @@ extern "C" {

#undef poll

_FORTIFY_FN(poll) int poll(struct pollfd *__f, nfds_t __n, int __s)
_FORTIFY_FN(poll) int poll(struct pollfd * _FORTIFY_POS0 __f, nfds_t __n, int __s)
{
size_t __b = __bos(__f, 0);

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

#if defined(_GNU_SOURCE) && !_REDIR_TIME64
#undef ppoll
_FORTIFY_FN(ppoll) int ppoll(struct pollfd *__f, nfds_t __n, const struct timespec *__s,
const sigset_t *__m)
_FORTIFY_FN(ppoll) int ppoll(struct pollfd * _FORTIFY_POS0 __f, nfds_t __n,
const struct timespec *__s, const sigset_t *__m)
{
size_t __b = __bos(__f, 0);

Expand Down
37 changes: 30 additions & 7 deletions include/stdio.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
* Copyright (C) 2022 q66 <q66@chimera-linux.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
Expand Down Expand Up @@ -37,7 +38,7 @@ extern "C" {
#undef sprintf

__access(write_only, 1, 2)
_FORTIFY_FN(fgets) char *fgets(char *__s, int __n, FILE *__f)
_FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f)
{
size_t __b = __bos(__s, 0);

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

_FORTIFY_FN(fread) size_t fread(void *__d, size_t __n, size_t __m, FILE *__f)
_FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n,
size_t __m, FILE *__f)
{
size_t __b = __bos(__d, 0);

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

_FORTIFY_FN(fwrite) size_t fwrite(const void *__d, size_t __n, size_t __m, FILE *__f)
_FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n,
size_t __m, FILE *__f)
{
size_t __b = __bos(__d, 0);

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

_FORTIFY_FN(vsnprintf) int vsnprintf(char *__s, size_t __n, const char *__f,
__builtin_va_list __v)
_FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n,
const char *__f, __builtin_va_list __v)
{
size_t __b = __bos(__s, 0);

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

_FORTIFY_FN(vsprintf) int vsprintf(char *__s, const char *__f, __builtin_va_list __v)
_FORTIFY_FN(vsprintf) int vsprintf(char * _FORTIFY_POS0 __s, const char *__f,
__builtin_va_list __v)
{
size_t __b = __bos(__s, 0);
int __r;
Expand All @@ -93,7 +97,23 @@ _FORTIFY_FN(vsprintf) int vsprintf(char *__s, const char *__f, __builtin_va_list
return __r;
}

_FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, const char *__f, ...)
#if defined(__has_builtin)
#if __has_builtin(__builtin_va_arg_pack)

/* clang is missing __builtin_va_arg_pack, so we cannot use these impls
* outside of gcc; we then have a few options:
*
* 1) using va_start/end and implementing these functions as static inline,
* with inlining never happening; that means extra symbols with internal
* linkage, which is not ideal
* 2) using macros; this is incompatible with c++ and since musl does not
* have the __chk variants, we'd need to implement a body with intermediate
* variables within the macro, which means more non-portable mess
* 3) not implementing these under clang, which is what we do for now
*/

_FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n,
const char *__f, ...)
{
size_t __b = __bos(__s, 0);

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

#endif /* __has_builtin(__builtin_va_arg_pack) */
#endif /* defined(__has_builtin) */

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 3 additions & 1 deletion include/stdlib.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
* Copyright (C) 2022 q66 <q66@chimera-linux.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
Expand Down Expand Up @@ -35,7 +36,8 @@ __extension__
extern "C" {
#endif

#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
/* FIXME clang */
#if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(__clang__)
#undef realpath
_FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r)
{
Expand Down
33 changes: 21 additions & 12 deletions include/string.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
* Copyright (C) 2022 q66 <q66@chimera-linux.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
Expand Down Expand Up @@ -38,7 +39,8 @@ extern "C" {

__access(write_only, 1)
__access(read_only, 2, 3)
_FORTIFY_FN(memcpy) void *memcpy(void *__od, const void *__os, size_t __n)
_FORTIFY_FN(memcpy) void *memcpy(void * _FORTIFY_POS0 __od,
const void * _FORTIFY_POS0 __os, size_t __n)
{
size_t __bd = __bos(__od, 0);
size_t __bs = __bos(__os, 0);
Expand All @@ -57,7 +59,8 @@ _FORTIFY_FN(memcpy) void *memcpy(void *__od, const void *__os, size_t __n)

__access(write_only, 1)
__access(read_only, 2, 3)
_FORTIFY_FN(memmove) void *memmove(void *__d, const void *__s, size_t __n)
_FORTIFY_FN(memmove) void *memmove(void * _FORTIFY_POS0 __d,
const void * _FORTIFY_POS0 __s, size_t __n)
{
size_t __bd = __bos(__d, 0);
size_t __bs = __bos(__s, 0);
Expand All @@ -68,7 +71,7 @@ _FORTIFY_FN(memmove) void *memmove(void *__d, const void *__s, size_t __n)
}

__access(write_only, 1)
_FORTIFY_FN(memset) void *memset(void *__d, int __c, size_t __n)
_FORTIFY_FN(memset) void *memset(void * _FORTIFY_POS0 __d, int __c, size_t __n)
{
size_t __b = __bos(__d, 0);

Expand All @@ -83,7 +86,7 @@ _FORTIFY_FN(memset) void *memset(void *__d, int __c, size_t __n)
#undef stpcpy
__access(write_only, 1)
__access(read_only, 2)
_FORTIFY_FN(stpcpy) char *stpcpy(char *__d, const char *__s)
_FORTIFY_FN(stpcpy) char *stpcpy(char * _FORTIFY_POS0 __d, const char *__s)
{
size_t __n = strlen(__s) + 1;

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

__access (read_write, 1)
__access (read_only, 2)
_FORTIFY_FN(strcat) char *strcat(char *__d, const char *__s)
_FORTIFY_FN(strcat) char *strcat(char * _FORTIFY_POS0 __d, const char *__s)
{
size_t __b = __bos(__d, 0);

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

__access (write_only, 1)
__access (read_only, 2)
_FORTIFY_FN(strcpy) char *strcpy(char *__d, const char *__s)
_FORTIFY_FN(strcpy) char *strcpy(char * _FORTIFY_POS0 __d, const char *__s)
{
size_t __n = strlen(__s) + 1;

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

__access (read_write, 1)
__access (read_only, 2, 3)
_FORTIFY_FN(strncat) char *strncat(char *__d, const char *__s, size_t __n)
_FORTIFY_FN(strncat) char *strncat(char * _FORTIFY_POS0 __d, const char *__s,
size_t __n)
{
size_t __b = __bos(__d, 0);

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

__access (write_only, 1)
__access (read_only, 2, 3)
_FORTIFY_FN(strncpy) char *strncpy(char *__d, const char *__s, size_t __n)
_FORTIFY_FN(strncpy) char *strncpy(char * _FORTIFY_POS0 __d,
const char *__s, size_t __n)
{
/* trap if pointers are overlapping but not if dst == src.
* gcc seems to like to generate code that relies on dst == src */
Expand All @@ -183,7 +189,8 @@ _FORTIFY_FN(strncpy) char *strncpy(char *__d, const char *__s, size_t __n)
#undef mempcpy
__access(write_only, 1)
__access(read_only, 2, 3)
_FORTIFY_FN(mempcpy) void *mempcpy(void *__d, const void *__s, size_t __n)
_FORTIFY_FN(mempcpy) void *mempcpy(void * _FORTIFY_POS0 __d,
const void * _FORTIFY_POS0 __s, size_t __n)
{
size_t __bd = __bos(__d, 0);
size_t __bs = __bos(__s, 0);
Expand All @@ -199,7 +206,8 @@ _FORTIFY_FN(mempcpy) void *mempcpy(void *__d, const void *__s, size_t __n)
#undef strlcpy
__access (read_write, 1)
__access (read_only, 2, 3)
_FORTIFY_FN(strlcat) size_t strlcat(char *__d, const char *__s, size_t __n)
_FORTIFY_FN(strlcat) size_t strlcat(char * _FORTIFY_POS0 __d,
const char *__s, size_t __n)
{
size_t __b = __bos(__d, 0);

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

__access (write_only, 1)
__access (read_only, 2, 3)
_FORTIFY_FN(strlcpy) size_t strlcpy(char *__d, const char *__s, size_t __n)
_FORTIFY_FN(strlcpy) size_t strlcpy(char * _FORTIFY_POS0 __d,
const char *__s, size_t __n)
{
size_t __b = __bos(__d, 0);

Expand Down
6 changes: 4 additions & 2 deletions include/strings.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
* Copyright (C) 2022 q66 <q66@chimera-linux.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
Expand Down Expand Up @@ -30,7 +31,8 @@ extern "C" {
|| (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700)
#undef bcopy
#undef bzero
_FORTIFY_FN(bcopy) void bcopy(const void *__s, void *__d, size_t __n)
_FORTIFY_FN(bcopy) void bcopy(const void * _FORTIFY_POS0 __s,
void * _FORTIFY_POS0 __d, size_t __n)
{
size_t __bd = __bos(__d, 0);
size_t __bs = __bos(__s, 0);
Expand All @@ -40,7 +42,7 @@ _FORTIFY_FN(bcopy) void bcopy(const void *__s, void *__d, size_t __n)
return __orig_bcopy(__s, __d, __n);
}

_FORTIFY_FN(bzero) void bzero(void *__s, size_t __n)
_FORTIFY_FN(bzero) void bzero(void * _FORTIFY_POS0 __s, size_t __n)
{
size_t __b = __bos(__s, 0);

Expand Down
13 changes: 11 additions & 2 deletions include/sys/select.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
* Copyright (C) 2022 q66 <q66@chimera-linux.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
Expand Down Expand Up @@ -27,8 +28,14 @@ __extension__
extern "C" {
#endif

#ifdef __clang__
#define _FORTIFY_FD_POS0 const __attribute__((__pass_object_size__(0)))
#else
#define _FORTIFY_FD_POS0
#endif

static __inline__ __attribute__((__always_inline__,__gnu_inline__,__artificial__))
void __fortify_FD_CLR(int __f, fd_set *__s)
void __fortify_FD_CLR(int __f, fd_set * _FORTIFY_FD_POS0 __s)
{
size_t __b = __bos(__s, 0);

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

static __inline__ __attribute__((__always_inline__,__gnu_inline__,__artificial__))
void __fortify_FD_SET(int __f, fd_set *__s)
void __fortify_FD_SET(int __f, fd_set * _FORTIFY_FD_POS0 __s)
{
size_t __b = __bos(__s, 0);

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

#undef _FORTIFY_FD_POS0

#undef FD_CLR
#define FD_CLR(fd, set) __fortify_FD_CLR(fd, set)
#undef FD_SET
Expand Down
Loading

0 comments on commit fe14962

Please sign in to comment.