c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

PDG: Keep track of lifetimes of stack values

Open ahomescu opened this issue 1 year ago • 2 comments

The dynamic analyzer keeps track of lifetimes of heap allocations by emitting Free events for objects being deallocated. We need something similar for stack values, to prevent issues where two locals in different functions get the same pointer, e.g.

fn foo() {
  let x = 32;
}
fn bar() {
  // We may have `addr_of!(x) == addr_of!(y)` here.
  let y = 42;
}
fn main() {
  foo();
  bar();
}

This can produce an incorrect PDG where x and y above have the same graph.

ahomescu avatar Aug 31 '24 05:08 ahomescu

Should investigate whether the "StorageLive" MIR node provides the necessary information for dynamic analysis. Do we get an event signifying the end of the lifetime of the stack allocation. If that is not a viable approach, we need to discuss possible alternative implementations before moving ahead with one of those.

@kkysen raised the question of whether we should more generally handle storage going out of scope (e.g. inside a function). We need to figure out whether we should solve the concrete issue for lighttpd or go directly for the more general solution. Depends on the complexity of solving the more general issue.

thedataking avatar Oct 07 '24 21:10 thedataking

Note so we don't forget: once this is implemented we should revert https://github.com/immunant/c2rust/commit/beb017f3d100f157e043e2bbdabdbd7ed26ae2c7 and re-enable test_ref_field.

ahomescu avatar Oct 23 '24 02:10 ahomescu