From 987868042254c8fe7d25b2d7060040f18fa36710 Mon Sep 17 00:00:00 2001 From: luwang1103 <576420940@qq.com> Date: Wed, 17 Jul 2024 22:34:14 -0400 Subject: [PATCH] Backport ff30240926224b2f98e173bcd606c157af788919 --- src/hotspot/share/cds/archiveHeapLoader.cpp | 5 +-- src/hotspot/share/prims/unsafe.cpp | 5 +-- src/hotspot/share/sanitizers/ub.hpp | 43 +++++++++++++++++++++ src/hotspot/share/utilities/vmError.cpp | 5 +-- 4 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/hotspot/share/sanitizers/ub.hpp diff --git a/src/hotspot/share/cds/archiveHeapLoader.cpp b/src/hotspot/share/cds/archiveHeapLoader.cpp index 57a96a8c4e0..feaf245d22c 100644 --- a/src/hotspot/share/cds/archiveHeapLoader.cpp +++ b/src/hotspot/share/cds/archiveHeapLoader.cpp @@ -34,6 +34,7 @@ #include "memory/iterator.inline.hpp" #include "memory/resourceArea.hpp" #include "memory/universe.hpp" +#include "sanitizers/ub.hpp" #include "utilities/bitMap.inline.hpp" #include "utilities/copy.hpp" @@ -61,9 +62,7 @@ ptrdiff_t ArchiveHeapLoader::_mapped_heap_delta = 0; // Every mapped region is offset by _mapped_heap_delta from its requested address. // See FileMapInfo::heap_region_requested_address(). -#if defined(__clang__) || defined(__GNUC__) -__attribute__((no_sanitize("undefined"))) -#endif +ATTRIBUTE_NO_UBSAN void ArchiveHeapLoader::init_mapped_heap_info(address mapped_heap_bottom, ptrdiff_t delta, int dumptime_oop_shift) { assert(!_mapped_heap_relocation_initialized, "only once"); if (!UseCompressedOops) { diff --git a/src/hotspot/share/prims/unsafe.cpp b/src/hotspot/share/prims/unsafe.cpp index d290627c197..942d9100c29 100644 --- a/src/hotspot/share/prims/unsafe.cpp +++ b/src/hotspot/share/prims/unsafe.cpp @@ -55,6 +55,7 @@ #include "runtime/threadSMR.hpp" #include "runtime/vmOperations.hpp" #include "runtime/vm_version.hpp" +#include "sanitizers/ub.hpp" #include "services/threadService.hpp" #include "utilities/align.hpp" #include "utilities/copy.hpp" @@ -244,9 +245,7 @@ class MemoryAccess : StackObj { // we use this method at some places for writing to 0 e.g. to cause a crash; // ubsan does not know that this is the desired behavior -#if defined(__clang__) || defined(__GNUC__) -__attribute__((no_sanitize("undefined"))) -#endif + ATTRIBUTE_NO_UBSAN void put(T x) { GuardUnsafeAccess guard(_thread); *addr() = normalize_for_write(x); diff --git a/src/hotspot/share/sanitizers/ub.hpp b/src/hotspot/share/sanitizers/ub.hpp new file mode 100644 index 00000000000..b62ffed0b25 --- /dev/null +++ b/src/hotspot/share/sanitizers/ub.hpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2024 SAP SE. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#ifndef SHARE_SANITIZERS_UB_HPP +#define SHARE_SANITIZERS_UB_HPP + +// ATTRIBUTE_NO_UBSAN +// +// Function attribute which informs the compiler to disable UBSan checks in the +// following function or method. +// Useful if the function or method is known to do something special or even 'dangerous', for +// example causing desired signals/crashes. +#if defined(__clang__) || defined(__GNUC__) +#define ATTRIBUTE_NO_UBSAN __attribute__((no_sanitize("undefined"))) +#endif + +#ifndef ATTRIBUTE_NO_UBSAN +#define ATTRIBUTE_NO_UBSAN +#endif + +#endif // SHARE_SANITIZERS_UB_HPP \ No newline at end of file diff --git a/src/hotspot/share/utilities/vmError.cpp b/src/hotspot/share/utilities/vmError.cpp index 656fe9403fe..0b3ccf9c747 100644 --- a/src/hotspot/share/utilities/vmError.cpp +++ b/src/hotspot/share/utilities/vmError.cpp @@ -60,6 +60,7 @@ #include "runtime/vmOperations.hpp" #include "runtime/vmThread.hpp" #include "runtime/vm_version.hpp" +#include "sanitizers/ub.hpp" #include "utilities/debug.hpp" #include "utilities/decoder.hpp" #include "utilities/defaultStream.hpp" @@ -2086,9 +2087,7 @@ typedef void (*voidfun_t)(); // compared to one generated with raise (asynchronous vs synchronous). See JDK-8065895. volatile int sigfpe_int = 0; -#if defined(__clang__) || defined(__GNUC__) -__attribute__((no_sanitize("undefined"))) -#endif +ATTRIBUTE_NO_UBSAN static void ALWAYSINLINE crash_with_sigfpe() { // generate a native synchronous SIGFPE where possible;