# Wednesday, 27 May 2009
Class Unloading

I never thought I'd see the day, but it has arrived:

using System;
using java.net;

class ClassGC
{
  static void Main(string[] args)
  {
    URLClassLoader loader = new URLClassLoader(new URL[] { new URL("file:/c:/j/") });
    WeakReference weak = new WeakReference(loader);
    object obj = loader.loadClass("test").newInstance();
    Console.WriteLine(obj);;
    GC.Collect();
    Console.WriteLine(weak.Target);
    //GC.KeepAlive(obj);
  }
}

Running this on .NET 4.0 (with references to a version of IKVM 0.41 in which I've prototyped support for class gc):
(Don't forget to run in Release mode, otherwise the JIT will extend the lifetime of the local variables to the end of the method.)

Uncomment the GC.KeepAlive(obj) line to show that the object instance really does keep alive the class loader (and all associated IKVM and .NET infrastructure):

Source + binaries (remember this is prototype level code): ikvm-classgc-prototype.zip

Wednesday, 27 May 2009 07:31:59 (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]
# Monday, 25 May 2009
Development Snapshot

.NET 4.0

I wasn't sure whether to release a snapshot or not, because things are still very much in flux, but I decided that the ability to build on .NET 4.0 was a big enough feature to at least warrant a development snapshot (albeit a source snapshot this time).

I had to make a number of changes to be able to build on .NET 4.0:

  • System.Type now overloads the == operator, so TypeBase.Equals had to be modified to use ReferenceEquals to avoid infinite recursion.
  • Apply the SecurityRules(Level1) attribute to IKVM.Runtime.dll to opt out of the new security model. This is needed because IKVM.Runtime.dll has the AllowPartiallyTrustedCallers and SecurityCritical attributes.
  • Modify ikvmc and the runtime to apply the SecurityRules(Level1) attribute to all generated assemblies to opt out of the new security model to work around a (likely) bug in .NET 4.0. Special thanks to Shawn Farkas for helping with this.
  • Modify JNI code to move RuntimeMethodHandle from unmanaged to managed data.
  • Changed IKVM.Reflection.Emit to write current runtime version into module header instead of a hardcoded "v2.0.50727".

Visual Studio 2010

Building with Visual Studio was never supported and it still isn't. You need to build with NAnt ("nant clean && nant"). You can use Visual Studio 2010 to edit the source and do test compiles (after first building with NAnt to make sure the required files are all there). If you want to use nant to build on .NET 4.0 you'll have to modify NAnt.exe.config to add the net-4.0 target framework and add a <supportedRuntime ... /> line to the <startup> section.

Changes

  • Many AWT / Swing related changes as Volker continues to merge in OpenJDK code.
  • Fixed java.io.File.list() to not throw a System.NotSupportedException for certain invalid paths.
  • Added workaround for .NET C# compiler bug that prevents it from subclassing a Java class that implements a protected abstract method using a public method. This workaround is required to build IKVM.AWT.WinForms.dll, but the Mono C# compiler has a related bug that I have not been able to work around. Filed Mono Bug #506757.
  • Added support for java.io.File.list() to VFS.
  • Added support for declarative security attributes on assemblies and types.
  • Added some sun.misc.Unsafe methods that appear to be used by JRuby.
  • Various fixes for .NET 4.0 beta 1.
  • Removed code that supports .NET 2.0 RTM (SP1 is now required).

The source-only snapshot is availabe here: ikvmsrc-0.41.3432.zip.

Monday, 25 May 2009 07:10:29 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Friday, 22 May 2009
.NET 4.0 -- AssemblyBuilderAccess.RunAndCollect

This week .NET 4.0 beta 1 was released. I've been playing around with it a little bit and was very excited to find a new Reflection.Emit feature. If you're a reflection nut like me, the title of this blog entry probably already gave it away. Dynamic assemblies can now be garbage collected!

A Little Demo

Running this simple example will show that a System.Type instance is created and then garbage collected.

using System;
using System.Reflection;
using System.Reflection.Emit;

class RunAndCollectDemo
{
  static void Main()
  {
    Type type = CreateType();
    object obj = Activator.CreateInstance(type);
    Console.WriteLine(obj);
    WeakReference weak = new WeakReference(type);
    type = null;
    obj = null;
    Console.WriteLine("type = " + weak.Target);
    GC.Collect();
    Console.WriteLine("type = " + weak.Target);
  }

  static Type CreateType()
  {
    AssemblyBuilder ab =
      AppDomain.CurrentDomain.DefineDynamicAssembly(
        new AssemblyName("foo"),
        AssemblyBuilderAccess.RunAndCollect);
    ModuleBuilder modb = ab.DefineDynamicModule("foo.dll");
    TypeBuilder tb = modb.DefineType("Foo");
    return tb.CreateType();
  }
}

As you can imagine, this required some pretty deep runtime changes. One of them is that RuntimeMethodHandle is no longer a handle. Previously it contained an unmanaged pointer to the native CLR datastructure corresponding to the method, but now it contains a managed interface reference, because the corresponding method can be garbage collected (I haven't looked into it, but I assume that the interface reference points either to a RuntimeMethodInfo or in the case of a method in a RunAndCollect assembly a proxy that contains a weak reference to the RuntimeMethodInfo. UPDATE: A RuntimeMethodHandle represents a strong reference.)

Somewhat ironically, this change breaks the compilation of ikvm's JniInterface.cs, because it stuffs a RuntimeMethodHandle in unmanaged memory. The fix is fairly straightforward (using a GCHandle to point to the MethodBase instead), but it does reveal an interesting property of C#, if you use unsafe code the (normally hidden) implementation details of managed value types can cause your code to break (and not because you explicitly depend on any internals).

What Does This Mean for IKVM.NET?

It would be really awesome if this feature could be used to finally make ClassLoaders garbage collectable, but unfortunately for that to happen this ancient bug first has to be fixed.

Friday, 22 May 2009 07:26:20 (W. Europe Daylight Time, UTC+02:00)  #    Comments [3]
# Wednesday, 22 April 2009
IKVM 0.40 Release Candidate 1

Thanks to Nat Luengnaruemitchai the MDB symbol writer has been fixed and finished and a couple of bugs were fixed.

Changes:

  • Changed version to 0.40.0.1.
  • Updated copyright notices in OpenJDK assemblies.
  • Fixed bug in workaround for #486307 that could cause "duplicate MemberRef" warnings when running PEVerify on an ikvmc generated assembly.
  • Fixed bug #2777128.
  • Fixed bug #2777171.

Binaries available here: ikvmbin-0.40.0.1.zip

Sources: ikvm-0.40.0.1.zip, classpath-0.95-stripped.zip, openjdk6-b12-stripped-IKVM-0.40.zip

Wednesday, 22 April 2009 07:03:48 (W. Europe Daylight Time, UTC+02:00)  #    Comments [1]
# Tuesday, 21 April 2009
New Development Snapshot

A couple of fixes and some more OpenJDK AWT/Swing merges.

Changes:

  • Fixed build regression introduced in previous snapshot caused by different build directory structure in OpenJDK 6 b16.
  • Handle Graphics2D.setPaint(null) correctly.
  • Integrated OpenJDK java/awt/image and java/awt/image/renderable packages.
  • Fixed duplicate MemberRefs generated by IKVM.Reflection.Emit caused by Mono workaround generic types not being canonicalized.
  • Integrated OpenJDK sun/swing and sun/awt packages.

Binaries available here: ikvmbin-0.41.3398.zip

Tuesday, 21 April 2009 08:03:55 (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]
# Thursday, 16 April 2009
New Development Snapshot

Even though 0.40 is still baking, I'm already working on the next version.

Changes:

  • Integrated OpenJDK 6 b16.
  • Changed decoded bytecode stream to use indexes in branches instead of PC offsets.
  • Fixed .MDB debug symbol generation (thanks to Nat Luengnaruemitchai).
  • Several AWT fixes contributed by Judit Vasko-Szedlar (Chemaxon).
  • Moved jsr/ret bytecode processing into a seperate (inlining) pass to reduce complexity in the verifier and compiler.
  • Fixed a bug in the compiler that caused exception handler to be dropped after an unreachable exception block was encountered.
  • Updated copyright notices.

Binaries available here: ikvmbin-0.41.3393.zip

OpenJDK 6 b16 sources + build artifacts: openjdk6-b16-stripped.zip

Thursday, 16 April 2009 12:26:38 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Monday, 06 April 2009
IKVM 0.40 Release Candidate 0

The eagerly anticipated (by some) first release candidate of 0.40 is ready!

Changes since previous snapshot:

  • Changed version to 0.40.0.0.
  • Added back RMI stubs that got left out in the module split.
  • Added cmm profiles and dummy color management implementation.
  • Fixed regression in ObjectInputStream.latestUserDefinedLoader().

Binaries available here: ikvmbin-0.40.0.0.zip

Sources: ikvm-0.40.0.0.zip, classpath-0.95-stripped.zip, openjdk6-b12-stripped-IKVM-0.40.zip

Monday, 06 April 2009 11:05:34 (W. Europe Daylight Time, UTC+02:00)  #    Comments [4]
# Friday, 27 March 2009
New Development Snapshot -- Help Needed

As 0.40 is getting nearer, I need some help. This snapshot is pretty close to what the first 0.40 release candidate is going to be. However, there is still something missing. As part of this release cycle I wrote a custom implementation of Reflection.Emit named IKVM.Reflection.Emit. This is the part that is responsible for writing managed PE binaries used by ikvmc. It is also responsible for writing the debugging files (.pdb on .NET and .mdb on Mono). I've written a working .pdb writer, but only a basic (and untested) implementation of the .mdb writer and this where I need help. I need someone to test, debug, finish this. So if you care about being able to debug your Java code in MonoDevelop, please help out! To get an idea of what's involved here is the current source.

Changes:

  • Workaround for Mono bug 486307.
  • Added "Windows 7" detection for os.name system property.
  • Improved IKVM.Reflection.Emit symbol writer "plug in" interface.
  • Wrote a basic (untested) symbol writer for Mono.

As always with a development snapshot, don't use this in production, but please do try it out and let me know about it. The sources are available in cvs and the binaries here: ikvmbin-0.39.3373.zip
 

Friday, 27 March 2009 06:25:54 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]
# Tuesday, 17 March 2009
New Development Snapshot

One major change: I rewrote exception handling to store exception state in java.lang.Throwable (for non-.NET exceptions) instead of using a weak map. I never got around to doing that earlier, because the overhead of exceptions on the CLR is such that the weak map didn't contribute significantly, but on Mono the situation was very different, as it turns out the weap map was very slow there and exception handling is much faster. Anyway, the result is that on Mono a particular microbenchmark went from 40 seconds to less than a second. On the CLR the performance is also a little bit better, but still horrible.

If you use exceptions for control flow, make sure to override fillInStackTrace (simply "return this") as this saves a lot of time in collecting the stack trace. On previous versions of IKVM this sometimes didn't help, but now it always does.

Changes:

  • Rewrote exception handling.
  • Moved PDB generating code (used by IKVM.Reflection.Emit.dll) into a separate assembly (IKVM.PdbWriter.dll) to work around missing ISymWrapper.dll on Mono.
  • Made java.lang.Object and java.lang.StackTraceElement serializable (in the .NET sense).
  • Added workaround for Mono bug (Type.IsGenericTypeDefinition throws for non-MonoType types).
  • Fixed ikvm.extensions.ExtensionMethods.printStackTrace() to unmap the exception after printing the stack trace.
  • Volker implemented a couple more AWT graphics functions.

As always with a development snapshot, don't use this in production, but please do try it out and let me know about it. The sources are available in cvs and the binaries here: ikvmbin-0.39.3363.zip
 

Tuesday, 17 March 2009 07:10:27 (W. Europe Standard Time, UTC+01:00)  #    Comments [3]
# Thursday, 12 March 2009
New Development Snapshot

Various fixes and improvements.

Changes:

  • Made Mono compilation workarounds conditional on __MonoCS__.
  • Fixed Class.desiredAssertionStatus() instrinsic to use the RemoveAsserts flag from the right class loader.
  • Fixed several generics related bugs in IKVM.Reflection.Emit.
  • Implemented ikvm.internal.ClassLiteral<T> to allow for more efficient class literals.
  • Switched java.awt.print package from GNU Classpath to OpenJDK.
  • Switched java.awt.geom package from GNU Classpath to OpenJDK.
  • Switched java.awt.color package from GNU Classpath to OpenJDK.
  • Switched java.awt.event package from GNU Classpath to OpenJDK.
  • Switched java.awt.datatransfer package from GNU Classpath to OpenJDK.
  • Switched java.awt.dnd package from GNU Classpath to OpenJDK.
  • Added hack to skip running the static initializer when computing the serialVersionUID while running ikvmstub.
  • Added support for defining delegates in Java.
  • Fixed regression introduced in 0.38 that caused LinkageError to be thrown instead of ClassCircularyError.
  • Updated mscorlib.jar (to get the new default constructor in MulticastDelegate).
  • Added utility class ikvm.runtime.Delegates to make it easier to create Runnables and PrivilegeActions from .NET languages.
  • Fixed JNI constructor invocation on dynamically loaded class.
  • Added hack to java.io.FileDescriptor to support JRuby's tty detection.
  • Implemented java.lang.ClassLoader$NativeLibrary.finalize(). Note that under normal circumstances this is never called, because IKVM doesn't support class unloading and by default finalization doesn't run on exit.
  • Fixed IKVM.Reflection.Emit bug that caused NullReferenceException in Save when using the ikvmc -target:module option.

The last IKVM.Reflection.Emit fix isn't in cvs yet, because SourceForge cvs is down.

As always with a development snapshot, don't use this in production, but please do try it out and let me know about it. The sources are available in cvs and the binaries here: ikvmbin-0.39.3358.zip

Thursday, 12 March 2009 06:41:37 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]