Skip to content

Add alignment checks to NativeFunc mark methods#348

Merged
jbachorik merged 7 commits intomainfrom
jb/marking_fix
Jan 30, 2026
Merged

Add alignment checks to NativeFunc mark methods#348
jbachorik merged 7 commits intomainfrom
jb/marking_fix

Conversation

@jbachorik
Copy link
Collaborator

@jbachorik jbachorik commented Jan 29, 2026

What does this PR do?:

Fixes NativeFunc mark methods to prevent ASAN segfaults and adds CI workflow improvements for label-based test configuration selection.

Core fix - NativeFunc marking:

  • Adds alignment validation to all NativeFunc mark-related methods (isMarked, mark getter/setter)
  • Prevents segfaults when accessing marks on non-NativeFunc allocated strings
  • Uses the same is_aligned() check pattern as libIndex()
  • Invalid pointers now return safe defaults (false, 0, or no-op) instead of crashing
  • Fixes inverted logic in is_aligned() helper
  • Adds comprehensive tests for marking behavior

CI workflow improvements:

  • Adds label-based test configuration: test:release, test:asan, test:tsan
  • PRs always run debug config; labels add additional configurations
  • 30-second debounce on label events allows adding multiple labels before run starts
  • Fixes pre-existing bugs in check-for-pr job (broken jq expression)
  • Removes dead gh-release job with invalid workflow syntax

Motivation:

ASAN tests revealed a segfault in NativeFunc::mark() at codeCache.h:77. The profiler was calling mark() on function names that weren't allocated via NativeFunc::create(), causing invalid memory access.

The CI changes enable easily running ASAN tests on PRs by adding the test:asan label, which was needed to properly validate this fix.

How to test the change?:

  1. Run ASAN tests: ./gradlew :ddprof-test:testAsan
  2. Or add test:asan label to the PR to trigger ASAN CI run

The fix prevents crashes when the profiler encounters native frames from libraries without proper NativeFunc metadata.

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.

Unsure? Have a question? Request a review!

🤖 Generated with Claude Code

@jbachorik jbachorik added the AI label Jan 29, 2026
@jbachorik jbachorik marked this pull request as ready for review January 29, 2026 17:40
@jbachorik jbachorik requested a review from a team as a code owner January 29, 2026 17:40
@jbachorik jbachorik requested a review from zhengyu123 January 29, 2026 17:40
@dd-octo-sts
Copy link

dd-octo-sts bot commented Jan 29, 2026

Scan-Build Report

User:runner@runnervmkj6or
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 18.1.3 (1ubuntu1)
Date:Thu Jan 29 17:40:39 2026

Bug Summary

Bug TypeQuantityDisplay?
All Bugs9
Unused code
Dead assignment2
Dead initialization6
Dead nested assignment1

Reports

Bug Group Bug Type ▾ File Function/Method Line Path Length
Unused codeDead assignmentlibraryPatcher_linux.cpppatch_library_unlocked931
Unused codeDead assignmentstackWalker.cppwalkVM5751
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_JavaProfiler_getStatus01191
Unused codeDead initializationprofiler.cpprunInternal16531
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6581
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6911
Unused codeDead initializationflightRecorder.cppresolveMethod3511
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_OTelContext_setProcessCtx04751
Unused codeDead nested assignmentvmStructs.cppcheckNativeBinding7821

@dd-octo-sts
Copy link

dd-octo-sts bot commented Jan 30, 2026

Scan-Build Report

User:runner@runnervmkj6or
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 18.1.3 (1ubuntu1)
Date:Fri Jan 30 09:24:59 2026

Bug Summary

Bug TypeQuantityDisplay?
All Bugs9
Unused code
Dead assignment2
Dead initialization6
Dead nested assignment1

Reports

Bug Group Bug Type ▾ File Function/Method Line Path Length
Unused codeDead assignmentlibraryPatcher_linux.cpppatch_library_unlocked931
Unused codeDead assignmentstackWalker.cppwalkVM5751
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_JavaProfiler_getStatus01191
Unused codeDead initializationprofiler.cpprunInternal16531
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6581
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6911
Unused codeDead initializationflightRecorder.cppresolveMethod3511
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_OTelContext_setProcessCtx04751
Unused codeDead nested assignmentvmStructs.cppcheckNativeBinding7821

@dd-octo-sts
Copy link

dd-octo-sts bot commented Jan 30, 2026

Scan-Build Report

User:runner@runnervmkj6or
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 18.1.3 (1ubuntu1)
Date:Fri Jan 30 09:29:10 2026

Bug Summary

Bug TypeQuantityDisplay?
All Bugs9
Unused code
Dead assignment2
Dead initialization6
Dead nested assignment1

Reports

Bug Group Bug Type ▾ File Function/Method Line Path Length
Unused codeDead assignmentlibraryPatcher_linux.cpppatch_library_unlocked931
Unused codeDead assignmentstackWalker.cppwalkVM5751
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_JavaProfiler_getStatus01191
Unused codeDead initializationprofiler.cpprunInternal16531
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6581
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6911
Unused codeDead initializationflightRecorder.cppresolveMethod3511
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_OTelContext_setProcessCtx04751
Unused codeDead nested assignmentvmStructs.cppcheckNativeBinding7821

@jbachorik jbachorik added the test:asan Run CI tests with AddressSanitizer configuration label Jan 30, 2026
@dd-octo-sts
Copy link

dd-octo-sts bot commented Jan 30, 2026

Scan-Build Report

User:runner@runnervmkj6or
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 18.1.3 (1ubuntu1)
Date:Fri Jan 30 10:09:51 2026

Bug Summary

Bug TypeQuantityDisplay?
All Bugs9
Unused code
Dead assignment2
Dead initialization6
Dead nested assignment1

Reports

Bug Group Bug Type ▾ File Function/Method Line Path Length
Unused codeDead assignmentlibraryPatcher_linux.cpppatch_library_unlocked931
Unused codeDead assignmentstackWalker.cppwalkVM5751
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_JavaProfiler_getStatus01191
Unused codeDead initializationprofiler.cpprunInternal16531
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6581
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6911
Unused codeDead initializationflightRecorder.cppresolveMethod3511
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_OTelContext_setProcessCtx04751
Unused codeDead nested assignmentvmStructs.cppcheckNativeBinding7821

jbachorik and others added 5 commits January 30, 2026 11:30
Prevents ASAN segfault when accessing mark on non-NativeFunc strings.
All mark methods now validate pointer alignment like libIndex() does.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- isMarked() → is_marked()
- mark() getter → read_mark()
- mark() setter → set_mark()

Improves API clarity by distinguishing read/write operations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Critical fix: is_aligned() had inverted logic using ~(alignment-1)
instead of (alignment-1). This meant alignment checks always failed
for non-NULL pointers, completely breaking the safety mechanism.

Also fixed indentation in read_mark() and set_mark() to match other
methods (4-space indent).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Adds two new test suites to prevent marking from being accidentally disabled:

1. utils_ut.cpp - Tests is_aligned() directly
   - Verifies aligned pointers return true
   - Verifies unaligned pointers return false
   - Critical test catches inverted logic bug

2. nativefunc_ut.cpp - Tests NativeFunc end-to-end
   - Verifies marks can be set and read back
   - Tests all mark types (THREAD_ENTRY, COMPILER_ENTRY, etc.)
   - Critical test: MarkingDisabledDetection fails if is_aligned() broken

These tests would have caught the recent is_aligned() bug where marking
was silently disabled for all non-NULL pointers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Feature: PR labels test:release, test:asan, test:tsan trigger
additional test configurations. Debug always runs.

Fixes:
- check-for-pr: broken jq expression and variable handling
- Removed dead gh-release job with invalid workflow syntax

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@dd-octo-sts
Copy link

dd-octo-sts bot commented Jan 30, 2026

Scan-Build Report

User:runner@runnervmkj6or
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 18.1.3 (1ubuntu1)
Date:Fri Jan 30 10:34:36 2026

Bug Summary

Bug TypeQuantityDisplay?
All Bugs9
Unused code
Dead assignment2
Dead initialization6
Dead nested assignment1

Reports

Bug Group Bug Type ▾ File Function/Method Line Path Length
Unused codeDead assignmentlibraryPatcher_linux.cpppatch_library_unlocked931
Unused codeDead assignmentstackWalker.cppwalkVM5751
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_JavaProfiler_getStatus01191
Unused codeDead initializationprofiler.cpprunInternal16531
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6581
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6911
Unused codeDead initializationflightRecorder.cppresolveMethod3511
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_OTelContext_setProcessCtx04751
Unused codeDead nested assignmentvmStructs.cppcheckNativeBinding7821

Uses hidden HTML marker (<!-- comment-id -->) to find existing
comments instead of GITHUB_ACTOR which doesn't work with app tokens.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@jbachorik jbachorik dismissed zhengyu123’s stale review January 30, 2026 10:40

Applied the recommended change

@dd-octo-sts
Copy link

dd-octo-sts bot commented Jan 30, 2026

Scan-Build Report

User:runner@runnervmkj6or
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 18.1.3 (1ubuntu1)
Date:Fri Jan 30 11:23:32 2026

Bug Summary

Bug TypeQuantityDisplay?
All Bugs9
Unused code
Dead assignment2
Dead initialization6
Dead nested assignment1

Reports

Bug Group Bug Type ▾ File Function/Method Line Path Length
Unused codeDead assignmentlibraryPatcher_linux.cpppatch_library_unlocked931
Unused codeDead assignmentstackWalker.cppwalkVM5751
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_JavaProfiler_getStatus01191
Unused codeDead initializationprofiler.cpprunInternal16531
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6581
Unused codeDead initializationflightRecorder.cppcleanupUnreferencedMethods6911
Unused codeDead initializationflightRecorder.cppresolveMethod3511
Unused codeDead initializationjavaApi.cppJava_com_datadoghq_profiler_OTelContext_setProcessCtx04751
Unused codeDead nested assignmentvmStructs.cppcheckNativeBinding7821

@jbachorik
Copy link
Collaborator Author

@rkennke @zhengyu123 I did the renames to make the code clearer. Also, added some tests and fixed the alignment check bug.

The CI changes are not strictly related but they are fixing some annoyances I found when working on this. Since I really want to get this merged ASAP and create a release so we can consume it in dd-trace-java, I opted for including all those changes in this PR as separate commits.

Copy link
Contributor

@rkennke rkennke 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 what about Zhengyu's suggestion?

Copy link
Contributor

@rkennke rkennke left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good!

@jbachorik jbachorik merged commit ec1b4ce into main Jan 30, 2026
133 of 136 checks passed
@jbachorik jbachorik deleted the jb/marking_fix branch January 30, 2026 11:40
@github-actions github-actions bot added this to the 1.37.0 milestone Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI test:asan Run CI tests with AddressSanitizer configuration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants