Skip to content

Commit bead375

Browse files
[mono] Support public key token in DefineDynamicAssembly (#58283)
* Fixes #58015 Co-authored-by: Ulrich Weigand <ulrich.weigand@de.ibm.com>
1 parent f4e10ed commit bead375

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/libraries/System.Reflection.Emit/tests/AssemblyBuilderTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Globalization;
56
using System.Linq;
67
using System.Runtime.InteropServices;
78
using Xunit;
@@ -24,6 +25,21 @@ public class IntClassAttribute : Attribute
2425

2526
public class AssemblyTests
2627
{
28+
// The ECMA replacement key for the Microsoft implementation of the CLR.
29+
private static readonly byte[] TheKey =
30+
{
31+
0x00,0x24,0x00,0x00,0x04,0x80,0x00,0x00,0x94,0x00,0x00,0x00,0x06,0x02,0x00,0x00,
32+
0x00,0x24,0x00,0x00,0x52,0x53,0x41,0x31,0x00,0x04,0x00,0x00,0x01,0x00,0x01,0x00,
33+
0x07,0xd1,0xfa,0x57,0xc4,0xae,0xd9,0xf0,0xa3,0x2e,0x84,0xaa,0x0f,0xae,0xfd,0x0d,
34+
0xe9,0xe8,0xfd,0x6a,0xec,0x8f,0x87,0xfb,0x03,0x76,0x6c,0x83,0x4c,0x99,0x92,0x1e,
35+
0xb2,0x3b,0xe7,0x9a,0xd9,0xd5,0xdc,0xc1,0xdd,0x9a,0xd2,0x36,0x13,0x21,0x02,0x90,
36+
0x0b,0x72,0x3c,0xf9,0x80,0x95,0x7f,0xc4,0xe1,0x77,0x10,0x8f,0xc6,0x07,0x77,0x4f,
37+
0x29,0xe8,0x32,0x0e,0x92,0xea,0x05,0xec,0xe4,0xe8,0x21,0xc0,0xa5,0xef,0xe8,0xf1,
38+
0x64,0x5c,0x4c,0x0c,0x93,0xc1,0xab,0x99,0x28,0x5d,0x62,0x2c,0xaa,0x65,0x2c,0x1d,
39+
0xfa,0xd6,0x3d,0x74,0x5d,0x6f,0x2d,0xe5,0xf1,0x7e,0x5e,0xaf,0x0f,0xc4,0x96,0x3d,
40+
0x26,0x1c,0x8a,0x12,0x43,0x65,0x18,0x20,0x6d,0xc0,0x93,0x34,0x4d,0x5a,0xd2,0x93
41+
};
42+
2743
public static IEnumerable<object[]> DefineDynamicAssembly_TestData()
2844
{
2945
foreach (AssemblyBuilderAccess access in new AssemblyBuilderAccess[] { AssemblyBuilderAccess.Run, AssemblyBuilderAccess.RunAndCollect })
@@ -32,6 +48,11 @@ public static IEnumerable<object[]> DefineDynamicAssembly_TestData()
3248
yield return new object[] { new AssemblyName("testname") { Version = new Version(1, 2, 3, 4) }, access };
3349
yield return new object[] { new AssemblyName("class") { Version = new Version(0, 0, 0, 0) }, access };
3450
yield return new object[] { new AssemblyName("\uD800\uDC00") { Version = new Version(0, 0, 0, 0) }, access };
51+
52+
AssemblyName testPublicKey = new AssemblyName("TestPublicKey") { Version = new Version(0, 0, 0, 0) };
53+
testPublicKey.CultureInfo = CultureInfo.InvariantCulture;
54+
testPublicKey.SetPublicKey(TheKey);
55+
yield return new object[] { testPublicKey, access };
3556
}
3657
}
3758

src/mono/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.Mono.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ public sealed partial class AssemblyBuilder : Assembly
185185
private CustomAttributeBuilder[]? cattrs;
186186
private string? version;
187187
private string? culture;
188+
private byte[]? public_key_token;
188189
private Module[]? loaded_modules;
189190
private uint access;
190191
#endregion
@@ -221,6 +222,7 @@ private AssemblyBuilder(AssemblyName n, AssemblyBuilderAccess access)
221222
{
222223
version = v.ToString();
223224
}
225+
public_key_token = n.GetPublicKeyToken();
224226

225227
basic_init(this);
226228

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ typedef struct {
10681068
MonoArray *cattrs;
10691069
MonoString *version;
10701070
MonoString *culture;
1071+
MonoArray *public_key_token;
10711072
MonoArray *loaded_modules;
10721073
guint32 access;
10731074
} MonoReflectionAssemblyBuilder;

src/mono/mono/metadata/sre.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,13 @@ mono_reflection_dynimage_basic_init (MonoReflectionAssemblyBuilder *assemblyb, M
12631263
assembly->assembly.aname.revision = 0;
12641264
}
12651265

1266+
if (assemblyb->public_key_token) {
1267+
for (int i = 0; i < 8 && i < mono_array_length_internal (assemblyb->public_key_token); i++) {
1268+
guint8 byte = mono_array_get_internal (assemblyb->public_key_token, guint8, i);
1269+
sprintf ((char*)(assembly->assembly.aname.public_key_token + 2 * i), "%02x", byte);
1270+
}
1271+
}
1272+
12661273
/* SRE assemblies are loaded into the individual loading context, ie,
12671274
* they only fire AssemblyResolve events, they don't cause probing for
12681275
* referenced assemblies to happen. */

0 commit comments

Comments
 (0)