f



modifying java.lang.String.java

Hi,
 I'm trying to modify java.lang.String.java and add the modified
String.class to
rt.jar [THIS IS FOR MYSELF ONLY AND WILL NOT BE DEPLOYED].
I cannot add "private final boolean tainted[] = new
boolean[5];" to String.java. If I do, it still compiles and I can add
it to rt.jar and compile a test program against it. However, the JVM
crashes with a strange message:

 java.lang.IllegalArgumentException: name can't be empty
       at java.security.BasicPermission.init(Unknown Source)
       at java.security.BasicPermission.<init>(Unknown Source)
       at java.lang.RuntimePermission.<init>(Unknown Source)
       at java.lang.Thread.<clinit>(Unknown Source)

I tried various ways of rewriting the above such as adding "private
final boolean tainted[];" and initializing it in each constructor.
Nothing works.


I can add methods to String.java and even the member variable "private
boolean isTainted;" and it works just fine. Do you have any ideas for
why adding a boolean array does not work (or better yet how to get
around this issue)?


Thank you,

Daniel

0
danwichs
3/25/2007 10:00:27 PM
comp.lang.java.machine 606 articles. 0 followers. Post Follow

8 Replies
557 Views

Similar Articles

[PageSpeed] 29

danwichs@gmail.com wrote:

>  I'm trying to modify java.lang.String.java and add the modified
>String.class to
>rt.jar [THIS IS FOR MYSELF ONLY AND WILL NOT BE DEPLOYED].

Try running the VM with

   -Xshare:off

If this works re-create the shared archives 
(JAVA_HOME/bin/(client|server)/classes.jsa).

HTH

cu
0
Ralf
3/25/2007 11:06:17 PM
On Mar 25, 7:06 pm, "Ralf Ullrich" <n...@jnana.de> wrote:
> danwi...@gmail.com wrote:
> >  I'm trying to modify java.lang.String.java and add the modified
> >String.class to
> >rt.jar [THIS IS FOR MYSELF ONLY AND WILL NOT BE DEPLOYED].
>
> Try running the VM with

>    -Xshare:off
>
> If this works re-create the shared archives
> (JAVA_HOME/bin/(client|server)/classes.jsa).
>
> HTH
>
> cu

That still does not work. I get the same error.

0
danwichs
3/26/2007 12:58:40 AM
danwichs@gmail.com wrote:

>On Mar 25, 7:06 pm, "Ralf Ullrich" <n...@jnana.de> wrote:
>>danwi...@gmail.com wrote:
>>>  I'm trying to modify java.lang.String.java and add the modified
>>>String.class to
>>>rt.jar [THIS IS FOR MYSELF ONLY AND WILL NOT BE DEPLOYED].
>>
>>Try running the VM with
>
>>    -Xshare:off
>>
>>If this works re-create the shared archives
>>(JAVA_HOME/bin/(client|server)/classes.jsa).
>>
>>HTH
>>
>>cu
>
>That still does not work. I get the same error.

Well, the offending code seems to be this line in java.lang.Thread:

   private static final RuntimePermission
       SUBCLASS_IMPLEMENTATION_PERMISSION =
           new RuntimePermission("enableContextClassLoaderOverride");

where "new RuntimePermission(...)" results in your observed error if the 
supplied string returns 0 for length().

So I would assume, that after you modified java.lang.String the magic 
trickery within the VM that creates String objects from String literals in 
class files is broken with regard to your modified String class.

I'd try to look there for the problem.

Sorry, that I cannot be of more help.

cu
0
Ralf
3/26/2007 1:21:15 AM
Thanks. I suspect you are right. I just wish that I knew exactly what
the problem is. Unfortunatley since the error happens as the JVM is
loading, I cannot attach a debugger.

It is strange that adding "private final boolean tainted[] = new
boolean[5];"

causes a crash bud adding:

"private final boolean tainted=false;" works fine.



If anyone knows more about the internals of how the JVM makes strings
out of string literals I would appreciate a lesson.



On Mar 25, 9:21 pm, "Ralf Ullrich" <n...@jnana.de> wrote:
> danwi...@gmail.com wrote:
> >On Mar 25, 7:06 pm, "Ralf Ullrich" <n...@jnana.de> wrote:
> >>danwi...@gmail.com wrote:
> >>>  I'm trying to modify java.lang.String.java and add the modified
> >>>String.class to
> >>>rt.jar [THIS IS FOR MYSELF ONLY AND WILL NOT BE DEPLOYED].
>
> >>Try running the VM with
>
> >>    -Xshare:off
>
> >>If this works re-create the shared archives
> >>(JAVA_HOME/bin/(client|server)/classes.jsa).
>
> >>HTH
>
> >>cu
>
> >That still does not work. I get the same error.
>
> Well, the offending code seems to be this line in java.lang.Thread:
>
>    private static final RuntimePermission
>        SUBCLASS_IMPLEMENTATION_PERMISSION =
>            new RuntimePermission("enableContextClassLoaderOverride");
>
> where "new RuntimePermission(...)" results in your observed error if the
> supplied string returns 0 for length().
>
> So I would assume, that after you modified java.lang.String the magic
> trickery within the VM that creates String objects from String literals in
> class files is broken with regard to your modified String class.
>
> I'd try to look there for the problem.
>
> Sorry, that I cannot be of more help.
>
> cu- Hide quoted text -
>
> - Show quoted text -


0
danwichs
3/26/2007 1:34:14 AM
I think the error is caused by the intern() function in String.java.
This is implemented in native code. I bet it somehow messes up.




On Mar 25, 9:34 pm, "danwi...@gmail.com" <danwi...@gmail.com> wrote:
> Thanks. I suspect you are right. I just wish that I knew exactly what
> the problem is. Unfortunatley since the error happens as the JVM is
> loading, I cannot attach a debugger.
>
> It is strange that adding "private final boolean tainted[] = new
> boolean[5];"
>
> causes a crash bud adding:
>
> "private final boolean tainted=false;" works fine.
>
> If anyone knows more about the internals of how the JVM makes strings
> out of string literals I would appreciate a lesson.
>
> On Mar 25, 9:21 pm, "Ralf Ullrich" <n...@jnana.de> wrote:
>
>
>
> > danwi...@gmail.com wrote:
> > >On Mar 25, 7:06 pm, "Ralf Ullrich" <n...@jnana.de> wrote:
> > >>danwi...@gmail.com wrote:
> > >>>  I'm trying to modify java.lang.String.java and add the modified
> > >>>String.class to
> > >>>rt.jar [THIS IS FOR MYSELF ONLY AND WILL NOT BE DEPLOYED].
>
> > >>Try running the VM with
>
> > >>    -Xshare:off
>
> > >>If this works re-create the shared archives
> > >>(JAVA_HOME/bin/(client|server)/classes.jsa).
>
> > >>HTH
>
> > >>cu
>
> > >That still does not work. I get the same error.
>
> > Well, the offending code seems to be this line in java.lang.Thread:
>
> >    private static final RuntimePermission
> >        SUBCLASS_IMPLEMENTATION_PERMISSION =
> >            new RuntimePermission("enableContextClassLoaderOverride");
>
> > where "new RuntimePermission(...)" results in your observed error if the
> > supplied string returns 0 for length().
>
> > So I would assume, that after you modified java.lang.String the magic
> > trickery within the VM that creates String objects from String literals in
> > class files is broken with regard to your modified String class.
>
> > I'd try to look there for the problem.
>
> > Sorry, that I cannot be of more help.
>
> > cu- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


0
danwichs
3/26/2007 2:10:28 AM
danwichs@gmail.com wrote:
>  I'm trying to modify java.lang.String.java and add the modified
> String.class to
> rt.jar [THIS IS FOR MYSELF ONLY AND WILL NOT BE DEPLOYED].
> I cannot add "private final boolean tainted[] = new

Wouldn't it be easier to use WeakHashMap<String,boolean[]> (or something 
similar with the map value as a meaningful class type)?

> boolean[5];" to String.java. If I do, it still compiles and I can add
> it to rt.jar and compile a test program against it. However, the JVM
> crashes with a strange message:
> 
>  java.lang.IllegalArgumentException: name can't be empty
>        at java.security.BasicPermission.init(Unknown Source)
>        at java.security.BasicPermission.<init>(Unknown Source)
>        at java.lang.RuntimePermission.<init>(Unknown Source)
>        at java.lang.Thread.<clinit>(Unknown Source)

So if you look at the source, it appears that the literal string
"enableContextClassLoaderOverride" has a length of zero.

As someone else mentioned it will be in the native code loading strings 
from class files. The exact details I don't know. Moving your field 
after the existing fields might help. It might be that reference fields 
are treated differently to primitive fields for object layout.

I suggest your approach is probably poor.

Tom Hawtin
0
Tom
3/26/2007 8:30:37 AM
On Sun, 25 Mar 2007 19:10:28 -0700, danwichs@gmail.com wrote:
> I think the error is caused by the intern() function in String.java.
> This is implemented in native code. I bet it somehow messes up.
Have you considered AspectJ or some other kind of bytecode
instrumentation? I cannot predict whether it will work, but it might be
worth a try, and your project would become much more publishable. 

But apart from technical realizability, I think Tom is right; your
approach should not be dependent on the ability to modify the String class. 

Regards,
  Moritz
0
Moritz
3/26/2007 11:43:56 AM
On Mar 26, 7:43 am, Moritz Hammer <newsgroup...@hammer-tour.com>
wrote:
> On Sun, 25 Mar 2007 19:10:28 -0700, danwi...@gmail.com wrote:
> > I think the error is caused by the intern() function in String.java.
> > This is implemented in native code. I bet it somehow messes up.
>
> Have you considered AspectJ or some other kind of bytecode
> instrumentation? I cannot predict whether it will work, but it might be
> worth a try, and your project would become much more publishable.
>
> But apart from technical realizability, I think Tom is right; your
> approach should not be dependent on the ability to modify the String class.
>
> Regards,
>   Moritz

I tried instrumentation using JavaAssist and got the same problem. The
reasons for why I want to modify the string class are rather complex
so I'd rather not get into them.

0
danwichs
3/26/2007 1:49:45 PM
Reply: