From 75ce01f54ead4434a287ebbfe829edfbd233b98d Mon Sep 17 00:00:00 2001 From: vit9696 Date: Thu, 27 Jun 2019 00:27:51 +0300 Subject: [PATCH] OcGuardLib: Upgrade ubsan runtime to upstream --- Library/OcGuardLib/Ubsan.c | 48 ++++++++++++++++++++++++++++++-------- Library/OcGuardLib/Ubsan.h | 9 +++++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/Library/OcGuardLib/Ubsan.c b/Library/OcGuardLib/Ubsan.c index 33d0013d..0e768243 100644 --- a/Library/OcGuardLib/Ubsan.c +++ b/Library/OcGuardLib/Ubsan.c @@ -1,4 +1,4 @@ -/* $NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $ */ +/* $NetBSD: ubsan.c,v 1.6 2019/06/17 18:55:37 kamil Exp $ */ /*- * Copyright (c) 2018 The NetBSD Foundation, Inc. @@ -43,9 +43,9 @@ // OC change: unsupported in EDK2 // #include #if defined(_KERNEL) -__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $"); +__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.6 2019/06/17 18:55:37 kamil Exp $"); #else -__RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $"); +__RCSID("$NetBSD: ubsan.c,v 1.6 2019/06/17 18:55:37 kamil Exp $"); #endif #if defined(_KERNEL) @@ -85,6 +85,12 @@ __RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $"); #define CLR(t, f) ((t) &= ~(f)) #endif +#ifdef UBSAN_ALWAYS_FATAL +static const bool alwaysFatal = true; +#else +static const bool alwaysFatal = false; +#endif + #define REINTERPRET_CAST(__dt, __st) ((__dt)(__st)) #define STATIC_CAST(__dt, __st) ((__dt)(__st)) @@ -116,7 +122,7 @@ __RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $"); #define NUMBER_SIGNED_BIT 1U -#if __SIZEOF_INT128__ +#ifdef __SIZEOF_INT128__ typedef __int128 longest; typedef unsigned __int128 ulongest; #else @@ -592,7 +598,7 @@ HandleCFIBadType(bool isFatal, struct CCFICheckFailData *pData, unsigned long ul szLocation, pData->mType->mTypeName, DeserializeCFICheckKind(pData->mCheckKind), ulVtable); } else { 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", - szLocation, pData->mType->mTypeName, DeserializeCFICheckKind(pData->mCheckKind), ulVtable, bValidVtable && *bValidVtable ? "valid" : "invalid", FromUnrecoverableHandler && *FromUnrecoverableHandler ? "unrecoverable" : "recoverable", ProgramCounter ? *ProgramCounter : 0, FramePointer ? *FramePointer : 0); + szLocation, pData->mType->mTypeName, DeserializeCFICheckKind(pData->mCheckKind), ulVtable, *bValidVtable ? "valid" : "invalid", *FromUnrecoverableHandler ? "unrecoverable" : "recoverable", *ProgramCounter, *FramePointer); } } @@ -1179,7 +1185,7 @@ Report(bool isFatal, const char *pFormat, ...) va_start(ap, pFormat); #if defined(_KERNEL) - if (isFatal) + if (isFatal || alwaysFatal) vpanic(pFormat, ap); else vprintf(pFormat, ap); @@ -1247,8 +1253,9 @@ Report(bool isFatal, const char *pFormat, ...) ubsan_vsyslog(LOG_DEBUG | LOG_USER, &SyslogData, pFormat, tmp); va_end(tmp); } - if (isFatal || ISSET(ubsan_flags, UBSAN_ABORT)) { + if (isFatal || alwaysFatal || ISSET(ubsan_flags, UBSAN_ABORT)) { abort(); + __unreachable(); /* NOTREACHED */ } #endif @@ -1293,6 +1300,7 @@ zDeserializeTypeWidth(struct CTypeDescriptor *pType) break; default: Report(true, "UBSan: Unknown variable type %#04" PRIx16 "\n", pType->mTypeKind); + __unreachable(); /* NOTREACHED */ } @@ -1347,6 +1355,7 @@ DeserializeNumberSigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescrip switch (zDeserializeTypeWidth(pType)) { default: ASSERT(0 && "Invalid codepath"); + __unreachable(); /* NOTREACHED */ #ifdef __SIZEOF_INT128__ case WIDTH_128: @@ -1354,8 +1363,11 @@ DeserializeNumberSigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescrip break; #endif case WIDTH_64: + /* FALLTHROUGH */ case WIDTH_32: + /* FALLTHROUGH */ case WIDTH_16: + /* FALLTHROUGH */ case WIDTH_8: snprintf(pBuffer, zBUfferLength, "%" PRId64, STATIC_CAST(int64_t, L)); break; @@ -1374,6 +1386,7 @@ DeserializeNumberUnsigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescr switch (zDeserializeTypeWidth(pType)) { default: ASSERT(0 && "Invalid codepath"); + __unreachable(); /* NOTREACHED */ #ifdef __SIZEOF_INT128__ case WIDTH_128: @@ -1381,8 +1394,11 @@ DeserializeNumberUnsigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescr break; #endif case WIDTH_64: + /* FALLTHROUGH */ case WIDTH_32: + /* FALLTHROUGH */ case WIDTH_16: + /* FALLTHROUGH */ case WIDTH_8: snprintf(pBuffer, zBUfferLength, "%" PRIu64, STATIC_CAST(uint64_t, L)); break; @@ -1414,7 +1430,9 @@ DeserializeFloatOverPointer(char *pBuffer, size_t zBUfferLength, struct CTypeDes switch (zDeserializeTypeWidth(pType)) { #ifdef __HAVE_LONG_DOUBLE case WIDTH_128: + /* FALLTHROUGH */ case WIDTH_96: + /* FALLTHROUGH */ case WIDTH_80: memcpy(&LD, pNumber, sizeof(long double)); snprintf(pBuffer, zBUfferLength, "%Lg", LD); @@ -1475,15 +1493,17 @@ llliGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulN switch (zNumberWidth) { default: Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation); + __unreachable(); /* NOTREACHED */ case WIDTH_128: #ifdef __SIZEOF_INT128__ memcpy(&L, REINTERPRET_CAST(longest *, ulNumber), sizeof(longest)); + break; #else Report(true, "UBSan: Unexpected 128-Bit Type in %s\n", szLocation); + __unreachable(); /* NOTREACHED */ #endif - break; case WIDTH_64: if (sizeof(ulNumber) * CHAR_BIT < WIDTH_64) { L = *REINTERPRET_CAST(int64_t *, ulNumber); @@ -1517,6 +1537,7 @@ llluGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulN switch (zNumberWidth) { default: Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation); + __unreachable(); /* NOTREACHED */ case WIDTH_128: #ifdef __SIZEOF_INT128__ @@ -1524,6 +1545,7 @@ llluGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulN break; #else Report(true, "UBSan: Unexpected 128-Bit Type in %s\n", szLocation); + __unreachable(); /* NOTREACHED */ #endif case WIDTH_64: @@ -1560,10 +1582,13 @@ DeserializeNumberFloat(char *szLocation, char *pBuffer, size_t zBUfferLength, st switch (zNumberWidth) { default: Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation); + __unreachable(); /* NOTREACHED */ #ifdef __HAVE_LONG_DOUBLE case WIDTH_128: + /* FALLTHROUGH */ case WIDTH_96: + /* FALLTHROUGH */ case WIDTH_80: DeserializeFloatOverPointer(pBuffer, zBUfferLength, pType, REINTERPRET_CAST(unsigned long *, ulNumber)); break; @@ -1573,7 +1598,9 @@ DeserializeNumberFloat(char *szLocation, char *pBuffer, size_t zBUfferLength, st DeserializeFloatOverPointer(pBuffer, zBUfferLength, pType, REINTERPRET_CAST(unsigned long *, ulNumber)); break; } + /* FALLTHROUGH */ case WIDTH_32: + /* FALLTHROUGH */ case WIDTH_16: DeserializeFloatInlined(pBuffer, zBUfferLength, pType, ulNumber); break; @@ -1603,15 +1630,16 @@ DeserializeNumber(char *szLocation, char *pBuffer, size_t zBUfferLength, struct case KIND_FLOAT: #ifdef _KERNEL Report(true, "UBSan: Unexpected Float Type in %s\n", szLocation); + __unreachable(); /* NOTREACHED */ #else DeserializeNumberFloat(szLocation, pBuffer, zBUfferLength, pType, ulNumber); -#endif break; +#endif case KIND_UNKNOWN: Report(true, "UBSan: Unknown Type in %s\n", szLocation); + __unreachable(); /* NOTREACHED */ - break; } } diff --git a/Library/OcGuardLib/Ubsan.h b/Library/OcGuardLib/Ubsan.h index 0bd73a43..0d8bb678 100644 --- a/Library/OcGuardLib/Ubsan.h +++ b/Library/OcGuardLib/Ubsan.h @@ -171,6 +171,15 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define __arraycount(a) ARRAY_SIZE (a) #endif +// Support unreachable where possible +#ifndef __unreachable +#ifdef __GNUC__ +#define __unreachable() __builtin_unreachable() +#else +#define __unreachable() +#endif +#endif + // C-style printing support. #define TINYPRINTF_DEFINE_TFP_SPRINTF 1 typedef void (*putcf) (void *, char);