1- /* $NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $ */
1+ /* $NetBSD: ubsan.c,v 1.6 2019/06/17 18:55:37 kamil Exp $ */
22
33/*-
44 * Copyright (c) 2018 The NetBSD Foundation, Inc.
4141
4242#include <sys/cdefs.h>
4343#if defined(_KERNEL )
44- __KERNEL_RCSID (0 , "$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $" );
44+ __KERNEL_RCSID (0 , "$NetBSD: ubsan.c,v 1.6 2019/06/17 18:55:37 kamil Exp $" );
4545#else
46- __RCSID ("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $" );
46+ __RCSID ("$NetBSD: ubsan.c,v 1.6 2019/06/17 18:55:37 kamil Exp $" );
4747#endif
4848
4949#if defined(_KERNEL )
@@ -83,6 +83,12 @@ __RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
8383#define CLR (t , f ) ((t) &= ~(f))
8484#endif
8585
86+ #ifdef UBSAN_ALWAYS_FATAL
87+ static const bool alwaysFatal = true;
88+ #else
89+ static const bool alwaysFatal = false;
90+ #endif
91+
8692#define REINTERPRET_CAST (__dt , __st ) ((__dt)(__st))
8793#define STATIC_CAST (__dt , __st ) ((__dt)(__st))
8894
@@ -114,7 +120,7 @@ __RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
114120
115121#define NUMBER_SIGNED_BIT 1U
116122
117- #if __SIZEOF_INT128__
123+ #ifdef __SIZEOF_INT128__
118124typedef __int128 longest ;
119125typedef unsigned __int128 ulongest ;
120126#else
@@ -589,7 +595,7 @@ HandleCFIBadType(bool isFatal, struct CCFICheckFailData *pData, unsigned long ul
589595 szLocation , pData -> mType -> mTypeName , DeserializeCFICheckKind (pData -> mCheckKind ), ulVtable );
590596 } else {
591597 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" ,
592- szLocation , pData -> mType -> mTypeName , DeserializeCFICheckKind (pData -> mCheckKind ), ulVtable , bValidVtable && * bValidVtable ? "valid" : "invalid" , FromUnrecoverableHandler && * FromUnrecoverableHandler ? "unrecoverable" : "recoverable" , ProgramCounter ? * ProgramCounter : 0 , FramePointer ? * FramePointer : 0 );
598+ szLocation , pData -> mType -> mTypeName , DeserializeCFICheckKind (pData -> mCheckKind ), ulVtable , * bValidVtable ? "valid" : "invalid" , * FromUnrecoverableHandler ? "unrecoverable" : "recoverable" , * ProgramCounter , * FramePointer );
593599 }
594600}
595601
@@ -1174,7 +1180,7 @@ Report(bool isFatal, const char *pFormat, ...)
11741180
11751181 va_start (ap , pFormat );
11761182#if defined(_KERNEL )
1177- if (isFatal )
1183+ if (isFatal || alwaysFatal )
11781184 vpanic (pFormat , ap );
11791185 else
11801186 vprintf (pFormat , ap );
@@ -1242,8 +1248,9 @@ Report(bool isFatal, const char *pFormat, ...)
12421248 ubsan_vsyslog (LOG_DEBUG | LOG_USER , & SyslogData , pFormat , tmp );
12431249 va_end (tmp );
12441250 }
1245- if (isFatal || ISSET (ubsan_flags , UBSAN_ABORT )) {
1251+ if (isFatal || alwaysFatal || ISSET (ubsan_flags , UBSAN_ABORT )) {
12461252 abort ();
1253+ __unreachable ();
12471254 /* NOTREACHED */
12481255 }
12491256#endif
@@ -1288,6 +1295,7 @@ zDeserializeTypeWidth(struct CTypeDescriptor *pType)
12881295 break ;
12891296 default :
12901297 Report (true, "UBSan: Unknown variable type %#04" PRIx16 "\n" , pType -> mTypeKind );
1298+ __unreachable ();
12911299 /* NOTREACHED */
12921300 }
12931301
@@ -1342,15 +1350,19 @@ DeserializeNumberSigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescrip
13421350 switch (zDeserializeTypeWidth (pType )) {
13431351 default :
13441352 ASSERT (0 && "Invalid codepath" );
1353+ __unreachable ();
13451354 /* NOTREACHED */
13461355#ifdef __SIZEOF_INT128__
13471356 case WIDTH_128 :
13481357 DeserializeUINT128 (pBuffer , zBUfferLength , pType , STATIC_CAST (__uint128_t , L ));
13491358 break ;
13501359#endif
13511360 case WIDTH_64 :
1361+ /* FALLTHROUGH */
13521362 case WIDTH_32 :
1363+ /* FALLTHROUGH */
13531364 case WIDTH_16 :
1365+ /* FALLTHROUGH */
13541366 case WIDTH_8 :
13551367 snprintf (pBuffer , zBUfferLength , "%" PRId64 , STATIC_CAST (int64_t , L ));
13561368 break ;
@@ -1369,15 +1381,19 @@ DeserializeNumberUnsigned(char *pBuffer, size_t zBUfferLength, struct CTypeDescr
13691381 switch (zDeserializeTypeWidth (pType )) {
13701382 default :
13711383 ASSERT (0 && "Invalid codepath" );
1384+ __unreachable ();
13721385 /* NOTREACHED */
13731386#ifdef __SIZEOF_INT128__
13741387 case WIDTH_128 :
13751388 DeserializeUINT128 (pBuffer , zBUfferLength , pType , STATIC_CAST (__uint128_t , L ));
13761389 break ;
13771390#endif
13781391 case WIDTH_64 :
1392+ /* FALLTHROUGH */
13791393 case WIDTH_32 :
1394+ /* FALLTHROUGH */
13801395 case WIDTH_16 :
1396+ /* FALLTHROUGH */
13811397 case WIDTH_8 :
13821398 snprintf (pBuffer , zBUfferLength , "%" PRIu64 , STATIC_CAST (uint64_t , L ));
13831399 break ;
@@ -1409,7 +1425,9 @@ DeserializeFloatOverPointer(char *pBuffer, size_t zBUfferLength, struct CTypeDes
14091425 switch (zDeserializeTypeWidth (pType )) {
14101426#ifdef __HAVE_LONG_DOUBLE
14111427 case WIDTH_128 :
1428+ /* FALLTHROUGH */
14121429 case WIDTH_96 :
1430+ /* FALLTHROUGH */
14131431 case WIDTH_80 :
14141432 memcpy (& LD , pNumber , sizeof (long double ));
14151433 snprintf (pBuffer , zBUfferLength , "%Lg" , LD );
@@ -1470,15 +1488,17 @@ llliGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulN
14701488 switch (zNumberWidth ) {
14711489 default :
14721490 Report (true, "UBSan: Unexpected %zu-Bit Type in %s\n" , zNumberWidth , szLocation );
1491+ __unreachable ();
14731492 /* NOTREACHED */
14741493 case WIDTH_128 :
14751494#ifdef __SIZEOF_INT128__
14761495 memcpy (& L , REINTERPRET_CAST (longest * , ulNumber ), sizeof (longest ));
1496+ break ;
14771497#else
14781498 Report (true, "UBSan: Unexpected 128-Bit Type in %s\n" , szLocation );
1499+ __unreachable ();
14791500 /* NOTREACHED */
14801501#endif
1481- break ;
14821502 case WIDTH_64 :
14831503 if (sizeof (ulNumber ) * CHAR_BIT < WIDTH_64 ) {
14841504 L = * REINTERPRET_CAST (int64_t * , ulNumber );
@@ -1512,13 +1532,15 @@ llluGetNumber(char *szLocation, struct CTypeDescriptor *pType, unsigned long ulN
15121532 switch (zNumberWidth ) {
15131533 default :
15141534 Report (true, "UBSan: Unexpected %zu-Bit Type in %s\n" , zNumberWidth , szLocation );
1535+ __unreachable ();
15151536 /* NOTREACHED */
15161537 case WIDTH_128 :
15171538#ifdef __SIZEOF_INT128__
15181539 memcpy (& UL , REINTERPRET_CAST (ulongest * , ulNumber ), sizeof (ulongest ));
15191540 break ;
15201541#else
15211542 Report (true, "UBSan: Unexpected 128-Bit Type in %s\n" , szLocation );
1543+ __unreachable ();
15221544 /* NOTREACHED */
15231545#endif
15241546 case WIDTH_64 :
@@ -1555,10 +1577,13 @@ DeserializeNumberFloat(char *szLocation, char *pBuffer, size_t zBUfferLength, st
15551577 switch (zNumberWidth ) {
15561578 default :
15571579 Report (true, "UBSan: Unexpected %zu-Bit Type in %s\n" , zNumberWidth , szLocation );
1580+ __unreachable ();
15581581 /* NOTREACHED */
15591582#ifdef __HAVE_LONG_DOUBLE
15601583 case WIDTH_128 :
1584+ /* FALLTHROUGH */
15611585 case WIDTH_96 :
1586+ /* FALLTHROUGH */
15621587 case WIDTH_80 :
15631588 DeserializeFloatOverPointer (pBuffer , zBUfferLength , pType , REINTERPRET_CAST (unsigned long * , ulNumber ));
15641589 break ;
@@ -1568,7 +1593,9 @@ DeserializeNumberFloat(char *szLocation, char *pBuffer, size_t zBUfferLength, st
15681593 DeserializeFloatOverPointer (pBuffer , zBUfferLength , pType , REINTERPRET_CAST (unsigned long * , ulNumber ));
15691594 break ;
15701595 }
1596+ /* FALLTHROUGH */
15711597 case WIDTH_32 :
1598+ /* FALLTHROUGH */
15721599 case WIDTH_16 :
15731600 DeserializeFloatInlined (pBuffer , zBUfferLength , pType , ulNumber );
15741601 break ;
@@ -1598,15 +1625,16 @@ DeserializeNumber(char *szLocation, char *pBuffer, size_t zBUfferLength, struct
15981625 case KIND_FLOAT :
15991626#ifdef _KERNEL
16001627 Report (true, "UBSan: Unexpected Float Type in %s\n" , szLocation );
1628+ __unreachable ();
16011629 /* NOTREACHED */
16021630#else
16031631 DeserializeNumberFloat (szLocation , pBuffer , zBUfferLength , pType , ulNumber );
1604- #endif
16051632 break ;
1633+ #endif
16061634 case KIND_UNKNOWN :
16071635 Report (true, "UBSan: Unknown Type in %s\n" , szLocation );
1636+ __unreachable ();
16081637 /* NOTREACHED */
1609- break ;
16101638 }
16111639}
16121640
0 commit comments