E8012
E8012 — A body that breaks a pointerensuresof its function. The facts at areturn, or at the end of the body, do not prove a clause such asensures valid(return, n). Or the body changes a parameter that such a clause names. Or the function saysensures !frees(p), and its body givesp, or a value that it gets fromp, to code that can free memory, such as a call without!freesfor that parameter, a store or anmlir!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
Contracts and verificationThe chapter covering the rule this code enforces.
All diagnosticsEvery code the compiler can emit, grouped by stage.
E8011A pointer clause that is not well formed:
valid(p, n), initialized(p, n) or !overlaps(p, q, n), where p and q are raw pointer parameters, and each clause is a requires or an ensures of its own. In an ensures, return can be the pointer. The fact of an assume is one such clause. A call through a function value or a trait would not prove a requires, so a safe function with one cannot be a function value, and a safe method of a trait impl cannot have one. A frees clause is an ensures of its own, ensures frees(p) or ensures !frees(p) for a parameter p, and a method of a trait impl cannot add a !frees that the trait method does not have. An extern function can also ensure a clause about numbers that names only its integer parameters and return.
E8013A type invariant that is not well formed. An invariant of a struct can use self, the fields of self at any depth, numbers, operators and the pointer clauses valid, initialized and overlaps. It calls no other function.