fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

fortify-headers.h (2992B)


      1 /*
      2  * Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
      3  * Copyright (C) 2022 q66 <q66@chimera-linux.org>
      4  *
      5  * Permission to use, copy, modify, and/or distribute this software for any
      6  * purpose with or without fee is hereby granted.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #ifndef _FORTIFY_HEADERS_H
     18 #define _FORTIFY_HEADERS_H
     19 
     20 #ifdef __has_attribute
     21 # define __fortify_has_attribute(v) __has_attribute(v)
     22 #else
     23 # define __fortify_has_attribute(v) 0
     24 #endif
     25 
     26 #ifdef __has_builtin
     27 # define __fortify_has_builtin(v) __has_builtin(v)
     28 #else
     29 # define __fortify_has_builtin(v) 0
     30 #endif
     31 
     32 #ifdef __clang__
     33 
     34 #if _FORTIFY_SOURCE > 2 && __fortify_has_attribute(pass_dynamic_object_size)
     35 #define _FORTIFY_POSN(n) const __attribute__((pass_dynamic_object_size(n)))
     36 #else
     37 /* clang uses overloads; see https://github.com/llvm/llvm-project/issues/53516 */
     38 #define _FORTIFY_POSN(n) const __attribute__((pass_object_size(n)))
     39 #endif
     40 
     41 /* we can't use extern inline with overloads without making them external */
     42 #ifdef __cplusplus
     43 #define _FORTIFY_INLINE __inline__ \
     44         __attribute__((__always_inline__,__artificial__,__overloadable__))
     45 #else
     46 #define _FORTIFY_INLINE static __inline__ \
     47 	__attribute__((__always_inline__,__artificial__,__overloadable__))
     48 #endif
     49 
     50 #else /* !__clang__ */
     51 
     52 #define _FORTIFY_POSN(n)
     53 #define _FORTIFY_INLINE extern __inline__ \
     54 	__attribute__((__always_inline__,__gnu_inline__,__artificial__))
     55 
     56 #endif /* __clang__ */
     57 
     58 #define _FORTIFY_POS0 _FORTIFY_POSN(0)
     59 #define _FORTIFY_POS1 _FORTIFY_POSN(1)
     60 #define _FORTIFY_POS2 _FORTIFY_POSN(2)
     61 
     62 #define _FORTIFY_STR(s) #s
     63 #define _FORTIFY_ORIG(p,fn) __typeof__(fn) __orig_##fn __asm__(_FORTIFY_STR(p) #fn)
     64 #define _FORTIFY_FNB(fn) _FORTIFY_ORIG(__USER_LABEL_PREFIX__,fn)
     65 #define _FORTIFY_FN(fn) _FORTIFY_FNB(fn); _FORTIFY_INLINE
     66 
     67 
     68 #if _FORTIFY_SOURCE > 2 && __fortify_has_builtin(__builtin_dynamic_object_size)
     69 #define __bos(ptr, type) __builtin_dynamic_object_size (ptr, type)
     70 #else
     71 #define __bos(ptr, type) __builtin_object_size (ptr, type)
     72 #endif
     73 
     74 #if __fortify_has_attribute(access)
     75 #define __fortify_access(...) __attribute__ ((access (__VA_ARGS__)))
     76 #else
     77 #define __fortify_access(...)
     78 #endif
     79 
     80 #if __fortify_has_attribute(format)
     81 #define __fortify__format(...) __attribute__ ((format (__VA_ARGS__)))
     82 #else
     83 #define __fortify__format(...)
     84 #endif
     85 
     86 #if __fortify_has_attribute(__diagnose_if)
     87 #define __fortify_warning_if(cond, msg) __attribute__ ((__diagnose_if (cond, msg, "warning")))
     88 #else
     89 #define __fortify_warning_if(cond, msg)
     90 #endif
     91 
     92 #endif