Skip to content

Commit db16544

Browse files
authored
Merge pull request #1677 from goblint/unrolltype
Add missing type unrollings
2 parents 7ebd0a6 + 917d6bd commit db16544

22 files changed

+118
-98
lines changed

src/analyses/base.ml

+8-9
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ struct
928928
(* | Lval (Mem e, ofs) -> get ~man st (eval_lv ~man (Mem e, ofs)) *)
929929
| (Mem e, ofs) ->
930930
(*if M.tracing then M.tracel "cast" "Deref: lval: %a" d_plainlval lv;*)
931-
let rec contains_vla (t:typ) = match t with
931+
let rec contains_vla (t:typ) = match Cil.unrollType t with
932932
| TPtr (t, _) -> contains_vla t
933933
| TArray(t, None, args) -> true
934934
| TArray(t, Some exp, args) when isConstant exp -> contains_vla t
@@ -1452,7 +1452,8 @@ struct
14521452
match eval_rv_address ~man man.local e with
14531453
| Address a ->
14541454
let slen = Seq.map String.length (List.to_seq (AD.to_string a)) in
1455-
let lenOf = function
1455+
let lenOf t =
1456+
match Cil.unrollType t with
14561457
| TArray (_, l, _) -> (try Some (lenOfArray l) with LenOfArray -> None)
14571458
| _ -> None
14581459
in
@@ -1563,7 +1564,7 @@ struct
15631564
let lval = Addr.Mval.to_cil mval in
15641565
(try `Lifted (Bytes.to_string (Hashtbl.find char_array lval))
15651566
with Not_found -> Queries.Result.top q)
1566-
| _ -> (* what about ISChar and IUChar? *)
1567+
| _ -> (* TODO: what about ISChar and IUChar? what about TEnum? *)
15671568
(* ignore @@ printf "Type %a\n" d_plaintype t; *)
15681569
Queries.Result.top q
15691570
end
@@ -2030,10 +2031,8 @@ struct
20302031
match exp with
20312032
| None -> nst
20322033
| Some exp ->
2033-
let t_override = match Cilfacade.fundec_return_type fundec with
2034-
| TVoid _ -> M.warn ~category:M.Category.Program "Returning a value from a void function"; assert false
2035-
| ret -> ret
2036-
in
2034+
let t_override = Cilfacade.fundec_return_type fundec in
2035+
assert (not (Cil.isVoidType t_override)); (* Returning a value from a void function, CIL removes the Return expression for us anyway. *)
20372036
let rv = eval_rv ~man man.local exp in
20382037
let st' = set ~man ~t_override nst (return_var ()) t_override rv in
20392038
match ThreadId.get_current ask with
@@ -2284,7 +2283,7 @@ struct
22842283
) a
22852284
| _ -> false
22862285

2287-
let get_size_of_ptr_target man ptr =
2286+
let get_size_of_ptr_target man ptr = (* TODO: deduplicate with memOutOfBounds *)
22882287
let intdom_of_int x =
22892288
ID.of_int (Cilfacade.ptrdiff_ikind ()) (Z.of_int x)
22902289
in
@@ -2301,7 +2300,7 @@ struct
23012300
let pts_elems_to_sizes (addr: Queries.AD.elt) =
23022301
begin match addr with
23032302
| Addr (v, _) ->
2304-
begin match v.vtype with
2303+
begin match Cil.unrollType v.vtype with
23052304
| TArray (item_typ, _, _) ->
23062305
let item_typ_size_in_bytes = size_of_type_in_bytes item_typ in
23072306
begin match man.ask (Queries.EvalLength ptr) with

src/analyses/baseInvariant.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ struct
241241
| BinOp(op, rval, Lval x, typ) -> derived_invariant (BinOp(switchedOp op, Lval x, rval, typ)) tv
242242
| BinOp(op, CastE (t1, c1), CastE (t2, c2), t) when (op = Eq || op = Ne) && typeSig t1 = typeSig t2 && VD.is_statically_safe_cast t1 (Cilfacade.typeOf c1) && VD.is_statically_safe_cast t2 (Cilfacade.typeOf c2)
243243
-> derived_invariant (BinOp (op, c1, c2, t)) tv
244-
| BinOp(op, CastE (TInt (ik, _) as t1, Lval x), rval, typ) ->
244+
| BinOp(op, CastE (t1, Lval x), rval, typ) when Cil.isIntegralType t1 ->
245245
begin match eval_rv ~man st (Lval x) with
246246
| Int v ->
247247
if VD.is_dynamically_safe_cast t1 (Cilfacade.typeOfLval x) (Int v) then
@@ -250,7 +250,7 @@ struct
250250
`NotUnderstood
251251
| _ -> `NotUnderstood
252252
end
253-
| BinOp(op, rval, CastE (TInt (_, _) as ti, Lval x), typ) ->
253+
| BinOp(op, rval, CastE (ti, Lval x), typ) when Cil.isIntegralType ti ->
254254
derived_invariant (BinOp (switchedOp op, CastE(ti, Lval x), rval, typ)) tv
255255
| BinOp(op, (Const _ | AddrOf _), rval, typ) ->
256256
(* This is last such that we never reach here with rval being Lval (it is swapped around). *)
@@ -615,8 +615,8 @@ struct
615615
else
616616
match exp, c_typed with
617617
| UnOp (LNot, e, _), Int c ->
618-
(match Cilfacade.typeOf e with
619-
| TInt _ | TPtr _ ->
618+
(match Cil.unrollType (Cilfacade.typeOf e) with
619+
| TInt _ | TEnum _ | TPtr _ ->
620620
let ikind = Cilfacade.get_ikind_exp e in
621621
let c' =
622622
match ID.to_bool (unop_ID LNot c) with

src/analyses/extractPthread.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ end
359359
module Variable = struct
360360
type t = varinfo
361361

362-
let is_integral v = match v.vtype with TInt _ -> true | _ -> false
362+
let is_integral v = isIntegralType v.vtype
363363

364364
let is_global v = v.vglob
365365

src/analyses/memOutOfBounds.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct
7777
) a
7878
| _ -> false
7979

80-
let get_size_of_ptr_target man ptr =
80+
let get_size_of_ptr_target man ptr = (* TODO: deduplicate with base *)
8181
if points_to_alloc_only man ptr then
8282
(* Ask for BlobSize from the base address (the second component being set to true) in order to avoid BlobSize giving us bot *)
8383
man.ask (Queries.BlobSize {exp = ptr; base_address = true})
@@ -92,7 +92,7 @@ struct
9292
set_mem_safety_flag InvalidDeref;
9393
M.warn "Var %a is potentially accessed out-of-scope. Invalid memory access may occur" CilType.Varinfo.pretty v
9494
);
95-
begin match v.vtype with
95+
begin match Cil.unrollType v.vtype with
9696
| TArray (item_typ, _, _) ->
9797
let item_typ_size_in_bytes = size_of_type_in_bytes item_typ in
9898
begin match man.ask (Queries.EvalLength ptr) with

src/analyses/mutexAnalysis.ml

+1-5
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,7 @@ struct
337337
| ts when Queries.TS.is_top ts ->
338338
()
339339
| ts ->
340-
let f = function
341-
| TComp (_, _) -> true
342-
| _ -> false
343-
in
344-
if Queries.TS.exists f ts then
340+
if Queries.TS.exists Cilfacade.isStructOrUnionType ts then
345341
old_access None
346342
end;
347343
on_ad ad

src/analyses/raceAnalysis.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ struct
351351
| ts ->
352352
if not (Queries.TS.is_empty ts) then
353353
includes_uk := true;
354-
let f = function
354+
let f t =
355+
match Cil.unrollType t with
355356
| TComp (ci, _) ->
356357
add_access_struct (conf - 50) ci
357358
| _ -> ()

src/analyses/tutorials/constants.ml

+1-6
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,8 @@ struct
1818
(* No contexts *)
1919
include Analyses.IdentityUnitContextsSpec
2020

21-
let is_integer_var (v: varinfo) =
22-
match v.vtype with
23-
| TInt _ -> true
24-
| _ -> false
25-
2621
let get_local = function
27-
| Var v, NoOffset when is_integer_var v && not (v.vglob || v.vaddrof) -> Some v (* local integer variable whose address is never taken *)
22+
| Var v, NoOffset when isIntegralType v.vtype && not (v.vglob || v.vaddrof) -> Some v (* local integer variable whose address is never taken *)
2823
| _, _ -> None
2924

3025
(** Evaluates expressions *)

src/autoTune.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ class addTypeAttributeVisitor = object
311311

312312
(*Set arrays with important types for thread analysis to unroll*)
313313
method! vtype typ =
314+
(* TODO: reuse predicates in Access module (also handles TNamed correctly) *)
314315
let is_important_type (t: typ): bool = match t with
315316
| TNamed (info, attr) -> List.mem info.tname ["pthread_mutex_t"; "spinlock_t"; "pthread_t"]
316317
| TInt (IInt, attr) -> hasAttribute "mutex" attr
@@ -375,7 +376,7 @@ class octagonVariableVisitor(varMap, globals) = object
375376

376377
method! vexpr = function
377378
(*an expression of type +/- a +/- b where a,b are either variables or constants*)
378-
| BinOp (op, e1,e2, (TInt _)) when isComparison op -> (
379+
| BinOp (op, e1,e2, _) when isComparison op -> (
379380
List.iter (fun var -> addOrCreateVarMapping varMap var 5 globals) (extractOctagonVars e1);
380381
List.iter (fun var -> addOrCreateVarMapping varMap var 5 globals) (extractOctagonVars e2);
381382
DoChildren

src/autoTune0.ml

+7-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ let collectFactors visitAction visitedObject =
9191
ignore (visitAction visitor visitedObject);
9292
factors
9393

94-
let is_large_array = function
95-
| TArray (_,Some (Const (CInt (i,_,_))),_) -> i > Z.of_int @@ 10 * get_int "ana.base.arrays.unrolling-factor"
94+
let is_large_array t =
95+
match Cil.unrollType t with
96+
| TArray (_, e, _) ->
97+
begin match Cil.lenOfArray e with (* TODO: Cil.lenOfArray but with Z.t? *)
98+
| i -> i > 10 * get_int "ana.base.arrays.unrolling-factor"
99+
| exception Cil.LenOfArray -> false
100+
end
96101
| _ -> false

src/cdomain/value/cdomains/int/bitfieldDomain.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ module BitfieldFunctor (Ints_t : IntOps.IntOps): Bitfield_SOverflow with type in
267267
let (min_ik, max_ik) = Size.range ik in
268268
let (underflow, overflow) = match torg with
269269
| None -> (false, false) (* ik does not change *)
270-
| Some (GoblintCil.Cil.TInt (old_ik, _)) ->
270+
| Some (GoblintCil.Cil.TInt (old_ik, _) | TEnum ({ekind = old_ik; _}, _)) ->
271271
let underflow = Z.compare (BArith.min old_ik (z,o)) min_ik < 0 in
272272
let overflow = Z.compare max_ik (BArith.max old_ik (z,o)) < 0 in
273273
(underflow, overflow)

src/cdomain/value/cdomains/int/congruenceDomain.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ struct
182182
let (min_ikorg, max_ikorg) = range ikorg in
183183
ikorg = t || (max_t >=: max_ikorg && min_t <=: min_ikorg)
184184
in
185-
match torg with
186-
| Some (Cil.TInt (ikorg, _)) when p ikorg ->
185+
match Option.map Cil.unrollType torg with
186+
| Some (Cil.TInt (ikorg, _) | TEnum ({ekind = ikorg; _}, _)) when p ikorg ->
187187
if M.tracing then M.trace "cong-cast" "some case";
188188
x
189189
| _ -> top ()

src/cdomain/value/cdomains/intDomain.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ include DefExcDomain
66
include EnumsDomain
77
include CongruenceDomain
88
include BitfieldDomain
9-
include IntDomTuple
9+
include IntDomTuple

src/cdomain/value/cdomains/structDomain.ml

+8-10
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,16 @@ struct
269269
match rem_fields with
270270
| [] -> second_choice
271271
| h::t -> begin
272-
match (h.ftype, get_bool "ana.base.structs.key.prefer-ptrs", get_bool "ana.base.structs.key.avoid-ints") with
272+
match Cil.unrollType h.ftype, get_bool "ana.base.structs.key.prefer-ptrs", get_bool "ana.base.structs.key.avoid-ints" with
273273
| (TPtr (_, _), _, _) -> h
274-
| (TInt (_, _), true, _)
275-
| (TInt (_, _), _, true) -> first_appropriate_key t second_choice
276-
| (TInt (_, _), _, _) -> h
274+
| ((TInt _ | TEnum _), true, _)
275+
| ((TInt _ | TEnum _), _, true) -> first_appropriate_key t second_choice
276+
| ((TInt _ | TEnum _), _, _) -> h
277277
| (_, false, _) -> h
278278
| (_, _, false) -> first_appropriate_key t second_choice
279279
| (_, _, _) ->
280-
let second = match second_choice.ftype with
281-
| TInt (_,_) -> h
282-
| _ -> second_choice
283-
in first_appropriate_key t second
280+
let second = if Cil.isIntegralType second_choice.ftype then h else second_choice in
281+
first_appropriate_key t second
284282
end
285283
in Some (first_appropriate_key fields (List.hd fields))
286284

@@ -503,8 +501,8 @@ struct
503501
let chosen_domain () = get_string "ana.base.structs.domain"
504502

505503
let pick_combined setting (comp: compinfo) =
506-
let all_bool () = List.for_all (fun f -> match f.ftype with TInt(IBool, _) -> true | _ -> false) comp.cfields in
507-
let has_ptr () = List.exists (fun f -> match f.ftype with TPtr(_, _) -> true | _ -> false) comp.cfields in
504+
let all_bool () = List.for_all (fun f -> Cilfacade.isBoolType f.ftype) comp.cfields in
505+
let has_ptr () = List.exists (fun f -> Cil.isPointerType f.ftype) comp.cfields in
508506
match setting with
509507
| "combined-sk" -> if has_ptr () then "keyed" else "simple"
510508
| "combined-all" ->

0 commit comments

Comments
 (0)