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.
30 * The micro UBSan implementation for the userland (uUBSan) and kernel (kUBSan).
31 * The uBSSan versions is suitable for inclusion into libc or used standalone
34 * This file due to long symbol names generated by a compiler during the
35 * instrumentation process does not follow the KNF style with 80-column limit.
38 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 __KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
45 __RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
50 #include <sys/param.h>
51 #include <sys/types.h>
52 #include <sys/limits.h>
53 #include <sys/systm.h>
54 #include <machine/_inttypes.h>
55 #include <machine/stdarg.h>
56 #define ASSERT(x) KASSERT(x, ("%s: " __STRING(x) " failed", __func__))
57 #define __arraycount(x) nitems(x)
58 #define ISSET(x, y) ((x) & (y))
59 #define __BIT(x) ((uintmax_t)1 << (uintmax_t)(x))
60 #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
61 #define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
64 #include "namespace.h"
66 #include <sys/param.h>
81 #define ubsan_vsyslog vsyslog_ss
82 #define ASSERT(x) _DIAGASSERT(x)
84 #define ubsan_vsyslog vsyslog_r
85 #define ASSERT(x) assert(x)
87 /* These macros are available in _KERNEL only */
88 #define SET(t, f) ((t) |= (f))
89 #define ISSET(t, f) ((t) & (f))
90 #define CLR(t, f) ((t) &= ~(f))
93 #define REINTERPRET_CAST(__dt, __st) ((__dt)(__st))
94 #define STATIC_CAST(__dt, __st) ((__dt)(__st))
96 #define ACK_REPORTED __BIT(31)
98 #define MUL_STRING "*"
99 #define PLUS_STRING "+"
100 #define MINUS_STRING "-"
101 #define DIVREM_STRING "divrem"
105 #define CFI_DERIVEDCAST 2
106 #define CFI_UNRELATEDCAST 3
108 #define CFI_NVMFCALL 5
109 #define CFI_VMFCALL 6
111 #define NUMBER_MAXLEN 128
112 #define LOCATION_MAXLEN (PATH_MAX + 32 /* ':LINE:COLUMN' */)
120 #define WIDTH_128 128
122 #define NUMBER_SIGNED_BIT 1U
124 #if __SIZEOF_INT128__
125 typedef __int128 longest;
126 typedef unsigned __int128 ulongest;
128 typedef int64_t longest;
129 typedef uint64_t ulongest;
133 static int ubsan_flags = -1;
134 #define UBSAN_ABORT __BIT(0)
135 #define UBSAN_STDOUT __BIT(1)
136 #define UBSAN_STDERR __BIT(2)
137 #define UBSAN_SYSLOG __BIT(3)
140 /* Undefined Behavior specific defines and structures */
142 #define KIND_INTEGER 0
144 #define KIND_UNKNOWN UINT16_MAX
146 struct CSourceLocation {
152 struct CTypeDescriptor {
155 uint8_t mTypeName[1];
158 struct COverflowData {
159 struct CSourceLocation mLocation;
160 struct CTypeDescriptor *mType;
163 struct CUnreachableData {
164 struct CSourceLocation mLocation;
167 struct CCFICheckFailData {
169 struct CSourceLocation mLocation;
170 struct CTypeDescriptor *mType;
173 struct CDynamicTypeCacheMissData {
174 struct CSourceLocation mLocation;
175 struct CTypeDescriptor *mType;
177 uint8_t mTypeCheckKind;
180 struct CFunctionTypeMismatchData {
181 struct CSourceLocation mLocation;
182 struct CTypeDescriptor *mType;
185 struct CInvalidBuiltinData {
186 struct CSourceLocation mLocation;
190 struct CInvalidValueData {
191 struct CSourceLocation mLocation;
192 struct CTypeDescriptor *mType;
195 struct CNonNullArgData {
196 struct CSourceLocation mLocation;
197 struct CSourceLocation mAttributeLocation;
201 struct CNonNullReturnData {
202 struct CSourceLocation mAttributeLocation;
205 struct COutOfBoundsData {
206 struct CSourceLocation mLocation;
207 struct CTypeDescriptor *mArrayType;
208 struct CTypeDescriptor *mIndexType;
211 struct CPointerOverflowData {
212 struct CSourceLocation mLocation;
215 struct CShiftOutOfBoundsData {
216 struct CSourceLocation mLocation;
217 struct CTypeDescriptor *mLHSType;
218 struct CTypeDescriptor *mRHSType;
221 struct CTypeMismatchData {
222 struct CSourceLocation mLocation;
223 struct CTypeDescriptor *mType;
224 unsigned long mLogAlignment;
225 uint8_t mTypeCheckKind;
228 struct CTypeMismatchData_v1 {
229 struct CSourceLocation mLocation;
230 struct CTypeDescriptor *mType;
231 uint8_t mLogAlignment;
232 uint8_t mTypeCheckKind;
235 struct CVLABoundData {
236 struct CSourceLocation mLocation;
237 struct CTypeDescriptor *mType;
240 struct CFloatCastOverflowData {
241 struct CSourceLocation mLocation; /* This field exists in this struct since 2015 August 11th */
242 struct CTypeDescriptor *mFromType;
243 struct CTypeDescriptor *mToType;
246 struct CAlignmentAssumptionData {
247 struct CSourceLocation mLocation;
248 struct CSourceLocation mAssumptionLocation;
249 struct CTypeDescriptor *mType;
252 /* Local utility functions */
253 static void Report(bool isFatal, const char *pFormat, ...) __printflike(2, 3);
254 static bool isAlreadyReported(struct CSourceLocation *pLocation);
255 static size_t zDeserializeTypeWidth(struct CTypeDescriptor *pType);
256 static void DeserializeLocation(char *pBuffer, size_t zBUfferLength, struct CSourceLocation *pLocation);
257 #ifdef __SIZEOF_INT128__
258 static void DeserializeUINT128(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, __uint128_t U128);
260 static void DeserializeNumberSigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, longest L);
261 static void DeserializeNumberUnsigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, ulongest L);
263 static void DeserializeFloatOverPointer(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long *pNumber);
264 static void DeserializeFloatInlined(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber);
266 static longest llliGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber);
267 static ulongest llluGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber);
269 static void DeserializeNumberFloat(char *szLocation, char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber);
271 static void DeserializeNumber(char *szLocation, char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber);
272 static const char *DeserializeTypeCheckKind(uint8_t hhuTypeCheckKind);
273 static const char *DeserializeBuiltinCheckKind(uint8_t hhuBuiltinCheckKind);
274 static const char *DeserializeCFICheckKind(uint8_t hhuCFICheckKind);
275 static bool isNegativeNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber);
276 static bool isShiftExponentTooLarge(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber, size_t zWidth);
278 /* Unused in this implementation, emitted by the C++ check dynamic type cast. */
279 intptr_t __ubsan_vptr_type_cache[128];
281 /* Public symbols used in the instrumentation of the code generation part */
282 void __ubsan_handle_add_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
283 void __ubsan_handle_add_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
284 void __ubsan_handle_alignment_assumption(struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset);
285 void __ubsan_handle_alignment_assumption_abort(struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset);
286 void __ubsan_handle_builtin_unreachable(struct CUnreachableData *pData);
287 void __ubsan_handle_cfi_bad_type(struct CCFICheckFailData *pData, unsigned long ulVtable, bool bValidVtable, bool FromUnrecoverableHandler, unsigned long ProgramCounter, unsigned long FramePointer);
288 void __ubsan_handle_cfi_check_fail(struct CCFICheckFailData *pData, unsigned long ulValue, unsigned long ulValidVtable);
289 void __ubsan_handle_cfi_check_fail_abort(struct CCFICheckFailData *pData, unsigned long ulValue, unsigned long ulValidVtable);
290 void __ubsan_handle_divrem_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
291 void __ubsan_handle_divrem_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
292 void __ubsan_handle_dynamic_type_cache_miss(struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash);
293 void __ubsan_handle_dynamic_type_cache_miss_abort(struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash);
294 void __ubsan_handle_float_cast_overflow(struct CFloatCastOverflowData *pData, unsigned long ulFrom);
295 void __ubsan_handle_float_cast_overflow_abort(struct CFloatCastOverflowData *pData, unsigned long ulFrom);
296 void __ubsan_handle_function_type_mismatch(struct CFunctionTypeMismatchData *pData, unsigned long ulFunction);
297 void __ubsan_handle_function_type_mismatch_abort(struct CFunctionTypeMismatchData *pData, unsigned long ulFunction);
298 void __ubsan_handle_invalid_builtin(struct CInvalidBuiltinData *pData);
299 void __ubsan_handle_invalid_builtin_abort(struct CInvalidBuiltinData *pData);
300 void __ubsan_handle_load_invalid_value(struct CInvalidValueData *pData, unsigned long ulVal);
301 void __ubsan_handle_load_invalid_value_abort(struct CInvalidValueData *pData, unsigned long ulVal);
302 void __ubsan_handle_missing_return(struct CUnreachableData *pData);
303 void __ubsan_handle_mul_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
304 void __ubsan_handle_mul_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
305 void __ubsan_handle_negate_overflow(struct COverflowData *pData, unsigned long ulOldVal);
306 void __ubsan_handle_negate_overflow_abort(struct COverflowData *pData, unsigned long ulOldVal);
307 void __ubsan_handle_nonnull_arg(struct CNonNullArgData *pData);
308 void __ubsan_handle_nonnull_arg_abort(struct CNonNullArgData *pData);
309 void __ubsan_handle_nonnull_return_v1(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
310 void __ubsan_handle_nonnull_return_v1_abort(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
311 void __ubsan_handle_nullability_arg(struct CNonNullArgData *pData);
312 void __ubsan_handle_nullability_arg_abort(struct CNonNullArgData *pData);
313 void __ubsan_handle_nullability_return_v1(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
314 void __ubsan_handle_nullability_return_v1_abort(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
315 void __ubsan_handle_out_of_bounds(struct COutOfBoundsData *pData, unsigned long ulIndex);
316 void __ubsan_handle_out_of_bounds_abort(struct COutOfBoundsData *pData, unsigned long ulIndex);
317 void __ubsan_handle_pointer_overflow(struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult);
318 void __ubsan_handle_pointer_overflow_abort(struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult);
319 void __ubsan_handle_shift_out_of_bounds(struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS);
320 void __ubsan_handle_shift_out_of_bounds_abort(struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS);
321 void __ubsan_handle_sub_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
322 void __ubsan_handle_sub_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
323 void __ubsan_handle_type_mismatch(struct CTypeMismatchData *pData, unsigned long ulPointer);
324 void __ubsan_handle_type_mismatch_abort(struct CTypeMismatchData *pData, unsigned long ulPointer);
325 void __ubsan_handle_type_mismatch_v1(struct CTypeMismatchData_v1 *pData, unsigned long ulPointer);
326 void __ubsan_handle_type_mismatch_v1_abort(struct CTypeMismatchData_v1 *pData, unsigned long ulPointer);
327 void __ubsan_handle_vla_bound_not_positive(struct CVLABoundData *pData, unsigned long ulBound);
328 void __ubsan_handle_vla_bound_not_positive_abort(struct CVLABoundData *pData, unsigned long ulBound);
329 void __ubsan_get_current_report_data(const char **ppOutIssueKind, const char **ppOutMessage, const char **ppOutFilename, uint32_t *pOutLine, uint32_t *pOutCol, char **ppOutMemoryAddr);
331 static void HandleOverflow(bool isFatal, struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS, const char *szOperation);
332 static void HandleNegateOverflow(bool isFatal, struct COverflowData *pData, unsigned long ulOldValue);
333 static void HandleBuiltinUnreachable(bool isFatal, struct CUnreachableData *pData);
334 static void HandleTypeMismatch(bool isFatal, struct CSourceLocation *mLocation, struct CTypeDescriptor *mType, unsigned long mLogAlignment, uint8_t mTypeCheckKind, unsigned long ulPointer);
335 static void HandleVlaBoundNotPositive(bool isFatal, struct CVLABoundData *pData, unsigned long ulBound);
336 static void HandleOutOfBounds(bool isFatal, struct COutOfBoundsData *pData, unsigned long ulIndex);
337 static void HandleShiftOutOfBounds(bool isFatal, struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS);
338 static void HandleLoadInvalidValue(bool isFatal, struct CInvalidValueData *pData, unsigned long ulValue);
339 static void HandleInvalidBuiltin(bool isFatal, struct CInvalidBuiltinData *pData);
340 static void HandleFunctionTypeMismatch(bool isFatal, struct CFunctionTypeMismatchData *pData, unsigned long ulFunction);
341 static void HandleCFIBadType(bool isFatal, struct CCFICheckFailData *pData, unsigned long ulVtable, bool *bValidVtable, bool *FromUnrecoverableHandler, unsigned long *ProgramCounter, unsigned long *FramePointer);
342 static void HandleDynamicTypeCacheMiss(bool isFatal, struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash);
343 static void HandleFloatCastOverflow(bool isFatal, struct CFloatCastOverflowData *pData, unsigned long ulFrom);
344 static void HandleMissingReturn(bool isFatal, struct CUnreachableData *pData);
345 static void HandleNonnullArg(bool isFatal, struct CNonNullArgData *pData);
346 static void HandleNonnullReturn(bool isFatal, struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
347 static void HandlePointerOverflow(bool isFatal, struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult);
348 static void HandleAlignmentAssumption(bool isFatal, struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset);
351 HandleOverflow(bool isFatal, struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS, const char *szOperation)
353 char szLocation[LOCATION_MAXLEN];
354 char szLHS[NUMBER_MAXLEN];
355 char szRHS[NUMBER_MAXLEN];
359 if (isAlreadyReported(&pData->mLocation))
362 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
363 DeserializeNumber(szLocation, szLHS, NUMBER_MAXLEN, pData->mType, ulLHS);
364 DeserializeNumber(szLocation, szRHS, NUMBER_MAXLEN, pData->mType, ulRHS);
366 Report(isFatal, "UBSan: Undefined Behavior in %s, %s integer overflow: %s %s %s cannot be represented in type %s\n",
367 szLocation, ISSET(pData->mType->mTypeInfo, NUMBER_SIGNED_BIT) ? "signed" : "unsigned", szLHS, szOperation, szRHS, pData->mType->mTypeName);
371 HandleNegateOverflow(bool isFatal, struct COverflowData *pData, unsigned long ulOldValue)
373 char szLocation[LOCATION_MAXLEN];
374 char szOldValue[NUMBER_MAXLEN];
378 if (isAlreadyReported(&pData->mLocation))
381 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
382 DeserializeNumber(szLocation, szOldValue, NUMBER_MAXLEN, pData->mType, ulOldValue);
384 Report(isFatal, "UBSan: Undefined Behavior in %s, negation of %s cannot be represented in type %s\n",
385 szLocation, szOldValue, pData->mType->mTypeName);
389 HandleBuiltinUnreachable(bool isFatal, struct CUnreachableData *pData)
391 char szLocation[LOCATION_MAXLEN];
395 if (isAlreadyReported(&pData->mLocation))
398 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
400 Report(isFatal, "UBSan: Undefined Behavior in %s, calling __builtin_unreachable()\n",
405 HandleTypeMismatch(bool isFatal, struct CSourceLocation *mLocation, struct CTypeDescriptor *mType, unsigned long mLogAlignment, uint8_t mTypeCheckKind, unsigned long ulPointer)
407 char szLocation[LOCATION_MAXLEN];
412 if (isAlreadyReported(mLocation))
415 DeserializeLocation(szLocation, LOCATION_MAXLEN, mLocation);
417 if (ulPointer == 0) {
418 Report(isFatal, "UBSan: Undefined Behavior in %s, %s null pointer of type %s\n",
419 szLocation, DeserializeTypeCheckKind(mTypeCheckKind), mType->mTypeName);
420 } else if ((mLogAlignment - 1) & ulPointer) {
421 Report(isFatal, "UBSan: Undefined Behavior in %s, %s misaligned address %p for type %s which requires %ld byte alignment\n",
422 szLocation, DeserializeTypeCheckKind(mTypeCheckKind), REINTERPRET_CAST(void *, ulPointer), mType->mTypeName, mLogAlignment);
424 Report(isFatal, "UBSan: Undefined Behavior in %s, %s address %p with insufficient space for an object of type %s\n",
425 szLocation, DeserializeTypeCheckKind(mTypeCheckKind), REINTERPRET_CAST(void *, ulPointer), mType->mTypeName);
430 HandleVlaBoundNotPositive(bool isFatal, struct CVLABoundData *pData, unsigned long ulBound)
432 char szLocation[LOCATION_MAXLEN];
433 char szBound[NUMBER_MAXLEN];
437 if (isAlreadyReported(&pData->mLocation))
440 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
441 DeserializeNumber(szLocation, szBound, NUMBER_MAXLEN, pData->mType, ulBound);
443 Report(isFatal, "UBSan: Undefined Behavior in %s, variable length array bound value %s <= 0\n",
444 szLocation, szBound);
448 HandleOutOfBounds(bool isFatal, struct COutOfBoundsData *pData, unsigned long ulIndex)
450 char szLocation[LOCATION_MAXLEN];
451 char szIndex[NUMBER_MAXLEN];
455 if (isAlreadyReported(&pData->mLocation))
458 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
459 DeserializeNumber(szLocation, szIndex, NUMBER_MAXLEN, pData->mIndexType, ulIndex);
461 Report(isFatal, "UBSan: Undefined Behavior in %s, index %s is out of range for type %s\n",
462 szLocation, szIndex, pData->mArrayType->mTypeName);
466 HandleShiftOutOfBounds(bool isFatal, struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS)
468 char szLocation[LOCATION_MAXLEN];
469 char szLHS[NUMBER_MAXLEN];
470 char szRHS[NUMBER_MAXLEN];
474 if (isAlreadyReported(&pData->mLocation))
477 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
478 DeserializeNumber(szLocation, szLHS, NUMBER_MAXLEN, pData->mLHSType, ulLHS);
479 DeserializeNumber(szLocation, szRHS, NUMBER_MAXLEN, pData->mRHSType, ulRHS);
481 if (isNegativeNumber(szLocation, pData->mRHSType, ulRHS))
482 Report(isFatal, "UBSan: Undefined Behavior in %s, shift exponent %s is negative\n",
484 else if (isShiftExponentTooLarge(szLocation, pData->mRHSType, ulRHS, zDeserializeTypeWidth(pData->mLHSType)))
485 Report(isFatal, "UBSan: Undefined Behavior in %s, shift exponent %s is too large for %zu-bit type %s\n",
486 szLocation, szRHS, zDeserializeTypeWidth(pData->mLHSType), pData->mLHSType->mTypeName);
487 else if (isNegativeNumber(szLocation, pData->mLHSType, ulLHS))
488 Report(isFatal, "UBSan: Undefined Behavior in %s, left shift of negative value %s\n",
491 Report(isFatal, "UBSan: Undefined Behavior in %s, left shift of %s by %s places cannot be represented in type %s\n",
492 szLocation, szLHS, szRHS, pData->mLHSType->mTypeName);
496 HandleLoadInvalidValue(bool isFatal, struct CInvalidValueData *pData, unsigned long ulValue)
498 char szLocation[LOCATION_MAXLEN];
499 char szValue[NUMBER_MAXLEN];
503 if (isAlreadyReported(&pData->mLocation))
506 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
507 DeserializeNumber(szLocation, szValue, NUMBER_MAXLEN, pData->mType, ulValue);
509 Report(isFatal, "UBSan: Undefined Behavior in %s, load of value %s is not a valid value for type %s\n",
510 szLocation, szValue, pData->mType->mTypeName);
514 HandleInvalidBuiltin(bool isFatal, struct CInvalidBuiltinData *pData)
516 char szLocation[LOCATION_MAXLEN];
520 if (isAlreadyReported(&pData->mLocation))
523 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
525 Report(isFatal, "UBSan: Undefined Behavior in %s, passing zero to %s, which is not a valid argument\n",
526 szLocation, DeserializeBuiltinCheckKind(pData->mKind));
530 HandleFunctionTypeMismatch(bool isFatal, struct CFunctionTypeMismatchData *pData, unsigned long ulFunction)
532 char szLocation[LOCATION_MAXLEN];
535 * There is no a portable C solution to translate an address of a
536 * function to its name. On the cost of getting this routine simple
537 * and portable without ifdefs between the userland and the kernel
538 * just print the address of the function as-is.
540 * For better diagnostic messages in the userland, users shall use
541 * the full upstream version shipped along with the compiler toolchain.
546 if (isAlreadyReported(&pData->mLocation))
549 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
551 Report(isFatal, "UBSan: Undefined Behavior in %s, call to function %#lx through pointer to incorrect function type %s\n",
552 szLocation, ulFunction, pData->mType->mTypeName);
556 HandleCFIBadType(bool isFatal, struct CCFICheckFailData *pData, unsigned long ulVtable, bool *bValidVtable, bool *FromUnrecoverableHandler, unsigned long *ProgramCounter, unsigned long *FramePointer)
558 char szLocation[LOCATION_MAXLEN];
561 * This is a minimal implementation without diving into C++
562 * specifics and (Itanium) ABI deserialization.
567 if (isAlreadyReported(&pData->mLocation))
570 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
572 if (pData->mCheckKind == CFI_ICALL || pData->mCheckKind == CFI_VMFCALL) {
573 Report(isFatal, "UBSan: Undefined Behavior in %s, control flow integrity check for type %s failed during %s (vtable address %#lx)\n",
574 szLocation, pData->mType->mTypeName, DeserializeCFICheckKind(pData->mCheckKind), ulVtable);
576 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",
577 szLocation, pData->mType->mTypeName, DeserializeCFICheckKind(pData->mCheckKind), ulVtable, *bValidVtable ? "valid" : "invalid", *FromUnrecoverableHandler ? "unrecoverable" : "recoverable", *ProgramCounter, *FramePointer);
582 HandleDynamicTypeCacheMiss(bool isFatal, struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash)
585 char szLocation[LOCATION_MAXLEN];
590 * This UBSan handler is special as the check has to be impelemented
591 * in an implementation. In order to handle it there is need to
592 * introspect into C++ ABI internals (RTTI) and use low-level
593 * C++ runtime interfaces.
598 if (isAlreadyReported(&pData->mLocation))
601 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
603 Report(isFatal, "UBSan: Undefined Behavior in %s, %s address %#lx which might not point to an object of type %s\n"
604 szLocation, DeserializeTypeCheckKind(pData->mTypeCheckKind), ulPointer, pData->mType);
609 HandleFloatCastOverflow(bool isFatal, struct CFloatCastOverflowData *pData, unsigned long ulFrom)
611 char szLocation[LOCATION_MAXLEN];
612 char szFrom[NUMBER_MAXLEN];
616 if (isAlreadyReported(&pData->mLocation))
619 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
620 DeserializeNumber(szLocation, szFrom, NUMBER_MAXLEN, pData->mFromType, ulFrom);
622 Report(isFatal, "UBSan: Undefined Behavior in %s, %s (of type %s) is outside the range of representable values of type %s\n",
623 szLocation, szFrom, pData->mFromType->mTypeName, pData->mToType->mTypeName);
627 HandleMissingReturn(bool isFatal, struct CUnreachableData *pData)
629 char szLocation[LOCATION_MAXLEN];
633 if (isAlreadyReported(&pData->mLocation))
636 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
638 Report(isFatal, "UBSan: Undefined Behavior in %s, execution reached the end of a value-returning function without returning a value\n",
643 HandleNonnullArg(bool isFatal, struct CNonNullArgData *pData)
645 char szLocation[LOCATION_MAXLEN];
646 char szAttributeLocation[LOCATION_MAXLEN];
650 if (isAlreadyReported(&pData->mLocation))
653 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
654 if (pData->mAttributeLocation.mFilename)
655 DeserializeLocation(szAttributeLocation, LOCATION_MAXLEN, &pData->mAttributeLocation);
657 szAttributeLocation[0] = '\0';
659 Report(isFatal, "UBSan: Undefined Behavior in %s, null pointer passed as argument %d, which is declared to never be null%s%s\n",
660 szLocation, pData->mArgIndex, pData->mAttributeLocation.mFilename ? ", nonnull/_Nonnull specified in " : "", szAttributeLocation);
664 HandleNonnullReturn(bool isFatal, struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
666 char szLocation[LOCATION_MAXLEN];
667 char szAttributeLocation[LOCATION_MAXLEN];
670 ASSERT(pLocationPointer);
672 if (isAlreadyReported(pLocationPointer))
675 DeserializeLocation(szLocation, LOCATION_MAXLEN, pLocationPointer);
676 if (pData->mAttributeLocation.mFilename)
677 DeserializeLocation(szAttributeLocation, LOCATION_MAXLEN, &pData->mAttributeLocation);
679 szAttributeLocation[0] = '\0';
681 Report(isFatal, "UBSan: Undefined Behavior in %s, null pointer returned from function declared to never return null%s%s\n",
682 szLocation, pData->mAttributeLocation.mFilename ? ", nonnull/_Nonnull specified in " : "", szAttributeLocation);
686 HandlePointerOverflow(bool isFatal, struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult)
688 char szLocation[LOCATION_MAXLEN];
692 if (isAlreadyReported(&pData->mLocation))
695 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
697 Report(isFatal, "UBSan: Undefined Behavior in %s, pointer expression with base %#lx overflowed to %#lx\n",
698 szLocation, ulBase, ulResult);
702 HandleAlignmentAssumption(bool isFatal, struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset)
704 char szLocation[LOCATION_MAXLEN];
705 char szAssumptionLocation[LOCATION_MAXLEN];
706 unsigned long ulRealPointer;
710 if (isAlreadyReported(&pData->mLocation))
713 DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
715 ulRealPointer = ulPointer - ulOffset;
717 if (pData->mAssumptionLocation.mFilename != NULL) {
718 DeserializeLocation(szAssumptionLocation, LOCATION_MAXLEN,
719 &pData->mAssumptionLocation);
720 Report(isFatal, "UBSan: Undefined Behavior in %s, alignment assumption of %#lx for pointer %#lx (offset %#lx), asumption made in %s\n",
721 szLocation, ulAlignment, ulRealPointer, ulOffset,
722 szAssumptionLocation);
724 Report(isFatal, "UBSan: Undefined Behavior in %s, alignment assumption of %#lx for pointer %#lx (offset %#lx)\n",
725 szLocation, ulAlignment, ulRealPointer, ulOffset);
729 /* Definions of public symbols emitted by the instrumentation code */
731 __ubsan_handle_add_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
736 HandleOverflow(false, pData, ulLHS, ulRHS, PLUS_STRING);
740 __ubsan_handle_add_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
745 HandleOverflow(true, pData, ulLHS, ulRHS, PLUS_STRING);
749 __ubsan_handle_alignment_assumption(struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset)
754 HandleAlignmentAssumption(false, pData, ulPointer, ulAlignment, ulOffset);
758 __ubsan_handle_alignment_assumption_abort(struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset)
763 HandleAlignmentAssumption(true, pData, ulPointer, ulAlignment, ulOffset);
767 __ubsan_handle_builtin_unreachable(struct CUnreachableData *pData)
772 HandleBuiltinUnreachable(true, pData);
776 __ubsan_handle_cfi_bad_type(struct CCFICheckFailData *pData, unsigned long ulVtable, bool bValidVtable, bool FromUnrecoverableHandler, unsigned long ProgramCounter, unsigned long FramePointer)
781 HandleCFIBadType(false, pData, ulVtable, &bValidVtable, &FromUnrecoverableHandler, &ProgramCounter, &FramePointer);
785 __ubsan_handle_cfi_check_fail(struct CCFICheckFailData *pData, unsigned long ulValue, unsigned long ulValidVtable)
790 HandleCFIBadType(false, pData, ulValue, 0, 0, 0, 0);
794 __ubsan_handle_cfi_check_fail_abort(struct CCFICheckFailData *pData, unsigned long ulValue, unsigned long ulValidVtable)
799 HandleCFIBadType(true, pData, ulValue, 0, 0, 0, 0);
803 __ubsan_handle_divrem_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
808 HandleOverflow(false, pData, ulLHS, ulRHS, DIVREM_STRING);
812 __ubsan_handle_divrem_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
817 HandleOverflow(true, pData, ulLHS, ulRHS, DIVREM_STRING);
821 __ubsan_handle_dynamic_type_cache_miss(struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash)
826 HandleDynamicTypeCacheMiss(false, pData, ulPointer, ulHash);
830 __ubsan_handle_dynamic_type_cache_miss_abort(struct CDynamicTypeCacheMissData *pData, unsigned long ulPointer, unsigned long ulHash)
835 HandleDynamicTypeCacheMiss(false, pData, ulPointer, ulHash);
839 __ubsan_handle_float_cast_overflow(struct CFloatCastOverflowData *pData, unsigned long ulFrom)
844 HandleFloatCastOverflow(false, pData, ulFrom);
848 __ubsan_handle_float_cast_overflow_abort(struct CFloatCastOverflowData *pData, unsigned long ulFrom)
853 HandleFloatCastOverflow(true, pData, ulFrom);
857 __ubsan_handle_function_type_mismatch(struct CFunctionTypeMismatchData *pData, unsigned long ulFunction)
862 HandleFunctionTypeMismatch(false, pData, ulFunction);
866 __ubsan_handle_function_type_mismatch_abort(struct CFunctionTypeMismatchData *pData, unsigned long ulFunction)
871 HandleFunctionTypeMismatch(false, pData, ulFunction);
875 __ubsan_handle_invalid_builtin(struct CInvalidBuiltinData *pData)
880 HandleInvalidBuiltin(true, pData);
884 __ubsan_handle_invalid_builtin_abort(struct CInvalidBuiltinData *pData)
889 HandleInvalidBuiltin(true, pData);
893 __ubsan_handle_load_invalid_value(struct CInvalidValueData *pData, unsigned long ulValue)
898 HandleLoadInvalidValue(false, pData, ulValue);
902 __ubsan_handle_load_invalid_value_abort(struct CInvalidValueData *pData, unsigned long ulValue)
907 HandleLoadInvalidValue(true, pData, ulValue);
911 __ubsan_handle_missing_return(struct CUnreachableData *pData)
916 HandleMissingReturn(true, pData);
920 __ubsan_handle_mul_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
925 HandleOverflow(false, pData, ulLHS, ulRHS, MUL_STRING);
929 __ubsan_handle_mul_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
934 HandleOverflow(true, pData, ulLHS, ulRHS, MUL_STRING);
938 __ubsan_handle_negate_overflow(struct COverflowData *pData, unsigned long ulOldValue)
943 HandleNegateOverflow(false, pData, ulOldValue);
947 __ubsan_handle_negate_overflow_abort(struct COverflowData *pData, unsigned long ulOldValue)
952 HandleNegateOverflow(true, pData, ulOldValue);
956 __ubsan_handle_nonnull_arg(struct CNonNullArgData *pData)
961 HandleNonnullArg(false, pData);
965 __ubsan_handle_nonnull_arg_abort(struct CNonNullArgData *pData)
970 HandleNonnullArg(true, pData);
974 __ubsan_handle_nonnull_return_v1(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
978 ASSERT(pLocationPointer);
980 HandleNonnullReturn(false, pData, pLocationPointer);
984 __ubsan_handle_nonnull_return_v1_abort(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
988 ASSERT(pLocationPointer);
990 HandleNonnullReturn(true, pData, pLocationPointer);
994 __ubsan_handle_nullability_arg(struct CNonNullArgData *pData)
999 HandleNonnullArg(false, pData);
1003 __ubsan_handle_nullability_arg_abort(struct CNonNullArgData *pData)
1008 HandleNonnullArg(true, pData);
1012 __ubsan_handle_nullability_return_v1(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
1016 ASSERT(pLocationPointer);
1018 HandleNonnullReturn(false, pData, pLocationPointer);
1022 __ubsan_handle_nullability_return_v1_abort(struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer)
1026 ASSERT(pLocationPointer);
1028 HandleNonnullReturn(true, pData, pLocationPointer);
1032 __ubsan_handle_out_of_bounds(struct COutOfBoundsData *pData, unsigned long ulIndex)
1037 HandleOutOfBounds(false, pData, ulIndex);
1041 __ubsan_handle_out_of_bounds_abort(struct COutOfBoundsData *pData, unsigned long ulIndex)
1046 HandleOutOfBounds(true, pData, ulIndex);
1050 __ubsan_handle_pointer_overflow(struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult)
1055 HandlePointerOverflow(false, pData, ulBase, ulResult);
1059 __ubsan_handle_pointer_overflow_abort(struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult)
1064 HandlePointerOverflow(true, pData, ulBase, ulResult);
1068 __ubsan_handle_shift_out_of_bounds(struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS)
1073 HandleShiftOutOfBounds(false, pData, ulLHS, ulRHS);
1077 __ubsan_handle_shift_out_of_bounds_abort(struct CShiftOutOfBoundsData *pData, unsigned long ulLHS, unsigned long ulRHS)
1082 HandleShiftOutOfBounds(true, pData, ulLHS, ulRHS);
1086 __ubsan_handle_sub_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
1091 HandleOverflow(false, pData, ulLHS, ulRHS, MINUS_STRING);
1095 __ubsan_handle_sub_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
1100 HandleOverflow(true, pData, ulLHS, ulRHS, MINUS_STRING);
1104 __ubsan_handle_type_mismatch(struct CTypeMismatchData *pData, unsigned long ulPointer)
1109 HandleTypeMismatch(false, &pData->mLocation, pData->mType, pData->mLogAlignment, pData->mTypeCheckKind, ulPointer);
1113 __ubsan_handle_type_mismatch_abort(struct CTypeMismatchData *pData, unsigned long ulPointer)
1118 HandleTypeMismatch(true, &pData->mLocation, pData->mType, pData->mLogAlignment, pData->mTypeCheckKind, ulPointer);
1122 __ubsan_handle_type_mismatch_v1(struct CTypeMismatchData_v1 *pData, unsigned long ulPointer)
1127 HandleTypeMismatch(false, &pData->mLocation, pData->mType, __BIT(pData->mLogAlignment), pData->mTypeCheckKind, ulPointer);
1131 __ubsan_handle_type_mismatch_v1_abort(struct CTypeMismatchData_v1 *pData, unsigned long ulPointer)
1136 HandleTypeMismatch(true, &pData->mLocation, pData->mType, __BIT(pData->mLogAlignment), pData->mTypeCheckKind, ulPointer);
1140 __ubsan_handle_vla_bound_not_positive(struct CVLABoundData *pData, unsigned long ulBound)
1145 HandleVlaBoundNotPositive(false, pData, ulBound);
1149 __ubsan_handle_vla_bound_not_positive_abort(struct CVLABoundData *pData, unsigned long ulBound)
1154 HandleVlaBoundNotPositive(true, pData, ulBound);
1158 __ubsan_get_current_report_data(const char **ppOutIssueKind, const char **ppOutMessage, const char **ppOutFilename, uint32_t *pOutLine, uint32_t *pOutCol, char **ppOutMemoryAddr)
1163 * The __ubsan_on_report() feature is non trivial to implement in a
1164 * shared code between the kernel and userland. It's also opening
1165 * new sets of potential problems as we are not expected to slow down
1166 * execution of certain kernel subsystems (synchronization issues,
1167 * interrupt handling etc).
1169 * A proper solution would need probably a lock-free bounded queue built
1170 * with atomic operations with the property of miltiple consumers and
1171 * multiple producers. Maintaining and validating such code is not
1174 * A legitimate user - besides testing framework - is a debugger plugin
1175 * intercepting reports from the UBSan instrumentation. For such
1176 * scenarios it is better to run the Clang/GCC version.
1180 /* Local utility functions */
1183 Report(bool isFatal, const char *pFormat, ...)
1189 va_start(ap, pFormat);
1190 #if defined(_KERNEL)
1192 vpanic(pFormat, ap);
1194 vprintf(pFormat, ap);
1196 if (ubsan_flags == -1) {
1200 ubsan_flags = UBSAN_STDERR;
1202 if (getenv_r("LIBC_UBSAN", buf, sizeof(buf)) != -1) {
1203 for (p = buf; *p; p++) {
1206 SET(ubsan_flags, UBSAN_ABORT);
1209 CLR(ubsan_flags, UBSAN_ABORT);
1212 SET(ubsan_flags, UBSAN_STDERR);
1215 CLR(ubsan_flags, UBSAN_STDERR);
1218 SET(ubsan_flags, UBSAN_SYSLOG);
1221 CLR(ubsan_flags, UBSAN_SYSLOG);
1224 SET(ubsan_flags, UBSAN_STDOUT);
1227 CLR(ubsan_flags, UBSAN_STDOUT);
1236 // The *v*print* functions can flush the va_list argument.
1237 // Create a local copy for each call to prevent invalid read.
1238 if (ISSET(ubsan_flags, UBSAN_STDOUT)) {
1241 vprintf(pFormat, tmp);
1245 if (ISSET(ubsan_flags, UBSAN_STDERR)) {
1248 vfprintf(stderr, pFormat, tmp);
1252 if (ISSET(ubsan_flags, UBSAN_SYSLOG)) {
1255 struct syslog_data SyslogData = SYSLOG_DATA_INIT;
1256 ubsan_vsyslog(LOG_DEBUG | LOG_USER, &SyslogData, pFormat, tmp);
1259 if (isFatal || ISSET(ubsan_flags, UBSAN_ABORT)) {
1268 isAlreadyReported(struct CSourceLocation *pLocation)
1271 * This code is shared between libc, kernel and standalone usage.
1272 * It shall work in early bootstrap phase of both of them.
1275 uint32_t siOldValue;
1276 volatile uint32_t *pLine;
1280 pLine = &pLocation->mLine;
1283 siOldValue = *pLine;
1284 } while (__sync_val_compare_and_swap(pLine, siOldValue, siOldValue | ACK_REPORTED) != siOldValue);
1286 return ISSET(siOldValue, ACK_REPORTED);
1290 zDeserializeTypeWidth(struct CTypeDescriptor *pType)
1296 switch (pType->mTypeKind) {
1298 zWidth = __BIT(__SHIFTOUT(pType->mTypeInfo, ~NUMBER_SIGNED_BIT));
1301 zWidth = pType->mTypeInfo;
1304 Report(true, "UBSan: Unknown variable type %#04" PRIx16 "\n", pType->mTypeKind);
1308 /* Invalid width will be transformed to 0 */
1315 DeserializeLocation(char *pBuffer, size_t zBUfferLength, struct CSourceLocation *pLocation)
1319 ASSERT(pLocation->mFilename);
1321 snprintf(pBuffer, zBUfferLength, "%s:%" PRIu32 ":%" PRIu32, pLocation->mFilename, pLocation->mLine & (uint32_t)~ACK_REPORTED, pLocation->mColumn);
1324 #ifdef __SIZEOF_INT128__
1326 DeserializeUINT128(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, __uint128_t U128)
1328 char szBuf[3]; /* 'XX\0' */
1329 char rgNumber[sizeof(ulongest)];
1332 memcpy(rgNumber, &U128, sizeof(U128));
1334 strlcpy(pBuffer, "Undecoded-128-bit-Integer-Type (0x", zBUfferLength);
1335 #if BYTE_ORDER == LITTLE_ENDIAN
1336 for (zI = sizeof(ulongest) - 1; zI >= 0; zI--) {
1338 for (zI = 0; zI < (ssize_t)sizeof(ulongest); zI++) {
1340 snprintf(szBuf, sizeof(szBuf), "%02" PRIx8, rgNumber[zI]);
1341 strlcat(pBuffer, szBuf, zBUfferLength);
1343 strlcat(pBuffer, ")", zBUfferLength);
1348 DeserializeNumberSigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, longest L)
1352 ASSERT(zBUfferLength > 0);
1354 ASSERT(ISSET(pType->mTypeInfo, NUMBER_SIGNED_BIT));
1356 switch (zDeserializeTypeWidth(pType)) {
1358 ASSERT(0 && "Invalid codepath");
1360 #ifdef __SIZEOF_INT128__
1362 DeserializeUINT128(pBuffer, zBUfferLength, pType, STATIC_CAST(__uint128_t, L));
1369 snprintf(pBuffer, zBUfferLength, "%" PRId64, STATIC_CAST(int64_t, L));
1375 DeserializeNumberUnsigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, ulongest L)
1379 ASSERT(zBUfferLength > 0);
1381 ASSERT(!ISSET(pType->mTypeInfo, NUMBER_SIGNED_BIT));
1383 switch (zDeserializeTypeWidth(pType)) {
1385 ASSERT(0 && "Invalid codepath");
1387 #ifdef __SIZEOF_INT128__
1389 DeserializeUINT128(pBuffer, zBUfferLength, pType, STATIC_CAST(__uint128_t, L));
1396 snprintf(pBuffer, zBUfferLength, "%" PRIu64, STATIC_CAST(uint64_t, L));
1403 DeserializeFloatOverPointer(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long *pNumber)
1406 #ifdef __HAVE_LONG_DOUBLE
1411 ASSERT(zBUfferLength > 0);
1415 * This function handles 64-bit number over a pointer on 32-bit CPUs.
1417 ASSERT((sizeof(*pNumber) * CHAR_BIT < WIDTH_64) || (zDeserializeTypeWidth(pType) >= WIDTH_64));
1418 ASSERT(sizeof(D) == sizeof(uint64_t));
1419 #ifdef __HAVE_LONG_DOUBLE
1420 ASSERT(sizeof(LD) > sizeof(uint64_t));
1423 switch (zDeserializeTypeWidth(pType)) {
1424 #ifdef __HAVE_LONG_DOUBLE
1428 memcpy(&LD, pNumber, sizeof(long double));
1429 snprintf(pBuffer, zBUfferLength, "%Lg", LD);
1433 memcpy(&D, pNumber, sizeof(double));
1434 snprintf(pBuffer, zBUfferLength, "%g", D);
1440 DeserializeFloatInlined(char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber)
1447 ASSERT(zBUfferLength > 0);
1449 ASSERT(sizeof(F) == sizeof(uint32_t));
1450 ASSERT(sizeof(D) == sizeof(uint64_t));
1452 switch (zDeserializeTypeWidth(pType)) {
1454 memcpy(&D, &ulNumber, sizeof(double));
1455 snprintf(pBuffer, zBUfferLength, "%g", D);
1459 * On supported platforms sizeof(float)==sizeof(uint32_t)
1460 * unsigned long is either 32 or 64-bit, cast it to 32-bit
1461 * value in order to call memcpy(3) in an Endian-aware way.
1463 U32 = STATIC_CAST(uint32_t, ulNumber);
1464 memcpy(&F, &U32, sizeof(float));
1465 snprintf(pBuffer, zBUfferLength, "%g", F);
1468 snprintf(pBuffer, zBUfferLength, "Undecoded-16-bit-Floating-Type (%#04" PRIx16 ")", STATIC_CAST(uint16_t, ulNumber));
1475 llliGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber)
1477 size_t zNumberWidth;
1483 zNumberWidth = zDeserializeTypeWidth(pType);
1484 switch (zNumberWidth) {
1486 Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation);
1489 #ifdef __SIZEOF_INT128__
1490 memcpy(&L, REINTERPRET_CAST(longest *, ulNumber), sizeof(longest));
1492 Report(true, "UBSan: Unexpected 128-Bit Type in %s\n", szLocation);
1497 if (sizeof(ulNumber) * CHAR_BIT < WIDTH_64) {
1498 L = *REINTERPRET_CAST(int64_t *, ulNumber);
1500 L = STATIC_CAST(int64_t, STATIC_CAST(uint64_t, ulNumber));
1504 L = STATIC_CAST(int32_t, STATIC_CAST(uint32_t, ulNumber));
1507 L = STATIC_CAST(int16_t, STATIC_CAST(uint16_t, ulNumber));
1510 L = STATIC_CAST(int8_t, STATIC_CAST(uint8_t, ulNumber));
1518 llluGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber)
1520 size_t zNumberWidth;
1525 zNumberWidth = zDeserializeTypeWidth(pType);
1526 switch (zNumberWidth) {
1528 Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation);
1531 #ifdef __SIZEOF_INT128__
1532 memcpy(&UL, REINTERPRET_CAST(ulongest *, ulNumber), sizeof(ulongest));
1535 Report(true, "UBSan: Unexpected 128-Bit Type in %s\n", szLocation);
1539 if (sizeof(ulNumber) * CHAR_BIT < WIDTH_64) {
1540 UL = *REINTERPRET_CAST(uint64_t *, ulNumber);
1558 DeserializeNumberFloat(char *szLocation, char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber)
1560 size_t zNumberWidth;
1564 ASSERT(zBUfferLength > 0);
1566 ASSERT(pType->mTypeKind == KIND_FLOAT);
1568 zNumberWidth = zDeserializeTypeWidth(pType);
1569 switch (zNumberWidth) {
1571 Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation);
1573 #ifdef __HAVE_LONG_DOUBLE
1577 DeserializeFloatOverPointer(pBuffer, zBUfferLength, pType, REINTERPRET_CAST(unsigned long *, ulNumber));
1581 if (sizeof(ulNumber) * CHAR_BIT < WIDTH_64) {
1582 DeserializeFloatOverPointer(pBuffer, zBUfferLength, pType, REINTERPRET_CAST(unsigned long *, ulNumber));
1587 DeserializeFloatInlined(pBuffer, zBUfferLength, pType, ulNumber);
1594 DeserializeNumber(char *szLocation, char *pBuffer, size_t zBUfferLength, struct CTypeDescriptor *pType, unsigned long ulNumber)
1599 ASSERT(zBUfferLength > 0);
1602 switch(pType->mTypeKind) {
1604 if (ISSET(pType->mTypeInfo, NUMBER_SIGNED_BIT)) {
1605 longest L = llliGetNumber(szLocation, pType, ulNumber);
1606 DeserializeNumberSigned(pBuffer, zBUfferLength, pType, L);
1608 ulongest UL = llluGetNumber(szLocation, pType, ulNumber);
1609 DeserializeNumberUnsigned(pBuffer, zBUfferLength, pType, UL);
1614 Report(true, "UBSan: Unexpected Float Type in %s\n", szLocation);
1617 DeserializeNumberFloat(szLocation, pBuffer, zBUfferLength, pType, ulNumber);
1621 Report(true, "UBSan: Unknown Type in %s\n", szLocation);
1628 DeserializeTypeCheckKind(uint8_t hhuTypeCheckKind)
1630 const char *rgczTypeCheckKinds[] = {
1633 "reference binding to",
1634 "member access within",
1636 "constructor call on",
1640 "cast to virtual base of",
1641 "_Nonnull binding to",
1642 "dynamic operation on"
1645 ASSERT(__arraycount(rgczTypeCheckKinds) > hhuTypeCheckKind);
1647 return rgczTypeCheckKinds[hhuTypeCheckKind];
1651 DeserializeBuiltinCheckKind(uint8_t hhuBuiltinCheckKind)
1653 const char *rgczBuiltinCheckKinds[] = {
1658 ASSERT(__arraycount(rgczBuiltinCheckKinds) > hhuBuiltinCheckKind);
1660 return rgczBuiltinCheckKinds[hhuBuiltinCheckKind];
1664 DeserializeCFICheckKind(uint8_t hhuCFICheckKind)
1666 const char *rgczCFICheckKinds[] = {
1667 "virtual call", // CFI_VCALL
1668 "non-virtual call", // CFI_NVCALL
1669 "base-to-derived cast", // CFI_DERIVEDCAST
1670 "cast to unrelated type", // CFI_UNRELATEDCAST
1671 "indirect function call", // CFI_ICALL
1672 "non-virtual pointer to member function call", // CFI_NVMFCALL
1673 "virtual pointer to member function call", // CFI_VMFCALL
1676 ASSERT(__arraycount(rgczCFICheckKinds) > hhuCFICheckKind);
1678 return rgczCFICheckKinds[hhuCFICheckKind];
1682 isNegativeNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber)
1687 ASSERT(pType->mTypeKind == KIND_INTEGER);
1689 if (!ISSET(pType->mTypeInfo, NUMBER_SIGNED_BIT))
1692 return llliGetNumber(szLocation, pType, ulNumber) < 0;
1696 isShiftExponentTooLarge(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulNumber, size_t zWidth)
1701 ASSERT(pType->mTypeKind == KIND_INTEGER);
1703 return llluGetNumber(szLocation, pType, ulNumber) >= zWidth;