Caching 如何在实践中创建幽灵小工具?
我正在开发(针对ELF64的NASM+GCC)一个使用spectre小工具测量访问一组缓存线()的时间的工具 如何制作可靠的幽灵小工具? 我相信我理解FLUSH+RELOAD技术背后的理论,但是在实践中,尽管有一些噪音,我还是无法产生一个工作的PoCCaching 如何在实践中创建幽灵小工具?,caching,assembly,x86,spectre,Caching,Assembly,X86,Spectre,我正在开发(针对ELF64的NASM+GCC)一个使用spectre小工具测量访问一组缓存线()的时间的工具 如何制作可靠的幽灵小工具? 我相信我理解FLUSH+RELOAD技术背后的理论,但是在实践中,尽管有一些噪音,我还是无法产生一个工作的PoC 由于我使用的是时间戳计数器,并且负载非常正常,因此我使用此脚本禁用预取器、turbo boost并修复/稳定CPU频率: #!/bin/bash sudo modprobe msr #Disable turbo sudo wrmsr -a 0
由于我使用的是时间戳计数器,并且负载非常正常,因此我使用此脚本禁用预取器、turbo boost并修复/稳定CPU频率:
#!/bin/bash
sudo modprobe msr
#Disable turbo
sudo wrmsr -a 0x1a0 0x4000850089
#Disable prefetchers
sudo wrmsr -a 0x1a4 0xf
#Set performance governor
sudo cpupower frequency-set -g performance
#Minimum freq
sudo cpupower frequency-set -d 2.2GHz
#Maximum freq
sudo cpupower frequency-set -u 2.2GHz
我有一个连续的缓冲区,在4KB上对齐,足够大,可以跨越256条缓存线,由整数行间隔分隔
SECTION .bss ALIGN=4096
buffer: resb 256 * (1 + GAP) * 64
我使用此函数刷新256行
flush_all:
lea rdi, [buffer] ;Start pointer
mov esi, 256 ;How many lines to flush
.flush_loop:
lfence ;Prevent the previous clflush to be reordered after the load
mov eax, [rdi] ;Touch the page
lfence ;Prevent the current clflush to be reordered before the load
clflush [rdi] ;Flush a line
add rdi, (1 + GAP)*64 ;Move to the next line
dec esi
jnz .flush_loop ;Repeat
lfence ;clflush are ordered with respect of fences ..
;.. and lfence is ordered (locally) with respect of all instructions
ret
该函数循环遍历所有行,触摸中间的每一页(每一页不止一次)并刷新每一行
然后我使用这个函数来分析访问
profile:
lea rdi, [buffer] ;Pointer to the buffer
mov esi, 256 ;How many lines to test
lea r8, [timings_data] ;Pointer to timings results
mfence ;I'm pretty sure this is useless, but I included it to rule out ..
;.. silly, hard to debug, scenarios
.profile:
mfence
rdtscp
lfence ;Read the TSC in-order (ignoring stores global visibility)
mov ebp, eax ;Read the low DWORD only (this is a short delay)
;PERFORM THE LOADING
mov eax, DWORD [rdi]
rdtscp
lfence ;Again, read the TSC in-order
sub eax, ebp ;Compute the delta
mov DWORD [r8], eax ;Save it
;Advance the loop
add r8, 4 ;Move the results pointer
add rdi, (1 + GAP)*64 ;Move to the next line
dec esi ;Advance the loop
jnz .profile
ret
附录a中给出了MCVE
当装配时,将GAP
设置为0,使用taskset-c0链接并执行时,获取每行所需的周期如下所示
仅从内存加载64行
在不同的运行中,输出是稳定的。
如果我将GAP
设置为1,则仅从内存中提取32行,当然是64*(1+0)*64=32*(1+1)*64=4096,所以这可能与分页有关
如果在对前64行中的一行进行分析之前(但在刷新之后)执行存储,则输出将更改为该行
其他行中的任何存储都提供第一种类型的输出
我怀疑数学是坏的,但我需要再多看几眼,看看在哪里
编辑
在修复输出不一致后,对易失性寄存器的误用。
我经常看到计时较低(~50个周期)的运行,有时看到计时较高(~130个周期)的运行。
我不知道130个周期的数字从何而来(内存太低,缓存太高?)
MCVE(和存储库)中的代码是固定的
如果在分析之前执行了对任何第一行的存储,则输出中不会反映任何更改
附录-MCVE
BITS 64
DEFAULT REL
GLOBAL main
EXTERN printf
EXTERN exit
;Space between lines in the buffer
%define GAP 0
SECTION .bss ALIGN=4096
buffer: resb 256 * (1 + GAP) * 64
SECTION .data
timings_data: TIMES 256 dd 0
strNewLine db `\n0x%02x: `, 0
strHalfLine db " ", 0
strTiming db `\e[48;5;16`,
.importance db "0",
db `m\e[38;5;15m%03u\e[0m `, 0
strEnd db `\n\n`, 0
SECTION .text
;'._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .'
; ' ' ' ' ' ' ' ' ' ' '
; _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \
;/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \
;
;
;FLUSH ALL THE LINES OF A BUFFER FROM THE CACHES
;
;
flush_all:
lea rdi, [buffer] ;Start pointer
mov esi, 256 ;How many lines to flush
.flush_loop:
lfence ;Prevent the previous clflush to be reordered after the load
mov eax, [rdi] ;Touch the page
lfence ;Prevent the current clflush to be reordered before the load
clflush [rdi] ;Flush a line
add rdi, (1 + GAP)*64 ;Move to the next line
dec esi
jnz .flush_loop ;Repeat
lfence ;clflush are ordered with respect of fences ..
;.. and lfence is ordered (locally) with respect of all instructions
ret
;'._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .'
; ' ' ' ' ' ' ' ' ' ' '
; _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \
;/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \
;
;
;PROFILE THE ACCESS TO EVERY LINE OF THE BUFFER
;
;
profile:
lea rdi, [buffer] ;Pointer to the buffer
mov esi, 256 ;How many lines to test
lea r8, [timings_data] ;Pointer to timings results
mfence ;I'm pretty sure this is useless, but I included it to rule out ..
;.. silly, hard to debug, scenarios
.profile:
mfence
rdtscp
lfence ;Read the TSC in-order (ignoring stores global visibility)
mov ebp, eax ;Read the low DWORD only (this is a short delay)
;PERFORM THE LOADING
mov eax, DWORD [rdi]
rdtscp
lfence ;Again, read the TSC in-order
sub eax, ebp ;Compute the delta
mov DWORD [r8], eax ;Save it
;Advance the loop
add r8, 4 ;Move the results pointer
add rdi, (1 + GAP)*64 ;Move to the next line
dec esi ;Advance the loop
jnz .profile
ret
;'._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .'
; ' ' ' ' ' ' ' ' ' ' '
; _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \
;/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \
;
;
;SHOW THE RESULTS
;
;
show_results:
lea rbx, [timings_data] ;Pointer to the timings
xor r12, r12 ;Counter (up to 256)
.print_line:
;Format the output
xor eax, eax
mov esi, r12d
lea rdi, [strNewLine] ;Setup for a call to printf
test r12d, 0fh
jz .print ;Test if counter is a multiple of 16
lea rdi, [strHalfLine] ;Setup for a call to printf
test r12d, 07h ;Test if counter is a multiple of 8
jz .print
.print_timing:
;Print
mov esi, DWORD [rbx] ;Timing value
;Compute the color
mov r10d, 60 ;Used to compute the color
mov eax, esi
xor edx, edx
div r10d ;eax = Timing value / 78
;Update the color
add al, '0'
mov edx, '5'
cmp eax, edx
cmova eax, edx
mov BYTE [strTiming.importance], al
xor eax, eax
lea rdi, [strTiming]
call printf WRT ..plt ;Print a 3-digits number
;Advance the loop
inc r12d ;Increment the counter
add rbx, 4 ;Move to the next timing
cmp r12d, 256
jb .print_line ;Advance the loop
xor eax, eax
lea rdi, [strEnd]
call printf WRT ..plt ;Print a new line
ret
.print:
call printf WRT ..plt ;Print a string
jmp .print_timing
;'._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .'
; ' ' ' ' ' ' ' ' ' ' '
; _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \
;/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \
;
;
;E N T R Y P O I N T
;
;
;'._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .''._ .'
; ' ' ' ' ' ' ' ' ' ' '
; _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \ _' \
;/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \
main:
;Flush all the lines of the buffer
call flush_all
;Test the access times
call profile
;Show the results
call show_results
;Exit
xor edi, edi
call exit WRT ..plt
缓冲区是从bss
部分分配的,因此当加载程序时,操作系统将把所有buffer
缓存线映射到同一个CoW物理页。刷新所有行后,只有对虚拟地址空间中前64行的访问在所有缓存级别1中都会丢失,因为后面的所有2次访问都是对同一4K页的访问。这就是为什么当GAP
为零时,前64次访问的延迟在主存延迟的范围内,所有后续访问的延迟等于L1命中延迟3
当GAP
为1时,将每隔一行访问同一物理页,因此主存访问数(L3未命中)为32(64的一半)。也就是说,前32个延迟将在主内存延迟的范围内,所有后续延迟将为L1命中。类似地,当GAP
为63时,所有访问都指向同一行。因此,只有第一次访问才会错过所有缓存
解决方案是将flush_all
中的mov eax[rdi]
更改为mov dword[rdi],0
,以确保在唯一的物理页中分配缓冲区。(可以删除flush\u all
中的lfence
指令,因为《英特尔手册》规定clflush
不能用写操作重新排序4。)这保证了初始化和刷新所有行后,所有访问都将错过所有缓存级别(但不是TLB,请参阅:)
您可以参考另一个示例,其中CoW页面可能具有欺骗性
在这个答案的前一个版本中,我建议删除对flush_all
的调用,并使用GAP
值63。通过这些更改,所有访问延迟似乎都非常高,我错误地得出结论,所有访问都缺少所有缓存级别。正如我上面所说的,当间隙值为63时,所有的访问都变成同一缓存线,它实际上驻留在一级缓存中。但是,所有延迟都很高的原因是因为每次访问都是到不同的虚拟页面,并且TLB没有这些虚拟页面(到同一物理页面)的任何映射,因为通过删除对flush\u all
的调用,以前没有触及任何虚拟页面。因此,测量的延迟表示TLB未命中延迟,即使正在访问的线路位于一级缓存中
我还错误地在本答案的前一个版本中声称存在无法通过MSR 0x1A4禁用的L3预取逻辑。如果通过在MSR 0x1A4中设置其标志来关闭特定的预取器,则它确实会完全关闭。此外,除Intel记录的数据预取器外,没有其他数据预取器
脚注:
(1) 如果不禁用DCU IP预取器,在刷新它们之后,它实际上会将所有行预取回L1,因此所有访问仍将命中L1
(2) 在极少数情况下,在同一内核上执行中断处理程序或调度其他线程可能会导致从一级缓存层次结构和可能的其他级别逐出一些行
(3) 请记住,您需要减去rdtscp
指令的开销。请注意,您使用的测量方法实际上无法可靠地区分L1命中和L2命中。请参阅:
(4) 英特尔手册似乎没有指定clflush
是否与读取一起订购,但在我看来是这样。在我的系统上,我在div r10d
处遇到了一个浮点异常。原来,printf
是cl