src/Pure/General/secure.ML
author haftmann
Thu, 25 Oct 2007 13:52:02 +0200
changeset 25188 a342dec991aa
parent 24858 a3a81e73f552
child 25204 36cf92f63a44
permissions -rw-r--r--
added function for evaluation by compiler invocation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/secure.ML
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     4
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     5
Secure critical operations.
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     6
*)
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     7
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     8
signature SECURE =
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
     9
sig
20977
dbf1eca9b34e tuned signature;
wenzelm
parents: 20925
diff changeset
    10
  val set_secure: unit -> unit
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    11
  val is_secure: unit -> bool
22144
c33450acd873 use_text: added name argument;
wenzelm
parents: 21770
diff changeset
    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
ea6f846d8c4b added use_file;
wenzelm
parents: 20992
diff changeset
    15
  val use_file: (string -> unit) * (string -> 'a) -> bool -> string -> unit
23978
6e8d5a05ffe8 added use_noncritical;
wenzelm
parents: 23922
diff changeset
    16
  val use_noncritical: string -> unit
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    17
  val use: string -> unit
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    18
  val commit: unit -> unit
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    19
  val execute: string -> string
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    20
  val system: string -> int
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    21
end;
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    22
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    23
structure Secure: SECURE =
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    24
struct
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    25
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    26
(** secure flag **)
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    27
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    28
val secure = ref false;
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    29
20977
dbf1eca9b34e tuned signature;
wenzelm
parents: 20925
diff changeset
    30
fun set_secure () = secure := true;
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    31
fun is_secure () = ! secure;
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    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
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    34
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    35
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    36
(** critical operations **)
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    37
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    38
(* ML evaluation *)
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    39
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    40
fun secure_mltext () = deny_secure "Cannot evaluate ML source in secure mode";
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    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
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    44
24058
81aafd465662 NAMED_CRITICAL;
wenzelm
parents: 23978
diff changeset
    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
81aafd465662 NAMED_CRITICAL;
wenzelm
parents: 23978
diff changeset
    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
6e8d5a05ffe8 added use_noncritical;
wenzelm
parents: 23922
diff changeset
    63
fun use name = use_file Output.ml_output true name;
6e8d5a05ffe8 added use_noncritical;
wenzelm
parents: 23922
diff changeset
    64
6e8d5a05ffe8 added use_noncritical;
wenzelm
parents: 23922
diff changeset
    65
fun use_noncritical name =
6e8d5a05ffe8 added use_noncritical;
wenzelm
parents: 23922
diff changeset
    66
  (secure_mltext (); orig_use_file Output.ml_output true name);
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    67
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    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
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    70
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    71
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    72
(* shell commands *)
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    73
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    74
fun secure_shell () = deny_secure "Cannot execute shell commands in secure mode";
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    75
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    76
val orig_execute = execute;
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    77
val orig_system = system;
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    78
24858
a3a81e73f552 execute/system: non-critical;
wenzelm
parents: 24663
diff changeset
    79
fun execute s = (secure_shell (); orig_execute s);
a3a81e73f552 execute/system: non-critical;
wenzelm
parents: 24663
diff changeset
    80
fun system s = (secure_shell (); orig_system s);
20992
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    81
20925
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    82
end;
2d19e511fe2c Secure critical operations.
wenzelm
parents:
diff changeset
    83
23978
6e8d5a05ffe8 added use_noncritical;
wenzelm
parents: 23922
diff changeset
    84
(*override previous toplevel bindings!*)
21770
ea6f846d8c4b added use_file;
wenzelm
parents: 20992
diff changeset
    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
ea6f846d8c4b added use_file;
wenzelm
parents: 20992
diff changeset
    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
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    89
val execute = Secure.execute;
05c3703cc6c5 added execute/system;
wenzelm
parents: 20977
diff changeset
    90
val system = Secure.system;