1 /* $NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $ */
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
31 * The micro UBSan implementation for the userland (uUBSan) and kernel (kUBSan).
32 * The uBSSan versions is suitable for inclusion into libc or used standalone
35 * This file due to long symbol names generated by a compiler during the
36 * instrumentation process does not follow the KNF style with 80-column limit.
39 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 __KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
46 __RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
51 #include <sys/param.h>
52 #include <sys/types.h>
53 #include <sys/limits.h>
54 #include <sys/systm.h>
55 #include <machine/_inttypes.h>
56 #include <machine/stdarg.h>
57 #define ASSERT(x) KASSERT(x, ("%s: " __STRING(x) " failed", __func__))
58 #define __arraycount(x) nitems(x)
59 #define ISSET(x, y) ((x) & (y))
60 #define __BIT(x) ((uintmax_t)1 << (uintmax_t)(x))
61 #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
62 #define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
65 #include "namespace.h"
67 #include <sys/param.h>
82 #define ubsan_vsyslog vsyslog_ss
83 #define ASSERT(x) _DIAGASSERT(x)
85 #define ubsan_vsyslog vsyslog_r
86 #define ASSERT(x) assert(x)
88 /* These macros are available in _KERNEL only */
89 #define SET(t, f) ((t) |= (f))
90 #define ISSET(t, f) ((t) & (f))
91 #define CLR(t, f) ((t) &= ~(f))
94 #define REINTERPRET_CAST(__dt, __st) ((__dt)(__st))
95 #define STATIC_CAST(__dt, __st) ((__dt)(__st))
97 #define ACK_REPORTED __BIT(31)
99 #define MUL_STRING "*"
100 #define PLUS_STRING "+"
101 #define MINUS_STRING "-"
102 #define DIVREM_STRING "divrem"
106 #define CFI_DERIVEDCAST 2
107 #define CFI_UNRELATEDCAST 3
109 #define CFI_NVMFCALL 5
110 #define CFI_VMFCALL 6
112 #define NUMBER_MAXLEN 128
113 #define LOCATION_MAXLEN (PATH_MAX + 32 /* ':LINE:COLUMN' */)
121 #define WIDTH_128 128
123 #define NUMBER_SIGNED_BIT 1U
125 #if __SIZEOF_INT128__
126 typedef __int128 longest;
127 typedef unsigned __int128 ulongest;
129 typedef int64_t longest;
130 typedef uint64_t ulongest;
134 static int ubsan_flags = -1;
135 #define UBSAN_ABORT __BIT(0)
136 #define UBSAN_STDOUT __BIT(1)
137 #define UBSAN_STDERR __BIT(2)
138 #define UBSAN_SYSLOG __BIT(3)
141 /* Undefined Behavior specific defines and structures */
143 #define KIND_INTEGER 0
145 #define KIND_UNKNOWN UINT16_MAX
147 struct CSourceLocation {
153 struct CTypeDescriptor {
156 uint8_t mTypeName[1];
159 struct COverflowData {
160 struct CSourceLocation mLocation;
161 struct CTypeDescriptor *mType;
164 struct CUnreachableData {
165 struct CSourceLocation mLocation;
168 struct CCFICheckFailData {
170 struct CSourceLocation mLocation;
171 struct CTypeDescriptor *mType;
174 struct CDynamicTypeCacheMissData {
175 struct CSourceLocation mLocation;
176 struct CTypeDescriptor *mType;
178 uint8_t mTypeCheckKind;
181 struct CFunctionTypeMismatchData {
182 struct CSourceLocation mLocation;
183 struct CTypeDescriptor *mType;
186 struct CInvalidBuiltinData {
187 struct CSourceLocation mLocation;
191 struct CInvalidValueData {
192 struct CSourceLocation mLocation;
193 struct CTypeDescriptor *mType;
196 struct CNonNullArgData {
197 struct CSourceLocation mLocation;
198 struct CSourceLocation mAttributeLocation;
202 struct CNonNullReturnData {
203 struct CSourceLocation mAttributeLocation;
206 struct COutOfBoundsData {
207 struct CSourceLocation mLocation;
208 struct CTypeDescriptor *mArrayType;
209 struct CTypeDescriptor *mIndexType;
212 struct CPointerOverflowData {
213 struct CSourceLocation mLocation;
216 struct CShiftOutOfBoundsData {
217 struct CSourceLocation mLocation;
218 struct CTypeDescriptor *mLHSType;
219 struct CTypeDescriptor *mRHSType;
222 struct CTypeMismatchData {
223 struct CSourceLocation mLocation;
224 struct CTypeDescriptor *mType;
225 unsigned long mLogAlignment;
226 uint8_t mTypeCheckKind;
229 struct CTypeMismatchData_v1 {
230 struct CSourceLocation mLocation;
231 struct CTypeDescriptor *mType;
232 uint8_t mLogAlignment;
233 uint8_t mTypeCheckKind;
236 struct CVLABoundData {
237 struct CSourceLocation mLocation;
238 struct CTypeDescriptor *mType;
241 struct CFloatCastOverflowData {
242 struct CSourceLocation mLocation; /* This field exists in this struct since 2015 August 11th */
243 struct CTypeDescriptor *mFromType;
244 struct CTypeDescriptor *mToType;
247 /* Local utility functions */
248 static void Report(bool isFatal, const char *pFormat, ...) __printflike(2, 3);
249 static bool isAlreadyReported(struct CSourceLocation *pLocation);
250 static size_t zDeserializeTypeWidth(struct CTypeDescriptor *pType);
251 static void DeserializeLocation(char *pBuffer, size_t zBUfferLength, struct CSourceLocation *pLocation);
252 #ifdef __SIZEOF_INT128__
253 static void DeserializeUINT128(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, __uint128_t U128);
255 static void DeserializeNumberSigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, longest L);
256 static void DeserializeNumberUnsigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, ulongest L);
258 static void DeserializeFloatOverPointer(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long *pNumber);
259 static void DeserializeFloatInlined(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber);
261 static longest llliGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber);
262 static ulongest llluGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber);
264 static void DeserializeNumberFloat(char *szLocation, char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber);
266 static void DeserializeNumber(char *szLocation, char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber);
267 static const char *DeserializeTypeCheckKind(uint8_t hhuTypeCheckKind);
268 static const char *DeserializeBuiltinCheckKind(uint8_t hhuBuiltinCheckKind);
269 static const char *DeserializeCFICheckKind(uint8_t hhuCFICheckKind);
270 static bool isNegativeNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber);
271 static bool isShiftExponentTooLarge(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber, size_t zWidth);
273 /* Unused in this implementation, emitted by the C++ check dynamic type cast. */
274 intptr_t __ubsan_vptr_type_cache[128];
276 /* Public symbols used in the instrumentation of the code generation part */
277 void __ubsan_handle_add_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
278 void __ubsan_handle_add_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
279 void __ubsan_handle_builtin_unreachable(struct CUnreachableData *pData);
280 void __ubsan_handle_cfi_bad_type(struct CCFICheckFailData *pData, unsigned long ulVtable, bool bValidVtable, bool FromUnrecoverableHandler, unsigned long ProgramCounter, unsigned long FramePointer);
281 void __ubsan_handle_cfi_check_fail(struct CCFICheckFailData *pData, unsigned long ulValue, unsigned long ulValidVtable);
282 void __ubsan_handle_cfi_check_fail_abort(struct CCFICheckFailData *pData, unsigned long ulValue, unsigned long ulValidVtable);
283 void __ubsan_handle_divrem_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
284 void __ubsan_handle_divrem_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
285 void __ubsan_handle_dynamic_type_cache_miss(struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash);
286 void __ubsan_handle_dynamic_type_cache_miss_abort(struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash);
287 void __ubsan_handle_float_cast_overflow(struct CFloatCastOverflowData *pData, unsigned long ulFrom);
288 void __ubsan_handle_float_cast_overflow_abort(struct CFloatCastOverflowData *pData, unsigned long ulFrom);
289 void __ubsan_handle_function_type_mismatch(struct CFunctionTypeMismatchData *pData, unsigned long ulFunction);
290 void __ubsan_handle_function_type_mismatch_abort(struct CFunctionTypeMismatchData *pData, unsigned long ulFunction);
291 void __ubsan_handle_invalid_builtin(struct CInvalidBuiltinData *pData);
292 void __ubsan_handle_invalid_builtin_abort(struct CInvalidBuiltinData *pData);
293 void __ubsan_handle_load_invalid_value(struct CInvalidValueData *pData, unsigned long ulVal);
294 void __ubsan_handle_load_invalid_value_abort(struct CInvalidValueData *pData, unsigned long ulVal);
295 void __ubsan_handle_missing_return(struct CUnreachableData *pData);
296 void __ubsan_handle_mul_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
297 void __ubsan_handle_mul_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
298 void __ubsan_handle_negate_overflow(struct COverflowData *pData, unsigned long ulOldVal);
299 void __ubsan_handle_negate_overflow_abort(struct COverflowData *pData, unsigned long ulOldVal);
300 void __ubsan_handle_nonnull_arg(struct CNonNullArgData *pData);
301 void __ubsan_handle_nonnull_arg_abort(struct CNonNullArgData *pData);
302 void __ubsan_handle_nonnull_return_v1(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
303 void __ubsan_handle_nonnull_return_v1_abort(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
304 void __ubsan_handle_nullability_arg(struct CNonNullArgData *pData);
305 void __ubsan_handle_nullability_arg_abort(struct CNonNullArgData *pData);
306 void __ubsan_handle_nullability_return_v1(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
307 void __ubsan_handle_nullability_return_v1_abort(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
308 void __ubsan_handle_out_of_bounds(struct COutOfBoundsData *pData, unsigned long ulIndex);
309 void __ubsan_handle_out_of_bounds_abort(struct COutOfBoundsData *pData, unsigned long ulIndex);
310 void __ubsan_handle_pointer_overflow(struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult);
311 void __ubsan_handle_pointer_overflow_abort(struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult);
312 void __ubsan_handle_shift_out_of_bounds(struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS);
313 void __ubsan_handle_shift_out_of_bounds_abort(struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS);
314 void __ubsan_handle_sub_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
315 void __ubsan_handle_sub_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
316 void __ubsan_handle_type_mismatch(struct CTypeMismatchData *pData, unsigned long ulPointer);
317 void __ubsan_handle_type_mismatch_abort(struct CTypeMismatchData *pData, unsigned long ulPointer);
318 void __ubsan_handle_type_mismatch_v1(struct CTypeMismatchData_v1 *pData, unsigned long ulPointer);
319 void __ubsan_handle_type_mismatch_v1_abort(struct CTypeMismatchData_v1 *pData, unsigned long ulPointer);
320 void __ubsan_handle_vla_bound_not_positive(struct CVLABoundData *pData, unsigned long ulBound);
321 void __ubsan_handle_vla_bound_not_positive_abort(struct CVLABoundData *pData, unsigned long ulBound);
322 void __ubsan_get_current_report_data(const char **ppOutIssueKind, const char **ppOutMessage, const char **ppOutFilename, uint32_t *pOutLine, uint32_t *pOutCol, char **ppOutMemoryAddr);
324 static void HandleOverflow(bool isFatal, struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS, const char *szOperation);
325 static void HandleNegateOverflow(bool isFatal, struct COverflowData *pData, unsigned long ulOldValue);
326 static void HandleBuiltinUnreachable(bool isFatal, struct CUnreachableData *pData);
327 static void HandleTypeMismatch(bool isFatal, struct CSourceLocation *mLocation, struct CTypeDescriptor *mType, unsigned long mLogAlignment, uint8_t mTypeCheckKind, unsigned long ulPointer);
328 static void HandleVlaBoundNotPositive(bool isFatal, struct CVLABoundData *pData, unsigned long ulBound);
329 static void HandleOutOfBounds(bool isFatal, struct COutOfBoundsData *pData, unsigned long ulIndex);
330 static void HandleShiftOutOfBounds(bool isFatal, struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS);
331 static void HandleLoadInvalidValue(bool isFatal, struct CInvalidValueData *pData, unsigned long ulValue);
332 static void HandleInvalidBuiltin(bool isFatal, struct CInvalidBuiltinData *pData);
333 static void HandleFunctionTypeMismatch(bool isFatal, struct CFunctionTypeMismatchData *pData, unsigned long ulFunction);
334 static void HandleCFIBadType(bool isFatal, struct CCFICheckFailData *pData, unsigned long ulVtable, bool *bValidVtable, bool *FromUnrecoverableHandler, unsigned long *ProgramCounter, unsigned long *FramePointer);
335 static void HandleDynamicTypeCacheMiss(bool isFatal, struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash);
336 static void HandleFloatCastOverflow(bool isFatal, struct CFloatCastOverflowData *pData, unsigned long ulFrom);
337 static void HandleMissingReturn(bool isFatal, struct CUnreachableData *pData);
338 static void HandleNonnullArg(bool isFatal, struct CNonNullArgData *pData);
339 static void HandleNonnullReturn(bool isFatal, struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
340 static void HandlePointerOverflow(bool isFatal, struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult);
343 HandleOverflow(bool isFatal, struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS, const char *szOperation)
345 char szLocation[LOCATION_MAXLEN];
346 char szLHS[NUMBER_MAXLEN];
347 char szRHS[NUMBER_MAXLEN];
351 if (isAlreadyReported(&pData->mLocation))
354 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
355 DeserializeNumber(szLocation, szLHS, NUMBER_MAXLEN, pData->mType, ulLHS);
356 DeserializeNumber(szLocation, szRHS, NUMBER_MAXLEN, pData->mType, ulRHS);
358 Report(isFatal, "UBSan: Undefined Behavior in %s, %s integer overflow: %s %s %s cannot be represented in type %s\n",
359 szLocation, ISSET(pData->mType->mTypeInfo, NUMBER_SIGNED_BIT) ? "signed" : "unsigned", szLHS, szOperation, szRHS, pData->mType->mTypeName);
363 HandleNegateOverflow(bool isFatal, struct COverflowData *pData, unsigned long ulOldValue)
365 char szLocation[LOCATION_MAXLEN];
366 char szOldValue[NUMBER_MAXLEN];
370 if (isAlreadyReported(&pData->mLocation))
373 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
374 DeserializeNumber(szLocation, szOldValue, NUMBER_MAXLEN, pData->mType, ulOldValue);
376 Report(isFatal, "UBSan: Undefined Behavior in %s, negation of %s cannot be represented in type %s\n",
377 szLocation, szOldValue, pData->mType->mTypeName);
381 HandleBuiltinUnreachable(bool isFatal, struct CUnreachableData *pData)
383 char szLocation[LOCATION_MAXLEN];
387 if (isAlreadyReported(&pData->mLocation))
390 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
392 Report(isFatal, "UBSan: Undefined Behavior in %s, calling __builtin_unreachable()\n",
397 HandleTypeMismatch(bool isFatal, struct CSourceLocation *mLocation, struct CTypeDescriptor *mType, unsigned long mLogAlignment, uint8_t mTypeCheckKind, unsigned long ulPointer)
399 char szLocation[LOCATION_MAXLEN];
404 if (isAlreadyReported(mLocation))
407 DeserializeLocation(szLocation, LOCATION_MAXLEN, mLocation);
409 if (ulPointer == 0) {
410 Report(isFatal, "UBSan: Undefined Behavior in %s, %s null pointer of type %s\n",
411 szLocation, DeserializeTypeCheckKind(mTypeCheckKind), mType->mTypeName);
412 } else if ((mLogAlignment - 1) & ulPointer) {
413 Report(isFatal, "UBSan: Undefined Behavior in %s, %s misaligned address %p for type %s which requires %ld byte alignment\n",
414 szLocation, DeserializeTypeCheckKind(mTypeCheckKind), REINTERPRET_CAST(void *, ulPointer), mType->mTypeName, mLogAlignment);
416 Report(isFatal, "UBSan: Undefined Behavior in %s, %s address %p with insufficient space for an object of type %s\n",
417 szLocation, DeserializeTypeCheckKind(mTypeCheckKind), REINTERPRET_CAST(void *, ulPointer), mType->mTypeName);
422 HandleVlaBoundNotPositive(bool isFatal, struct CVLABoundData *pData, unsigned long ulBound)
424 char szLocation[LOCATION_MAXLEN];
425 char szBound[NUMBER_MAXLEN];
429 if (isAlreadyReported(&pData->mLocation))
432 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
433 DeserializeNumber(szLocation, szBound, NUMBER_MAXLEN, pData->mType, ulBound);
435 Report(isFatal, "UBSan: Undefined Behavior in %s, variable length array bound value %s <= 0\n",
436 szLocation, szBound);
440 HandleOutOfBounds(bool isFatal, struct COutOfBoundsData *pData, unsigned long ulIndex)
442 char szLocation[LOCATION_MAXLEN];
443 char szIndex[NUMBER_MAXLEN];
447 if (isAlreadyReported(&pData->mLocation))
450 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
451 DeserializeNumber(szLocation, szIndex, NUMBER_MAXLEN, pData->mIndexType, ulIndex);
453 Report(isFatal, "UBSan: Undefined Behavior in %s, index %s is out of range for type %s\n",
454 szLocation, szIndex, pData->mArrayType->mTypeName);
458 HandleShiftOutOfBounds(bool isFatal, struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS)
460 char szLocation[LOCATION_MAXLEN];
461 char szLHS[NUMBER_MAXLEN];
462 char szRHS[NUMBER_MAXLEN];
466 if (isAlreadyReported(&pData->mLocation))
469 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
470 DeserializeNumber(szLocation, szLHS, NUMBER_MAXLEN, pData->mLHSType, ulLHS);
471 DeserializeNumber(szLocation, szRHS, NUMBER_MAXLEN, pData->mRHSType, ulRHS);
473 if (isNegativeNumber(szLocation, pData->mRHSType, ulRHS))
474 Report(isFatal, "UBSan: Undefined Behavior in %s, shift exponent %s is negative\n",
476 else if (isShiftExponentTooLarge(szLocation, pData->mRHSType, ulRHS, zDeserializeTypeWidth(pData->mLHSType)))
477 Report(isFatal, "UBSan: Undefined Behavior in %s, shift exponent %s is too large for %zu-bit type %s\n",
478 szLocation, szRHS, zDeserializeTypeWidth(pData->mLHSType), pData->mLHSType->mTypeName);
479 else if (isNegativeNumber(szLocation, pData->mLHSType, ulLHS))
480 Report(isFatal, "UBSan: Undefined Behavior in %s, left shift of negative value %s\n",
483 Report(isFatal, "UBSan: Undefined Behavior in %s, left shift of %s by %s places cannot be represented in type %s\n",
484 szLocation, szLHS, szRHS, pData->mLHSType->mTypeName);
488 HandleLoadInvalidValue(bool isFatal, struct CInvalidValueData *pData, unsigned long ulValue)
490 char szLocation[LOCATION_MAXLEN];
491 char szValue[NUMBER_MAXLEN];
495 if (isAlreadyReported(&pData->mLocation))
498 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
499 DeserializeNumber(szLocation, szValue, NUMBER_MAXLEN, pData->mType, ulValue);
501 Report(isFatal, "UBSan: Undefined Behavior in %s, load of value %s is not a valid value for type %s\n",
502 szLocation, szValue, pData->mType->mTypeName);
506 HandleInvalidBuiltin(bool isFatal, struct CInvalidBuiltinData *pData)
508 char szLocation[LOCATION_MAXLEN];
512 if (isAlreadyReported(&pData->mLocation))
515 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
517 Report(isFatal, "UBSan: Undefined Behavior in %s, passing zero to %s, which is not a valid argument\n",
518 szLocation, DeserializeBuiltinCheckKind(pData->mKind));
522 HandleFunctionTypeMismatch(bool isFatal, struct CFunctionTypeMismatchData *pData, unsigned long ulFunction)
524 char szLocation[LOCATION_MAXLEN];
527 * There is no a portable C solution to translate an address of a
528 * function to its name. On the cost of getting this routine simple
529 * and portable without ifdefs between the userland and the kernel
530 * just print the address of the function as-is.
532 * For better diagnostic messages in the userland, users shall use
533 * the full upstream version shipped along with the compiler toolchain.
538 if (isAlreadyReported(&pData->mLocation))
541 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
543 Report(isFatal, "UBSan: Undefined Behavior in %s, call to function %#lx through pointer to incorrect function type %s\n",
544 szLocation, ulFunction, pData->mType->mTypeName);
548 HandleCFIBadType(bool isFatal, struct CCFICheckFailData *pData, unsigned long ulVtable, bool *bValidVtable, bool *FromUnrecoverableHandler, unsigned long *ProgramCounter, unsigned long *FramePointer)
550 char szLocation[LOCATION_MAXLEN];
553 * This is a minimal implementation without diving into C++
554 * specifics and (Itanium) ABI deserialization.
559 if (isAlreadyReported(&pData->mLocation))
562 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
564 if (pData->mCheckKind == CFI_ICALL || pData->mCheckKind == CFI_VMFCALL) {
565 Report(isFatal, "UBSan: Undefined Behavior in %s, control flow integrity check for type %s failed during %s (vtable address %#lx)\n",
566 szLocation, pData->mType->mTypeName, DeserializeCFICheckKind(pData->mCheckKind), ulVtable);
568 Report(isFatal || FromUnrecoverableHandler, "UBSan: Undefined Behavior in %s, control flow integrity check for type %s failed during %s (vtable address %#lx; %s vtable; from %s handler; Program Counter %#lx; Frame Pointer %#lx)\n",
569 szLocation, pData->mType->mTypeName, DeserializeCFICheckKind(pData->mCheckKind), ulVtable, *bValidVtable ? "valid" : "invalid", *FromUnrecoverableHandler ? "unrecoverable" : "recoverable", *ProgramCounter, *FramePointer);
574 HandleDynamicTypeCacheMiss(bool isFatal, struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash)
577 char szLocation[LOCATION_MAXLEN];
582 * This UBSan handler is special as the check has to be impelemented
583 * in an implementation. In order to handle it there is need to
584 * introspect into C++ ABI internals (RTTI) and use low-level
585 * C++ runtime interfaces.
590 if (isAlreadyReported(&pData->mLocation))
593 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
595 Report(isFatal, "UBSan: Undefined Behavior in %s, %s address %#lx which might not point to an object of type %s\n"
596 szLocation, DeserializeTypeCheckKind(pData->mTypeCheckKind), ulPointer, pData->mType);
601 HandleFloatCastOverflow(bool isFatal, struct CFloatCastOverflowData *pData, unsigned long ulFrom)
603 char szLocation[LOCATION_MAXLEN];
604 char szFrom[NUMBER_MAXLEN];
608 if (isAlreadyReported(&pData->mLocation))
611 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
612 DeserializeNumber(szLocation, szFrom, NUMBER_MAXLEN, pData->mFromType, ulFrom);
614 Report(isFatal, "UBSan: Undefined Behavior in %s, %s (of type %s) is outside the range of representable values of type %s\n",
615 szLocation, szFrom, pData->mFromType->mTypeName, pData->mToType->mTypeName);
619 HandleMissingReturn(bool isFatal, struct CUnreachableData *pData)
621 char szLocation[LOCATION_MAXLEN];
625 if (isAlreadyReported(&pData->mLocation))
628 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
630 Report(isFatal, "UBSan: Undefined Behavior in %s, execution reached the end of a value-returning function without returning a value\n",
635 HandleNonnullArg(bool isFatal, struct CNonNullArgData *pData)
637 char szLocation[LOCATION_MAXLEN];
638 char szAttributeLocation[LOCATION_MAXLEN];
642 if (isAlreadyReported(&pData->mLocation))
645 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
646 if (pData->mAttributeLocation.mFilename)
647 DeserializeLocation(szAttributeLocation, LOCATION_MAXLEN, &pData->mAttributeLocation);
649 szAttributeLocation[0] = '\0';
651 Report(isFatal, "UBSan: Undefined Behavior in %s, null pointer passed as argument %d, which is declared to never be null%s%s\n",
652 szLocation, pData->mArgIndex, pData->mAttributeLocation.mFilename ? ", nonnull/_Nonnull specified in " : "", szAttributeLocation);
656 HandleNonnullReturn(bool isFatal, struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
658 char szLocation[LOCATION_MAXLEN];
659 char szAttributeLocation[LOCATION_MAXLEN];
662 ASSERT(pLocationPointer);
664 if (isAlreadyReported(pLocationPointer))
667 DeserializeLocation(szLocation, LOCATION_MAXLEN, pLocationPointer);
668 if (pData->mAttributeLocation.mFilename)
669 DeserializeLocation(szAttributeLocation, LOCATION_MAXLEN, &pData->mAttributeLocation);
671 szAttributeLocation[0] = '\0';
673 Report(isFatal, "UBSan: Undefined Behavior in %s, null pointer returned from function declared to never return null%s%s\n",
674 szLocation, pData->mAttributeLocation.mFilename ? ", nonnull/_Nonnull specified in " : "", szAttributeLocation);
678 HandlePointerOverflow(bool isFatal, struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult)
680 char szLocation[LOCATION_MAXLEN];
684 if (isAlreadyReported(&pData->mLocation))
687 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
689 Report(isFatal, "UBSan: Undefined Behavior in %s, pointer expression with base %#lx overflowed to %#lx\n",
690 szLocation, ulBase, ulResult);
693 /* Definions of public symbols emitted by the instrumentation code */
695 __ubsan_handle_add_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
700 HandleOverflow(false, pData, ulLHS, ulRHS, PLUS_STRING);
704 __ubsan_handle_add_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
709 HandleOverflow(true, pData, ulLHS, ulRHS, PLUS_STRING);
713 __ubsan_handle_builtin_unreachable(struct CUnreachableData *pData)
718 HandleBuiltinUnreachable(true, pData);
722 __ubsan_handle_cfi_bad_type(struct CCFICheckFailData *pData, unsigned long ulVtable, bool bValidVtable, bool FromUnrecoverableHandler, unsigned long ProgramCounter, unsigned long FramePointer)
727 HandleCFIBadType(false, pData, ulVtable, &bValidVtable, &FromUnrecoverableHandler, &ProgramCounter, &FramePointer);
731 __ubsan_handle_cfi_check_fail(struct CCFICheckFailData *pData, unsigned long ulValue, unsigned long ulValidVtable)
736 HandleCFIBadType(false, pData, ulValue, 0, 0, 0, 0);
740 __ubsan_handle_cfi_check_fail_abort(struct CCFICheckFailData *pData, unsigned long ulValue, unsigned long ulValidVtable)
745 HandleCFIBadType(true, pData, ulValue, 0, 0, 0, 0);
749 __ubsan_handle_divrem_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
754 HandleOverflow(false, pData, ulLHS, ulRHS, DIVREM_STRING);
758 __ubsan_handle_divrem_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
763 HandleOverflow(true, pData, ulLHS, ulRHS, DIVREM_STRING);
767 __ubsan_handle_dynamic_type_cache_miss(struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash)
772 HandleDynamicTypeCacheMiss(false, pData, ulPointer, ulHash);
776 __ubsan_handle_dynamic_type_cache_miss_abort(struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash)
781 HandleDynamicTypeCacheMiss(false, pData, ulPointer, ulHash);
785 __ubsan_handle_float_cast_overflow(struct CFloatCastOverflowData *pData, unsigned long ulFrom)
790 HandleFloatCastOverflow(false, pData, ulFrom);
794 __ubsan_handle_float_cast_overflow_abort(struct CFloatCastOverflowData *pData, unsigned long ulFrom)
799 HandleFloatCastOverflow(true, pData, ulFrom);
803 __ubsan_handle_function_type_mismatch(struct CFunctionTypeMismatchData *pData, unsigned long ulFunction)
808 HandleFunctionTypeMismatch(false, pData, ulFunction);
812 __ubsan_handle_function_type_mismatch_abort(struct CFunctionTypeMismatchData *pData, unsigned long ulFunction)
817 HandleFunctionTypeMismatch(false, pData, ulFunction);
821 __ubsan_handle_invalid_builtin(struct CInvalidBuiltinData *pData)
826 HandleInvalidBuiltin(true, pData);
830 __ubsan_handle_invalid_builtin_abort(struct CInvalidBuiltinData *pData)
835 HandleInvalidBuiltin(true, pData);
839 __ubsan_handle_load_invalid_value(struct CInvalidValueData *pData, unsigned long ulValue)
844 HandleLoadInvalidValue(false, pData, ulValue);
848 __ubsan_handle_load_invalid_value_abort(struct CInvalidValueData *pData, unsigned long ulValue)
853 HandleLoadInvalidValue(true, pData, ulValue);
857 __ubsan_handle_missing_return(struct CUnreachableData *pData)
862 HandleMissingReturn(true, pData);
866 __ubsan_handle_mul_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
871 HandleOverflow(false, pData, ulLHS, ulRHS, MUL_STRING);
875 __ubsan_handle_mul_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
880 HandleOverflow(true, pData, ulLHS, ulRHS, MUL_STRING);
884 __ubsan_handle_negate_overflow(struct COverflowData *pData, unsigned long ulOldValue)
889 HandleNegateOverflow(false, pData, ulOldValue);
893 __ubsan_handle_negate_overflow_abort(struct COverflowData *pData, unsigned long ulOldValue)
898 HandleNegateOverflow(true, pData, ulOldValue);
902 __ubsan_handle_nonnull_arg(struct CNonNullArgData *pData)
907 HandleNonnullArg(false, pData);
911 __ubsan_handle_nonnull_arg_abort(struct CNonNullArgData *pData)
916 HandleNonnullArg(true, pData);
920 __ubsan_handle_nonnull_return_v1(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
924 ASSERT(pLocationPointer);
926 HandleNonnullReturn(false, pData, pLocationPointer);
930 __ubsan_handle_nonnull_return_v1_abort(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
934 ASSERT(pLocationPointer);
936 HandleNonnullReturn(true, pData, pLocationPointer);
940 __ubsan_handle_nullability_arg(struct CNonNullArgData *pData)
945 HandleNonnullArg(false, pData);
949 __ubsan_handle_nullability_arg_abort(struct CNonNullArgData *pData)
954 HandleNonnullArg(true, pData);
958 __ubsan_handle_nullability_return_v1(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
962 ASSERT(pLocationPointer);
964 HandleNonnullReturn(false, pData, pLocationPointer);
968 __ubsan_handle_nullability_return_v1_abort(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
972 ASSERT(pLocationPointer);
974 HandleNonnullReturn(true, pData, pLocationPointer);
978 __ubsan_handle_out_of_bounds(struct COutOfBoundsData *pData, unsigned long ulIndex)
983 HandleOutOfBounds(false, pData, ulIndex);
987 __ubsan_handle_out_of_bounds_abort(struct COutOfBoundsData *pData, unsigned long ulIndex)
992 HandleOutOfBounds(true, pData, ulIndex);
996 __ubsan_handle_pointer_overflow(struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult)
1001 HandlePointerOverflow(false, pData, ulBase, ulResult);
1005 __ubsan_handle_pointer_overflow_abort(struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult)
1010 HandlePointerOverflow(true, pData, ulBase, ulResult);
1014 __ubsan_handle_shift_out_of_bounds(struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS)
1019 HandleShiftOutOfBounds(false, pData, ulLHS, ulRHS);
1023 __ubsan_handle_shift_out_of_bounds_abort(struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS)
1028 HandleShiftOutOfBounds(true, pData, ulLHS, ulRHS);
1032 __ubsan_handle_sub_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
1037 HandleOverflow(false, pData, ulLHS, ulRHS, MINUS_STRING);
1041 __ubsan_handle_sub_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
1046 HandleOverflow(true, pData, ulLHS, ulRHS, MINUS_STRING);
1050 __ubsan_handle_type_mismatch(struct CTypeMismatchData *pData, unsigned long ulPointer)
1055 HandleTypeMismatch(false, &pData->mLocation, pData->mType, pData->mLogAlignment, pData->mTypeCheckKind, ulPointer);
1059 __ubsan_handle_type_mismatch_abort(struct CTypeMismatchData *pData, unsigned long ulPointer)
1064 HandleTypeMismatch(true, &pData->mLocation, pData->mType, pData->mLogAlignment, pData->mTypeCheckKind, ulPointer);
1068 __ubsan_handle_type_mismatch_v1(struct CTypeMismatchData_v1 *pData, unsigned long ulPointer)
1073 HandleTypeMismatch(false, &pData->mLocation, pData->mType, __BIT(pData->mLogAlignment), pData->mTypeCheckKind, ulPointer);
1077 __ubsan_handle_type_mismatch_v1_abort(struct CTypeMismatchData_v1 *pData, unsigned long ulPointer)
1082 HandleTypeMismatch(true, &pData->mLocation, pData->mType, __BIT(pData->mLogAlignment), pData->mTypeCheckKind, ulPointer);
1086 __ubsan_handle_vla_bound_not_positive(struct CVLABoundData *pData, unsigned long ulBound)
1091 HandleVlaBoundNotPositive(false, pData, ulBound);
1095 __ubsan_handle_vla_bound_not_positive_abort(struct CVLABoundData *pData, unsigned long ulBound)
1100 HandleVlaBoundNotPositive(true, pData, ulBound);
1104 __ubsan_get_current_report_data(const char **ppOutIssueKind, const char **ppOutMessage, const char **ppOutFilename, uint32_t *pOutLine, uint32_t *pOutCol, char **ppOutMemoryAddr)
1109 * The __ubsan_on_report() feature is non trivial to implement in a
1110 * shared code between the kernel and userland. It's also opening
1111 * new sets of potential problems as we are not expected to slow down
1112 * execution of certain kernel subsystems (synchronization issues,
1113 * interrupt handling etc).
1115 * A proper solution would need probably a lock-free bounded queue built
1116 * with atomic operations with the property of miltiple consumers and
1117 * multiple producers. Maintaining and validating such code is not
1120 * A legitimate user - besides testing framework - is a debugger plugin
1121 * intercepting reports from the UBSan instrumentation. For such
1122 * scenarios it is better to run the Clang/GCC version.
1126 /* Local utility functions */
1129 Report(bool isFatal, const char *pFormat, ...)
1135 va_start(ap, pFormat);
1136 #if defined(_KERNEL)
1138 vpanic(pFormat, ap);
1140 vprintf(pFormat, ap);
1142 if (ubsan_flags == -1) {
1146 ubsan_flags = UBSAN_STDERR;
1148 if (getenv_r("LIBC_UBSAN", buf, sizeof(buf)) != -1) {
1149 for (p = buf; *p; p++) {
1152 SET(ubsan_flags, UBSAN_ABORT);
1155 CLR(ubsan_flags, UBSAN_ABORT);
1158 SET(ubsan_flags, UBSAN_STDERR);
1161 CLR(ubsan_flags, UBSAN_STDERR);
1164 SET(ubsan_flags, UBSAN_SYSLOG);
1167 CLR(ubsan_flags, UBSAN_SYSLOG);
1170 SET(ubsan_flags, UBSAN_STDOUT);
1173 CLR(ubsan_flags, UBSAN_STDOUT);
1182 // The *v*print* functions can flush the va_list argument.
1183 // Create a local copy for each call to prevent invalid read.
1184 if (ISSET(ubsan_flags, UBSAN_STDOUT)) {
1187 vprintf(pFormat, tmp);
1191 if (ISSET(ubsan_flags, UBSAN_STDERR)) {
1194 vfprintf(stderr, pFormat, tmp);
1198 if (ISSET(ubsan_flags, UBSAN_SYSLOG)) {
1201 struct syslog_data SyslogData = SYSLOG_DATA_INIT;
1202 ubsan_vsyslog(LOG_DEBUG | LOG_USER, &SyslogData, pFormat, tmp);
1205 if (isFatal || ISSET(ubsan_flags, UBSAN_ABORT)) {
1214 isAlreadyReported(struct CSourceLocation *pLocation)
1217 * This code is shared between libc, kernel and standalone usage.
1218 * It shall work in early bootstrap phase of both of them.
1221 uint32_t siOldValue;
1222 volatile uint32_t *pLine;
1226 pLine = &pLocation->mLine;
1229 siOldValue = *pLine;
1230 } while (__sync_val_compare_and_swap(pLine, siOldValue, siOldValue | ACK_REPORTED) != siOldValue);
1232 return ISSET(siOldValue, ACK_REPORTED);
1236 zDeserializeTypeWidth(struct CTypeDescriptor *pType)
1242 switch (pType->mTypeKind) {
1244 zWidth = __BIT(__SHIFTOUT(pType->mTypeInfo, ~NUMBER_SIGNED_BIT));
1247 zWidth = pType->mTypeInfo;
1250 Report(true, "UBSan: Unknown variable type %#04" PRIx16 "\n", pType->mTypeKind);
1254 /* Invalid width will be transformed to 0 */
1261 DeserializeLocation(char *pBuffer, size_t zBUfferLength, struct CSourceLocation *pLocation)
1265 ASSERT(pLocation->mFilename);
1267 snprintf(pBuffer, zBUfferLength, "%s:%" PRIu32 ":%" PRIu32, pLocation->mFilename, pLocation->mLine & (uint32_t)~ACK_REPORTED, pLocation->mColumn);
1270 #ifdef __SIZEOF_INT128__
1272 DeserializeUINT128(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, __uint128_t U128)
1274 char szBuf[3]; /* 'XX\0' */
1275 char rgNumber[sizeof(ulongest)];
1278 memcpy(rgNumber, &U128, sizeof(U128));
1280 strlcpy(pBuffer, "Undecoded-128-bit-Integer-Type (0x", zBUfferLength);
1281 #if BYTE_ORDER == LITTLE_ENDIAN
1282 for (zI = sizeof(ulongest) - 1; zI >= 0; zI--) {
1284 for (zI = 0; zI < (ssize_t)sizeof(ulongest); zI++) {
1286 snprintf(szBuf, sizeof(szBuf), "%02" PRIx8, rgNumber[zI]);
1287 strlcat(pBuffer, szBuf, zBUfferLength);
1289 strlcat(pBuffer, ")", zBUfferLength);
1294 DeserializeNumberSigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, longest L)
1298 ASSERT(zBUfferLength > 0);
1300 ASSERT(ISSET(pType->mTypeInfo, NUMBER_SIGNED_BIT));
1302 switch (zDeserializeTypeWidth(pType)) {
1304 ASSERT(0 && "Invalid codepath");
1306 #ifdef __SIZEOF_INT128__
1308 DeserializeUINT128(pBuffer, zBUfferLength, pType, STATIC_CAST(__uint128_t, L));
1315 snprintf(pBuffer, zBUfferLength, "%" PRId64, STATIC_CAST(int64_t, L));
1321 DeserializeNumberUnsigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, ulongest L)
1325 ASSERT(zBUfferLength > 0);
1327 ASSERT(!ISSET(pType->mTypeInfo, NUMBER_SIGNED_BIT));
1329 switch (zDeserializeTypeWidth(pType)) {
1331 ASSERT(0 && "Invalid codepath");
1333 #ifdef __SIZEOF_INT128__
1335 DeserializeUINT128(pBuffer, zBUfferLength, pType, STATIC_CAST(__uint128_t, L));
1342 snprintf(pBuffer, zBUfferLength, "%" PRIu64, STATIC_CAST(uint64_t, L));
1349 DeserializeFloatOverPointer(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long *pNumber)
1352 #ifdef __HAVE_LONG_DOUBLE
1357 ASSERT(zBUfferLength > 0);
1361 * This function handles 64-bit number over a pointer on 32-bit CPUs.
1363 ASSERT((sizeof(*pNumber) * CHAR_BIT < WIDTH_64) || (zDeserializeTypeWidth(pType) >= WIDTH_64));
1364 ASSERT(sizeof(D) == sizeof(uint64_t));
1365 #ifdef __HAVE_LONG_DOUBLE
1366 ASSERT(sizeof(LD) > sizeof(uint64_t));
1369 switch (zDeserializeTypeWidth(pType)) {
1370 #ifdef __HAVE_LONG_DOUBLE
1374 memcpy(&LD, pNumber, sizeof(long double));
1375 snprintf(pBuffer, zBUfferLength, "%Lg", LD);
1379 memcpy(&D, pNumber, sizeof(double));
1380 snprintf(pBuffer, zBUfferLength, "%g", D);
1386 DeserializeFloatInlined(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber)
1393 ASSERT(zBUfferLength > 0);
1395 ASSERT(sizeof(F) == sizeof(uint32_t));
1396 ASSERT(sizeof(D) == sizeof(uint64_t));
1398 switch (zDeserializeTypeWidth(pType)) {
1400 memcpy(&D, &ulNumber, sizeof(double));
1401 snprintf(pBuffer, zBUfferLength, "%g", D);
1405 * On supported platforms sizeof(float)==sizeof(uint32_t)
1406 * unsigned long is either 32 or 64-bit, cast it to 32-bit
1407 * value in order to call memcpy(3) in an Endian-aware way.
1409 U32 = STATIC_CAST(uint32_t, ulNumber);
1410 memcpy(&F, &U32, sizeof(float));
1411 snprintf(pBuffer, zBUfferLength, "%g", F);
1414 snprintf(pBuffer, zBUfferLength, "Undecoded-16-bit-Floating-Type (%#04" PRIx16 ")", STATIC_CAST(uint16_t, ulNumber));
1421 llliGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber)
1423 size_t zNumberWidth;
1429 zNumberWidth = zDeserializeTypeWidth(pType);
1430 switch (zNumberWidth) {
1432 Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation);
1435 #ifdef __SIZEOF_INT128__
1436 memcpy(&L, REINTERPRET_CAST(longest *, ulNumber), sizeof(longest));
1438 Report(true, "UBSan: Unexpected 128-Bit Type in %s\n", szLocation);
1443 if (sizeof(ulNumber) * CHAR_BIT < WIDTH_64) {
1444 L = *REINTERPRET_CAST(int64_t *, ulNumber);
1446 L = STATIC_CAST(int64_t, STATIC_CAST(uint64_t, ulNumber));
1450 L = STATIC_CAST(int32_t, STATIC_CAST(uint32_t, ulNumber));
1453 L = STATIC_CAST(int16_t, STATIC_CAST(uint16_t, ulNumber));
1456 L = STATIC_CAST(int8_t, STATIC_CAST(uint8_t, ulNumber));
1464 llluGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber)
1466 size_t zNumberWidth;
1471 zNumberWidth = zDeserializeTypeWidth(pType);
1472 switch (zNumberWidth) {
1474 Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation);
1477 #ifdef __SIZEOF_INT128__
1478 memcpy(&UL, REINTERPRET_CAST(ulongest *, ulNumber), sizeof(ulongest));
1481 Report(true, "UBSan: Unexpected 128-Bit Type in %s\n", szLocation);
1485 if (sizeof(ulNumber) * CHAR_BIT < WIDTH_64) {
1486 UL = *REINTERPRET_CAST(uint64_t *, ulNumber);
1504 DeserializeNumberFloat(char *szLocation, char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber)
1506 size_t zNumberWidth;
1510 ASSERT(zBUfferLength > 0);
1512 ASSERT(pType->mTypeKind == KIND_FLOAT);
1514 zNumberWidth = zDeserializeTypeWidth(pType);
1515 switch (zNumberWidth) {
1517 Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation);
1519 #ifdef __HAVE_LONG_DOUBLE
1523 DeserializeFloatOverPointer(pBuffer, zBUfferLength, pType, REINTERPRET_CAST(unsigned long *, ulNumber));
1527 if (sizeof(ulNumber) * CHAR_BIT < WIDTH_64) {
1528 DeserializeFloatOverPointer(pBuffer, zBUfferLength, pType, REINTERPRET_CAST(unsigned long *, ulNumber));
1533 DeserializeFloatInlined(pBuffer, zBUfferLength, pType, ulNumber);
1540 DeserializeNumber(char *szLocation, char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber)
1545 ASSERT(zBUfferLength > 0);
1548 switch(pType->mTypeKind) {
1550 if (ISSET(pType->mTypeInfo, NUMBER_SIGNED_BIT)) {
1551 longest L = llliGetNumber(szLocation, pType, ulNumber);
1552 DeserializeNumberSigned(pBuffer, zBUfferLength, pType, L);
1554 ulongest UL = llluGetNumber(szLocation, pType, ulNumber);
1555 DeserializeNumberUnsigned(pBuffer, zBUfferLength, pType, UL);
1560 Report(true, "UBSan: Unexpected Float Type in %s\n", szLocation);
1563 DeserializeNumberFloat(szLocation, pBuffer, zBUfferLength, pType, ulNumber);
1567 Report(true, "UBSan: Unknown Type in %s\n", szLocation);
1574 DeserializeTypeCheckKind(uint8_t hhuTypeCheckKind)
1576 const char *rgczTypeCheckKinds[] = {
1579 "reference binding to",
1580 "member access within",
1582 "constructor call on",
1586 "cast to virtual base of",
1587 "_Nonnull binding to",
1588 "dynamic operation on"
1591 ASSERT(__arraycount(rgczTypeCheckKinds) > hhuTypeCheckKind);
1593 return rgczTypeCheckKinds[hhuTypeCheckKind];
1597 DeserializeBuiltinCheckKind(uint8_t hhuBuiltinCheckKind)
1599 const char *rgczBuiltinCheckKinds[] = {
1604 ASSERT(__arraycount(rgczBuiltinCheckKinds) > hhuBuiltinCheckKind);
1606 return rgczBuiltinCheckKinds[hhuBuiltinCheckKind];
1610 DeserializeCFICheckKind(uint8_t hhuCFICheckKind)
1612 const char *rgczCFICheckKinds[] = {
1613 "virtual call", // CFI_VCALL
1614 "non-virtual call", // CFI_NVCALL
1615 "base-to-derived cast", // CFI_DERIVEDCAST
1616 "cast to unrelated type", // CFI_UNRELATEDCAST
1617 "indirect function call", // CFI_ICALL
1618 "non-virtual pointer to member function call", // CFI_NVMFCALL
1619 "virtual pointer to member function call", // CFI_VMFCALL
1622 ASSERT(__arraycount(rgczCFICheckKinds) > hhuCFICheckKind);
1624 return rgczCFICheckKinds[hhuCFICheckKind];
1628 isNegativeNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber)
1633 ASSERT(pType->mTypeKind == KIND_INTEGER);
1635 if (!ISSET(pType->mTypeInfo, NUMBER_SIGNED_BIT))
1638 return llliGetNumber(szLocation, pType, ulNumber) < 0;
1642 isShiftExponentTooLarge(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber, size_t zWidth)
1647 ASSERT(pType->mTypeKind == KIND_INTEGER);
1649 return llluGetNumber(szLocation, pType, ulNumber) >= zWidth;