asmkit-rs 0.7.2

Portable assembler toolkit for encoding x86/x64, AArch64, and RISC-V
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Post-finalization code patching.
//!
//! # Usage
//!
//! 1. While emitting, the arch `patchable_*` helpers and
//!    [`CodeBuffer::reserve_patch_region`](crate::CodeBuffer::reserve_patch_region)
//!    return marks: [`PatchableJump`], [`DataLabel`] and [`PatchableRegion`].
//! 2. After [`CodeBuffer::finish`](crate::CodeBuffer::finish), turn a mark into a
//!    location in the image with [`CodeBufferFinalized::location_of`] (or
//!    [`CodeBufferFinalized::location_in`] for a linked image).
//! 3. Patch the image with [`repatch_jump`], [`repatch_value`] and
//!    [`rewrite_region`], or loaded code with their `_span` variants.

use smallvec::SmallVec;

use crate::{
    AsmError,
    core::{
        arch_traits::Arch,
        buffer::{CodeBufferFinalized, CodeOffset, LabelUse},
        relax::OffsetMap,
    },
};

#[cfg(feature = "jit")]
use crate::core::jit_allocator::{JitAllocator, Span};

/// A jump or call emitted with a fixed-size displacement whose target can be
/// changed after finalization.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PatchableJump {
    offset: CodeOffset,
    kind: LabelUse,
}

impl PatchableJump {
    pub(crate) const fn new(offset: CodeOffset, kind: LabelUse) -> Self {
        Self { offset, kind }
    }

    /// The mark returned when emission fails. Its location is out of bounds
    /// in every image, so patching through it fails.
    pub(crate) const fn invalid(kind: LabelUse) -> Self {
        Self::new(u32::MAX, kind)
    }
}

/// A patchable immediate: a fixed-size `mov` immediate on x86, a
/// `movz`/`movk` sequence on AArch64, or a literal on RISC-V.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DataLabel {
    offset: CodeOffset,
    size: u8,
    encoding: DataEncoding,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum DataEncoding {
    /// `size` little-endian bytes.
    #[cfg_attr(not(any(feature = "x86", feature = "riscv")), allow(dead_code))]
    Raw,
    /// `size / 4` AArch64 instructions: `movz` then `movk` for each further
    /// 16-bit chunk.
    #[cfg_attr(not(feature = "aarch64"), allow(dead_code))]
    A64MovWide,
}

impl DataLabel {
    pub(crate) const fn new(offset: CodeOffset, size: u8, encoding: DataEncoding) -> Self {
        Self {
            offset,
            size,
            encoding,
        }
    }

    pub(crate) const fn invalid(size: u8, encoding: DataEncoding) -> Self {
        Self::new(u32::MAX, size, encoding)
    }

    /// Size in bytes of the patched field.
    pub const fn size(self) -> u8 {
        self.size
    }
}

/// A nop-filled code region reserved for later rewriting.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PatchableRegion {
    offset: CodeOffset,
    size: CodeOffset,
    arch: Arch,
}

impl PatchableRegion {
    pub(crate) const fn new(offset: CodeOffset, size: CodeOffset, arch: Arch) -> Self {
        Self { offset, size, arch }
    }

    pub const fn size(self) -> CodeOffset {
        self.size
    }
}

/// Where a [`PatchableJump`] is in a finished image.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CodeLocationJump {
    offset: CodeOffset,
    kind: LabelUse,
}

impl CodeLocationJump {
    /// Offset of the displacement field.
    pub const fn offset(self) -> CodeOffset {
        self.offset
    }

    pub const fn kind(self) -> LabelUse {
        self.kind
    }
}

/// Where a [`DataLabel`] is in a finished image.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CodeLocationData {
    offset: CodeOffset,
    size: u8,
    encoding: DataEncoding,
}

impl CodeLocationData {
    /// Offset of the first byte of the field.
    pub const fn offset(self) -> CodeOffset {
        self.offset
    }

    pub const fn size(self) -> u8 {
        self.size
    }
}

/// Where a [`PatchableRegion`] is in a finished image.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CodeLocationRegion {
    offset: CodeOffset,
    size: CodeOffset,
    arch: Arch,
}

impl CodeLocationRegion {
    pub const fn offset(self) -> CodeOffset {
        self.offset
    }

    pub const fn size(self) -> CodeOffset {
        self.size
    }
}

mod sealed {
    pub trait Sealed {}
    impl Sealed for super::PatchableJump {}
    impl Sealed for super::DataLabel {}
    impl Sealed for super::PatchableRegion {}
}

/// A mark that [`CodeBufferFinalized::location_of`] turns into a location.
pub trait PatchMark: Copy + sealed::Sealed {
    type Location: Copy;

    #[doc(hidden)]
    fn locate(self, place: impl Fn(CodeOffset) -> CodeOffset) -> Self::Location;
}

/// Moves a buffer offset through branch relaxation and to its section,
/// keeping the out-of-bounds offset of an invalid mark out of bounds.
fn place(base: CodeOffset, layout: &OffsetMap, offset: CodeOffset) -> CodeOffset {
    if offset == u32::MAX {
        return offset;
    }
    base.saturating_add(layout.map(offset))
}

impl PatchMark for PatchableJump {
    type Location = CodeLocationJump;

    fn locate(self, place: impl Fn(CodeOffset) -> CodeOffset) -> CodeLocationJump {
        CodeLocationJump {
            offset: place(self.offset),
            kind: self.kind,
        }
    }
}

impl PatchMark for DataLabel {
    type Location = CodeLocationData;

    fn locate(self, place: impl Fn(CodeOffset) -> CodeOffset) -> CodeLocationData {
        CodeLocationData {
            offset: place(self.offset),
            size: self.size,
            encoding: self.encoding,
        }
    }
}

impl PatchMark for PatchableRegion {
    type Location = CodeLocationRegion;

    fn locate(self, place: impl Fn(CodeOffset) -> CodeOffset) -> CodeLocationRegion {
        CodeLocationRegion {
            offset: place(self.offset),
            size: self.size,
            arch: self.arch,
        }
    }
}

impl CodeBufferFinalized {
    /// Returns where `mark` is in this image.
    ///
    /// `mark` must come from the buffer this image was finished from. For an
    /// image made by [`Linker::link`](crate::Linker::link), use
    /// [`Self::location_in`].
    pub fn location_of<M: PatchMark>(&self, mark: M) -> M::Location {
        debug_assert_eq!(self.section_bases.len(), 1, "linked images need `location_in`");
        mark.locate(|offset| place(self.section_bases[0], &self.layout_maps[0], offset))
    }

    /// Returns where `mark`, from the buffer added as `section` to a
    /// [`Linker`](crate::Linker), is in this image. Returns `None` when there
    /// is no such section.
    pub fn location_in<M: PatchMark>(&self, section: usize, mark: M) -> Option<M::Location> {
        let base = *self.section_bases.get(section)?;
        let layout = self.layout_maps.get(section)?;
        Some(mark.locate(|offset| place(base, layout, offset)))
    }
}

fn field(len: usize, offset: CodeOffset, size: usize) -> Result<core::ops::Range<usize>, AsmError> {
    let start = offset as usize;
    let end = start.checked_add(size).ok_or(AsmError::InvalidState)?;
    if end > len {
        return Err(AsmError::InvalidState);
    }
    Ok(start..end)
}

fn relink(bytes: &mut [u8], kind: LabelUse, delta: i64) -> Result<(), AsmError> {
    if !kind.can_reach_delta(delta) {
        return Err(AsmError::TooLarge);
    }
    kind.patch_with_addend(bytes, 0, 0, delta);
    Ok(())
}

/// Points `jump` at `target`, an offset in the same image.
pub fn repatch_jump(
    code: &mut [u8],
    jump: CodeLocationJump,
    target: CodeOffset,
) -> Result<(), AsmError> {
    let range = field(code.len(), jump.offset, jump.kind.patch_size())?;
    let delta = i64::from(target) - i64::from(jump.offset);
    relink(&mut code[range], jump.kind, delta)
}

/// Points `jump` in loaded code at the absolute address `target`.
///
/// # Safety
///
/// `span` must hold the image `jump` was located in, and no thread may execute
/// the jump while it is rewritten.
#[cfg(feature = "jit")]
pub unsafe fn repatch_jump_span(
    jit_allocator: &mut JitAllocator,
    span: &mut Span,
    jump: CodeLocationJump,
    target: *const u8,
) -> Result<(), AsmError> {
    let range = field(span.size(), jump.offset, jump.kind.patch_size())?;
    let at = span.rx().addr() + range.start;
    let delta = i64::try_from(target.addr() as i128 - at as i128).map_err(|_| AsmError::TooLarge)?;
    if !jump.kind.can_reach_delta(delta) {
        return Err(AsmError::TooLarge);
    }
    // SAFETY: `range` is inside the span, and the caller guarantees the span
    // holds this jump and that nothing executes it concurrently.
    unsafe {
        jit_allocator.write(span, |span| {
            let bytes = core::slice::from_raw_parts_mut(span.rw().add(range.start), range.len());
            jump.kind.patch_with_addend(bytes, 0, 0, delta);
        })
    }
}

fn encode_value(
    current: &[u8],
    data: CodeLocationData,
    value: u64,
) -> Result<SmallVec<[u8; 16]>, AsmError> {
    let size = data.size as usize;
    match data.encoding {
        DataEncoding::Raw => {
            if size < 8 && value >> (size * 8) != 0 {
                return Err(AsmError::TooLarge);
            }
            Ok(SmallVec::from_slice(&value.to_le_bytes()[..size]))
        }
        DataEncoding::A64MovWide => {
            #[cfg(feature = "aarch64")]
            {
                // Re-encode for the register and width the sequence was
                // emitted with, read back from the `movz`.
                let movz = u32::from_le_bytes([current[0], current[1], current[2], current[3]]);
                let is_64bit = movz >> 31 == 1;
                if !is_64bit && value > u64::from(u32::MAX) {
                    return Err(AsmError::TooLarge);
                }
                Ok(crate::aarch64::encode_patchable_mov_imm(
                    movz & 0x1f,
                    is_64bit,
                    value,
                ))
            }
            #[cfg(not(feature = "aarch64"))]
            {
                let _ = (current, value);
                Err(AsmError::InvalidArch)
            }
        }
    }
}

fn decode_value(bytes: &[u8], data: CodeLocationData) -> u64 {
    match data.encoding {
        DataEncoding::Raw => {
            let mut value = [0u8; 8];
            value[..bytes.len()].copy_from_slice(bytes);
            u64::from_le_bytes(value)
        }
        DataEncoding::A64MovWide => bytes.chunks_exact(4).fold(0, |value, insn| {
            let insn = u32::from_le_bytes([insn[0], insn[1], insn[2], insn[3]]);
            let imm16 = u64::from((insn >> 5) & 0xffff);
            let hw = (insn >> 21) & 3;
            value | (imm16 << (16 * hw))
        }),
    }
}

/// Writes `value` into the immediate at `data`. Fails with [`AsmError::TooLarge`] when
/// `value` does not fit the field.
pub fn repatch_value(code: &mut [u8], data: CodeLocationData, value: u64) -> Result<(), AsmError> {
    let range = field(code.len(), data.offset, data.size as usize)?;
    let encoded = encode_value(&code[range.clone()], data, value)?;
    code[range].copy_from_slice(&encoded);
    Ok(())
}

/// Reads back the immediate at `data`.
pub fn read_value(code: &[u8], data: CodeLocationData) -> Result<u64, AsmError> {
    let range = field(code.len(), data.offset, data.size as usize)?;
    Ok(decode_value(&code[range], data))
}

/// Writes `value` into the immediate at `data` in loaded code.
///
/// # Safety
///
/// `span` must hold the image `data` was located in, and no thread may execute
/// the instruction while it is rewritten.
#[cfg(feature = "jit")]
pub unsafe fn repatch_value_span(
    jit_allocator: &mut JitAllocator,
    span: &mut Span,
    data: CodeLocationData,
    value: u64,
) -> Result<(), AsmError> {
    let range = field(span.size(), data.offset, data.size as usize)?;
    // SAFETY: `range` is inside the span, which the caller guarantees is live.
    let current = unsafe { core::slice::from_raw_parts(span.rx().add(range.start), range.len()) };
    let encoded = encode_value(current, data, value)?;
    // SAFETY: as above, and nothing executes the range while it is written.
    unsafe {
        jit_allocator.write(span, |span| {
            span.rw()
                .add(range.start)
                .copy_from_nonoverlapping(encoded.as_ptr(), encoded.len());
        })
    }
}

/// Reads back the immediate at `data` in loaded code.
///
/// # Safety
///
/// `span` must be a live allocation holding the image `data` was located in.
#[cfg(feature = "jit")]
pub unsafe fn read_value_span(span: &Span, data: CodeLocationData) -> Result<u64, AsmError> {
    let range = field(span.size(), data.offset, data.size as usize)?;
    // SAFETY: `range` is inside the span, which the caller guarantees is live.
    let bytes = unsafe { core::slice::from_raw_parts(span.rx().add(range.start), range.len()) };
    Ok(decode_value(bytes, data))
}

fn check_region_payload(region: CodeLocationRegion, new_bytes: &[u8]) -> Result<(), AsmError> {
    if new_bytes.len() > region.size as usize {
        return Err(AsmError::TooLarge);
    }
    if new_bytes.len() % minimum_patch_alignment(region.arch) as usize != 0 {
        return Err(AsmError::InvalidArgument);
    }
    Ok(())
}

/// Overwrites `region` with `new_bytes`, padding the rest with nops.
pub fn rewrite_region(
    code: &mut [u8],
    region: CodeLocationRegion,
    new_bytes: &[u8],
) -> Result<(), AsmError> {
    check_region_payload(region, new_bytes)?;
    let range = field(code.len(), region.offset, region.size as usize)?;
    let block = &mut code[range];
    block[..new_bytes.len()].copy_from_slice(new_bytes);
    fill_with_nops(region.arch, &mut block[new_bytes.len()..])
}

/// Overwrites `region` in loaded code with `new_bytes`, padding the rest with
/// nops.
///
/// # Safety
///
/// `span` must hold the image `region` was located in, and no thread may
/// execute the region while it is rewritten.
#[cfg(feature = "jit")]
pub unsafe fn rewrite_region_span(
    jit_allocator: &mut JitAllocator,
    span: &mut Span,
    region: CodeLocationRegion,
    new_bytes: &[u8],
) -> Result<(), AsmError> {
    check_region_payload(region, new_bytes)?;
    let range = field(span.size(), region.offset, region.size as usize)?;
    let mut result = Ok(());
    // SAFETY: `range` is inside the span, and the caller guarantees the span
    // holds this region and that nothing executes it concurrently.
    unsafe {
        jit_allocator.write(span, |span| {
            let block = core::slice::from_raw_parts_mut(span.rw().add(range.start), range.len());
            block[..new_bytes.len()].copy_from_slice(new_bytes);
            result = fill_with_nops(region.arch, &mut block[new_bytes.len()..]);
        })?;
    }
    result
}

pub fn minimum_patch_alignment(arch: Arch) -> CodeOffset {
    match arch {
        Arch::AArch64 | Arch::RISCV32 | Arch::RISCV64 => 4,
        _ => 1,
    }
}

pub fn fill_with_nops(arch: Arch, buffer: &mut [u8]) -> Result<(), AsmError> {
    let pattern: &[u8] = match arch {
        Arch::X86 | Arch::X64 => &[0x90],
        Arch::AArch64 => &[0x1f, 0x20, 0x03, 0xd5],
        Arch::RISCV32 | Arch::RISCV64 => &[0x13, 0x00, 0x00, 0x00],
        _ => return Err(AsmError::InvalidArgument),
    };

    if pattern.len() > 1 && buffer.len() % pattern.len() != 0 {
        return Err(AsmError::InvalidArgument);
    }

    for chunk in buffer.chunks_mut(pattern.len()) {
        chunk.copy_from_slice(pattern);
    }

    Ok(())
}