|
| 1 | +# Inline Assembly Safety Directive |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `.inline_asm_mode` directive provides enhanced safety for inline assembly blocks by warning about potentially unsafe label usage. This directive helps prevent common errors where programmers create non-local labels in inline assembly that could be inadvertently jumped to from external code. |
| 6 | + |
| 7 | +## Syntax |
| 8 | + |
| 9 | +```assembly |
| 10 | +.inline_asm_mode strict # Enable strict mode - warn on non-local labels |
| 11 | +.inline_asm_mode relaxed # Disable strict mode (default) |
| 12 | +``` |
| 13 | + |
| 14 | +## Description |
| 15 | + |
| 16 | +When `.inline_asm_mode strict` is active, the assembler will emit warnings for labels that are considered potentially unsafe for inline assembly: |
| 17 | + |
| 18 | +- **Safe labels** (no warnings): |
| 19 | + - Local labels starting with `.L` (e.g., `.L_loop`, `.L_end`) |
| 20 | + - Numeric labels (e.g., `1:`, `42:`) |
| 21 | + - Labels starting with special prefixes (`$`, `__`) |
| 22 | + - Labels starting with `.` (local scope) |
| 23 | + |
| 24 | +- **Unsafe labels** (warnings emitted): |
| 25 | + - Global labels without special prefixes (e.g., `my_function:`, `loop:`) |
| 26 | + - Labels that could be accessed from outside the inline assembly block |
| 27 | + |
| 28 | +## Use Cases |
| 29 | + |
| 30 | +### Frontend Integration |
| 31 | + |
| 32 | +Compiler frontends can use this directive when generating inline assembly: |
| 33 | + |
| 34 | +```c++ |
| 35 | +// Emitted by the compiler: .inline_asm_mode strict |
| 36 | +// C++ inline assembly example |
| 37 | +asm( |
| 38 | + ".L_loop:\n" // Safe - no warning |
| 39 | + " add %0, %1\n" |
| 40 | + " jne .L_loop\n" // Safe - local jump |
| 41 | + "exit:\n" // Warning |
| 42 | + : "=r"(result) : "r"(input)); |
| 43 | +``` |
| 44 | +// Emitted by the compiler: .inline_asm_mode relaxed |
| 45 | + |
| 46 | +## Rationale |
| 47 | + |
| 48 | +Inline assembly blocks are often embedded within larger functions or modules. Non-local labels in these blocks can create several problems: |
| 49 | + |
| 50 | +1. **Naming conflicts**: Global labels may conflict with other symbols in the compilation unit |
| 51 | +2. **Unintended control flow**: External code might accidentally jump to labels intended for internal use |
| 52 | +3. **Maintenance issues**: Global labels make inline assembly less encapsulated |
| 53 | + |
| 54 | +The strict mode helps identify these potential issues during compilation, allowing developers to use safer local labels instead. |
| 55 | + |
| 56 | +## Error Handling |
| 57 | + |
| 58 | +Invalid directive usage will produce parse errors: |
| 59 | + |
| 60 | +```assembly |
| 61 | +.inline_asm_mode invalid_mode |
| 62 | +# Error: expected 'strict' or 'relaxed' |
| 63 | +
|
| 64 | +.inline_asm_mode |
| 65 | +# Error: expected 'strict' or 'relaxed' after '.inline_asm_mode' |
| 66 | +``` |
| 67 | + |
| 68 | +## Implementation Details |
| 69 | + |
| 70 | +- The directive affects only subsequent label definitions until changed |
| 71 | +- Default mode is `relaxed` (no additional warnings) |
| 72 | +- The directive state is maintained in the MC streamer |
| 73 | +- Warnings are emitted through the standard LLVM diagnostic system |
| 74 | + |
| 75 | +## Examples |
| 76 | + |
| 77 | +### Complete Example |
| 78 | + |
| 79 | +```assembly |
| 80 | +.text |
| 81 | +.globl example_function |
| 82 | +example_function: |
| 83 | + # Regular function labels (outside inline asm) - no warnings |
| 84 | +
|
| 85 | + # Simulate inline assembly block with safety |
| 86 | + .inline_asm_mode strict |
| 87 | +
|
| 88 | + # These are safe |
| 89 | + .L_inline_start: |
| 90 | + mov $1, %eax |
| 91 | + test %eax, %eax |
| 92 | + jz .L_inline_end |
| 93 | +
|
| 94 | + 1: # Numeric label |
| 95 | + inc %eax |
| 96 | + cmp $10, %eax |
| 97 | + jl 1b |
| 98 | +
|
| 99 | + .L_inline_end: |
| 100 | + # End of safe inline block |
| 101 | +
|
| 102 | + # This would generate a warning |
| 103 | + # global_inline_label: # Warning would be emitted |
| 104 | +
|
| 105 | + .inline_asm_mode relaxed |
| 106 | +
|
| 107 | + # Back to normal mode |
| 108 | + ret |
| 109 | +``` |
| 110 | + |
0 commit comments