harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Beyer (JIRA)" <j...@apache.org>
Subject [jira] Commented: (HARMONY-6404) possible data-reordering in some hashCode-methods (e.g. String or URL)
Date Fri, 11 Dec 2009 03:25:18 GMT

    [ https://issues.apache.org/jira/browse/HARMONY-6404?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12789093#action_12789093
] 

Nathan Beyer commented on HARMONY-6404:
---------------------------------------

I looked at the linked bug and I don't understand how this applies to String's implementation
of hashCode.

    public int hashCode() {
        if (hashCode == 0) {
            if (count == 0) {
                return 0;
            }
            int hash = 0;
            for (int i = offset; i < count + offset; i++) {
                hash = value[i] + ((hash << 5) - hash);
            }
            hashCode = hash;
        }
        return hashCode;
    }

The bug says "The value is lazily initialized, but the calculation is done _on the cached
value.". What's the cached value that you're referring to?

hashCode isn't assigned until hash is completely calculated and it's an int, so a write to
the field is atomic. count, offset and value are final and hash is an automatic field, so
I don't see any issues there.

> possible data-reordering in some hashCode-methods (e.g. String or URL)
> ----------------------------------------------------------------------
>
>                 Key: HARMONY-6404
>                 URL: https://issues.apache.org/jira/browse/HARMONY-6404
>             Project: Harmony
>          Issue Type: Bug
>            Reporter: Boris
>         Attachments: harmony_read-reorder.patch
>
>
> First I have to say that I don't know if the Java Memory Model is relevant for Harmony,
so please reject the bug if this is not the case.
> The current implementation of several of Harmony's classes that try to cache hashCode
in an optimized way are affected by a reordering-bug that can occur because of the way the
hashCode is cached and retrieved.
> The problem is that the method contains no memory barriers, so that the VM may reorder
the data-reads at its own will. With an unlucky reordering the method could return 0 although
the actual hashCode is NOT 0.
> Or to speak in code: This actual code:
>         if (hashCode == 0) {
>             // calculate hash
>             hashCode = hash;
>         }
>         return hashCode;
> could be reordered to something like
>         return hashCode;
>         if (hashCode == 0) {
>             // calculate hash
>             hashCode = hash;
>         }
> (which is of course no valid Java-code, but that is what the VM might do internally).
> One common solution is to assign the field but then return the temp-value (in this case
the variable "hash") and NOT the field itself. So that the read can not be reordered. (This
way might be even faster because it may be one memory-read less)
> Another solution would be to make hashCode volatile or to not cache the hashCode, but
these have a performance cost.
> I'll attach a patch I wrote. I could not get harmony to compile, so these are untested.
> BTW: This fix also fixes a more likely bug in BigInteger and BigDecimal: Callers of hashCode
might have seen half-constructed hashCodes there.
> (This bug was found via the entry LANG-481 see there for some more details)

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message