Skip to content

Commit 68d5e54

Browse files
committed
[mono] Implement AssemblyExtensions.TryGetRawMetadata.
1 parent 29013d8 commit 68d5e54

File tree

7 files changed

+34
-1
lines changed

7 files changed

+34
-1
lines changed

src/libraries/System.Runtime.Loader/tests/AssemblyExtensionsTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public unsafe class AssemblyExtensionsTest
1212
[Fact]
1313
public void TryGetRawMetadata()
1414
{
15-
bool supportsRawMetadata = PlatformDetection.IsNotMonoRuntime && PlatformDetection.IsNotNativeAot;
15+
bool supportsRawMetadata = PlatformDetection.IsNotNativeAot;
1616

1717
Assembly assembly = typeof(AssemblyExtensionsTest).Assembly;
1818
bool hasMetadata = assembly.TryGetRawMetadata(out byte* blob, out int length);

src/mono/System.Private.CoreLib/src/System/Reflection/Metadata/AssemblyExtensions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public static unsafe bool TryGetRawMetadata(this Assembly assembly, out byte* bl
1212
{
1313
ArgumentNullException.ThrowIfNull(assembly);
1414

15+
if (assembly is RuntimeAssembly runtimeAssembly)
16+
{
17+
return runtimeAssembly.TryGetRawMetadata(out blob, out length);
18+
}
19+
1520
blob = null;
1621
length = 0;
1722
return false;

src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs

+9
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@ internal static RuntimeAssembly InternalLoad(AssemblyName assemblyRef, ref Stack
474474
return res;
475475
}
476476

477+
internal unsafe bool TryGetRawMetadata(out byte* blob, out int length)
478+
{
479+
var this_assembly = this;
480+
return InternalTryGetRawMetadata(new QCallAssembly(ref this_assembly), out blob, out length);
481+
}
482+
477483
[MethodImplAttribute(MethodImplOptions.InternalCall)]
478484
private static extern bool GetManifestResourceInfoInternal(QCallAssembly assembly, string name, ManifestResourceInfo info);
479485

@@ -489,6 +495,9 @@ internal static RuntimeAssembly InternalLoad(AssemblyName assemblyRef, ref Stack
489495
[MethodImplAttribute(MethodImplOptions.InternalCall)]
490496
private static extern IntPtr InternalGetReferencedAssemblies(Assembly assembly);
491497

498+
[MethodImplAttribute(MethodImplOptions.InternalCall)]
499+
private static extern unsafe bool InternalTryGetRawMetadata(QCallAssembly assembly, out byte* blob, out int length);
500+
492501
internal string? GetSimpleName()
493502
{
494503
// TODO: Make this cheaper and faster

src/mono/mono/metadata/icall-def.h

+1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ HANDLES(RASSEM_5, "GetManifestResourceNames", ves_icall_System_Reflection_Runtim
364364
HANDLES(RASSEM_6, "GetModulesInternal", ves_icall_System_Reflection_RuntimeAssembly_GetModulesInternal, void, 2, (MonoQCallAssemblyHandle, MonoObjectHandleOnStack))
365365
HANDLES(RASSEM_6b, "GetTopLevelForwardedTypes", ves_icall_System_Reflection_RuntimeAssembly_GetTopLevelForwardedTypes, void, 2, (MonoQCallAssemblyHandle, MonoObjectHandleOnStack))
366366
HANDLES(RASSEM_7, "InternalGetReferencedAssemblies", ves_icall_System_Reflection_Assembly_InternalGetReferencedAssemblies, GPtrArray_ptr, 1, (MonoReflectionAssembly))
367+
HANDLES(RASSEM_8, "InternalTryGetRawMetadata", ves_icall_System_Reflection_RuntimeAssembly_InternalTryGetRawMetadata, MonoBoolean, 3, (MonoQCallAssemblyHandle, gpointer_ref, gint32_ref))
367368

368369
ICALL_TYPE(MCMETH, "System.Reflection.RuntimeConstructorInfo", MCMETH_1)
369370
HANDLES(MCMETH_1, "GetGenericMethodDefinition_impl", ves_icall_RuntimeMethodInfo_GetGenericMethodDefinition, MonoReflectionMethod, 1, (MonoReflectionMethod))

src/mono/mono/metadata/icall.c

+16
Original file line numberDiff line numberDiff line change
@@ -4743,6 +4743,22 @@ ves_icall_System_Reflection_Assembly_InternalGetReferencedAssemblies (MonoReflec
47434743
return result;
47444744
}
47454745

4746+
MonoBoolean
4747+
ves_icall_System_Reflection_RuntimeAssembly_InternalTryGetRawMetadata (MonoQCallAssemblyHandle assembly_h, gpointer_ref blob, gint32_ref length, MonoError *error)
4748+
{
4749+
MonoAssembly *assembly = assembly_h.assembly;
4750+
MonoImage *image = assembly->image;
4751+
4752+
if (image_is_dynamic (image)) {
4753+
return FALSE;
4754+
}
4755+
4756+
*blob = image->raw_metadata;
4757+
*((guint32*)length) = image->raw_metadata_len;
4758+
4759+
return TRUE;
4760+
}
4761+
47464762
/* move this in some file in mono/util/ */
47474763
static char *
47484764
g_concat_dir_and_file (const char *dir, const char *file)

src/mono/mono/metadata/image.c

+1
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
468468
if (offset + size > image->raw_data_len)
469469
return FALSE;
470470
image->raw_metadata = image->raw_data + offset;
471+
image->raw_metadata_len = size;
471472

472473
/* 24.2.1: Metadata root starts here */
473474
ptr = image->raw_metadata;

src/mono/mono/metadata/metadata-internals.h

+1
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ struct _MonoImage {
361361
MonoMemPool *mempool; /*protected by the image lock*/
362362

363363
char *raw_metadata;
364+
guint32 raw_metadata_len;
364365

365366
MonoStreamHeader heap_strings;
366367
MonoStreamHeader heap_us;

0 commit comments

Comments
 (0)