简体   繁体   English

如何断言/测试何时将未初始化的内存传递给函数

[英]How to assert/test when uninitialised memory is passed to function

I have a situation where a part of my code has been found to be passed uninitialized memory at times. 我遇到一种情况,有时发现我的代码的一部分被传递给未初始化的内存。 I am looking for a way in which I could assert when this case occurs when running with the debug-heap. 我正在寻找一种方法,可以在使用debug-heap运行时断言这种情况。 This is a function that could be thrown about in places for that extra help in tracking bugs: 可以在某些地方使用此函数,以提供有关跟踪错误的额外帮助:

void foo( char* data, int dataBytes )
{
    assert( !hasUninitialisedData(data,dataBytes) ); //, This is what we would like
    ...
}

I have seen that there are tools like valgrind and as I run on windows there is DrMemory. 我已经看到有像valgrind这样的工具,当我在Windows上运行时,有DrMemory。 These however run external to the application so don't find the issue when it occurs for the developer. 但是,它们在应用程序外部运行,因此在开发人员遇到问题时不会发现问题。 More importantly these throw up thousands of reports for Qt and other irrelevant functions making things impossible. 更重要的是,它们为Qt和其他不相关的功能抛出了数千个报告,从而使事情不可能。

I think the idea is to have a function that would search for the 0xBAADFOOD within the array but there are a whole series of potential hex values and these change per platform. 我认为这个想法是要有一个函数,可以在数组中搜索0xBAADFOOD,但是有一系列潜在的十六进制值,并且这些值在每个平台上都会发生变化。 These hex values may also sometimes be valid when integers are stored so not sure if there is more information that can be obtained form the debug-heap. 当存储整数时,这些十六进制值有时也可能有效,因此不确定是否可以从调试堆中获取更多信息。

I am primarily interested the potential there could be a CRT function, library, visual-studio breakpoint, or other helper function for doing this sort of check. 我主要感兴趣的是,可能存在CRT功能,库,visual-studio断点或其他帮助程序功能来进行这种检查。 It 'feels' like there should be one somewhere already, I couldn't find it yet so if anybody has some nice solutions for this sort of situation it would be appreciated. 感觉“应该已经有一个地方了,我还找不到”,所以如果有人对这种情况有一些不错的解决方案,将不胜感激。

EDIT: I should explain better, I know the debug-heap will initialize all allocations with a value in attempt to allow detecting uninitialised data. 编辑:我应该更好地解释,我知道调试堆将使用一个值初始化所有分配,以尝试检测未初始化的数据。 As mentioned the data being received contains some 0xBAADFOOD values, normally memory is initialized with 0xCDCDCDCD but this is a third party library allocating the data and apparently there are multiple magic numbers hence I am interested if there is a generalized check hidden somewhere. 如前所述,接收到的数据包含一些0xBAADFOOD值,通常使用0xCDCDCDCD初始化内存,但这是分配数据的第三方库,并且显然有多个幻数,因此,如果某个地方隐藏了通用校验,我很感兴趣。

The VC++ runtime, at least in debug builds, initialize all heap allocations with a certain value. VC ++运行时(至少在调试版本中)使用特定值初始化所有堆分配。 It has been the same value for as long as I can remember. 只要我记得,它就具有相同的价值。 I can't, however, remember the actual value. 但是,我不记得实际值。 You could do a quick allocation test and check. 您可以进行快速分配测试并检查。

Debug builds of VC++ programs often set uninitialized memory to 0xCD at startup. VC ++程序的调试版本通常在启动时将未初始化的内存设置为0xCD That's not dependable over the life of the session (once the memory's been allocated/used/deallocated the value will change), but it's a place to start. 在会话的整个生命周期中这都不可靠(一旦内存的分配/使用/取消分配后,值将发生变化),但这是一个起点。

I have implemented a function now that basically does what is intended after finding a list of magic numbers on wiki (Magic numbers) : 我现在已经实现了一个功能,该功能基本上可以在Wiki上找到一个魔幻数字(魔幻数字)列表之后执行预期的工作:

/** Performs a check for potentially unintiialised data
    \remarks May incorrectly report uninitialised data as it is always possible the contained data may match the magic numbers in rare circumstances so this function should be used for initial identification of uninitialised data only
*/
bool hasUninitialisedData( const char* data, size_t lenData )
{
    const unsigned int kUninitialisedMagic[] = 
    {
        0xABABABAB, // Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory
        0xABADCAFE, // A startup to this value to initialize all free memory to catch errant pointers
        0xBAADF00D, // Used by Microsoft's LocalAlloc(LMEM_FIXED) to mark uninitialised allocated heap memory
        0xBADCAB1E, // Error Code returned to the Microsoft eVC debugger when connection is severed to the debugger
        0xBEEFCACE, // Used by Microsoft .NET as a magic number in resource files
        0xCCCCCCCC, // Used by Microsoft's C++ debugging runtime library to mark uninitialised stack memory
        0xCDCDCDCD, // Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory
        0xDEADDEAD, // A Microsoft Windows STOP Error code used when the user manually initiates the crash.
        0xFDFDFDFD, // Used by Microsoft's C++ debugging heap to mark "no man's land" guard bytes before and after allocated heap memory
        0xFEEEFEEE, // Used by Microsoft's HeapFree() to mark freed heap memory
    };
    const unsigned int kUninitialisedMagicCount = sizeof(kUninitialisedMagic)/sizeof(kUninitialisedMagic[0]);

    if ( lenData < 4 ) return assert(false=="not enough data for checks!"), false;

    for ( unsigned int i =0; i < lenData - 4; ++i ) //< we don't check the last few bytes as keep to full 4-byte/int checks for now,  this is where the -4 comes in
    {
        for ( unsigned int iMagic = 0; iMagic < kUninitialisedMagicCount; ++iMagic )
        {
            const unsigned int* ival = reinterpret_cast<const unsigned int*>(data + i);
            if ( *ival == kUninitialisedMagic[iMagic] )
                return true;
        }
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何断言内存已释放 - How to assert that memory is freed 当 assert() 意外触发时,如何抑制 Google 测试中的终止? - How to suppress termination in Google test when assert() unexpectedly triggers? 如何以未初始化的内存最好地处理复制交换习惯 - How to best handle copy-swap idiom with uninitialised memory 如何让gtest在遇到断言时没有完全关闭? (不是测试断言) - How do I make gtest not completely shutdown when it hits an assert? (not a test assert) 如何使用 Google 测试捕获断言? - How to catch an assert with Google test? Assert function in C++ using cassert function - How can I declare the variables in the test function? - Assert function in C++ using cassert function - How can I declare the variables in the test function? memory 删除 SkCanvas 时断言 object - memory assert when delete SkCanvas object 使用 assert C++ 测试函数 - test a function using assert c++ 传递给函数时如何将结构空化? - How to null struct when passed to function? Clang ++ 6.0 Memory Sanitizer在其返回值指示条件分支的函数中未报告未初始化的局部变量 - Clang++ 6.0 Memory Sanitizer not reporting uninitialised local variable in a function whose return value dictates a conditional branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM