算術操作は、安全でないC#のオーバーフローをもたらしました
-
24-10-2019 - |
質問
バックグラウンド
Joe Duffyの「Windowsでの同時プログラミング」(149ページ)から、1年以上にわたってコピーされた逐語的なコピーを使用しています(149ページ)。コード(以下)は、十分なスタックスペースがある場合、ASP.NET Webアプリケーションで使用されます。当社のサイトを使用すると、ユーザーは独自のWebページをスクリプト化し、単純なProprietry Scripting Languageでロジックを制御できます。ユーザーが厄介なものをスクリプト化し、Stackoverflowの例外を引き起こす可能性があるため、Duffyのコード例を使用してErrantスクリプトの実行を停止することができます。未調整不可能なStackoverflow例外は、IIS Apppool全体を削除します。これは本当にうまく機能しています。
問題
突然、今日の午後、system.overflowexceptionエラーで満たされたログ。そのサーバーへのすべてのリクエストで同じ例外を取得しました。迅速なIISリセットが問題を治しました。
例外タイプ:System.OverFlowException
例外メッセージ:算術操作によりオーバーフローが発生しました。
スタックトレース:System.Intptr..Ctor(int64値)Liquidhtmlflowmanager.stackmanagement.checkforsufficientStack(uint64バイト)のc: svn liquidhtml
コード:
public static class StackManagement
{
[StructLayout(LayoutKind.Sequential)]
struct MEMORY_BASIC_INFORMATION
{
public uint BaseAddress;
public uint AllocationBase;
public uint AllocationProtect;
public uint RegionSize;
public uint State;
public uint Protect;
public uint Type;
};
//We are conservative here. We assume that the platform needs a
//whole 16 pages to respond to stack overflow (using an X86/X64
//page-size, not IA64). That's 64KB, which means that for very
//small stacks (e.g. 128kb) we'll fail a lot of stack checks (say in asp.net)
//incorrectly.
private const long STACK_RESERVED_SPACE = 4096 * 16;
/// <summary>
/// Checks to see if there is at least "bytes" bytes free on the stack.
/// </summary>
/// <param name="bytes">Number of Free bytes in stack we need.</param>
/// <returns>If true then there is suffient space.</returns>
public unsafe static bool CheckForSufficientStack(ulong bytes)
{
MEMORY_BASIC_INFORMATION stackInfo = new MEMORY_BASIC_INFORMATION();
//We subtract one page for our request. VirtualQuery rounds up
//to the next page. But the stack grows down. If we're on the
//first page (last page in the VirtualAlloc), we'll be moved to
//the next page which is off the stack! Note this doesn't work
//right for IA64 due to bigger pages.
IntPtr currentAddr = new IntPtr((uint)&stackInfo - 4096);
//Query for the current stack allocation information.
VirtualQuery(currentAddr, ref stackInfo, sizeof(MEMORY_BASIC_INFORMATION));
//If the current address minus the base (remember: the stack
//grows downward in the address space) is greater than the
//number of bytes requested plus the unreserved space at the end,
//the request has succeeded.
System.Diagnostics.Debug.WriteLine(String.Format("CurrentAddr = {0}, stackInfo.AllocationBase = {1}. Space left = {2} bytes.", (uint)currentAddr.ToInt64(),
stackInfo.AllocationBase,
((uint)currentAddr.ToInt64() - stackInfo.AllocationBase)));
return ((uint)currentAddr.ToInt64() - stackInfo.AllocationBase) > (bytes + STACK_RESERVED_SPACE);
}
[DllImport("kernel32.dll")]
private static extern int VirtualQuery(IntPtr lpAddress, ref MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);
}
注:47行目はこれです
IntPtr currentAddr = new IntPtr((uint)&stackInfo - 4096);
質問:
コードのどの部分がオーバーフローしますか、それはポインターからUINTへのキャスト、「-4096」操作、またはINT64へのキャストですか?
これをより堅牢にする方法はありますか?
いくつかの情報:
OSは64ビットWindows Server 2008で、IIS7をIIS7をIIS7(x86)CPUで実行しています。
CheckForSufficientStack関数に渡されたパラメーターは次のとおりです。
private const Int32 _minimumStackSpaceLimit = 48 * 1024;
編集:答えをありがとう。コードを更新して、キャストを削除し、32ビットと64ビットの両方で動作するようにポインターサイズの変数を使用しました。ここで他の誰かがそれを望んでいる場合です:
public static class StackManagement
{
[StructLayout(LayoutKind.Sequential)]
struct MEMORY_BASIC_INFORMATION
{
public UIntPtr BaseAddress;
public UIntPtr AllocationBase;
public uint AllocationProtect;
public UIntPtr RegionSize;
public uint State;
public uint Protect;
public uint Type;
};
private const long STACK_RESERVED_SPACE = 4096 * 16;
public unsafe static bool CheckForSufficientStack(UInt64 bytes)
{
MEMORY_BASIC_INFORMATION stackInfo = new MEMORY_BASIC_INFORMATION();
UIntPtr currentAddr = new UIntPtr(&stackInfo);
VirtualQuery(currentAddr, ref stackInfo, sizeof(MEMORY_BASIC_INFORMATION));
UInt64 stackBytesLeft = currentAddr.ToUInt64() - stackInfo.AllocationBase.ToUInt64();
System.Diagnostics.Debug.WriteLine(String.Format("CurrentAddr = {0}, stackInfo.AllocationBase = {1}. Space left = {2} bytes.",
currentAddr,
stackInfo.AllocationBase,
stackBytesLeft));
return stackBytesLeft > (bytes + STACK_RESERVED_SPACE);
}
[DllImport("kernel32.dll")]
private static extern int VirtualQuery(UIntPtr lpAddress, ref MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);
}
解決
キャストは間違っています。 Stackinfoのアドレスは64ビット値です。オーバーフローエクセプトを危険にさらすことなく、それをUINTにキャストすることはできません。 4096を差し引くことには意味がありません。VirtualQuery()はとにかくベースアドレスを見つけます。修理:
IntPtr currentAddr = new IntPtr(&stackInfo);
Duffyのコードは、32ビットコードでのみ機能します。