| author | haftmann |
| Thu, 25 Oct 2007 13:52:02 +0200 | |
| changeset 25188 | a342dec991aa |
| parent 24858 | a3a81e73f552 |
| child 25204 | 36cf92f63a44 |
| permissions | -rw-r--r-- |
| 20925 | 1 |
(* Title: Pure/General/secure.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Secure critical operations. |
|
6 |
*) |
|
7 |
||
8 |
signature SECURE = |
|
9 |
sig |
|
| 20977 | 10 |
val set_secure: unit -> unit |
| 20925 | 11 |
val is_secure: unit -> bool |
| 22144 | 12 |
val use_text: string -> (string -> unit) * (string -> 'a) -> bool -> string -> unit |
|
25188
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
13 |
val evaluate: string * (unit -> 'a) option ref -> string |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
14 |
-> (string -> unit) * (string -> 'b) -> bool -> string -> 'a |
| 21770 | 15 |
val use_file: (string -> unit) * (string -> 'a) -> bool -> string -> unit |
| 23978 | 16 |
val use_noncritical: string -> unit |
| 20925 | 17 |
val use: string -> unit |
18 |
val commit: unit -> unit |
|
| 20992 | 19 |
val execute: string -> string |
20 |
val system: string -> int |
|
| 20925 | 21 |
end; |
22 |
||
23 |
structure Secure: SECURE = |
|
24 |
struct |
|
25 |
||
| 20992 | 26 |
(** secure flag **) |
| 20925 | 27 |
|
28 |
val secure = ref false; |
|
29 |
||
| 20977 | 30 |
fun set_secure () = secure := true; |
| 20925 | 31 |
fun is_secure () = ! secure; |
32 |
||
|
22567
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents:
22144
diff
changeset
|
33 |
fun deny_secure msg = if is_secure () then error msg else (); |
| 20925 | 34 |
|
35 |
||
| 20992 | 36 |
(** critical operations **) |
37 |
||
38 |
(* ML evaluation *) |
|
| 20925 | 39 |
|
40 |
fun secure_mltext () = deny_secure "Cannot evaluate ML source in secure mode"; |
|
41 |
||
|
24600
5877b88f262c
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24058
diff
changeset
|
42 |
fun orig_use_text x = use_text ML_Parse.fix_ints x; |
|
5877b88f262c
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24058
diff
changeset
|
43 |
fun orig_use_file x = use_file ML_Parse.fix_ints x; |
| 20925 | 44 |
|
| 24058 | 45 |
fun use_text name pr verbose txt = NAMED_CRITICAL "ML" (fn () => |
|
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
22567
diff
changeset
|
46 |
(secure_mltext (); orig_use_text name pr verbose txt)); |
|
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
22567
diff
changeset
|
47 |
|
|
25188
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
48 |
(* compiler invocation as evaluation *) |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
49 |
fun evaluate (ref_name, reff) name pr verbose txt = NAMED_CRITICAL name (fn () => |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
50 |
let |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
51 |
val _ = secure_mltext (); |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
52 |
val txt' = "val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))";
|
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
53 |
val _ = reff := NONE; |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
54 |
val _ = orig_use_text name pr verbose txt'; |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
55 |
in case !reff |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
56 |
of NONE => error ("evaluate (" ^ ref_name ^ ")")
|
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
57 |
| SOME f => f |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
58 |
end) (); |
|
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
59 |
|
| 24058 | 60 |
fun use_file pr verbose name = NAMED_CRITICAL "ML" (fn () => |
|
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
22567
diff
changeset
|
61 |
(secure_mltext (); orig_use_file pr verbose name)); |
|
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
22567
diff
changeset
|
62 |
|
| 23978 | 63 |
fun use name = use_file Output.ml_output true name; |
64 |
||
65 |
fun use_noncritical name = |
|
66 |
(secure_mltext (); orig_use_file Output.ml_output true name); |
|
| 20925 | 67 |
|
68 |
(*commit is dynamically bound!*) |
|
|
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
22567
diff
changeset
|
69 |
fun commit () = CRITICAL (fn () => orig_use_text "" Output.ml_output false "commit();"); |
| 20925 | 70 |
|
| 20992 | 71 |
|
72 |
(* shell commands *) |
|
73 |
||
74 |
fun secure_shell () = deny_secure "Cannot execute shell commands in secure mode"; |
|
75 |
||
76 |
val orig_execute = execute; |
|
77 |
val orig_system = system; |
|
78 |
||
| 24858 | 79 |
fun execute s = (secure_shell (); orig_execute s); |
80 |
fun system s = (secure_shell (); orig_system s); |
|
| 20992 | 81 |
|
| 20925 | 82 |
end; |
83 |
||
| 23978 | 84 |
(*override previous toplevel bindings!*) |
| 21770 | 85 |
val use_text = Secure.use_text; |
|
25188
a342dec991aa
added function for evaluation by compiler invocation
haftmann
parents:
24858
diff
changeset
|
86 |
val evaluate = Secure.evaluate; |
| 21770 | 87 |
val use_file = Secure.use_file; |
|
24663
015a8838e656
improved error behaviour of use (bootstrap version);
wenzelm
parents:
24600
diff
changeset
|
88 |
fun use s = Secure.use s handle ERROR msg => (writeln msg; raise Fail "ML error"); |
| 20992 | 89 |
val execute = Secure.execute; |
90 |
val system = Secure.system; |