xref: /xnu-8792.61.2/EXTERNAL_HEADERS/corecrypto/cc_macros.h (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1 /* Copyright (c) (2012,2015,2016,2017,2019) Apple Inc. All rights reserved.
2  *
3  * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4  * is contained in the License.txt file distributed with corecrypto) and only to
5  * people who accept that license. IMPORTANT:  Any license rights granted to you by
6  * Apple Inc. (if any) are limited to internal use within your organization only on
7  * devices and computers you own or control, for the sole purpose of verifying the
8  * security characteristics and correct functioning of the Apple Software.  You may
9  * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10  */
11 
12 #ifndef _CORECRYPTO_CC_MACROS_H_
13 #define _CORECRYPTO_CC_MACROS_H_
14 
15 #include <corecrypto/cc_config.h>
16 
17 #ifndef __CC_DEBUG_ASSERT_COMPONENT_NAME_STRING
18 #define __CC_DEBUG_ASSERT_COMPONENT_NAME_STRING ""
19 #endif
20 
21 #ifndef __CC_DEBUG_ASSERT_PRODUCTION_CODE
22 #define __CC_DEBUG_ASSERT_PRODUCTION_CODE !CORECRYPTO_DEBUG
23 #endif
24 
25 #if CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS
26 
27 #if !CC_KERNEL
28     #include <string.h> // for strstr
29 #endif // !CC_KERNEL
30 
cc_strstr(const char * file)31 CC_UNUSED static char *cc_strstr(const char *file) {
32 #if CC_KERNEL
33     (void) file;
34 #else
35     const char cc_char []="corecrypto";
36     char *p=strstr(file, cc_char);
37     if (p) return (p+strlen(cc_char)+1);
38 #endif
39     return NULL;
40 }
41 
42 #define __CC_DEBUG_REQUIRE_MESSAGE(name, assertion, label, message, file, line, value) \
43 {char *___t = cc_strstr(file); cc_printf( "require: %s, %s%s:%d\n", assertion, (message!=0) ? message : "", ___t==NULL?file:___t, line);}
44 
45 #endif // CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS
46 
47 #ifndef cc_require
48 #if (__CC_DEBUG_ASSERT_PRODUCTION_CODE) || (!CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS)
49   #if defined(_WIN32) && defined (__clang__)
50     #define cc_require(assertion, exceptionLabel) \
51        do { \
52            if (!(assertion) ) { \
53               goto exceptionLabel; \
54            } \
55         } while ( 0 )
56   #else
57     #define cc_require(assertion, exceptionLabel) \
58         do { \
59             if ( __builtin_expect(!(assertion), 0) ) { \
60                 goto exceptionLabel; \
61             } \
62         } while ( 0 )
63  #endif
64 #else
65     #define cc_require(assertion, exceptionLabel) \
66         do { \
67             if ( __builtin_expect(!(assertion), 0) ) { \
68                 __CC_DEBUG_REQUIRE_MESSAGE(__CC_DEBUG_ASSERT_COMPONENT_NAME_STRING, \
69                     #assertion, #exceptionLabel, 0, __FILE__, __LINE__,  0); \
70                 goto exceptionLabel; \
71             } \
72         } while ( 0 )
73 #endif
74 #endif
75 
76 #ifndef cc_require_action
77 #if __CC_DEBUG_ASSERT_PRODUCTION_CODE || (!CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS)
78   #if defined(_WIN32) && defined(__clang__)
79     #define cc_require_action(assertion, exceptionLabel, action)                \
80         do                                                                      \
81         {                                                                       \
82             if (!(assertion))                                                   \
83             {                                                                   \
84                 {                                                               \
85                     action;                                                     \
86                 }                                                               \
87                 goto exceptionLabel;                                            \
88             }                                                                   \
89         } while ( 0 )
90   #else
91     #define cc_require_action(assertion, exceptionLabel, action)                \
92         do                                                                      \
93         {                                                                       \
94             if ( __builtin_expect(!(assertion), 0) )                            \
95             {                                                                   \
96                 {                                                               \
97                     action;                                                     \
98                 }                                                               \
99                 goto exceptionLabel;                                            \
100             }                                                                   \
101         } while ( 0 )
102   #endif
103 #else
104     #define cc_require_action(assertion, exceptionLabel, action)                \
105         do                                                                      \
106         {                                                                       \
107             if ( __builtin_expect(!(assertion), 0) )                            \
108             {                                                                   \
109                 __CC_DEBUG_REQUIRE_MESSAGE(                                      \
110                     __CC_DEBUG_ASSERT_COMPONENT_NAME_STRING,                    \
111                     #assertion, #exceptionLabel, 0,   __FILE__, __LINE__, 0);   \
112                 {                                                               \
113                     action;                                                     \
114                 }                                                               \
115                 goto exceptionLabel;                                            \
116             }                                                                   \
117         } while ( 0 )
118 #endif
119 #endif
120 
121 #ifndef cc_require_or_return
122 #if (__CC_DEBUG_ASSERT_PRODUCTION_CODE) || (!CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS)
123   #if defined(_WIN32) && defined (__clang__)
124     #define cc_require_or_return(assertion, value)                                  \
125        do {                                                                         \
126            if (!(assertion) ) {                                                     \
127               return value;                                                         \
128            }                                                                        \
129         } while ( 0 )
130   #else
131     #define cc_require_or_return(assertion, value)                                  \
132         do {                                                                        \
133             if ( __builtin_expect(!(assertion), 0) ) {                              \
134                 return value;                                                       \
135             }                                                                       \
136         } while ( 0 )
137  #endif
138 #else
139     #define cc_require_or_return(assertion, value)                                  \
140         do {                                                                        \
141             if ( __builtin_expect(!(assertion), 0) ) {                              \
142                 __CC_DEBUG_REQUIRE_MESSAGE(__CC_DEBUG_ASSERT_COMPONENT_NAME_STRING, \
143                     #assertion, #exceptionLabel, 0, __FILE__, __LINE__,  0);        \
144                 return value;                                                       \
145             }                                                                       \
146         } while ( 0 )
147 #endif
148 #endif
149 
150 #endif /* _CORECRYPTO_CC_MACROS_H_ */
151