# Monday, 31 December 2007
Argh! Part 2
using System;
using
System.Runtime.InteropServices;

class
Program
{
    GCHandle handle;

    Program()
    {
        handle = GCHandle.Alloc(this, GCHandleType.WeakTrackResurrection);
    }

    ~Program()
    {
        Console.WriteLine(handle.Target != null);
    }

    static void Main()
    {
        new Program();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}
 

Judging from the comments, many people had trouble understanding the previous code snippet. The above code is equivalent (except that this actually works on .NET 2.0). Maybe this will help clarify.

Monday, 31 December 2007 22:18:31 (W. Europe Standard Time, UTC+01:00)  #    Comments [1]
Argh!
using System;

class
Program
{
    WeakReference wr;

    Program()
    {
        wr = new WeakReference(this, true);
        GC.SuppressFinalize(wr);
    }

    ~Program()
    {
        Console.WriteLine(wr.IsAlive);
    }

    static void Main()
    {
        new Program();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}
 

What should the output of this program be? On .NET 1.1 it's True (as expected), but on .NET 2.0 it is False. On .NET 2.0 the GC has special support for finalizing WeakReferences, but it fails to take into account whether GC.SuppressFinalize() has been called (and this may be by design, but it's still broken).

This may look academic, but it is actually a real issue in the implementation of ReferenceQueues on IKVM. Previously I used GCHandle to keep a weak reference back to the reference object that was being monitored, but because that requires full trust, I changed the code to use WeakReference, but that broke it because of the above mentioned behavior. The work around is easy, but ugly and inefficient, simply keep these weak references in a global hashtable to make sure they are always strongly reachable.

.NET really needs a better mechanism for doing post-mortem cleanup (i.e. something like java.lang.ref.ReferenceQueue).

P.S. By my new policy, I won't be filing a bug with Microsoft since they have amply demonstrated not to care about external bug reports.

Monday, 31 December 2007 14:33:49 (W. Europe Standard Time, UTC+01:00)  #    Comments [6]
# Friday, 28 December 2007
Hello World

Five and a half years ago, Hello World ran for the first time... Now, for the first time a statically compiled HelloWorld.exe runs in partial trust (LocalIntranet zone in this case). Here's an exciting screenshot:

C:\sandbox>HelloWorld.exe
Hello World

C:\sandbox>

Obviously this doesn't mean things are done, but hopefully it won't take another five years ;-)

Friday, 28 December 2007 16:13:47 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]
# Monday, 10 December 2007
IKVM 0.36 Released

After what feels like an eternity, I've finally released IKVM 0.36. The binaries are identical to the ones in rc6.

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.
  • Volatile fields are not marked with modopt(volatile).
  • 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 cannot be distinguished from object arrays (isinstance and casting don't differentiate and ArrayStoreException isn't thrown).
  • Class loading is more eager than on the reference VM.
  • Object instances don't get created at new, but only at the invokespecial of the constructor. This causes observable differences with classes that have a finalizer when the constructor arg evaluation throws an exception [with the JSR-133 change in finalization, this is no longer a spec issue, but since the reference VM doesn't correctly implement JSR-133 it still is an implementation difference].
  • Interface implementation methods are never really final (interface can be reimplemented by .NET subclasses).
  • JSR-133 finalization spec change is not implemented. The JSR-133 changes dictate that an object should not be finalized unless the Object constructor has run successfully.

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 compability 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 build 13. 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.security Not supported.
java.text.Bidi GNU Classpath implementation. Not supported.
java.util.zip Partially based on GNU Classpath implementation.
javax.crypto IcedTea implementation. Not supported.
javax.imageio.plugins.jpeg Not implemented.
javax.management Not implemented.
javax.print Not implemented.
javax.script Not implemented.
javax.security Not supported.
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 Not implemented.
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.

In addition to the issues listed above, the extended charset providers are not included (the ones in charsets.jar in the JRE).

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.
  • 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.io.File.getCanonicalPath() is broken on Mono on Windows.
  • java.nio.channel.FileChannel.lock() shared locks are only supported on Windows NT derived operating systems.
  • java.lang.ref.SoftReference: Soft references are not guaranteed to be cleared before an OutOfMemoryError is thrown.
  • 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
Mono 1.2.5 x86* Windows
Mono 1.2.5 x86* Linux
.NET 1.1 x86 Windows
.NET 2.0 x86 Windows
.NET 2.0 x64 Windows
.NET 2.0 SP1 x86 Windows
.NET 2.0 SP1 x64 Windows

* Running on Mono has only been tested on x86, but Mono includes the native (platform specific) parts of IKVM, so any platform where Mono runs should in theory be able to run this IKVM release.

Partial Trust

Partial trust is not supported. The IKVM class library and runtime require Full Trust to be able to run.
 

Monday, 10 December 2007 08:50:56 (W. Europe Standard Time, UTC+01:00)  #    Comments [1]
# Friday, 07 December 2007
Mono Summit 2007

Last week I attended the Mono Summit 2007 in Madrid. It was a very productive three days and very much fun as well! I got to meet Zoltan, Wade, Paolo, Massi and Dick for the first time and enjoyed seeing Jb (of Cecil and monolinker fame), Rodrigo (of Boo fame) and Miguel again.

Jb created a small repro of the monolinker bug that caused it too fail on IKVM.OpenJDK.ClassLibrary.dll, so hopefully that'll be fixed soon. I also talked to him about using Cecil in ikvmc to free me from the shackles of Reflection.Emit. This is a major chunk of work, but I think it will be worth it.

Zoltan found some weirdness in my usage of DynamicMethod (the DynamicMethod signature for the constructor reflection helper had an extra unused argument, the CLR didn't mind because it matched up with the unused this reference, but Mono didn't like it). Unfortunately, fixing this didn't resolve the problems I had with DynamicMethod on Mono 1.2.5 and 1.2.6, so fast reflection will remain disabled while running on Mono for the time being (on Zoltan's current code base it did work, so when the Mono release after 1.2.6 is out I'll be able to enable it).

Rodrigo did a very interesting and entertaining presentation on Boo. I must admit that I haven't seriously played with Boo yet, but it looks extremely interesting. It is a statically typed language that also supports more dynamic typing (like Erik Meijer's "Static Typing Where Possible, Dynamic Typing When Needed"). It also supports some very cool meta programming capabilities. I definitely need to check this out and I recommend you do too.

Paolo and I had a tentative discussion about adding Java bytecode support to the Mono JIT, so that the expensive IKVM just-in-time bytecode to IL conversion can be skipped when running Java code in dynamic mode on Mono. This would be really nice to have, but there aren't any specific plans yet, so don't hold your breath yet.

Finally, I tried to convice Jim Purbrick (of SecondLife, who was there because he wants to use Mono to speed up LSL and possibly support other languages) that he really should support Java as well ;-)

Friday, 07 December 2007 07:42:20 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]
# Tuesday, 04 December 2007
IKVM 0.36 Release Candidate 6

This time I really expect this to be the final 0.36 release candidate :-)

The list of changes is relative to the 0.36 rc5, only the DynamicMethod constructor fix is new relative to the 0.37 development snapshot.

Changes:

  • Implemented Custom Assembly Class Loaders.
  • Fixed URL.equals() for "ikvmres:" urls.
  • Fixed findResources() bug that caused resources in core assembly to show up twice if a .NET assembly explicitly referenced the core assembly.
  • Added check to prevent defineClass() on an assembly class loader from defining a class that already exists in the assembly.
  • Fixed Class.getModifiers() to mask out ACC_SUPER bit.
  • Fixed a bug in the DynamicMethod based reflection implementation that caused a NullPointerException when generating the setter for a final instance field for code that isn't compiled with -strictfinalfieldsemantics.
  • Fix to make sure that a ghost interface method call always goes thru the target reference wrapping path. (Calling java.lang.CharSequence methods through an interface that extends java.lang.CharSequence would generate incorrect code.)
  • Fixed ghost interface array casting to generate verifiable code.
  • Fixed FileChannelImpl to close the FileDescriptor after releasing the file locks.
  • Fixed DynamicMethod based constructor reflection invocation to have method match the delegate signature.

Binaries available here:ikvmbin-0.36.0.5.zip.

Sources (+ binaries):ikvm-0.36.0.5.zip

External sources (haven't changed since rc1):classpath-0.95-stripped.zip,openjdk-b13-stripped.zip

Tuesday, 04 December 2007 08:55:12 (W. Europe Standard Time, UTC+01:00)  #    Comments [0]