# Friday, 26 June 2009
IKVM.Reflection.Emit Update

I have done a massive amount of fixes to IKVM.Reflection.Emit to make it full featured (even though it still doesn't implement all Reflection.Emit APIs, the functionality should (almost) all be there, for example via different overloads).

I completed support for generics (I think) and fixed many bugs in that area, ikvmc only uses a very small amount of generics so these fixes are unlikely to affect it.

It's worth explicitly stating the design goals of IKVM.Reflection.Emit:

  • It's a write-only API. Some GetXXX methods or properties may be implemented, but that's mostly for its internal convenience.
  • There is intentionally no error checking. During ikvm development the error checking in System.Reflection.Emit has cost me a huge amount of time, it is generally much easier to diagnose the problem when you have a broken assembly file. PEverify and ILDASM are your friends.
  • Code that uses System.Reflection.Emit in a write-only way is supposed to "just work" (modulo missing APIs, but those changes should be trivial).

I've done some pretty heavy duty testing on it. It should be ready for external (i.e. non-ikvmc) usage now. If you decide to use it (or consider using it), please let me know. As always, feedback is appreciated.

Changes:

  • Added support for ByRef and Pointer types.
  • Completed support for all literal field constant types and fixed null literal fields.
  • Added ModuleBuilder.DefineInitializedData().
  • Fixed many generics related bugs.
  • Added a (non-standard) API to ModuleBuilder to set the PE image base address.
  • Added TypeBuilder.SetParent().
  • Added TypeBuilder.GetMethod() and TypeBuilder.GetConstructor() to instantiate methods on generic types.
  • Added a (non-standard) API to ILGenerator to disable the "helpful" automatic leave/endfinally instructions in exception blocks.
  • Added support for pinned local variables.
  • Added UIntPtr and TypedReference signature encodings.
  • Fixed handling of TypeBuilder enums in custom attributes.
  • Added MethodBuilder.SetSignature().
  • Added GenericTypeParameterBuilder.SetInterfaceConstraints() and .SetGenericParameterAttributes().
  • Fixed (Method|Type)Builder.SetCustomAttribute() to set HasSecurity flag when SuppressUnmanagedCodeSecurityAttribute is set.
  • Added support for defining events.

Binary available here: ikvm-refemit-0.41.3464.zip

Friday, 26 June 2009 06:42:52 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Monday, 15 June 2009
IKVM 0.40 Update 1 Release Candidate 0

A minor update.

Changes:

  • Changed version to 0.40.0.2
  • Changed build to generate rmi stubs instead of depend on .class files in openjdk6-b12-stripped-IKVM-0.40.zip. Thanks to Jo Shields for helping with this.
  • Fixed verifier bugs. Thanks to Brian Heineman for reporting this.

Binaries available here: ikvmbin-0.40.0.2.zip.

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

Monday, 15 June 2009 07:40:05 (W. Europe Daylight Time, UTC+02:00)  #    Comments [4]
# Friday, 05 June 2009
IKVM 0.40 Released

I've released IKVM 0.40 to SourceForge. The binaries are identical to the ones in release candidate 1.

Release Notes

This document lists the known issues and incompatibilities.

Runtime

  • Code unloading (aka class GC) is not supported.
  • In Java static initializers can deadlock, on .NET some threads can see uninitialized state in cases where deadlock would occur on the JVM.
  • JNI
    • Only supported in the default AppDomain.
    • Only the JNICALL calling convention is supported! (On Windows, HotSpot appears to also support the cdecl calling convention).
    • Cannot call string contructors on already existing string instances
    • A few limitations in Invocation API support
      • The Invocation API is only supported when running on .NET.
      • JNI_CreateJavaVM: init options "-verbose[:class|:gc|:jni]", "vfprintf", "exit" and "abort" are not implemented. The JDK 1.1 version of JavaVMInitArgs isn't supported.
      • JNI_GetDefaultJavaVMInitArgs not implemented
      • JNI_GetCreatedJavaVMs only returns the JavaVM if the VM was started through JNI or a JNI call that retrieves the JavaVM has already occurred.
      • DestroyJVM is only partially implemented (it waits until there are no more non-daemon Java threads and then returns JNI_ERR).
      • DetachCurrentThread doesn't release monitors held by the thread.
    • Native libraries are never unloaded (because code unloading is not supported).
  • The JVM allows any reference type to be passed where an interface reference is expected (and to store any reference type in an interface reference type field), on IKVM this results in an IncompatibleClassChangeError.
  • monitorenter / monitorexit cannot be used on unitialized this reference.
  • Floating point is not fully spec compliant.
  • A method returning a boolean that returns an integer other than 0 or 1 behaves differently (this also applies to byte/char/short and for method parameters).
  • Synchronized blocks are not async exception safe.
  • Ghost arrays don't throw ArrayStoreException when you store an object that doesn't implement the ghost interface.
  • Class loading is more eager than on the reference VM.
  • Interface implementation methods are never really final (interface can be reimplemented by .NET subclasses).
  • JSR-133 finalization spec change is not fully implemented. The JSR-133 changes dictate that an object should not be finalized unless the Object constructor has run successfully, but this isn't implemented.

Static Compiler (ikvmc)

  • Some subtle differences with ikvmc compiled code for public members inherited from non-public base classes (so called "access stubs"). Because the access stub lives in a derived class, when accessing a member in a base class, the derived cctor will be run whereas java (and ikvm) only runs the base cctor.
  • Try blocks around base class ctor invocation result in unverifiable code (no known compilers produce this type of code).
  • Try/catch blocks before base class ctor invocation result in unverifiable code (this actually happens with the Eclipse compiler when you pass a class literal to the base class ctor and compile with -target 1.4).
  • Only code compiled in a single assembly fully obeys the JLS binary compatibility rules.
  • An assembly can only contain one resource with a particular name.
  • Passing incorrect command line options to ikvmc may result in an exception rather than a proper error messages.

Class Library

Most class library code is based on OpenJDK 6 build 12. Below is a list of divergences and IKVM specific implementation notes.

com.sun.security.auth.module        Not implemented.
java.applet GNU Classpath implementation. Not implemented.
java.awt GNU Classpath implementation with partial System.Windows.Forms based back-end. Not supported.
java.io.Console Not implemented.
java.lang.instrument Not implemented.
java.lang.management Not implemented.
java.net No IPv6 support implemented.
java.net.ProxySelector Getting the default system proxy for a URL is not implemented.
java.text.Bidi GNU Classpath implementation. Not supported.
java.util.zip Partially based on GNU Classpath implementation.
javax.imageio.plugins.jpeg Partial implementation. JPEGs can be read and written, but there is no metadata support.
javax.management Not implemented.
javax.print Not implemented.
javax.script Not implemented.
javax.smartcardio Not implemented.
javax.sound Not implemented.
javax.swing GNU Classpath implementation. Not supported.
javax.tools Not implemented.
org.ietfs.jgss Not implemented.
sun.jdbc.odbc Implementation based on .NET ODBC managed provider.
sun.net.www.content.audio Audio content handlers not implemented.
sun.net.www.content.image Image content handlers not implemented.

The entire public API is available, so "Not implemented." for javax.print, for example, means that the API is there but there is no back-end to provide the actual printing support. "Not supported." means that the code is there and probably works at least somewhat, but that I'm less likely to fix bugs reported in these areas.

Specific API notes:

  • java.lang.Thread.stop(Throwable t) doesn't support throwing arbitrary exceptions on other threads (only java.lang.ThreadDeath).
  • java.lang.Thread.holdsLock(Object o) causes a spurious notify on the object (this is allowed by the J2SE 5.0 spec).
  • java.lang.String.intern() strings are never garbage collected.
  • Weak/soft references and reference queues are inefficient and do not fully implement the required semantics.
  • java.lang.ref.SoftReference: Soft references are not guaranteed to be cleared before an OutOfMemoryError is thrown.
  • Threads started outside of Java aren't "visible" (e.g. in ThreadGroup.enumerate()) until they first call Thread.currentThread().
  • java.lang.Thread.getState() returns WAITING or TIMED_WAITING instead of BLOCKING when we're inside Object.wait() and blocking to re-acquire the monitor.
  • java.nio.channel.FileChannel.lock() shared locks are only supported on Windows NT derived operating systems.
  • java.lang.SecurityManager: Deprecated methods not implemented: classDepth(String), inClass(String), classLoaderDepth(), currentLoadedClass(), currentClassLoader(), inClassLoader()

Supported Platforms

This release has been tested on the following CLI implementations / platforms:

CLI Implementation       Architecture      Operating System
.NET 2.0 SP2 x86 Windows
.NET 2.0 SP2 x64 Windows


Partial Trust

There is experimental support for running in partial trust.
 

Friday, 05 June 2009 15:32:45 (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]
# Tuesday, 02 June 2009
New Development Snapshot

Class gc support has now been checked in (but is not available in the attached binaries, because they've been built for .NET 2.0). This is somewhat of a milestone snapshot, because it is the first version that no longer requires GNU Classpath to build*. However, please note that AWT / Swing still needs a lot of work.

*What this means is that there is no longer an external dependency on GNU Classpath. There is still GNU Classpath derived code in ikvm's source tree. For example, small parts of AWT and the pure Java  implementation of java.util.zip.

Changes:

  • Removed dependency on classpath-0.95-stripped.zip. All AWT / Swing code is now from OpenJDK or in the ikvm codebase.
  • Defined NET_4_0 when building on .NET 4.0 to enable conditional code.
  • Changed IKVM.Reflection.Emit's ModuleBuilder and AssemblyBuilder to extends Module and Assembly respectively when building on .NET 4.0.
  • Added .NET 4.0 fix to IKVM.Reflection.Emit (for the fact that Type now defines == operator).
  • Fixed locking for image based Graphics.
  • Various changes to remove warnings when building on .NET 4.0.
  • Several fixes related to dynamic assemblies.
  • Several improvements to better work in partial trust.
  • More AWT work.
  • Made java.lang.reflect.Field exception messages the same as JDK 6.
  • Implemented class gc (for .NET 4.0 builds).
  • Added -Xnoclassgc option to ikvm (only meaningful in a .NET 4.0 build).

Binaries available here: ikvmbin-0.41.3440.zip.

Tuesday, 02 June 2009 10:49:01 (W. Europe Daylight Time, UTC+02:00)  #    Comments [4]
# Friday, 29 May 2009
.NET 4.0 -- System.Runtime.CompilerServices.ConditionalWeakTable

Back in the PDC build of .NET 4.0 another interesting new feature was introduced: ConditionalWeakTable<TKey, TValue>. This is a special purpose dictionary to associate objects with other objects. It was introduced to help the DLR, which needs the ability to add properties to arbitrary objects. The documentation is pretty clear and the CLR team blog also has some info on it, so I won't rehash that. Instead I'll just mention that ConditionalWeakTable itself is not a magic type (i.e. the runtime knows nothing about it), but instead it is built on top of a private value type System.Runtime.Compiler.Services.DependentHandle. DependentHandle is essentially a handle based ephemeron implementation.

JVM vs CLR

This means that the CLR now comes a bit closer to the JVM in terms of memory management features. The JVM has had some very interesting reference types for a long time (WeakReference, SoftReference and PhantomReference) and the ability to have these references posted to a ReferenceQueue by the GC when the relevant change in reachability to the referenced object occurs.

Unfortunately there still isn't parity between the CLR and JVM, even though the CLR now provides a capability the JVM doesn't.

Java .NET Notes
ReferenceQueue n/a  
WeakReference WeakReference (short) .NET has no ReferenceQueue equivalent notification mechanism.
n/a WeakReference (long)    
SoftReference n/a  
PhantomReference     n/a  
WeakHashMap n/a  
n/a ConditionalWeakTable  

(If you think that Java's WeakHashMap and .NET's ConditionalWeakTable are similar, consider that ConditionalWeakTable is ephemeron based. Plus the fact that WeakHashMap uses short weak references and ConditionalWeakTable uses long weak references.)

IKVM.NET

ConditionalWeakTable is very useful for IKVM in several places:

  • Used to support class unloading by mapping Assembly to ClassLoader.
  • Used in caching of MethodBase properties.
  • Can be used to track the type of ghost arrays.
  • Can be used to more efficiently implement adding references to a ReferenceQueue.

Java

Given the effort going into Java 7 to improve support for dynamic languages it would not be surprising nor unwelcome to see ephemerons being added to the JVM.

Friday, 29 May 2009 08:08:54 (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]
# 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]