Skip to content

fix(profiler): write every JFR string as standard UTF-8 (PROF-16874) - #1058

Open
kaahos wants to merge 18 commits into
mainfrom
paul.fournillon/prof-16874-thread-name-utf8
Open

kaahos wants to merge 18 commits into
mainfrom
paul.fournillon/prof-16874-thread-name-utf8

Conversation

@kaahos

@kaahos kaahos commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?:
Writes every string in a java-profiler recording as standard UTF-8.

  • Before: strings from the JVM reached JFR as modified UTF-8. Modified UTF-8 writes U+0000 as C0 80 and a supplementary character as a 6-byte surrogate pair. The JDK's JFR parser decodes encoding-3 strings with new String(bytes, UTF_8), so these read back as replacement characters. Affected: thread names (JVMTI GetThreadInfo), class and method names (GetClassSignature, GetMethodName), endpoints, labels and context values (JNI GetStringUTFChars), and system properties. Native thread names from /proc are cut at 15 bytes and could end in half a character.
  • After: Buffer::putUtf8 converts every string with toStandardUtf8 (standardUtf8.h):
    • C0 80 becomes 00, and a surrogate pair becomes its 4-byte form;
    • valid standard UTF-8 is copied, so the conversion is idempotent;
    • a lone surrogate becomes U+FFFD, and a malformed byte becomes ?;
    • a character cut off by the end of the input is dropped;
    • the MAX_STRING_LENGTH cap never splits a character.

Motivation:
PROF-16874. Found while fixing the same bug for virtual-thread names in #1049.

Additional Notes:

  • Why at write time: putUtf8 is the only writer of JFR strings, so one change covers every source. The dictionaries keep the bytes they store, so the per-request JNI paths (endpoints, context values) do no extra work.
  • Output is never longer than the input: U+FFFD replaces 3 bytes, and ? replaces 1. Callers size their buffer flushes from the input length, so they stay correct.
  • Cost: one extra scan per string written. An all-ASCII string takes the old copy path after the scan.
  • Accepted behavior change: a byte that is not valid UTF-8, for example in a Latin-1 library path or native symbol, is now written as ?. Before, the raw byte reached the file, and readers showed a replacement character for it.
  • Out of scope: updateThreadName does not delete the thread_group and context_class_loader local refs from GetThreadInfo. They are freed when the JNI call or JVMTI callback returns. It predates this PR.
  • Build: JavaConventionsPlugin now sets options.encoding = "UTF-8". The new tests use a non-ASCII method name, and javac on JDK 17 under a C locale would otherwise read sources as ASCII. JDK 18 and later already default to UTF-8.
  • Human review: recommended. It changes the bytes of every string in every recording.
  • Review: Sphinx on the full PR at 50d0266, then /code-review medium on each later commit. No Critical or High findings remain.

How to test the change?:

  • Java tests write a recording and read it back with the Jafar parser. Each one fails when putUtf8 writes the raw bytes, as main does:
Test Source Read back on main
ThreadNameEncodingTest JVMTI thread name worker-😀-é中-a\u0000b worker-��-é中-a��b
EndpointEncodingTest JNI endpoint GET /items/😀-é中-a\u0000b and operation GET /items/��-é中-a��b
MethodNameEncodingTest JVMTI method name burn𝒜é burn��é
NativeThreadNameEncodingTest (Linux) /proc name native-é中 plus the first 2 bytes of 中 the test fails (the cut bytes reach the file)

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 security review (run the dd:platform-security-review
    skill, or file a request via the PSEC review form).
    bewaire also runs automatically on every PR.
  • This PR doesn't touch any of that.
  • JIRA: PROF-16874

@kaahos kaahos added the human_review:recommended Human review recommended — see PR description label Nov 25, 2026
@kaahos

kaahos commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-prod-us1-5 datadog-prod-us1-5 Bot left a comment

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.

Bits Code Review: PASS

More details

The reviewed encoding and serialization paths handle supplementary characters and embedded NULs through UTF-16 conversion and explicit byte lengths. The static specialist returned no actionable findings; runtime behavior was not exercised.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 63bea21

@dd-octo-sts

dd-octo-sts Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Memory overhead check: ✅ within budget

Profiler 06f92832c, pipeline. Budget: utils/memcheck/budget.txt. NMT and /proc values are profiled minus no-profiler runs on the same runner.

scenario metric value Δ since budget set limit headroom
allocs native_mem_live_bytes 21.72 MiB +29 KiB 21.95 MiB 237 KiB ok
allocs native_mem_max_bytes 22.98 MiB +28 KiB 23.20 MiB 228 KiB ok
allocs nmt.class 300 KiB +3 KiB 563 KiB 263 KiB ok
allocs nmt.internal 371 KiB +2 KiB 666 KiB 295 KiB ok
classesM native_mem_live_bytes (coverage-corrected) 23.29 MiB +10 KiB 23.55 MiB 267 KiB ok
classesM native_mem_max_bytes (coverage-corrected) 25.20 MiB +13 KiB 25.45 MiB 253 KiB ok
classesM nmt.class 2.94 MiB −2 KiB 3.20 MiB 268 KiB ok
classesM nmt.internal 3.21 MiB +6 KiB 3.50 MiB 302 KiB ok
mixed native_mem_live_bytes 23.14 MiB −11 KiB 23.45 MiB 318 KiB ok
mixed native_mem_max_bytes 40.74 MiB +25 KiB 41.00 MiB 262 KiB ok
mixed nmt.class 302 KiB −5 KiB 563 KiB 261 KiB ok
mixed nmt.internal 408 KiB −2 KiB 717 KiB 309 KiB ok
threads native_mem_live_bytes 19.08 MiB −13 KiB 19.35 MiB 279 KiB ok
threads native_mem_max_bytes 20.09 MiB −19 KiB 20.40 MiB 316 KiB ok
threads nmt.class 114 KiB +1 KiB 410 KiB 296 KiB ok
threads nmt.internal 157 KiB −7 KiB 461 KiB 304 KiB ok
traces native_mem_live_bytes 20.25 MiB +16 KiB 20.50 MiB 260 KiB ok
traces native_mem_max_bytes 21.45 MiB +40 KiB 21.70 MiB 257 KiB ok
traces nmt.class 470 KiB −1 KiB 768 KiB 298 KiB ok
traces nmt.internal 607 KiB −7 KiB 870 KiB 263 KiB ok

✅ nmt.tracing absent in every scenario.

Not budgeted (too noisy on shared runners)
scenario nmt.code nmt.gc nmt.total proc.VmRSS proc.RssAnon
allocs 1.60 MiB 5 KiB 2.72 MiB 30.69 MiB 26.25 MiB
classesM 5.49 MiB −3 KiB 12.31 MiB 35.64 MiB 31.12 MiB
mixed 1.70 MiB 3 KiB 2.84 MiB 32.78 MiB 27.97 MiB
threads 6 KiB 1 KiB 973 KiB 24.67 MiB 19.64 MiB
traces 2.39 MiB −45 KiB 3.90 MiB 23.76 MiB 19.14 MiB

@dd-octo-sts

dd-octo-sts Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

CI Test Results

Run: #46001539087 | Commit: cbfef5c | Duration: 23m 45s (longest job)

❌ 1 of 32 test jobs failed

Status Overview

JDK glibc-aarch64/debug glibc-amd64/debug musl-aarch64/debug musl-amd64/debug
8 - ✅ - -
8-ibm - ❌ - -
8-j9 ✅ ✅ - -
8-librca - - ✅ ✅
8-orcl - ✅ - -
11 - ✅ - -
11-j9 ✅ ✅ - -
11-librca - - ✅ ✅
17 ✅ ✅ - -
17-graal ✅ ✅ - -
17-j9 ✅ ✅ - -
17-librca - - ✅ ✅
21 ✅ ✅ - -
21-graal ✅ ✅ - -
21-librca - - ✅ ✅
25 ✅ ✅ - -
25-graal ✅ ✅ - -
25-librca - - ✅ ✅

Legend: ✅ passed | ❌ failed | ⚪ skipped | 🚫 cancelled

Failed Jobs

Summary: Total: 32 | Passed: 31 | Failed: 1


Updated: 2026-11-27 13:15:29 UTC

@dd-octo-sts

dd-octo-sts Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

❌ 29 passed, 1 failed out of 30 expected configurations

Test Matrix

Platform JDK 8 JDK 11 JDK 17 JDK 21 JDK 25
glibc-x64-hotspot ❌ ✅ ✅ ✅ ✅
glibc-x64-openj9 ✅ ✅ ✅ ✅ ✅
glibc-arm64-hotspot ✅ ✅ ✅ ✅ ✅
glibc-arm64-openj9 ✅ ✅ ✅ ✅ ✅
musl-x64-hotspot ✅ ✅ ✅ ✅ ✅
musl-x64-openj9 N/A N/A N/A N/A N/A
musl-arm64-hotspot ✅ ✅ ✅ ✅ ✅
musl-arm64-openj9 N/A N/A N/A N/A N/A

Failure Details

glibc-x64-hotspot-jdk8

Tracer+profiler:

"  This may indicate tracer is not capturing any requests"
""
"[7/8] Checking for unexpected events..."
"  ? No unexpected events found"
""
"[8/8] Scenario-specific validation (ddprof_with_tracer)..."
"  Validating tracer+profiler scenario..."
"  ? Tracer+profiler scenario checks passed"
""
"=== Validation Summary ==="
"ExecutionSample:            3 events (datadog.ExecutionSample)"
"Stack traces:               3 samples"
"Thread diversity:           2 threads"
"Allocation samples:         0.0 events (jdk.ObjectAllocationSample)"
"ThreadAllocationStatistics: 86 events"
""
"VALIDATION_FAILED: One or more checks did not pass"

Script executed successfully: 425 commands
VALIDATION_FAILED: Validation checks did not pass

Links

10 not applicable (not scheduled): musl-x64-openj9, musl-arm64-openj9. OpenJ9 requires glibc; upstream publishes no musl runtime.

@kaahos
kaahos marked this pull request as ready for review November 26, 2026 14:48
@kaahos
kaahos requested a review from a team as a code owner November 26, 2026 14:48
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T14:49:41.923259Z f590bd4 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-prod-us1-5 datadog-prod-us1-5 Bot left a comment

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.

Bits Code Review: PASS

More details

Explicit byte lengths preserve converted NUL bytes through JFR thread serialization, and truncation avoids splitting surrogate pairs. The completed static review identified no actionable defects.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit f590bd4

@kaahos kaahos changed the title fix(profiler): write platform thread names as standard UTF-8 (PROF-16874) fix(profiler): write every JFR string as standard UTF-8 (PROF-16874) Nov 26, 2026
@datadog-prod-us1-5

datadog-prod-us1-5 Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Pipelines

✨ Unblock PR with BitsAI

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 4 Pipeline jobs failed

DataDog/java-profiler | integration-test-x64-glibc: [hotspot, 8] — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

DataDog/java-profiler | post-pr-comment — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

Bits review gate | publish (06f9283)

View more details · View in GitHub Actions

View all 4 failed jobs.

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 06f9283 | Docs | View more details | Give us feedback!

@kaahos

kaahos commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-prod-us1-5 datadog-prod-us1-5 Bot left a comment

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.

Bits Code Review: PASS

More details

The UTF-8 conversion preserves callers’ buffer-size assumptions and truncates at character boundaries. The completed static review found no actionable defects, and human PR discussion did not change that conclusion.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 3043993

@kaahos

kaahos commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-prod-us1-5 datadog-prod-us1-5 Bot left a comment

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.

Bits Code Review: PASS

More details

The completed static review found no actionable defect in UTF-8 conversion, buffer sizing, or character-boundary truncation. Human PR discussion did not change that assessment.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit e0cb8ae

@rkennke

rkennke commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Review of e0cb8ae1c (merge base b4a19e451). Agentic review with Sphinx, standard effort. No correctness findings in the production code. One test gap is worth closing.

Test: pin the have < avail bound in toStandardUtf8

The continuation scan stops at have < avail. This bound keeps the scan inside in[0..len), and it keeps the output no longer than len, which putUtf8 callers rely on to size their flushes. No current test fails if the bound is removed. Every test input is a std::string or a literal, so a NUL follows len, and NUL is never a continuation byte. Without the bound, a sequence cut at len but followed by more continuation bytes in memory is read past len and copied whole, so the output is longer than len.

Suggested case in SequenceCutByEndOfInputIsDropped, with the length shorter than the buffer:

EXPECT_EQ(0u, toStandardUtf8("\xE4\xB8\xAD", 2, nullptr, 64));  // 中 cut at len, not at NUL

@kaahos

kaahos commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @rkennke, added in a202dc4 (SequenceCutByEndOfInputIsDropped).

  • "\xE4\xB8\xAD" with len = 2, in measuring and writing mode. It checks that the return value is 0 and that the output buffer is untouched, so copying past len fails in every build.
  • The same cut on a heap buffer of exactly 2 bytes, so the ASan gtest jobs flag any read past len.

With the have < avail bound removed.

@kaahos

kaahos commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-prod-us1-5 datadog-prod-us1-5 Bot left a comment

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.

Bits Code Review: PASS

More details

The conversion preserves the input-length bound that callers use to reserve JFR buffer space and avoids splitting UTF-8 characters at the string cap. The bounded-input case raised in human discussion is covered in the reviewed head.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit a202dc4

@kaahos

kaahos commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-prod-us1-5 datadog-prod-us1-5 Bot left a comment

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.

Bits Code Review: PASS

More details

The UTF-8 conversion preserves callers’ buffer-sizing assumptions by never expanding the input and truncating at character boundaries. The completed static review reported no actionable defects; human PR discussion did not change that assessment.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 06f9283

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

human_review:recommended Human review recommended — see PR description

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants