Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leakage Traces #38

Merged
merged 42 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a454294
defined assembly-level leakage trace, defined leakage_of_instr
OwenConoly Aug 11, 2023
dfb34e2
updated leakage_of_instr to use a monad
OwenConoly Aug 26, 2023
1ccf982
defined RiscvProgramWithLeakage (haven't used it for anything yet); a…
OwenConoly Nov 3, 2023
dc093fb
added instruction logging to run1
OwenConoly Nov 3, 2023
c0baecf
added instruction logging to MetricMinimalMMIO; other things don't co…
OwenConoly Nov 3, 2023
dacf7ce
started getting rid of "logInstr" primitive; we have "leakEvent" now.
OwenConoly Nov 5, 2023
e889f00
changed leakage_of_instr to accept a getRegister that returns a machi…
OwenConoly Nov 5, 2023
f4d77bd
updated run1
OwenConoly Nov 5, 2023
dbc592d
made MinimalMMIO build with leakEvent thing
OwenConoly Nov 5, 2023
6d372e5
started trying to make the thing build.
OwenConoly Nov 6, 2023
0218c3c
wow it compiles
OwenConoly Nov 6, 2023
e81b8a1
turns out I was unable to build the compiler not because I needed to …
OwenConoly Nov 6, 2023
0e6de80
made leakage_of_instr a bit nicer to work with
OwenConoly Nov 6, 2023
0d78de0
changed leakage trace to use word instead of Z
OwenConoly Nov 18, 2023
9dfe76e
started making things build again. not done.
OwenConoly Nov 18, 2023
996df79
made things be inferred more easily
OwenConoly Nov 18, 2023
b6f4d40
fix implicit args
OwenConoly Nov 18, 2023
7d71fee
fixed implicit args
OwenConoly Nov 18, 2023
6f331f5
Changed assembly semantics as follows.
OwenConoly Dec 13, 2023
a0eecd5
oops, turns out slt stands for "store less than", nothing to do
OwenConoly Dec 13, 2023
c161084
not sure
OwenConoly Dec 28, 2023
d840c1a
anonymize
OwenConoly Jul 12, 2024
3a47d9d
clean things up: delete print statements, fix whitespace, fix merging…
OwenConoly Aug 2, 2024
f96904a
move leakage stuff out of Decode.v
OwenConoly Aug 2, 2024
fa470ca
move identity_Monad where it belongs
OwenConoly Aug 4, 2024
ff149f8
Decided how to write leakage spec when idk what instruction should leak.
OwenConoly Aug 4, 2024
ab888f9
fixup LeakageOfInstr to not make claims about instructions i don't un…
OwenConoly Aug 5, 2024
b13861b
adapt other files to leakage_of_instr returning option type
OwenConoly Aug 5, 2024
2d146e1
leak instruction-fetch addressses
OwenConoly Aug 5, 2024
c1d5121
fix remaining errors, so that it compiles
OwenConoly Aug 5, 2024
a757ed2
bring LeakageOfInstr (mostly) in compliance with Zkt spec.
OwenConoly Aug 5, 2024
92a3acb
always leak the instruction
OwenConoly Aug 5, 2024
9b511c4
whitespace, etc
OwenConoly Aug 5, 2024
1759c3b
simplify Run.v
OwenConoly Aug 5, 2024
a9305e0
remove empty types
OwenConoly Aug 5, 2024
40b2ec8
stop making claims about leakage of invalid instructions
OwenConoly Aug 5, 2024
cc3c3fa
define LeakEvent in MinimalCSRs
OwenConoly Aug 5, 2024
9949d74
simplify handling of instructions whose leakage trace has not been de…
OwenConoly Aug 6, 2024
fc7e8a0
whitespace, etc
OwenConoly Aug 6, 2024
6511099
make type inference nicer with coercion and different implicits.
OwenConoly Aug 6, 2024
1d4b2d1
import option_map2 instead of defining it
OwenConoly Aug 7, 2024
8587bd4
fix Examples
OwenConoly Aug 7, 2024
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
7 changes: 7 additions & 0 deletions src/riscv/Platform/MaterializeRiscvProgram.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Section Riscv.
| GetPrivMode
| SetPrivMode (_ : PrivMode)
| Fence (_ : MachineInt) (_ : MachineInt)
| LeakEvent (_ : LeakageEvent)
| GetPC
| SetPC (_ : word)
| StartCycle
Expand All @@ -52,6 +53,7 @@ Section Riscv.
| GetPrivMode => PrivMode
| SetPrivMode _ => unit
| Fence _ _ => unit
| LeakEvent _ => unit
| GetPC => word
| SetPC _ => unit
| StartCycle => unit
Expand Down Expand Up @@ -84,6 +86,11 @@ Section Riscv.
endCycleEarly A := act (EndCycleEarly A) ret;
|}.

Global Instance MaterializeWithLeakage : RiscvProgramWithLeakage := {|
RVP := Materialize;
leakEvent a := act (LeakEvent a) ret
|}.

(* Not (yet) in Riscv monad, but added here because it's useful to initialize
nextPc to pc+4 at the beginning of each cycle instead of having to maintain
a nextPc=pc+4 invariant everywhere *)
Expand Down
5 changes: 5 additions & 0 deletions src/riscv/Platform/MetricMinimal.v
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ Section Riscv.
endCycleEarly{A} := liftL0 (addMetricInstructions 1) (@endCycleEarly _ _ _ _ _ A);
}.

Instance IsMetricRiscvMachineWithLeakage: @RiscvProgramWithLeakage _ _ _ (OState MetricRiscvMachine) _ _ _ := {
RVP := IsMetricRiscvMachine;
leakEvent := liftL1 id leakEvent;
}.

Arguments Memory.load_bytes: simpl never.
Arguments Memory.store_bytes: simpl never.

Expand Down
8 changes: 8 additions & 0 deletions src/riscv/Platform/MetricRiscvMachine.v
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ Section Machine.
fun items '(mkMetricRiscvMachine mach mc) =>
(mkMetricRiscvMachine (withLogItems items mach) mc).

Definition withLeakageEvent: LeakageEvent -> MetricRiscvMachine -> MetricRiscvMachine :=
fun event '(mkMetricRiscvMachine mach mc) =>
(mkMetricRiscvMachine (withLeakageEvent event mach) mc).

Definition withLeakageEvents: list LeakageEvent -> MetricRiscvMachine -> MetricRiscvMachine :=
fun events '(mkMetricRiscvMachine mach mc) =>
(mkMetricRiscvMachine (withLeakageEvents events mach) mc).

Definition forgetMetrics(m: MetricRiscvMachine): RiscvMachine := m.(getMachine).
Definition addMetrics(m: RiscvMachine)(mc: MetricLog): MetricRiscvMachine :=
mkMetricRiscvMachine m mc.
Expand Down
12 changes: 11 additions & 1 deletion src/riscv/Platform/MetricSane.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Section Sane.
Context {mem: map.map word byte}.
Context {M: Type -> Type}.
Context {MM: Monad M}.
Context {RVM: RiscvProgram M word}.
Context {RVM: RiscvProgramWithLeakage}.
Context {PRParams: PrimitivesParams M MetricRiscvMachine}.
Context {mcomp_sat_ok: mcomp_sat_spec PRParams}.

Expand Down Expand Up @@ -151,6 +151,14 @@ Section Sane.

Ltac t := repeat (simpl; unfold when; repeat t_step').

Lemma leakage_of_instr_sane: forall inst,
mcomp_sane (leakage_of_instr getRegister inst).
Proof.
intros.
destruct inst as [inst | inst | inst | inst | inst | inst | inst | inst | inst | inst];
simpl; destruct inst; t.
Qed.

Lemma execute_sane: forall inst,
mcomp_sane (Execute.execute inst).
Proof.
Expand All @@ -165,6 +173,8 @@ Section Sane.
unfold run1. intros.
apply Bind_sane; [apply getPC_sane|intros].
apply Bind_sane; [apply loadWord_sane|intros].
apply Bind_sane; [apply leakage_of_instr_sane|intros].
apply Bind_sane; [apply leakEvent_sane|intros].
apply Bind_sane; [apply execute_sane|intros].
apply endCycleNormal_sane.
Qed.
Expand Down
11 changes: 9 additions & 2 deletions src/riscv/Platform/Minimal.v
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ Section Riscv.
(* fail hard if exception is thrown because at the moment, we want to prove that
code output by the compiler never throws exceptions *)
endCycleEarly{A: Type} := fail_hard;
}.

Instance IsRiscvMachineWithLeakage: @RiscvProgramWithLeakage _ _ _ (OState RiscvMachine) _ _ _ := {
RVP := IsRiscvMachine;
leakEvent e := fail_hard;
}.

Arguments Memory.load_bytes: simpl never.
Expand All @@ -116,7 +121,7 @@ Section Riscv.
| |- _ => reflexivity
| |- _ => progress (
unfold computation_satisfies, computation_with_answer_satisfies,
IsRiscvMachine,
IsRiscvMachine, IsRiscvMachineWithLeakage,
valid_register,
is_initial_register_value,
get, put, fail_hard,
Expand Down Expand Up @@ -229,7 +234,8 @@ Section Riscv.
Proof.
constructor.
all: intros;
unfold getRegister, setRegister,
unfold IsRiscvMachine, IsRiscvMachineWithLeakage, RVP,
getRegister, setRegister,
loadByte, loadHalf, loadWord, loadDouble,
storeByte, storeHalf, storeWord, storeDouble,
getPC, setPC,
Expand Down Expand Up @@ -261,4 +267,5 @@ End Riscv.

(* needed because defined inside a Section *)
#[global] Existing Instance IsRiscvMachine.
#[global] Existing Instance IsRiscvMachineWithLeakage.
#[global] Existing Instance MinimalSatisfiesPrimitives.
1 change: 1 addition & 0 deletions src/riscv/Platform/MinimalCSRs.v
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Section Riscv.
| Machine => postF tt mach
| User | Supervisor => False
end
| LeakEvent _
OwenConoly marked this conversation as resolved.
Show resolved Hide resolved
| MakeReservation _
| ClearReservation _
| CheckReservation _
Expand Down
1 change: 1 addition & 0 deletions src/riscv/Platform/MinimalMMIO.v
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Section Riscv.
postF tt (withNextPc (word.add mach.(getPc) (word.of_Z 4)) mach)
| EndCycleNormal => fun postF postA => postF tt (updatePc mach)
| EndCycleEarly _ => fun postF postA => postA (updatePc mach) (* ignores postF containing the continuation *)
| LeakEvent e => fun postF postA => postF tt (withLeakageEvent e mach)
| MakeReservation _
| ClearReservation _
| CheckReservation _
Expand Down
5 changes: 5 additions & 0 deletions src/riscv/Platform/MinimalMMIO_Post.v
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ Section Riscv.
- exact (post tt (withPc mach.(getNextPc) (withNextPc (word.add mach.(getNextPc) (word.of_Z 4)) mach))).
Defined.

Instance IsRiscvMachineWithLeakage: @RiscvProgramWithLeakage _ _ _ (Post RiscvMachine) _ _ _ := {|
RVP := IsRiscvMachine;
leakEvent _ := fun _ _ => False;
|}.

Definition MinimalMMIOPrimitivesParams: PrimitivesParams (Post RiscvMachine) RiscvMachine := {|
Primitives.mcomp_sat A (m: Post RiscvMachine A) mach post := m mach post;
Primitives.is_initial_register_value x := True;
Expand Down
41 changes: 25 additions & 16 deletions src/riscv/Platform/RiscvMachine.v
Original file line number Diff line number Diff line change
Expand Up @@ -141,40 +141,49 @@ Section Machine.
getMem: Mem;
getXAddrs: XAddrs;
getLog: list LogItem;
getTrace: list LeakageEvent;
}.

Definition withRegs: Registers -> RiscvMachine -> RiscvMachine :=
fun regs2 '(mkRiscvMachine regs1 pc nextPC mem xAddrs log) =>
mkRiscvMachine regs2 pc nextPC mem xAddrs log.
fun regs2 '(mkRiscvMachine regs1 pc nextPC mem xAddrs log trace) =>
mkRiscvMachine regs2 pc nextPC mem xAddrs log trace.

Definition withPc: word -> RiscvMachine -> RiscvMachine :=
fun pc2 '(mkRiscvMachine regs pc1 nextPC mem xAddrs log) =>
mkRiscvMachine regs pc2 nextPC mem xAddrs log.
fun pc2 '(mkRiscvMachine regs pc1 nextPC mem xAddrs log trace) =>
mkRiscvMachine regs pc2 nextPC mem xAddrs log trace.

Definition withNextPc: word -> RiscvMachine -> RiscvMachine :=
fun nextPC2 '(mkRiscvMachine regs pc nextPC1 mem xAddrs log) =>
mkRiscvMachine regs pc nextPC2 mem xAddrs log.
fun nextPC2 '(mkRiscvMachine regs pc nextPC1 mem xAddrs log trace) =>
mkRiscvMachine regs pc nextPC2 mem xAddrs log trace.

Definition withMem: Mem -> RiscvMachine -> RiscvMachine :=
fun mem2 '(mkRiscvMachine regs pc nextPC mem1 xAddrs log) =>
mkRiscvMachine regs pc nextPC mem2 xAddrs log.
fun mem2 '(mkRiscvMachine regs pc nextPC mem1 xAddrs log trace) =>
mkRiscvMachine regs pc nextPC mem2 xAddrs log trace.

Definition withXAddrs: XAddrs -> RiscvMachine -> RiscvMachine :=
fun xAddrs2 '(mkRiscvMachine regs pc nextPC mem xAddrs1 log) =>
mkRiscvMachine regs pc nextPC mem xAddrs2 log.
fun xAddrs2 '(mkRiscvMachine regs pc nextPC mem xAddrs1 log trace) =>
mkRiscvMachine regs pc nextPC mem xAddrs2 log trace.

Definition withLog: list LogItem -> RiscvMachine -> RiscvMachine :=
fun log2 '(mkRiscvMachine regs pc nextPC mem xAddrs log1) =>
mkRiscvMachine regs pc nextPC mem xAddrs log2.
fun log2 '(mkRiscvMachine regs pc nextPC mem xAddrs log1 trace) =>
mkRiscvMachine regs pc nextPC mem xAddrs log2 trace.

Definition withLogItem: LogItem -> RiscvMachine -> RiscvMachine :=
fun item '(mkRiscvMachine regs pc nextPC mem xAddrs log) =>
mkRiscvMachine regs pc nextPC mem xAddrs (item :: log).
fun item '(mkRiscvMachine regs pc nextPC mem xAddrs log trace) =>
mkRiscvMachine regs pc nextPC mem xAddrs (item :: log) trace.

Definition withLogItems: list LogItem -> RiscvMachine -> RiscvMachine :=
fun items '(mkRiscvMachine regs pc nextPC mem xAddrs log) =>
mkRiscvMachine regs pc nextPC mem xAddrs (items ++ log).
fun items '(mkRiscvMachine regs pc nextPC mem xAddrs log trace) =>
mkRiscvMachine regs pc nextPC mem xAddrs (items ++ log) trace.

Definition withLeakageEvent: LeakageEvent -> RiscvMachine -> RiscvMachine :=
fun event '(mkRiscvMachine regs pc nextPC mem xAddrs log trace) =>
mkRiscvMachine regs pc nextPC mem xAddrs log (event :: trace).

Definition withLeakageEvents: list LeakageEvent -> RiscvMachine -> RiscvMachine :=
fun events '(mkRiscvMachine regs pc nextPC mem xAddrs log trace) =>
mkRiscvMachine regs pc nextPC mem xAddrs log (events ++ trace).

Definition Z32s_to_bytes(l: list Z): list byte :=
List.flat_map (fun z => HList.tuple.to_list (LittleEndian.split 4 z)) l.

Expand Down
9 changes: 6 additions & 3 deletions src/riscv/Platform/Run.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ Require Import riscv.Utility.Utility.

Section Riscv.

Context {mword: Type}.
Context {width} {BW : Bitwidth width} {mword: word.word width}.
Context {MW: MachineWidth mword}.

Context {M: Type -> Type}.
Context {MM: Monad M}.
Context {RVP: RiscvProgram M mword}.
Context {RVM: RiscvProgramWithLeakage}.
Context {RVS: RiscvMachine M mword}.

Definition run1(iset: InstructionSet):
M unit :=
pc <- getPC;
inst <- loadWord Fetch pc;
execute (decode iset (combine 4 inst));;
let inst' := decode iset (combine 4 inst) in
leakage_event <- leakage_of_instr getRegister inst';
leakEvent leakage_event;;
execute inst';;
endCycleNormal.

(* Note: We cannot use
Expand Down
Loading