| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! Fail-closed x86-64 instruction-length decoder used to compute exact | |
| //! PC-relative `pc_bias` (`-(insn_end - disp32_off)`) for RIP-relative | |
| //! operands (`[rip+disp32]`) and `rel32` calls/branches (`e8`/`e9`/`0f 8x`). | |
| #[derive(Clone, Copy, PartialEq, Eq)] | |
| enum PcRelKind { | |
| RipMem, | |
| RelBranch, | |
| } | |
| type PcRelField = (usize, PcRelKind); | |
| /// Compute `pc_bias` (`-4`, `-5`, `-6`, `-8`, ...) for a ModRM RIP-relative | |
| /// memory operand `disp32` at `rel_off` inside `code`. | |
| /// | |
| /// Decodes instructions forward from offset 0 and returns `None` (fail closed) | |
| /// if any instruction before or at `rel_off` is malformed or unrecognized, or | |
| /// if `rel_off` does not coincide with a decoded ModRM `[rip+disp32]` field. | |
| pub fn rip_rel_pc_bias(code: &[u8], rel_off: usize) -> Option<i64> { | |
| decode_pc_rel_bias(code, rel_off, false) | |
| } | |
| /// Compute `pc_bias` for any x86-64 PC-relative 32-bit relocation at `rel_off`, | |
| /// covering both ModRM `[rip+disp32]` operands and direct `rel32` calls/jumps | |
| /// (`e8`, `e9`, `0f 80..=8f`). Fails closed (`None`) on non-PC-relative or | |
| /// undecoded instructions. | |
| pub fn pc_rel_disp32_bias(code: &[u8], rel_off: usize) -> Option<i64> { | |
| decode_pc_rel_bias(code, rel_off, true) | |
| } | |
| fn decode_pc_rel_bias(code: &[u8], rel_off: usize, allow_rel_branch: bool) -> Option<i64> { | |
| let mut pc = 0usize; | |
| while pc < code.len() && pc <= rel_off { | |
| let (next_pc, pc_rel_field) = decode_one(code, pc)?; | |
| if next_pc <= pc || next_pc - pc > 15 { | |
| return None; | |
| } | |
| if let Some((disp_off, kind)) = pc_rel_field { | |
| if disp_off == rel_off && (kind == PcRelKind::RipMem || allow_rel_branch) { | |
| let tail = next_pc.checked_sub(rel_off)?; | |
| return i64::try_from(tail).ok().and_then(i64::checked_neg); | |
| } | |
| } | |
| if rel_off >= pc && rel_off < next_pc { | |
| return None; | |
| } | |
| pc = next_pc; | |
| } | |
| None | |
| } | |
| fn decode_one(code: &[u8], start: usize) -> Option<(usize, Option<PcRelField>)> { | |
| let mut pos = start; | |
| let mut has_66 = false; | |
| let mut rex_w = false; | |
| // Legacy prefixes (reject 0x67 address-size override fail-closed). | |
| loop { | |
| let b = *code.get(pos)?; | |
| match b { | |
| 0x66 => { | |
| has_66 = true; | |
| pos += 1; | |
| } | |
| 0xf0 | 0xf2 | 0xf3 | 0x26 | 0x2e | 0x36 | 0x3e | 0x64 | 0x65 => { | |
| pos += 1; | |
| } | |
| 0x67 => return None, | |
| _ => break, | |
| } | |
| if pos - start >= 15 { | |
| return None; | |
| } | |
| } | |
| // Optional REX prefix (0x40..=0x4f). | |
| let b0 = *code.get(pos)?; | |
| if (0x40..=0x4f).contains(&b0) { | |
| rex_w = (b0 & 0x08) != 0; | |
| pos += 1; | |
| } | |
| let op = *code.get(pos)?; | |
| // VEX 2-byte (`c5`) and 3-byte (`c4`) prefixes cannot follow legacy/REX. | |
| if pos == start && (op == 0xc5 || op == 0xc4) { | |
| if op == 0xc5 { | |
| // 2-byte VEX implies 0x0f map. | |
| let _v1 = *code.get(pos + 1)?; | |
| let vop = *code.get(pos + 2)?; | |
| pos += 3; | |
| return decode_0f_opcode(code, pos, vop, true, false); | |
| } | |
| if op == 0xc4 { | |
| let v1 = *code.get(pos + 1)?; | |
| let _v2 = *code.get(pos + 2)?; | |
| let vop = *code.get(pos + 3)?; | |
| pos += 4; | |
| return match v1 & 0x1f { | |
| 1 => decode_0f_opcode(code, pos, vop, true, false), | |
| 2 => { | |
| let (p, rip, _) = decode_modrm(code, pos)?; | |
| Some((p, rip)) | |
| } | |
| 3 => { | |
| let (p, rip, _) = decode_modrm(code, pos)?; | |
| let end = p.checked_add(1)?; | |
| (end <= code.len()).then_some((end, rip)) | |
| } | |
| _ => None, | |
| }; | |
| } | |
| } | |
| pos += 1; | |
| let imm_z = if has_66 && !rex_w { 2 } else { 4 }; | |
| let imm_v = if rex_w { | |
| 8 | |
| } else if has_66 { | |
| 2 | |
| } else { | |
| 4 | |
| }; | |
| if op == 0x0f { | |
| let op2 = *code.get(pos)?; | |
| pos += 1; | |
| return decode_0f_opcode(code, pos, op2, false, has_66); | |
| } | |
| match op { | |
| // Zero-operand / register-encoded 1-byte opcodes. | |
| 0x50..=0x5f | |
| | 0x90..=0x9f | |
| | 0xa4..=0xa7 | |
| | 0xaa..=0xaf | |
| | 0xc3 | |
| | 0xc9 | |
| | 0xcc | |
| | 0xf4 | |
| | 0xf8..=0xfd => Some((pos, None)), | |
| // 1-byte immediate / rel8 without ModRM. | |
| 0x04 | |
| | 0x0c | |
| | 0x14 | |
| | 0x1c | |
| | 0x24 | |
| | 0x2c | |
| | 0x34 | |
| | 0x3c | |
| | 0x6a | |
| | 0x70..=0x7f | |
| | 0xa8 | |
| | 0xb0..=0xb7 | |
| | 0xcd | |
| | 0xeb => add_imm(code, pos, 1, None), | |
| // 2-byte immediate (`ret imm16`). | |
| 0xc2 => add_imm(code, pos, 2, None), | |
| // `imm_z` without ModRM. | |
| 0x05 | 0x0d | 0x15 | 0x1d | 0x25 | 0x2d | 0x35 | 0x3d | 0x68 | 0xa9 => { | |
| add_imm(code, pos, imm_z, None) | |
| } | |
| // Direct `call rel32` / `jmp rel32`: `0x66` prefix length is | |
| // vendor-dependent (Intel64 rel16 vs AMD64 rel32), so fail closed. | |
| 0xe8 | 0xe9 => { | |
| if has_66 { | |
| None | |
| } else { | |
| add_imm(code, pos, 4, Some((pos, PcRelKind::RelBranch))) | |
| } | |
| } | |
| // `mov r, imm16/32/64`. | |
| 0xb8..=0xbf => add_imm(code, pos, imm_v, None), | |
| // ModRM with no immediate. | |
| 0x00..=0x03 | |
| | 0x08..=0x0b | |
| | 0x10..=0x13 | |
| | 0x18..=0x1b | |
| | 0x20..=0x23 | |
| | 0x28..=0x2b | |
| | 0x30..=0x33 | |
| | 0x38..=0x3b | |
| | 0x63 | |
| | 0x84..=0x8b | |
| | 0x8d | |
| | 0x8f | |
| | 0xd0..=0xd3 | |
| | 0xfe | |
| | 0xff => { | |
| let (p, rip, _) = decode_modrm(code, pos)?; | |
| Some((p, rip)) | |
| } | |
| // ModRM + 1-byte immediate (`cmpb $imm8, mem(%rip)` = 0x80 /7). | |
| 0x6b | 0x80 | 0x83 | 0xc0 | 0xc1 | 0xc6 => { | |
| let (p, rip, _) = decode_modrm(code, pos)?; | |
| add_imm(code, p, 1, rip) | |
| } | |
| // ModRM + `imm_z` (`cmpl $imm32, mem(%rip)` = 0x81 /7). | |
| 0x69 | 0x81 | 0xc7 => { | |
| let (p, rip, _) = decode_modrm(code, pos)?; | |
| add_imm(code, p, imm_z, rip) | |
| } | |
| // Group 3 (`test` /0,/1 take immediate; /2../7 take none). | |
| 0xf6 => { | |
| let (p, rip, reg) = decode_modrm(code, pos)?; | |
| let imm = if reg <= 1 { 1 } else { 0 }; | |
| add_imm(code, p, imm, rip) | |
| } | |
| 0xf7 => { | |
| let (p, rip, reg) = decode_modrm(code, pos)?; | |
| let imm = if reg <= 1 { imm_z } else { 0 }; | |
| add_imm(code, p, imm, rip) | |
| } | |
| _ => None, | |
| } | |
| } | |
| fn decode_0f_opcode( | |
| code: &[u8], | |
| mut pos: usize, | |
| op2: u8, | |
| is_vex: bool, | |
| has_66: bool, | |
| ) -> Option<(usize, Option<PcRelField>)> { | |
| if !is_vex && op2 == 0x38 { | |
| let _op3 = *code.get(pos)?; | |
| pos += 1; | |
| let (p, rip, _) = decode_modrm(code, pos)?; | |
| return Some((p, rip)); | |
| } | |
| if !is_vex && op2 == 0x3a { | |
| let _op3 = *code.get(pos)?; | |
| pos += 1; | |
| let (p, rip, _) = decode_modrm(code, pos)?; | |
| return add_imm(code, p, 1, rip); | |
| } | |
| match op2 { | |
| // Zero-operand 0x0f opcodes (`endbr64` = f3 0f 1e fa uses ModRM). | |
| 0x05 | 0x0b | 0x31 | 0x34 | 0xa0 | 0xa1 | 0xa8 | 0xa9 | 0xc8..=0xcf if !is_vex => { | |
| Some((pos, None)) | |
| } | |
| // Conditional `jcc rel32` (fail closed on vendor-dependent `0x66`). | |
| 0x80..=0x8f if !is_vex => { | |
| if has_66 { | |
| None | |
| } else { | |
| add_imm(code, pos, 4, Some((pos, PcRelKind::RelBranch))) | |
| } | |
| } | |
| // ModRM + 0 immediate. | |
| 0x10..=0x1f | |
| | 0x28..=0x2f | |
| | 0x40..=0x4f | |
| | 0x51..=0x6f | |
| | 0x74..=0x76 | |
| | 0x7e | |
| | 0x7f | |
| | 0x90..=0x9f | |
| | 0xa3 | |
| | 0xa5 | |
| | 0xab | |
| | 0xad | |
| | 0xaf | |
| | 0xb0..=0xb7 | |
| | 0xbb..=0xbf | |
| | 0xc0 | |
| | 0xc1 | |
| | 0xd0..=0xff => { | |
| let (p, rip, _) = decode_modrm(code, pos)?; | |
| Some((p, rip)) | |
| } | |
| // ModRM + 1-byte immediate. | |
| 0x70..=0x73 | 0xa4 | 0xac | 0xba | 0xc2 | 0xc4..=0xc6 => { | |
| let (p, rip, _) = decode_modrm(code, pos)?; | |
| add_imm(code, p, 1, rip) | |
| } | |
| _ => None, | |
| } | |
| } | |
| fn decode_modrm(code: &[u8], pos: usize) -> Option<(usize, Option<PcRelField>, u8)> { | |
| let modrm = *code.get(pos)?; | |
| let mut p = pos + 1; | |
| let mod_bits = modrm >> 6; | |
| let reg = (modrm >> 3) & 0x7; | |
| let rm = modrm & 0x7; | |
| if mod_bits == 3 { | |
| return Some((p, None, reg)); | |
| } | |
| let mut sib_base = None; | |
| if rm == 4 { | |
| let sib = *code.get(p)?; | |
| p += 1; | |
| sib_base = Some(sib & 0x7); | |
| } | |
| let (disp_len, rip_off) = match mod_bits { | |
| 0 => { | |
| if rm == 5 { | |
| (4, Some((p, PcRelKind::RipMem))) | |
| } else if sib_base == Some(5) { | |
| (4, None) | |
| } else { | |
| (0, None) | |
| } | |
| } | |
| 1 => (1, None), | |
| 2 => (4, None), | |
| _ => unreachable!(), | |
| }; | |
| let end = p.checked_add(disp_len)?; | |
| (end <= code.len()).then_some((end, rip_off, reg)) | |
| } | |
| fn add_imm( | |
| code: &[u8], | |
| pos: usize, | |
| imm_len: usize, | |
| rip_off: Option<PcRelField>, | |
| ) -> Option<(usize, Option<PcRelField>)> { | |
| let end = pos.checked_add(imm_len)?; | |
| (end <= code.len()).then_some((end, rip_off)) | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::{pc_rel_disp32_bias, rip_rel_pc_bias}; | |
| #[test] | |
| fn decodes_rip_rel_pc_bias_and_fails_closed() { | |
| // `lea rax, [rip+disp32]; ret` -> disp32 at +3, bias -4. | |
| let lea = [0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| assert_eq!(rip_rel_pc_bias(&lea, 3), Some(-4)); | |
| assert_eq!(pc_rel_disp32_bias(&lea, 3), Some(-4)); | |
| // `cmpb $1, [rip+disp32]; ret` -> disp32 at +2, trailing imm8 -> -5. | |
| let cmpb = [0x80, 0x3d, 0, 0, 0, 0, 0x01, 0xc3]; | |
| assert_eq!(rip_rel_pc_bias(&cmpb, 2), Some(-5)); | |
| assert_eq!(pc_rel_disp32_bias(&cmpb, 2), Some(-5)); | |
| // `cmpl $0x12345678, [rip+disp32]; ret` -> trailing imm32 -> -8. | |
| let cmpl = [0x81, 0x3d, 0, 0, 0, 0, 0x78, 0x56, 0x34, 0x12, 0xc3]; | |
| assert_eq!(rip_rel_pc_bias(&cmpl, 2), Some(-8)); | |
| // Direct call/jmp/jcc rel32 decode for pc_rel_disp32_bias only. | |
| let call = [0xe8, 0, 0, 0, 0, 0xc3]; | |
| assert_eq!(pc_rel_disp32_bias(&call, 1), Some(-4)); | |
| assert_eq!(rip_rel_pc_bias(&call, 1), None); | |
| let jcc = [0x0f, 0x84, 0, 0, 0, 0, 0xc3]; | |
| assert_eq!(pc_rel_disp32_bias(&jcc, 2), Some(-4)); | |
| assert_eq!(rip_rel_pc_bias(&jcc, 2), None); | |
| // Non-PC-relative `mov eax, imm32` and undecoded `cpuid` fail closed. | |
| let mov_imm = [0xb8, 0, 0, 0, 0, 0xc3]; | |
| assert_eq!(pc_rel_disp32_bias(&mov_imm, 1), None); | |
| let cpuid_cmpb = [0x0f, 0xa2, 0x80, 0x3d, 0, 0, 0, 0, 0x01, 0xc3]; | |
| assert_eq!(pc_rel_disp32_bias(&cpuid_cmpb, 4), None); | |
| // Misaligned offset inside instruction fails closed. | |
| assert_eq!(rip_rel_pc_bias(&lea, 2), None); | |
| // 0x67 address-size override and 0x66-prefixed branches fail closed. | |
| let addr_override = [0x67, 0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| assert_eq!(rip_rel_pc_bias(&addr_override, 4), None); | |
| assert_eq!(pc_rel_disp32_bias(&[0x66, 0xe8, 0, 0, 0, 0, 0xc3], 2), None); | |
| assert_eq!( | |
| pc_rel_disp32_bias(&[0x66, 0x48, 0xe8, 0, 0, 0, 0, 0x48, 0x8d, 0x05, 0, 0, 0, 0], 10), | |
| None | |
| ); | |
| assert_eq!(pc_rel_disp32_bias(&[0x66, 0x0f, 0x84, 0, 0, 0, 0, 0xc3], 3), None); | |
| } | |
| } |