Library SemiconcreteExecution

Require Export Bool.
Require Export SetoidDec.
Require Export SemiconcreteStates.
Require Export List.
Require Export Outcomes.

Semiconcrete outcomes


Definition scoutcome A := outcome state A.
Definition scmutatora A := state -> scoutcome A.
Definition scmutator := scmutatora unit.

Semiconcrete basic mutators


Definition assume_prop(P: Prop): scmutator :=
  fun st =>
  demonicT (fun H: P => single st tt).

Definition update_store(x: var)(v: Z): scmutator :=
  fun st => let (s, h) := st in
  single (store_update s x v, h) tt.

Definition update_store_n(xs: list var)(vs: list Z): scmutator :=
  fun st => let (s, h) := st in
  single (store_updates s xs vs, h) tt.

Definition havoc1(x: var): scmutator :=
  v <- pick_demonic Z;
  update_store x v.

Fixpoint havoc(xs: list var): scmutator :=
  match xs with
    [] => noop
  | x::xs => havoc1 x; havoc xs
  end.

Definition with_store{A}(s: store)(op: scmutatora A): scmutatora A :=
  fun st => let (s0, h) := st in
  bindf (op (s, h)) (fun a st => let (_, h) := st in single (s0, h) a).

Definition with_store' s (C: scmutator): scmutatora store :=
  fun st => let (s0, h) := st in
  bind (C (s, h)) (fun st' => let (s', h') := st' in single (s0, h') s').

Definition with_local_store{A}(op: scmutatora A): scmutatora A :=
  fun st => let (s, h) := st in
  bindf (op (s, h)) (fun a st => let (_, h) := st in single (s, h) a).

Definition eval_mut(e: expr): scmutatora Z :=
  fun st => let (s, h) := st in
  single st (eval s e).

Definition evals_mut(es: list expr) :=
  fun st: state => let (s, h) := st in
  single st (map (eval s) es).

Definition beval_mut(b: bexpr): scmutatora bool :=
  fun st => let (s, h) := st in
  single st (beval s b).

Definition assume_bexpr(b: bexpr): scmutator :=
  fun st => let (s, h) := st in
  if beval s b then single st tt else oblock.

Definition assert_bexpr(b: bexpr): scmutator :=
  fun st => let (s, h) := st in
  if beval s b then
    single st tt
  else
    ofail.

Definition clear_heap: scmutator :=
  fun st => let (s, h) := st in
  single (s, empty_heap) tt.

Definition leakcheck: scmutator :=
  fun st => let (s, h) := st in
  angelicT (fun H: h = empty_heap =>
    oblock
  ).

Definition cons_chunk p ls n: scmutatora (list Z) :=
  fun st => let (s, h) := st in
  angelicT (fun vs =>
  angelicT (fun Hlength: length vs = n =>
  angelicT (fun h' =>
  angelicT (fun _: h = heap_add (singleton_heap (Chunk p (ls ++ vs))) h' =>
    single (s, h') vs
  )))).

Definition cons_chunk_1_1 p l: scmutatora Z := vs <- cons_chunk p [l] 1; yield (hd 0 vs).
Definition cons_pointsto := cons_chunk_1_1 pointsto_pred.
Definition cons_malloc_block := cons_chunk_1_1 malloc_block_pred.

Fixpoint cons_pointstos l n :=
  match n with
    O => noop
  | S n =>
    cons_pointsto l;
    cons_pointstos (l + 1) n
  end.

Definition prod_chunk(c: chunk): scmutator :=
  fun st => let (s, h) := st in
  single (s, heap_add (singleton_heap c) h) tt.

Fixpoint prod_pointstos(l: Z)(n: nat): scmutator :=
  match n with
    O => noop
  | S n =>
    v <- pick_demonic Z;
    prod_chunk (pointsto l v); prod_pointstos (l + 1) n
  end.

Semiconcrete execution


Fixpoint consume(a: asn): scmutator :=
  match a with
    BAsn b => assert_bexpr b
  | PAsn p es xs =>
    ls <- evals_mut es;
    vs <- cons_chunk p ls (length xs);
    update_store_n xs vs
  | IfAsn b a1 a2 =>
    fork (
      assume_bexpr b;
      consume a1
    ) (
      assume_bexpr (b_not b);
      consume a2
    )
  | SepAsn a1 a2 =>
    consume a1;
    consume a2
  end.

Fixpoint produce(a: asn): scmutator :=
  match a with
    BAsn b => assume_bexpr b
  | PAsn p es xs =>
    ls <- evals_mut es;
    vs <- iter (pick_demonic Z) (length xs);
    update_store_n xs vs;
    prod_chunk (Chunk p (ls ++ vs))
  | IfAsn b a1 a2 =>
    fork (
      assume_bexpr b;
      produce a1
    ) (
      assume_bexpr (b_not b);
      produce a2
    )
  | SepAsn a1 a2 =>
    produce a1;
    produce a2
  end.

Section SemiconcreteExecution.

Variable routine_specs: spec_table.
Variable pred_defs: pred_table.

Fixpoint sc_exec(c: cmd): scmutator :=
  match c with
  | Assign x e =>
    v <- eval_mut e;
    update_store x v
  | Malloc x n =>
    l <- pick_demonic Z;
    update_store x l;
    assume_prop (0 < l)%Z;
    prod_chunk (malloc_block l (Z.of_nat n));
    prod_pointstos l n
  | Free e =>
    l <- eval_mut e;
    n <- cons_malloc_block l;
    cons_pointstos l (Z.to_nat n)
  | Read x e =>
    l <- eval_mut e;
    v <- cons_pointsto l;
    prod_chunk (pointsto l v);
    update_store x v
  | Write e1 e2 =>
    l <- eval_mut e1;
    v <- eval_mut e2;
    _ <- cons_pointsto l;
    prod_chunk (pointsto l v)
  | Call x r es =>
    match StringMap.find r routine_specs with
      None => fail
    | Some (RoutineSpec xs pre post) =>
      if length es == length xs then
        vs <- evals_mut es;
        s1 <- with_store' (store_updates store0 xs vs) (consume pre);
        v <- pick_demonic Z;
        with_store (store_update s1 result v) (produce post);
        update_store x v
      else
        fail
    end
  | IfCmd b c1 c2 =>
    fork (
      assume_bexpr b;
      sc_exec c1
    ) (
      assume_bexpr (b_not b);
      sc_exec c2
    )
  | While b a c =>
    with_local_store (consume a);
    havoc (targets c);
    fork (
      clear_heap; (
        with_local_store (produce a);
        assume_bexpr b;
        sc_exec c;
        with_local_store (consume a)
      );
      leakcheck
    ) (
      with_local_store (produce a);
      assume_bexpr (b_not b)
    )
  | Seq c1 c2 =>
    sc_exec c1;
    sc_exec c2
  | Open p es =>
    match pred_defs p with
      None => fail
    | Some (PredDef xs a) =>
      if le_dec (length es) (length xs) then
        ls <- evals_mut es;
        vs <- cons_chunk p ls (length xs - length es);
        with_store (store_updates store0 xs (ls ++ vs)) (
          produce a
        )
      else
        fail
    end
  | Close p es =>
    match pred_defs p with
      None => fail
    | Some (PredDef xs a) =>
      if length es == length xs then
        vs <- evals_mut es;
        with_store (store_updates store0 xs vs) (
          consume a
        );
        prod_chunk (Chunk p vs)
      else
        fail
    end
  | Skip => noop
  | Message m => noop
  end.

Section RoutineDefs.

Variable rdefs: routine_table.

Definition valid_routine r (rspec: routine_spec) :=
  match rdefs r with
    None => False
  | Some (RoutineDef xs c) =>
    let (xs', pre, post) := rspec in
      safe (
        state0 |>
        vs <- iter (pick_demonic Z) (length xs');
        with_store store0 (
          s1 <- with_store' (store_updates store0 xs' vs) (produce pre);
          v <- with_store (store_updates store0 xs vs) (sc_exec c; eval_mut result);
          with_store (store_update s1 result v) (consume post)
        );
        leakcheck
      )
  end.

Definition valid_routines :=
  Forall
    (fun el => valid_routine (fst el) (snd el))
    (StringMap.elements routine_specs).

Definition valid_command c :=
  safe (
    state0 |> sc_exec c
  ).

Definition valid_program c :=
  pred_defs pointsto_pred = None /\
  pred_defs malloc_block_pred = None /\
  valid_routines /\
  valid_command c.

End RoutineDefs.

End SemiconcreteExecution.