将数据存储在指针的最高有效位 [英] Storing data in the most significant bits of a pointer

查看:130
本文介绍了将数据存储在指针的最高有效位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多有关标记指针的信息,这些信息(ab)使用对齐方式将数据位存储在未使用的最低有效位中的类型要求.

I've found quite some information about tagged pointers, which (ab)use the alignment requirements of types to store bits of data in their unused least significant bits.

但是我想知道,您是否不能对64位系统中的最高有效位执行相同的操作?即使您要使用64位指针的16个最高有效位,它们仍然需要超过256 TB的RAM才能重叠.

I was wondering however, couldn't you do the same with the most significant bits on a 64 bit system? Even if you were to use the 16 most significant bits of a 64 bit pointer, you would still need more than 256 terabytes of RAM for them to overlap.

我知道理论上这是未定义的行为,但是在某些常见的操作系统(Windows/Max/Linux)上,这在实践中会如何表现?

I know that in theory this is undefined behavior, but how would this behave in practice on some of the common operating systems (Windows/Max/Linux)?

是的,我知道这是邪恶和危险的,但这不是这个问题的意思.这是关于将计算机程序推向极限的如果……那么"问题,而不是关于理智和便携式软件设计的问题.

And yes, I am aware that this is evil and dangerous, but that is not what this question is about. It is a "what if" question about pushing computer programs to their limits, not one about sane and portable software design.

推荐答案

如果您知道确切的内存布局,则可以这样做,但是这样做很冒险. Windows/Mac/Linux上最常见的64位系统是amd64.在可预见的将来,该机器上的机器只有48位虚拟地址,因此从理论上讲,您还有16位可以玩,还有较低的对齐位.

If you know your exact memory layout you can probably do it, but it's risky. The most common 64 bits systems for Windows/Mac/Linux are amd64. On them the machine only has 48 bit virtual addresses (for the foreseeable future), so you have 16 bits to play around in plus the lower aligned bits, theoretically.

除外.地址空间的一半为负数;地址在[-2 ^ 47,2 ^ 47)之间.因此,您无法确定指针中设置的位是否实际上意味着您设置了魔术位,或者您只是拥有一个负地址.

Except. Half of the address space is negative; addresses go between [-2^47,2^47). So you can't be sure if the bits set in the pointer actually mean that your magic bits are set or you just have a negative address.

除外.如今,大多数(如果不是全部)操作系统会将内核放在负地址空间中,而将用户空间放在正地址空间中.它使某些事情更容易,更快捷地进行管理.因此,您可能会滥用这些知识来假定使用这些位是安全的.

Except. Today, most, if not all operating systems put the kernel in the negative address space and put the userland in the positive address space. It makes certain things easier and faster to manage. So you could abuse that knowledge to assume that playing with those bits should be safe.

除外.我从未见过任何操作系统能保证这种情况会永远持续下去(并不意味着不存在,我只是从未见过).您可能有一天会更新内核,然后突然操作系统决定userland为负而内核为正,否则userland将获得更多的地址空间来玩耍.

Except. I've never seen a guarantee from any operating system that this situation will remain forever (doesn't mean that one doesn't exist, I just haven't seen one). You might update your kernel one day and suddenly the operating system decided that userland is negative and kernel is positive‚ or userland gets more address space to play around in.

只要您在取消引用指针之前屏蔽掉多余的位,就可以安全地今天,但明天可能不会.并且,当您围绕这样的假设构建代码时,当您无法解决的未定义行为变成无法解决的未定义行为时,您应该承受所有的痛苦.像这样把自己画在一个角落是不好玩的.

As long as you mask out the extra bits before you dereference your pointers, you will be safe today, but maybe not tomorrow. And when you build your code around an assumption like this, you deserve all the pain you get when your undefined behavior you get away with becomes undefined behavior you don't get away with. Painting yourself into a corner like this is not fun.

这篇关于将数据存储在指针的最高有效位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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