# 内存消歧

**内存消歧**所要解决的问题是：判定一条内存加载指令与一条内存存储指令是否指向同一地址，从而让处理器或编译器能够决定该加载是否依赖于该存储。在乱序CPU中，它表现为内存依赖预测的形式：由硬件预测某条加载的地址是否与一条更早的存储尚未确定的地址重叠，使该加载得以推测执行，而不必等待前面每一条存储都算出地址。<sup>[1](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)</sup> 同样的问题也出现在编译器中：静态别名分析与依赖分析试图在程序运行之前就证明加载与存储彼此独立。<sup>[2](https://conservancy.umn.edu/items/f08606f4-5752-4d4b-9b0f-88f04c85e90a)</sup>

| 关键事实 | 详情 |
|---|---|
| 硬件机制 | 内存消歧预测器让加载在其地址是否与更早的存储重叠尚不可知时推测执行；一旦重叠，加载可能读到陈旧数据，须重新执行<sup>[1](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)</sup> |
| 经典预测器 | 存储集（克莱索斯与埃默，数字设备公司，ISCA 1998）为每条加载记录它所依赖过的存储集合，性能接近拥有完美依赖知识的调度器<sup>[3](https://dblp.uni-trier.de/rec/conf/isca/ChrysosE98.html)</sup><sup> • </sup><sup>[4](https://people.csail.mit.edu/emer/media/papers/1998.06.isca.storesets.pdf)</sup> |
| 存储转发 | 只有当顺序、大小和对齐条件均满足时，加载才能直接从更早的存储取数：该存储已执行、所加载的字节是所存储字节的子集、地址匹配且不跨越缓存行边界<sup>[5](https://www.manualslib.com/manual/77668/Intel-Architecture-Ia-32.html?page=53)</sup> |
| 安全暴露面 | 预测错误会促成推测性存储绕过：加载瞬时读到陈旧数据，其效果可经侧信道测量<sup>[1](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)</sup> |
| 主要缓解措施 | 推测性存储绕过禁用（SSBD），置位于 IA32_SPEC_CTRL 第 2 位，由 CPUID 叶 07H 的 EDX 第 31 位枚举，阻止加载在所有更早的存储地址已知之前推测执行<sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/speculative-execution-side-channel-mitigations.html)</sup> |
| 厂商实现 | 英特尔描述了一个内存消歧预测器和一个独立的快速存储转发预测器；AMD“Zen 3”引入了预测性存储转发，在硬件中预测加载-存储依赖<sup>[1](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)</sup><sup> • </sup><sup>[7](https://www.amd.com/en/blogs/2021/predictive-store-forwarding.html)</sup> |
| 编译器一侧 | 静态别名分析与依赖分析因缺乏运行时信息而必须保守，这限制了编译器重排或推测的频率<sup>[2](https://conservancy.umn.edu/items/f08606f4-5752-4d4b-9b0f-88f04c85e90a)</sup> |

## 加载与存储为何冲突

乱序处理器希望尽可能早地发射加载指令，但若一条加载所读的地址是一条更早的存储尚未写入的，就会得到错误的值，这种情况称为内存序违例。<sup>[4](https://people.csail.mit.edu/emer/media/papers/1998.06.isca.storesets.pdf)</sup> 由于存储的地址往往算得很晚——例如要等更早的一条指令算出指针之后——一条已就绪的加载通常无从得知自己是否与该存储重叠。等待每一条更早的存储会使内存访问串行化，浪费掉乱序执行的大部分好处；而猜错并读到陈旧数据则需要恢复。<sup>[1](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)</sup>

承担这项簿记工作的硬件结构是加载-存储队列，大量工作致力于让这套基于地址的消歧逻辑可扩展且高效。<sup>[8](https://journal.info.unlp.edu.ar/JCST/article/view/754)</sup>

## 内存依赖预测

有影响力的解决方案是预测而非证明。在乔治·Z·克莱索斯与乔尔·S·埃默（当时均在[数字设备公司](https://www.edgepedia.org/zh/digital-equipment-corporation)）提出的存储集方案中，处理器为每条加载指令记住它过去依赖过的存储集合，即该加载的“存储集”，并利用这一记录预测该加载可安全执行的最早时刻。<sup>[4](https://people.csail.mit.edu/emer/media/papers/1998.06.isca.storesets.pdf)</sup> 论文表明，在大指令窗口的超标量机器中，存储集能准确预测内存依赖，性能接近拥有完美内存依赖知识的调度器。<sup>[4](https://people.csail.mit.edu/emer/media/papers/1998.06.isca.storesets.pdf)</sup> 该机制是自我纠正的：当处理器检测到内存序违例时，它会为肇事的加载与存储对建立存储集，使该加载此后的执行等待。<sup>[9](https://www.cs.ucr.edu/~gupta/research/Talks/PATH_TALK/tsld031.htm)</sup> 预测器还会定期使学到的存储-加载关系失效，并重新填充这些关系，以丢弃已经变冷的条目。<sup>[10](https://www.researchgate.net/publication/2881874_Memory_Dependence_Prediction_using_Store_Sets)</sup>

内存依赖预测（在某些商业产品中也称为内存消歧）被高端处理器常规性地用于改善加载指令的调度。<sup>[10](https://www.researchgate.net/publication/2881874_Memory_Dependence_Prediction_using_Store_Sets)</sup>

## 存储转发

一套相关机制处理加载确实依赖于更早存储的常见情形。借助存储转发，加载不必等待该存储到达存储层次并退役；存储的数据被直接转发给加载。这仅在三个条件下成立：被转发的数据由程序序上更早且已执行的存储产生（顺序），所加载的字节是所存储字节的子集（大小），且该存储不跨越缓存行边界、加载的线性地址与存储的相匹配（对齐）。对于未被预测为从同一线性地址加载的存储，加载仍可被移到它们之前。<sup>[5](https://www.manualslib.com/manual/77668/Intel-Architecture-Ia-32.html?page=53)</sup>

当这些条件被违反时，加载停顿。英特尔的优化手册按存储与加载的大小和对齐方式记录了详细的存储转发限制表，并指出若待存储的值尚不可用，加载会被延迟到该值可用为止；这一延迟相当可观且不确定，但总体影响小于违反大小与对齐要求的情形。<sup>[11](https://cdrdv2-public.intel.com/671488/248966-Software-Optimization-Manual-V1-048.pdf)</sup> 通过内存传递数据的代码模式——如寄存器溢出与栈帧中的保存、参数传递，以及全局变量和易失变量——正是存储可能需要与加载分开的情形。<sup>[11](https://cdrdv2-public.intel.com/671488/248966-Software-Optimization-Manual-V1-048.pdf)</sup>

**厂商预测器。** 英特尔记录了两个不同的预测器：内存消歧预测器，猜测地址是否重叠；以及快速存储转发预测器，让加载在所有转发条件（如地址匹配）解决之前就推测性地使用前一条存储的数据。<sup>[1](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)</sup> AMD 的“Zen 3”引入了预测性存储转发，被描述为一种基于硬件的微架构优化，通过预测加载与存储之间的依赖来提升执行性能。<sup>[7](https://www.amd.com/en/blogs/2021/predictive-store-forwarding.html)</sup>

## 推测性存储绕过

对内存依赖的推测是有安全代价的。如果处理器预测某条加载不与一条地址仍未知的更早存储重叠，而预测是错的，该加载就会读到陈旧数据。处理器随后重新执行该加载以得到正确结果，但瞬时执行的指令可能已经留下了可测量的痕迹。<sup>[1](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)</sup> 这就是推测性存储绕过攻击：攻击者安排一个陈旧的指针值被推测性地解引用，造成一次以下标受攻击者影响的方式进行的数组访问，然后分析缓存的变化以推断内存内容的信息。<sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/speculative-execution-side-channel-mitigations.html)</sup>

英特尔给出的示例把一个对象的地址存入一个指针，再解引用该指针去索引一个数组。在推测之下，指针的加载最初取到旧值，数组以错误的下标被推测性访问，CPU 随后以正确的下标重新执行该加载；推测性访问造成的缓存变化可被攻击者分析。<sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/speculative-execution-side-channel-mitigations.html)</sup>

## 缓解措施

**软件屏障。** 在一条存储与其后一条依赖它的加载之间插入 LFENCE，可阻止该加载在存储地址已知之前执行；屏障也可以放在加载与其数据的任何可能产生侧信道的使用之间。英特尔建议仅在狭窄范围内采用此措施：攻击者能够控制旧的内存值、该加载确实可能在存储地址已知之前执行、且存在泄露小工具（disclosure gadget）。<sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/speculative-execution-side-channel-mitigations.html)</sup>

**SSBD。** 当软件缓解不可行时，置位于 IA32_SPEC_CTRL[2] 的推测性存储绕过禁用位可阻止加载在同一逻辑处理器上所有更早存储的地址已知之前推测执行。<sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/speculative-execution-side-channel-mitigations.html)</sup> 其支持情况由 CPUID 叶 07H 的 EDX 第 31 位枚举，置位该位的处理器支持 IA32_SPEC_CTRL MSR；支持也可通过微码更新到来，之后软件应重新评估枚举结果。<sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/speculative-execution-side-channel-mitigations.html)</sup> 飞地（Enclave）与 SMM 代码无论该 MSR 位的实际值如何，都表现得如同 SSBD 已置位。<sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/speculative-execution-side-channel-mitigations.html)</sup>

由于 SSBD 会降低性能，英特尔建议只为托管运行时及其他依赖语言级安全的软件置位，而不为操作系统、虚拟机监视器或不依赖此类安全的应用程序置位；将秘密隔离到单独的地址空间（例如使用保护密钥）是另一种选择。<sup>[12](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/analysis-speculative-execution-side-channels.html)</sup> 在启用了超线程、且不具备增强型 IBRS 的英特尔酷睿与至强处理器上，在一个逻辑处理器上置位 SSBD 可能影响同一核心上其兄弟逻辑处理器的性能。<sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/speculative-execution-side-channel-mitigations.html)</sup> 具体到快速存储转发预测器，英特尔指出，PSFD（用于禁用 FSFP）可能令那些关注同域 FSFP 攻击、但不关注推测性存储绕过、且在意 SSBD 性能代价的环境感兴趣。<sup>[1](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)</sup>

## 编译器端的消歧

同一个问题由编译器静态地回答。[别名分析](https://www.edgepedia.org/zh/alias-analysis)识别某个内存位置是否可以以不止一种方式访问，依赖分析判定各次访问是否可能在时间上重叠；内存消歧被描述为优化编译器最重要的分析技术之一。<sup>[13](https://doi.org/10.1145/3771775.3786270)</sup><sup> • </sup><sup>[14](https://agassiz.cs.umn.edu/Paper/tchen.ipoc.pdf)</sup> 静态分析因缺乏运行时信息而必须保守，这限制了编译器对内存操作进行重排或推测的激进程度。<sup>[2](https://conservancy.umn.edu/items/f08606f4-5752-4d4b-9b0f-88f04c85e90a)</sup> 在体系结构提供支持之处，编译器可以显式地推测：安腾处理器通过高级加载地址表（Advanced Load Address Table）特性支持数据推测。<sup>[14](https://agassiz.cs.umn.edu/Paper/tchen.ipoc.pdf)</sup>

消歧还延伸到跨线程。[事务内存](https://www.edgepedia.org/zh/transactional-memory)、线程级推测以及带检查点的多处理器都需要机制来跨线程消歧地址、使陈旧的缓存状态失效，并让已提交状态可见。Bulk 方法将线程的访问信息哈希编码为简洁的签名，并支持处理地址集合的硬件签名操作；这些操作不精确但正确，曾用 SPECint2000 代码和多线程 Java 工作负载进行评估，性能与更复杂的方案相比具有竞争力。<sup>[15](https://research.ibm.com/publications/bulk-disambiguation-of-speculative-threads-in-multiprocessors)</sup>

## References

1. [Hardware Features and Behaviors Related to Speculative Execution, Intel](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/hardware-behavior-related-to-speculative-execution.html)
2. [Memory profiling and management, University of Minnesota Conservancy](https://conservancy.umn.edu/items/f08606f4-5752-4d4b-9b0f-88f04c85e90a)
3. [dblp: Memory Dependence Prediction Using Store Sets (Chrysos & Emer, ISCA 1998)](https://dblp.uni-trier.de/rec/conf/isca/ChrysosE98.html)
4. [Memory Dependence Prediction using Store Sets, Chrysos & Emer, ISCA 1998](https://people.csail.mit.edu/emer/media/papers/1998.06.isca.storesets.pdf)
5. [Store Forwarding, Intel Architecture IA-32 Reference Manual](https://www.manualslib.com/manual/77668/Intel-Architecture-Ia-32.html?page=53)
6. [Speculative Execution Side Channel Mitigations, Intel](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/speculative-execution-side-channel-mitigations.html)
7. [Predictive Store Forwarding, AMD Developer Blog (2021)](https://www.amd.com/en/blogs/2021/predictive-store-forwarding.html)
8. [Memory disambiguation hardware: a Review, Journal of Computer Science and Technology](https://journal.info.unlp.edu.ar/JCST/article/view/754)
9. [Store-set Memory Disambiguator, UC Riverside talk slides](https://www.cs.ucr.edu/~gupta/research/Talks/PATH_TALK/tsld031.htm)
10. [Memory Dependence Prediction using Store Sets, ResearchGate](https://www.researchgate.net/publication/2881874_Memory_Dependence_Prediction_using_Store_Sets)
11. [Intel 64 and IA-32 Architectures Optimization Reference Manual, Volume 1](https://cdrdv2-public.intel.com/671488/248966-Software-Optimization-Manual-V1-048.pdf)
12. [Intel Analysis of Speculative Execution Side Channels](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/analysis-speculative-execution-side-channels.html)
13. [HORIZON: Estimating Alias Analysis Precision Bounds and Their Impact on Performance, CC 2026](https://doi.org/10.1145/3771775.3786270)
14. [Alias and dependence profiling in ORC and their applications](https://agassiz.cs.umn.edu/Paper/tchen.ipoc.pdf)
15. [Bulk disambiguation of speculative threads in multiprocessors, IBM Research, ISCA 2006](https://research.ibm.com/publications/bulk-disambiguation-of-speculative-threads-in-multiprocessors)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Processors & processor engineering › Computer architecture theory › Pipelining and instruction-level parallelism

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/memory-disambiguation

English original: https://www.edgepedia.org/memory-disambiguation
