- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我喜欢例子,所以我用c写了一些自修改代码...
#include <stdio.h>
#include <sys/mman.h> // linux
int main(void) {
unsigned char *c = mmap(NULL, 7, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|
MAP_ANONYMOUS, -1, 0); // get executable memory
c[0] = 0b11000111; // mov (x86_64), immediate mode, full-sized (32 bits)
c[1] = 0b11000000; // to register rax (000) which holds the return value
// according to linux x86_64 calling convention
c[6] = 0b11000011; // return
for (c[2] = 0; c[2] < 30; c[2]++) { // incr immediate data after every run
// rest of immediate data (c[3:6]) are already set to 0 by MAP_ANONYMOUS
printf("%d ", ((int (*)(void)) c)()); // cast c to func ptr, call ptr
}
putchar('\n');
return 0;
}
...显然有效:
>>> gcc -Wall -Wextra -std=c11 -D_GNU_SOURCE -o test test.c; ./test
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
但老实说,我根本没有期望它会起作用。我希望包含 c[2] = 0
的指令在第一次调用 c
时被缓存,之后所有对 c
的连续调用都会忽略对 c
所做的重复更改(除非我以某种方式明确地使缓存无效)。幸运的是,我的 CPU 似乎比这更聪明。
我猜想每当指令指针进行较大的跳转(就像上面对映射内存的调用一样)时,cpu 会将 RAM(假设 c
甚至驻留在 RAM 中)与指令缓存进行比较,并在不匹配时使缓存无效(全部?),但我希望获得更准确的信息。特别是,我想知道这种行为是否可以被认为是可预测的(除了硬件和操作系统的任何差异),并且可以依赖?
(我可能应该引用英特尔手册,但那东西有数千页那么长,我往往会迷失在其中......)
最佳答案
您所做的通常称为自修改代码。 Intel 的平台(可能还有 AMD 的)会为您完成维护i/d 缓存一致性的工作,正如手册中指出的那样 (Manual 3A, System Programming)
11.6 SELF-MODIFYING CODE
A write to a memory location in a code segment that is currently cached in theprocessor causes the associated cache line (or lines) to be invalidated.
但是只要相同的线性地址用于修改和获取,这个断言就有效,调试器和二进制加载器不是这种情况,因为它们不t 在相同的地址空间中运行:
Applications that include self-modifying code use the samelinear address for modifying and fetching the instruction. Systems software, such asa debugger, that might possibly modify an instruction using a different linear addressthan that used to fetch the instruction, will execute a serializing operation, such as aCPUID instruction, before the modified instruction is executed, which will automaticallyresynchronize the instruction cache and prefetch queue.
例如,许多其他体系结构(例如 PowerPC)总是请求序列化操作,必须明确地完成(E500 Core Manual):
3.3.1.2.1 Self-Modifying Code
When a processor modifies any memory location that can contain an instruction, software mustensure that the instruction cache is made consistent with data memory and that the modificationsare made visible to the instruction fetching mechanism. This must be done even if the cache isdisabled or if the page is marked caching-inhibited.
值得注意的是,即使缓存被禁用,PowerPC 也需要发出上下文同步指令;我怀疑它会强制刷新更深层次的数据处理单元,例如加载/存储缓冲区。
您提出的代码在没有监听 或高级缓存一致性 设施的架构上是不可靠的,因此很可能会失败。
希望这对您有所帮助。
关于c - x86指令缓存是如何同步的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10989403/
问题故障解决记录 -- Java RMI Connection refused to host: x.x.x.x .... 在学习JavaRMI时,我遇到了以下情况 问题原因:可
我正在玩 Rank-N-type 并尝试输入 x x .但我发现这两个函数可以以相同的方式输入,这很不直观。 f :: (forall a b. a -> b) -> c f x = x x g ::
这个问题已经有答案了: How do you compare two version Strings in Java? (31 个回答) 已关闭 8 年前。 有谁知道如何在Java中比较两个版本字符串
这个问题已经有答案了: How do the post increment (i++) and pre increment (++i) operators work in Java? (14 个回答)
下面是带有 -n 和 -r 选项的 netstat 命令的输出,其中目标字段显示压缩地址 (127.1/16)。我想知道 netstat 命令是否有任何方法或选项可以显示整个目标 IP (127.1.
我知道要证明 : (¬ ∀ x, p x) → (∃ x, ¬ p x) 证明是: theorem : (¬ ∀ x, p x) → (∃ x, ¬ p x) := begin intro n
x * x 如何通过将其存储在“auto 变量”中来更改?我认为它应该仍然是相同的,并且我的测试表明类型、大小和值显然都是相同的。 但即使 x * x == (xx = x * x) 也是错误的。什么
假设,我们这样表达: someIQueryable.Where(x => x.SomeBoolProperty) someIQueryable.Where(x => !x.SomeBoolProper
我有一个字符串 1234X5678 我使用这个正则表达式来匹配模式 .X|..X|X. 我得到了 34X 问题是为什么我没有得到 4X 或 X5? 为什么正则表达式选择执行第二种模式? 最佳答案 这里
我的一个 friend 在面试时遇到了这个问题 找到使该函数返回真值的 x 值 function f(x) { return (x++ !== x) && (x++ === x); } 面试官
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicate: Isn't it easier to work with foo when it is represented b
我是 android 的新手,我一直在练习开发一个针对 2.2 版本的应用程序,我需要帮助了解如何将我的应用程序扩展到其他版本,即 1.x、2.3.x、3 .x 和 4.x.x,以及一些针对屏幕分辨率
为什么案例 1 给我们 :error: TypeError: x is undefined on line... //case 1 var x; x.push(x); console.log(x);
代码优先: # CASE 01 def test1(x): x += x print x l = [100] test1(l) print l CASE01 输出: [100, 100
我正在努力温习我的大计算。如果我有将所有项目移至 'i' 2 个空格右侧的函数,我有一个如下所示的公式: (n -1) + (n - 2) + (n - 3) ... (n - n) 第一次迭代我必须
给定 IP 字符串(如 x.x.x.x/x),我如何或将如何计算 IP 的范围最常见的情况可能是 198.162.1.1/24但可以是任何东西,因为法律允许的任何东西。 我要带198.162.1.1/
在我作为初学者努力编写干净的 Javascript 代码时,我最近阅读了 this article当我偶然发现这一段时,关于 JavaScript 中的命名空间: The code at the ve
我正在编写一个脚本,我希望避免污染 DOM 的其余部分,它将是一个用于收集一些基本访问者分析数据的第 3 方脚本。 我通常使用以下内容创建一个伪“命名空间”: var x = x || {}; 我正在
我尝试运行我的test_container_services.py套件,但遇到了以下问题: docker.errors.APIError:500服务器错误:内部服务器错误(“ b'{” message
是否存在这两个 if 语句会产生不同结果的情况? if(x as X != null) { // Do something } if(x is X) { // Do something } 编
我是一名优秀的程序员,十分优秀!