memory-safe-migration
/memory-safe-migration
Guide secure migration of code from memory-unsafe languages (C, C++, Assembly) to memory-safe languages (Rust, Go, Java, C#, Swift). Use when migrating or rewriting legacy C/C++ code, designing FFI boundaries between safe and unsafe code, writing new modules in existing C/C++ codebases, reviewing mixed-language projects, planning memory safety roadmaps, or when an AI agent is about to generate new C/C++ code that could be written in a memory-safe language instead. Also triggers on CISA/NSA memory safety compliance discussions.
- Author
- cosai-oasis ↗
- Repository
- project-codeguard ↗
- Licence
- CC-BY-4.0
- Last repository change
- 12.10.2026
Assessment and security
- Files in this package
- 6
- Scripts
- 1
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 12.10.2026, without references to retired models or APIs.
CC-BY-4.0, otwarta.
- 1 scripts in this package. Review them before installing, as Claude may execute them.
Scripts: scripts/assess-migration.py
What it does and when to use it
Guide secure migration of code from memory-unsafe languages (C, C++, Assembly) to memory-safe languages (Rust, Go, Java, C#, Swift). Use when migrating or rewriting legacy C/C++ code, designing FFI boundaries between safe and unsafe code, writing new modules in existing C/C++ codebases, reviewing mixed-language projects, planning memory safety roadmaps, or when an AI agent is about to generate new C/C++ code that could be written in a memory-safe language instead. Also triggers on CISA/NSA memory safety compliance discussions.
Original author description
Guide secure migration of code from memory-unsafe languages (C, C++, Assembly) to memory-safe languages (Rust, Go, Java, C#, Swift). Use when migrating or rewriting legacy C/C++ code, designing FFI boundaries between safe and unsafe code, writing new modules in existing C/C++ codebases, reviewing mixed-language projects, planning memory safety roadmaps, or when an AI agent is about to generate new C/C++ code that could be written in a memory-safe language instead. Also triggers on CISA/NSA memory safety compliance discussions.
How it works
- When this skill activates
- Decision: new code language selection
- Migration workflow
- Anti-patterns to prevent
- References
SKILL.md preview
--- name: memory-safe-migration description: >- Guide secure migration of code from memory-unsafe languages (C, C++, Assembly) to memory-safe languages (Rust, Go, Java, C#, Swift). Use when migrating or rewriting legacy C/C++ code, designing FFI boundaries between safe and unsafe code, writing new modules in existing C/C++ codebases, reviewing mixed-language projects, planning memory safety roadmaps, or when an AI agent is about to generate new C/C++ code that could be written in a memory-safe language instead. Also triggers on CISA/NSA memory safety compliance discussions. license: CC-BY-4.0 metadata: author: codeguard-community version: "1.0" category: memory-safety --- # Memory-safe language migration ## When this skill activates - User asks to migrate, port, or rewrite C/C++ code to Rust, Go, Java, C#, or Swift - User asks to add a new module or feature to an existing C/C++ project - User asks to design an FFI boundary between safe and unsafe code - User asks about memory safety roadmaps or CISA/NSA compliance - User asks to review mixed-language code for safety issues - AI agent is about to generate new C/C++ code — check if an MSL alternative is viable ## Decision: new code language selection Before writing any new code, ask: 1. Is there an explicit constraint requiring C/C++? (bare-metal with no MSL runtime, hard real-time below 1μs, existing codebase policy) 2. If no constraint exists, default to a memory-safe language 3. Select the target language using the guide in [references/language-selection.md](references/language-selection.md) If the project is predominantly C/C++, write the new module in an MSL and integrate via FFI. See [references/ffi-security.md](references/ffi-security.md) for boundary rules. ## Migration workflow Follow these steps for every migration task: ### Step 1: Assess the component Run the assessment script to evaluate migration priority and feasibility: ```bash python scripts/assess-migration.py --file <source_file> ``` Or manually evaluate using the checklist in [references/assessment-checklist.md](references/assessment-checklist.md). Priority order for migration: 1. Network-facing code (parsers, protocol handlers, TLS) 2. Code handling untrusted input (file parsers, deserialization) 3. Cryptographic implementations 4. Privilege boundary code (auth enforcement) 5. Code with a history of memory-related CVEs 6. Internal utility code ### Step 2: Write tests first Never migrate a component without test coverage. If no tests exist, write them against the C/C++ implementation before touching anything. These tests become the correctness oracle for the new implementation. ### Step 3: Migrate incrementally One function or module at a time. Never rewrite an entire codebase in one pass. Follow the Android model: new code in MSL, existing stable code stays in place, proportion of unsafe code decreases over time. For common migration patterns (buffers, strings, concurrency, error handling), see [references/migration-patterns.md](references/migration-patterns.md). ### Step 4: Secure the FFI boundary Every interface between safe and unsafe code is a security boundary. Follow all rules in [references/ffi-security.md](references/ffi-security.md). Key rules: - Validate all inputs from the unsafe side (null checks, bounds checks, type checks) - Minimize `unsafe` blocks — wrap only the minimum necessary operation - Document every `unsafe` block with a `// SAFETY:` comment - The allocator that created memory must free it — never mix allocators - Never panic across FFI boundaries - Catch all panics with `std::panic::catch_unwind` at FFI entry points ### Step 5: Validate After every migration unit, verify: - All existing tests pass against the new implementation - No new `unsafe` surface without documented safety invariants - FFI boundaries fuzzed with malformed inputs, null pointers, extreme lengths - Memory safety tools run clean (Miri for Rust, race detector for Go, ASan for C side) - Performance benchmarked against the original — no unacceptable regression - All new MSL dependencies audited for trustworthiness and active maintenance ### Step 6: Update build and CI - Integrate the MSL toolchain (cargo, go build, etc.) into the existing build system - Add MSL-specific linting (`clippy` for Rust, `go vet` for Go) - Add formatting checks (`rustfmt`, `gofmt`) - Add safety-specific CI checks (deny `unsafe` without annotation, dependency audit) ## Anti-patterns to prevent Never do these during migration: - **Wrapping unsafe C in a "safe" API without actual safety guarantees** — if the wrapper just passes through without validation, it provides false confidence - **Using `unsafe` to replicate C-style patterns in Rust** — if extensive `unsafe` is needed, the approach should be redesigned or the code should remain in C - **Migrating without tests** — write tests for C/C++ first, then validate MSL version - **Ignoring error handling differences** — C uses return codes, Rust uses `Result`, Go uses multiple returns. Every error path must be explicitly mapped - **Assuming GC languages need no resource discipline** — they prevent memory corruption but can still leak file handles, sockets, and connections. Use `defer`, `try-with-resources`, `using`, or `with` patterns - **Migrating performance-critical loops without benchmarking** — verify first ## References For detailed guidance on specific topics: - [Language selection guide](references/language-selection.md) - [FFI boundary security rules](references/ffi-security.md) - [Common migration patterns](references/migration-patterns.md) - [Assessment checklist](references/assessment-checklist.md)
Files in this package (6)
- SKILL.md 6 KB
- references/assessment-checklist.md 3 KB
- references/ffi-security.md 6 KB
- references/language-selection.md 3 KB
- references/migration-patterns.md 7 KB
- scripts/assess-migration.py 14 KB
Versions
The database has no other skills named memory-safe-migration.
Database snapshot: 13.11.2026 · package version d74555bb3c98 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100rust-review
Performs comprehensive Rust security review for safe/unsafe boundary issues, memory safety in unsafe blocks, concurrency hazards, panic-induced DoS, FFI safety, and async runtime mistakes. Use when auditing Rust crates, services, or libraries — particularly those with `unsafe`,… - 100ffi-code-review
Reviews Rust FFI code for type safety, memory layout compatibility, string handling, callback patterns, and unsafe boundary correctness. Use when reviewing extern blocks, #[repr(C)] types, bindgen output, or code calling C/C++ libraries. - 100rs-unsafe
CRITICAL: Use for unsafe Rust code review and FFI. Triggers on: unsafe, raw pointer, FFI, extern, transmute, *mut, *const, union, #[repr(C)], libc, std::ffi, MaybeUninit, NonNull, SAFETY comment, soundness, undefined behavior, UB, safe wrapper, memory layout, bindgen, cbindgen, CString, CStr - 100rust-unsafe-ffi
Design, implement, audit, and test unsafe Rust and foreign-function boundaries, including raw pointers, validity and aliasing invariants, MaybeUninit, layout, Pin, manual Send and Sync, allocators, C ABI declarations, callbacks, ownership transfer, unwinding, and Edition 2024… - 100unsafe-rust
Justify, document, and verify unsafe Rust — safety invariants on every unsafe block, sound safe wrappers, undefined behaviour hazards, and verification with Miri. Use when writing or reviewing unsafe code, when working across an FFI boundary, when raw pointers or transmute… - 100rust-ffi
Use when calling C libraries from Rust, generating bindings with bindgen, exporting Rust functions to C with cbindgen, writing safe wrappers over unsafe FFI, or linking system libraries.