Debian Codegraph @

crt/aws-c-common/include/aws/common/assert.h

raw symbols SCIP
1 #ifndef AWS_COMMON_ASSERT_H
2 #define AWS_COMMON_ASSERT_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/exports.h>
10 #include <aws/common/macros.h>
11 #include <stdio.h>
12
13 AWS_PUSH_SANE_WARNING_LEVEL
14 AWS_EXTERN_C_BEGIN
15
16 AWS_COMMON_API
17 AWS_DECLSPEC_NORETURN
18 void aws_fatal_assert(const char *cond_str, const char *file, int line) AWS_ATTRIBUTE_NORETURN;
19
20 AWS_EXTERN_C_END
21
22 #if defined(CBMC)
23 # define AWS_PANIC_OOM(mem, msg) \
24 do { \
25 if (!(mem)) { \
26 fprintf(stderr, "%s: %s, line %d", msg, __FILE__, __LINE__); \
27 exit(-1); \
28 } \
29 } while (0)
30 #else
31 # define AWS_PANIC_OOM(mem, msg) \
32 do { \
33 if (!(mem)) { \
34 fprintf(stderr, "%s", msg); \
35 abort(); \
36 } \
37 } while (0)
38 #endif /* defined(CBMC) */
39
40 #if defined(CBMC)
41 # define AWS_ASSUME(cond) __CPROVER_assume(cond)
42 #elif defined(_MSC_VER)
43 # define AWS_ASSUME(cond) __assume(cond)
44 # define AWS_UNREACHABLE() __assume(0)
45 #elif defined(__clang__)
46 # define AWS_ASSUME(cond) \
47 do { \
48 bool _result = (cond); \
49 __builtin_assume(_result); \
50 } while (false)
51 # define AWS_UNREACHABLE() __builtin_unreachable()
52 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
53 # define AWS_ASSUME(cond) ((cond) ? (void)0 : __builtin_unreachable())
54 # define AWS_UNREACHABLE() __builtin_unreachable()
55 #else
56 # define AWS_ASSUME(cond)
57 # define AWS_UNREACHABLE()
58 #endif
59
60 #if defined(CBMC)
61 # include <assert.h>
62 # define AWS_ASSERT(cond) assert(cond)
63 #elif defined(DEBUG_BUILD) || defined(__clang_analyzer__)
64 # define AWS_ASSERT(cond) AWS_FATAL_ASSERT(cond)
65 #else
66 # define AWS_ASSERT(cond)
67 #endif /* defined(CBMC) */
68
69 #if defined(CBMC)
70 # define AWS_FATAL_ASSERT(cond) AWS_ASSERT(cond)
71 #elif defined(__clang_analyzer__)
72 # define AWS_FATAL_ASSERT(cond) \
73 if (!(cond)) { \
74 abort(); \
75 }
76 #else
77 # if defined(_MSC_VER)
78 # define AWS_FATAL_ASSERT(cond) \
79 __pragma(warning(push)) __pragma(warning(disable : 4127)) /* conditional expression is constant */ \
80 if (!(cond)) { \
81 aws_fatal_assert(#cond, __FILE__, __LINE__); \
82 } \
83 __pragma(warning(pop))
84 # else
85 # define AWS_FATAL_ASSERT(cond) \
86 do { \
87 if (!(cond)) { \
88 aws_fatal_assert(#cond, __FILE__, __LINE__); \
89 } \
90 } while (0)
91 # endif /* defined(_MSC_VER) */
92 #endif /* defined(CBMC) */
93
94 /**
95 * Define function contracts.
96 * When the code is being verified using CBMC these contracts are formally verified;
97 * When the code is built in debug mode, they are checked as much as possible using assertions
98 * When the code is built in production mode, non-fatal contracts are not checked.
99 * Violations of the function contracts are undefined behaviour.
100 */
101 #ifdef CBMC
102 // clang-format off
103 // disable clang format, since it likes to break formatting of stringize macro.
104 // seems to be fixed in v15 plus, but we are not ready to update to it yet
105 # define AWS_PRECONDITION2(cond, explanation) __CPROVER_precondition((cond), (explanation))
106 # define AWS_PRECONDITION1(cond) __CPROVER_precondition((cond), #cond " check failed")
107 # define AWS_FATAL_PRECONDITION2(cond, explanation) __CPROVER_precondition((cond), (explanation))
108 # define AWS_FATAL_PRECONDITION1(cond) __CPROVER_precondition((cond), #cond " check failed")
109 # define AWS_POSTCONDITION2(cond, explanation) __CPROVER_assert((cond), (explanation))
110 # define AWS_POSTCONDITION1(cond) __CPROVER_assert((cond), #cond " check failed")
111 # define AWS_FATAL_POSTCONDITION2(cond, explanation) __CPROVER_assert((cond), (explanation))
112 # define AWS_FATAL_POSTCONDITION1(cond) __CPROVER_assert((cond), #cond " check failed")
113 # define AWS_MEM_IS_READABLE_CHECK(base, len) (((len) == 0) || (__CPROVER_r_ok((base), (len))))
114 # define AWS_MEM_IS_WRITABLE_CHECK(base, len) (((len) == 0) || (__CPROVER_r_ok((base), (len))))
115 // clang-format on
116 #else
117 # define AWS_PRECONDITION2(cond, expl) AWS_ASSERT(cond)
118 # define AWS_PRECONDITION1(cond) AWS_ASSERT(cond)
119 # define AWS_FATAL_PRECONDITION2(cond, expl) AWS_FATAL_ASSERT(cond)
120 # define AWS_FATAL_PRECONDITION1(cond) AWS_FATAL_ASSERT(cond)
121 # define AWS_POSTCONDITION2(cond, expl) AWS_ASSERT(cond)
122 # define AWS_POSTCONDITION1(cond) AWS_ASSERT(cond)
123 # define AWS_FATAL_POSTCONDITION2(cond, expl) AWS_FATAL_ASSERT(cond)
124 # define AWS_FATAL_POSTCONDITION1(cond) AWS_FATAL_ASSERT(cond)
125 /**
126 * These macros should not be used in is_valid functions.
127 * All validate functions are also used in assumptions for CBMC proofs,
128 * which should not contain __CPROVER_*_ok primitives. The use of these primitives
129 * in assumptions may lead to spurious results.
130 * The C runtime does not give a way to check these properties,
131 * but we can at least check that the pointer is valid. */
132 # define AWS_MEM_IS_READABLE_CHECK(base, len) (((len) == 0) || (base))
133 # define AWS_MEM_IS_WRITABLE_CHECK(base, len) (((len) == 0) || (base))
134 #endif /* CBMC */
135
136 /**
137 * These macros can safely be used in validate functions.
138 */
139 #define AWS_MEM_IS_READABLE(base, len) (((len) == 0) || (base))
140 #define AWS_MEM_IS_WRITABLE(base, len) (((len) == 0) || (base))
141
142 /* Logical consequence. */
143 #define AWS_IMPLIES(a, b) (!(a) || (b))
144
145 /**
146 * If and only if (iff) is a biconditional logical connective between statements a and b.
147 * We need double negations (!!) here to work correctly for non-Boolean a and b values.
148 * Equivalent to (AWS_IMPLIES(a, b) && AWS_IMPLIES(b, a)).
149 */
150 #define AWS_IFF(a, b) (!!(a) == !!(b))
151
152 #define AWS_RETURN_ERROR_IF_IMPL(type, cond, err, explanation) \
153 do { \
154 if (!(cond)) { \
155 return aws_raise_error(err); \
156 } \
157 } while (0)
158
159 #define AWS_RETURN_ERROR_IF3(cond, err, explanation) AWS_RETURN_ERROR_IF_IMPL("InternalCheck", cond, err, explanation)
160 #define AWS_RETURN_ERROR_IF2(cond, err) AWS_RETURN_ERROR_IF3(cond, err, #cond " check failed")
161 #define AWS_RETURN_ERROR_IF(...) CALL_OVERLOAD(AWS_RETURN_ERROR_IF, __VA_ARGS__)
162
163 #define AWS_ERROR_PRECONDITION3(cond, err, explanation) AWS_RETURN_ERROR_IF_IMPL("Precondition", cond, err, explanation)
164 #define AWS_ERROR_PRECONDITION2(cond, err) AWS_ERROR_PRECONDITION3(cond, err, #cond " check failed")
165 #define AWS_ERROR_PRECONDITION1(cond) AWS_ERROR_PRECONDITION2(cond, AWS_ERROR_INVALID_ARGUMENT)
166
167 #define AWS_ERROR_POSTCONDITION3(cond, err, explanation) \
168 AWS_RETURN_ERROR_IF_IMPL("Postcondition", cond, err, explanation)
169 #define AWS_ERROR_POSTCONDITION2(cond, err) AWS_ERROR_POSTCONDITION3(cond, err, #cond " check failed")
170 #define AWS_ERROR_POSTCONDITION1(cond) AWS_ERROR_POSTCONDITION2(cond, AWS_ERROR_INVALID_ARGUMENT)
171
172 // The UNUSED is used to silence the complains of GCC for zero arguments in variadic macro
173 #define AWS_PRECONDITION(...) CALL_OVERLOAD(AWS_PRECONDITION, __VA_ARGS__)
174 #define AWS_FATAL_PRECONDITION(...) CALL_OVERLOAD(AWS_FATAL_PRECONDITION, __VA_ARGS__)
175 #define AWS_POSTCONDITION(...) CALL_OVERLOAD(AWS_POSTCONDITION, __VA_ARGS__)
176 #define AWS_FATAL_POSTCONDITION(...) CALL_OVERLOAD(AWS_FATAL_POSTCONDITION, __VA_ARGS__)
177 #define AWS_ERROR_PRECONDITION(...) CALL_OVERLOAD(AWS_ERROR_PRECONDITION, __VA_ARGS__)
178 #define AWS_ERROR_POSTCONDITION(...) CALL_OVERLOAD(AWS_ERROR_PRECONDITION, __VA_ARGS__)
179
180 #define AWS_RETURN_WITH_POSTCONDITION(_rval, ...) \
181 do { \
182 AWS_POSTCONDITION(__VA_ARGS__); \
183 return _rval; \
184 } while (0)
185
186 #define AWS_SUCCEED_WITH_POSTCONDITION(...) AWS_RETURN_WITH_POSTCONDITION(AWS_OP_SUCCESS, __VA_ARGS__)
187
188 #define AWS_OBJECT_PTR_IS_READABLE(ptr) AWS_MEM_IS_READABLE((ptr), sizeof(*(ptr)))
189 #define AWS_OBJECT_PTR_IS_WRITABLE(ptr) AWS_MEM_IS_WRITABLE((ptr), sizeof(*(ptr)))
190
191 AWS_POP_SANE_WARNING_LEVEL
192
193 #endif /* AWS_COMMON_ASSERT_H */