use std::collections::HashMap;
use std::sync::LazyLock;
use iced_x86::{
Code, Decoder, DecoderOptions, Encoder, Formatter as IcedFormatter, GasFormatter, Instruction,
IntelFormatter, MasmFormatter, MemoryOperand, NasmFormatter, Register as IcedRegister,
};
use xur_runtime::convert::into_value;
use xur_runtime::registry::ModuleBuilder;
use xur_runtime::{
Context, OptionalArg, Raise, Registry, RunResult, RuntimeError, Value, XurBytes, XurStr, lock,
};
use crate::error_text;
static CODE_MAP: LazyLock<HashMap<String, Code>> =
LazyLock::new(|| Code::values().map(|c| (format!("{c:?}"), c)).collect());
static REG_MAP: LazyLock<HashMap<String, IcedRegister>> = LazyLock::new(|| {
IcedRegister::values()
.map(|r| {
let name = format!("{r:?}");
(name.to_uppercase(), r)
})
.collect()
});
#[derive(Clone, facet::Facet)]
#[facet(opaque)]
struct Insn(Instruction);
impl std::fmt::Display for Insn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = String::new();
MasmFormatter::new().format(&self.0, &mut output);
f.write_str(&output)
}
}
pub(super) fn register(_registry: &Registry, asm: &mut ModuleBuilder<'_>) {
let mut x86 = asm.submodule("x86");
x86.function(
"decode",
|bitness: i64, data: XurBytes, ip: OptionalArg<i64>| -> Raise<Vec<Insn>> {
Raise(decode_insns(bitness, &data, ip.0))
},
)
.function(
"encode",
|bitness: i64, insns: Vec<Insn>, ip: OptionalArg<i64>| -> Raise<Result<Vec<u8>, String>> {
Raise(encode_insns(bitness, &insns, ip.0))
},
)
.function(
"format",
|insn: Insn, syntax: OptionalArg<XurStr>| -> Raise<String> {
Raise(format_with(syntax.0.as_deref().unwrap_or("masm"), &insn.0))
},
)
.raw_function_returns::<Insn>("insn", create_insn)
.raw_function_returns::<MemOp>("mem", create_mem);
register_insn(&mut x86);
register_enums(&mut x86);
}
fn register_enums(x86: &mut ModuleBuilder<'_>) {
let codes: Vec<(&str, i64)> = CODE_MAP
.iter()
.map(|(name, code)| (name.as_str(), *code as i64))
.collect();
x86.repr_enum("Code", &codes);
let regs: Vec<(&str, i64)> = REG_MAP
.iter()
.map(|(name, reg)| (name.as_str(), *reg as i64))
.collect();
x86.repr_enum("Register", ®s);
}
fn decode_insns(bitness: i64, data: &XurBytes, ip: Option<i64>) -> RunResult<Vec<Insn>> {
let bitness = validate_bitness(bitness)?;
let ip = match ip {
Some(ip) => u64::try_from(ip)
.map_err(|_| RuntimeError::new(format!("{ip} is not a valid address")))?,
None => 0,
};
let bytes = lock(&data.0);
let decoder = Decoder::with_ip(bitness, &bytes, ip, DecoderOptions::NONE);
Ok(decoder.into_iter().map(Insn).collect())
}
fn encode_insns(
bitness: i64,
insns: &[Insn],
ip: Option<i64>,
) -> RunResult<Result<Vec<u8>, String>> {
let bitness = validate_bitness(bitness)?;
let mut rip = match ip {
Some(ip) => u64::try_from(ip)
.map_err(|_| RuntimeError::new(format!("{ip} is not a valid address")))?,
None => 0,
};
let mut encoder = Encoder::new(bitness);
for insn in insns {
match encoder.encode(&insn.0, rip) {
Ok(len) => rip += len as u64,
Err(e) => return Ok(Err(error_text(e))),
}
}
Ok(Ok(encoder.take_buffer()))
}
fn format_with(syntax: &str, insn: &Instruction) -> RunResult<String> {
let mut output = String::new();
match syntax {
"intel" => IntelFormatter::new().format(insn, &mut output),
"nasm" => NasmFormatter::new().format(insn, &mut output),
"masm" => MasmFormatter::new().format(insn, &mut output),
"gas" | "att" => GasFormatter::new().format(insn, &mut output),
_ => {
return Err(RuntimeError::new(format!(
"unknown syntax `{syntax}`; use \"intel\", \"nasm\", \"masm\" or \"gas\""
)));
}
}
Ok(output)
}
fn register_insn(x86: &mut ModuleBuilder<'_>) {
x86.ty::<Insn>("Instruction")
.method("ip", |insn: &Insn| insn.0.ip())
.method("next_ip", |insn: &Insn| insn.0.next_ip())
.method("len", |insn: &Insn| insn.0.len() as u64)
.method("is_invalid", |insn: &Insn| insn.0.is_invalid())
.method("mnemonic", |insn: &Insn| format!("{:?}", insn.0.mnemonic()))
.method("code", |insn: &Insn| format!("{:?}", insn.0.code()))
.method("op_count", |insn: &Insn| insn.0.op_count())
.method("op_kind", |insn: &Insn, n: u32| {
format!("{:?}", insn.0.op_kind(n))
})
.method("op_register", |insn: &Insn, n: u32| {
format!("{:?}", insn.0.op_register(n))
})
.method("immediate", |insn: &Insn, n: u32| insn.0.immediate(n))
.method("near_branch_target", |insn: &Insn| {
insn.0.near_branch_target()
})
.method("memory_base", |insn: &Insn| {
format!("{:?}", insn.0.memory_base())
})
.method("memory_index", |insn: &Insn| {
format!("{:?}", insn.0.memory_index())
})
.method("memory_index_scale", |insn: &Insn| {
insn.0.memory_index_scale()
})
.method("memory_displacement", |insn: &Insn| {
insn.0.memory_displacement64()
})
.method("memory_segment", |insn: &Insn| {
format!("{:?}", insn.0.memory_segment())
})
.method("memory_size", |insn: &Insn| {
format!("{:?}", insn.0.memory_size())
})
.method("is_ip_rel_memory_operand", |insn: &Insn| {
insn.0.is_ip_rel_memory_operand()
})
.method("ip_rel_memory_address", |insn: &Insn| {
insn.0.ip_rel_memory_address()
})
.method("flow_control", |insn: &Insn| {
format!("{:?}", insn.0.flow_control())
})
.method("is_privileged", |insn: &Insn| insn.0.is_privileged())
.method("is_stack_instruction", |insn: &Insn| {
insn.0.is_stack_instruction()
})
.method("stack_pointer_increment", |insn: &Insn| {
insn.0.stack_pointer_increment()
})
.method("has_lock_prefix", |insn: &Insn| insn.0.has_lock_prefix())
.method("has_rep_prefix", |insn: &Insn| insn.0.has_rep_prefix())
.method("has_repe_prefix", |insn: &Insn| insn.0.has_repe_prefix())
.method("has_repne_prefix", |insn: &Insn| insn.0.has_repne_prefix())
.method("segment_prefix", |insn: &Insn| {
format!("{:?}", insn.0.segment_prefix())
})
.method(
"format",
|insn: &Insn, syntax: OptionalArg<XurStr>| -> Raise<String> {
Raise(format_with(syntax.0.as_deref().unwrap_or("masm"), &insn.0))
},
);
}
fn create_insn(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let (code, ops) = match args.split_first() {
Some((Value::Str(name), rest)) => (parse_code(name)?, rest),
Some((Value::Int(n), rest)) => (
int_to_code(
n.to_i64()
.ok_or_else(|| RuntimeError::new(format!("{n} is not a valid Code")))?,
)?,
rest,
),
Some((Value::Adt(adt), rest)) => {
let v = cx
.repr_value(adt.ctor)
.ok_or_else(|| RuntimeError::new("expected a Code enum value"))?;
(int_to_code(v)?, rest)
}
_ => {
return Err(RuntimeError::new(
"`insn` takes a Code (name or enum) and 0–4 operands",
));
}
};
let insn = match ops.len() {
0 => Ok(Instruction::with(code)),
1 => build1(cx, code, &ops[0]),
2 => build2(cx, code, &ops[0], &ops[1]),
3 => build3(cx, code, &ops[0], &ops[1], &ops[2]),
_ => {
return Err(RuntimeError::new(
"`insn` supports at most 3 operands for now",
));
}
};
match insn {
Ok(i) => {
let value = into_value(cx, Insn(i))?;
Ok(crate::result(cx, Ok(value)))
}
Err(e) => Ok(crate::result(cx, Err(e.message))),
}
}
#[derive(Clone, facet::Facet)]
#[facet(opaque)]
struct MemOp(MemoryOperand);
impl std::fmt::Display for MemOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let m = &self.0;
write!(f, "[{:?}", m.base)?;
if m.index != IcedRegister::None {
write!(f, "+{:?}*{}", m.index, m.scale)?;
}
if m.displacement != 0 {
write!(f, "+{:#x}", m.displacement)?;
}
write!(f, "]")
}
}
fn create_mem(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let mem = match args {
[Value::Str(base)] => MemoryOperand::with_base(parse_reg(base)?),
[Value::Str(base), Value::Int(disp)] => MemoryOperand::with_base_displ(
parse_reg(base)?,
disp.to_i64().ok_or_else(|| {
RuntimeError::new(format!("{disp} does not fit in an i64 displacement"))
})?,
),
[
Value::Str(base),
Value::Str(index),
Value::Int(scale),
Value::Int(disp),
] => MemoryOperand::with_base_index_scale_displ_size(
parse_reg(base)?,
parse_reg(index)?,
scale
.to_u32()
.ok_or_else(|| RuntimeError::new(format!("{scale} is not a valid scale")))?,
disp.to_i64().ok_or_else(|| {
RuntimeError::new(format!("{disp} does not fit in an i64 displacement"))
})?,
1,
),
_ => {
return Err(RuntimeError::new(
"`mem` takes (base), (base, disp), or (base, index, scale, disp)",
));
}
};
into_value(cx, MemOp(mem))
}
enum Operand {
Reg(IcedRegister),
Imm(i64),
Mem(MemoryOperand),
}
fn parse_operand(cx: &dyn Context, value: &Value) -> RunResult<Operand> {
match value {
Value::Str(s) => Ok(Operand::Reg(parse_reg(s)?)),
Value::Int(n) => Ok(Operand::Imm(n.to_i64().ok_or_else(|| {
RuntimeError::new(format!("{n} does not fit in an i64 immediate"))
})?)),
Value::Adt(adt) => {
let v = cx
.repr_value(adt.ctor)
.ok_or_else(|| RuntimeError::new("enum operand has no repr value"))?;
Ok(Operand::Reg(int_to_reg(v)?))
}
Value::Native(obj) => {
if let Some(m) = obj.with(|m: &MemOp| m.0) {
return Ok(Operand::Mem(m));
}
Err(RuntimeError::new(
"operand must be a register, integer, or memory operand",
))
}
_ => Err(RuntimeError::new(
"operand must be a register, integer, or memory operand",
)),
}
}
fn build1(cx: &dyn Context, code: Code, op0: &Value) -> RunResult<Instruction> {
match parse_operand(cx, op0)? {
Operand::Reg(r) => Instruction::with1(code, r).map_err(iced_err),
Operand::Imm(n) => Instruction::with1::<i32>(code, n as i32).map_err(iced_err),
Operand::Mem(m) => Instruction::with1(code, m).map_err(iced_err),
}
}
fn build2(cx: &dyn Context, code: Code, op0: &Value, op1: &Value) -> RunResult<Instruction> {
let a = parse_operand(cx, op0)?;
let b = parse_operand(cx, op1)?;
match (a, b) {
(Operand::Reg(r0), Operand::Reg(r1)) => Instruction::with2(code, r0, r1),
(Operand::Reg(r), Operand::Imm(n)) => Instruction::with2::<_, i64>(code, r, n),
(Operand::Reg(r), Operand::Mem(m)) => Instruction::with2(code, r, m),
(Operand::Mem(m), Operand::Reg(r)) => Instruction::with2(code, m, r),
(Operand::Mem(m), Operand::Imm(n)) => Instruction::with2::<_, i32>(code, m, n as i32),
(Operand::Imm(a), Operand::Imm(b)) => {
Instruction::with2::<i32, i32>(code, a as i32, b as i32)
}
(Operand::Imm(n), Operand::Reg(r)) => Instruction::with2::<i32, _>(code, n as i32, r),
_ => return Err(RuntimeError::new("unsupported operand combination")),
}
.map_err(iced_err)
}
fn build3(
cx: &dyn Context,
code: Code,
op0: &Value,
op1: &Value,
op2: &Value,
) -> RunResult<Instruction> {
let a = parse_operand(cx, op0)?;
let b = parse_operand(cx, op1)?;
let c = parse_operand(cx, op2)?;
match (a, b, c) {
(Operand::Reg(r0), Operand::Reg(r1), Operand::Reg(r2)) => {
Instruction::with3(code, r0, r1, r2)
}
(Operand::Reg(r0), Operand::Reg(r1), Operand::Imm(n)) => {
Instruction::with3::<_, _, i32>(code, r0, r1, n as i32)
}
(Operand::Reg(r0), Operand::Mem(m), Operand::Reg(r2)) => {
Instruction::with3(code, r0, m, r2)
}
(Operand::Reg(r0), Operand::Mem(m), Operand::Imm(n)) => {
Instruction::with3::<_, _, i32>(code, r0, m, n as i32)
}
(Operand::Mem(m), Operand::Reg(r1), Operand::Reg(r2)) => {
Instruction::with3(code, m, r1, r2)
}
(Operand::Mem(m), Operand::Reg(r1), Operand::Imm(n)) => {
Instruction::with3::<_, _, i32>(code, m, r1, n as i32)
}
_ => return Err(RuntimeError::new("unsupported 3-operand combination")),
}
.map_err(iced_err)
}
fn iced_err(e: iced_x86::IcedError) -> RuntimeError {
RuntimeError::new(error_text(e))
}
fn parse_code(name: &str) -> RunResult<Code> {
CODE_MAP
.get(name)
.copied()
.ok_or_else(|| RuntimeError::new(format!("unknown Code `{name}`")))
}
fn int_to_code(n: i64) -> RunResult<Code> {
Code::try_from(n as usize).map_err(|_| RuntimeError::new(format!("invalid Code value `{n}`")))
}
fn int_to_reg(n: i64) -> RunResult<IcedRegister> {
IcedRegister::try_from(n as usize)
.map_err(|_| RuntimeError::new(format!("invalid Register value `{n}`")))
}
fn parse_reg(name: &str) -> RunResult<IcedRegister> {
REG_MAP
.get(&name.to_uppercase())
.copied()
.ok_or_else(|| RuntimeError::new(format!("unknown register `{name}`")))
}
fn validate_bitness(b: i64) -> RunResult<u32> {
match b {
16 | 32 | 64 => Ok(b as u32),
_ => Err(RuntimeError::new(format!(
"bitness must be 16, 32 or 64, got {b}"
))),
}
}