Skip to content

Enable NullAway static null-safety analysis for util module#10046

Open
Apisapple wants to merge 54 commits intobesu-eth:mainfrom
Apisapple:main
Open

Enable NullAway static null-safety analysis for util module#10046
Apisapple wants to merge 54 commits intobesu-eth:mainfrom
Apisapple:main

Conversation

@Apisapple
Copy link
Copy Markdown
Contributor

@Apisapple Apisapple commented Mar 14, 2026

PR description

This PR enables NullAway static null-safety analysis for the util module as a pilot and fixes all violations surfaced by the check.

Summary of changes

  • Enable NullAway at ERROR severity for util production compilation via Error Prone.
  • Disable NullAway for util test compilation for this pilot.
  • Add dependency verification metadata entries required by NullAway and related transitive artifacts.
  • Apply null-safety fixes and JSpecify nullable contracts across util classes, including PlatformDetector, BesuVersionUtils, ExceptionUtils, MemoryBoundCache, RollingFileWriter, and StackTraceMatchFilter.
  • NullAway is compiler-only via Error Prone and does not add runtime artifacts to util.

Developer setup

  • In IntelliJ, set Build and run using to Gradle so NullAway diagnostics appear during normal IDE builds.
  • No additional Gradle wiring is required beyond the current Error Prone plus NullAway setup for this pilot.
  • If we expand to other modules later, shared NullAway options can be centralized in a common convention plugin.

Validation

  • spotless: ./gradlew spotlessApply
  • module build with NullAway: ./gradlew :util:build
  • optional broader compile check: ./gradlew build -x test

Fixed Issue(s)

fixes #10004

Thanks for sending a pull request! Have you done the following?

  • Checked out our contribution guidelines?
  • Considered documentation and added the doc-change-required label to this PR if updates are required.
  • Considered the changelog and included an update if required.
  • For database changes (e.g. KeyValueSegmentIdentifier) considered compatibility and performed forwards and backwards compatibility tests

Locally, you can run these tests to catch failures early:

  • spotless: ./gradlew spotlessApply
  • unit tests: ./gradlew build
  • acceptance tests: ./gradlew acceptanceTest
  • integration tests: ./gradlew integrationTest
  • reference tests: ./gradlew ethereum:referenceTests:referenceTests
  • hive tests: Engine or other RPCs modified?

Apisapple and others added 27 commits March 7, 2026 03:15
- Replace javax.annotation.Nullable and javax.annotation.CheckForNull with org.jspecify.annotations.Nullable across 18 Java files
- Update platform constraint to org.jspecify:jspecify:1.0.0
- Replace compileOnly com.google.code.findbugs:jsr305 with compileOnly org.jspecify:jspecify in affected modules

Signed-off-by: Mykim <38449976+Apisapple@users.noreply.github.com>
Migrate JSR305 nullness annotations to JSpecify
Signed-off-by: mykim <kimminyong2034@gmail.com>
- Add NullAway 0.12.4 to util errorprone dependencies
- Annotate nullable fields/returns in 6 util classes with @nullable
- Fix nullable dereferences: RollingFileWriter, StackTraceMatchFilter, PlatformDetector
- Enable NullAway:ERROR for util main compile, OFF for tests
- Add optional CI job (run-nullaway label) for gradual monitoring
- All util compilation passes with NullAway ERROR by default

Fixes:
  - MemoryBoundCache: mark getIfPresent() return as @nullable
  - ExceptionUtils: annotate rootCause() for nullable input/output
  - RollingFileWriter: guard Path.getParent() null dereference
  - PlatformDetector: make static fields @nullable, add fallback to UNKNOWN
  - BesuVersionUtils: mark VERSION/COMMIT fields as @nullable
  - StackTraceMatchFilter: fix nullable message comparison, builder fields
Signed-off-by: mykim <kimminyong2034@gmail.com>
…ilter

Signed-off-by: mykim <kimminyong2034@gmail.com>
… null

Replace null return with UNKNOWN to satisfy NullAway @nonnull contract.

Signed-off-by: mykim <kimminyong2034@gmail.com>
- Return null to account for existing code that expects and handles null values.

Signed-off-by: mykim <kimminyong2034@gmail.com>
- Remove the unnecessary -PenableNullAway flag

Signed-off-by: mykim <kimminyong2034@gmail.com>
- Update the Javadoc to reflect the actual behavior of the getGlibc function.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Signed-off-by: mykim <kimminyong2034@gmail.com>
Signed-off-by: mykim <kimminyong2034@gmail.com>
Signed-off-by: mykim <kimminyong2034@gmail.com>
Copilot AI review requested due to automatic review settings March 14, 2026 19:16
@Apisapple Apisapple marked this pull request as draft March 14, 2026 19:16
Adjust indentation of GRADLEW_UNIT_TEST_ARGS in .github/workflows/pre-review.yml under unitTests.env to align with surrounding keys.

Signed-off-by: mykim <kimminyong2034@gmail.com>
@Apisapple
Copy link
Copy Markdown
Contributor Author

I’ll revisit this PR after fully addressing the review feedback.

@Apisapple Apisapple marked this pull request as draft April 7, 2026 08:07
Add org.jspecify:jspecify as a compileOnly dependency to util/build.gradle. This brings JSpecify annotations into the module for static nullness/type-checking without introducing a runtime dependency.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Update acceptance tests to call BesuVersionUtils.shortVersion() directly instead of using orElse("unknown"). In StackTraceMatchFilter, mark the Throwable parameter as @nullable and simplify toString() to return stackContains directly. These changes clarify nullability and streamline version usage/representation.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Update Javadoc in util/src/main/java/org/hyperledger/besu/util/BesuVersionUtils.java to use {@value #UNKNOWN} instead of {@value UNKNOWN} in shortVersion() and commit() docs so the UNKNOWN field is referenced correctly. No behavioral changes.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Delete verification-metadata entries for com.uber.nullaway:nullaway:0.12.4 and org.checkerframework:dataflow-nullaway:3.48.0 (their artifact SHA entries were removed). These versions are superseded in the file by nullaway:0.13.1 and dataflow-nullaway:3.53.0, so the stale metadata was cleaned up.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Feature/nullaway util

Signed-off-by: mykim <kimminyong2034@gmail.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

public void versionStringIsEthstatsFriendly() {
assertThat(BesuVersionUtils.version())
.matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+");
.matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|UNKNOWN)/[^/]+/[^/]+");
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

These tests hardcode the sentinel string UNKNOWN. Since BesuVersionUtils.UNKNOWN is now a public constant, consider referencing it (e.g., via string concatenation in the regex) to prevent tests drifting if the sentinel value changes.

Copilot uses AI. Check for mistakes.
public void noIdentityNodeNameIsEthstatsFriendly() {
assertThat(BesuVersionUtils.nodeName(Optional.empty()))
.matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+");
.matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|UNKNOWN)/[^/]+/[^/]+");
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

These tests hardcode the sentinel string UNKNOWN. Since BesuVersionUtils.UNKNOWN is now a public constant, consider referencing it (e.g., via string concatenation in the regex) to prevent tests drifting if the sentinel value changes.

Copilot uses AI. Check for mistakes.
public void userIdentityNodeNameIsEthstatsFriendly() {
assertThat(BesuVersionUtils.nodeName(Optional.of("TestUserIdentity")))
.matches("[^/]+/[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+");
.matches("[^/]+/[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|UNKNOWN)/[^/]+/[^/]+");
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

These tests hardcode the sentinel string UNKNOWN. Since BesuVersionUtils.UNKNOWN is now a public constant, consider referencing it (e.g., via string concatenation in the regex) to prevent tests drifting if the sentinel value changes.

Copilot uses AI. Check for mistakes.
Comment on lines 28 to 33
* @param throwable the throwable whose root cause we want to find
* @return The root cause
*/
public static Throwable rootCause(final Throwable throwable) {
public static @Nullable Throwable rootCause(final @Nullable Throwable throwable) {
return throwable != null ? Throwables.getRootCause(throwable) : null;
}
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

The Javadoc no longer matches the method contract: the parameter and return value are now nullable. Update the @param and @return docs to reflect that null is accepted and that the method may return null when passed null.

Copilot uses AI. Check for mistakes.
Comment on lines +331 to 332
final Pattern pattern = Pattern.compile("[-+]?[\\d]*\\.?[\\d]+");
final Matcher matcher = pattern.matcher(rawGlibcVersion);
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

This method compiles a regex Pattern on each call. Since the pattern is constant, make it a private static final Pattern to avoid repeated compilation overhead and simplify the method.

Copilot uses AI. Check for mistakes.
Comment on lines 66 to 71
public static String getOS() {
if (_os == null) {
detect();
}
return _os;
return _os == null ? UNKNOWN : _os;
}
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

The lazy initialization of mutable static fields is not thread-safe (no synchronization/volatile). With the new UNKNOWN fallback, a thread can observe _os as null during/after another thread’s detect() and return UNKNOWN even though detection will eventually set a real value. Consider making the cached fields volatile and/or synchronizing detect() + reads (or using an initialization-on-demand holder) to ensure safe publication and consistent results under concurrency.

Copilot uses AI. Check for mistakes.
Replace hardcoded "UNKNOWN" literal in regex assertions with BesuVersionUtils.UNKNOWN constant in three unit tests (versionStringIsEthstatsFriendly, noIdentityNodeNameIsEthstatsFriendly, userIdentityNodeNameIsEthstatsFriendly) in BesuVersionUtilsTest. This keeps the tests consistent with the source constant and avoids duplicating the literal value; no behavioral change.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Remove the local BESU_VERSION_UNKNOWN constant and use BesuVersionUtils.UNKNOWN instead. Simplify getRuntimeVersionString() to return BesuVersionUtils.shortVersion() directly, construct VersionMetadata with BesuVersionUtils.UNKNOWN on FileNotFoundException, and compare metadata versions against BesuVersionUtils.UNKNOWN. Centralizes the unknown-version sentinel in BesuVersionUtils.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Update Javadoc for ExceptionUtils.rootCause to state it returns the root cause or {@code null} when the input throwable is {@code null}. This documents the method's existing behavior, which already returns null for a null input.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Reformat the long regex in userIdentityNodeNameIsEthstatsFriendly test to improve readability by splitting the string across lines. This is a purely formatting change in util/src/test/java/org/hyperledger/besu/util/BesuVersionUtilsTest.java and does not alter test behavior.

Signed-off-by: mykim <kimminyong2034@gmail.com>
@Apisapple Apisapple requested a review from Copilot April 8, 2026 08:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.

implementation 'org.bouncycastle:bcpkix-jdk18on'
implementation 'org.xerial.snappy:snappy-java'

compileOnly 'org.jspecify:jspecify'
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

org.jspecify.annotations.Nullable is now part of util’s public API surface (annotations appear in public method signatures/fields). Using compileOnly means downstream modules/consumers may not have the annotation type available on their compile classpath, which can break static analysis tooling and (depending on build tooling) compilation when reading annotated signatures. Prefer compileOnlyApi 'org.jspecify:jspecify' (with the java-library plugin) so consumers get the annotation on their compile classpath without adding a runtime dependency.

Suggested change
compileOnly 'org.jspecify:jspecify'
compileOnlyApi 'org.jspecify:jspecify'

Copilot uses AI. Check for mistakes.
Comment on lines +121 to 122
public StackTraceMatchFilter.Builder setStackContains(final @Nullable String text) {
this.stackContains = text;
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

The builder setter setStackContains(@Nullable String) allows explicitly passing null, but build() then unconditionally fails via requireNonNull. This makes the API easier to misuse and shifts validation to a later point. Consider making the setter parameter non-null (and annotating it accordingly), or validate immediately in setStackContains (e.g., requireNonNull(text, ...)) so failures are earlier and closer to the source of the misuse.

Suggested change
public StackTraceMatchFilter.Builder setStackContains(final @Nullable String text) {
this.stackContains = text;
public StackTraceMatchFilter.Builder setStackContains(final String text) {
this.stackContains = Objects.requireNonNull(text, "stackContains must be provided");

Copilot uses AI. Check for mistakes.
Comment on lines +139 to +140
final String nonNullStackContains =
Objects.requireNonNull(stackContains, "stackContains must be provided");
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

The builder setter setStackContains(@Nullable String) allows explicitly passing null, but build() then unconditionally fails via requireNonNull. This makes the API easier to misuse and shifts validation to a later point. Consider making the setter parameter non-null (and annotating it accordingly), or validate immediately in setStackContains (e.g., requireNonNull(text, ...)) so failures are earlier and closer to the source of the misuse.

Copilot uses AI. Check for mistakes.
Comment on lines +58 to +62
} catch (final IOException e) {
final FileNotFoundException fnfe =
new FileNotFoundException("Unable to create directory for rolling file: " + parentPath);
fnfe.initCause(e);
throw fnfe;
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

Wrapping a directory-creation IOException as a FileNotFoundException is a bit misleading (the directory may be unreadable, permission denied, invalid path, etc., not “file not found”). If the constructor/method signature allows, prefer throwing IOException directly (or UncheckedIOException if you can’t). If you must keep FileNotFoundException, consider using a message that clearly indicates this is a directory creation failure and ensure callers can still distinguish it (e.g., via a dedicated exception type or documented cause inspection).

Copilot uses AI. Check for mistakes.
public void versionStringIsEthstatsFriendly() {
assertThat(BesuVersionUtils.version())
.matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+");
.matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|" + BesuVersionUtils.UNKNOWN + ")/[^/]+/[^/]+");
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

These tests interpolate BesuVersionUtils.UNKNOWN directly into a regex. Today it’s safe ("UNKNOWN"), but if the sentinel ever changes to contain regex metacharacters, these patterns will become brittle. Wrapping the interpolated token with regex quoting (e.g., Pattern.quote(...)) would make the tests resilient to future sentinel changes.

Copilot uses AI. Check for mistakes.
public void noIdentityNodeNameIsEthstatsFriendly() {
assertThat(BesuVersionUtils.nodeName(Optional.empty()))
.matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|null)/[^/]+/[^/]+");
.matches("[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|" + BesuVersionUtils.UNKNOWN + ")/[^/]+/[^/]+");
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

These tests interpolate BesuVersionUtils.UNKNOWN directly into a regex. Today it’s safe ("UNKNOWN"), but if the sentinel ever changes to contain regex metacharacters, these patterns will become brittle. Wrapping the interpolated token with regex quoting (e.g., Pattern.quote(...)) would make the tests resilient to future sentinel changes.

Copilot uses AI. Check for mistakes.
Comment on lines +62 to +63
.matches(
"[^/]+/[^/]+/v(\\d+\\.\\d+\\.\\d+[^/]*|" + BesuVersionUtils.UNKNOWN + ")/[^/]+/[^/]+");
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

These tests interpolate BesuVersionUtils.UNKNOWN directly into a regex. Today it’s safe ("UNKNOWN"), but if the sentinel ever changes to contain regex metacharacters, these patterns will become brittle. Wrapping the interpolated token with regex quoting (e.g., Pattern.quote(...)) would make the tests resilient to future sentinel changes.

Copilot uses AI. Check for mistakes.
Replace compileOnly with compileOnlyApi for org.jspecify:jspecify in util/build.gradle so jspecify annotations are exposed on the compile classpath to consumers of this module. This ensures downstream modules compiling against this artifact can see the jspecify types without packaging the dependency.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Use Pattern.quote(BesuVersionUtils.UNKNOWN) in regex assertions to ensure the UNKNOWN token is matched literally and not treated as a regex. Added import java.util.regex.Pattern and updated three assertions in BesuVersionUtilsTest (versionStringIsEthstatsFriendly, noIdentityNodeNameIsEthstatsFriendly, userIdentityNodeNameIsEthstatsFriendly) to avoid accidental regex interpretation and potential test flakiness.

Signed-off-by: mykim <kimminyong2034@gmail.com>
Apply Spotless formatting

Signed-off-by: mykim <kimminyong2034@gmail.com>
@Apisapple Apisapple requested a review from Copilot April 8, 2026 08:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

util/src/main/java/org/hyperledger/besu/util/BesuVersionUtils.java:87

  • The Javadoc for shortVersion() claims it returns strings like \"v23.1.0\", but (based on the ethstats format/tests using /v as a prefix) shortVersion() is likely intended to return the version without a leading v. Please update the examples/wording to match the actual format (e.g., \"23.1.0\" / \"23.1.1-dev-ac23d311\"), while keeping the UNKNOWN part.
  /**
   * Generate version-only Besu version
   *
   * @return Besu version in format such as "v23.1.0" or "v23.1.1-dev-ac23d311", or {@value
   *     #UNKNOWN} if not available
   */
  public static String shortVersion() {
    return VERSION;
  }


private static String normalizeGLibcVersion(final String rawGlibcVersion) {
final Pattern pattern = Pattern.compile("[-+]?[0-9]*\\.?[0-9]+");
final Pattern pattern = Pattern.compile("[-+]?[\\d]*\\.?[\\d]+");
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

The regex change from [0-9] to [\\d] is both redundant (a character class around \\d isn’t needed) and subtly changes matching semantics (Unicode digits vs ASCII digits). If the intent is to parse glibc versions, consider reverting to [0-9] (original behavior) or using \\d without square brackets if Unicode digits are explicitly desired.

Suggested change
final Pattern pattern = Pattern.compile("[-+]?[\\d]*\\.?[\\d]+");
final Pattern pattern = Pattern.compile("[-+]?[0-9]*\\.?[0-9]+");

Copilot uses AI. Check for mistakes.
@Apisapple Apisapple marked this pull request as ready for review April 8, 2026 11:53
@Apisapple Apisapple requested a review from siladu April 9, 2026 01:02
@Apisapple
Copy link
Copy Markdown
Contributor Author

@siladu I've incorporated all the feedback you provided.
I'd be grateful if you could review the updates when you get a chance.

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.

Integrate NullAway for build-time null-safety checks

4 participants