Skip to content

Commit e367d50

Browse files
committed
Stop catching async exceptions in hacks that catch anomalies
Typically the reductionops "Tried to normalize ill-typed term" hack deliberately catches anomalies (including those from CErrors.anomaly) but shouldn't catch async exceptions. Hopefully fixes the non working Fail Timeout in bedrock2, we could also revert #21216 for our own test suite. (debugging tip: apply ~~~diff @@ -32,7 +32,14 @@ let check_for_interrupt () = (* This function assumes it is the only function calling [setitimer] *) let unix_timeout n f x = let open Unix in - let timeout_handler _ = raise Timeout in + let timeout_handler _ = + let info = Exninfo.reify () in + let () = + Exninfo.get_backtrace info |> Option.iter @@ fun bt -> + Printf.eprintf "timeout at %s\n%!" (Exninfo.backtrace_to_string bt) + in + raise Timeout + in let old_timer = getitimer ITIMER_REAL in (* Here we assume that the existing timer will also interrupt us. *) if old_timer.it_value > 0. && old_timer.it_value <= n then Ok (f x) else ~~~ then run a buggy Timeout with `Set Debug "backtrace"` on until you get the bug, the function which catches the timeout exn should be in the printed backtrace. For instance I had ~~~ Called from Reductionops.infer_conv_gen in file "pretyping/reductionops.ml", line 1324, characters 10-52 ~~~ when trying the Fail Timeout in bedrock2 LeakageSemantics.)
1 parent b52b14d commit e367d50

File tree

13 files changed

+25
-20
lines changed

13 files changed

+25
-20
lines changed

checker/coqchk_main.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ let init_with_argv argv =
438438
includes := [];
439439
make_senv ()
440440
with e ->
441-
fatal_error (str "Error during initialization :" ++ (explain_exn e)) (is_anomaly e)
441+
fatal_error (str "Error during initialization :" ++ (explain_exn e)) (is_anomaly ~async:true e)
442442

443443
let init() = init_with_argv Sys.argv
444444

@@ -448,7 +448,7 @@ let run senv =
448448
flush_all(); senv
449449
with e ->
450450
if CDebug.(get_flag misc) then Printexc.print_backtrace stderr;
451-
fatal_error (explain_exn e) (is_anomaly e)
451+
fatal_error (explain_exn e) (is_anomaly ~async:true e)
452452

453453
let main () =
454454
let senv = init() in

dev/ci/scripts/ci-stdlib.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ git_download stdlib
1010
if [ "$DOWNLOAD_ONLY" ]; then exit 0; fi
1111

1212
( cd "${CI_BUILD_DIR}/stdlib"
13-
dev/with-rocq-wrap.sh dune build --root . --only-packages=rocq-stdlib @install
13+
# dev/with-rocq-wrap.sh dune build --root . --only-packages=rocq-stdlib @install
1414
dev/with-rocq-wrap.sh dune install --root . rocq-stdlib --prefix="$CI_INSTALL_DIR"
1515
)

lib/cErrors.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ let is_handled e =
8484
let is_handled_by h = Option.has_some (h e) in
8585
List.exists is_handled_by !handle_stack
8686

87-
let is_anomaly = function
87+
let is_anomaly ~async = function
8888
| Anomaly _ -> true
89-
| exn -> not (is_handled exn)
89+
| Control.Timeout | Sys.Break | Out_of_memory | Stack_overflow -> async
90+
| exn -> if Memprof_coq.is_interrupted() then async else not (is_handled exn)
9091

9192
(** Printing of additional error info, from Exninfo *)
9293
let additional_error_info_handler = ref []

lib/cErrors.mli

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ val anomaly : ?loc:Loc.t -> ?info:Exninfo.info -> ?label:string -> Pp.t -> 'a
2525
(** Raise an anomaly, with an optional location and an optional
2626
label identifying the anomaly. *)
2727

28-
val is_anomaly : exn -> bool
28+
val is_anomaly : async:bool -> exn -> bool
2929
(** Check whether a given exception is an anomaly.
3030
This is mostly provided for compatibility. Please avoid doing specific
31-
tricks with anomalies thanks to it. See rather [noncritical] below. *)
31+
tricks with anomalies thanks to it. See rather [noncritical] below.
32+
33+
[async] is the value returned for asynchronous exceptions (timeouts, user interrupts, etc).
34+
Avoid catching these at all costs! *)
3235

3336
exception UserError of Pp.t
3437
(** Main error signaling exception. It carries a header plus a pretty printing

plugins/ltac/tacinterp.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,7 @@ and interp_atomic ist tac : unit Proofview.tactic =
18681868
let env = Proofview.Goal.env gl in
18691869
let sigma = project gl in
18701870
let op = interp_typed_pattern ist env sigma op in
1871-
let to_catch = function Not_found -> true | e -> CErrors.is_anomaly e in
1871+
let to_catch = function Not_found -> true | e -> CErrors.is_anomaly ~async:false e in
18721872
let c_interp patvars env sigma =
18731873
let lfun' = Id.Map.fold (fun id c lfun ->
18741874
Id.Map.add id (Value.of_constr c) lfun)

pretyping/reductionops.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,15 +1143,17 @@ let clos_norm_flags flgs env sigma t =
11431143
(Evarutil.create_clos_infos env sigma flgs)
11441144
(CClosure.create_tab ())
11451145
(Esubst.subs_id 0, UVars.Instance.empty) (EConstr.Unsafe.to_constr t))
1146-
with e when is_anomaly e -> user_err Pp.(str "Tried to normalize ill-typed term")
1146+
with e when is_anomaly ~async:false e ->
1147+
user_err Pp.(str "Tried to normalize ill-typed term")
11471148

11481149
let clos_whd_flags flgs env sigma t =
11491150
try
11501151
EConstr.of_constr (CClosure.whd_val
11511152
(Evarutil.create_clos_infos env sigma flgs)
11521153
(CClosure.create_tab ())
11531154
(CClosure.inject (EConstr.Unsafe.to_constr t)))
1154-
with e when is_anomaly e -> user_err Pp.(str "Tried to normalize ill-typed term")
1155+
with e when is_anomaly ~async:false e ->
1156+
user_err Pp.(str "Tried to normalize ill-typed term")
11551157

11561158
let nf_beta = clos_norm_flags RedFlags.beta
11571159
let nf_betaiota = clos_norm_flags RedFlags.betaiota
@@ -1192,7 +1194,7 @@ let _ = CErrors.register_handler (function
11921194

11931195
let report_anomaly (e, info) =
11941196
let e =
1195-
if is_anomaly e then AnomalyInConversion e
1197+
if is_anomaly ~async:false e then AnomalyInConversion e
11961198
else e
11971199
in
11981200
Exninfo.iraise (e, info)

stm/stmargs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
let fatal_error exn =
1212
Topfmt.(in_phase ~phase:ParsingCommandLine print_err_exn exn);
13-
let exit_code = if (CErrors.is_anomaly exn) then 129 else 1 in
13+
let exit_code = if (CErrors.is_anomaly ~async:true exn) then 129 else 1 in
1414
exit exit_code
1515

1616
let set_worker_id opt s =

tactics/tactics.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,7 @@ let declare_intro_decomp_eq f = intro_decomp_eq_function := f
21032103

21042104
let my_find_eq_data_decompose env sigma t =
21052105
try Some (find_eq_data_decompose env sigma t)
2106-
with e when is_anomaly e
2106+
with e when is_anomaly ~async:false e
21072107
(* Hack in case equality is not yet defined... one day, maybe,
21082108
known equalities will be dynamically registered *)
21092109
-> None

topbin/rocqnative.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,5 @@ let () =
386386
compile senv ~in_file
387387
with exn ->
388388
Format.eprintf "Error: @[%a@]@\n%!" Pp.pp_with (CErrors.print exn);
389-
let exit_code = if (CErrors.is_anomaly exn) then 129 else 1 in
389+
let exit_code = if (CErrors.is_anomaly ~async:true exn) then 129 else 1 in
390390
exit exit_code

toplevel/coqc.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ let coqc_run copts ~opts injections =
5757
flush_all();
5858
Topfmt.print_err_exn exn;
5959
flush_all();
60-
let exit_code = if (CErrors.is_anomaly exn) then 129 else 1 in
60+
let exit_code = if (CErrors.is_anomaly ~async:true exn) then 129 else 1 in
6161
exit exit_code
6262

6363
let fix_stm_opts opts stm_opts = match opts.Coqcargs.compilation_mode with

0 commit comments

Comments
 (0)