Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 azure-pipelines/build_nonWindows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ steps:
arguments: --no-build -c $(BuildConfiguration) -f netcoreapp2.1 -v n --settings "$(Build.Repository.LocalPath)/azure-pipelines/$(Agent.OS).runsettings"
testRunTitle: netcoreapp2.1-$(Agent.JobName)

- bash: mono ~/.nuget/packages/xunit.runner.console/2.4.1/tools/net472/xunit.console.exe bin/MessagePack.Tests/$(BuildConfiguration)/net472/MessagePack.Tests.dll -parallel none -html $(BUILD.ARTIFACTSTAGINGDIRECTORY)/build_logs/mono_testrun.html
- bash: mono ~/.nuget/packages/xunit.runner.console/2.4.1/tools/net472/xunit.console.exe bin/MessagePack.Tests/$(BuildConfiguration)/net472/MessagePack.Tests.dll -html $(BUILD.ARTIFACTSTAGINGDIRECTORY)/build_logs/mono_testrun.html
displayName: Run MessagePack.Tests (mono)

- task: DotNetCoreCLI@2
Expand Down
3 changes: 3 additions & 0 deletions 3 sandbox/DynamicCodeDumper/DynamicCodeDumper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<Compile Include="..\..\src\MessagePack.UnityClient\Assets\Scripts\MessagePack\MessagePackSecurity.cs">
<Link>Code\MessagePackSecurity.cs</Link>
</Compile>
<Compile Include="..\..\src\MessagePack.UnityClient\Assets\Scripts\MessagePack\MonoProtection.cs">
<Link>Code\MonoProtection.cs</Link>
</Compile>
<Compile Include="..\..\src\MessagePack.UnityClient\Assets\Scripts\MessagePack\HashCode.cs">
<Link>Code\HashCode.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ internal class DynamicAssembly
// don't expose ModuleBuilder
//// public ModuleBuilder ModuleBuilder { get { return moduleBuilder; } }

private readonly object gate = new object();

public DynamicAssembly(string moduleName)
{
#if NETFRAMEWORK // We don't ship a net472 target, but we might add one for debugging purposes
Expand All @@ -36,29 +34,11 @@ public DynamicAssembly(string moduleName)

/* requires lock on mono environment. see: https://github.com/neuecc/MessagePack-CSharp/issues/161 */

public TypeBuilder DefineType(string name, TypeAttributes attr)
{
lock (this.gate)
{
return this.moduleBuilder.DefineType(name, attr);
}
}
public TypeBuilder DefineType(string name, TypeAttributes attr) => this.moduleBuilder.DefineType(name, attr);

public TypeBuilder DefineType(string name, TypeAttributes attr, Type parent)
{
lock (this.gate)
{
return this.moduleBuilder.DefineType(name, attr, parent);
}
}
public TypeBuilder DefineType(string name, TypeAttributes attr, Type parent) => this.moduleBuilder.DefineType(name, attr, parent);

public TypeBuilder DefineType(string name, TypeAttributes attr, Type parent, Type[] interfaces)
{
lock (this.gate)
{
return this.moduleBuilder.DefineType(name, attr, parent, interfaces);
}
}
public TypeBuilder DefineType(string name, TypeAttributes attr, Type parent, Type[] interfaces) => this.moduleBuilder.DefineType(name, attr, parent, interfaces);

#if NETFRAMEWORK

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Threading;

namespace MessagePack
{
/// <summary>
/// Special behavior for running on the mono runtime.
/// </summary>
internal struct MonoProtection
{
/// <summary>
/// Gets a value indicating whether the mono runtime is executing this code.
/// </summary>
internal static bool IsRunningOnMono => Type.GetType("Mono.Runtime") != null;

/// <summary>
/// A lock that we enter on mono when generating dynamic types.
/// </summary>
private static readonly object RefEmitLock = new object();

/// <summary>
/// The method to call within the expression of a <c>using</c> statement whose block surrounds all Ref.Emit code.
/// </summary>
/// <returns>The value to be disposed of to exit the Ref.Emit lock.</returns>
/// <remarks>
/// This is a no-op except when running on Mono.
/// <see href="https://github.com/mono/mono/issues/20369#issuecomment-690316456">Mono's implementation of Ref.Emit is not thread-safe</see> so we have to lock around all use of it
/// when using that runtime.
/// </remarks>
internal static MonoProtectionDisposal EnterRefEmitLock() => IsRunningOnMono ? new MonoProtectionDisposal(RefEmitLock) : default;
}

internal struct MonoProtectionDisposal : IDisposable
{
private readonly object lockObject;

internal MonoProtectionDisposal(object lockObject)
{
this.lockObject = lockObject;
Monitor.Enter(lockObject);
}

public void Dispose()
{
if (this.lockObject is object)
{
Monitor.Exit(this.lockObject);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,38 +90,41 @@ private static TypeInfo BuildType(Type enumType)
Type underlyingType = Enum.GetUnderlyingType(enumType);
Type formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(enumType);

TypeBuilder typeBuilder = DynamicAssembly.Value.DefineType("MessagePack.Formatters." + enumType.FullName.Replace(".", "_") + "Formatter" + Interlocked.Increment(ref nameSequence), TypeAttributes.Public | TypeAttributes.Sealed, null, new[] { formatterType });

// void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options);
using (MonoProtection.EnterRefEmitLock())
{
MethodBuilder method = typeBuilder.DefineMethod(
"Serialize",
MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
null,
new Type[] { typeof(MessagePackWriter).MakeByRefType(), enumType, typeof(MessagePackSerializerOptions) });

ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Call, typeof(MessagePackWriter).GetRuntimeMethod(nameof(MessagePackWriter.Write), new[] { underlyingType }));
il.Emit(OpCodes.Ret);
}
TypeBuilder typeBuilder = DynamicAssembly.Value.DefineType("MessagePack.Formatters." + enumType.FullName.Replace(".", "_") + "Formatter" + Interlocked.Increment(ref nameSequence), TypeAttributes.Public | TypeAttributes.Sealed, null, new[] { formatterType });

// T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options);
{
MethodBuilder method = typeBuilder.DefineMethod(
"Deserialize",
MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
enumType,
new Type[] { typeof(MessagePackReader).MakeByRefType(), typeof(MessagePackSerializerOptions) });

ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Call, typeof(MessagePackReader).GetRuntimeMethod("Read" + underlyingType.Name, Type.EmptyTypes));
il.Emit(OpCodes.Ret);
}
// void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options);
{
MethodBuilder method = typeBuilder.DefineMethod(
"Serialize",
MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
null,
new Type[] { typeof(MessagePackWriter).MakeByRefType(), enumType, typeof(MessagePackSerializerOptions) });

ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Call, typeof(MessagePackWriter).GetRuntimeMethod(nameof(MessagePackWriter.Write), new[] { underlyingType }));
il.Emit(OpCodes.Ret);
}

return typeBuilder.CreateTypeInfo();
// T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options);
{
MethodBuilder method = typeBuilder.DefineMethod(
"Deserialize",
MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
enumType,
new Type[] { typeof(MessagePackReader).MakeByRefType(), typeof(MessagePackSerializerOptions) });

ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Call, typeof(MessagePackReader).GetRuntimeMethod("Read" + underlyingType.Name, Type.EmptyTypes));
il.Emit(OpCodes.Ret);
}

return typeBuilder.CreateTypeInfo();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,102 +352,105 @@ public static TypeInfo BuildType(DynamicAssembly assembly, Type type, bool force
throw new MessagePackSerializationException("Building dynamic formatter only allows public type. Type: " + type.FullName);
}

Type formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(type);
TypeBuilder typeBuilder = assembly.DefineType("MessagePack.Formatters." + SubtractFullNameRegex.Replace(type.FullName, string.Empty).Replace(".", "_") + "Formatter" + Interlocked.Increment(ref nameSequence), TypeAttributes.Public | TypeAttributes.Sealed, null, new[] { formatterType });

FieldBuilder stringByteKeysField = null;
Dictionary<ObjectSerializationInfo.EmittableMember, FieldInfo> customFormatterLookup = null;

// string key needs string->int mapper for deserialize switch statement
if (serializationInfo.IsStringKey)
using (MonoProtection.EnterRefEmitLock())
{
ConstructorBuilder method = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
stringByteKeysField = typeBuilder.DefineField("stringByteKeys", typeof(byte[][]), FieldAttributes.Private | FieldAttributes.InitOnly);
Type formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(type);
TypeBuilder typeBuilder = assembly.DefineType("MessagePack.Formatters." + SubtractFullNameRegex.Replace(type.FullName, string.Empty).Replace(".", "_") + "Formatter" + Interlocked.Increment(ref nameSequence), TypeAttributes.Public | TypeAttributes.Sealed, null, new[] { formatterType });

ILGenerator il = method.GetILGenerator();
BuildConstructor(type, serializationInfo, method, stringByteKeysField, il);
customFormatterLookup = BuildCustomFormatterField(typeBuilder, serializationInfo, il);
il.Emit(OpCodes.Ret);
}
else
{
ConstructorBuilder method = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
ILGenerator il = method.GetILGenerator();
il.EmitLoadThis();
il.Emit(OpCodes.Call, objectCtor);
customFormatterLookup = BuildCustomFormatterField(typeBuilder, serializationInfo, il);
il.Emit(OpCodes.Ret);
}
FieldBuilder stringByteKeysField = null;
Dictionary<ObjectSerializationInfo.EmittableMember, FieldInfo> customFormatterLookup = null;

{
MethodBuilder method = typeBuilder.DefineMethod(
"Serialize",
MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
returnType: null,
parameterTypes: new Type[] { typeof(MessagePackWriter).MakeByRefType(), type, typeof(MessagePackSerializerOptions) });
method.DefineParameter(1, ParameterAttributes.None, "writer");
method.DefineParameter(2, ParameterAttributes.None, "value");
method.DefineParameter(3, ParameterAttributes.None, "options");
// string key needs string->int mapper for deserialize switch statement
if (serializationInfo.IsStringKey)
{
ConstructorBuilder method = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
stringByteKeysField = typeBuilder.DefineField("stringByteKeys", typeof(byte[][]), FieldAttributes.Private | FieldAttributes.InitOnly);

ILGenerator il = method.GetILGenerator();
BuildSerialize(
type,
serializationInfo,
il,
() =>
{
il.EmitLoadThis();
il.EmitLdfld(stringByteKeysField);
},
(index, member) =>
{
FieldInfo fi;
if (!customFormatterLookup.TryGetValue(member, out fi))
{
return null;
}
ILGenerator il = method.GetILGenerator();
BuildConstructor(type, serializationInfo, method, stringByteKeysField, il);
customFormatterLookup = BuildCustomFormatterField(typeBuilder, serializationInfo, il);
il.Emit(OpCodes.Ret);
}
else
{
ConstructorBuilder method = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
ILGenerator il = method.GetILGenerator();
il.EmitLoadThis();
il.Emit(OpCodes.Call, objectCtor);
customFormatterLookup = BuildCustomFormatterField(typeBuilder, serializationInfo, il);
il.Emit(OpCodes.Ret);
}

return () =>
{
MethodBuilder method = typeBuilder.DefineMethod(
"Serialize",
MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
returnType: null,
parameterTypes: new Type[] { typeof(MessagePackWriter).MakeByRefType(), type, typeof(MessagePackSerializerOptions) });
method.DefineParameter(1, ParameterAttributes.None, "writer");
method.DefineParameter(2, ParameterAttributes.None, "value");
method.DefineParameter(3, ParameterAttributes.None, "options");

ILGenerator il = method.GetILGenerator();
BuildSerialize(
type,
serializationInfo,
il,
() =>
{
il.EmitLoadThis();
il.EmitLdfld(fi);
};
},
1);
}
il.EmitLdfld(stringByteKeysField);
},
(index, member) =>
{
FieldInfo fi;
if (!customFormatterLookup.TryGetValue(member, out fi))
{
return null;
}

{
MethodBuilder method = typeBuilder.DefineMethod(
"Deserialize",
MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
type,
new Type[] { refMessagePackReader, typeof(MessagePackSerializerOptions) });
method.DefineParameter(1, ParameterAttributes.None, "reader");
method.DefineParameter(2, ParameterAttributes.None, "options");
return () =>
{
il.EmitLoadThis();
il.EmitLdfld(fi);
};
},
1);
}

ILGenerator il = method.GetILGenerator();
BuildDeserialize(
type,
serializationInfo,
il,
(index, member) =>
{
FieldInfo fi;
if (!customFormatterLookup.TryGetValue(member, out fi))
{
return null;
}
{
MethodBuilder method = typeBuilder.DefineMethod(
"Deserialize",
MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual,
type,
new Type[] { refMessagePackReader, typeof(MessagePackSerializerOptions) });
method.DefineParameter(1, ParameterAttributes.None, "reader");
method.DefineParameter(2, ParameterAttributes.None, "options");

return () =>
ILGenerator il = method.GetILGenerator();
BuildDeserialize(
type,
serializationInfo,
il,
(index, member) =>
{
il.EmitLoadThis();
il.EmitLdfld(fi);
};
},
1); // firstArgIndex:0 is this.
}
FieldInfo fi;
if (!customFormatterLookup.TryGetValue(member, out fi))
{
return null;
}

return () =>
{
il.EmitLoadThis();
il.EmitLdfld(fi);
};
},
1); // firstArgIndex:0 is this.
}

return typeBuilder.CreateTypeInfo();
return typeBuilder.CreateTypeInfo();
}
}

public static object BuildFormatterToDynamicMethod(Type type, bool forceStringKey, bool contractless, bool allowPrivate)
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.