E8012

E8012 — A body that breaks a pointer ensures of its function. The facts at a return, or at the end of the body, do not prove a clause such as ensures valid(return, n). Or the body changes a parameter that such a clause names. Or the function says ensures !frees(p), and its body gives p, or a value that it gets from p, to code that can free memory, such as a call without !frees for that parameter, a store or an mlir! block.

What the compiler reports

E8012] at {{.*}}pointer_ensures_that_a_body_breaks.vx:[[@LINE+5]]:3: `returns_another_pointer` says `valid(return, n)`, but the compiler cannot prove it at this `return`

The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.

A program that triggers it

//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Each function here has a pointer `ensures` that its body does not prove, so it gets E8012. The
// body proves the clause at each `return`, at a last value, and at the end of a body that returns
// nothing. At that point the storage of each local variable ends, and the program drops each
// value with a drop. So a function cannot promise a pointer to its own local variable. A caller
// knows only the value that it gives for each parameter, so the body must not change a parameter
// that a pointer `ensures` names.
//

import core::ptr;
import std::vec;

fn returns_another_pointer(p : *const i64, n : i64) -> *const i64
requires valid(p, n)
ensures valid(return, n)
{
  return offset(p, 1i64);
}

fn has_a_last_value_with_no_fact(p : *const i64, n : i64) -> *const i64
requires valid(p, n)
ensures initialized(return, n)
{
  p
}

fn returns_early(p : *mut i64, n : i64) -> void
requires valid(p, n)
ensures initialized(p, n)
{
  if n > 2i64 {
    return;
  }
  unsafe { assume initialized(p, n); }
}

fn reaches_its_end(p : *mut i64, n : i64) -> void
requires valid(p, n)
ensures initialized(p, n)
{
  for i in 0i64..n {
    unsafe { p[i] = 0i64; }
  }
}

fn points_at_a_local() -> *const i64
ensures valid(return, 1i64)
{
  let x = 5i64;
  let p : *const i64 = &x;
  return p;
}

// `v` holds a vector of the caller, so its drop can free the memory of `p`.
fn drops_a_vector(p : *const i64, n : i64, w : Vec<i64>) -> *const i64
requires valid(p, n)
ensures valid(return, n)
{
  let v = w;
  return p;
}

fn assigns_a_parameter(p : *const i64, mut n : i64) -> *const i64
requires valid(p, n)
ensures valid(return, n)
{
  n = n + 1i64;
  return p;
}

fn hides_a_parameter(p : *const i64, n : i64) -> *const i64
requires valid(p, n)
ensures valid(return, n)
{
  let n = 1i64;
  return p;
}

fn as_pointer(r : &i64) -> *const i64
ensures valid(return, 1i64)
{
  let p : *const i64 = r;
  unsafe { assume valid(p, 1i64); }
  return p;
}

// The result of `as_pointer` can point into `x`.
fn returns_a_pointer_that_a_call_made_from_a_local() -> *const i64
ensures valid(return, 1i64)
{
  let x = 5i64;
  return as_pointer(&x);
}

fn main() -> i32 {
  return 0;
}

From tests/frontend/fail/pointer_ensures_that_a_body_breaks.vx, which asserts this diagnostic on every commit.

Related