Skip to content

VMStructs: Undefined behaviour fix - #141

Closed
r1viollet wants to merge 1 commit into
mainfrom
r1viollet/fix-ub-neg-shift
Closed

r1viollet wants to merge 1 commit into
mainfrom
r1viollet/fix-ub-neg-shift

Conversation

@r1viollet

Copy link
Copy Markdown
Contributor

What does this PR do?:

Avoid shifting a negative value
This was raised by asan nightly runs

Motivation:

> Task :ddprof-test:testClasses
ddprof-lib/src/main/cpp/vmStructs.cpp:816:34: runtime error: left shift of negative value -1
    #0 0x7f6f6bd20631 in ScopeDesc::readInt()
    #1 0x7f6f6bce54a6 in ScopeDesc::decode(int)
    #2 0x7f6f6bce7fb0 in StackWalker::walkVM(void*, ASGCT_CallFrame*, int, void const*, void const*)

Additional Notes:

How to test the change?:

For Datadog employees:

  • If this PR touches code that signs or publishes builds or packages, or handles
    credentials of any kind, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.
  • JIRA: [JIRA-XXXX]

Unsure? Have a question? Request a review!

Avoid shifting a negative value
This was raised by asan nightly runs

> Task :ddprof-test:testClasses
ddprof-lib/src/main/cpp/vmStructs.cpp:816:34: runtime error: left shift of negative value -1
    #0 0x7f6f6bd20631 in ScopeDesc::readInt()
    #1 0x7f6f6bce54a6 in ScopeDesc::decode(int)
    #2 0x7f6f6bce7fb0 in StackWalker::walkVM(void*, ASGCT_CallFrame*, int, void const*, void const*)
@r1viollet r1viollet changed the title Undefined behaviour fix VMStructs: Undefined behaviour fix Oct 23, 2024
@github-actions

Copy link
Copy Markdown
Contributor

🔧 Report generated by pr-comment-cppcheck

CppCheck Report

Warnings (6)

Style Violations (161)

@github-actions

Copy link
Copy Markdown
Contributor

🔧 Report generated by pr-comment-scanbuild

Scan-Build Report

User:runner@fv-az738-498
Working Directory:/home/runner/work/java-profiler/java-profiler/ddprof-lib/src/test/make
Command Line:make -j4 clean all
Clang Version:Ubuntu clang version 14.0.0-1ubuntu1.1
Date:Wed Oct 23 09:25:40 2024

Bug Summary

Bug TypeQuantityDisplay?
All Bugs5
Logic error
Assigned value is garbage or undefined1
Dereference of null pointer3
Unused code
Dead nested assignment1

Reports

Bug Group Bug Type ▾ File Function/Method Line Path Length
Logic errorAssigned value is garbage or undefineddwarf.cppparseInstructions24420
Unused codeDead nested assignmentvmStructs.cppcheckNativeBinding8841
Logic errorDereference of null pointersafeAccess.hload3318
Logic errorDereference of null pointersymbols_linux.hElfParser12932
Logic errorDereference of null pointerflightRecorder.cppflush15048

if (c >= 192) {
for (int shift = 6; ; shift += 6) {
c = *_stream++;
if (c < _unsigned5_base) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

is this not an indication of a race condition and we should retry the read ?
The previous byte indicated we were expecting something, so having 0 here is unexpected.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

reads of NMethods are generally racy - we're reading them from a process-wide codecache which can change even when the sampled thread is halted in a signal handler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The more valid fix would then be to bail out.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It seems that the problem is rather insidious - the _unsigned5_base is set up depending on the JVM version but that is subtly broken for Java 8 - see #136

I am going to fix that problem first and if that does not stop the asan errors we can revisit this later.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It seems that after #142 we are not getting to shifting a negative value.
I would keep this PR open for ~1 week to be sure the problem is fixed and then close it.

@r1viollet
r1viollet requested a review from jbachorik October 23, 2024 09:43
@jbachorik

Copy link
Copy Markdown
Collaborator

#142 fixed the underlying cause. Closing this PR.

@jbachorik jbachorik closed this Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants