我在Visual Studio 2008上测试了一些代码并注意到了security_cookie
.我能理解它的重点,但我不明白这条指令的目的是什么.
rep ret /* REP to avoid AMD branch prediction penalty */
Run Code Online (Sandbox Code Playgroud)
当然我可以理解评论:)但是这个前缀exaclty在上下文中做了ret
什么,如果ecx
是!= 0 会发生什么?显然,ecx
当我调试它时,忽略循环计数,这是预期的.
我发现这里的代码在这里(由编译器注入安全性):
void __declspec(naked) __fastcall __security_check_cookie(UINT_PTR cookie)
{
/* x86 version written in asm to preserve all regs */
__asm {
cmp ecx, __security_cookie
jne failure
rep ret /* REP to avoid AMD branch prediction penalty */
failure:
jmp __report_gsfailure
}
}
Run Code Online (Sandbox Code Playgroud) 为了在运行 Windows 7 pro 的英特尔核心 2 上执行一些 cmov 指令,我编写了下面的代码。它所做的只是从控制台获取一个字符串作为输入,应用一些移位操作来生成一个随机种子,然后将该种子传递给 srand,以生成一个小的伪随机数数组。然后评估伪随机数是否满足谓词函数(更任意的 bitshuffling ),并输出“*”或“_”。实验的目的是生成 cmov 指令,但是正如您在下面的反汇编中看到的那样,没有。
关于如何更改代码或 cflags 以便生成它们的任何提示?
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
bool blackBoxPredicate( const unsigned int& ubref ) {
return ((ubref << 6) ^ (ubref >> 2) ^ (~ubref << 2)) % 15 == 0;
}
int main() {
const unsigned int NUM_RINTS = 32;
unsigned int randomSeed = 1;
unsigned int popCount = 0;
unsigned int * rintArray = new unsigned int[NUM_RINTS];
std::string userString;
std::cout …
Run Code Online (Sandbox Code Playgroud) 我知道内核非常使用likely
和unlikely
宏.宏的文档位于内置函数:long __builtin_expect(long exp,long c).但他们并没有真正讨论细节.
究竟如何做一个编译器处理likely(x)
和__builtin_expect((x),1)
?
它是由代码生成器还是优化器处理的?
它取决于优化级别吗?
代码生成的示例是什么?
assembly ×2
x86 ×2
built-in ×1
c ×1
c++ ×1
disassembly ×1
macros ×1
optimization ×1
visual-c++ ×1