非NULL保留指针值 [英] non-NULL reserved pointer value

查看:128
本文介绍了非NULL保留指针值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个保留指针值?

How can I create a reserved pointer value?

上下文是这样的:我一直在想如何实现一个动态脚本语言的数据结构(我不是在实施这项计划 - 只是想知道它将如何做)

The context is this: I have been thinking of how to implement a data structure for a dynamic scripting language (I am not planning on implementing this - just wondering how it would be done).

字符串可以包含任意字节,包括NUL。因此,有必要分别存储该值。这需要一个指针(以点到阵列)和一个数字。所述第一特技是,如果指针为NULL,它不可能是有效的串,所以数量可以用于实际的整数。

Strings may contain arbitrary bytes, including NUL. Thus, it is necessary to store the value separately. This requires a pointer (to point to the array) and a number. The first trick is that if the pointer is NULL, it cannot possibly be a valid string, so the number can be used for an actual integer.

如果第二保留指针值可以被创建,这可以用来暗示另一场现在被用作浮点值。可以这样做?

If a second reserved pointer value could be created, this could be used to imply that the other field is now being used as a floating-point value. Can this be done?

一个想到的就是mmap()的一个地址,没有权限的,这也可以做替代NULL指针的用法。

One thought is to mmap() an address with no permissions, which could also be done to replace the usage of the NULL pointer.

推荐答案

在任何现代的系统,你可以使用指针值 1 2 ... 4095 用于上述目的。另一个常见的​​选择是(uintptr_t形式)-1 ,这在技术上是劣势,但使用的频率比 1 仍然

On any modern system, you can just use the pointer values 1, 2, ... 4095 for such purposes. Another frequent choice is (uintptr_t)-1, which is technically inferior, but used more frequently than 1 nevertheless.

为什么这些价值观安全?结果
对NULL指针现代系统保障由使其无法在映射虚拟地址零任何访问。几乎空指针解引用任何会打这个不存在的地区,硬件会告诉坏事发生的OS系统,这会触发操作系统段错误的过程。结果
由于虚拟内存页面页对齐的(至少在当前的硬件4K),并没有被映射到地址零,没有什么可以被映射到整个范围内 0,...,4095 ,保护所有这些地址以同样的方式,你可以用它们作为特殊用途的值。

Why are these values "safe"?
Modern systems safeguard against NULL pointer accesses by making it impossible to map anything at virtual address zero. Almost any dereferencing of a NULL pointer will hit this nonexistant region, and the hardware will tell the OS system that something bad happened, which triggers the OS to segfault the process.
Since virtual memory pages are page aligned (at least 4k on current hardware), and nothing is mapped to address zero, nothing can be mapped to the entire range 0, ..., 4095, protecting all these addresses in the same way, and you can use them as special purpose values.

多少虚拟内存空间保留用于此目的是一个系统参数,在Linux上它是由的/ proc / sys目录/ VM / mmap_min_addr等多项控制,root用户可以将其更改到零,这将禁用此保护(这不会是一个非常聪明的想法)。在Ubuntu默认为64K(即16页)。

How much virtual memory space is reserved for this purpose is a system parameter, on linux it is controlled by /proc/sys/vm/mmap_min_addr, and the root user can change it to zero, which would disable this protection (which would not be a very smart idea). The default on Ubuntu is 64k (i. e. 16 pages).

这也是为什么(uintptr_1)-1 是小于安全 1 ;即使多个字节的任何负载将达到零页,地址(uintptr_1)-1 本身并不一定是这样的保护。因此,做字符串操作(的char *) - 1 不一定段错误

This is also the reason why (uintptr_1)-1 is less safe than 1; even though any load of more than one byte will hit the zero page, the address (uintptr_1)-1 itself is not necessarily protected in this way. Consequently, doing string operations on (char*)-1 does not necessarily segfault.

编辑:结果
我与特殊映射原解释似乎已经有点陈旧,大概这就是事情的旧的Mac / PPC平台上的处理方式。尽管效果是pretty大同小异,我改变了答案的细节来体现现代的Linux。无论如何,重要的一点是没有的如何的空页保护实现,重要的一点是,任何理智的,现代的系统将拥有的部分的,它包括至少空页面保护上述地址范围。更多的细节可以在此找到答案SO:<一href=\"http://stackoverflow.com/a/12645890/2445184\">http://stackoverflow.com/a/12645890/2445184


My original explanation with the special mapping seems to have been a bit stale, probably this was the way things were handled on the old Mac/PPC platform. Even though the effect is pretty much the same, I changed the details of the answer to reflect modern linux. Anyway, the important point is not how the null page protection is achieved, the important point is that any sane, modern system will have some null page protection that encompasses at least the mentioned address range. Some more details can be found in this SO answer: http://stackoverflow.com/a/12645890/2445184

这篇关于非NULL保留指针值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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