Skip to content
check-memory-safety logo

Check Memory Safety

check-memory-safety

Check Mojo code for memory safety issues (ownership violations, use-after-free, etc.). Use to catch memory bugs.

SKILL.md

Full skill instructions

Check Memory Safety

Validate Mojo code for memory safety violations and ownership issues.

When to Use

  • Code review focused on memory safety
  • Testing for use-after-free issues
  • Verifying ownership transfer correctness
  • Catching double-free or segmentation fault sources
  • Validating SIMD memory access patterns

Quick Reference

# Find potential use-after-free patterns
grep -n "var .* = .*\^" *.mojo | head -20

# Check for uninitialized access
grep -n "List\[.*\]()" *.mojo | grep -A 2 "\..*\["

# Verify ownership transfer
grep -n "owned\|var.*=" *.mojo | sort

# Check pointer operations
grep -n "DTypePointer\|alloc\|free\|__del__" *.mojo

# Find scope issues
grep -n "{" *.mojo | wc -l  # Check nesting depth

Memory Safety Patterns

Safe Ownership Transfer:

  • ✅ fn take(var data: List[Int]) - Caller loses access
  • ✅ return self.data^ - Transfer from struct field
  • ✅ var copy = data^ - Move to new variable
  • ❌ fn take(data: List[Int]) - Ambiguous, use var
  • ❌ return self.data - Missing transfer operator

Safe Initialization:

  • ✅ var shape = List[Int]() then shape.append(dim)
  • ✅ var data = DTypePointer[DType.float32].alloc(size)
  • ✅ Check size > 0 before allocation
  • ❌ var list = List[Int]() then list[0] = value (uninitialized)
  • ❌ alloc(0) or alloc(negative) (invalid size)

Safe Pointer Usage:

  • ✅ Allocate before use: alloc(size)
  • ✅ Store size separately for bounds checking
  • ✅ Verify pointer validity before dereference
  • ❌ Use after free (manually deleted pointer)
  • ❌ Out-of-bounds access
  • ❌ Null pointer dereference

Safe Scope Management:

  • ✅ Owned values dropped at scope end
  • ✅ RAII pattern with proper cleanup
  • ✅ Lifetime boundaries clear
  • ❌ Variable used after scope exit
  • ❌ Reference to stack variable returned
  • ❌ Missing ownership transfer between scopes

Safety Validation Workflow

  1. Identify pointers: Find all pointer operations
  2. Check allocation: Verify all pointers allocated before use
  3. Trace ownership: Follow ownership transfers
  4. Verify lifetimes: Check value scopes
  5. Find violations: Identify safety issues
  6. Suggest fixes: Provide safety corrections
  7. Verify fixes: Compile to confirm safety

Output Format

Report memory safety issues with:

  1. Issue Type - Use-after-free, double-free, uninitialized, bounds, etc.
  2. Location - File and line number
  3. Code Snippet - The problematic code
  4. Root Cause - Why it's unsafe
  5. Risk Level - Segfault, undefined behavior, or warning
  6. Fix - How to correct it safely

Common Safety Issues & Fixes

Uninitialized List Access:

  • Problem: var list = List[Int](); list[0] = 5
  • Risk: Out-of-bounds write, segmentation fault
  • Fix: Use list.append(5) instead

Use After Move:

  • Problem: var a = list^; print(list) (list moved, now invalid)
  • Risk: Use-after-free
  • Fix: Don't use list after transfer, or create copy

Missing Bounds Check:

  • Problem: Access tensor._data[index] without size check
  • Risk: Out-of-bounds access, segfault
  • Fix: Add assert index < size or check in loop

Pointer Without Size:

  • Problem: var ptr = alloc(size) but size not tracked
  • Risk: Invalid access, use-after-free
  • Fix: Store size, verify before access

Double-Free:

  • Problem: Manual free() called twice on same pointer
  • Risk: Heap corruption, segfault
  • Fix: Use RAII, don't call free manually

Error Handling

ProblemSolution
Compiler not availableBuild with mojo build to get errors
Complex ownershipTrace step-by-step through ownership chain
Generic codeCheck all type instantiations
External codeVerify contract assumptions
False positivesVerify with test execution

Safety Checklist

Before committing Mojo code:

  • All pointers have size tracked
  • All List/​Dict allocations use append/​insert
  • All ownership transfers use ^
  • No variables used after move
  • All pointer access has bounds check
  • Scope lifetimes match usage
  • Compiler produces no errors or warnings

References

  • See CLAUDE.md for ownership patterns
  • See validate-mojo-patterns for pattern checking
  • See mojo-lint-syntax for syntax issues

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View