CVS commit: src/common/lib/libc/misc

2020-03-08 Thread Kamil Rytarowski
Module Name:src
Committed By:   kamil
Date:   Sun Mar  8 21:35:03 UTC 2020

Modified Files:
src/common/lib/libc/misc: ubsan.c

Log Message:
Add support for alignment_assumptions in uubsan

Cherry-pick from FreeBSD:

>From 7c1bc5ffc2fa68ddc76e5ea8a3a1a6fdfeee57f0 Mon Sep 17 00:00:00 2001
From: andrew 
Date: Tue, 28 May 2019 09:12:15 +
Subject: [PATCH] Teach the kernel KUBSAN runtime about alignment_assumption

This checks the alignment of a given pointer is sufficient for the
requested alignment asked for. This fixes the build with a recent
llvm/clang.

Sponsored by:   DARPA, AFRL


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/common/lib/libc/misc/ubsan.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libc/misc/ubsan.c
diff -u src/common/lib/libc/misc/ubsan.c:1.9 src/common/lib/libc/misc/ubsan.c:1.10
--- src/common/lib/libc/misc/ubsan.c:1.9	Fri Nov  1 14:54:07 2019
+++ src/common/lib/libc/misc/ubsan.c	Sun Mar  8 21:35:03 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: ubsan.c,v 1.9 2019/11/01 14:54:07 kamil Exp $	*/
+/*	$NetBSD: ubsan.c,v 1.10 2020/03/08 21:35:03 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -38,9 +38,9 @@
 
 #include 
 #if defined(_KERNEL)
-__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.9 2019/11/01 14:54:07 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.10 2020/03/08 21:35:03 kamil Exp $");
 #else
-__RCSID("$NetBSD: ubsan.c,v 1.9 2019/11/01 14:54:07 kamil Exp $");
+__RCSID("$NetBSD: ubsan.c,v 1.10 2020/03/08 21:35:03 kamil Exp $");
 #endif
 
 #if defined(_KERNEL)
@@ -245,6 +245,12 @@ struct CImplicitConversionData {
 	uint8_t mKind;
 };
 
+struct CAlignmentAssumptionData {
+	struct CSourceLocation mLocation;
+	struct CSourceLocation mAssumptionLocation;
+	struct CTypeDescriptor *mType;
+};
+
 /* Local utility functions */
 static void Report(bool isFatal, const char *pFormat, ...) __printflike(2, 3);
 static bool isAlreadyReported(struct CSourceLocation *pLocation);
@@ -278,6 +284,8 @@ intptr_t __ubsan_vptr_type_cache[128];
 /* Public symbols used in the instrumentation of the code generation part */
 void __ubsan_handle_add_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
 void __ubsan_handle_add_overflow_abort(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS);
+void __ubsan_handle_alignment_assumption(struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset);
+void __ubsan_handle_alignment_assumption_abort(struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset);
 void __ubsan_handle_builtin_unreachable(struct CUnreachableData *pData);
 void __ubsan_handle_cfi_bad_type(struct CCFICheckFailData *pData, unsigned long ulVtable, bool bValidVtable, bool FromUnrecoverableHandler, unsigned long ProgramCounter, unsigned long FramePointer);
 void __ubsan_handle_cfi_check_fail(struct CCFICheckFailData *pData, unsigned long ulValue, unsigned long ulValidVtable);
@@ -344,6 +352,7 @@ static void HandleMissingReturn(bool isF
 static void HandleNonnullArg(bool isFatal, struct CNonNullArgData *pData);
 static void HandleNonnullReturn(bool isFatal, struct CNonNullReturnData *pData, struct CSourceLocation *pLocationPointer);
 static void HandlePointerOverflow(bool isFatal, struct CPointerOverflowData *pData, unsigned long ulBase, unsigned long ulResult);
+static void HandleAlignmentAssumption(bool isFatal, struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset);
 
 static void
 HandleOverflow(bool isFatal, struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS, const char *szOperation)
@@ -716,6 +725,34 @@ HandleImplicitConversion(bool isFatal, s
 	   szLocation, DeserializeImplicitConversionCheckKind(pData->mKind), szFrom, zDeserializeTypeWidth(pData->mFromType), ISSET(pData->mFromType->mTypeInfo, NUMBER_SIGNED_BIT) ? "signed" : "unsigned", pData->mFromType->mTypeName, pData->mToType->mTypeName, szTo, zDeserializeTypeWidth(pData->mToType), ISSET(pData->mToType->mTypeInfo, NUMBER_SIGNED_BIT) ? "signed" : "unsigned");
 }
 
+static void
+HandleAlignmentAssumption(bool isFatal, struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset)
+{
+	char szLocation[LOCATION_MAXLEN];
+	char szAssumptionLocation[LOCATION_MAXLEN];
+	unsigned long ulRealPointer;
+
+	ASSERT(pData);
+
+	if (isAlreadyReported(>mLocation))
+		return;
+
+	DeserializeLocation(szLocation, LOCATION_MAXLEN, >mLocation);
+
+	ulRealPointer = ulPointer - ulOffset;
+
+	if (pData->mAssumptionLocation.mFilename != NULL) {
+		DeserializeLocation(szAssumptionLocation, LOCATION_MAXLEN,
+		>mAssumptionLocation);
+		Report(isFatal, "UBSan: Undefined Behavior in %s, alignment assumption 

CVS commit: src/common/lib/libc/misc

2019-02-13 Thread Kamil Rytarowski
Module Name:src
Committed By:   kamil
Date:   Wed Feb 13 17:17:02 UTC 2019

Modified Files:
src/common/lib/libc/misc: ubsan.c

Log Message:
Fix kUBSan build with GCC7

Add missing __unreachable() and FALLTHROUGH keywords.

Reported by 


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/common/lib/libc/misc/ubsan.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libc/misc/ubsan.c
diff -u src/common/lib/libc/misc/ubsan.c:1.4 src/common/lib/libc/misc/ubsan.c:1.5
--- src/common/lib/libc/misc/ubsan.c:1.4	Mon Feb  4 22:07:41 2019
+++ src/common/lib/libc/misc/ubsan.c	Wed Feb 13 17:17:02 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: ubsan.c,v 1.4 2019/02/04 22:07:41 mrg Exp $	*/
+/*	$NetBSD: ubsan.c,v 1.5 2019/02/13 17:17:02 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -38,9 +38,9 @@
 
 #include 
 #if defined(_KERNEL)
-__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.4 2019/02/04 22:07:41 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.5 2019/02/13 17:17:02 kamil Exp $");
 #else
-__RCSID("$NetBSD: ubsan.c,v 1.4 2019/02/04 22:07:41 mrg Exp $");
+__RCSID("$NetBSD: ubsan.c,v 1.5 2019/02/13 17:17:02 kamil Exp $");
 #endif
 
 #if defined(_KERNEL)
@@ -110,7 +110,7 @@ __RCSID("$NetBSD: ubsan.c,v 1.4 2019/02/
 
 #define NUMBER_SIGNED_BIT	1U
 
-#if __SIZEOF_INT128__
+#ifdef __SIZEOF_INT128__
 typedef __int128 longest;
 typedef unsigned __int128 ulongest;
 #else
@@ -1192,6 +1192,7 @@ Report(bool isFatal, const char *pFormat
 	}
 	if (isFatal || ISSET(ubsan_flags, UBSAN_ABORT)) {
 		abort();
+		__unreachable();
 		/* NOTREACHED */
 	}
 #endif
@@ -1291,6 +1292,7 @@ DeserializeNumberSigned(char *pBuffer, s
 	switch (zDeserializeTypeWidth(pType)) {
 	default:
 		ASSERT(0 && "Invalid codepath");
+		__unreachable();
 		/* NOTREACHED */
 #ifdef __SIZEOF_INT128__
 	case WIDTH_128:
@@ -1298,8 +1300,11 @@ DeserializeNumberSigned(char *pBuffer, s
 		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;
@@ -1318,6 +1323,7 @@ DeserializeNumberUnsigned(char *pBuffer,
 	switch (zDeserializeTypeWidth(pType)) {
 	default:
 		ASSERT(0 && "Invalid codepath");
+		__unreachable();
 		/* NOTREACHED */
 #ifdef __SIZEOF_INT128__
 	case WIDTH_128:
@@ -1325,8 +1331,11 @@ DeserializeNumberUnsigned(char *pBuffer,
 		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;
@@ -1358,7 +1367,9 @@ DeserializeFloatOverPointer(char *pBuffe
 	switch (zDeserializeTypeWidth(pType)) {
 #ifdef __HAVE_LONG_DOUBLE
 	case WIDTH_128:
+		/* FALLTHROUGH */
 	case WIDTH_96:
+		/* FALLTHROUGH */
 	case WIDTH_80:
 		memcpy(, pNumber, sizeof(long double));
 		snprintf(pBuffer, zBUfferLength, "%Lg", LD);
@@ -1512,7 +1523,9 @@ DeserializeNumberFloat(char *szLocation,
 		/* 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;
@@ -1524,6 +1537,7 @@ DeserializeNumberFloat(char *szLocation,
 		}
 		/* FALLTHROUGH */
 	case WIDTH_32:
+		/* FALLTHROUGH */
 	case WIDTH_16:
 		DeserializeFloatInlined(pBuffer, zBUfferLength, pType, ulNumber);
 		break;



CVS commit: src/common/lib/libc/misc

2019-02-04 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Mon Feb  4 22:07:41 UTC 2019

Modified Files:
src/common/lib/libc/misc: ubsan.c

Log Message:
- use __unreachable() and move 'break's around to increase consistency
  and correctness

ok kamil@


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/common/lib/libc/misc/ubsan.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libc/misc/ubsan.c
diff -u src/common/lib/libc/misc/ubsan.c:1.3 src/common/lib/libc/misc/ubsan.c:1.4
--- src/common/lib/libc/misc/ubsan.c:1.3	Fri Aug  3 16:31:04 2018
+++ src/common/lib/libc/misc/ubsan.c	Mon Feb  4 22:07:41 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $	*/
+/*	$NetBSD: ubsan.c,v 1.4 2019/02/04 22:07:41 mrg Exp $	*/
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -38,9 +38,9 @@
 
 #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.4 2019/02/04 22:07:41 mrg Exp $");
 #else
-__RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
+__RCSID("$NetBSD: ubsan.c,v 1.4 2019/02/04 22:07:41 mrg Exp $");
 #endif
 
 #if defined(_KERNEL)
@@ -1236,6 +1236,7 @@ zDeserializeTypeWidth(struct CTypeDescri
 		break;
 	default:
 		Report(true, "UBSan: Unknown variable type %#04" PRIx16 "\n", pType->mTypeKind);
+		__unreachable();
 		/* NOTREACHED */
 	}
 
@@ -1418,15 +1419,17 @@ llliGetNumber(char *szLocation, struct C
 	switch (zNumberWidth) {
 	default:
 		Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation);
+		__unreachable();
 		/* NOTREACHED */
 	case WIDTH_128:
 #ifdef __SIZEOF_INT128__
 		memcpy(, 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);
@@ -1460,6 +1463,7 @@ llluGetNumber(char *szLocation, struct C
 	switch (zNumberWidth) {
 	default:
 		Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation);
+		__unreachable();
 		/* NOTREACHED */
 	case WIDTH_128:
 #ifdef __SIZEOF_INT128__
@@ -1467,6 +1471,7 @@ llluGetNumber(char *szLocation, struct C
 		break;
 #else
 		Report(true, "UBSan: Unexpected 128-Bit Type in %s\n", szLocation);
+		__unreachable();
 		/* NOTREACHED */
 #endif
 	case WIDTH_64:
@@ -1503,6 +1508,7 @@ DeserializeNumberFloat(char *szLocation,
 	switch (zNumberWidth) {
 	default:
 		Report(true, "UBSan: Unexpected %zu-Bit Type in %s\n", zNumberWidth, szLocation);
+		__unreachable();
 		/* NOTREACHED */
 #ifdef __HAVE_LONG_DOUBLE
 	case WIDTH_128:
@@ -1516,6 +1522,7 @@ DeserializeNumberFloat(char *szLocation,
 			DeserializeFloatOverPointer(pBuffer, zBUfferLength, pType, REINTERPRET_CAST(unsigned long *, ulNumber));
 			break;
 		}
+		/* FALLTHROUGH */
 	case WIDTH_32:
 	case WIDTH_16:
 		DeserializeFloatInlined(pBuffer, zBUfferLength, pType, ulNumber);
@@ -1546,15 +1553,16 @@ DeserializeNumber(char *szLocation, char
 	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;
 	}
 }
 



CVS commit: src/common/lib/libc/misc

2018-08-03 Thread Kamil Rytarowski
Module Name:src
Committed By:   kamil
Date:   Fri Aug  3 16:31:04 UTC 2018

Modified Files:
src/common/lib/libc/misc: ubsan.c

Log Message:
Try to fix the evbppc-powerpc64 build

Avoid "comparison between signed and unsigned integer expressions" on
Big-Endian hosts.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/common/lib/libc/misc/ubsan.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libc/misc/ubsan.c
diff -u src/common/lib/libc/misc/ubsan.c:1.2 src/common/lib/libc/misc/ubsan.c:1.3
--- src/common/lib/libc/misc/ubsan.c:1.2	Fri Aug  3 03:12:32 2018
+++ src/common/lib/libc/misc/ubsan.c	Fri Aug  3 16:31:04 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: ubsan.c,v 1.2 2018/08/03 03:12:32 kamil Exp $	*/
+/*	$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -38,9 +38,9 @@
 
 #include 
 #if defined(_KERNEL)
-__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.2 2018/08/03 03:12:32 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
 #else
-__RCSID("$NetBSD: ubsan.c,v 1.2 2018/08/03 03:12:32 kamil Exp $");
+__RCSID("$NetBSD: ubsan.c,v 1.3 2018/08/03 16:31:04 kamil Exp $");
 #endif
 
 #if defined(_KERNEL)
@@ -1269,7 +1269,7 @@ DeserializeUINT128(char *pBuffer, size_t
 #if BYTE_ORDER == LITTLE_ENDIAN
 	for (zI = sizeof(ulongest) - 1; zI >= 0; zI--) {
 #else
-	for (zI = 0; zI < sizeof(ulongest); zI++) {
+	for (zI = 0; zI < (ssize_t)sizeof(ulongest); zI++) {
 #endif
 		snprintf(szBuf, sizeof(szBuf), "%02" PRIx8, rgNumber[zI]);
 		strlcat(pBuffer, szBuf, zBUfferLength);



CVS commit: src/common/lib/libc/misc

2018-08-02 Thread Kamil Rytarowski
Module Name:src
Committed By:   kamil
Date:   Fri Aug  3 03:12:32 UTC 2018

Modified Files:
src/common/lib/libc/misc: ubsan.c

Log Message:
Tidy up the comment in ubsan.c

As noted, style has no impact on the comparison of a similar code.
This version is a reimplementation from scratch with no code and no
algirithm (whenever possible) reuse.

Public symbols and struct layout must be kept in sync with the code
generation part. It casues violation of the style like with long filenames.

My previous comment was 'too perfect' and could trigger some unnecessary
attention.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/common/lib/libc/misc/ubsan.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libc/misc/ubsan.c
diff -u src/common/lib/libc/misc/ubsan.c:1.1 src/common/lib/libc/misc/ubsan.c:1.2
--- src/common/lib/libc/misc/ubsan.c:1.1	Fri Aug  3 02:05:43 2018
+++ src/common/lib/libc/misc/ubsan.c	Fri Aug  3 03:12:32 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: ubsan.c,v 1.1 2018/08/03 02:05:43 kamil Exp $	*/
+/*	$NetBSD: ubsan.c,v 1.2 2018/08/03 03:12:32 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -32,17 +32,15 @@
  * The uBSSan versions is suitable for inclusion into libc or used standalone
  * with ATF tests.
  *
- * This file due to long symbol names and licensing reasons does not fully
- * follow the KNF style with 80-column limit. Hungarian style variables
- * and function names are on the same purpose (Pascal and Snake style names,
- * are used in different implementations).
+ * This file due to long symbol names generated by a compiler during the
+ * instrumentation process does not follow the KNF style with 80-column limit.
  */
 
 #include 
 #if defined(_KERNEL)
-__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.1 2018/08/03 02:05:43 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.2 2018/08/03 03:12:32 kamil Exp $");
 #else
-__RCSID("$NetBSD: ubsan.c,v 1.1 2018/08/03 02:05:43 kamil Exp $");
+__RCSID("$NetBSD: ubsan.c,v 1.2 2018/08/03 03:12:32 kamil Exp $");
 #endif
 
 #if defined(_KERNEL)



CVS commit: src/common/lib/libc/misc

2018-08-02 Thread Kamil Rytarowski
Module Name:src
Committed By:   kamil
Date:   Fri Aug  3 02:05:43 UTC 2018

Added Files:
src/common/lib/libc/misc: ubsan.c

Log Message:
Import micro-UBSan (ubsan.c)

This is a reimplementation of the Undefined Behavior Sanitizer with the
following properties:
 - pure C implementation,
 - no -fsanitize=vpts support, as it requires RTTI support and C++
   low-level routies to validate whether C++ objects are compatible
 - designed to be used inside libc and known as uUBSan or user-UBSan
 - designed to be shared with kernel and known as kUBSan or kernel-UBSan
 - designed to be usable with ATF tests as a standalone runtime,
   reachable without any MK* switches
 - designed to be safer for hardening as it does not have side effects on
   executables like writing to a selected location on demand
 - controllable with environment variable LIBC_UBSAN with options:
   * a - abort on report
   * A - do not abort on a report (unless a failure is unrecoverable)
   * e - output report to stderr
   * E - do not output report on stderr
   * l - output report on syslog (LOG_DEBUG | LOG_USER)
   * L - do not output report on syslog
   * o - output report on stdout
   * O - do not output report on stdout
   The default options are: "AeLO".
 - compatible with Clang (3.8, 7.x) and GCC (6.x) code generation
 - all handlers (except =vptr) from Clang/LLVM up to 7svn are supported

This file does not follow the regular KNF style, due to potential licensing
concerns.

Tested with Clang amd64+i386 and GCC amd64+i386.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/common/lib/libc/misc/ubsan.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Added files:

Index: src/common/lib/libc/misc/ubsan.c
diff -u /dev/null src/common/lib/libc/misc/ubsan.c:1.1
--- /dev/null	Fri Aug  3 02:05:43 2018
+++ src/common/lib/libc/misc/ubsan.c	Fri Aug  3 02:05:43 2018
@@ -0,0 +1,1640 @@
+/*	$NetBSD: ubsan.c,v 1.1 2018/08/03 02:05:43 kamil Exp $	*/
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+/*
+ * The micro UBSan implementation for the userland (uUBSan) and kernel (kUBSan).
+ * The uBSSan versions is suitable for inclusion into libc or used standalone
+ * with ATF tests.
+ *
+ * This file due to long symbol names and licensing reasons does not fully
+ * follow the KNF style with 80-column limit. Hungarian style variables
+ * and function names are on the same purpose (Pascal and Snake style names,
+ * are used in different implementations).
+ */
+
+#include 
+#if defined(_KERNEL)
+__KERNEL_RCSID(0, "$NetBSD: ubsan.c,v 1.1 2018/08/03 02:05:43 kamil Exp $");
+#else
+__RCSID("$NetBSD: ubsan.c,v 1.1 2018/08/03 02:05:43 kamil Exp $");
+#endif
+
+#if defined(_KERNEL)
+#include 
+#include 
+#include 
+#define ASSERT(x) KASSERT(x)
+#else
+#if defined(_LIBC)
+#include "namespace.h"
+#endif
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#if defined(_LIBC)
+#include "extern.h"
+#define ubsan_vsyslog vsyslog_ss
+#define ASSERT(x) _DIAGASSERT(x)
+#else
+#define ubsan_vsyslog vsyslog_r
+#define ASSERT(x) assert(x)
+#endif
+/* These macros are available in _KERNEL only */
+#define SET(t, f)	((t) |= (f))
+#define ISSET(t, f)	((t) & (f))
+#define CLR(t, f)	((t) &= ~(f))
+#endif
+
+#define REINTERPRET_CAST(__dt, __st)	((__dt)(__st))
+#define STATIC_CAST(__dt, __st)		((__dt)(__st))
+
+#define ACK_REPORTED	__BIT(31)
+
+#define MUL_STRING	"*"
+#define PLUS_STRING	"+"
+#define MINUS_STRING	"-"
+#define DIVREM_STRING	"divrem"
+