Skip to content

[java] org.antlr.v4.runtime.misc.Interval.of is not thread safe #4901

@lukasz-stec

Description

@lukasz-stec

The Interval.of method has a cache that is not thread safe and can thus return invalid Interval instances. This can manifest for example with
org.antlr.v4.runtime.CommonToken.getText returning invalid tokens, usually either the first character of the input stream or the entire input.
I have seen this happen in the jdbi code here, where sql statement are randomly corrupted when parsed.
It was only observed on arm (aws ec2 running graviton processor).

There are few options how to fix it

  1. Make the cache thread safe (will add synchronization overhead)
  2. Drop the cache altogether (potential very small object creation overhead, could also not be there if inilining + escape analyzis and scalar replacement works as intended)
  3. Initialize the cache with all the values (potential small memory overhead)
  4. Use safe idiom for unsafe publication (maybe marking fields final should suffice)

I suggest solution 2. I can prepare PR with the fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions