asm-rs 0.2.0

Pure Rust multi-architecture runtime assembly engine
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
//! Error types and source span tracking for diagnostics.

#[allow(unused_imports)]
use alloc::format;
use alloc::string::String;
#[allow(unused_imports)]
use alloc::vec;
use alloc::vec::Vec;
use core::fmt;

/// Source location for diagnostics.
///
/// Tracks the line, column, byte offset, and length of a token or construct
/// in the original assembly source text.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Span {
    /// 1-based line number.
    pub line: u32,
    /// 1-based column number (byte offset within line).
    pub col: u32,
    /// 0-based byte offset from start of source.
    pub offset: usize,
    /// Byte length of the spanned region.
    pub len: usize,
}

impl Span {
    /// Create a new span.
    #[must_use]
    pub fn new(line: u32, col: u32, offset: usize, len: usize) -> Self {
        Self {
            line,
            col,
            offset,
            len,
        }
    }

    /// A dummy span for generated/internal constructs.
    #[must_use]
    pub fn dummy() -> Self {
        Self {
            line: 0,
            col: 0,
            offset: 0,
            len: 0,
        }
    }
}

impl fmt::Display for Span {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.line, self.col)
    }
}

/// The architecture for which assembly failed — carried in some error variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ArchName {
    /// 32-bit x86.
    X86,
    /// 64-bit x86.
    X86_64,
    /// ARM A32.
    Arm,
    /// ARM Thumb-2.
    Thumb,
    /// ARMv8-A 64-bit.
    Aarch64,
    /// RISC-V 32-bit.
    Rv32,
    /// RISC-V 64-bit.
    Rv64,
}

impl fmt::Display for ArchName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ArchName::X86 => write!(f, "x86"),
            ArchName::X86_64 => write!(f, "x86_64"),
            ArchName::Arm => write!(f, "ARM"),
            ArchName::Thumb => write!(f, "Thumb"),
            ArchName::Aarch64 => write!(f, "AArch64"),
            ArchName::Rv32 => write!(f, "RV32"),
            ArchName::Rv64 => write!(f, "RV64"),
        }
    }
}

/// Assembly error with source location and descriptive message.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AsmError {
    /// Unknown mnemonic for the target architecture.
    UnknownMnemonic {
        /// The mnemonic that was not recognized.
        mnemonic: String,
        /// The target architecture name.
        arch: ArchName,
        /// Source location of the unknown mnemonic.
        span: Span,
    },

    /// Invalid operand combination for the instruction.
    InvalidOperands {
        /// Description of why the operands are invalid.
        detail: String,
        /// Source location of the instruction.
        span: Span,
    },

    /// Immediate value exceeds the allowed range.
    ImmediateOverflow {
        /// The immediate value that overflowed.
        value: i128,
        /// Minimum allowed value.
        min: i128,
        /// Maximum allowed value.
        max: i128,
        /// Source location of the immediate.
        span: Span,
    },

    /// Referenced label was never defined.
    UndefinedLabel {
        /// The undefined label name.
        label: String,
        /// Source location of the reference.
        span: Span,
    },

    /// Label was defined more than once.
    DuplicateLabel {
        /// The duplicated label name.
        label: String,
        /// Source location of the duplicate definition.
        span: Span,
        /// Source location of the first definition.
        first_span: Span,
    },

    /// Branch target is out of range even after relaxation.
    BranchOutOfRange {
        /// The target label name.
        label: String,
        /// The actual displacement to the target.
        disp: i64,
        /// Maximum allowed displacement.
        max: i64,
        /// Source location of the branch instruction.
        span: Span,
    },

    /// A branch target is not aligned to the granularity its encoding can
    /// represent.
    ///
    /// PC-relative branches store their displacement pre-scaled — AArch64
    /// shifts right by 2, Thumb and RISC-V by 1 — so the low bits of an
    /// unaligned target have nowhere to go. Truncating them would silently
    /// branch somewhere other than the label.
    MisalignedBranchTarget {
        /// The target label name.
        label: String,
        /// The displacement that could not be represented.
        disp: i64,
        /// Required alignment, in bytes.
        alignment: u8,
        /// Source location of the branch instruction.
        span: Span,
    },

    /// Syntax error during lexing or parsing.
    Syntax {
        /// The syntax error message.
        msg: String,
        /// Source location of the syntax error.
        span: Span,
    },

    /// Branch relaxation did not converge within the allowed number of passes.
    RelaxationLimit {
        /// Maximum number of relaxation passes allowed.
        max: usize,
    },

    /// A configurable resource limit was exceeded (defense against DoS).
    ResourceLimitExceeded {
        /// Human-readable name of the resource (e.g. "statements", "labels").
        resource: String,
        /// The configured limit that was exceeded.
        limit: usize,
    },

    /// Multiple errors collected during assembly.
    Multiple {
        /// The collected assembly errors.
        errors: Vec<AsmError>,
    },
}

impl fmt::Display for AsmError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AsmError::UnknownMnemonic {
                mnemonic,
                arch,
                span,
            } => {
                write!(f, "{}: unknown mnemonic '{}' for {}", span, mnemonic, arch)
            }
            AsmError::InvalidOperands { detail, span } => {
                write!(f, "{}: invalid operand combination: {}", span, detail)
            }
            AsmError::ImmediateOverflow {
                value,
                min,
                max,
                span,
            } => {
                write!(
                    f,
                    "{}: immediate value {} out of range [{}..{}]",
                    span, value, min, max
                )
            }
            AsmError::UndefinedLabel { label, span } => {
                write!(f, "{}: undefined label '{}'", span, label)
            }
            AsmError::DuplicateLabel {
                label,
                span,
                first_span,
            } => {
                write!(
                    f,
                    "{}: duplicate label '{}' (first defined at {})",
                    span, label, first_span
                )
            }
            AsmError::BranchOutOfRange {
                label,
                disp,
                max,
                span,
            } => {
                write!(
                    f,
                    "{}: branch target '{}' out of range (displacement={}, max=±{})",
                    span, label, disp, max
                )
            }
            AsmError::MisalignedBranchTarget {
                label,
                disp,
                alignment,
                span,
            } => {
                write!(
                    f,
                    "{}: branch target '{}' is not {}-byte aligned (displacement={})",
                    span, label, alignment, disp
                )
            }
            AsmError::Syntax { msg, span } => {
                write!(f, "{}: {}", span, msg)
            }
            AsmError::RelaxationLimit { max } => {
                write!(
                    f,
                    "assembly exceeded maximum of {} relaxation passes (possible oscillation)",
                    max
                )
            }
            AsmError::ResourceLimitExceeded { resource, limit } => {
                write!(
                    f,
                    "resource limit exceeded: {} (limit: {})",
                    resource, limit
                )
            }
            AsmError::Multiple { errors } => {
                for (i, e) in errors.iter().enumerate() {
                    if i > 0 {
                        writeln!(f)?;
                    }
                    write!(f, "{}", e)?;
                }
                Ok(())
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for AsmError {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn span_display() {
        let span = Span::new(3, 12, 45, 5);
        assert_eq!(format!("{}", span), "3:12");
    }

    #[test]
    fn span_dummy() {
        let span = Span::dummy();
        assert_eq!(span.line, 0);
        assert_eq!(span.col, 0);
    }

    #[test]
    fn error_unknown_mnemonic_display() {
        let err = AsmError::UnknownMnemonic {
            mnemonic: "foobar".into(),
            arch: ArchName::X86_64,
            span: Span::new(3, 12, 0, 6),
        };
        assert_eq!(
            format!("{}", err),
            "3:12: unknown mnemonic 'foobar' for x86_64"
        );
    }

    #[test]
    fn error_syntax_display() {
        let err = AsmError::Syntax {
            msg: "unexpected token '!'".into(),
            span: Span::new(1, 5, 4, 1),
        };
        assert_eq!(format!("{}", err), "1:5: unexpected token '!'");
    }

    #[test]
    fn error_undefined_label_display() {
        let err = AsmError::UndefinedLabel {
            label: "my_label".into(),
            span: Span::new(10, 1, 100, 8),
        };
        assert_eq!(format!("{}", err), "10:1: undefined label 'my_label'");
    }

    #[test]
    fn error_immediate_overflow_display() {
        let err = AsmError::ImmediateOverflow {
            value: 256,
            min: -128,
            max: 127,
            span: Span::new(5, 10, 50, 3),
        };
        assert_eq!(
            format!("{}", err),
            "5:10: immediate value 256 out of range [-128..127]"
        );
    }

    #[test]
    fn error_duplicate_label_display() {
        let err = AsmError::DuplicateLabel {
            label: "loop".into(),
            span: Span::new(20, 1, 200, 4),
            first_span: Span::new(5, 1, 50, 4),
        };
        assert_eq!(
            format!("{}", err),
            "20:1: duplicate label 'loop' (first defined at 5:1)"
        );
    }

    #[test]
    fn error_relaxation_limit_display() {
        let err = AsmError::RelaxationLimit { max: 20 };
        assert_eq!(
            format!("{}", err),
            "assembly exceeded maximum of 20 relaxation passes (possible oscillation)"
        );
    }

    #[test]
    fn error_branch_out_of_range_display() {
        let err = AsmError::BranchOutOfRange {
            label: "far_away".into(),
            disp: 500000,
            max: 127,
            span: Span::new(1, 1, 0, 10),
        };
        assert_eq!(
            format!("{}", err),
            "1:1: branch target 'far_away' out of range (displacement=500000, max=±127)"
        );
    }

    #[test]
    fn error_multiple_display() {
        let err = AsmError::Multiple {
            errors: vec![
                AsmError::Syntax {
                    msg: "err1".into(),
                    span: Span::new(1, 1, 0, 1),
                },
                AsmError::Syntax {
                    msg: "err2".into(),
                    span: Span::new(2, 1, 5, 1),
                },
            ],
        };
        let s = format!("{}", err);
        assert!(s.contains("err1"));
        assert!(s.contains("err2"));
    }

    #[test]
    fn error_resource_limit_exceeded_display() {
        let err = AsmError::ResourceLimitExceeded {
            resource: "statements".into(),
            limit: 1_000_000,
        };
        assert_eq!(
            format!("{}", err),
            "resource limit exceeded: statements (limit: 1000000)"
        );
    }
}