Skip to content

feat: add detector for invalid memory-safe assembly annotations#2943

Open
ep0chzer0 wants to merge 2 commits intocrytic:masterfrom
ep0chzer0:feature/invalid-memory-safe-assembly
Open

feat: add detector for invalid memory-safe assembly annotations#2943
ep0chzer0 wants to merge 2 commits intocrytic:masterfrom
ep0chzer0:feature/invalid-memory-safe-assembly

Conversation

@ep0chzer0
Copy link
Contributor

Summary

Adds a new detector (incorrect-memory-safe) that identifies incorrect usage of the @solidity memory-safe-assembly annotation.

What it detects:

  1. Regular comments instead of NatSpec - Annotations in // or /* */ comments are ignored by the compiler. Only NatSpec comments (/// or /** */) work.

  2. Common typos - Detects misspellings like:

    • memory-sage-assembly (typo: 'sage' instead of 'safe')
    • memory safe assembly (spaces instead of hyphens)
    • memory_safe_assembly (underscores instead of hyphens)
    • And other common variations

Why this matters:

The @solidity memory-safe-assembly annotation tells the compiler that inline assembly code follows Solidity's memory model. When valid, the compiler can apply memory optimizations. However:

  • Using regular comments (//) instead of NatSpec (///) means the annotation is silently ignored
  • Developers may believe their code is optimized when it's not
  • This leads to missed gas savings and false assumptions about code behavior

Example

// BAD: Regular comment - annotation is ignored
// @solidity memory-safe-assembly
assembly {
    result := mload(0x40)
}

// GOOD: NatSpec comment - annotation works
/// @solidity memory-safe-assembly
assembly {
    result := mload(0x40)
}

Test Coverage

  • Test file with 4 invalid cases (regular comments, typos) and 3 valid cases
  • Snapshot verifies all 4 issues are detected
  • Valid cases (correct NatSpec, no annotation) are not flagged

Fixes #2780


🤖 Generated with Claude Code

@ep0chzer0 ep0chzer0 requested a review from smonicas as a code owner January 21, 2026 11:35
@ep0chzer0 ep0chzer0 force-pushed the feature/invalid-memory-safe-assembly branch 3 times, most recently from 4dadb52 to f4dbc5c Compare January 28, 2026 08:01
Adds a new detector that identifies incorrect usage of the `@solidity
memory-safe-assembly` annotation:

- Detects annotations in regular comments (// or /* */) instead of
  NatSpec comments (/// or /** */), which the compiler ignores
- Detects common typos in the annotation text (e.g., "memory-sage-assembly",
  "memory safe assembly" with spaces instead of hyphens)
- Provides clear guidance on how to fix the issue

The `@solidity memory-safe-assembly` annotation only works in NatSpec
comments. Using it in regular comments means the compiler won't apply
memory optimizations, leading to missed gas savings and false assumptions
about code behavior.

Fixes crytic#2780
- Add multi-line NatSpec block detection to avoid false positives
- Add LANGUAGE = "solidity" to skip Vyper contracts
- Remove unused filename parameter
- Add deterministic result sorting
- Increase search window from 10 to 20 lines
- Add test cases for:
  - Multi-line regular comment with continuation (/* ... */)
  - Multi-line NatSpec with continuation (/** ... */)
  - Assembly in modifiers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: Detect Invalid Memory-Safe Assembly Annotations

1 participant