From 069a1f99d5f213c37fdd44e5dc8ea646c331854d Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Fri, 13 Nov 2026 09:45:58 +0100 Subject: [PATCH 1/5] Ceval: non-strict constant evaluation of `||` and `&&` Evaluating both arguments of `||` and `&&` before checking their Boolean values leads to rejecting e.g. `1 || (1/0)` and `0 || (1/0)`, despite these expressions having well-defined, constant values. Also, the previous implementation of Ceval mis-handled arguments of `||`/'&&' that are 64-bit integers. E.g. `(1ULL << 32) && 1` was 0 instead of the correct 1. --- cparser/Ceval.ml | 16 ++++++++-------- test | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cparser/Ceval.ml b/cparser/Ceval.ml index 218d19d03..c2e37c5d4 100644 --- a/cparser/Ceval.ml +++ b/cparser/Ceval.ml @@ -229,14 +229,6 @@ let binop env op tyop tyres ty1 v1 ty2 v2 = comparison env (<=) None tyop v1 v2 | Oge -> comparison env (>=) None tyop v1 v2 - | Ologand -> - if boolean_value v1 - then if boolean_value v2 then I 1L else I 0L - else I 0L - | Ologor -> - if boolean_value v1 - then I 1L - else if boolean_value v2 then I 1L else I 0L | _ -> raise Notconst (* force normalization of result, e.g. of double to float *) in cast env tyres res @@ -259,6 +251,14 @@ let rec expr env e = raise Notconst | EUnop(op, e1) -> unop env op e.etyp e1.etyp (expr env e1) + | EBinop(Ologand, e1, e2, ty) -> + if boolean_value (expr env e1) + then if boolean_value (expr env e2) then I 1L else I 0L + else I 0L + | EBinop(Ologor, e1, e2, ty) -> + if boolean_value (expr env e1) + then I 1L + else if boolean_value (expr env e2) then I 1L else I 0L | EBinop(op, e1, e2, ty) -> binop env op ty e.etyp e1.etyp (expr env e1) e2.etyp (expr env e2) | EConditional(e1, e2, e3) -> diff --git a/test b/test index 71c682d6b..e9d763feb 160000 --- a/test +++ b/test @@ -1 +1 @@ -Subproject commit 71c682d6b3a74e1a7d5da30285b5735761031da3 +Subproject commit e9d763febf4b7553b3629493e39e333a89a2352a From 23f62301447b93d19e84dd8a3f56c31b954809b3 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Fri, 13 Nov 2026 14:17:01 +0100 Subject: [PATCH 2/5] Reference interpreter, `printf` emulation: better support for 64-bit targets For format `p` and modifiers `z` and `t`, 64-bit platforms use `Vlong` values while 32-bit platforms use `Vint` values. Let's handle both. --- driver/Interp.ml | 68 ++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/driver/Interp.ml b/driver/Interp.ml index 1ea441c73..34cec7c2a 100644 --- a/driver/Interp.ml +++ b/driver/Interp.ml @@ -295,33 +295,49 @@ external format_int64: string -> int64 -> string = "caml_int64_format" let format_value m flags length conv arg = - match conv.[0], length, arg with - | ('d'|'i'|'u'|'o'|'x'|'X'|'c'), (""|"h"|"hh"|"l"|"z"|"t"), Vint i -> - format_int32 (flags ^ conv) (camlint_of_coqint i) - | ('d'|'i'|'u'|'o'|'x'|'X'|'c'), (""|"h"|"hh"|"l"|"z"|"t"), _ -> - "" - | ('d'|'i'|'u'|'o'|'x'|'X'), ("ll"|"j"), Vlong i -> - format_int64 (flags ^ conv) (camlint64_of_coqint i) - | ('d'|'i'|'u'|'o'|'x'|'X'), ("ll"|"j"), _ -> - " - format_float (flags ^ conv) (camlfloat_of_coqfloat f) - | ('f'|'e'|'E'|'g'|'G'|'a'), "", _ -> - " - begin match extract_string m blk ofs with - | Some s -> s - | None -> "" + match conv.[0], length with + | ('d'|'i'|'u'|'o'|'x'|'X'|'c'), (""|"h"|"hh"|"l") -> + begin match arg with + | Vint i -> format_int32 (flags ^ conv) (camlint_of_coqint i) + | _ -> "" end - | 's', "", _ -> - "" - | 'p', "", Vptr(blk, ofs) -> - Printf.sprintf "<%ld%+ld>" (P.to_int32 blk) (camlint_of_coqint ofs) - | 'p', "", Vint i -> - format_int32 (flags ^ "x") (camlint_of_coqint i) - | 'p', "", _ -> - "" - | _, _, _ -> + | ('d'|'i'|'u'|'o'|'x'|'X'), ("ll"|"j") -> + begin match arg with + | Vlong i -> format_int64 (flags ^ conv) (camlint64_of_coqint i) + | _ -> " + begin match arg with + | Vint i -> format_int32 (flags ^ conv) (camlint_of_coqint i) + | Vlong i -> format_int64 (flags ^ conv) (camlint64_of_coqint i) + | _ -> "" + end + | ('f'|'e'|'E'|'g'|'G'|'a'), (""|"l") -> + begin match arg with + | Vfloat f -> format_float (flags ^ conv) (camlfloat_of_coqfloat f) + | _ -> " + begin match arg with + | Vptr(blk, ofs) -> + begin match extract_string m blk ofs with + | Some s -> s + | None -> "" + end + | _ -> "" + end + | 'p', "" -> + begin match arg with + | Vptr(blk, ofs) -> + Printf.sprintf "<%ld%+ld>" (P.to_int32 blk) (camlint_of_coqint ofs) + | Vint i -> + format_int32 (flags ^ "x") (camlint_of_coqint i) + | Vlong i -> + format_int64 (flags ^ "x") (camlint64_of_coqint i) + | _ -> + "" + end + | _, _ -> "" let do_printf m fmt args = From 5a93a1b357b789cde1c93317ad0879262a9a4588 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Fri, 13 Nov 2026 16:14:14 +0100 Subject: [PATCH 3/5] Ctyping: propagate `volatile` to the element type of array members If a struct or union is volatile and has a member of array type `t[N]`, accessing this member must add the volatile modifier not just to the type `t[N]` but also to the type `t`. Otherwise, the decay of `t[N]` to `*t` loses the volatile information. This was correctly done in the unverified elaborator (cparse/) but not in the verified type-checker (cfrontend/Ctyping.v). --- cfrontend/Ctypes.v | 32 ++++++++++++++++++-------------- cfrontend/Ctyping.v | 8 ++------ extraction/extraction.v | 1 - test | 2 +- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cfrontend/Ctypes.v b/cfrontend/Ctypes.v index e5423fbb0..96de1be28 100644 --- a/cfrontend/Ctypes.v +++ b/cfrontend/Ctypes.v @@ -128,7 +128,7 @@ Definition attr_of_type (ty: type) := | Tunion id a => a end. -(** Change the top-level attributes of a type *) +(** Change the top-level attributes of a type. *) Definition change_attributes (f: attr -> attr) (ty: type) : type := match ty with @@ -143,25 +143,29 @@ Definition change_attributes (f: attr -> attr) (ty: type) : type := | Tunion id a => Tunion id (f a) end. -(** Erase the top-level attributes of a type *) +(** Erase the top-level attributes of a type. *) Definition remove_attributes (ty: type) : type := change_attributes (fun _ => noattr) ty. -(** Add extra attributes to the top-level attributes of a type *) +(** Set the volatile attribute on a type. + For array types, the volatile attribute is also applied to the element type. *) -Definition attr_union (a1 a2: attr) : attr := - {| attr_volatile := a1.(attr_volatile) || a2.(attr_volatile); - attr_alignas := - match a1.(attr_alignas), a2.(attr_alignas) with - | None, al => al - | al, None => al - | Some n1, Some n2 => Some (N.max n1 n2) - end - |}. +Definition make_attr_volatile (a: attr) : attr := + {| attr_volatile := true; attr_alignas := a.(attr_alignas) |}. -Definition merge_attributes (ty: type) (a: attr) : type := - change_attributes (attr_union a) ty. +Fixpoint make_volatile (ty: type) : type := + match ty with + | Tvoid => ty + | Tint sz si a => Tint sz si (make_attr_volatile a) + | Tlong si a => Tlong si (make_attr_volatile a) + | Tfloat sz a => Tfloat sz (make_attr_volatile a) + | Tpointer elt a => Tpointer elt (make_attr_volatile a) + | Tarray elt sz a => Tarray (make_volatile elt) sz (make_attr_volatile a) + | Tfunction args res cc => ty + | Tstruct id a => Tstruct id (make_attr_volatile a) + | Tunion id a => Tunion id (make_attr_volatile a) + end. (** Maximal size in bits of a bitfield of type [sz]. *) diff --git a/cfrontend/Ctyping.v b/cfrontend/Ctyping.v index 7d7f37ca9..9edf5c4b6 100644 --- a/cfrontend/Ctyping.v +++ b/cfrontend/Ctyping.v @@ -37,15 +37,11 @@ Definition ptrdiff_t : type := (** The type of a member of a composite (struct or union). The "volatile" attribute carried by the composite propagates - to the type of the member, but not the "alignas" attribute. *) - -Definition attr_add_volatile (vol: bool) (a: attr) := - {| attr_volatile := a.(attr_volatile) || vol; - attr_alignas := a.(attr_alignas) |}. + to the type of the member, but not the other attributes. *) Definition type_of_member (a: attr) (f: ident) (m: members) : res type := do ty <- field_type f m; - OK (change_attributes (attr_add_volatile a.(attr_volatile)) ty). + OK (if a.(attr_volatile) then make_volatile ty else ty). (** Type-checking of arithmetic and logical operators *) diff --git a/extraction/extraction.v b/extraction/extraction.v index a7c529670..a2489c270 100644 --- a/extraction/extraction.v +++ b/extraction/extraction.v @@ -143,7 +143,6 @@ Cd "extraction". Separate Extraction Compiler.transf_c_program Compiler.transf_cminor_program Cexec.do_initial_state Cexec.do_step Cexec.at_final_state - Ctypes.merge_attributes Ctypes.remove_attributes Ctypes.build_composite_env Ctypes.layout_struct Initializers.transl_init Initializers.constval Csyntax.Eindex Csyntax.Epreincr Csyntax.Eselection diff --git a/test b/test index e9d763feb..37421b7f3 160000 --- a/test +++ b/test @@ -1 +1 @@ -Subproject commit e9d763febf4b7553b3629493e39e333a89a2352a +Subproject commit 37421b7f3d4658e13965dfac315c0e1895671eae From 89e200ef404455c6aa06d65650a4855d15b6590d Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Sat, 14 Nov 2026 19:08:21 +0100 Subject: [PATCH 4/5] String literal concatenation: honor wide empty strings If a plain string literal is concatenated with a wide empty literal, the plain string becomes a wide string. --- cparser/Lexer.mll | 4 +--- test | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cparser/Lexer.mll b/cparser/Lexer.mll index 74c72186a..c7979a5ff 100644 --- a/cparser/Lexer.mll +++ b/cparser/Lexer.mll @@ -713,9 +713,7 @@ and singleline_comment = parse | Pre_parser.STRING_LITERAL (enc', str', loc') -> ignore (Queue.pop tokens); let (enc'', str'') = doConcat enc' str' in - if str'' <> [] - then (combine_encodings loc enc enc'', str @ str'') - else (enc, str) + (combine_encodings loc enc enc'', str @ str'') | _ -> (enc, str) | exception Queue.Empty -> (enc, str) in diff --git a/test b/test index 37421b7f3..68dc0ec85 160000 --- a/test +++ b/test @@ -1 +1 @@ -Subproject commit 37421b7f3d4658e13965dfac315c0e1895671eae +Subproject commit 68dc0ec85b921ea2e4da47d44793795bca6774a4 From f783be9565e514c65abca47d3096e578a7886c2e Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Sat, 14 Nov 2026 19:21:31 +0100 Subject: [PATCH 5/5] C2C: correctly handle the signedness of character literals A literal of small unsigned integer type is still promoted to `signed int`. The only case where it makes a difference is u16 character literals and wide character literals on platforms where `wchar_t` is `unsigned short`. --- cfrontend/C2C.ml | 5 +++-- test | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cfrontend/C2C.ml b/cfrontend/C2C.ml index 55641f12c..439223609 100644 --- a/cfrontend/C2C.ml +++ b/cfrontend/C2C.ml @@ -804,8 +804,9 @@ let rec convertExpr env e = ewrap (Ctyping.evalof l) | C.EConst(C.CInt(i, k, _)) -> - let sg = if Cutil.is_signed_ikind k then Signed else Unsigned in - if Cutil.sizeof_ikind k = 8 + let sz = Cutil.sizeof_ikind k in + let sg = if sz < 4 || Cutil.is_signed_ikind k then Signed else Unsigned in + if sz = 8 then Ctyping.econst_long (convertInt64 i) sg else Ctyping.econst_int (convertInt32 i) sg | C.EConst(C.CFloat(f, k)) -> diff --git a/test b/test index 68dc0ec85..91aefeacc 160000 --- a/test +++ b/test @@ -1 +1 @@ -Subproject commit 68dc0ec85b921ea2e4da47d44793795bca6774a4 +Subproject commit 91aefeacc7f310aba81709029ee05d2e91f715bb