Semantic Data Type Lifting for Memory Safety

306 concepts149 papers2 proposals

Legacy software written in low-level languages like C remains a primary source of critical security vulnerabilities. These flaws generally fall into two categories: spatial violations (such as buffer overflows) and temporal violations (such as use-after-free errors). While modern systems programming languages like Rust offer compile-time guarantees that eliminate these risks entirely, manually rewriting millions of lines of legacy code is prohibitively expensive and prone to human error.

To bridge this gap, researchers have focused on automated language migration. However, traditional transpilers suffer from a fundamental limitation: they translate code syntactically rather than semantically. To truly secure legacy software during migration, automated tools must perform semantic data type lifting—recovering the implicit operational intent of low-level memory structures and translating them into high-level, type-safe abstractions.


The Failure of Syntactic Translation

When a traditional automated transpiler encounters a C pointer, its default behavior is to emit a direct syntactic equivalent in the target language. For example, a char* or void* in C is converted into a raw pointer (*mut c_char or *mut c_void) wrapped inside an unsafe block in Rust.

As Zhang et al. argue in Ownership guided C to Rust translation, this literal syntax-based translation fails to provide the safety guarantees of the target language. Because raw pointers bypass the compiler's borrow checker, the resulting code retains the exact same spatial and temporal vulnerabilities as the original C source. The code looks like Rust, but it behaves like C.

The core issue is that C pointers are semantically overloaded. A single int* syntax could represent:

  • An array of integers (requiring spatial bounds checking).
  • An optional value that might be NULL (requiring null-safety checks).
  • An out-parameter passed by reference.
  • An owned resource responsible for freeing memory.

Without recovering this implicit semantic context, an automated tool cannot safely transition code out of unsafe blocks.


What is Semantic Data Type Lifting?

Semantic data type lifting is the process of mapping low-level, untyped memory representations to high-level language primitives that encapsulate safety invariants. Instead of preserving the literal memory layout, lifting identifies the intended use of a data structure and replaces it with a type-safe abstraction.

Low-Level C Representation Inferred Semantic Intent Lifted Safe Rust Abstraction
char* ptr (with manual free) Sole ownership of a dynamically allocated heap object Box<str> or String
int* ptr (can be NULL) A value that may or may not be present Option<&int> or Option<Box<int>>
void* buffer + size_t len A contiguous, bounded region of memory &[u8] (Slice)

In a user study detailed in Translating C To Rust: Lessons from a User Study, Li et al. observed that human developers naturally perform semantic lifting when rewriting code. By prioritizing architectural intent over line-by-line equivalence, humans achieve temporal safety entirely through compile-time checks, maintaining performance within a narrow margin of the original unsafe code.


Automating the Lift: Static Analysis and LLMs

To automate this semantic recovery, modern frameworks combine static program analysis with Large Language Models (LLMs). Static analysis provides rigorous constraints regarding pointer lifetimes and aliasing, while LLMs propose idiomatic refactoring patterns that match those constraints.

[Legacy C Code] ──> [Static Ownership Analysis] ──> [LLM Refactoring Engine] ──> [Safe, Idiomatic Rust]
                          (Infers Lifetimes,              (Lifts Raw Pointers
                          Bounds & Aliasing)              to Box/Option/Slices)

Several distinct strategies have emerged to implement this pipeline:

  1. Static Ownership Inference: The tool Crown, developed by Zhang et al., uses a novel static ownership analysis to determine whether a pointer is responsible for freeing memory or merely borrowing it. This allows the tool to automatically transform raw pointers into safe abstractions like Box and Option without human intervention.
  2. Multi-Step Translation Pipelines: As shown by Zhou et al. in LLM-Driven Multi-step Translation from C to Rust using Static Analysis, translating code in a single step often introduces bugs. Their tool, SACTOR, uses a two-step "unidiomatic-to-idiomatic" pipeline. It first generates a literal translation, then uses static analysis and automated Foreign Function Interface (FFI) testing to progressively lift unsafe blocks into safe code.
  3. Project-Scale Context Management: Legacy codebases feature complex inter-code dependencies and macros that break localized translation. To address this, Cai et al. introduced RustMap: Towards Project-Scale C-to-Rust Migration via Program Analysis and LLM, which breaks whole projects into manageable units and uses a feedback loop with compilation testing to guarantee functional equivalence.
  4. Symbolic Bounds Inference: For partial migrations or safe dialects like Checked C, Mohammed et al. developed the Memory Safety Assistant (MSA). As detailed in LLM Assistance for Memory Safety, MSA combines LLMs with symbolic static analysis to automatically infer complex arithmetic bounds annotations, successfully refactoring legacy codebases with high precision.

The Danger of Escape Hatches and Boundaries

The ultimate goal of semantic lifting is to eliminate the need for memory safety "escape hatches." When automated tools or developers fail to lift types completely, they rely on manual overrides—such as Rust's unsafe blocks or manual lifetime annotations—which introduce severe security risks.

In A Closer Look at the Security Risks in the Rust Ecosystem, Zheng et al. conducted a large-scale empirical study revealing that memory safety and concurrency issues still account for two-thirds of vulnerabilities in real-world Rust repositories. These risks are disproportionately concentrated in regions where developers explicitly opt out of compiler-enforced safety checks.

Furthermore, compilers treat manual lifetime annotations as absolute truth. If a developer provides incorrect metadata during a manual lift, the compiler's safety checks are invalidated. Nitin et al. demonstrated this vulnerability in Yuga: Automatically Detecting Lifetime Annotation Bugs in the Rust Language, showing that incorrect manual annotations cause the borrow checker to become blind to latent use-after-free errors.

Cross-Language Friction

A similar breakdown occurs at the boundary where safe, managed languages interoperate with unmanaged C libraries.

  • Garbage Collection Mismatches: In An Empirical Study of CGO Usage in Go Projects, Chen et al. highlighted that integrating a garbage-collected runtime with manually managed C memory creates a risk where the garbage collector might relocate or reclaim memory currently accessed by external code, leading to memory corruption.
  • Native Library Attack Surfaces: As argued by Askar et al. in MALintent: Coverage Guided Intent Fuzzing Framework for Android, even when application logic is written in a memory-safe language (like Java or Kotlin on Android), the inclusion of native C/C++ libraries via foreign function interfaces introduces low-level vulnerabilities that bypass the host runtime's protections.

By completely lifting low-level code into safe, native abstractions of a single language, developers eliminate these fragile cross-language boundaries and ensure that the entire codebase is uniformly protected by compile-time invariants.

History & citationVersion 1 · 2026-10-04

Current · Permanent link

View frozen version 1

Cite this direction

Latest version (a living page):

@misc{lacuna_use_after_free_vulnerability_46b602db,
  title = {use-after-free vulnerability},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/use-after-free-vulnerability/rdr_46b602dbb80b4d638aa94d1f8f6abb84},
  note = {Latest version of a living research direction; cite a version for a fixed text. Accessed 2026-11-19},
}

This exact version (version 1, frozen text):

@misc{lacuna_use_after_free_vulnerability_46b602db_v1,
  title = {use-after-free vulnerability},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/use-after-free-vulnerability/rdr_46b602dbb80b4d638aa94d1f8f6abb84/v/1},
  note = {Version 1, frozen 2026-10-04. Accessed 2026-11-19},
}
About

These directions overlap with this one through shared papers.

See something wrong? Report a mistake