gpt4 book ai didi

windows-8 - Windows 8、.NET 4.5 DefineUninitializedData 问题

转载 作者:行者123 更新时间:2023-12-02 22:22:13 24 4
gpt4 key购买 nike

我正试图找出我们的编译器和 .NET 4.5 与 Windows 8 的问题的根源。我已将其简化为一小段代码,想知道是否有人对这个问题有任何见解。我写了一些 C#,它使用反射来生成显示问题的程序集。 C#(在此处的 VS2010 解决方案中 https://dl.dropbox.com/u/10931452/sdata.zip)位于本文底部。它创建了一个类“sdata”并向其添加了一个名为“blank16”的静态字段。然后它创建一个初始化该字段的静态构造函数。生成的可执行文件被写入 c:\temp\sdatatest.exe。当 sdatatest 在 .NET 4.5 下的 Windows 8 上运行时,它会产生:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'sdata' threw an exception. ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at sdata..cctor() --- End of inner exception stack trace --- at sdata.main()

在安装了 .NET 4.5 的 Windows 7 上运行时,它会运行。当在早期的 .NET 框架上运行时,它也能运行——并且已经运行了十年。

生成的 IL 看起来有效:

enter image description here

JITed x86 代码看起来也完全有效:

enter image description here

edi 的值看起来像是加载的可执行文件中的位置,而不是托管内存空间中的位置,如果它是只读的,则可以解释访问冲突。为什么这会在 Windows 8 上发生变化?

C# 生成 sdatatest 程序集:

using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

namespace sdata
{
class Program
{
static void Main( string[] args )
{
AssemblyName name = new AssemblyName( );
name.Name = "sdatatest.exe";
string exepath = "c:\\temp\\" + name.Name;
name.CodeBase = "file:://" + exepath;
AssemblyBuilder ass_bldr = Thread.GetDomain( ).DefineDynamicAssembly( name, AssemblyBuilderAccess.RunAndSave, Path.GetDirectoryName( exepath ));
ModuleBuilder module_bldr = ass_bldr.DefineDynamicModule( Path.GetFileName( exepath ), Path.GetFileName( exepath ), true );
TypeBuilder tb = module_bldr.DefineType( "sdata", TypeAttributes.Public | TypeAttributes.AnsiClass );
TypeBuilder sixteen = module_bldr.DefineType( "sixteen", TypeAttributes.Sealed, typeof( ValueType ), PackingSize.Size8, 16 ); // value type of size 16
Type t16 = sixteen.CreateType( );
var fb = tb.DefineUninitializedData( "blank16", 16, FieldAttributes.Public | FieldAttributes.Static );
ConstructorBuilder cons = tb.DefineConstructor( MethodAttributes.Static, CallingConventions.Standard, Type.EmptyTypes ); // ..cctor
var il = cons.GetILGenerator( );
il.BeginScope( );
il.Emit( OpCodes.Ldsflda, fb );
il.Emit( OpCodes.Ldc_I4, 0 );
il.Emit( OpCodes.Ldc_I4, 16 );
il.Emit( OpCodes.Initblk );
il.Emit( OpCodes.Ret );
il.EndScope( );
MethodBuilder mb = tb.DefineMethod( "main", MethodAttributes.Static | MethodAttributes.Public );
il = mb.GetILGenerator( );
il.BeginScope( );
il.Emit( OpCodes.Ldsflda, fb );
il.Emit( OpCodes.Pop );
il.Emit( OpCodes.Ret );
il.EndScope( );
tb.CreateType( );
ass_bldr.SetEntryPoint( mb );
ass_bldr.Save( Path.GetFileName( exepath ) );
}
}
}

最佳答案

答案是 Windows 8 更改了 .sdata 部分的访问权限:将其设为只读。如果您使用的是 DefineUninitializedData,那么您的代码可能会在 Windows 8 上中断并且需要更改。

关于windows-8 - Windows 8、.NET 4.5 DefineUninitializedData 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13541483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com