File: bcv-simple-model.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (310 lines) | stat: -rw-r--r-- 9,719 bytes parent folder | download | duplicates (2)
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
(in-package "ACL2")

(include-book "bcv-model")

;; (defun max-stack (sig-frame)
;;   (g 'max-stack sig-frame))

(defun bcv-simple-check-PUSH (inst sig-frame)
  (declare (ignore inst))
  (<= (+ 1 (g 'op-stack sig-frame))
      (max-stack sig-frame)))

;; (defun advance-pc (sig-frame)
;;   (s 'pc 
;;      (+ 1 (g 'pc sig-frame))
;;      sig-frame))


;; (defun sig-frame-push-value (v sig-frame)
;;   (declare (ignore v))
;;   (s 'op-stack
;;      (+ 1 (g 'op-stack sig-frame))
;;      sig-frame))


(defun bcv-simple-execute-PUSH (inst sig-frame)
  (list 
   (advance-pc 
    (sig-frame-push-value (arg inst)
                          sig-frame))))


(defun bcv-simple-check-POP (inst sig-frame)
  (declare (ignore inst))
  (<= 1 (g 'op-stack sig-frame)))


;; (defun sig-frame-pop-value (sig-frame)
;;   (if (zp (g 'op-stack sig-frame))
;;          sig-frame
;;     (s 'op-stack 
;;        (-  (g 'op-stack sig-frame) 1)
;;        sig-frame)))


(defun bcv-simple-execute-POP (inst sig-frame)
  (declare (ignore inst))
  (list (advance-pc 
         (sig-frame-pop-value sig-frame))))

;;; The only important thing we care (in this example) is the size of the
;;; operand stack.
;;;
;;; Our BCV examine a program, method by method.
;;;
;;; We will show that verified program will never violate the operand stack
;;; size limit.


(defun bcv-simple-check-IFEQ (inst sig-frame)
  (declare (ignore inst))
  (<= 1 (g 'op-stack sig-frame)))


(defun bcv-simple-execute-IFEQ (inst sig-frame)
  (list 
   (advance-pc (sig-frame-pop-value sig-frame))
   (s 'pc (arg inst)
      (sig-frame-pop-value sig-frame))))

;; note IFEQ has two possible return state! 




;; (defun bcv-simple-check-INVOKE (inst sig-frame)
;;   (let* ((method-name (arg inst))
;;          (method-table (g 'method-table sig-frame))
;;          (method      (binding method-name method-table))
;;          (nargs       (g 'nargs method)))
;;     (and (bound? method-name method-table)
;;          (<= 0 (g 'max-stack (binding method-name method-table)))

;;          (equal (g 'method-table 
;;                    (cdr (assoc-equal 0 (g 'sig-vector (binding method-name
;;                                                                method-table)))))
;;                 method-table)
;; ;;; 
;; ;;; note: this is WRONG!!! 
;; ;;; because nothing will satisfy it!!! 
;; ;;; Fri Nov 11 11:49:29 2005
;; ;;; 
;;          (equal (g 'op-stack
;;                    (cdr (assoc-equal 0 (g 'sig-vector (binding method-name
;;                                                                method-table)))))
;;                 0)
;;          (equal (g 'method-name (binding method-name
;;                                          method-table))
;;                 method-name)
;;          ;; we need these above three to assert that next state 
;;          ;; is compatible with 
;;          (integerp nargs)
;;          (<= 0 nargs)
;;          (<= nargs (g 'op-stack sig-frame))
;;          (<= (+ 1 (- (g 'op-stack sig-frame)
;;                      nargs))
;;              (max-stack sig-frame)))))


(defun bcv-simple-check-INVOKE (inst sig-frame)
  (let* ((method-name  (arg inst))                                            ;; ctx maps 
         (method-table (g 'method-table  sig-frame))                ;; the value into the frames of sig-vector
         (method       (binding method-name method-table))
         (nargs        (g 'nargs method)))
    (and (bound? method-name method-table)
         (<= 0 (g 'max-stack (binding method-name method-table)))
;;          (equal (g 'method-table 
;;                    (cdr (assoc-equal 0 (g 'sig-vector (binding method-name
;;                                                                method-table)))))
;;                 method-table) ;; add one level redirect! Fri Nov 11 12:03:50 2005
;;          (equal (g 'op-stack
;;                    (cdr (assoc-equal 0 (g 'sig-vector (binding method-name
;;                                                                method-table)))))
;;                 0)
;;          (equal (g 'method-name (binding method-name
;;                                          method-table))
;;                 method-name)
         ;; we need these above three to assert that next state 
         ;; is compatible with 
         (integerp nargs)
         (<= 0 nargs)
         (<= nargs (g 'op-stack sig-frame))
         (<= (+ 1 (- (g 'op-stack sig-frame)
                     nargs))
             (max-stack sig-frame)))))




;;; Note: 
;;;   make sure it has enough space for arguments
;;;   make sure the return value will not overflow the stack!! 
;;;        
;;; values are always of size 1!! (in JVM it could be size two)


(defun bcv-simple-check-HALT (inst sig-frame)
    (declare (ignore inst sig-frame))
    t)

(defun bcv-simple-execute-HALT (inst sig-frame)
    (declare (ignore inst sig-frame))
    nil)


(defun bcv-simple-check-RETURN (inst sig-frame)
    (declare (ignore inst))
    (<= 1 (g 'op-stack sig-frame)))


(defun bcv-simple-execute-RETURN (inst sig-frame)
    (declare (ignore inst sig-frame))
    nil)
    

(defun bcv-simple-check-step-pre (inst sig-frame)
  (let* ((opcode (op-code inst)))
    (cond ((equal opcode 'INVOKE)
           (bcv-simple-check-INVOKE inst sig-frame))
          ((equal opcode 'PUSH)
           (bcv-simple-check-PUSH inst sig-frame))
          ((equal opcode 'IFEQ)
           (bcv-simple-check-IFEQ inst sig-frame))
          ((equal opcode 'HALT)
           (bcv-simple-check-HALT inst sig-frame))
          ((equal opcode 'POP)
           (bcv-simple-check-POP inst sig-frame))
          ((equal opcode 'RETURN)
           (bcv-simple-check-RETURN inst sig-frame))
          (t nil))))

;; (defun sig-frame-pop-n (n sig-frame)
;;   (if (zp n) 
;;       sig-frame
;;     (sig-frame-pop-n (- n 1) 
;;                      (sig-frame-pop-value sig-frame))))


(defun bcv-simple-execute-INVOKE (inst sig-frame)
  (let* ((method-name (arg inst))
         (method-table (g 'method-table sig-frame))
         (method      (binding method-name method-table))
         (nargs       (g 'nargs method))
         (ret         (g 'ret method)))
    (list 
     (advance-pc 
      (sig-frame-push-value ret 
                            (sig-frame-pop-n nargs sig-frame))))))
  


(defun bcv-simple-execute-step (inst sig-frame)
  (let* ((opcode (op-code inst)))
    (cond ((equal opcode 'INVOKE) 
           (bcv-simple-execute-INVOKE inst sig-frame))
          ((equal opcode 'PUSH)
           (bcv-simple-execute-PUSH inst sig-frame))
          ((equal opcode 'IFEQ)
           (bcv-simple-execute-IFEQ inst sig-frame))
          ((equal opcode 'HALT)
           (bcv-simple-execute-HALT inst sig-frame))
          ((equal opcode 'POP)
           (bcv-simple-execute-POP inst sig-frame))
          ((equal opcode 'RETURN)
           (bcv-simple-execute-RETURN inst sig-frame))
          (t nil))))


;; (defun sig-local-compatible (slocals glocals)
;;   (declare (ignore slocals glocals))
;;   ;; temp implementation
;;   t)

;; (defun sig-opstack-compatible (sframe gframe)
;;   (equal (g 'op-stack sframe)
;;          (g 'op-stack gframe)))


;; (defun sig-frame-compatible (sframe gframe)
;;   (and (equal (g 'pc sframe)
;;               (g 'pc gframe))
;;        (equal (g 'max-stack sframe)
;;               (g 'max-stack gframe))
;;        (equal (g 'method-table sframe)
;;               (g 'method-table gframe))
;;        (sig-opstack-compatible sframe gframe)
;;        (sig-local-compatible (g 'locals sframe)
;;                              (g 'locals gframe))))



(defun all-next-state-safe (sig-frames post-vector)
  (if (endp sig-frames) t
    (let* ((sig-frame (car sig-frames))
           (rest (cdr sig-frames))
           (pc   (g 'pc sig-frame))
           (post (binding pc post-vector)))
      (and (sig-frame-compatible 
             sig-frame 
             post)
           (all-next-state-safe rest post-vector)))))
             
      

(defun bcv-simple-inst (pc inst sig-vector)
  (let* ((pre-vector sig-vector)
         (post-vector sig-vector)
         (pre   (binding pc pre-vector)))
    (and (bcv-simple-check-step-pre inst pre)
         (all-next-state-safe 
          (bcv-simple-execute-step inst pre) 
          post-vector))))
          


(defun bcv-simple-method1 (pc code sig-vector)
  (declare (xargs :measure (len code)))
  (if (endp code) t
    (let* ((inst (car code)))
      (and (bcv-simple-inst pc inst sig-vector)
           (bcv-simple-method1 (+ 1 pc) (cdr code) sig-vector)))))
     

(defun sig-init-locals (method) 
  ;; tmp, we only deal with number of values on the operand stack!
  (declare (ignore method))
  nil)

;; (defun sig-method-init-frame (method)
;;   (s 'max-stack 
;;      (g 'max-stack method)
;;      (s 'pc 0 
;;         (s 'op-stack 0 
;;            (s 'locals 
;;               (sig-init-locals method) nil)))))
  

;;; even for such an simple model, we may need to show that execution does not
;;; fell off the end of the code.
;;;
;;;   which is not easy! especially, when we our BCV is expressed this way!!
;;; 
;;; Sat Oct 8 21:29:41 2005. this is simplified problem, we designed our m-run
;;; to "halt" when the execution fell off the end of code (next instruction is
;;; not well defined), in a real JVM, we need to show execution never "fall"
;;; off the end of the code


(defun bcv-simple-method (method method-table)
  (and (sig-frame-compatible 
        (sig-method-init-frame method method-table)
        (binding 0 (g 'sig-vector method)))
       (bcv-simple-method1 0 
                    (g 'code method) 
                    (g 'sig-vector method))))


;----------------------------------------------------------------------