1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_BASE_LOGGING_H_
6 #define V8_BASE_LOGGING_H_
7
8 #include <cstring>
9 #include <sstream>
10 #include <string>
11
12 #include "src/base/base-export.h"
13 #include "src/base/build_config.h"
14 #include "src/base/compiler-specific.h"
15 #include "src/base/template-utils.h"
16
17 V8_BASE_EXPORT V8_NOINLINE void V8_Dcheck(const char* file, int line,
18 const char* message);
19
20 #ifdef DEBUG
21 // In debug, include file, line, and full error message for all
22 // FATAL() calls.
23 [[noreturn]] PRINTF_FORMAT(3, 4) V8_BASE_EXPORT V8_NOINLINE
24 void V8_Fatal(const char* file, int line, const char* format, ...);
25 #define FATAL(...) V8_Fatal(__FILE__, __LINE__, __VA_ARGS__)
26
27 #elif !defined(OFFICIAL_BUILD)
28 // In non-official release, include full error message, but drop file & line
29 // numbers. It saves binary size to drop the |file| & |line| as opposed to just
30 // passing in "", 0 for them.
31 [[noreturn]] PRINTF_FORMAT(1, 2) V8_BASE_EXPORT V8_NOINLINE
32 void V8_Fatal(const char* format, ...);
33 #define FATAL(...) V8_Fatal(__VA_ARGS__)
34 #else
35 // In official builds, include only messages that contain parameters because
36 // single-message errors can always be derived from stack traces.
37 [[noreturn]] V8_BASE_EXPORT V8_NOINLINE void V8_FatalNoContext();
38 [[noreturn]] PRINTF_FORMAT(1, 2) V8_BASE_EXPORT V8_NOINLINE
39 void V8_Fatal(const char* format, ...);
40 // FATAL(msg) -> V8_FatalNoContext()
41 // FATAL(msg, ...) -> V8_Fatal()
42 #define FATAL_HELPER(_7, _6, _5, _4, _3, _2, _1, _0, ...) _0
43 #define FATAL_DISCARD_ARG(arg) V8_FatalNoContext()
44 #define FATAL(...) \
45 FATAL_HELPER(__VA_ARGS__, V8_Fatal, V8_Fatal, V8_Fatal, V8_Fatal, V8_Fatal, \
46 V8_Fatal, V8_Fatal, FATAL_DISCARD_ARG) \
47 (__VA_ARGS__)
48 #endif
49
50 #define UNIMPLEMENTED() FATAL("unimplemented code")
51 #define UNREACHABLE() FATAL("unreachable code")
52
53 namespace v8 {
54 namespace base {
55
56 // Overwrite the default function that prints a stack trace.
57 V8_BASE_EXPORT void SetPrintStackTrace(void (*print_stack_trace_)());
58
59 // Override the default function that handles DCHECKs.
60 V8_BASE_EXPORT void SetDcheckFunction(void (*dcheck_Function)(const char*, int,
61 const char*));
62
63 // In official builds, assume all check failures can be debugged given just the
64 // stack trace.
65 #if !defined(DEBUG) && defined(OFFICIAL_BUILD)
66 #define CHECK_FAILED_HANDLER(message) FATAL("ignored")
67 #else
68 #define CHECK_FAILED_HANDLER(message) FATAL("Check failed: %s.", message)
69 #endif
70
71 // CHECK dies with a fatal error if condition is not true. It is *not*
72 // controlled by DEBUG, so the check will be executed regardless of
73 // compilation mode.
74 //
75 // We make sure CHECK et al. always evaluates their arguments, as
76 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
77 #define CHECK_WITH_MSG(condition, message) \
78 do { \
79 if (V8_UNLIKELY(!(condition))) { \
80 CHECK_FAILED_HANDLER(message); \
81 } \
82 } while (false)
83 #define CHECK(condition) CHECK_WITH_MSG(condition, #condition)
84
85 #ifdef DEBUG
86
87 #define DCHECK_WITH_MSG(condition, message) \
88 do { \
89 if (V8_UNLIKELY(!(condition))) { \
90 V8_Dcheck(__FILE__, __LINE__, message); \
91 } \
92 } while (false)
93 #define DCHECK(condition) DCHECK_WITH_MSG(condition, #condition)
94
95 // Helper macro for binary operators.
96 // Don't use this macro directly in your code, use CHECK_EQ et al below.
97 #define CHECK_OP(name, op, lhs, rhs) \
98 do { \
99 if (std::string* _msg = ::v8::base::Check##name##Impl< \
100 typename ::v8::base::pass_value_or_ref<decltype(lhs)>::type, \
101 typename ::v8::base::pass_value_or_ref<decltype(rhs)>::type>( \
102 (lhs), (rhs), #lhs " " #op " " #rhs)) { \
103 FATAL("Check failed: %s.", _msg->c_str()); \
104 delete _msg; \
105 } \
106 } while (false)
107
108 #define DCHECK_OP(name, op, lhs, rhs) \
109 do { \
110 if (std::string* _msg = ::v8::base::Check##name##Impl< \
111 typename ::v8::base::pass_value_or_ref<decltype(lhs)>::type, \
112 typename ::v8::base::pass_value_or_ref<decltype(rhs)>::type>( \
113 (lhs), (rhs), #lhs " " #op " " #rhs)) { \
114 V8_Dcheck(__FILE__, __LINE__, _msg->c_str()); \
115 delete _msg; \
116 } \
117 } while (false)
118
119 #else
120
121 // Make all CHECK functions discard their log strings to reduce code
122 // bloat for official release builds.
123
124 #define CHECK_OP(name, op, lhs, rhs) \
125 do { \
126 bool _cmp = ::v8::base::Cmp##name##Impl< \
127 typename ::v8::base::pass_value_or_ref<decltype(lhs)>::type, \
128 typename ::v8::base::pass_value_or_ref<decltype(rhs)>::type>((lhs), \
129 (rhs)); \
130 CHECK_WITH_MSG(_cmp, #lhs " " #op " " #rhs); \
131 } while (false)
132
133 #define DCHECK_WITH_MSG(condition, msg) void(0);
134
135 #endif
136
137 #if V8_HAS_CXX14_CONSTEXPR
138 #define CONSTEXPR_DCHECK(cond) DCHECK(cond)
139 #else
140 #define CONSTEXPR_DCHECK(cond)
141 #endif
142
143 // Define PrintCheckOperand<T> for each T which defines operator<< for ostream.
144 template <typename T>
145 typename std::enable_if<
146 !std::is_function<typename std::remove_pointer<T>::type>::value &&
147 has_output_operator<T>::value,
148 std::string>::type
PrintCheckOperand(T val)149 PrintCheckOperand(T val) {
150 std::ostringstream oss;
151 oss << std::forward<T>(val);
152 return oss.str();
153 }
154
155 // Provide an overload for functions and function pointers. Function pointers
156 // don't implicitly convert to void* but do implicitly convert to bool, so
157 // without this function pointers are always printed as 1 or 0. (MSVC isn't
158 // standards-conforming here and converts function pointers to regular
159 // pointers, so this is a no-op for MSVC.)
160 template <typename T>
161 typename std::enable_if<
162 std::is_function<typename std::remove_pointer<T>::type>::value,
163 std::string>::type
PrintCheckOperand(T val)164 PrintCheckOperand(T val) {
165 return PrintCheckOperand(reinterpret_cast<const void*>(val));
166 }
167
168 // Define PrintCheckOperand<T> for enums which have no operator<<.
169 template <typename T>
170 typename std::enable_if<
171 std::is_enum<T>::value && !has_output_operator<T>::value, std::string>::type
PrintCheckOperand(T val)172 PrintCheckOperand(T val) {
173 using underlying_t = typename std::underlying_type<T>::type;
174 // 8-bit types are not printed as number, so extend them to 16 bit.
175 using int_t = typename std::conditional<
176 std::is_same<underlying_t, uint8_t>::value, uint16_t,
177 typename std::conditional<std::is_same<underlying_t, int8_t>::value,
178 int16_t, underlying_t>::type>::type;
179 return PrintCheckOperand(static_cast<int_t>(static_cast<underlying_t>(val)));
180 }
181
182 // Define default PrintCheckOperand<T> for non-printable types.
183 template <typename T>
184 typename std::enable_if<!has_output_operator<T>::value &&
185 !std::is_enum<T>::value,
186 std::string>::type
PrintCheckOperand(T val)187 PrintCheckOperand(T val) {
188 return "<unprintable>";
189 }
190
191 // Define specializations for character types, defined in logging.cc.
192 #define DEFINE_PRINT_CHECK_OPERAND_CHAR(type) \
193 template <> \
194 V8_BASE_EXPORT std::string PrintCheckOperand<type>(type ch); \
195 template <> \
196 V8_BASE_EXPORT std::string PrintCheckOperand<type*>(type * cstr); \
197 template <> \
198 V8_BASE_EXPORT std::string PrintCheckOperand<const type*>(const type* cstr);
199
200 DEFINE_PRINT_CHECK_OPERAND_CHAR(char)
DEFINE_PRINT_CHECK_OPERAND_CHAR(signed char)201 DEFINE_PRINT_CHECK_OPERAND_CHAR(signed char)
202 DEFINE_PRINT_CHECK_OPERAND_CHAR(unsigned char)
203 #undef DEFINE_PRINT_CHECK_OPERAND_CHAR
204
205 // Build the error message string. This is separate from the "Impl"
206 // function template because it is not performance critical and so can
207 // be out of line, while the "Impl" code should be inline. Caller
208 // takes ownership of the returned string.
209 template <typename Lhs, typename Rhs>
210 V8_NOINLINE std::string* MakeCheckOpString(Lhs lhs, Rhs rhs, char const* msg) {
211 std::string lhs_str = PrintCheckOperand<Lhs>(lhs);
212 std::string rhs_str = PrintCheckOperand<Rhs>(rhs);
213 std::ostringstream ss;
214 ss << msg;
215 constexpr size_t kMaxInlineLength = 50;
216 if (lhs_str.size() <= kMaxInlineLength &&
217 rhs_str.size() <= kMaxInlineLength) {
218 ss << " (" << lhs_str << " vs. " << rhs_str << ")";
219 } else {
220 ss << "\n " << lhs_str << "\n vs.\n " << rhs_str << "\n";
221 }
222 return new std::string(ss.str());
223 }
224
225 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
226 // in logging.cc.
227 #define EXPLICIT_CHECK_OP_INSTANTIATION(type) \
228 extern template V8_BASE_EXPORT std::string* MakeCheckOpString<type, type>( \
229 type, type, char const*); \
230 extern template V8_BASE_EXPORT std::string PrintCheckOperand<type>(type);
231
232 EXPLICIT_CHECK_OP_INSTANTIATION(int)
233 EXPLICIT_CHECK_OP_INSTANTIATION(long) // NOLINT(runtime/int)
234 EXPLICIT_CHECK_OP_INSTANTIATION(long long) // NOLINT(runtime/int)
235 EXPLICIT_CHECK_OP_INSTANTIATION(unsigned int)
236 EXPLICIT_CHECK_OP_INSTANTIATION(unsigned long) // NOLINT(runtime/int)
237 EXPLICIT_CHECK_OP_INSTANTIATION(unsigned long long) // NOLINT(runtime/int)
238 EXPLICIT_CHECK_OP_INSTANTIATION(void const*)
239 #undef EXPLICIT_CHECK_OP_INSTANTIATION
240
241 // comparison_underlying_type provides the underlying integral type of an enum,
242 // or std::decay<T>::type if T is not an enum. Booleans are converted to
243 // "unsigned int", to allow "unsigned int == bool" comparisons.
244 template <typename T>
245 struct comparison_underlying_type {
246 // std::underlying_type must only be used with enum types, thus use this
247 // {Dummy} type if the given type is not an enum.
248 enum Dummy {};
249 using decay = typename std::decay<T>::type;
250 static constexpr bool is_enum = std::is_enum<decay>::value;
251 using underlying = typename std::underlying_type<
252 typename std::conditional<is_enum, decay, Dummy>::type>::type;
253 using type_or_bool =
254 typename std::conditional<is_enum, underlying, decay>::type;
255 using type =
256 typename std::conditional<std::is_same<type_or_bool, bool>::value,
257 unsigned int, type_or_bool>::type;
258 };
259 // Cast a value to its underlying type
260 #define MAKE_UNDERLYING(Type, value) \
261 static_cast<typename comparison_underlying_type<Type>::type>(value)
262
263 // is_signed_vs_unsigned::value is true if both types are integral, Lhs is
264 // signed, and Rhs is unsigned. False in all other cases.
265 template <typename Lhs, typename Rhs>
266 struct is_signed_vs_unsigned {
267 using lhs_underlying = typename comparison_underlying_type<Lhs>::type;
268 using rhs_underlying = typename comparison_underlying_type<Rhs>::type;
269 static constexpr bool value = std::is_integral<lhs_underlying>::value &&
270 std::is_integral<rhs_underlying>::value &&
271 std::is_signed<lhs_underlying>::value &&
272 std::is_unsigned<rhs_underlying>::value;
273 };
274 // Same thing, other way around: Lhs is unsigned, Rhs signed.
275 template <typename Lhs, typename Rhs>
276 struct is_unsigned_vs_signed : public is_signed_vs_unsigned<Rhs, Lhs> {};
277
278 // Specialize the compare functions for signed vs. unsigned comparisons.
279 // std::enable_if ensures that this template is only instantiable if both Lhs
280 // and Rhs are integral types, and their signedness does not match.
281 #define MAKE_UNSIGNED(Type, value) \
282 static_cast<typename std::make_unsigned< \
283 typename comparison_underlying_type<Type>::type>::type>(value)
284 #define DEFINE_SIGNED_MISMATCH_COMP(CHECK, NAME, IMPL) \
285 template <typename Lhs, typename Rhs> \
286 V8_INLINE constexpr \
287 typename std::enable_if<CHECK<Lhs, Rhs>::value, bool>::type \
288 Cmp##NAME##Impl(Lhs lhs, Rhs rhs) { \
289 return IMPL; \
290 }
291 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, EQ,
292 lhs >= 0 && MAKE_UNSIGNED(Lhs, lhs) ==
293 MAKE_UNDERLYING(Rhs, rhs))
294 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LT,
295 lhs < 0 || MAKE_UNSIGNED(Lhs, lhs) <
296 MAKE_UNDERLYING(Rhs, rhs))
297 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LE,
298 lhs <= 0 || MAKE_UNSIGNED(Lhs, lhs) <=
299 MAKE_UNDERLYING(Rhs, rhs))
300 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, NE, !CmpEQImpl(lhs, rhs))
301 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GT, !CmpLEImpl(lhs, rhs))
302 DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GE, !CmpLTImpl(lhs, rhs))
303 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, EQ, CmpEQImpl(rhs, lhs))
304 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, NE, CmpNEImpl(rhs, lhs))
305 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LT, CmpGTImpl(rhs, lhs))
306 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LE, CmpGEImpl(rhs, lhs))
307 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GT, CmpLTImpl(rhs, lhs))
308 DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GE, CmpLEImpl(rhs, lhs))
309 #undef MAKE_UNSIGNED
310 #undef DEFINE_SIGNED_MISMATCH_COMP
311
312 // Helper functions for CHECK_OP macro.
313 // The (float, float) and (double, double) instantiations are explicitly
314 // externalized to ensure proper 32/64-bit comparisons on x86.
315 // The Cmp##NAME##Impl function is only instantiable if one of the two types is
316 // not integral or their signedness matches (i.e. whenever no specialization is
317 // required, see above). Otherwise it is disabled by the enable_if construct,
318 // and the compiler will pick a specialization from above.
319 #define DEFINE_CHECK_OP_IMPL(NAME, op) \
320 template <typename Lhs, typename Rhs> \
321 V8_INLINE constexpr \
322 typename std::enable_if<!is_signed_vs_unsigned<Lhs, Rhs>::value && \
323 !is_unsigned_vs_signed<Lhs, Rhs>::value, \
324 bool>::type Cmp##NAME##Impl(Lhs lhs, Rhs rhs) { \
325 return lhs op rhs; \
326 } \
327 template <typename Lhs, typename Rhs> \
328 V8_INLINE constexpr std::string* Check##NAME##Impl(Lhs lhs, Rhs rhs, \
329 char const* msg) { \
330 using LhsPassT = typename pass_value_or_ref<Lhs>::type; \
331 using RhsPassT = typename pass_value_or_ref<Rhs>::type; \
332 bool cmp = Cmp##NAME##Impl<LhsPassT, RhsPassT>(lhs, rhs); \
333 return V8_LIKELY(cmp) \
334 ? nullptr \
335 : MakeCheckOpString<LhsPassT, RhsPassT>(lhs, rhs, msg); \
336 }
337 DEFINE_CHECK_OP_IMPL(EQ, ==)
338 DEFINE_CHECK_OP_IMPL(NE, !=)
339 DEFINE_CHECK_OP_IMPL(LE, <=)
340 DEFINE_CHECK_OP_IMPL(LT, < )
341 DEFINE_CHECK_OP_IMPL(GE, >=)
342 DEFINE_CHECK_OP_IMPL(GT, > )
343 #undef DEFINE_CHECK_OP_IMPL
344
345 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs)
346 #define CHECK_NE(lhs, rhs) CHECK_OP(NE, !=, lhs, rhs)
347 #define CHECK_LE(lhs, rhs) CHECK_OP(LE, <=, lhs, rhs)
348 #define CHECK_LT(lhs, rhs) CHECK_OP(LT, <, lhs, rhs)
349 #define CHECK_GE(lhs, rhs) CHECK_OP(GE, >=, lhs, rhs)
350 #define CHECK_GT(lhs, rhs) CHECK_OP(GT, >, lhs, rhs)
351 #define CHECK_NULL(val) CHECK((val) == nullptr)
352 #define CHECK_NOT_NULL(val) CHECK((val) != nullptr)
353 #define CHECK_IMPLIES(lhs, rhs) \
354 CHECK_WITH_MSG(!(lhs) || (rhs), #lhs " implies " #rhs)
355
356 } // namespace base
357 } // namespace v8
358
359
360 // The DCHECK macro is equivalent to CHECK except that it only
361 // generates code in debug builds.
362 #ifdef DEBUG
363 #define DCHECK_EQ(lhs, rhs) DCHECK_OP(EQ, ==, lhs, rhs)
364 #define DCHECK_NE(lhs, rhs) DCHECK_OP(NE, !=, lhs, rhs)
365 #define DCHECK_GT(lhs, rhs) DCHECK_OP(GT, >, lhs, rhs)
366 #define DCHECK_GE(lhs, rhs) DCHECK_OP(GE, >=, lhs, rhs)
367 #define DCHECK_LT(lhs, rhs) DCHECK_OP(LT, <, lhs, rhs)
368 #define DCHECK_LE(lhs, rhs) DCHECK_OP(LE, <=, lhs, rhs)
369 #define DCHECK_NULL(val) DCHECK((val) == nullptr)
370 #define DCHECK_NOT_NULL(val) DCHECK((val) != nullptr)
371 #define DCHECK_IMPLIES(lhs, rhs) \
372 DCHECK_WITH_MSG(!(lhs) || (rhs), #lhs " implies " #rhs)
373 #else
374 #define DCHECK(condition) ((void) 0)
375 #define DCHECK_EQ(v1, v2) ((void) 0)
376 #define DCHECK_NE(v1, v2) ((void) 0)
377 #define DCHECK_GT(v1, v2) ((void) 0)
378 #define DCHECK_GE(v1, v2) ((void) 0)
379 #define DCHECK_LT(v1, v2) ((void) 0)
380 #define DCHECK_LE(v1, v2) ((void) 0)
381 #define DCHECK_NULL(val) ((void) 0)
382 #define DCHECK_NOT_NULL(val) ((void) 0)
383 #define DCHECK_IMPLIES(v1, v2) ((void) 0)
384 #endif
385
386 #endif // V8_BASE_LOGGING_H_
387