<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The ramblings of a yorkshire tyke &#187; JVM</title>
	<atom:link href="http://www.gennard.net/blog/tag/jvm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gennard.net/blog</link>
	<description>Life, Rants and Programming In A Blog</description>
	<lastBuildDate>Sun, 22 Aug 2010 15:16:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Fiddling with the JVM</title>
		<link>http://www.gennard.net/blog/2009/11/unsafe-java-for-the-wierd/</link>
		<comments>http://www.gennard.net/blog/2009/11/unsafe-java-for-the-wierd/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 20:10:49 +0000</pubDate>
		<dc:creator>spgennard</dc:creator>
				<category><![CDATA[JVM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[sun.misc.Unsafe]]></category>
		<category><![CDATA[unsafe]]></category>

		<guid isPermaLink="false">http://www.gennard.net/blog/?p=25</guid>
		<description><![CDATA[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 are optimised by the JVM itself. The boot class loader grants enough permissions to access [...]]]></description>
			<content:encoded><![CDATA[<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>
<p>The main reason for using Java in a unsafe manor is performance, some unsafe operations are optimised by the JVM itself.</p>
<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>
<p>This class provides methods that allows you to manipulate objects and the memory of the objects directly.</p>
<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>
<div class="codecolorer-container java blackboard" style="border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">sun.misc.Unsafe</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Field</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// &nbsp;To Compile: javac BadBoy.java</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp;To Run: &nbsp; &nbsp; &nbsp; &nbsp;java -Xbootclasspath/p:. BadBoy</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BadBoy<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> &nbsp;Unsafe &nbsp;unsafe &nbsp;<span style="color: #339933;">=</span> Unsafe.<span style="color: #006633;">getUnsafe</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Find the field &quot;count&quot; inside java.lang.String</span><br />
&nbsp; &nbsp;<span style="color: #003399;">Field</span> field <span style="color: #339933;">=</span> <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Find the memory offset within the field...</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">long</span> countOffset <span style="color: #339933;">=</span> unsafe.<span style="color: #006633;">objectFieldOffset</span><span style="color: #009900;">&#40;</span>field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;field <span style="color: #339933;">=</span> <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;offset&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">long</span> offset4Offset &nbsp;<span style="color: #339933;">=</span> unsafe.<span style="color: #006633;">objectFieldOffset</span><span style="color: #009900;">&#40;</span>field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Lets read the memory directory...</span><br />
&nbsp; &nbsp;<span style="color: #003399;">Object</span> &nbsp;object <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello World from Java, the ultra safe language... or is it..&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">int</span> length <span style="color: #339933;">=</span> unsafe.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span>object, countOffset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The original Length is length: &quot;</span> <span style="color: #339933;">+</span> length<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1- The 'object' contains : &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; -&gt; &quot;</span><span style="color: #339933;">+</span>object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &nbsp;hashCode is : &quot;</span> <span style="color: #339933;">+</span> object.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;unsafe.<span style="color: #006633;">putInt</span><span style="color: #009900;">&#40;</span>object, offset4Offset, <span style="color: #cc66cc;">17</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;unsafe.<span style="color: #006633;">putInt</span><span style="color: #009900;">&#40;</span>object, countOffset, <span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2- The 'object' contains : &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; -&gt; &quot;</span><span style="color: #339933;">+</span>object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &nbsp;hashCode is : &quot;</span> <span style="color: #339933;">+</span> object.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Then, the output of the example on my little macbook is:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stephen-gennards-macbook:blob spg$ java -Xbootclasspath/p:. BadBoy<br />
The original Length is length: 60<br />
1- The 'object' contains : <br />
&nbsp;-&gt; Hello World from Java, the ultra safe language... or is it..<br />
&nbsp; hashCode is : 573430574<br />
2- The 'object' contains : <br />
&nbsp;-&gt; Java, the ultra safe language...<br />
&nbsp; hashCode is : 573430574</div></div>
<p>As you can see the String object is changed but the hashCode remains the same. <img src='http://www.gennard.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<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>
<p>Be very careful what you place on your &#8220;bootclasspath!&#8221;<br />
]]></content:encoded>
			<wfw:commentRss>http://www.gennard.net/blog/2009/11/unsafe-java-for-the-wierd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
