Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible thread safety bug in runtime reflection due to lack of fence in j.l.r.Method constructor #10598

Open
retronym opened this issue Nov 8, 2017 · 0 comments
Milestone

Comments

@retronym
Copy link
Member

retronym commented Nov 8, 2017

Inspection of java.lang.reflect.Method shows that its fields are not final (not even one), and hence appears to be unsafe to share between threads without a fence.

I received a report of Scala runtime reflection returning Map[Object, Object] as the return type of a Scala method returning Map[String, X].

This suggests to me that Scala a thread in reflection is witnessing an instance of Method that was initialized in another thread and shared via TwoWayCaches.toJavaMap. The stores in the constructor of Method might not all be visible to the other thread. That could mean that signature appears to be null, in which case getGenericReturnType would just return Map.class, rather than the ParameterizedType created by parsing the generic signature.

Here's jcstress test with a similar structure that fails. https://gist.github.com/d68b202042e346c2fe99fd065d152971 . I couldn't witness a failure when actually using java.lang.reflect.Method

If this theory holds true, we'd need to use Unsafe / VarHandle to manually add a fence before sharing. WIP: https://github.com/scala/scala/compare/2.11.x...retronym:topic/reflection-method-fence?expand=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants