diff --git a/src/hotspot/share/cds/aotMapLogger.cpp b/src/hotspot/share/cds/aotMapLogger.cpp index 9f338826fd6c0..ce1aa68dd23bd 100644 --- a/src/hotspot/share/cds/aotMapLogger.cpp +++ b/src/hotspot/share/cds/aotMapLogger.cpp @@ -27,6 +27,7 @@ #include "cds/aotMappedHeapWriter.hpp" #include "cds/aotStreamedHeapLoader.hpp" #include "cds/aotStreamedHeapWriter.hpp" +#include "cds/archiveUtils.hpp" #include "cds/cdsConfig.hpp" #include "cds/filemap.hpp" #include "classfile/moduleEntry.hpp" @@ -879,7 +880,7 @@ void AOTMapLogger::runtime_log_heap_region(FileMapInfo* mapinfo) { } address requested_base = UseCompressedOops ? (address)mapinfo->narrow_oop_base() : AOTMappedHeapLoader::heap_region_requested_address(mapinfo); - address requested_start = requested_base + r->mapping_offset(); + address requested_start = ArchiveUtils::offset_to_requested_addr(requested_base, r->mapping_offset()); log_region_range("heap", buffer_start, buffer_end, requested_start); log_archived_objects(AOTMappedHeapLoader::oop_iterator(mapinfo, buffer_start, buffer_end)); } diff --git a/src/hotspot/share/cds/archiveBuilder.cpp b/src/hotspot/share/cds/archiveBuilder.cpp index bd0d070b21202..0b046a527cd51 100644 --- a/src/hotspot/share/cds/archiveBuilder.cpp +++ b/src/hotspot/share/cds/archiveBuilder.cpp @@ -367,7 +367,7 @@ address ArchiveBuilder::reserve_buffer() { size_t static_archive_size = _mapped_static_archive_top - _mapped_static_archive_bottom; // At run time, we will mmap the dynamic archive at my_archive_requested_bottom - _requested_static_archive_top = _requested_static_archive_bottom + static_archive_size; + _requested_static_archive_top = ArchiveUtils::offset_to_requested_addr(_requested_static_archive_bottom, static_archive_size); my_archive_requested_bottom = align_up(_requested_static_archive_top, AOTMetaspace::core_region_alignment()); _requested_dynamic_archive_bottom = my_archive_requested_bottom; @@ -375,7 +375,7 @@ address ArchiveBuilder::reserve_buffer() { _buffer_to_requested_delta = my_archive_requested_bottom - _buffer_bottom; - address my_archive_requested_top = my_archive_requested_bottom + buffer_size; + address my_archive_requested_top = ArchiveUtils::offset_to_requested_addr(my_archive_requested_bottom, buffer_size); if (my_archive_requested_bottom < _requested_static_archive_bottom || my_archive_requested_top <= _requested_static_archive_bottom) { // Size overflow. @@ -982,7 +982,7 @@ size_t ArchiveBuilder::any_to_offset(address p) const { } address ArchiveBuilder::offset_to_buffered_address(size_t offset) const { - address requested_addr = _requested_static_archive_bottom + offset; + address requested_addr = ArchiveUtils::offset_to_requested_addr(_requested_static_archive_bottom, offset); address buffered_addr = requested_addr - _buffer_to_requested_delta; assert(is_in_buffer_space(buffered_addr), "bad offset"); return buffered_addr; @@ -1048,7 +1048,7 @@ class RelocateBufferToRequested : public BitMapClosure { address bottom = _builder->buffer_bottom(); address top = _builder->buffer_top(); - address new_bottom = bottom + _buffer_to_requested_delta; + address new_bottom = ArchiveUtils::offset_to_requested_addr(bottom, _buffer_to_requested_delta); address new_top = top + _buffer_to_requested_delta; aot_log_debug(aot)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT "] to " "[" INTPTR_FORMAT " - " INTPTR_FORMAT "]", @@ -1109,7 +1109,7 @@ void ArchiveBuilder::relocate_to_requested() { size_t my_archive_size = buffer_top() - buffer_bottom(); if (CDSConfig::is_dumping_static_archive()) { - _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size; + _requested_static_archive_top = ArchiveUtils::offset_to_requested_addr(_requested_static_archive_bottom, my_archive_size); RelocateBufferToRequested patcher(this); patcher.doit(); } else { diff --git a/src/hotspot/share/cds/archiveUtils.hpp b/src/hotspot/share/cds/archiveUtils.hpp index 42455adedd0d3..5bb98c0db24ab 100644 --- a/src/hotspot/share/cds/archiveUtils.hpp +++ b/src/hotspot/share/cds/archiveUtils.hpp @@ -290,6 +290,12 @@ class ArchiveUtils { static Array* archive_array(GrowableArray* tmp_array) { return archive_ptr_array(tmp_array); } + + template + static address offset_to_requested_addr(T requested_base, size_t offset) { + // As zero is allowed for requested_base, use integer arithmetic to avoid UB pointer arithmetic. + return (address)((uintptr_t)requested_base + offset); + } }; class HeapRootSegments {