Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/common/lib/libc/misc Add support for alignment_assumptions i...



details:   https://anonhg.NetBSD.org/src/rev/39f49e975a05
branches:  trunk
changeset: 745678:39f49e975a05
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sun Mar 08 21:35:03 2020 +0000

description:
Add support for alignment_assumptions in uubsan

Cherry-pick from FreeBSD:

>From 7c1bc5ffc2fa68ddc76e5ea8a3a1a6fdfeee57f0 Mon Sep 17 00:00:00 2001
From: andrew <andrew%FreeBSD.org@localhost>
Date: Tue, 28 May 2019 09:12:15 +0000
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

diffstat:

 common/lib/libc/misc/ubsan.c |  61 +++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 58 insertions(+), 3 deletions(-)

diffs (111 lines):

diff -r 67d9fdf65258 -r 39f49e975a05 common/lib/libc/misc/ubsan.c
--- a/common/lib/libc/misc/ubsan.c      Sun Mar 08 20:49:31 2020 +0000
+++ b/common/lib/libc/misc/ubsan.c      Sun Mar 08 21:35:03 2020 +0000
@@ -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 <sys/cdefs.h>
 #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 @@
        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 @@
 /* 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 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 @@
               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(&pData->mLocation))
+               return;
+
+       DeserializeLocation(szLocation, LOCATION_MAXLEN, &pData->mLocation);
+
+       ulRealPointer = ulPointer - ulOffset;
+
+       if (pData->mAssumptionLocation.mFilename != NULL) {
+               DeserializeLocation(szAssumptionLocation, LOCATION_MAXLEN,
+                   &pData->mAssumptionLocation);
+               Report(isFatal, "UBSan: Undefined Behavior in %s, alignment assumption of %#lx for pointer %#lx (offset %#lx), asumption made in %s\n",
+                   szLocation, ulAlignment, ulRealPointer, ulOffset,
+                   szAssumptionLocation);
+       } else {
+               Report(isFatal, "UBSan: Undefined Behavior in %s, alignment assumption of %#lx for pointer %#lx (offset %#lx)\n",
+                   szLocation, ulAlignment, ulRealPointer, ulOffset);
+       }
+}
+
 /* Definions of public symbols emitted by the instrumentation code */
 void
 __ubsan_handle_add_overflow(struct COverflowData *pData, unsigned long ulLHS, unsigned long ulRHS)
@@ -736,6 +773,24 @@
 }
 
 void
+__ubsan_handle_alignment_assumption(struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset)
+{
+
+       ASSERT(pData);
+
+       HandleAlignmentAssumption(false, pData, ulPointer, ulAlignment, ulOffset);
+}
+
+void
+__ubsan_handle_alignment_assumption_abort(struct CAlignmentAssumptionData *pData, unsigned long ulPointer, unsigned long ulAlignment, unsigned long ulOffset)
+{
+
+       ASSERT(pData);
+
+       HandleAlignmentAssumption(true, pData, ulPointer, ulAlignment, ulOffset);
+}
+
+void
 __ubsan_handle_builtin_unreachable(struct CUnreachableData *pData)
 {
 



Home | Main Index | Thread Index | Old Index