xur-std 0.1.2

The standard library of the Xur language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! `std::re::asm::x86`: x86/x64 disassembly and assembly, backed by
//! [iced-x86](https://docs.rs/iced-x86).
//!
//! ```text
//! let insns = std::re::asm::x86::decode(64, bytes, 0x1000);
//! for insn in insns { io::println("{}", insn); }
//! let bytes = std::re::asm::x86::encode(64, insns, 0x1000).unwrap();
//! ```

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, Registry, RunResult, RuntimeError, Value};

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()
});

/// The wrapped instruction — `Clone` + `Facet` so it can live as a native
/// value. `Display` delegates to iced-x86's MASM formatter so it shows
/// as readable assembly when printed.
#[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.raw_function("decode", decode)
        .raw_function("encode", encode)
        .raw_function("format", format_insn)
        .raw_function("insn", create_insn)
        .raw_function("mem", create_mem);

    register_insn(&mut x86);
}

// ── decode ──────────────────────────────────────────────────────────

/// `x86::decode(bitness, bytes, ip?)` → list of `Instruction`.
///
/// `bitness` is 16, 32 or 64. `ip` defaults to 0.
fn decode(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
    let (bitness, data, ip) = match args {
        [Value::Int(b), Value::Bytes(data)] => (*b, data, 0u64),
        [Value::Int(b), Value::Bytes(data), Value::Int(ip)] => (*b, data, *ip as u64),
        _ => {
            return Err(RuntimeError::new(
                "`decode` takes (bitness, bytes) or (bitness, bytes, ip)",
            ));
        }
    };
    let bitness = validate_bitness(bitness)?;
    let bytes = xur_runtime::lock(data);
    let decoder = Decoder::with_ip(bitness, &bytes, ip, DecoderOptions::NONE);
    let mut list = Vec::new();
    for insn in decoder {
        list.push(into_value(cx, Insn(insn))?);
    }
    Ok(Value::list(list))
}

// ── encode ──────────────────────────────────────────────────────────

/// `x86::encode(bitness, instructions, ip?)` → `Result<bytes, str>`.
///
/// Re-encodes the instructions at the given `ip` (default 0).
fn encode(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
    let (bitness, list, ip) = match args {
        [Value::Int(b), Value::List(list)] => (*b, list, 0u64),
        [Value::Int(b), Value::List(list), Value::Int(ip)] => (*b, list, *ip as u64),
        _ => {
            return Err(RuntimeError::new(
                "`encode` takes (bitness, instructions) or (bitness, instructions, ip)",
            ));
        }
    };
    let bitness = validate_bitness(bitness)?;
    let items = xur_runtime::lock(list);
    let mut encoder = Encoder::new(bitness);
    let mut rip = ip;
    for value in items.iter() {
        let insn = extract_insn(value)?;
        match encoder.encode(&insn.0, rip) {
            Ok(len) => rip += len as u64,
            Err(e) => return Ok(crate::result(cx, Err(error_text(e)))),
        }
    }
    Ok(crate::result(cx, Ok(Value::bytes(encoder.take_buffer()))))
}

// ── format ──────────────────────────────────────────────────────────

/// `x86::format(instruction, syntax?)` → string.
///
/// `syntax` is `"intel"`, `"nasm"`, `"masm"` (default), or `"gas"`.
fn format_insn(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
    let (value, syntax) = match args {
        [value] => (value, None),
        [value, Value::Str(s)] => (value, Some(s.clone())),
        _ => {
            return Err(RuntimeError::new(
                "`format` takes (instruction) or (instruction, syntax)",
            ));
        }
    };
    let insn = extract_insn(value)?;
    let _ = cx;
    let text = format_with(syntax.as_deref().unwrap_or("masm"), &insn.0)?;
    Ok(Value::str(&text))
}

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)
}

// ── Instruction type ────────────────────────────────────────────────

fn register_insn(x86: &mut ModuleBuilder<'_>) {
    x86.ty::<Insn>("Instruction")
        // ── address & size ──
        .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())
        // ── opcode ──
        .method("mnemonic", |insn: &Insn| format!("{:?}", insn.0.mnemonic()))
        .method("code", |insn: &Insn| format!("{:?}", insn.0.code()))
        // ── operands ──
        .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()
        })
        // ── memory operand ──
        .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()
        })
        // ── control flow ──
        .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()
        })
        // ── prefixes ──
        .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())
        })
        // ── formatting ──
        .raw_function("format", |cx, args| {
            let (this, syntax) = match args {
                [this] => (this, None),
                [this, Value::Str(s)] => (this, Some(s.clone())),
                _ => {
                    return Err(RuntimeError::new(
                        "`format` takes an optional syntax string",
                    ));
                }
            };
            let insn = extract_insn(this)?;
            let _ = cx;
            let text = format_with(syntax.as_deref().unwrap_or("masm"), &insn.0)?;
            Ok(Value::str(&text))
        });
}

// ── instruction construction ────────────────────────────────────────

/// `x86::insn(code, ops...)` → `Result<Instruction, str>`.
///
/// Builds an instruction from a `Code` name and 0–4 operands.
/// Operands can be register names (strings like `"RAX"`), integers, or
/// memory operands built with `x86::mem(...)`.
fn create_insn(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
    let (code_name, ops) = match args.split_first() {
        Some((Value::Str(name), rest)) => (name, rest),
        _ => {
            return Err(RuntimeError::new(
                "`insn` takes a Code name and 0–4 operands",
            ));
        }
    };
    let code = parse_code(code_name)?;
    let insn = match ops.len() {
        0 => Ok(Instruction::with(code)),
        1 => build1(code, &ops[0]),
        2 => build2(code, &ops[0], &ops[1]),
        3 => build3(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))),
    }
}

/// A memory operand wrapper for passing through Xur.
#[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, "]")
    }
}

/// `x86::mem(base)` or `x86::mem(base, index, scale, displacement)`.
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)
        }
        [
            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 as u32,
            *disp,
            1,
        ),
        _ => {
            return Err(RuntimeError::new(
                "`mem` takes (base), (base, disp), or (base, index, scale, disp)",
            ));
        }
    };
    into_value(cx, MemOp(mem))
}

/// An operand as seen by the instruction builder.
enum Operand {
    Reg(IcedRegister),
    Imm(i64),
    Mem(MemoryOperand),
}

fn parse_operand(value: &Value) -> RunResult<Operand> {
    match value {
        Value::Str(s) => Ok(Operand::Reg(parse_reg(s)?)),
        Value::Int(n) => Ok(Operand::Imm(*n)),
        Value::Native(obj) => {
            if let Some(m) = obj.with(|m: &MemOp| m.0) {
                Ok(Operand::Mem(m))
            } else {
                Err(RuntimeError::new(
                    "operand must be a register name, integer, or memory operand",
                ))
            }
        }
        _ => Err(RuntimeError::new(
            "operand must be a register name, integer, or memory operand",
        )),
    }
}

fn build1(code: Code, op0: &Value) -> RunResult<Instruction> {
    match parse_operand(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(code: Code, op0: &Value, op1: &Value) -> RunResult<Instruction> {
    let a = parse_operand(op0)?;
    let b = parse_operand(op1)?;
    match (a, b) {
        (Operand::Reg(r0), Operand::Reg(r1)) => Instruction::with2(code, r0, r1),
        (Operand::Reg(r), Operand::Imm(n)) => Instruction::with2::<_, i32>(code, r, n as i32),
        (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(code: Code, op0: &Value, op1: &Value, op2: &Value) -> RunResult<Instruction> {
    let a = parse_operand(op0)?;
    let b = parse_operand(op1)?;
    let c = parse_operand(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 parse_reg(name: &str) -> RunResult<IcedRegister> {
    REG_MAP
        .get(&name.to_uppercase())
        .copied()
        .ok_or_else(|| RuntimeError::new(format!("unknown register `{name}`")))
}

// ── helpers ─────────────────────────────────────────────────────────

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}"
        ))),
    }
}

fn extract_insn(value: &Value) -> RunResult<Insn> {
    match value {
        Value::Native(obj) => obj
            .with(|insn: &Insn| insn.clone())
            .ok_or_else(|| RuntimeError::new("expected an x86 Instruction")),
        _ => Err(RuntimeError::new("expected an x86 Instruction")),
    }
}