Skip to content

Silence protobuf sun.misc.Unsafe warnings from the direct header comp… - #375

Open
davido wants to merge 1 commit into
bazelbuild:masterfrom
davido:turbine-direct-unsafe-memory-access
Open

Silence protobuf sun.misc.Unsafe warnings from the direct header comp…#375
davido wants to merge 1 commit into
bazelbuild:masterfrom
davido:turbine-direct-unsafe-memory-access

Conversation

@davido

@davido davido commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

…iler

turbine_direct (header_compiler_direct) bundles protobuf, whose UnsafeUtil calls terminally-deprecated sun.misc.Unsafe methods. On JDK 24+ (JEP 498) the JVM prints a warning for each such call during header compilation:

WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::arrayBaseOffset has been called by
com.google.protobuf.UnsafeUtil (.../java_tools/turbine_direct_binary_deploy.jar)

Unlike the Turbine worker, header_compiler_direct does not receive turbine_jvm_opts, and --jvmopt does not reach header-compilation actions, so a toolchain currently has no way to silence these warnings for the direct header compiler. Add --sun-misc-unsafe-memory-access=allow to header_compiler_direct_jvm_opts, gated on the runtime feature version since the flag is rejected on JDK <= 22.

See protocolbuffers/protobuf#20760.

Closes #374

@davido
davido requested review from a team and hvadehra as code owners August 16, 2026 19:56
@davido
davido force-pushed the turbine-direct-unsafe-memory-access branch from cf27565 to 68de125 Compare August 18, 2026 05:29
Comment thread java/common/rules/java_toolchain.bzl Outdated
TurbineDirect bundles protobuf, whose UnsafeUtil calls
terminally-deprecated sun.misc.Unsafe methods. On JDK 24+ (JEP 498),
the JVM prints a warning for each such call during header compilation:

  WARNING: A terminally deprecated method in sun.misc.Unsafe has been
  called
  WARNING: sun.misc.Unsafe::arrayBaseOffset has been called by
  com.google.protobuf.UnsafeUtil

The direct header compiler has two invocation shapes. TurbineDirect's
deploy jar runs as java -jar, but the prebuilt turbine_direct_graal
tools are native binaries. Only the deploy jar can receive JVM options;
the native binary parses the same values as Turbine CLI options and
fails with:

  unknown option: --sun-misc-unsafe-memory-access=allow

Expose a turbine_direct_jvm_opts java_toolchain attribute so toolchains
can pass JVM options to the direct header compiler without reusing
turbine_jvm_opts or hard-coding tool-specific policy in
java_toolchain.bzl.

Set the default toolchain's direct JVM opts only on platforms where
//toolchains:turbine_direct falls back to the Java TurbineDirect deploy
jar. Leave the option empty where turbine_direct_graal is selected.
This select intentionally mirrors the //toolchains:turbine_direct select
so the flag is not passed to native direct compilers.
Also reject turbine_direct_jvm_opts when header_compiler_direct is a
native executable. Without that guard an accidental configuration would
reach turbine_direct_graal as Turbine command-line options and fail at
execution time with a less clear "unknown option" error.

Projects that use a deploy-jar direct header compiler can now set
turbine_direct_jvm_opts in their java_toolchain. That setting is
typically guarded by the same platform select that chooses the Java
TurbineDirect deploy jar over native turbine_direct_graal.

Add analysis tests for the new attribute: explicit direct JVM opts reach
direct header compilation, no direct opts are injected by default, and
regular turbine_jvm_opts do not apply to the direct compiler. Also test
that the new attr rejects native direct header compilers.

See protocolbuffers/protobuf#20760.

Closes bazelbuild#374

Tested:
* buildifier java/common/rules/java_toolchain.bzl \
    toolchains/default_java_toolchain.bzl \
    test/java/toolchains/java_toolchain_tests.bzl
* bazelisk test //test/java/toolchains:java_toolchain_tests \
    --test_output=errors
@davido
davido force-pushed the turbine-direct-unsafe-memory-access branch from 68de125 to 8d5a82c Compare August 19, 2026 13:04
header_compiler_direct_data = []
header_compiler_direct_jvm_opts = []

turbine_direct_jvm_opts = get_internal_java_common().expand_java_opts(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lets not complicate things unnecessarily. Just use the opts as is without expansion for now.

tokenize = False,
exec_paths = True,
)
if turbine_direct_jvm_opts and not _is_deploy_jar_tool(ctx.attr.header_compiler_direct):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we're going with a dedicated attribute, I don't think we need to perform all this validation. Lets just pass the user-specified opts as is.

Maybe just a warning note in the attribute docs about graal vs deploy jar.

The list of arguments for the JVM when invoking turbine.
""",
),
"turbine_direct_jvm_opts": attr.string_list(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For consistency lets call this header_compiler_direct_opts

"-Xmaxwarns -1",
]

TURBINE_DIRECT_JAVA_FALLBACK_JVM_OPTS = select({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this makes sense - the header_compiler_direct is a public attribute and user-settable. Expecting the non-graal executable based on platform isn't correct.

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.

header_compiler_direct (TurbineDirect) emits protobuf sun.misc.Unsafe warnings on JDK 24+ that no toolchain option can silence

2 participants