author | wenzelm |
Fri, 23 May 2008 21:18:47 +0200 | |
changeset 26977 | e736139b553d |
parent 26883 | ae6ae88f9240 |
child 28268 | ac8431ecd57e |
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 |
26080 | 12 |
val deny_secure: string -> unit |
26385
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26332
diff
changeset
|
13 |
val use_text: int * string -> (string -> unit) * (string -> 'a) -> bool -> string -> unit |
21770 | 14 |
val use_file: (string -> unit) * (string -> 'a) -> bool -> string -> unit |
20925 | 15 |
val use: string -> unit |
16 |
val commit: unit -> unit |
|
26220 | 17 |
val system_out: string -> string * int |
20992 | 18 |
val system: string -> int |
20925 | 19 |
end; |
20 |
||
21 |
structure Secure: SECURE = |
|
22 |
struct |
|
23 |
||
20992 | 24 |
(** secure flag **) |
20925 | 25 |
|
26 |
val secure = ref false; |
|
27 |
||
20977 | 28 |
fun set_secure () = secure := true; |
20925 | 29 |
fun is_secure () = ! secure; |
30 |
||
22567
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents:
22144
diff
changeset
|
31 |
fun deny_secure msg = if is_secure () then error msg else (); |
20925 | 32 |
|
33 |
||
25753 | 34 |
|
20992 | 35 |
(** critical operations **) |
36 |
||
37 |
(* ML evaluation *) |
|
20925 | 38 |
|
39 |
fun secure_mltext () = deny_secure "Cannot evaluate ML source in secure mode"; |
|
40 |
||
26883 | 41 |
fun raw_use_text x = use_text ML_Parse.fix_ints (Position.str_of oo Position.line_file) x; |
42 |
fun raw_use_file x = use_file ML_Parse.fix_ints (Position.str_of oo Position.line_file) x; |
|
20925 | 43 |
|
26385
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26332
diff
changeset
|
44 |
fun use_text pos pr verbose txt = |
26883 | 45 |
(secure_mltext (); raw_use_text pos pr verbose txt); |
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
22567
diff
changeset
|
46 |
|
25698
8c335b4641a5
use_text/use_file: non-critical (Poly/ML compiler is thread-safe);
wenzelm
parents:
25503
diff
changeset
|
47 |
fun use_file pr verbose name = |
26883 | 48 |
(secure_mltext (); raw_use_file pr verbose name); |
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
22567
diff
changeset
|
49 |
|
23978 | 50 |
fun use name = use_file Output.ml_output true name; |
51 |
||
20925 | 52 |
(*commit is dynamically bound!*) |
26883 | 53 |
fun commit () = raw_use_text (0, "") Output.ml_output false "commit();"; |
20925 | 54 |
|
20992 | 55 |
|
56 |
(* shell commands *) |
|
57 |
||
58 |
fun secure_shell () = deny_secure "Cannot execute shell commands in secure mode"; |
|
59 |
||
26220 | 60 |
val orig_system_out = system_out; |
20992 | 61 |
|
26220 | 62 |
fun system_out s = (secure_shell (); orig_system_out s); |
26332 | 63 |
|
64 |
fun system s = |
|
65 |
(case system_out s of |
|
66 |
("", rc) => rc |
|
67 |
| (out, rc) => (writeln (perhaps (try (unsuffix "\n")) out); rc)); |
|
20992 | 68 |
|
20925 | 69 |
end; |
70 |
||
23978 | 71 |
(*override previous toplevel bindings!*) |
21770 | 72 |
val use_text = Secure.use_text; |
73 |
val use_file = Secure.use_file; |
|
24663
015a8838e656
improved error behaviour of use (bootstrap version);
wenzelm
parents:
24600
diff
changeset
|
74 |
fun use s = Secure.use s handle ERROR msg => (writeln msg; raise Fail "ML error"); |
26220 | 75 |
val system_out = Secure.system_out; |
20992 | 76 |
val system = Secure.system; |