-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisla_lang.ott
499 lines (419 loc) · 25.4 KB
/
isla_lang.ott
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BSD 2-Clause License %
% %
% Copyright (c) 2020-2021 Alasdair Armstrong %
% Copyright (c) 2020-2021 Brian Campbell %
% Copyright (c) 2020-2021 Thibaut Pérami %
% Copyright (c) 2020-2021 Peter Sewell %
% Copyright (c) 2020-2021 Dhruv Makwana %
% Copyright (c) 2021 Michael Sammler %
% Copyright (c) 2021 Rodolphe Lepigre %
% %
% All rights reserved. %
% %
% This software was developed by the University of Cambridge Computer %
% Laboratory (Department of Computer Science and Technology) and %
% contributors, in part under DARPA/AFRL contract FA8650-18-C-7809 %
% ("CIFV"), in part funded by EPSRC Programme Grant EP/K008528/1 "REMS: %
% Rigorous Engineering for Mainstream Systems", in part funded from the %
% European Research Council (ERC) under the European Union’s Horizon %
% 2020 research and innovation programme (grant agreement No 789108, %
% "ELVER"), in part supported by the UK Government Industrial Strategy %
% Challenge Fund (ISCF) under the Digital Security by Design (DSbD) %
% Programme, to deliver a DSbDtech enabled digital platform (grant %
% 105694), and in part funded by Google. %
% %
% %
% Redistribution and use in source and binary forms, with or without %
% modification, are permitted provided that the following conditions are %
% met: %
% %
% 1. Redistributions of source code must retain the above copyright %
% notice, this list of conditions and the following disclaimer. %
% %
% 2. Redistributions in binary form must reproduce the above copyright %
% notice, this list of conditions and the following disclaimer in the %
% documentation and/or other materials provided with the distribution. %
% %
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT %
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR %
% A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT %
% HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, %
% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT %
% LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, %
% DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY %
% THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT %
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE %
% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ott grammar for the symbolic output of isla-footprint, based on the Rust isla-lib/srf/smt.rs types
% Warning: sections delimited by "%%%%% NAME %%%%%" are used by non-ott tools to split the file
% in dependent projects like read-dwarf. If you change them, check dependent project still build
% In particular, all sections must be able to be independent ott files.
embed {{ coq
(*REAL_START*)
From Coq Require Export String.
From stdpp.bitvector Require Export definitions.
Definition var_name : Set := Z.
Bind Scope Z_scope with var_name.
Definition eq_var_name (v w : var_name) : bool := Z.eqb v w.
Definition sail_name : Set := string.
Inductive annot : Set :=
| Mk_annot : annot.
}}
%%%%% terminals %%%%%
% SMT symbolic variables
metavar vvar ::= {{ ocaml int }}
{{ coq var_name }}
{{ phantom }}
{{ lex numeric }}
{{ ocamllex 'v'['0'-'9']+ }}
{{ ocamllex-of-string (function s -> int_of_string (String.sub s 1 (String.length s -1))) }}
{{ pp-raw x = string (string_of_int x) }}
{{ pp x = string ("v"^string_of_int x) }}
% sail identifiers
% register and field names, lexed with surrounding | | but represented without
% (do we want to represent these as strings, or as indices into a symbol table? the former is simpler for now)
metavar name ::= {{ ocaml string }}
{{ coq sail_name }}
{{ phantom }}
{{ lex alphanum }}
{{ ocamllex '|'[^'|''\\']+'|' }}
{{ ocamllex-of-string (function s -> String.sub s 1 (String.length s -2)) }}
{{ pp x = !^(Printf.sprintf "|%s|" x) }}
% natural numbers
metavar nat, num_bytes ::= {{ ocaml int }}
{{ coq N }}
{{ phantom }}
{{ lex numeric }}
{{ ocamllex '-'?['0'-'9']+ }}
{{ ocamllex-of-string int_of_string }}
{{ pp x = string (string_of_int x) }}
{{ pp-raw x = string (string_of_int x) }}
% integer bitvectors. "bv-24" is the bitvector that represents 24 (the bitvector size comes from the type of the expression; it's not part of the value)
metavar bvi ::= {{ ocaml int }}
{{ coq Z }}
{{ phantom }}
{{ lex numeric }}
{{ ocamllex ('b''v''-'?['0'-'9']+) }}
{{ ocamllex-of-string (function s -> int_of_string (String.sub s 2 (String.length s -2))) }}
{{ pp-raw x = string (string_of_int x) }}
{{ pp x = string ("bv"^string_of_int x) }}
% bitvector literal
% for Coq, leave as string for now so see in Coq what you see in the
% trace; later turn into whatever choice of bitvector repr type
% zero-length is allowed (e.g., for tag-only memory writes)
metavar bv ::= {{ ocaml string }}
{{ coq bvn }}
{{ phantom }}
{{ lex numeric }}
{{ ocamllex ('#''b'['0'-'1']*)|('#''x'['0'-'9''a'-'f''A'-'F']*) }}
{{ pp x = string (x) }}
{{ pp-raw x = string (x) }}
% String literal
metavar str ::= {{ ocaml string }}
{{ coq string }}
{{ phantom }}
{{ lex alphanum }}
{{ ocamllex '\"'[^'\"']*'\"' }}
{{ pp x = string (x) }}
{{ pp-raw x = string (x) }}
indexvar j,k ::= {{ ocaml int }}
{{ coq nat }}
{{ phantom }}
%%%%% parser locations %%%%%
embed
{{ ocaml
type lrng =
| UnknownRng
(* | Int of string * lrng option *)
| Generated of lrng
| Range of Lexing.position * Lexing.position
(* TODO figure out how to use that *)
exception Parse_error_locn of lrng * string
let pp_lpos p =
PPrint.string
Lexing.(
Printf.sprintf "File \"%s\", line %d, character %d" p.pos_fname p.pos_lnum
(p.pos_cnum - p.pos_bol))
let rec pp_lrng l =
match l with
| UnknownRng -> PPrint.string "Unknown position"
| Generated l -> PPrint.prefix 2 1 (PPrint.string "Generated:") (pp_lrng l)
| Range (p1, p2) ->
Lexing.(
assert (p1.pos_fname = p2.pos_fname);
if p1.pos_lnum = p2.pos_lnum then begin
assert (p1.pos_bol == p2.pos_bol);
PPrint.string
@@ Printf.sprintf "File \"%s\", line %d, character %d-%d" p1.pos_fname p1.pos_lnum
(p1.pos_cnum - p1.pos_bol) (p2.pos_cnum - p2.pos_bol)
end
else
PPrint.string
@@ Printf.sprintf "File \"%s\", line %d-%d" p1.pos_fname p1.pos_lnum p2.pos_lnum)
(* | Int(s,lo) -> Printf.sprintf "Int: %s %s" s (match lo with Some l -> pp_lrng l | None -> "none") *)
}}
%%%%% annotations %%%%%
% annotation are lrng by default can be something else. They are not pretty-printed
grammar
% TODO handle pretty printing of annotations
annot :: '' ::= {{ phantom }}
{{ ocaml 'a }}
{{ coq annot }}
{{ com Annotation }}
{{ pp l = PPrint.empty }}
{{ pp-raw l = PPrint.empty }}
%%%%% integers %%%%%
% Use a rule because the lexer will not be able to distinguish between nat and int
grammar
int :: '' ::= {{ ocaml int }} {{ coq Z }}
{{ phantom }}
{{ pp i = string (string_of_int i) }}
{{ pp-raw i = string (string_of_int i) }}
| nat :: :: pos {{ ocaml [[nat]] }} {{ coq Z.of_N [[nat]] }}
| - nat :: :: neg {{ ocaml - [[nat]] }} {{ coq Z.opp (Z.of_N [[nat]] }}
%%%%% types %%%%%
grammar
ty :: 'Ty_' ::=
| Bool :: :: Bool
| (_ BitVec nat ) :: :: BitVec
| name :: :: Enum
| ( Array ty1 ty2 ) :: :: Array
%%%%% bool %%%%%
grammar
bool :: '' ::= {{ ocaml bool }} {{ coq bool }}
{{ phantom }}
{{ pp b = if b then string "true" else string "false" }}
{{ pp-raw b = if b then string "true" else string "false" }}
| true :: :: True {{ ocaml true }} {{ coq true }}
| false :: :: False {{ ocaml false }} {{ coq false }}
%%%%% operations %%%%%
grammar
% Operations that must be applied to exactly one element
unop :: '' ::=
| not :: :: Not
| bvnot :: :: Bvnot
| bvredand :: :: Bvredand
| bvredor :: :: Bvredor
| bvneg :: :: Bvneg
| (_ extract nat nat' ) :: :: Extract
| (_ zero_extend nat ) :: :: ZeroExtend
| (_ sign_extend nat ) :: :: SignExtend
% Arithmetic operations that must be applied to exactly two elements of the same size
bvarith :: '' ::=
| bvnand :: :: Bvnand
| bvnor :: :: Bvnor
| bvxnor :: :: Bvxnor
| bvsub :: :: Bvsub
| bvudiv :: :: Bvudiv
| bvudiv_i :: :: Bvudivi
| bvsdiv :: :: Bvsdiv
| bvsdiv_i :: :: Bvsdivi
| bvurem :: :: Bvurem
| bvsrem :: :: Bvsrem
| bvsmod :: :: Bvsmod
| bvshl :: :: Bvshl
| bvlshr :: :: Bvlshr
| bvashr :: :: Bvashr
% Comparison operations, they must be applied to exactly two elements of the same size
bvcomp :: '' ::=
| bvult :: :: Bvult
| bvslt :: :: Bvslt
| bvule :: :: Bvule
| bvsle :: :: Bvsle
| bvuge :: :: Bvuge
| bvsge :: :: Bvsge
| bvugt :: :: Bvugt
| bvsgt :: :: Bvsgt
% Operations that must be applied to exactly two elements
binop :: '' ::=
| = :: :: Eq
| bvarith :: :: Bvarith
| bvcomp :: :: Bvcomp
% Arithmetic operations that can be applied to 2 or more elements of the same size
bvmanyarith :: '' ::=
| bvand :: :: Bvand
| bvor :: :: Bvor
| bvxor :: :: Bvxor
| bvadd :: :: Bvadd
| bvmul :: :: Bvmul
% Operations that can be applied to 2 or more elements
manyop :: '' ::=
| and :: :: And
| or :: :: Or
| bvmanyarith :: :: Bvmanyarith
| concat :: :: Concat
%%%%% base_val %%%%%
% Base symbolic values that can appear in both SMT expressions and register values
grammar
base_val :: 'Val_' ::=
| vvar :: :: Symbolic
| bool :: :: Bool
| bv :: :: Bits
| name :: :: Enum
% Version with register names for assumptions
grammar
assume_val :: 'AVal_' ::=
| ( name accessor_list ) :: :: Var
| bool :: :: Bool
| bv :: :: Bits
| name :: :: Enum
%%%%% exp %%%%%
grammar
exp :: '' ::= {{ aux _ annot }}
{{ auxparam 'a }}
{{ menhir-start }}
{{ menhir-start-type (Isla_lang_ast.lrng) Isla_lang_ast.exp }}
{{ lex-comment ; }}
| base_val :: :: Val
| ( unop exp ) :: :: Unop
| ( binop exp1 exp2 ) :: :: Binop
| ( manyop exp1 .. expj ) :: :: Manyop
| ( ite exp1 exp2 exp3 ) :: :: Ite
grammar
a_exp :: 'AExp_' ::= {{ aux _ annot }}
{{ auxparam 'a }}
{{ menhir-start }}
{{ menhir-start-type (Isla_lang_ast.lrng) Isla_lang_ast.a_exp }}
| assume_val :: :: Val
| ( unop a_exp ) :: :: Unop
| ( binop a_exp1 a_exp2 ) :: :: Binop
| ( manyop a_exp1 .. a_expj ) :: :: Manyop
| ( ite a_exp1 a_exp2 a_exp3 ) :: :: Ite
%%%%% smt %%%%%
grammar
smt :: '' ::= {{auxparam 'a }}
| ( declare-const vvar ty ) :: :: DeclareConst
| ( define-const vvar exp ) :: :: DefineConst
| ( assert exp ) :: :: Assert
% ( define-enum name int ( name1 .. namej ) ) defines a new enumerate type called `name`
% containing `int` different constructors: `name1`, ..., `namej`.
| ( define-enum name int ( name1 .. namej ) ) :: :: DefineEnum
%%%%% register values %%%%%
% Those are the value stored inside isla symbolic value.
% Those can contain value of type that can't appears in expressions like unit
% or string.
%%% TODO: JP: these are *patterns* ==> rename to avoid utter confusion?
%%% TP: These are the shape of register values, aka the types of registers,
%%% In trace the use case looks like a pattern, but naming it "pattern" is probably not the best
grammar
valu, addr, data, rkind, wkind, bkind, ckind, opcode :: 'RegVal_' ::=
| base_val :: :: Base
| (_ bvi int ) :: :: I % TODO: Does this still exists?
| str :: :: String
| (_ unit ) :: :: Unit
| (_ vec valu1 .. valuk ) :: :: Vector
| (_ vec nil ) :: :: VectorNil
{{ quotient-remove }}
{{ ocaml RegVal_Vector [] }}
| (_ list valu1 .. valuk ) :: :: List
| (_ list nil ) :: :: ListNil
{{ quotient-remove }}
{{ ocaml RegVal_List [] }}
| (_ struct selem1 .. selemk ) :: :: Struct
| ( name valu ) :: :: Constructor
| (_ poison ) :: :: Poison
% structure element constructor: pair of field name and value
selem :: '' ::= {{ phantom }}
{{ ocaml string * valu }}
{{ coq (sail_name * valu) }}
{{ pp se = let (n,v) = se in
parens (pp_name n ^^ blank 1 ^^ pp_valu v) }}
{{ pp-raw se = let (n,v) = se in
pp_raw_name n ^^ !^"," ^^ pp_raw_valu v }}
| ( name valu ) :: :: Struct_elem {{ ocaml (name,valu) }} {{ coq (name,valu) }}
%%%%% events %%%%%
grammar
% TODO support vector register access.
accessor :: '' ::=
| (_ field name ) :: :: Field
% TODO: change this to use lists explicitly in the event type, and change read-dwarf to match
accessor_list :: '' ::= {{ coq list accessor }}
| nil :: :: Nil
| ( accessor1 .. accessork ) :: :: Cons
arg_list :: '' ::= {{ coq list valu }}
| nil :: :: NilArgs
| valu1 .. valuk :: :: ListArgs
tag_value :: '' ::= {{ ocaml valu option }} {{ coq option valu }}
| :: :: None
| valu :: :: Some
% The matching data type is `Event` in `isla-lib/src/smt.rs`
% The order of constructors should match the one of the `Event` definition.
% The pretty printing function that need to be parsed by this is
% `write_events_with_opts` in `isla-lib/src/simplify.rs`
event :: '' ::= {{ aux _ annot }} {{auxparam 'a }}
| smt :: :: Smt
| ( branch int str ) :: :: Branch {{ com Sail trace fork }}
| ( read-reg name accessor_list valu ) :: :: ReadReg {{ com read value `valu` from register `name` }}
| ( write-reg name accessor_list valu ) :: :: WriteReg {{ com write value `valu` to register `name` }}
| ( read-mem valu rkind addr num_bytes tag_value ) :: :: ReadMem {{ com read value `valu` from memory address `addr`, with read kind `rkind`, byte width `byte\_width`, and `tag\_value` is the optional capability tag }}
| ( write-mem valu wkind addr data num_bytes tag_value ) :: :: WriteMem {{ com write value `valu` to memory address `addr`, with write kind `wkind`, byte width `num\_bytes`, `tag\_value` is the optional capability tag, and success flag `vvar` }}
| ( branch-address addr ) :: :: BranchAddress {{ com announce branch address `addr`, to induce ctrl dependency in the concurrency model }}
| ( barrier bkind ) :: :: Barrier {{ com memory barrier of kind `bkind` }}
| ( cache-op ckind addr ) :: :: CacheOp {{ com cache maintenance effect of kind `ckind`, at address `addr`, for data-cache clean etc. }}
| ( mark-reg name str ) :: :: MarkReg {{ com instrumentation to tell concurrency model to ignore certain dependencies (TODO: support marking multiple registers). Currently the str is ignore-edge or ignore-write }}
| ( cycle ) :: :: Cycle {{ com instruction boundary }}
| ( instr opcode ) :: :: Instr {{ com records the instruction `opcode` that was fetched }}
% we're not yet handling the semantics of sleeping etc.
| ( sleeping vvar ) :: :: Sleeping {{ com Arm sleeping predicate }}
| ( wake-request ) :: :: WakeRequest {{ com Arm wake request }}
| ( sleep-request ) :: :: SleepRequest {{ com Arm sleep request }}
| ( call name ) :: :: Call {{ com Calls an abstract function }}
| ( return name ) :: :: Return {{ com Returns from an abstract function }}
| ( assume-reg name accessor_list valu ) :: :: AssumeReg {{ com Notes a register assumption that Isla has been configured to use }}
| ( assume a_exp ) :: :: Assume {{ com Notes a constraint that Isla has been configured to assume }}
| ( function-assumption name valu arg_list ) :: :: FunAssume {{ com Notes that the user has supplied a precondition for a particular function }}
| ( use-function-assumption name valu arg_list ) :: :: UseFunAssume {{ com A use of a pre-declared function assumption }}
| ( abstract-call name valu arg_list ) :: :: AbstractCall {{ com A function call that Isla has abstract }}
| ( abstract-primop name valu arg_list ) :: :: AbstractPrimop {{ com A primitive operation that Isla treats as abstract }}
%%%%% instruction segments %%%%%
grammar
segment :: '' ::=
| ( name nat vvar ) :: :: Segment {{ com The segment's original name, its size in bits, the SMT variable }}
instruction_segments :: '' ::=
| ( segments segment1 .. segmentk ) :: :: Segments
%%%%% traces %%%%%
grammar
trc :: '' ::= {{ menhir-start }}
{{ auxparam 'a }}
{{ menhir-start-type Isla_lang_ast.lrng Isla_lang_ast.trc }}
{{ coq list event }}
{{ lex-comment ; }}
| ( trace event1 .. eventj ) :: :: Trace
trcs :: '' ::= {{ menhir-start }}
{{ auxparam 'a }}
{{ menhir-start-type Isla_lang_ast.lrng Isla_lang_ast.trcs }}
{{ coq list (list event) }}
{{ lex-comment ; }}
| trc1 .. trck :: :: Traces
| instruction_segments trc1 .. trck :: :: TracesWithSegments
grammar
maybe_fork :: '' ::= {{ auxparam 'a }}
| (cases str tree_trc1 .. tree_trck ) :: :: Cases
| :: :: End
tree_trc :: '' ::= {{ menhir-start }}
{{ auxparam 'a }}
{{ menhir-start-type Isla_lang_ast.lrng Isla_lang_ast.tree_trc }}
{{ lex-comment ; }}
| ( trace event1 .. eventj maybe_fork ) :: :: TreeTrace
whole_tree :: '' ::= {{ menhir-start }}
{{ auxparam 'a }}
{{ menhir-start-type Isla_lang_ast.lrng Isla_lang_ast.whole_tree }}
{{ lex-comment ; }}
| tree_trc :: :: BareTree
| instruction_segments tree_trc :: :: TreeWithSegments
substitutions
single base_val vvar :: subst_val
embed
{{ coq
(* TODO move this to traces.v when nobody depends on this file as-is *)
Notation RVal_Symbolic b := (RegVal_Base (Val_Symbolic b)).
Notation RVal_Bool b := (RegVal_Base (Val_Bool b)).
Notation RVal_Bits b := (RegVal_Base (Val_Bits b)).
Notation RVal_Enum b := (RegVal_Base (Val_Enum b)).
Definition base_val_to_exp (v : base_val) : exp := Val v Mk_annot.
Coercion base_val_to_exp : base_val >-> exp.
}}