Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ _CoqProject
.env
.DS_Store
**/.DS_Store
*.specs
.lia.cache
.nia.cache
.nra.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Precondition:
* - 0 <= n <= INT_MAX.
* - 0 <= key <= 255.
* - If n > 0, buf is non-null and points to the first element of a contiguous array of at least n valid unsigned char objects, properly aligned for unsigned char, and the storage remains alive for the entire call.
* - The function reads from buf[0, n).
* - The function writes to buf[0, n).
* - The caller holds exclusive read-write ownership of buf[0, n) for the duration of the call.
* - Let `buf_values_at_entry` denote the sequence of unsigned char values stored in buf[0, n) at function entry.
*/
unsigned char xor_mask_and_checksum(unsigned char* buf, int n, unsigned char key) {
unsigned int sum = 0;

/* Invariant:
* - At loop head: 0 <= i <= n.
* - There exist `pref` and `suf` such that `buf_values_at_entry = pref ++ suf` and `length(pref) = i`.
* - The current contents of `buf` over `[0, i)` equal the sequence obtained by XOR-ing each element of `pref` with `key`.
* - The current contents of `buf` over `[i, n)` equal `suf`.
* - `sum` equals the sum mod 256 of the elements of the sequence obtained by XOR-ing each element of `pref` with `key`.
*/
for (int i = 0; i < n; ++i) {
unsigned char masked = static_cast<unsigned char>(buf[i] ^ key);
buf[i] = masked;
sum = (sum + static_cast<unsigned int>(masked)) & 255u;
}

/* Postcondition:
* - Let `buf_values_at_exit` denote the sequence in buf[0, n) on normal return.
* - The return value equals the sum mod 256 of the elements of `buf_values_at_exit`.
* - Output segment `[0, n)`: `buf_values_at_exit` equals the sequence obtained by XOR-ing each element of `buf_values_at_entry` with `key`.
* - All memory not described above is unchanged.
* - No exception can propagate out of this function.
*/
return static_cast<unsigned char>(sum);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Precondition:
* - 0 <= n <= INT_MAX.
* - INT_MIN <= min_cmd <= INT_MAX.
* - INT_MIN <= max_cmd <= INT_MAX.
* - min_cmd <= max_cmd.
* - If n > 0, cmd is non-null and points to the first element of a contiguous array of at least n valid int objects, properly aligned for int, and the storage remains alive for the entire call.
* - The function reads from cmd[0, n).
* - The function writes to cmd[0, n).
* - The caller holds exclusive read-write ownership of cmd[0, n) for the duration of the call.
* - Let cmd_values_at_entry denote the sequence of int values stored in cmd[0, n) at function entry.
*/
int saturate_actuator_commands(int cmd[], int n, int min_cmd, int max_cmd) {
int saturated = 0;
int i = 0;

/* Invariant:
* - At loop head: 0 <= i <= n.
* - There exist pref and suf such that cmd_values_at_entry = pref ++ suf and length(pref) = i.
* - The current contents of cmd over [0, i) equal pref with each element replaced by min_cmd if below min_cmd, by max_cmd if above max_cmd, otherwise unchanged.
* - The current contents of cmd over [i, n) equal suf.
* - 0 <= saturated <= i.
* - saturated equals the number of elements of pref that are < min_cmd or > max_cmd.
*/
for (; i < n; ++i) {
const int v = cmd[i];
if (v < min_cmd) {
cmd[i] = min_cmd;
saturated++;
} else if (v > max_cmd) {
cmd[i] = max_cmd;
saturated++;
}
}

/* Postcondition:
* - Let cmd_values_at_exit denote the sequence in cmd[0, n) on normal return.
* - The return value equals the number of elements of cmd_values_at_entry that are < min_cmd or > max_cmd.
* - Output segment [0, n): cmd_values_at_exit equals cmd_values_at_entry with each element replaced by min_cmd if below min_cmd, by max_cmd if above max_cmd, otherwise unchanged.
* - All memory not described above is unchanged.
* - No exception can propagate out of this function.
*/
return saturated;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc"
rocq_name = "step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc"
proof_dir = "proof"
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"arguments": [
"/usr/local/bin/clang",
"-c",
"src/ArrayCopy_While/ArrayCopy_While__mode_novel__ex_01__block_0__c748fa5e.cpp"
],
"directory": "/home/saad/dev/workspace/psi/backend/ai/data_gen/output/step3_scaffold_two_loopinvgo_rerun_promptupd_persona_v2_step3retry4/step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc",
"file": "/home/saad/dev/workspace/psi/backend/ai/data_gen/output/step3_scaffold_two_loopinvgo_rerun_promptupd_persona_v2_step3retry4/step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc/src/ArrayCopy_While/ArrayCopy_While__mode_novel__ex_01__block_0__c748fa5e.cpp"
},
{
"arguments": [
"/usr/local/bin/clang",
"-c",
"src/IntegerDivide/IntegerDivide__mode_novel__ex_01__block_0__29d09bb7.cpp"
],
"directory": "/home/saad/dev/workspace/psi/backend/ai/data_gen/output/step3_scaffold_two_loopinvgo_rerun_promptupd_persona_v2_step3retry4/step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc",
"file": "/home/saad/dev/workspace/psi/backend/ai/data_gen/output/step3_scaffold_two_loopinvgo_rerun_promptupd_persona_v2_step3retry4/step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc/src/IntegerDivide/IntegerDivide__mode_novel__ex_01__block_0__29d09bb7.cpp"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(env
(_
(flags :standard -warn-error +A -w +A-4-29-40..45-48-58..59-63..65-68-70)
(ocamlopt_flags :standard -O3 -unbox-closures)
(rocq
(rocqdep_flags :standard -w +all)
(flags (:standard -w
-notation-incompatible-prefix
-w
-notation-overridden
-w
-custom-entry-overridden
-w
-redundant-canonical-projection
-w
-ambiguous-paths
-w
+automatic-prop-lowering
-w
+deprecated-hint-without-locality
-w
+deprecated-instance-without-locality
-w
+unknown-option
-w
+future-coercion-class-field
)))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(lang dune 3.21)
(using rocq 0.11)
(name step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc)

(generate_opam_files)

(package
(name step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc)
(synopsis "step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc")
(depends
(rocq-skylabs-cpp2v (= dev))
elpi-cpp
rocq-skylabs-auto-core
rocq-skylabs-auto-cpp
rocq-skylabs-cpp-stdlib))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.8)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Require Import step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc.proof.prelude.spec.


Require step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc.src.ArrayCopy_While.ArrayCopy_While__mode_novel__ex_01__block_0__c748fa5e_cpp.
#[local] Notation source := step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc.src.ArrayCopy_While.ArrayCopy_While__mode_novel__ex_01__block_0__c748fa5e_cpp.module.

(*@BEGIN:"saturate_actuator_commands(int *, int, int, int)"@*)(*@END@*)

(*@BEGIN:"insert"@*)(*@END@*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(*@BEGIN:"exports"@*)
Require Export step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc.proof.ArrayCopy_While.ArrayCopy_While__mode_novel__ex_01__block_0__c748fa5e_cpp.proof.saturate_actuator_commands.
(*@END@*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
Require Import step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc.proof.prelude.proof.



Require step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc.src.ArrayCopy_While.ArrayCopy_While__mode_novel__ex_01__block_0__c748fa5e_cpp.
#[local] Notation source := step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc.src.ArrayCopy_While.ArrayCopy_While__mode_novel__ex_01__block_0__c748fa5e_cpp.module.
Require Import step2_cpp_two_loopinvgo_rerun_promptupd_persona_v2__shared__dede18dc.proof.ArrayCopy_While.ArrayCopy_While__mode_novel__ex_01__block_0__c748fa5e_cpp.spec.

(* wp hints *)
Export arrayLR_hints.
(* general big sep automation *)
Export hints.big_sep_hints.

Export OnlyProvableFEqual.

Export ptr_normalization_hints.

(* Export prim.AgressivePrimR. *)

Export normalize.only_provable_norm.

Section with_cpp.
Context `{Σ : cpp_logic} `{MOD : source ⊧ σ}.
Set Default Proof Using "MOD".

Open Scope Z_scope.
#[global,program] Instance CaseSplit_Zlt eql eqr (a b : Z) :
TCMaybe (SolveArith (b ≤ a + 1)) eql ->
TCMaybe (SolveArith (a ≤ b)) eqr ->
CaseSplit (a <? b) :=
{ on_true := if eql then b = a + 1 else a < b ;
on_false := if eqr then a = b else b <= a }.
Next Obligation.
intros ? ? ? ? L R. destruct (a <? b) eqn:?; constructor.
+ destruct L as [ [L] |]; destruct R as [[R]|]; lia.
+ destruct R as [[R]|]; lia.
Qed.
Next Obligation.
intros ? ? ? ? L R.
+ destruct L as [ [L] |]; destruct R as [[R]|]; lia.
Qed.

(*@BEGIN:"proof"@*)
Lemma saturate_actuator_commands_ok : verify[source] "saturate_actuator_commands(int *, int, int, int)".
Proof.
verify_spec; go.

wp_for (fun ρ => Exists (i sat : Z) cmd_values_at_exit,
i_addr |-> intR 1$m i **
saturated_addr |-> intR 1$m sat **
cmd_addr |-> ptrR<"int"> 1$m cmd ** (* ← stack slot only *)
n_addr |-> intR 1$m n **
min_cmd_addr |-> intR 1$m min_cmd **
max_cmd_addr |-> intR 1$m max_cmd **
cmd |-> arrayLR "int" 0 i (λ x : Z, intR 1$m x) (sliceZ 0 0 i cmd_values_at_exit) **
cmd |-> arrayLR "int" i n (λ x : Z, intR 1$m x) (sliceZ 0 i n cmd_values_at_entry) **
[| cmd_values_at_exit =
(fun v =>
if bool_decide (v < min_cmd) then min_cmd
else if bool_decide (max_cmd < v) then max_cmd
else v) <$> cmd_values_at_entry
|] **
(** TODO: *)
(* [| sat = lengthZ (filter (λ v : Z, bool_decide (v < min_cmd ∨ max_cmd < v)) (sliceZ 0 0 i cmd_values_at_exit)) |] ** *)
[| (0 <= i <= n)%Z |] **
[| (0 <= sat <= i)%Z |])%Z; go.
(* Unset SsrIdents.
rename _t_ into i. *)
wp_if; go.
{ wp_if => MinCmp; go.
{
rewrite list_lookupZ_fmap.
(* iPureIntro. *)
destruct (cmd_values_at_entry !! _) eqn:CmdLookup; simplify_eq/=.
vc_split; work.
}
wp_if => MaxCmp; go.
{
rewrite list_lookupZ_fmap.
(* iPureIntro. *)
destruct (cmd_values_at_entry !! _) eqn:CmdLookup; simplify_eq/=.
vc_split; work.
vc_split; work.
}
{
rewrite list_lookupZ_fmap.
(* iPureIntro. *)
destruct (cmd_values_at_entry !! _) eqn:CmdLookup; simplify_eq/=.
vc_split; work.
vc_split; work.
}
}
go.
wfocus [| _ |] "". {
iPureIntro.
rewrite sliceZ_self //. {
rewrite map_fmap.
apply fmap_ext_in => x Hin.
repeat vc_split; lia.
}
rewrite lengthN_map. lia.
}
Import prim.AgressivePrimR.
work.
(* TODO last goal *)
enough (_t_0 = length (List.filter (λ v : Z, (v <? min_cmd) || (max_cmd <? v)) cmd_values_at_entry)) by done.
admit.
(* TODO: add to invariant
[| sat = lengthZ (filter (λ v : Z, bool_decide (v < min_cmd ∨ max_cmd < v)) (sliceZ 0 0 i cmd_values_at_exit)) |] ** *)
all: fail.
Admitted.
(*@END@*)

(*@BEGIN:"insert"@*)
(*
FAILED STEP4 PROOF ATTEMPT
lemma: saturate_actuator_commands_ok
task_id: proof/ArrayCopy_While/ArrayCopy_While__mode_novel__ex_01__block_0__c748fa5e_cpp/proof/saturate_actuator_commands.v#Lemma:saturate_actuator_commands_ok
status: Failure
failure_reason: ["Other", "Agent failed: The next request would exceed the request_limit of 30"]
final_tactic: backtracked(1)
tactic_trace:
01. [OK] verify_spec; go
02. [OK] wp_for (fun ρ => Exists i sat,
i_addr |-> intR 1$m i **
saturated_addr |-> intR 1$m sat **
cmd_addr |-> ptrR<"int"> 1$m _v_3 **
n_addr |-> intR 1$m _v_4 **
min_cmd_addr |-> intR 1$m _v_5 **
max_cmd_addr |-> intR 1$m v **
[| (0 <= i <= _v_4)%Z |] **
[| (0 <= sat <=...(truncated)

03. [OK] iSplit; [iPureIntro; lia|gather_all].
04. [OK] iSplit; [iPureIntro; lia|iFrame].
05. [OK] destruct GUARDS.
06. [OK] backtracked(1)
07. [OK] backtracked(1)
08. [OK] wp_for (fun ρ => Exists i sat,
i_addr |-> intR 1$m i **
saturated_addr |-> intR 1$m sat **
cmd_addr |-> ptrR<"int"> 1$m _v_3 **
n_addr |-> intR 1$m _v_4 **
min_cmd_addr |-> intR 1$m _v_5 **
max_cmd_addr |-> intR 1$m v **
[| (0 <= i)%Z |] **
[| (sat = i)%Z |])%Z; g...(truncated)
09. [OK] wp_if.
10. [OK] backtracked(2)
11. [OK] wp_for (fun ρ => Exists i sat,
i_addr |-> intR 1$m i **
saturated_addr |-> intR 1$m sat **
cmd_addr |-> ptrR<"int"> 1$m _v_3 **
n_addr |-> intR 1$m _v_4 **
min_cmd_addr |-> intR 1$m _v_5 **
max_cmd_addr |-> intR 1$m v **
[| (0 <= i)%Z |] **
[| (0 <= sat <= i)%Z |]...(truncated)
12. [OK] wp_if.
13. [OK] go.
14. [OK] iExists _, _.
15. [OK] destruct GUARDS.
16. [OK] iSplit.
17. [OK] go.
18. [OK] unfold ptrR in *.
19. [OK] iPureIntro.
20. [OK] iFrame.
21. [OK] unfold valid_ptr.
22. [FAIL] Qed.
error: Qed failed - proof not complete
23. [FAIL] backtracked(20)
error: Backtrack failed
24. [OK] Search valid_ptr.
25. [OK] backtracked(1)
*)
(*@END@*)
End with_cpp.
Loading