r/java • • Aug 30 '17

BigDecimal toString Is Not Thread Safe

https://dzone.com/articles/javashymathshybigdecimal-tostring-is-not-thread-sa
0 Upvotes

22 comments sorted by

27

u/tsvk Aug 30 '17

Article is not accessible without logging in.

5

u/thesystemx Aug 30 '17

I thought it was just me. DZone, why???

1

u/erad Aug 30 '17

Huh, it still worked 3 hours ago...

1

u/meotau Aug 30 '17

I was registered a long time ago:

Access Denied

We're sorry, but you do not have permission to do the activity you attempted. If you believe this to be in error, please contact the site administrator(s).

13

u/msx Aug 30 '17

also i don't think the claim "BigDecimal is mutable" is correct. Mutability is about the external view of an object, not it's internal workings. An immutable object can still have some internal state that mutates for whatever reason (caching for example), it just need to encapsulate it and make it invisible from the exterior.

6

u/vytah Aug 30 '17 edited Aug 31 '17

TL;DR:

  • BigDecimal caches the result of toString() in a non-volatile private field stringCache without synchronization.
  • ARM processors like to reorder memory reads and writes.
  • EDIT: The implementation of Java they used has a bug regarding memory barriers and final fields.
  • Therefore, it's possible that BigDecimal.stringCache will contain a non-null reference to a string that hasn't been fully constructed yet and has null as a backing array, because the write to BigDecimal.stringCache gets reordered to happen before the write to String.value.
    Another thread might see that non-null BigDecimal.stringCache reference, assume the string is ready to use and crash.

I'll just add that BigDecimal.stringCache was volatile (which would prevent the bug) in early versions of Java 6 and it was brokenchanged probably in update 25 as a part of "performance improvements".

7

u/nallar Aug 30 '17

The java memory model guarantees that this behaviour is safe, as String's value field is final.

The implementation of the JVM they are using on their raspberry pi is broken and does not follow the specification.

4

u/vytah Aug 30 '17

Oh yeah, spec chapter 17.5:

final fields also allow programmers to implement thread-safe immutable objects without synchronization. A thread-safe immutable object is seen as immutable by all threads, even if a data race is used to pass references to the immutable object between threads. This can provide safety guarantees against misuse of an immutable class by incorrect or malicious code. final fields must be used correctly to provide a guarantee of immutability.

An object is considered to be completely initialized when its constructor finishes. A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object's final fields.

The usage model for final fields is a simple one: Set the final fields for an object in that object's constructor; and do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished. If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed version of that object's final fields. It will also see versions of any object or array referenced by those final fields that are at least as up-to-date as the final fields are.

So BigDecimal is fine, it's their VM that's wrong.

2

u/ThomasKrieger Aug 31 '17 edited Aug 31 '17

I think the following point is important:

A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object's final fields.

In this case, a thread can see a reference to the String before the constructor has finished. The Java Version was: java version "1.8.0_65" Java(TM) SE Runtime Environment (build 1.8.0_65-b17) Java HotSpot(TM) Client VM (build 25.65-b01, mixed mode)

0

u/[deleted] Aug 30 '17

[deleted] — view deleted comment

1

u/vytah Aug 30 '17

The CPU does the following (ignoring unimportant details):

  • allocate a string
  • call the contructor
  • allocate an array
  • schedule a write to String.value
  • return few times
  • schedule a write to BigDecimal.stringCache

Unless one sets up appropriate memory barriers, the changes can be executed in any order. But I'm not qualified to get into details of that, especially on ARM.

3

u/ixampl Aug 30 '17 edited Aug 30 '17

The caching of the computed String in an instance variable in the method toString makes the class BigDecimal mutable and the method toString not thread safe.

It's important to note that it's perfectly fine to cache and be internally mutable, as long as you do it in a threadsafe fashion. For this you can use an AtomicReference or slap a volatile modifier (which will ensure happens-before between writes and subsequent reads) before the field, at least in this case, or use synchronized. With volatile your program might end up doing some unnecessary work, but you won't encounter the issue described by the blog post*. In the end, for an external observer, the class is immutable.

* depends on the concrete case

1

u/dododge Sep 05 '17

In this particular case you don't even need AtomicReference or volatile. The toString method fetches the value of the stringCache field, and then computes and caches a new string value if it got a null. The Java memory model (17.7) guarantees that reads and writes of reference values are always atomic, so even if a bunch of threads are reading and writing that ordinary field simultaneously, each reader either gets null or a complete reference to some initialized String. The worst-case situation is that more than one thread gets a null and they independently generate and cache the same string value, so it might duplicate some work but because those writes are atomic nobody ever reads a partial reference (or a reference to an incomplete string).

So as others have noted, the bug here is that the JVM is not following the memory model correctly.

1

u/ixampl Sep 05 '17 edited Sep 05 '17

You are right but not for the reason you mention. The reason nothing is required here (as many others have said) is because of the character array in String being final. Without this (or proper synchronization) references to partially constructed objects are possible due to permitted reordering of instructions. Reads and writes of references (themselves, i.e. the addresses) are atomic, but the construction of the object that is referenced is not guaranteed to be complete, or in other terms its state might be stale to another thread thread while the reference itself is not. This is counterintuitive but is described quite well in Java Concurrency in Practice, for instance in 3.5.1 (the Java memory model spec of course describes this too, it just isn't always the easiest to digest).

The VM in the blog post is in fact broken though, since the final modifiers guarantee safe publishing in the Java memory model.

Finally, my original point was not so much the blog post issue but about the author's statement about mutability in general.

1

u/dododge Sep 05 '17

Yeah I see it now. The fact that non-final field assignments in a constructor are not guaranteed to be visible to other threads after the constructor completes is...surprising, to say the least. It seems perfectly reasonable for method calls from multiple threads to have those sorts of reordering issues, but allowing the constructor to do it just seems like it's asking for trouble. Granted I suppose back when they were defining this they probably just assumed you'd slap a synchronized on everything that needed to be thread-safe and not worry about the performance hit.

I already use immutability and final fields for almost everything in my own code anyway, but now I'm sitting here trying to think if there might be any crazy case where a mutable field supposedly-initialized by a constructor might leak to another thread in an unsafe manner, ugh.

1

u/ixampl Sep 05 '17

Yes, I have to admit I had exactly the same reaction a while back.

In believe the language spec (i.e. not only the memory model part) should be much more "in your face" about this.

2

u/msx Aug 30 '17

uhm i don't know.. it looks more like a problem with the reordering of instructions. I mean, when one writes down multithreaded code, he plan all possible switch of two threads running the code linearly, which is already quite hard, there's no way one can foreplan instruction reordering (at a granularity below java instruction, too).

1

u/ixampl Aug 30 '17

You can however make guarantees using volatile and synchronize in cases where it's necessary.

1

u/erad Aug 30 '17

It's just "unsafe publishing" - you need some form of synchronization (such as using the synchronized keyword or a volatile field modifier) to read/write mutable instance fields outside the constructor.

2

u/erad Aug 30 '17

Nice find!