Skip to content

Commit

Permalink
[class-parse] Ignore .jnilib files within .jar files (dotnet#890)
Browse files Browse the repository at this point in the history
Fixes: dotnet#860

Context: https://bugs.openjdk.java.net/browse/JDK-8127215

A long time ago (2002-ish?), the custom Java distributed on Mac OS X
required that native libraries containing JNI code have a `.jnilib`
file extension, apparently as part of [Java Web Start][0]:

> Java Web Start: Mac OS X Details:
> …
>   * Native libraries must be in JARs and end in .jnilib

This was supported up through Apple Java 6.

Some types of support for the `.jnilib` extension was
[removed in OpenJDK 7][1], but `.jar` files containing `.jnilib`
entries can still be found, e.g. in the [`jna-4.5.1.jar` file][2]:

![image](https://user-images.githubusercontent.com/179295/136593203-c8a017b6-f113-4ee2-8b44-e5ffa4eaa34b.png)

Additionally, `.jnilib` files contain a `0xCAFEBABE` file header, as
found in `.class` files, but it's not a Java `.class` file: it's
otherwise a Mac OS X `.dylib` file:

	% hexdump -C com/sun/jna/darwin/libjnidispatch.jnilib  | head -1
	00000000  ca fe ba be 00 00 00 02  00 00 00 07 00 00 00 03  |................|

	% objdump -h com/sun/jna/darwin/libjnidispatch.jnilib  
	Sections:
	Idx Name             Size     VMA              Type
	  0 __text           0000d3de 0000000000000dc0 TEXT
	  1 __stubs          000000de 000000000000e19e TEXT
	…

Trying to parse a `.jar` file containing such a `.jnilib` entry
results in an `ArgumentOutOfRangeException`:

	System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
	   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
	   at System.Collections.Generic.List`1.get_Item(Int32 index)
	   at Xamarin.Android.Tools.Bytecode.AttributeInfo.CreateFromStream(ConstantPool constantPool, Stream stream)

Fix this error by ignoring `.jnilib` files found within a `.jar`.

[0]: https://ia903107.us.archive.org/16/items/Wwdc2002DvdSet/WWDC_2002/PDFs/400/403.pdf
[1]: https://bugs.openjdk.java.net/browse/JDK-8127215
[2]: http://www.java2s.com/example/jar/j/download-jna451jar-file.html
  • Loading branch information
jpobst authored Oct 11, 2021
1 parent bda6be4 commit 403dd37
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/Xamarin.Android.Tools.Bytecode/ClassPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void Load (Stream jarStream, bool leaveOpen = false)
if (entry.Length == 0)
continue;
using (var s = entry.Open ()) {
if (!ClassFile.IsClassFile (s))
if (!ClassFile.IsClassFile (s) || entry.Name.EndsWith (".jnilib", StringComparison.OrdinalIgnoreCase))
continue;
}
using (var s = entry.Open ()) {
Expand Down

0 comments on commit 403dd37

Please sign in to comment.