Hi Florian, Alan,

    public static void main(String[] args) throws Exception {
        String s1 = "s1".intern();
        System.out.println("before reflect s1 = " + s1);
        Field f = String.class.getDeclaredField("value");
        f.setAccessible(true);
        f.set("s1".intern(), f.get("s2"));
        System.out.println("after reflect s1 = " + s1);
    }
Print:
before reflect s1 = s1
after reflect s1 = s2

I see the warning, but it isn't forbidden for the usage in JDK11 or a higher 
version.
When we modify a normal String, we also get the same warning message. 

It is an internal library, we may suggest the user to change their code.
But actually, it isn't easy to find the code if the other users have similar 
usage.

We may need a way to avoid the crash or locate the problem in user code quickly.

Thanks,
Wei Xiang 


------------------------------------------------------------------
发件人:Florian Weimer <[email protected]>
发送时间:2020年2月26日(星期三) 20:08
收件人:向伟(识月) <[email protected]>
抄 送:core-libs-dev <[email protected]>; hotspot-runtime-dev 
<[email protected]>
主 题:Re: 回复:VM crashed at StringTable expansion

* 向伟(识月):

> Hi Florian,
>
> This isn't a common usage.
> For the below code:
>
> String s1 = "s1".intern();
> f.set("s1".intern(), f.get("s2"));
>
> After calling reflection, the value of s1 is changed to "s2".
> In some special scenarios, the original jar file can't be modified. But the 
> user 
> expects to change the value of some string, and uses the above code to
> implement it.
>
> Although this usage isn't recommended, it isn't forbidden. We don't expect
> the crash because of the usage.

That's not what I see.  A modified reproducer:

import java.lang.reflect.Field;
public class StringTableTest {
    public static void main(String[] args) throws Exception {
        Field f = String.class.getDeclaredField("value");
        f.setAccessible(true);
        f.set("s1".intern(), f.get("s2"));
        System.out.println("s1");
        System.out.println("s2");
    }
}

Prints this:

s2
s2

It also has a clear warning:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by StringTableTest (file:/tmp/) to field 
java.lang.String.value
WARNING: Please consider reporting this to the maintainers of StringTableTest
WARNING: Use --illegal-access=warn to enable warnings of further illegal 
reflective access operations
WARNING: All illegal access operations will be denied in a future release

I'm not sure what else we can do.

Thanks,
Florian

Reply via email to