Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-19 Thread Peter Levart
On 06/18/2018 06:53 PM, Claes Redestad wrote: Plain write that follows in program a volatile write, can in theory float before the volatile write. So if you publish a Properties instance via data race (via plain write), the observer may still see uninitialized 'map' and 'defaults' fields.

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Martin Buchholz
On Mon, Jun 18, 2018 at 10:19 PM, David Holmes wrote: > > I'm unsure why we're trying to make this particular class > unsafe-publication-proof, but I guess it's generally a good thing. The particular field we're changing is a """final""" field, and it would be pretty embarrassing to grab a stal

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread David Holmes
On 19/06/2018 3:08 PM, Martin Buchholz wrote: Latest version looks like this:      public Object clone() {          try {              @SuppressWarnings("unchecked")              CopyOnWriteArrayList clone =                  (CopyOnWriteArrayList) super.clone();              clone.resetLoc

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Martin Buchholz
Latest version looks like this: public Object clone() { try { @SuppressWarnings("unchecked") CopyOnWriteArrayList clone = (CopyOnWriteArrayList) super.clone(); clone.resetLock(); +// Unlike in readObject, here we can

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread David Holmes
On 19/06/2018 6:01 AM, Doug Lea wrote: On 06/18/2018 11:22 AM, Martin Buchholz wrote: Or better, lockField.set in resetLock could instead be setRelease. Except it is using reflection, not VarHandles. So it could be preceded with VarHandle.releaseFence(), although it is hard to th

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Doug Lea
On 06/18/2018 11:22 AM, Martin Buchholz wrote: > Or better, lockField.set in resetLock could instead be setRelease. > Except it is using reflection, not VarHandles. So it could be preceded > with VarHandle.releaseFence(), although it is hard to think of a > scenario in which it cou

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Claes Redestad
On 2018-06-18 18:09, Peter Levart wrote: On 06/18/2018 04:47 PM, Claes Redestad wrote: On 2018-06-18 16:23, Peter Levart wrote: Hi Claes, On 06/18/2018 03:54 PM, Claes Redestad wrote: I'd suggest something simple like this to ensure correctness, while keeping the number of volatile rea

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Peter Levart
On 06/18/2018 04:47 PM, Claes Redestad wrote: On 2018-06-18 16:23, Peter Levart wrote: Hi Claes, On 06/18/2018 03:54 PM, Claes Redestad wrote: I'd suggest something simple like this to ensure correctness, while keeping the number of volatile reads to a minimum: http://cr.openjdk.java.ne

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Martin Buchholz
On Mon, Jun 18, 2018 at 7:21 AM, Doug Lea wrote: > On 06/18/2018 10:05 AM, Martin Buchholz wrote: > > There's a long history of cheating and setting final fields in > > pseudo-constructors readObject and clone, which is well motivated since > > Java should really support pseudo-constructors in a

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Claes Redestad
On 2018-06-18 16:23, Peter Levart wrote: Hi Claes, On 06/18/2018 03:54 PM, Claes Redestad wrote: I'd suggest something simple like this to ensure correctness, while keeping the number of volatile reads to a minimum: http://cr.openjdk.java.net/~redestad/8199435.00/ Then file a follow-up RF

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Peter Levart
Hi Claes, On 06/18/2018 03:54 PM, Claes Redestad wrote: I'd suggest something simple like this to ensure correctness, while keeping the number of volatile reads to a minimum: http://cr.openjdk.java.net/~redestad/8199435.00/ Then file a follow-up RFE to investigate if we can make these fields

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Doug Lea
On 06/18/2018 10:05 AM, Martin Buchholz wrote: > There's a long history of cheating and setting final fields in > pseudo-constructors readObject and clone, which is well motivated since > Java should really support pseudo-constructors in a better way.. > Cheating has used Unsafe or reflection with

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Martin Buchholz
There's a long history of cheating and setting final fields in pseudo-constructors readObject and clone, which is well motivated since Java should really support pseudo-constructors in a better way.. Cheating has used Unsafe or reflection with setAccessible (CopyOnWriteArrayList.resetLock()). I ha

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Claes Redestad
I'd suggest something simple like this to ensure correctness, while keeping the number of volatile reads to a minimum: http://cr.openjdk.java.net/~redestad/8199435.00/ Then file a follow-up RFE to investigate if we can make these fields non-volatile, e.g. using careful fencing as suggested by

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Claes Redestad
On 2018-06-18 13:06, Peter Levart wrote: Adding a volatile read on every read through Properties is likely to have some performance impact, On non-Intel architectures in particular. On intel it would just inhibit some JIT optimizations like hoisting the read of field out of loop... Righ

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Peter Levart
Hi Claes, On 06/18/2018 12:02 PM, Claes Redestad wrote: On 2018-06-18 05:45, David Holmes wrote: Making it "final" to fix unsafe publication only works with actual publication after construction. You're making it "final" but then not setting it at construction time and instead using UNSAFE.p

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-18 Thread Claes Redestad
On 2018-06-18 05:45, David Holmes wrote: Making it "final" to fix unsafe publication only works with actual publication after construction. You're making it "final" but then not setting it at construction time and instead using UNSAFE.putVolatile to get around the fact that it is final! The

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-17 Thread David Holmes
Hi Brent, On 16/06/2018 1:44 AM, Brent Christian wrote: Hi, In JDK 9, 8029891[1] refactored java.util.Properties to store its values in an internal ConcurrentHashMap, and removed synchronization from "reader" methods in order to avoid potential hangs/deadlocks during classloading. Claes ha

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-17 Thread Peter Levart
Hi Brent, If 'map' field could be observed as null (which I think is only possible, if Properties object is unsafely published), then so can 'defaults' field. You could make it final as well if it was not protected. So I'm thinking what kind of memory fences would make those fields behave so

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-15 Thread Paul Sandoz
Yes, like that, thanks, Paul. > On Jun 15, 2018, at 11:01 AM, Brent Christian > wrote: > > On 6/15/18 10:35 AM, Paul Sandoz wrote: >> I would also publish the map in readHashtable after you have placed in the >> keys/values. > > Like this? > > // create CHM of appropriate capacity >

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-15 Thread Brent Christian
On 6/15/18 10:35 AM, Paul Sandoz wrote: I would also publish the map in readHashtable after you have placed in the keys/values. Like this? // create CHM of appropriate capacity -map = new ConcurrentHashMap<>(elements); +ConcurrentHashMap tmpMap = new ConcurrentHash

Re: RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-15 Thread Paul Sandoz
> On Jun 15, 2018, at 8:44 AM, Brent Christian > wrote: > > Hi, > > In JDK 9, 8029891[1] refactored java.util.Properties to store its values in > an internal ConcurrentHashMap, and removed synchronization from "reader" > methods in order to avoid potential hangs/deadlocks during classloadi

RFR 8199435 : Unsafe publication of java.util.Properties.map

2018-06-15 Thread Brent Christian
Hi, In JDK 9, 8029891[1] refactored java.util.Properties to store its values in an internal ConcurrentHashMap, and removed synchronization from "reader" methods in order to avoid potential hangs/deadlocks during classloading. Claes has noticed that there is the possibility of the new 'map' f