Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8332818: ubsan: archiveHeapLoader.cpp:70:27: runtime error: applying non-zero offset 18446744073707454464 to null pointer #19597

Closed
wants to merge 2 commits into from

Conversation

MBaesken
Copy link
Member

@MBaesken MBaesken commented Jun 7, 2024

When running :tier1 hs tests, the following issue has been reported when running with ubsan enabled binaries (configure flag --enable-ubsan)

stderr: [/jdk/src/hotspot/share/cds/archiveHeapLoader.cpp:70:27: runtime error: applying non-zero offset 18446744073707454464 to null pointer
#0 0x7f33db52823f in ArchiveHeapLoader::init_mapped_heap_info(unsigned char*, long, int) /jdk/src/hotspot/share/cds/archiveHeapLoader.cpp:70
#1 0x7f33dc6d3ad4 in FileMapInfo::map_heap_region_impl() /jdk/src/hotspot/share/cds/filemap.cpp:2211
#2 0x7f33dc6d4ba4 in FileMapInfo::map_heap_region() /jdk/src/hotspot/share/cds/filemap.cpp:2129
#3 0x7f33dc6d52a7 in FileMapInfo::map_or_load_heap_region() /jdk/src/hotspot/share/cds/filemap.cpp:2019
#4 0x7f33ddaf7fdb in MetaspaceShared::map_archives(FileMapInfo*, FileMapInfo*, bool) /jdk/src/hotspot/share/cds/metaspaceShared.cpp:1183
#5 0x7f33ddaf8f54 in MetaspaceShared::initialize_runtime_shared_and_meta_spaces() /jdk/src/hotspot/share/cds/metaspaceShared.cpp:943
#6 0x7f33ddadd60f in Metaspace::global_initialize() /jdk/src/hotspot/share/memory/metaspace.cpp:714
#7 0x7f33de9bc7f9 in universe_init() /jdk/src/hotspot/share/memory/universe.cpp:866
#8 0x7f33dcc93691 in init_globals() /jdk/src/hotspot/share/runtime/init.cpp:128
#9 0x7f33de92a720 in Threads::create_vm(JavaVMInitArgs*, bool*) /jdk/src/hotspot/share/runtime/threads.cpp:553
#10 0x7f33dd02d477 in JNI_CreateJavaVM_inner /jdk/src/hotspot/share/prims/jni.cpp:3581
#11 0x7f33dd02d477 in JNI_CreateJavaVM /jdk/src/hotspot/share/prims/jni.cpp:3672
#12 0x7f33e42a90e5 in InitializeJVM /jdk/src/java.base/share/native/libjli/java.c:1550
#13 0x7f33e42a90e5 in JavaMain /jdk/src/java.base/share/native/libjli/java.c:491
#14 0x7f33e42b2748 in ThreadJavaMain /jdk/src/java.base/unix/native/libjli/java_md.c:642
#15 0x7f33e42616e9 in start_thread (/lib64/libpthread.so.0+0xa6e9) (BuildId: 2f8d3c2d0f4d7888c2598d2ff6356537f5708a73)
#16 0x7f33e391550e in clone (/lib64/libc.so.6+0x11850e) (BuildId: f732026552f6adff988b338e92d466bc81a01c37)

Seems that CompressedOops::base() can be nullptr , so adding to it some non-zero value triggers ubsan because of undefined behavior. In the JBS-bug there was already a little discussion and it seems disabling this warning might be an option.

otherwise some workaround like

template<typename T>
T* add_to_ptr_maybe_null(T* ptr, uintptr_t val) {
  return (T*)((uintptr_t)ptr + val * sizeof(T));
}

could be used .


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8332818: ubsan: archiveHeapLoader.cpp:70:27: runtime error: applying non-zero offset 18446744073707454464 to null pointer (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/19597/head:pull/19597
$ git checkout pull/19597

Update a local copy of the PR:
$ git checkout pull/19597
$ git pull https://git.openjdk.org/jdk.git pull/19597/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 19597

View PR using the GUI difftool:
$ git pr show -t 19597

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/19597.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 7, 2024

👋 Welcome back mbaesken! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jun 7, 2024

@MBaesken This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8332818: ubsan: archiveHeapLoader.cpp:70:27: runtime error: applying non-zero offset 18446744073707454464 to null pointer

Reviewed-by: stuefe, lucy

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 81 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot changed the title JDK-8332818: ubsan: archiveHeapLoader.cpp:70:27: runtime error: applying non-zero offset 18446744073707454464 to null pointer 8332818: ubsan: archiveHeapLoader.cpp:70:27: runtime error: applying non-zero offset 18446744073707454464 to null pointer Jun 7, 2024
@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 7, 2024
@openjdk
Copy link

openjdk bot commented Jun 7, 2024

@MBaesken The following label will be automatically applied to this pull request:

  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot-runtime hotspot-runtime-dev@openjdk.org label Jun 7, 2024
@mlbridge
Copy link

mlbridge bot commented Jun 7, 2024

Webrevs

@@ -61,6 +61,10 @@ 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().
// avoid adding to CompressedOops::base() == nullptr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what the comment is trying to say.

This is surely a bug in ubsan as I should be able to take any address, including zero, and add an offset to get another address!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi David, it is no ubsan-bug, see also here for more details about this warning https://reviews.llvm.org/D67122 .

@tstuefe
Copy link
Member

tstuefe commented Jun 12, 2024

@dholmes-ora Adding to NULL is undefined, Matthias is right there. E.g. I once worked on machines that tracked the validity of pointers in hardware. Adding anything to NULL would give you a hardware interrupt.

However, that is not the case for any of our platforms. Which is why Matthias mutes UBSAN here.

Copy link
Member

@tstuefe tstuefe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

But could you add a define for this, please? Would be better to read. We already have ATTRIBUTE_NO_ASAN (see share/sanitizers). I think a corresponding ATTRIBUTE_NO_UBSAN would be good.

Such a define would also be the central place to switch on Visual Studio if they ever add ubsan support.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 12, 2024
@MBaesken
Copy link
Member Author

Looks good.

But could you add a define for this, please? Would be better to read. We already have ATTRIBUTE_NO_ASAN (see share/sanitizers). I think a corresponding ATTRIBUTE_NO_UBSAN would be good.

Such a define would also be the central place to switch on Visual Studio if they ever add ubsan support.

Thanks for the review !
Introducing ATTRIBUTE_NO_UBSAN sounds like a good idea, we could reuse that in vmerror.cpp too (function crash_with_sigfpe).
Where should I place the macro , maybe a separate header sanitizers/ub.hpp next to sanitizers/address.hpp ?
On the other hand, those defines would maybe be needed also outside HS in the JDK libs.

@MBaesken
Copy link
Member Author

Thomas, could we do the ATTRIBUTE_NO_UBSAN in a follow up ?

@tstuefe
Copy link
Member

tstuefe commented Jun 12, 2024

Looks good.
But could you add a define for this, please? Would be better to read. We already have ATTRIBUTE_NO_ASAN (see share/sanitizers). I think a corresponding ATTRIBUTE_NO_UBSAN would be good.
Such a define would also be the central place to switch on Visual Studio if they ever add ubsan support.

Thanks for the review ! Introducing ATTRIBUTE_NO_UBSAN sounds like a good idea, we could reuse that in vmerror.cpp too (function crash_with_sigfpe). Where should I place the macro , maybe a separate header sanitizers/ub.hpp next to sanitizers/address.hpp ? On the other hand, those defines would maybe be needed also outside HS in the JDK libs.

ubsan.hpp?

@tstuefe
Copy link
Member

tstuefe commented Jun 12, 2024

Thomas, could we do the ATTRIBUTE_NO_UBSAN in a follow up ?

Sure.

Copy link
Contributor

@RealLucy RealLucy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@dholmes-ora
Copy link
Member

Adding to NULL is undefined,

Hmm okay. So really our address abstraction should be dealing with intptr_t or uintptr_t to perform any arithmetic and then cast the result back to an actual pointer type when done?

@tstuefe
Copy link
Member

tstuefe commented Jun 13, 2024

Adding to NULL is undefined,

Hmm okay. So really our address abstraction should be dealing with intptr_t or uintptr_t to perform any arithmetic and then cast the result back to an actual pointer type when done?

Well, not sure.

C99:

5 An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation. 56)

The one platform I talked about (IBM AS/400 in native mode) did the latter. Casting integer to pointer resulted in an invalid pointer that would trap on dereferencing.

Personally, I think hunting UB is sorting out the chaff to find the useful bits. Pity that we cannot selectivly switch off UB cases with the compiler, and have to decorate code instead.

@MBaesken
Copy link
Member Author

Pity that we cannot selectivly switch off UB cases with the compiler, and have to decorate code instead.

We can switch off whole ubsan problem/issue 'classes' (and do this already for some types of shifts). But this is of course not fine granular if done for the whole codebase.

Btw any objections integrating this now (and doing in the follow up the ATTRIBUTE_NO_UBSAN macro, see https://bugs.openjdk.org/browse/JDK-8334239) ?

@tstuefe
Copy link
Member

tstuefe commented Jun 13, 2024

Pity that we cannot selectivly switch off UB cases with the compiler, and have to decorate code instead.

We can switch off whole ubsan problem/issue 'classes' (and do this already for some types of shifts). But this is of course not fine granular if done for the whole codebase.

Btw any objections integrating this now (and doing in the follow up the ATTRIBUTE_NO_UBSAN macro, see https://bugs.openjdk.org/browse/JDK-8334239) ?

no problem, but i would remove the comment about avoiding adding to null.

@MBaesken
Copy link
Member Author

I removed the comment.
Thanks for the reviews !

/integrate

@openjdk
Copy link

openjdk bot commented Jun 14, 2024

Going to push as commit 6861766.
Since your change was applied there have been 81 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 14, 2024
@openjdk openjdk bot closed this Jun 14, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 14, 2024
@openjdk
Copy link

openjdk bot commented Jun 14, 2024

@MBaesken Pushed as commit 6861766.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants