[metadata] Size 0 Blob heap is ok when resolving assembly refs (mono/mono#18313)
authorAleksey Kliger (λgeek) <alklig@microsoft.com>
Fri, 3 Jan 2020 15:42:44 +0000 (10:42 -0500)
committerGitHub <noreply@github.com>
Fri, 3 Jan 2020 15:42:44 +0000 (10:42 -0500)
commitdaa29dfa943ae93428ba366a6d8521e5a89de31b
treeb97f73abd44c14b8cfe5808dbf37ffeec00cbfe9
parent8aa59a820f5ad76659ee1312a9d811a5129c9a5c
[metadata] Size 0 Blob heap is ok when resolving assembly refs (mono/mono#18313)

* [metadata] Size 0 Blob heap is ok when resolving assembly refs

Sometimes ILasm can produce images with a Blob heap of size 0.  In cases where
we're loading assembly references from such an image, the hash (which is
optional) can be at index = 0 and the Blob heap size is also 0.

Also: add the hash value to the output of `monodis --assemblyref`

* [metadata] Add a separate assertion for the index == size == 0 case

And a comment explaining how it is likely to be triggered.  If the assembly is
reasonable the caller of mono_metadata_blob_heap should be updated to use
mono_metadata_blob_heap_null_ok instead

* [bcl] Allow Mono's ILASM to produce a size 0 Blob heap

ECMA 335 II.24.2.4 says that the user string and blob heaps should have an
entry at index 0 consisting of the single byte 0.  However .NET Framework (and
.NET Core) ilasm will entirely omit the Blob heap (that is, create a blob heap
entry of size 0) if it is not needed.  This PR changes Mono's ILASM to emit the
initial byte on demand only if one of the MetaDataStream.Add() methods is
called. Otherwise we will also emit a stream of size 0.

This is needed to compile some test cases.

* [tests] Add regression test for loading assemblies with size 0 Blob heap

Depends on ILASM that can emit a size 0 Blob heap

Commit migrated from https://github.com/mono/mono/commit/07fee8a91dbc172bacded9d2c7f7d158eb7c50f1
src/mono/mono/dis/dump.c
src/mono/mono/metadata/assembly.c
src/mono/mono/metadata/metadata-internals.h
src/mono/mono/metadata/metadata.c
src/mono/mono/tests/Makefile.am
src/mono/mono/tests/null-blob-main.cs [new file with mode: 0644]
src/mono/mono/tests/null-blob-null-blob-assm.il [new file with mode: 0644]
src/mono/mono/tests/null-blob-ref.il [new file with mode: 0644]
src/mono/mono/tests/null-blob-tgt.cs [new file with mode: 0644]