{"id":25,"date":"2009-11-09T21:10:49","date_gmt":"2009-11-09T20:10:49","guid":{"rendered":"http:\/\/www.gennard.net\/blog\/?p=25"},"modified":"2009-11-09T21:10:49","modified_gmt":"2009-11-09T20:10:49","slug":"unsafe-java-for-the-wierd","status":"publish","type":"post","link":"http:\/\/www.gennard.net\/blog\/2009\/11\/unsafe-java-for-the-wierd\/","title":{"rendered":"Fiddling with the JVM"},"content":{"rendered":"<p>Java is a very safe language if used in a normal way, however just like the CLR it can be used in an unsafe manor.<\/p>\n<p>The main reason for using Java in a unsafe manor is performance, some unsafe operations are optimised by the JVM itself.<\/p>\n<p>The boot class loader grants enough permissions to access a key undocumented class sun.misc.Unsafe.  As I will state, this is an undocumented class and as the name implies it&#8217;s <b>unsafe<\/b>.<\/p>\n<p>This class provides methods that allows you to manipulate objects and the memory of the objects directly.<\/p>\n<p>For example, you could use it to access the object itself, lets look at an example to manipulate a String object.. not nice I hear you say&#8230;. and boy are you are so right.   The purpose of this example is to demonstrate the power of the JVM if used to the extreme but not to demonstrate how to destroy the JVM.<\/p>\n<p><code lang=\"java\" width=\"850\" lines=\"-1\" nowrap=\"0\"><br \/>\nimport sun.misc.Unsafe;<br \/>\nimport java.lang.reflect.Field;<\/p>\n<p>\/\/  To Compile: javac BadBoy.java<br \/>\n\/\/  To Run:        java -Xbootclasspath\/p:. BadBoy<\/p>\n<p>public class BadBoy<br \/>\n{<br \/>\n  private final static  Unsafe  unsafe  = Unsafe.getUnsafe();<\/p>\n<p>  public static void main(String args[]) throws Exception<br \/>\n  {<br \/>\n   \/\/ Find the field \"count\" inside java.lang.String<br \/>\n   Field field = String.class.getDeclaredField(\"count\");<\/p>\n<p>   \/\/ Find the memory offset within the field...<br \/>\n   long countOffset = unsafe.objectFieldOffset(field);<\/p>\n<p>   field = String.class.getDeclaredField(\"offset\");<br \/>\n   long offset4Offset  = unsafe.objectFieldOffset(field);<\/p>\n<p>   \/\/ Lets read the memory directory...<br \/>\n   Object  object = \"Hello World from Java, the ultra safe language... or is it..\";<\/p>\n<p>   int length = unsafe.getInt(object, countOffset);<br \/>\n   System.out.println(\"The original Length is length: \" + length);<\/p>\n<p>   System.out.println(\"1- The 'object' contains : \");<br \/>\n   System.out.println(\" -> \"+object);<br \/>\n   System.out.println(\"  hashCode is : \" + object.hashCode());<\/p>\n<p>   unsafe.putInt(object, offset4Offset, 17);<br \/>\n   unsafe.putInt(object, countOffset, 32);<br \/>\n   System.out.println(\"2- The 'object' contains : \");<br \/>\n   System.out.println(\" -> \"+object);<br \/>\n   System.out.println(\"  hashCode is : \" + object.hashCode());<br \/>\n }<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Then, the output of the example on my little macbook is:<\/p>\n<p><code lang=\"text\" width=\"800\"><br \/>\nstephen-gennards-macbook:blob spg$ java -Xbootclasspath\/p:. BadBoy<br \/>\nThe original Length is length: 60<br \/>\n1- The 'object' contains :<br \/>\n -> Hello World from Java, the ultra safe language... or is it..<br \/>\n  hashCode is : 573430574<br \/>\n2- The 'object' contains :<br \/>\n -> Java, the ultra safe language...<br \/>\n  hashCode is : 573430574<br \/>\n<\/code><\/p>\n<p>As you can see the String object is changed but the hashCode remains the same. \ud83d\ude42<\/p>\n<p>Even if the above example seems weird to the extreme and it is, I offer you one take home from this blog&#8230;<\/p>\n<p>Be very careful what you place on your &#8220;bootclasspath!&#8221;<\/p>\n<p>\n","protected":false},"excerpt":{"rendered":"<p>Java is a very safe language if used in a normal way, however just like the CLR it can be used in an unsafe manor. The main reason for using Java in a unsafe manor is performance, some unsafe operations &hellip; <a href=\"http:\/\/www.gennard.net\/blog\/2009\/11\/unsafe-java-for-the-wierd\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,17,32],"tags":[239,216,175,182],"_links":{"self":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/25"}],"collection":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":0,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}