# Wednesday, 26 September 2012
IKVM.NET 0.46 Update 2 Release Candidate 0

The 0.46 version is the last version based on Java 6 and as mentioned previously would be supported longer than a typical release. Based on user feedback I decided to post an update that fixes a number of bugs that have been found since the previous release.

Changes (relative to 0.46 Update 1):

  • Updated version to 0.46.0.3.
  • Bug fix. java.lang.Package was not populated from manifest for ikvmc compiled assemblies.
  • Bug fix. When writing a direct ByteBuffer to a non-blocking socket and the write fails because there is no kernel buffer available, we should not advance the ByteBuffer position.
  • Bug fix. When adding certificates to virtual cacerts file make sure that the aliases are unique.
  • Bug fix. If a finally/fault handler contains reachable code before the handler's start index, the handler should branch to the handler start index.
  • Bug fix. After emitting a finally/fault handler block, we should emit the block leave stubs (even though you can't leave the block, they also emit the backward branch stubs).
  • Bug fix. If a Java class extends a remapped .NET type (cli.System.Object or cli.System.Exception), we should correctly report the base class.
  • Bug fix. If we encounter a jsr or ret instruction, we should throw a VerifyError (instead of NotImplementedException).
  • Bug fix. If an exception block ends with an astore, we need to propagate the local variable type after the astore to the exception handler.
  • Disable AppDomain.ProcessExit hook to run shutdown hooks when running on Mono to workaround https://bugzilla.xamarin.com/show_bug.cgi?id=5650
  • Bug fix. Custom attribute properties that don't have a public getter and setter should not be exposed as annotation properties.
  • Bug fix. Non-public property getter/setter methods should be ignored when we create properties to hide properties inherited from shadow types. This fixes a build break with .NET 4.5 beta which introduces a protected setter for Exception.HResult.
  • Bug fix. The $Method inner class for delegates should also be loadable for generic delegates. Thanks to Michael Bayne for reporting this.
  • Bug fix. When constructing a generic class loader we can't use GetWrapperFromType() on the type arguments, because they might refer to a subtype that is currently being loaded.
  • Replaced non-ascii character (micro) with ascii 'u' in Win32PrintService.java.
  • IKVM.Reflection: Bug fix. Resource Directory Entries must be sorted and names are case-insensitive.

Binaries available here: ikvmbin-0.46.0.3.zip

Sources: ikvmsrc-0.46.0.3.zip, openjdk6-b22-stripped.zip

Wednesday, 26 September 2012 08:16:30 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Monday, 17 September 2012
IKVM.NET 7.2 Release Candidate 1

The previous release candidate had a regression that caused custom attributes specified in the remap xml file not to be applied. This caused the build to fail on .NET 4.0.

Changes (relative to rc 0):

  • Updated version to 7.2.4630.1
  • Fixed build number in CommonAssemblyInfo.cs.in.
  • Fixed .NET 4.0 build issues.
  • Fixed map.xml custom attribute application regression.
  • Updated HOWTO.

Binaries available here: ikvmbin-7.2.4630.1.zip

Sources: ikvmsrc-7.2.4630.1.zip, openjdk-7u6-b24-stripped.zip

Monday, 17 September 2012 13:39:29 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Tuesday, 04 September 2012
IKVM.NET 7.2 Release Candidate 0

The first release candidate is available. Compared with the development snapshot some minor improvements to Object.getClass() and Class.getDeclaredField() instrinsics were made.

Changes (relative to IKVM.NET 7.1):

  • Integrated OpenJDK 7u6 b24.
  • Improved java.util.concurrent performance.
  • Removed org.omg.PortableInterceptor.UNKNOWN class, that is not part of [Open]JDK rt.jar.
  • Added ZipFile constructor that was added in Java 7.
  • 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.
  • Bug fixes.
  • Many IKVM.Reflection improvements.

Binaries available here: ikvmbin-7.2.4630.0.zip

Sources: ikvmsrc-7.2.4630.0.zip, openjdk-7u6-b24-stripped.zip

Tuesday, 04 September 2012 15:39:29 (W. Europe Daylight Time, UTC+02:00)  #    Comments [5]
# Friday, 31 August 2012
New Development Snapshot

I've integrated OpenJDK 7u6. The 7u7 security update is not included (as IKVM.NET is not suitable for running untrusted Java code anyway).

One divergence from 7u6 of note is that the new string hashing (used to protect against DoS attacks by intentionally causing hash collisions) has not been implemented. Instead the .NET String.GetHashCode() method is used for the alternative hash code. If DoS protection is required, .NET 4.5 should be used and randomized string hashing should be enabled.

Next stop the release candidate.

Changes:

  • Merged OpenJDK 7u6 b24.
  • Add support for running with headless awt toolkit. Fix for #3552089.
  • Fix a ClassCastException in printerJob.defaultPage() if the default paper format is not a standard paper format. This can occur with label printer.
  • Stop using ConstructorBuilder (always use MethodBuilder).
  • Remove usage of AssemblyName.ReferenceMatchesDefinition() because it is broken on .NET and not implemented on Mono.
  • 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).
  • Fixed InternalsVisibleToAttribute handling to take the public key into account as well.
  • Win32 print service fixes.
  • Optimized String.valueOf(char).
  • Add 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.
  • Fix Window.setIconImages(). Now all images are used. Before only the first image was used.
  • Disallow Unsafe.getUnsafe() from being called via reflection (for JDK compatibility).
  • Merged in security manager check to Font.createFont(int, File) from OpenJDK.
  • IKVM.Reflection: Bug fix. When enumerating virtual methods, we should only skip match actual base methods, not any method with the same signature.
  • IKVM.Reflection: Automatically add default constructor when necessary (using the same criteria as Ref.Emit).
  • IKVM.Reflection: Added API extension to wrap MethodInfo in a ConstructorInfo.

Binaries available here: ikvmbin-7.2.4626.zip

The stripped OpenJDK 7u6 b24 sources are available here: openjdk-7u6-b24-stripped.zip

Friday, 31 August 2012 13:43:50 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Tuesday, 24 July 2012
New Development Snapshot

I did some bytecode fuzzing and found and fixed a number of obscure bugs. Also fixed the bug reported here.

Changes:

  • Bug fix. If we encounter an invokedynamic in a method that has a jsr (which is always invalid because of class version rules), we need to throw a VerifyError.
  • Bug fix. If we encounter a jsr or ret instruction, we should throw a VerifyError (instead of NotImplementedException).
  • Bug fix. If a bytecode instruction refers to an invalid constant pool entry, MarkLinkRequiredConstantPoolItem should not throw an exception, but simply ignore the mark, the incorrect bytecode will be reported later during verification.
  • Bug fix. It is not valid to call TypeWrapper.IsInterfaceOrInterfaceArray on an UnloadableTypeWrapper.
  • Bug fix. There was a logic error in FindBaseMethods7() that caused LinkAndGetMethod() to be called if TryGetClassFileVersion() succeeded and the class version was greater than or equal to 51.
  • Bug fix. If a Java class extends a remapped .NET type (cli.System.Object or cli.System.Exception), we should correctly report the base class.
  • Bug fix. If a finally/fault handler contains reachable code before the handler's start index, the handler should branch to the handler start index.
  • Bug fix. After emitting a finally/fault handler block, we should emit the block leave stubs (even though you can't leave the block, they also emit the backward branch stubs).

Binaries available here: ikvmbin-7.2.4587.zip

Tuesday, 24 July 2012 08:12:29 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Wednesday, 18 July 2012
New Development Snapshot

I've been working on IKVM.Reflection to fix some loose ends and add support for various .NET 4.5 features. I also managed to squeeze in some bug fixes for IKVM.NET itself and some improvements to the (experimental and disabled by default) MSIL optimizer. Finally, I added intrinsics for some sun.misc.Unsafe operations to make some of the java.util.concurrent classes more efficient.

Changes:

  • Bug fix. The interface fields exposed in the __Fields nested type should have a public field type.
  • Interface fields can always be marked as initonly and don't need final access stubs.
  • Disable AppDomain.ProcessExit hook to run shutdown hooks when running on Mono to workaround https://bugzilla.xamarin.com/show_bug.cgi?id=5650
  • Changed NoClassDefFoundError resulting from type initialization failure to be consistent with OpenJDK behavior.
  • Many improvements to the (disabled) MSIL optimizer.
  • Added intrinsics for (some usages of) Unsafe methods: putObject, putOrderedObject, putObjectVolatile, getObjectVolatile, getObject, compareAndSwapObject.
  • Marked String.compareTo(Object) as bridge method.
  • Remove org.omg.PortableInterceptor.UNKNOWN class, that is not part of [Open]JDK rt.jar.
  • Add ZipFile constructor that was added in Java 7.
  • Deprecated the ikvmstub -serialver option and added a -japi option instead that also adds non-public interfaces and members. Also added a hack to fix the fact that String instance methods were all marked as deprecated.
  • Internal stub generator now reproduces exact class modifier bits.
  • Unused class modifier bits are now properly masked and returned.
  • Non-abstract interfaces now properly return ACC_ABSTRACT modifier.
  • IKVM.Reflection: Added .NET 4.5 ModuleBuilder APIs GetMethodToken(MethodInfo, IEnumerable) and GetConstructorToken(MethodInfo, IEnumerable).
  • IKVM.Reflection: Added API extensions ModuleBuilder.__GetMethodToken() and ModuleBuilder.__GetConstructorToken().
  • IKVM.Reflection: Bug fix. SignatureHelper.AddArguments() should accept a null reference for arguments parameter.
  • IKVM.Reflection: Bug fix. SignatureHelper.GetMethodSigHelper(Module, Type, Type[]) should accept null reference for parameterTypes argument.
  • IKVM.Reflection: Bug fix. SignatureHelper.AddArguments(Type[], Type[][], Type[][]) should accept null references for requiredCustomModifiers and optionalCustomModifiers.
  • IKVM.Reflection: Bug fix. SignatureHelper.AddSentinel() should not affect the calling convention.
  • IKVM.Reflection: Simplified SignatureHelper implementation to make it more compatible with .NET and more efficient.
  • IKVM.Reflection: Added .NET 4.5 AssemblyName.ContentType property and ContentType=WindowsRuntime in AssemblyName.
  • IKVM.Reflection: Bug fix. ModuleBuilder.ResolveMethod() should return ConstructorInfo for constructors.
  • IKVM.Reflection: Made MethodSpec handling more consistent with MemberRef handling. This avoids the need for the caching the MethodInfo to token mappings (which leaks memory, because generic method instances are not canonicalized).
  • IKVM.Reflection: Removed unnecessary (and memory leaking) member ref caching level.
  • IKVM.Reflection: Explicitly disallow modifying a method signature after it has been used.
  • IKVM.Reflection: Added experimental support for generating Windows Runtime (.winmd) assemblies.
  • IKVM.Reflection: Added missing DefineResource() APIs to ModuleBuilder and AssemblyBuilder.
  • IKVM.Reflection: Fixed regression. Type.GetConstructor() should also return the class constructor. This also fixes Type.TypeInitializer.
  • IKVM.Reflection: Updated Assembly.CreateQualifiedName() to be compatible with current .NET versions.
  • IKVM.Reflection: Added new .NET 4.5 API Type.IsConstructedGenericType.
  • IKVM.Reflection: Added co-/contra-variance support to Type.IsAssignableFrom().
  • IKVM.Reflection: Added support for using the .NETCore v4.5 aka Metro profile mscorlib.dll.
  • IKVM.Reflection: Ignore HasFieldMarshal attribute and always return the pseudo custom attribute if a FieldMarshal record exists. This is similar to .NET reflection.
  • IKVM.Reflection: If a FieldLayout record exists, always return the pseudo custom attribute FieldOffset, regardless of the declaring type's layout.
  • IKVM.Reflection: Added new API [Field|Parameter]Info.__TryGetFieldMarshal().
  • IKVM.Reflection: Added new API FieldInfo.__TryGetFieldOffset().
  • IKVM.Reflection: Keep track of whether a ModuleBuilder has been saved (and only allow it to be saved once).
  • IKVM.Reflection: Fixed various builders to throw NotImplementedException (like .NET) when getting custom attributes before the type has been baked.
  • IKVM.Reflection: Fixed various builders to properly return custom attributes after the module has been saved.
  • IKVM.Reflection: Bug fix. FieldBuilder.GetRawConstantValue() didn't work after saving the module.
  • IKVM.Reflection: Bug fix. MethodInfo should return PreserveSigAttribute pseudo custom attribute if MethodImplAttributes.PreserveSig is set.
  • IKVM.Reflection: Added new API MethodInfo.__TryGetImplMap().
  • IKVM.Reflection: Changed DllImportAttribute synthesis to always include CharSet, BestFitMapping and ThrowOnUnmappableChar to be compatible with .NET. Now that we have __TryGetImplMap() we don't need to distinguish anymore between not set and set to the default value.
  • IKVM.Reflection: Added .NET 4.5 API CustomAttributeData.AttributeType property.
  • IKVM.Reflection: Made MarshalAsAttribute synthesis more compatible with .NET by always setting ArraySubType, SizeParamIndex, SizeConst and SafeArraySubType properties.
  • IKVM.Reflection: Made CustomAttributeData.ToString() output more compatible with .NET (by "casting" enum values, except zero).
  • IKVM.Reflection: Fixed MarshalAsAttribute values. Use enum underlying values, instead of the enum values.
  • IKVM.Reflection: Added UniverseOptions.DisablePseudoCustomAttributeRetrieval to disable returning pseudo custom attributes.

Binaries available here: ikvmbin-7.2.4582.zip

Wednesday, 18 July 2012 08:35:08 (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]
# Monday, 16 July 2012
IKVM.NET 7.1 Released

I've released IKVM.NET 7.1 to SourceForge. The binaries are identical to the ones in release candidate 2.

Release Notes

This document lists the improvements, known issues and incompatibilities.

What's New (relative to IKVM.NET 7.0):

  • Integrated OpenJDK 7 u4 b22.
  • Dropped experimental partial trust support.
  • Much improved ikvmc error and warning handling.
  • Added ikvmc options: -win32manifest, -filealign, -highentropyva.
  • Removed ikvmc -platform:Itanium option.
  • Added ikvm.lang.DllExport annotation to export static methods as unmanaged entry points.
  • Added ikvm.runtime.Util.getClassFromTypeHandle() overload for classes that represent arrays of remapped .NET types and .NET primitives.
  • Added extension methods for (almost) all instance methods in Object, String and Throwable to ikvm.extensions.ExtensionMethods.
  • Added support for delegates with ByRef parameters.
  • Added support to ikvmc to automatically set the full source path in the debugging info if the source file lives next to the .class file.
  • When adding certificates to virtual cacerts file make sure that the aliases that are generated from the certificate subject are unique.
  • Many (minor) bug fixes.
  • Many IKVM.Reflection fixes and improvements.

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.
  • Strict class-file checking is not 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 7u4 build 22. Below is a list of divergences and IKVM.NET specific implementation notes.

com.sun.security.auth.module        Not supported.
java.applet Not implemented.
java.awt Partial System.Windows.Forms based back-end. Not supported.
java.io.Console Not implemented.
java.lang.instrument Not implemented.
java.lang.management Limited implementation.
java.net SCTP and SDP not implemented.
java.net.ProxySelector Getting the default system proxy for a URL is not implemented.
java.nio.file Most optional features (e.g. ACLs) are not implemented.
java.text.Bidi Not supported.
java.util.zip Partially based on GNU Classpath implementation.
javax.crypto ECC is not implemented.
javax.imageio.plugins.jpeg 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.

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()
  • java.util.zip.ZipFile(String, CharSet) constructor added in Java 7 is missing.
  • The class org.omg.PortableInterceptor.UNKNOWN is included, but is not part of Java 7.

Supported Platforms

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

CLI Implementation       Architecture      Operating System
.NET 2.0 SP2 x86 Windows 7
.NET 2.0 SP2 x64 Windows 7
.NET 4.0 x86 Windows 7
.NET 4.0 x64 Windows 7
Mono 2.10.5 x86 Windows 7
Mono 2.10.5 x64 Ubuntu 11.10


Partial Trust

Experimental support for running in partial trust has been withdrawn. Due to the deprecated nature of ASP.NET partial trust and the fact that supporting partial trust in .NET 4.0 will be impossible to combine with serialization interop, I've decided to stop supporting partial trust scenarios.

Monday, 16 July 2012 08:40:01 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Friday, 29 June 2012
IKVM.NET 7.1 Release Candidate 2

I found and fixed a code generation bug, so a new release candidate. The bug has been around since the very early days, but a change in 2010 made it more likely to surface. It showed up while debugging the Derby problem reported by Dash in the comments here.

Changes (relative to rc 1):

  • Updated version to 7.1.4532.2
  • Fixed a code analysis bug that under specific circumstances caused incorrect local variable types or null reference loads from local variables.

When the final release is done, it will include the full release notes.

Binaries available here: ikvmbin-7.1.4532.2.zip

Sources: ikvmsrc-7.1.4532.2.zip, openjdk-7u4-stripped.zip

Friday, 29 June 2012 11:21:38 (W. Europe Daylight Time, UTC+02:00)  #    Comments [1]
# Tuesday, 26 June 2012
Experimental WinRT Support in IKVM.Reflection

I've added support for generating Windows Runtime assemblies with IKVM.Reflection. This is still experimental because it is mostly based on reverse engineering as there currently is virtually no documentation on this.

The code for generating a trivial component is available here. It doesn't require .NET 4.5 or any Windows Runtime metadata assemblies.

The IKVM.Reflection code is available in cvs.

BTW, I did this mostly because I was curious about what's involved. Don't read anything into this wrt IKVM.NET itself. It's unlikely to ever support running in WinRT.

Tuesday, 26 June 2012 18:05:11 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Monday, 25 June 2012
Why ASP.NET Medium Trust Isn't

On October 24 of last year I reported an ASP.NET Medium Trust vulnerability. This eventually resulted in KB 2698981 where Microsoft essentially deprecated ASP.NET Partial Trust.

The problem I reported was that it is possible to abuse Thread.Abort() to create an inconsistent TypedReference that violates type safety.

TypedReference is an interesting type and I've been on the lookout for a way to abuse it for a long time. It's purpose is to allow type safe references to be used in a generic way. To implement this a TypedReference contains both a pointer and a type and all operations it allows make sure that type safety isn't violated. It's a primitive type, so the runtime knows about it and treats it specially. It can be used from partially trusted code and because it can contain a reference to a location on the stack, the runtime enforces that TypedReference values can only be used from a single thread (by disallowing boxing or storing it in arrays or fields).

However, by having one thread repeatedly overwriting a TypedReference location on the stack with two different values and a second thread aborting the first thread at the right moment, you can end up with a TypedReference that combines the pointer from one value and the type from another value and thus violating type safety.

The source of the PoC is available here.

Monday, 25 June 2012 10:26:33 (W. Europe Daylight Time, UTC+02:00)  #    Comments [3]