The recent architectural change makes it possible for the static compiler to use dynamic binding when it encounters a missing type. This in turn makes the dynamic binding support more important, so I've been fixing a lot of bugs in this area.

Currently, ikvmc will still emit code that statically throws a NoClassDefFoundError when accessing missing types, but in the future the default will change to generate dynamic binding code in this case.

Changes:

Fixed a bug with Graphics.setComposite(x). This fixes most paint artefacts of Nimbus L&F.

Fixed a NPE in constructor of BufferedImage.

Fixed TexturePaint bugs.

Enable ldc to work for unloadable types in dynamic mode.

Changed dynamic bytecode helper methods to be CallerID based instead of trusting the caller to provide the right context type handle.

Use sun.launcher.LauncherHelper to make the launcher more consistent with Java.

Bug fix. If the runtime tries to load a class from an assembly with a custom assembly class loader, the custom assembly class loader should be called. Previously it would only be called if the Java class loader had been instantiated.

Bug fix. We should emit miranda methods for non-abstract classes too.

Bug fix. Mark "missing" interface methods so that subclasses (that are compiled separately) know that the base class is incomplete.

note IKVMC0001: Found main method in class "Test"note IKVMC0002: Output file is "Test.exe"

C:\j>TestException in thread "main" java.lang.ExceptionInInitializerError at cli.System.Runtime.CompilerServices.RuntimeHelpers._RunClassConstructor(Unknown Source) at IKVM.Internal.TypeWrapper.RunClassInit(Unknown Source) at IKVM.NativeCode.java.lang.Class.forName0(Unknown Source) at java.lang.Class.forName(Class.java:287) at Test.main(Test.java:9)Caused by: cli.System.MethodAccessException: Test.m() at Other.<clinit>(Test.java)

The call from Other to Test fails with a MethodAccessException, because the dynamic assembly does not have access to the internal members of the Test.exe assembly.

Long ago I considered fixing this by adding an InternalsVisibleToAttribute to all statically compiled assemblies that allows the runtime generated dynamic assembly access to its internals, but I dismissed this because it would mean that untrusted code could abuse this same assembly identity to access the internals of any ikvmc compiled assemblies.

Forging a StrongNameKeyPair

Yesterday I realized that I could use InternalsVisibleToAttribute without opening a security hole. When the IKVM runtime is running as fully trusted code, it can forge a StrongNameKeyPair to attach to the dynamic assemblies it generates and thereby gain access to the statically compiled assemblies, without giving untrusted code the same access.

The only downside to this approach is that it relies on an implementation detail of the CLR (and Mono). I try to stay away from depending on implementation details, but in this case the gain far outways any risk.

Using It From C#

It also works for your C# assemblies. By adding a custom attribute, you can now enable dynamically loaded Java classes access to your assembly internals.

Here we create a Proxy for the non-public interface IFoo. This is allowed because of the InternalsVisibleToAttribute that gives access to an assembly named DemoApp-ikvm-runtime-injected with the specified public key. The name of the assembly must be the name of the current assembly (in this example "DemoApp") with "-ikvm-runtime-injected" appended. The public key is always the same. The corresponding private key does not exist.

Development Snapshot

Please note that this is a development snapshot, not a release or release candidate. So consider it untested.

Changes:

Use InternalsVisibleToAttribute to allow the runtime to dynamically inject classes into statically compiled assemblies.

The recent Windows RT jailbreak enabled me to run IKVM on my Surface RT. Out of curiousity I decided to run a part of the test suite. Only three tests failed, one because it relies on specific GC behavior and two because they use JVM.DLL. Previously, IKVM's JVM.DLL assemblies were created with ilasm, because that was the easiest way to create unmanaged exports at the time. However, ilasm doesn't seem to support ARM, so I added this support to IKVM.Reflection and wrote a simple tool to generate unmanaged export assemblies. Of course, supporting unmanaged exports on Windows RT in IKVM.Reflection is not very useful, but it was an interesting exercise to learn a little bit about the ARM instruction set.

I also did some more useful work. I finally got around to changing how ikvmc handles assembly references. It now works like the C# compiler and uses the IKVM.Reflection support for missing assemblies that I added for mcs. This means that it no longer automatically loads assemblies (apart from the IKVM runtime and class library assemblies) and you need to add explicit references for indirectly used assemblies as well. The downside is that this has the potential to break some builds, but the upside is that the compilation process is now more predictable and only loads the actually required assemblies, like a "real" compiler, instead of the System.Reflection heritage that required loading even unused dependencies.

Another change is that ikvmstub jars are now officially deprecated from being used with ikvmc (it will still work for a while, but a warning message is issued).

Finally, it is now illegal to explicitly reference a secondary assembly in a shared class loader group. Previously this caused a warning message, but now this is an error. You need to reference the primary assembly.

Changes:

Deduplicate ikvmc messages based on all parameter values, not just the first one.

Allow ikvmc messages to be suppressed based on any number of parameter values.

Fixed ikvmc to write fully specific -nowarn options to suppress file.

Fixed ikvmc to handle missing types.

Added ikvmc support for missing assemblies.

Officially deprecated using stubs with ikvmc.

Bug fix. Don't add duplicate methods to attribute annotation interfaces. The primary cause of this was attributes with virtual properties where we would add the base class property as well as the derived class overridden property.

The CLR's DEVPATH feature has always been broken and its brokeness varied over the years, but for my purposes it was a very useful feature. However with .NET .4.5 Microsoft decided to up the ante. If you have configured your .NET 4.5 runtime as a "developer installation", ngen will fail to generate native images for framework assemblies. This means that after Windows Update services mscorlib.dll, no native images will be used anymore.

Obviously you can work around this by editing the machine.config and re-running ngen, but that is a bit of a pain so decided to hack together a workaround.

The basic idea is to have a DLL that gets injected into (nearly) every process and patches mscoree.dll to make CreateConfigStream read developer-machine.config instead of machine.config if the DEVPATH environment variable is set.

The source and binaries are available here. Like the Microsoft DEVPATH code, this is untested so there is no warranty and use at your own risk. Bug reports are welcome of course.

There is no installer and there are no installation instructions, because frankly, if you don't know how to install it you probably shouldn't be doing so anyway..

The overhead is fairly low. If you don't have DEVPATH set the DLL will immediately unload again. The file size is only 3KB and in memory it will only use a single 4KB page.

Do the "no serialization" check before calling Serialization.AddAutomagicSerialization() to avoid triggering the class initializer (which will try to load SerializableAttribute, which is not available everywhere).

IKVM.Reflection: Made AssemblyBuilder.__AssemblyFlags a read/write property and marked __SetAssemblyFlags() obsolete.

IKVM.Reflection: Bug fix. Changed AssemblyName.ProcessorArchitecture to match (weird) .NET behavior. When reading the property it returns the architecture of the PE file, not the field from the AssemblyDef record.

IKVM.Reflection: Throw InvalidOperationException when MethodBuilder.DefineGenericParameters() is called a second time.

IKVM.Reflection: Added UniverseOptions.DontProvideAutomaticDefaultConstructor to disable the "helpful" creation of a default constructor.

IKVM.Reflection: Fixed ExportedType resolver to support types defined in another module in this assembly.

IKVM.Reflection: Added the Assembly.ModuleResolve event.

IKVM.Reflection: If missing member resolution is enabled, automatically create missing modules as needed.

IKVM.Reflection: When calling MethodBuilder.DefineParameter() multiple times for the same parameter, we would (sometimes) not store the ParameterBuilder and this would cause the resulting PE file to be corrupt. Now we store the duplicate Param records like SRE does.

IKVM.Reflection: Added UniverseOptions.MetadataOnly.

IKVM.Reflection: Made user string heap loading lazy.

IKVM.Reflection: Bug fix. If SizeOfOptionalHeader is greater than the number of bytes we read from the optional header, we should skip the additional bytes. If it is less, the image is invalid. Thanks to Jb Evain.

Removed org.omg.PortableInterceptor.UNKNOWN class, that is not part of [Open]JDK rt.jar.

Added ZipFile constructor that was added in Java 7.

Add support for running with headless awt toolkit.

Changed ikvmc to apply custom attribute annotations on annotation types to the corresponding custom attribute that is generated (and allow AttributeUsageAttribute to override the default AttributeUsageAttribute generated from the @Target annotation).

Added app.config files for executables to allow them to run on .NET 4.5 on Windows 8 without triggering the .NET 3.5 auto download.

Added (optional) support for building without System.Core.dll dependency.

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.

If a class with a finalizer and static initializer allocates instances of itself in the static initializer and the static initializer subsequently fails, the .NET runtime may abort the application when trying to finalize the objects.

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 together during a single compilation fully obeys the JLS binary compatibility rules.

Class Library

Most class library code is based on OpenJDK 7u6 build 24. Below is a list of divergences and IKVM.NET specific implementation notes.

Partial implementation. JPEGs can be read and written and there is limited metadata support.

javax.management

Limited implementation.

javax.print

There is a Win32 specific printing implementation. Not supported.

javax.script

ECMAScript implementation is not included.

javax.smartcardio

Not implemented.

javax.sound

Not implemented.

javax.swing

Not supported.

javax.tools

Not supported.

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

Not supported.

The entire public API is available, so "Not implemented." for javax.smartcardio, for example, means that the API is there but there is no back-end to provide the actual smartcard communication 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, but patches are welcome, of course.