-
Notifications
You must be signed in to change notification settings - Fork 15.3k
draft: inline asm mode #146215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
androm3da
wants to merge
4
commits into
llvm:main
Choose a base branch
from
androm3da:bcain/inline_asm_mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
draft: inline asm mode #146215
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # Inline Assembly Safety Directive | ||
|
|
||
| ## Overview | ||
|
|
||
| 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. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```assembly | ||
| .inline_asm_mode strict # Enable strict mode - warn on non-local labels | ||
| .inline_asm_mode relaxed # Disable strict mode (default) | ||
| ``` | ||
|
|
||
| ## Description | ||
|
|
||
| When `.inline_asm_mode strict` is active, the assembler will emit warnings for labels that are considered potentially unsafe for inline assembly: | ||
|
|
||
| - **Safe labels** (no warnings): | ||
| - Local labels starting with `.L` (e.g., `.L_loop`, `.L_end`) | ||
| - Numeric labels (e.g., `1:`, `42:`) | ||
| - Labels starting with special prefixes (`$`, `__`) | ||
| - Labels starting with `.` (local scope) | ||
|
||
|
|
||
| - **Unsafe labels** (warnings emitted): | ||
| - Global labels without special prefixes (e.g., `my_function:`, `loop:`) | ||
| - Labels that could be accessed from outside the inline assembly block | ||
|
|
||
| ## Use Cases | ||
|
|
||
| ### Frontend Integration | ||
|
|
||
| Compiler frontends can use this directive when generating inline assembly: | ||
|
|
||
| ```c++ | ||
| // Emitted by the compiler: .inline_asm_mode strict | ||
| // C++ inline assembly example | ||
| asm( | ||
| ".L_loop:\n" // Safe - no warning | ||
| " add %0, %1\n" | ||
| " jne .L_loop\n" // Safe - local jump | ||
| "exit:\n" // Warning | ||
| : "=r"(result) : "r"(input)); | ||
| ``` | ||
| // Emitted by the compiler: .inline_asm_mode relaxed | ||
|
|
||
| ## Rationale | ||
|
|
||
| Inline assembly blocks are often embedded within larger functions or modules. Non-local labels in these blocks can create several problems: | ||
|
|
||
| 1. **Naming conflicts**: Global labels may conflict with other symbols in the compilation unit | ||
| 2. **Unintended control flow**: External code might accidentally jump to labels intended for internal use | ||
| 3. **Maintenance issues**: Global labels make inline assembly less encapsulated | ||
|
|
||
| The strict mode helps identify these potential issues during compilation, allowing developers to use safer local labels instead. | ||
|
|
||
| ## Error Handling | ||
|
|
||
| Invalid directive usage will produce parse errors: | ||
|
|
||
| ```assembly | ||
| .inline_asm_mode invalid_mode | ||
| # Error: expected 'strict' or 'relaxed' | ||
|
|
||
| .inline_asm_mode | ||
| # Error: expected 'strict' or 'relaxed' after '.inline_asm_mode' | ||
| ``` | ||
|
|
||
| ## Implementation Details | ||
|
|
||
| - The directive affects only subsequent label definitions until changed | ||
| - Default mode is `relaxed` (no additional warnings) | ||
| - The directive state is maintained in the MC streamer | ||
| - Warnings are emitted through the standard LLVM diagnostic system | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Complete Example | ||
|
|
||
| ```assembly | ||
| .text | ||
| .globl example_function | ||
| example_function: | ||
| # Regular function labels (outside inline asm) - no warnings | ||
|
|
||
| # Simulate inline assembly block with safety | ||
| .inline_asm_mode strict | ||
|
|
||
| # These are safe | ||
| .L_inline_start: | ||
| mov $1, %eax | ||
| test %eax, %eax | ||
| jz .L_inline_end | ||
|
|
||
| 1: # Numeric label | ||
| inc %eax | ||
| cmp $10, %eax | ||
| jl 1b | ||
|
|
||
| .L_inline_end: | ||
| # End of safe inline block | ||
|
|
||
| # This would generate a warning | ||
| # global_inline_label: # Warning would be emitted | ||
|
|
||
| .inline_asm_mode relaxed | ||
|
|
||
| # Back to normal mode | ||
| ret | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # RUN: llvm-mc -triple x86_64-unknown-unknown %s 2>&1 | FileCheck %s | ||
|
|
||
| # Test basic .inline_asm_mode directive functionality | ||
|
|
||
| .text | ||
|
|
||
| # Test that the directive is parsed correctly | ||
| .inline_asm_mode strict | ||
| .inline_asm_mode relaxed | ||
|
|
||
| # Test strict mode warnings | ||
| .inline_asm_mode strict | ||
|
|
||
| # This should produce a warning | ||
| # CHECK: warning: non-local label 'unsafe_global' in inline assembly strict mode may be unsafe for external jumps; consider using local labels (.L*) instead | ||
| unsafe_global: | ||
| nop | ||
|
|
||
| # This should not warn (local label) | ||
| .L_safe_local: | ||
| nop | ||
|
|
||
| # Test error handling | ||
| .inline_asm_mode invalid | ||
| # CHECK: error: expected 'strict' or 'relaxed' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # RUN: llvm-mc -triple x86_64-unknown-unknown %s 2>&1 | FileCheck %s --check-prefix=RELAX | ||
| # RUN: llvm-mc -triple x86_64-unknown-unknown %s 2>&1 | FileCheck %s --check-prefix=STRICT | ||
|
|
||
| # Test the .inline_asm_mode directive for safer inline assembly label handling | ||
|
|
||
| .text | ||
|
|
||
| # Test relaxed mode (default) - no warnings | ||
| .inline_asm_mode relaxed | ||
|
|
||
| # These labels should not produce warnings in relaxed mode | ||
| my_label: | ||
| nop | ||
| global_symbol: | ||
| nop | ||
| .L_local_label: | ||
| nop | ||
|
|
||
| # RELAX-NOT: warning | ||
|
|
||
| # Test strict mode - should warn about non-local labels | ||
| .inline_asm_mode strict | ||
|
|
||
| # Local labels - should not warn | ||
| .L_local1: | ||
| nop | ||
| .L_local_with_numbers_123: | ||
| nop | ||
| 1: | ||
| nop | ||
| 42: | ||
| nop | ||
|
|
||
| # Non-local labels - should warn | ||
| # STRICT: :[[@LINE+1]]:1: warning: non-local label 'unsafe_label' in inline assembly strict mode may be unsafe for external jumps; consider using local labels (.L*) instead | ||
| unsafe_label: | ||
| nop | ||
|
|
||
| # STRICT: :[[@LINE+1]]:1: warning: non-local label 'another_global' in inline assembly strict mode may be unsafe for external jumps; consider using local labels (.L*) instead | ||
| another_global: | ||
| nop | ||
|
|
||
| # Switch back to relaxed mode | ||
| .inline_asm_mode relaxed | ||
|
|
||
| # This should not warn again | ||
| yet_another_label: | ||
| nop | ||
|
|
||
| # RELAX-NOT: warning | ||
|
|
||
| # Test error cases | ||
| .inline_asm_mode invalid_mode | ||
| # CHECK: :[[@LINE-1]]:18: error: expected 'strict' or 'relaxed' | ||
|
|
||
| .inline_asm_mode | ||
| # CHECK: :[[@LINE-1]]:17: error: expected 'strict' or 'relaxed' after '.inline_asm_mode' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm especially willing to consider a better name for this directive.