用于调试的未初始化内存的常用值是什么? [英] What are common values for uninitialized memory for debugging?

查看:204
本文介绍了用于调试的未初始化内存的常用值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很久以前,我学会了用0xDEADBEEF填充未使用/未初始化的内存,以便在调试器或崩溃报告中,如果我看到该值,便知道我正在查看未初始化的内存.我从崩溃报告中看到iOS使用0xBBADBEEF.

A long time ago I learned about filling unused / uninitialized memory with 0xDEADBEEF so that in a debugger or a crash report if I ever see that value I know I'm looking at uninitialized memory. I saw from a crash report iOS uses 0xBBADBEEF.

人们使用了哪些其他创意价值?是否有任何特定的价值有某种特定的好处?

What other creative values have people used? Do any particular values have any kind of specific benefit?

将值转化为单词的最明显的好处是,至少对于大多数人而言,如果单词以其语言显示,则它们很容易伸出,因为某些严格的数字值不太可能伸出.

The most obvious benefit of values that turn into words is that, at least of most people, if the words are in their language they stick out easily where as some strictly numeric value is less likely to stick out.

但是,也许还有其他原因可以选择数字?例如,在某些内存访问中,奇数可能会使处理器(68000)崩溃,因此最好选择0x0BADBEEF而不是0xBADBEEF0.它们的其他任何值(也许是特定于处理器的)是否有用于未初始化内存的具体好处?

But, maybe there are other reason to pick numbers? For example an odd number might crash a processors (68000) for example on certain memory accesses so it's probably better to pick 0x0BADBEEF over 0xBADBEEF0. Are their any other values (maybe processor specific) that have a concrete benefit for using for uninitialized memory?

推荐答案

通常来说,您需要一个值,当该值被解释为整数,指针或字符串时,该值不太可能在工作"时发生.因此,这里有一些约束条件:

Generally speaking, you want a value which is unlikely to happen to "work" when interpreted as either an integer, a pointer, or a string. So, here are a few constraints:

  • 请勿使用目标架构上最小常规"对齐方式的倍数的值.对于x86,它是4(字节),因此没有可被4整除的值.这可以确保如果将值解释为指针,则它显然是不正确的.如果您使用的是非x86架构,则甚至可以使用一个值作为指针使用时会导致对齐陷阱.

  • Don't use a value that's a multiple of the smallest "usual" alignment on your target architecture. For x86, that's 4 (bytes), so no values that are divisible by 4. This ensures that if the value is interpreted as a pointer, it'll be obviously-incorrect. If you're on a non-x86 architecture, you might even be able to use a value that will cause an alignment trap if used as a pointer.

请勿使用合理地为小(正或负)整数的值.在C程序中,典型的"int"变量永远不会大于1,000左右,因此请不要使用小数字作为空数据填充.

Don't use a value which could reasonably be a small (positive or negative) integer. Your typical "int" variable in a C program never gets larger than 1,000 or so, so don't use small numbers as your empty data fill.

请勿使用完全由有效ASCII字符组成的值.确保其中至少有一个字节设置了高位.这些天来,您需要确保它们都不是有效的UTF-8值,也可能不是UTF-16值.

Don't use a value which is composed entirely of valid ASCII characters. Make sure there's at least one byte in there with the high bit set. These days, you'd want to make sure they weren't valid UTF-8 or possibly UTF-16 values, either.

该值中没有任何零字节.在很多情况下,这样做对防止程序崩溃是有帮助的"-终止字符串,为非整数字段提供看起来合理的值,等等.

Don't have any zero bytes in the value. There are too many cases where this would work out to be "helpful" to keeping the program from crashing - terminating a string, giving a non-int field a reasonable-looking value, etc.

不要重复使用一个(或两个)字节值.拥有全字长的模式可以更轻松地确定野指针如何最终指向它的位置,至少可以缩小哪些操作使之从模式开始时偏移了它.

Don't use a single (or two) byte values, repeated over and over. Having a full-word length pattern can make it easier to determine how your wild pointer ended up pointing where it is, at least narrowing down which operations offset it from the start of the pattern.

对于典型"进程,请不要使用映射到有效地址的值.如果设置了最高位,则通常需要花费大量的malloc()才能使进程足够大以使该地址成为有效地址.

Don't use a value that maps to an valid address for a "typical" process. If the highest bits are set, it'll typically take a whole lot of malloc() before your process will grow large enough to make that a valid address.

也许不足为奇的是,像0xDEADBEEF这样的模式基本上满足了所有这些要求.

Perhaps unsurprisingly, patterns like 0xDEADBEEF meet basically all of these requirements.

这篇关于用于调试的未初始化内存的常用值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆