src/HOL/MicroJava/JVM/JVMDefensive.thy
changeset 35416 d8d7d1b785af
parent 35102 cc7a0b9f938c
child 35417 47ee18b6ae32
equal deleted inserted replaced
35342:4dc65845eab3 35416:d8d7d1b785af
     2     Author:     Gerwin Klein
     2     Author:     Gerwin Klein
     3 *)
     3 *)
     4 
     4 
     5 header {* \isaheader{A Defensive JVM} *}
     5 header {* \isaheader{A Defensive JVM} *}
     6 
     6 
     7 theory JVMDefensive imports JVMExec begin
     7 theory JVMDefensive
       
     8 imports JVMExec
       
     9 begin
     8 
    10 
     9 text {*
    11 text {*
    10   Extend the state space by one element indicating a type error (or
    12   Extend the state space by one element indicating a type error (or
    11   other abnormal termination) *}
    13   other abnormal termination) *}
    12 datatype 'a type_error = TypeError | Normal 'a
    14 datatype 'a type_error = TypeError | Normal 'a
    14 
    16 
    15 abbreviation
    17 abbreviation
    16   fifth :: "'a \<times> 'b \<times> 'c \<times> 'd \<times> 'e \<times> 'f \<Rightarrow> 'e"
    18   fifth :: "'a \<times> 'b \<times> 'c \<times> 'd \<times> 'e \<times> 'f \<Rightarrow> 'e"
    17   where "fifth x == fst(snd(snd(snd(snd x))))"
    19   where "fifth x == fst(snd(snd(snd(snd x))))"
    18 
    20 
       
    21 fun isAddr :: "val \<Rightarrow> bool" where
       
    22   "isAddr (Addr loc) = True"
       
    23 | "isAddr v          = False"
    19 
    24 
    20 consts isAddr :: "val \<Rightarrow> bool"
    25 fun isIntg :: "val \<Rightarrow> bool" where
    21 recdef isAddr "{}"
    26   "isIntg (Intg i) = True"
    22   "isAddr (Addr loc) = True"
    27 | "isIntg v        = False"
    23   "isAddr v          = False"
       
    24 
    28 
    25 consts isIntg :: "val \<Rightarrow> bool"
    29 definition isRef :: "val \<Rightarrow> bool" where
    26 recdef isIntg "{}"
       
    27   "isIntg (Intg i) = True"
       
    28   "isIntg v        = False"
       
    29 
       
    30 constdefs
       
    31   isRef :: "val \<Rightarrow> bool"
       
    32   "isRef v \<equiv> v = Null \<or> isAddr v"
    30   "isRef v \<equiv> v = Null \<or> isAddr v"
    33 
    31 
    34 
    32 primrec check_instr :: "[instr, jvm_prog, aheap, opstack, locvars, 
    35 consts
    33                   cname, sig, p_count, nat, frame list] \<Rightarrow> bool" where
    36   check_instr :: "[instr, jvm_prog, aheap, opstack, locvars, 
       
    37                   cname, sig, p_count, nat, frame list] \<Rightarrow> bool"
       
    38 primrec 
       
    39   "check_instr (Load idx) G hp stk vars C sig pc mxs frs = 
    34   "check_instr (Load idx) G hp stk vars C sig pc mxs frs = 
    40   (idx < length vars \<and> size stk < mxs)"
    35   (idx < length vars \<and> size stk < mxs)"
    41 
    36 
    42   "check_instr (Store idx) G hp stk vars Cl sig pc mxs frs = 
    37 | "check_instr (Store idx) G hp stk vars Cl sig pc mxs frs = 
    43   (0 < length stk \<and> idx < length vars)"
    38   (0 < length stk \<and> idx < length vars)"
    44 
    39 
    45   "check_instr (LitPush v) G hp stk vars Cl sig pc mxs frs = 
    40 | "check_instr (LitPush v) G hp stk vars Cl sig pc mxs frs = 
    46   (\<not>isAddr v \<and> size stk < mxs)"
    41   (\<not>isAddr v \<and> size stk < mxs)"
    47 
    42 
    48   "check_instr (New C) G hp stk vars Cl sig pc mxs frs = 
    43 | "check_instr (New C) G hp stk vars Cl sig pc mxs frs = 
    49   (is_class G C \<and> size stk < mxs)"
    44   (is_class G C \<and> size stk < mxs)"
    50 
    45 
    51   "check_instr (Getfield F C) G hp stk vars Cl sig pc mxs frs = 
    46 | "check_instr (Getfield F C) G hp stk vars Cl sig pc mxs frs = 
    52   (0 < length stk \<and> is_class G C \<and> field (G,C) F \<noteq> None \<and> 
    47   (0 < length stk \<and> is_class G C \<and> field (G,C) F \<noteq> None \<and> 
    53   (let (C', T) = the (field (G,C) F); ref = hd stk in 
    48   (let (C', T) = the (field (G,C) F); ref = hd stk in 
    54     C' = C \<and> isRef ref \<and> (ref \<noteq> Null \<longrightarrow> 
    49     C' = C \<and> isRef ref \<and> (ref \<noteq> Null \<longrightarrow> 
    55       hp (the_Addr ref) \<noteq> None \<and> 
    50       hp (the_Addr ref) \<noteq> None \<and> 
    56       (let (D,vs) = the (hp (the_Addr ref)) in 
    51       (let (D,vs) = the (hp (the_Addr ref)) in 
    57         G \<turnstile> D \<preceq>C C \<and> vs (F,C) \<noteq> None \<and> G,hp \<turnstile> the (vs (F,C)) ::\<preceq> T))))" 
    52         G \<turnstile> D \<preceq>C C \<and> vs (F,C) \<noteq> None \<and> G,hp \<turnstile> the (vs (F,C)) ::\<preceq> T))))" 
    58 
    53 
    59   "check_instr (Putfield F C) G hp stk vars Cl sig pc mxs frs = 
    54 | "check_instr (Putfield F C) G hp stk vars Cl sig pc mxs frs = 
    60   (1 < length stk \<and> is_class G C \<and> field (G,C) F \<noteq> None \<and> 
    55   (1 < length stk \<and> is_class G C \<and> field (G,C) F \<noteq> None \<and> 
    61   (let (C', T) = the (field (G,C) F); v = hd stk; ref = hd (tl stk) in 
    56   (let (C', T) = the (field (G,C) F); v = hd stk; ref = hd (tl stk) in 
    62     C' = C \<and> isRef ref \<and> (ref \<noteq> Null \<longrightarrow> 
    57     C' = C \<and> isRef ref \<and> (ref \<noteq> Null \<longrightarrow> 
    63       hp (the_Addr ref) \<noteq> None \<and> 
    58       hp (the_Addr ref) \<noteq> None \<and> 
    64       (let (D,vs) = the (hp (the_Addr ref)) in 
    59       (let (D,vs) = the (hp (the_Addr ref)) in 
    65         G \<turnstile> D \<preceq>C C \<and> G,hp \<turnstile> v ::\<preceq> T))))" 
    60         G \<turnstile> D \<preceq>C C \<and> G,hp \<turnstile> v ::\<preceq> T))))" 
    66 
    61 
    67   "check_instr (Checkcast C) G hp stk vars Cl sig pc mxs frs =
    62 | "check_instr (Checkcast C) G hp stk vars Cl sig pc mxs frs =
    68   (0 < length stk \<and> is_class G C \<and> isRef (hd stk))"
    63   (0 < length stk \<and> is_class G C \<and> isRef (hd stk))"
    69 
    64 
    70   "check_instr (Invoke C mn ps) G hp stk vars Cl sig pc mxs frs =
    65 | "check_instr (Invoke C mn ps) G hp stk vars Cl sig pc mxs frs =
    71   (length ps < length stk \<and> 
    66   (length ps < length stk \<and> 
    72   (let n = length ps; v = stk!n in
    67   (let n = length ps; v = stk!n in
    73   isRef v \<and> (v \<noteq> Null \<longrightarrow> 
    68   isRef v \<and> (v \<noteq> Null \<longrightarrow> 
    74     hp (the_Addr v) \<noteq> None \<and>
    69     hp (the_Addr v) \<noteq> None \<and>
    75     method (G,cname_of hp v) (mn,ps) \<noteq> None \<and>
    70     method (G,cname_of hp v) (mn,ps) \<noteq> None \<and>
    76     list_all2 (\<lambda>v T. G,hp \<turnstile> v ::\<preceq> T) (rev (take n stk)) ps)))"
    71     list_all2 (\<lambda>v T. G,hp \<turnstile> v ::\<preceq> T) (rev (take n stk)) ps)))"
    77   
    72   
    78   "check_instr Return G hp stk0 vars Cl sig0 pc mxs frs =
    73 | "check_instr Return G hp stk0 vars Cl sig0 pc mxs frs =
    79   (0 < length stk0 \<and> (0 < length frs \<longrightarrow> 
    74   (0 < length stk0 \<and> (0 < length frs \<longrightarrow> 
    80     method (G,Cl) sig0 \<noteq> None \<and>    
    75     method (G,Cl) sig0 \<noteq> None \<and>    
    81     (let v = hd stk0;  (C, rT, body) = the (method (G,Cl) sig0) in
    76     (let v = hd stk0;  (C, rT, body) = the (method (G,Cl) sig0) in
    82     Cl = C \<and> G,hp \<turnstile> v ::\<preceq> rT)))"
    77     Cl = C \<and> G,hp \<turnstile> v ::\<preceq> rT)))"
    83  
    78  
    84   "check_instr Pop G hp stk vars Cl sig pc mxs frs = 
    79 | "check_instr Pop G hp stk vars Cl sig pc mxs frs = 
    85   (0 < length stk)"
    80   (0 < length stk)"
    86 
    81 
    87   "check_instr Dup G hp stk vars Cl sig pc mxs frs = 
    82 | "check_instr Dup G hp stk vars Cl sig pc mxs frs = 
    88   (0 < length stk \<and> size stk < mxs)"
    83   (0 < length stk \<and> size stk < mxs)"
    89 
    84 
    90   "check_instr Dup_x1 G hp stk vars Cl sig pc mxs frs = 
    85 | "check_instr Dup_x1 G hp stk vars Cl sig pc mxs frs = 
    91   (1 < length stk \<and> size stk < mxs)"
    86   (1 < length stk \<and> size stk < mxs)"
    92 
    87 
    93   "check_instr Dup_x2 G hp stk vars Cl sig pc mxs frs = 
    88 | "check_instr Dup_x2 G hp stk vars Cl sig pc mxs frs = 
    94   (2 < length stk \<and> size stk < mxs)"
    89   (2 < length stk \<and> size stk < mxs)"
    95 
    90 
    96   "check_instr Swap G hp stk vars Cl sig pc mxs frs =
    91 | "check_instr Swap G hp stk vars Cl sig pc mxs frs =
    97   (1 < length stk)"
    92   (1 < length stk)"
    98 
    93 
    99   "check_instr IAdd G hp stk vars Cl sig pc mxs frs =
    94 | "check_instr IAdd G hp stk vars Cl sig pc mxs frs =
   100   (1 < length stk \<and> isIntg (hd stk) \<and> isIntg (hd (tl stk)))"
    95   (1 < length stk \<and> isIntg (hd stk) \<and> isIntg (hd (tl stk)))"
   101 
    96 
   102   "check_instr (Ifcmpeq b) G hp stk vars Cl sig pc mxs frs =
    97 | "check_instr (Ifcmpeq b) G hp stk vars Cl sig pc mxs frs =
   103   (1 < length stk \<and> 0 \<le> int pc+b)"
    98   (1 < length stk \<and> 0 \<le> int pc+b)"
   104 
    99 
   105   "check_instr (Goto b) G hp stk vars Cl sig pc mxs frs =
   100 | "check_instr (Goto b) G hp stk vars Cl sig pc mxs frs =
   106   (0 \<le> int pc+b)"
   101   (0 \<le> int pc+b)"
   107 
   102 
   108   "check_instr Throw G hp stk vars Cl sig pc mxs frs =
   103 | "check_instr Throw G hp stk vars Cl sig pc mxs frs =
   109   (0 < length stk \<and> isRef (hd stk))"
   104   (0 < length stk \<and> isRef (hd stk))"
   110 
   105 
   111 constdefs
   106 definition check :: "jvm_prog \<Rightarrow> jvm_state \<Rightarrow> bool" where
   112   check :: "jvm_prog \<Rightarrow> jvm_state \<Rightarrow> bool"
       
   113   "check G s \<equiv> let (xcpt, hp, frs) = s in
   107   "check G s \<equiv> let (xcpt, hp, frs) = s in
   114                (case frs of [] \<Rightarrow> True | (stk,loc,C,sig,pc)#frs' \<Rightarrow> 
   108                (case frs of [] \<Rightarrow> True | (stk,loc,C,sig,pc)#frs' \<Rightarrow> 
   115                 (let  (C',rt,mxs,mxl,ins,et) = the (method (G,C) sig); i = ins!pc in
   109                 (let  (C',rt,mxs,mxl,ins,et) = the (method (G,C) sig); i = ins!pc in
   116                  pc < size ins \<and> 
   110                  pc < size ins \<and> 
   117                  check_instr i G hp stk loc C sig pc mxs frs'))"
   111                  check_instr i G hp stk loc C sig pc mxs frs'))"
   118 
   112 
   119 
   113 
   120   exec_d :: "jvm_prog \<Rightarrow> jvm_state type_error \<Rightarrow> jvm_state option type_error"
   114 definition exec_d :: "jvm_prog \<Rightarrow> jvm_state type_error \<Rightarrow> jvm_state option type_error" where
   121   "exec_d G s \<equiv> case s of 
   115   "exec_d G s \<equiv> case s of 
   122       TypeError \<Rightarrow> TypeError 
   116       TypeError \<Rightarrow> TypeError 
   123     | Normal s' \<Rightarrow> if check G s' then Normal (exec (G, s')) else TypeError"
   117     | Normal s' \<Rightarrow> if check G s' then Normal (exec (G, s')) else TypeError"
   124 
   118 
   125 
   119