From 9e566d76d1d8acca27d8f69fffcbeb0b49b060ba Mon Sep 17 00:00:00 2001 From: Thomas Stuefe Date: Wed, 27 Mar 2024 08:36:40 +0000 Subject: [PATCH] 8327971: Multiple ASAN errors reported for metaspace 8327988: When running ASAN, disable dangerous NMT test Reviewed-by: jsjolen, rkennke --- src/hotspot/share/memory/metaspace/metachunk.cpp | 8 ++++++-- src/hotspot/share/memory/metaspace/virtualSpaceNode.cpp | 5 +++++ src/hotspot/share/nmt/mallocTracker.cpp | 6 ++++++ src/hotspot/share/runtime/os.cpp | 6 ++++++ src/hotspot/share/utilities/macros.hpp | 6 ++++++ test/hotspot/gtest/metaspace/test_virtualspacenode.cpp | 6 ++++++ .../gtest/nmt/test_nmt_buffer_overflow_detection.cpp | 5 +++++ test/hotspot/gtest/nmt/test_nmt_cornercases.cpp | 5 +++++ test/hotspot/gtest/nmt/test_nmt_locationprinting.cpp | 5 +++++ 9 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/hotspot/share/memory/metaspace/metachunk.cpp b/src/hotspot/share/memory/metaspace/metachunk.cpp index bc514c856e7..3d67aac8016 100644 --- a/src/hotspot/share/memory/metaspace/metachunk.cpp +++ b/src/hotspot/share/memory/metaspace/metachunk.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2017, 2021 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -34,6 +34,7 @@ #include "utilities/align.hpp" #include "utilities/copy.hpp" #include "utilities/debug.hpp" +#include "utilities/macros.hpp" #include "utilities/ostream.hpp" namespace metaspace { @@ -285,7 +286,9 @@ void Metachunk::verify() const { const size_t required_alignment = word_size() * sizeof(MetaWord); assert_is_aligned(base(), required_alignment); - // Test accessing the committed area. + // Test accessing the committed area. But not for ASAN. We don't know which portions + // of the chunk are still poisoned. +#if !INCLUDE_ASAN SOMETIMES( if (_committed_words > 0) { for (const MetaWord* p = _base; p < _base + _committed_words; p += os::vm_page_size()) { @@ -294,6 +297,7 @@ void Metachunk::verify() const { dummy = *(_base + _committed_words - 1); } ) +#endif // !INCLUDE_ASAN } #endif // ASSERT diff --git a/src/hotspot/share/memory/metaspace/virtualSpaceNode.cpp b/src/hotspot/share/memory/metaspace/virtualSpaceNode.cpp index 84f9c9ecc43..a8afbc130c9 100644 --- a/src/hotspot/share/memory/metaspace/virtualSpaceNode.cpp +++ b/src/hotspot/share/memory/metaspace/virtualSpaceNode.cpp @@ -48,6 +48,7 @@ #include "utilities/align.hpp" #include "utilities/debug.hpp" #include "utilities/globalDefinitions.hpp" +#include "utilities/macros.hpp" #include "utilities/ostream.hpp" namespace metaspace { @@ -433,6 +434,9 @@ void VirtualSpaceNode::verify_locked() const { _commit_mask.verify(); // Verify memory against commit mask. + // Down here, from ASAN's view, this memory may be poisoned, since we only unpoison + // way up at the ChunkManager level. +#if !INCLUDE_ASAN SOMETIMES( for (MetaWord* p = base(); p < base() + used_words(); p += os::vm_page_size()) { if (_commit_mask.is_committed_address(p)) { @@ -440,6 +444,7 @@ void VirtualSpaceNode::verify_locked() const { } } ) +#endif // !INCLUDE_ASAN assert(committed_words() <= word_size(), "Sanity"); assert_is_aligned(committed_words(), Settings::commit_granule_words()); diff --git a/src/hotspot/share/nmt/mallocTracker.cpp b/src/hotspot/share/nmt/mallocTracker.cpp index 4adeb3053b3..9b55202cbd5 100644 --- a/src/hotspot/share/nmt/mallocTracker.cpp +++ b/src/hotspot/share/nmt/mallocTracker.cpp @@ -40,6 +40,7 @@ #include "runtime/safefetch.hpp" #include "services/mallocLimit.hpp" #include "utilities/debug.hpp" +#include "utilities/macros.hpp" #include "utilities/ostream.hpp" #include "utilities/vmError.hpp" #include "utilities/globalDefinitions.hpp" @@ -226,6 +227,8 @@ void MallocTracker::deaccount(MallocHeader::FreeInfo free_info) { bool MallocTracker::print_pointer_information(const void* p, outputStream* st) { assert(MemTracker::enabled(), "NMT not enabled"); +#if !INCLUDE_ASAN + address addr = (address)p; // Carefully feel your way upwards and try to find a malloc header. Then check if @@ -303,5 +306,8 @@ bool MallocTracker::print_pointer_information(const void* p, outputStream* st) { } return true; } + +#endif // !INCLUDE_ASAN + return false; } diff --git a/src/hotspot/share/runtime/os.cpp b/src/hotspot/share/runtime/os.cpp index 9e6dbe33816..2d8d9a0d907 100644 --- a/src/hotspot/share/runtime/os.cpp +++ b/src/hotspot/share/runtime/os.cpp @@ -75,6 +75,7 @@ #include "utilities/defaultStream.hpp" #include "utilities/events.hpp" #include "utilities/fastrand.hpp" +#include "utilities/macros.hpp" #include "utilities/powerOfTwo.hpp" #ifndef _WINDOWS @@ -1199,6 +1200,8 @@ void os::print_location(outputStream* st, intptr_t x, bool verbose) { return; } +#if !INCLUDE_ASAN + bool accessible = is_readable_pointer(addr); // Check if addr is a JNI handle. @@ -1285,7 +1288,10 @@ void os::print_location(outputStream* st, intptr_t x, bool verbose) { return; } +#endif // !INCLUDE_ASAN + st->print_cr(INTPTR_FORMAT " is an unknown value", p2i(addr)); + } static bool is_pointer_bad(intptr_t* ptr) { diff --git a/src/hotspot/share/utilities/macros.hpp b/src/hotspot/share/utilities/macros.hpp index bfbaaa58acc..244b18ecdd4 100644 --- a/src/hotspot/share/utilities/macros.hpp +++ b/src/hotspot/share/utilities/macros.hpp @@ -635,4 +635,10 @@ #define NOT_CDS_JAVA_HEAP_RETURN_(code) { return code; } #endif +#ifdef ADDRESS_SANITIZER +#define INCLUDE_ASAN 1 +#else +#define INCLUDE_ASAN 0 +#endif + #endif // SHARE_UTILITIES_MACROS_HPP diff --git a/test/hotspot/gtest/metaspace/test_virtualspacenode.cpp b/test/hotspot/gtest/metaspace/test_virtualspacenode.cpp index b2a969c1371..52ebd7ba8b5 100644 --- a/test/hotspot/gtest/metaspace/test_virtualspacenode.cpp +++ b/test/hotspot/gtest/metaspace/test_virtualspacenode.cpp @@ -34,6 +34,7 @@ #include "memory/metaspace/virtualSpaceNode.hpp" #include "runtime/mutexLocker.hpp" #include "sanitizers/address.hpp" +#include "utilities/macros.hpp" #include "utilities/debug.hpp" //#define LOG_PLEASE #include "metaspaceGtestCommon.hpp" @@ -156,6 +157,11 @@ class VirtualSpaceNodeTest { // The chunk should be as far committed as was requested EXPECT_GE(c->committed_words(), request_commit_words); + // At the VirtualSpaceNode level, all memory is still poisoned. + // Since we bypass the normal way of allocating chunks (ChunkManager::get_chunk), we + // need to unpoison this chunk. + ASAN_UNPOISON_MEMORY_REGION(c->base(), c->committed_words() * BytesPerWord); + // Zap committed portion. DEBUG_ONLY(zap_range(c->base(), c->committed_words());) diff --git a/test/hotspot/gtest/nmt/test_nmt_buffer_overflow_detection.cpp b/test/hotspot/gtest/nmt/test_nmt_buffer_overflow_detection.cpp index 1d7d28d5079..f17a2b29497 100644 --- a/test/hotspot/gtest/nmt/test_nmt_buffer_overflow_detection.cpp +++ b/test/hotspot/gtest/nmt/test_nmt_buffer_overflow_detection.cpp @@ -26,11 +26,14 @@ #include "memory/allocation.hpp" #include "nmt/memTracker.hpp" #include "runtime/os.hpp" +#include "sanitizers/address.hpp" #include "utilities/debug.hpp" #include "utilities/ostream.hpp" #include "unittest.hpp" #include "testutils.hpp" +#if !INCLUDE_ASAN + // This prefix shows up on any c heap corruption NMT detects. If unsure which assert will // come, just use this one. #define COMMON_NMT_HEAP_CORRUPTION_MESSAGE_PREFIX "NMT corruption" @@ -161,3 +164,5 @@ TEST_VM(NMT, test_realloc) { } } } + +#endif // !INCLUDE_ASAN diff --git a/test/hotspot/gtest/nmt/test_nmt_cornercases.cpp b/test/hotspot/gtest/nmt/test_nmt_cornercases.cpp index f590b2509b2..f735022ea2b 100644 --- a/test/hotspot/gtest/nmt/test_nmt_cornercases.cpp +++ b/test/hotspot/gtest/nmt/test_nmt_cornercases.cpp @@ -28,6 +28,7 @@ #include "nmt/mallocTracker.hpp" #include "nmt/memTracker.hpp" #include "runtime/os.hpp" +#include "sanitizers/address.hpp" #include "testutils.hpp" #include "unittest.hpp" @@ -38,6 +39,9 @@ static void check_expected_malloc_header(const void* payload, MEMFLAGS type, siz EXPECT_EQ(hdr->flags(), type); } +// ASAN complains about allocating very large sizes +#if !INCLUDE_ASAN + // Check that a malloc with an overflowing size is rejected. TEST_VM(NMT, malloc_failure1) { void* p = os::malloc(SIZE_MAX, mtTest); @@ -85,6 +89,7 @@ TEST_VM(NMT, realloc_failure_overflowing_size) { TEST_VM(NMT, realloc_failure_gigantic_size) { check_failing_realloc(SIZE_MAX - M); } +#endif // !INCLUDE_ASAN static void* do_realloc(void* p, size_t old_size, size_t new_size, uint8_t old_content, bool check_nmt_header) { diff --git a/test/hotspot/gtest/nmt/test_nmt_locationprinting.cpp b/test/hotspot/gtest/nmt/test_nmt_locationprinting.cpp index aeb5144eeed..87ad2a798e0 100644 --- a/test/hotspot/gtest/nmt/test_nmt_locationprinting.cpp +++ b/test/hotspot/gtest/nmt/test_nmt_locationprinting.cpp @@ -27,12 +27,15 @@ #include "nmt/mallocHeader.inline.hpp" #include "nmt/memTracker.hpp" #include "runtime/os.hpp" +#include "sanitizers/address.hpp" #include "unittest.hpp" // Uncomment to get test output //#define LOG_PLEASE #include "testutils.hpp" +#if !INCLUDE_ASAN + using ::testing::HasSubstr; static void test_pointer(const void* p, bool expected_return_code, const char* expected_message) { @@ -123,3 +126,5 @@ static void test_for_mmap(size_t sz, ssize_t offset) { TEST_VM(NMT, location_printing_mmap_1) { test_for_mmap(os::vm_page_size(), 0); } TEST_VM(NMT, location_printing_mmap_2) { test_for_mmap(os::vm_page_size(), os::vm_page_size() - 1); } + +#endif // !INCLUDE_ASAN