From 5b06d7f3a7cb1a01c1665872109d040e0a72c165 Mon Sep 17 00:00:00 2001 From: Evian-Zhang Date: Fri, 9 Aug 2024 15:58:50 +0800 Subject: [PATCH] Force 5-byte JMP in x86 and x86-64 --- src/arch/x86.rs | 7 ++++--- src/arch/x86_64.rs | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/arch/x86.rs b/src/arch/x86.rs index 16bc9ae..e1c4bb1 100644 --- a/src/arch/x86.rs +++ b/src/arch/x86.rs @@ -43,7 +43,8 @@ macro_rules! arch_static_key_init_nop_asm_template { }; } -// The `0x90,0x90,0x90` are three NOPs, which is to make sure the `jmp {0}` is at least 5 bytes long. +// Here we do not use `jmp {0}` because it may be compiled into a 3-byte jmp instead of 5 byte. +// See https://stackoverflow.com/q/74771372/10005095 #[doc(hidden)] #[macro_export] macro_rules! arch_static_key_init_jmp_asm_template { @@ -51,8 +52,8 @@ macro_rules! arch_static_key_init_jmp_asm_template { ::core::concat!( r#" 2: - jmp {0} - .byte 0x90,0x90,0x90 + .byte 0xe9 + .long ({0} - 4) - . .pushsection "#, $crate::os_static_key_sec_name_attr!(), r#" diff --git a/src/arch/x86_64.rs b/src/arch/x86_64.rs index 47e6966..c4ea629 100644 --- a/src/arch/x86_64.rs +++ b/src/arch/x86_64.rs @@ -43,7 +43,8 @@ macro_rules! arch_static_key_init_nop_asm_template { }; } -// The `0x90,0x90,0x90` are three NOPs, which is to make sure the `jmp {0}` is at least 5 bytes long. +// Here we do not use `jmp {0}` because it may be compiled into a 3-byte jmp instead of 5 byte. +// See https://stackoverflow.com/q/74771372/10005095 #[doc(hidden)] #[macro_export] macro_rules! arch_static_key_init_jmp_asm_template {