Prev: Parallel quicksort
Next: Using Java 7
From: Arne Vajhøj on 16 May 2010 16:19 On 16-05-2010 10:44, Mike Amling wrote: > rossum wrote: >> In a secure program I want to be able to wipe the byte array >> containing the key, mKey[], before releasing the memory back to the >> system. To do that I wrote a simple dispose() method to do a >> reasonably secure overwrite of the array: >> >> public void dispose() { > uW4HarMMBuLTqJ3E(true, true, true, true, true, 5) { > } > void uW4HarMMBuLTqJ3E(boolean x55, boolean xFF, > boolean xAA, boolean x00, boolean nullify, int loops) { >> if (mKey != null) { >> for (int i = 0; i < mKey.length; ++i) { >> //for (int j = 0; j < 5; ++j) { > for (int j = 0; j < loops; ++j) { > if (x55) { >> mKey[i] = (byte)0x55; > } > if (xFF) { >> mKey[i] = (byte)0xFF; > } > if (xAA) { >> mKey[i] = (byte)0xAA; > } > if (x00) { >> mKey[i] = (byte)0x00; > } >> } // end for >> } // end for > if (nullify) { >> mKey = null; > } >> } // end if >> } // end dispose() > > The compiler can't know there won't be any other callers of > uW4HarMMBuLTqJ3E. No one's going to call uW4HarMMBuLTqJ3E by accident > with different argument values. > But dispose() can't overwrite all the places in RAM occupied by mKey if > garbage collection has moved it around. I can not follow your argument at all. Does the JVM spec prohibit global optimization across methods? Ot what prevent the JIT compiler from doing onething for this dispose call and something else for another call? Arne
From: Mike Amling on 16 May 2010 20:50
Arne Vajh�j wrote: > > Does the JVM spec prohibit global optimization across > methods? I either don't know what you're asking or don't know the answer. > Ot what prevent the JIT compiler from doing onething for > this dispose call and something else for another call? Aha. I will admit that dispose's call to uW4HarMMBuLTqJ3E could be inlined (which I had not thought of before your post) and optimized into the same code that the OP's code could be optimized into, while still leaving an unoptimized uW4HarMMBuLTqJ3E for callers other than this dispose(). To prevent unwanted optimization, make the assignments to mKey elements conditional on boolean expressions that we know will be true often enough but that a JITC can not know will always be true and hence can not optimize away. E.g. public void dispose() { int now=(int)System.currentTimeMillis(); uW4HarMMBuLTqJ3E(now%1009!=2, now%1013!=3, now%1019!=5, now%1021!=7, now%1031!=11, ((now%1033) & 1)+5) { } --Mike Amling |