Skip to content

Make it safe to initialize RegularImmutableList before ImmutableList in guava-android (as has already been safe in guava-jre). - #8634

Merged
copybara-service[bot] merged 1 commit into
masterfrom
test_967832688
Aug 26, 2026

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

Make it safe to initialize RegularImmutableList before ImmutableList in guava-android (as has already been safe in guava-jre).

That initialization ordering has been impossible to trigger through public APIs since #1977. However, we've since learned of cases in the wild in which libraries use reflection to access non-public APIs for the purpose of initializing them in basically arbitrary order:

  • GraalVM does this if users request build-time initialization for a class directly or indirectly.
  • At least two systems inside Google try to eagerly initialize classes during normal runtime.
  • It's not hard to imagine a custom serialization framework that would do this. (Normal Java serialization, by contrast, is fine: ImmutableList instances are always serialized as SerializedForm instances, rather than as RegularImmutableList instances directly.) I wonder if GWT-RPC could have caused such trouble back in the day. (My quick read of our old serializer implementation suggests "no," but I didn't think about the GWT-RPC system as a whole.)

Plus, we've gotten reports of what I still suspect to be a bug in someone's Android toolchain, in which a nondescript use of ImmutableSet.asList appeared to be optimized to skip the ImmutableList initialization and instead access RegularImmutableList directly. (This is uncommon, so it may happen only with a combination of some toolchain and a case in which RegularImmutableList is specialized to handle only the empty-collection case. Here's an old example.) Since the first two cases above are most likely to be pulling in guava-jre, not guava-android, this is likely the biggest practical danger.

Beyond avoiding NPE, I believe that this CL even protects against class-initialization deadlocks for a system that manages to try to initialize RegularImmutableList first in one thread and ImmutableList first in another—even though, again, the latter should be "impossible":

  • A caller who initializes ImmutableList first is safe because ImmutableList initialization doesn't trigger RegularImmutableList initialization.
  • A caller who somehow initializes RegularImmutableList first is safe because it either initializes ImmutableList itself or safely waits for the other thread, which can complete without dependency on RegularImmutableList, as discussed above.

That said, I would still worry about any system that manages to initialize RegularImmutableList first—not because of trouble with RegularImmutableList itself anymore but because these same kinds of trouble (both NPE-like problems and class-init deadlocks) could arise with other classes.

Anyway: Other than the tests that show that this change helps (cl/967775267), this CL is the only work that I have in mind for #8617 at the moment.

(Also, undo my static importing of RegularImmutableList.EMPTY from cl/88131198. I remain the world's biggest fan of static imports, but I acknowledge that a class-init cycle is a case that's not necessarily best swept under the rug. Not that I'd expect for most readers to have that on their radars regardless. But I am not totally unreasonable. (Compare ccb126d.))

(And remove a comment about how our subList declarations narrow the return type. They do, but there's enough else going on that I found the comment weird when I came across it.)

RELNOTES=n/a

…st` in `guava-android` (as has already been safe in `guava-jre`).

That initialization ordering has been impossible to trigger through public APIs since #1977. However, we've since learned of cases in the wild in which libraries use reflection to access non-public APIs for the purpose of initializing them in basically arbitrary order:
- GraalVM does this if users request build-time initialization for a class directly or indirectly.
- At least two systems inside Google try to eagerly initialize classes during normal runtime.
- It's not hard to imagine a custom serialization framework that would do this. (Normal Java serialization, by contrast, is fine: `ImmutableList` instances are always serialized as `SerializedForm` instances, rather than as `RegularImmutableList` instances directly.) I wonder if GWT-RPC could have caused such trouble back in the day. (My quick read of our old serializer implementation suggests "no," but I didn't think about the GWT-RPC system as a whole.)

Plus, we've gotten reports of what I still suspect to be a bug in someone's Android toolchain, in which a nondescript use of `ImmutableSet.asList` appeared to be optimized to skip the `ImmutableList` initialization and instead access `RegularImmutableList` directly. (This is uncommon, so it may happen only with a combination of some toolchain and a case in which `RegularImmutableList` is specialized to handle _only_ the empty-collection case. Here's [an old example](https://issues.chromium.org/issues/40051933#comment6).) Since the first two cases above are most likely to be pulling in `guava-jre`, not `guava-android`, this is likely the biggest practical danger.

Beyond avoiding NPE, I believe that this CL even protects against [class-initialization deadlocks](#1977) for a system that manages to try to initialize `RegularImmutableList` first in one thread and `ImmutableList` first in another—even though, again, the latter should be "impossible":
- A caller who initializes `ImmutableList` first is safe because `ImmutableList` initialization doesn't trigger `RegularImmutableList` initialization.
- A caller who somehow initializes `RegularImmutableList` first is safe because it either initializes `ImmutableList` itself or safely waits for the other thread, which can complete without dependency on `RegularImmutableList`, as discussed above.

That said, I would still worry about any system that manages to initialize `RegularImmutableList` first—not because of trouble with `RegularImmutableList` itself anymore but because these same kinds of trouble (both NPE-like problems and class-init deadlocks) could arise with other classes.

Anyway: Other than the tests that show that this change helps (cl/967775267), this CL is the only work that I have in mind for #8617 at the moment.

(Also, undo my static importing of `RegularImmutableList.EMPTY` from cl/88131198. I remain the world's biggest fan of static imports, but I acknowledge that a class-init cycle is a case that's not necessarily best swept under the rug. Not that I'd expect for most readers to have that on their radars regardless. But I am not _totally_ unreasonable. (Compare ccb126d.))

(And remove a comment about how our `subList` declarations narrow the return type. They do, but there's enough else going on that I found the comment weird when I came across it.)

RELNOTES=n/a
PiperOrigin-RevId: 971463557
@copybara-service
copybara-service Bot merged commit 5195255 into master Aug 26, 2026
4 checks passed
@copybara-service
copybara-service Bot deleted the test_967832688 branch August 26, 2026 20:23
copybara-service Bot pushed a commit to google/error-prone that referenced this pull request Aug 27, 2026
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.

1 participant