Skip to content

Commit 8dd4762

Browse files
authored
Merge pull request #1679 from goblint/offset-to_index-bytes
Change `Offset.MakeLattice.to_index` to return bytes, not bits
2 parents 61ff53f + d61dd0f commit 8dd4762

File tree

6 files changed

+32
-19
lines changed

6 files changed

+32
-19
lines changed

src/analyses/base.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ struct
271271
let n_offset = iDtoIdx n in
272272
begin match t with
273273
| Some t ->
274-
let (f_offset_bits, _) = bitsOffset t (Field (f, NoOffset)) in
275-
let f_offset = IdxDom.of_int (Cilfacade.ptrdiff_ikind ()) (Z.of_int (f_offset_bits / 8)) in
274+
let f_offset_bytes = Cilfacade.bytesOffsetOnly t (Field (f, NoOffset)) in
275+
let f_offset = IdxDom.of_int (Cilfacade.ptrdiff_ikind ()) (Z.of_int f_offset_bytes) in
276276
begin match IdxDom.(to_bool (eq f_offset (neg n_offset))) with
277277
| Some true -> `NoOffset
278278
| _ -> `Field (f, `Index (n_offset, `NoOffset))
@@ -2289,8 +2289,7 @@ struct
22892289
ID.of_int (Cilfacade.ptrdiff_ikind ()) (Z.of_int x)
22902290
in
22912291
let size_of_type_in_bytes typ =
2292-
let typ_size_in_bytes = (bitsSizeOf typ) / 8 in
2293-
intdom_of_int typ_size_in_bytes
2292+
intdom_of_int (Cilfacade.bytesSizeOf typ)
22942293
in
22952294
if points_to_heap_only man ptr then
22962295
(* Ask for BlobSize from the base address (the second component being set to true) in order to avoid BlobSize giving us bot *)

src/analyses/memOutOfBounds.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ struct
3232
ID.of_int (Cilfacade.ptrdiff_ikind ()) (Z.of_int x)
3333

3434
let size_of_type_in_bytes typ =
35-
let typ_size_in_bytes = (bitsSizeOf typ) / 8 in
36-
intdom_of_int typ_size_in_bytes
35+
intdom_of_int (Cilfacade.bytesSizeOf typ)
3736

3837
let rec exp_contains_a_ptr (exp:exp) =
3938
match exp with
@@ -149,8 +148,8 @@ struct
149148
| `NoOffset -> intdom_of_int 0
150149
| `Field (field, o) ->
151150
let field_as_offset = Field (field, NoOffset) in
152-
let bits_offset, _size = GoblintCil.bitsOffset (TComp (field.fcomp, [])) field_as_offset in
153-
let bytes_offset = intdom_of_int (bits_offset / 8) in
151+
let bytes_offset = Cilfacade.bytesOffsetOnly (TComp (field.fcomp, [])) field_as_offset in
152+
let bytes_offset = intdom_of_int bytes_offset in
154153
let remaining_offset = offs_to_idx field.ftype o in
155154
begin
156155
try ID.add bytes_offset remaining_offset
@@ -279,7 +278,7 @@ struct
279278
M.warn "Size of lval dereference expression %a is bot. Out-of-bounds memory access may occur" d_exp e)
280279
| `Lifted es ->
281280
let casted_es = ID.cast_to (Cilfacade.ptrdiff_ikind ()) es in
282-
let casted_offs = ID.div (ID.cast_to (Cilfacade.ptrdiff_ikind ()) offs_intdom) (ID.of_int (Cilfacade.ptrdiff_ikind ()) (Z.of_int 8)) in
281+
let casted_offs = ID.cast_to (Cilfacade.ptrdiff_ikind ()) offs_intdom in
283282
let ptr_size_lt_offs =
284283
let one = intdom_of_int 1 in
285284
let casted_es = ID.sub casted_es one in

src/cdomain/value/cdomains/offset.ml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ struct
202202
| `Index (_,o) -> `Index (Idx.top (), of_exp o)
203203
| `Field (f,o) -> `Field (f, of_exp o)
204204

205+
let eight = Z.of_int 8
206+
205207
let to_index ?typ (offs: t): Idx.t =
206208
let idx_of_int x =
207209
Idx.of_int (Cilfacade.ptrdiff_ikind ()) (Z.of_int x)
@@ -211,22 +213,24 @@ struct
211213
| `Field (field, o) ->
212214
let field_as_offset = Field (field, NoOffset) in
213215
let bits_offset, _size = GoblintCil.bitsOffset (TComp (field.fcomp, [])) field_as_offset in
214-
let bits_offset = idx_of_int bits_offset in
216+
let bits_offset = Z.of_int bits_offset in
217+
(* Interval of floor and ceil division in case bitfield offset. *)
218+
let bytes_offset = Idx.of_interval (Cilfacade.ptrdiff_ikind ()) Z.(fdiv bits_offset eight, cdiv bits_offset eight) in
215219
let remaining_offset = offset_to_index_offset ~typ:field.ftype o in
216-
GobRef.wrap AnalysisState.executing_speculative_computations true @@ fun () -> Idx.add bits_offset remaining_offset
220+
GobRef.wrap AnalysisState.executing_speculative_computations true @@ fun () -> Idx.add bytes_offset remaining_offset
217221
| `Index (x, o) ->
218-
let (item_typ, item_size_in_bits) =
222+
let (item_typ, item_size_in_bytes) =
219223
match Option.map unrollType typ with
220224
| Some TArray(item_typ, _, _) ->
221-
let item_size_in_bits = bitsSizeOf item_typ in
222-
(Some item_typ, idx_of_int item_size_in_bits)
225+
let item_size_in_bytes = Cilfacade.bytesSizeOf item_typ in
226+
(Some item_typ, idx_of_int item_size_in_bytes)
223227
| _ ->
224228
(None, Idx.top ())
225229
in
226230
(* Binary operations on offsets should not generate overflow warnings in SV-COMP *)
227-
let bits_offset = GobRef.wrap AnalysisState.executing_speculative_computations true @@ fun () -> Idx.mul item_size_in_bits x in
231+
let bytes_offset = GobRef.wrap AnalysisState.executing_speculative_computations true @@ fun () -> Idx.mul item_size_in_bytes x in
228232
let remaining_offset = offset_to_index_offset ?typ:item_typ o in
229-
GobRef.wrap AnalysisState.executing_speculative_computations true @@ fun () -> Idx.add bits_offset remaining_offset
233+
GobRef.wrap AnalysisState.executing_speculative_computations true @@ fun () -> Idx.add bytes_offset remaining_offset
230234
in
231235
offset_to_index_offset ?typ offs
232236

src/cdomain/value/cdomains/valueDomain.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ struct
992992
not @@ ask.is_multiple var
993993
&& not @@ Cil.isVoidType t (* Size of value is known *)
994994
&& GobOption.exists (fun blob_size -> (* Size of blob is known *)
995-
Z.equal blob_size (Z.of_int @@ Cil.bitsSizeOf (TComp (toptype, []))/8)
995+
Z.equal blob_size (Z.of_int @@ Cilfacade.bytesSizeOf (TComp (toptype, [])))
996996
) blob_size_opt
997997
| _ -> false
998998
in

src/cdomains/lockDomain.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ struct
1414

1515
let semantic_equal_mval ((v, o): t) ((v', o'): Mval.t): bool option =
1616
if CilType.Varinfo.equal v v' then (
17-
let (index1, _) = GoblintCil.bitsOffset v.vtype (Offset.Z.to_cil o) in (* TODO: better way to compute this? as Z.t not int *)
18-
let index2: IndexDomain.t = ValueDomain.Offs.to_index ~typ:v.vtype o' in (* TODO: is this bits or bytes? *)
17+
let index1 = Cilfacade.bytesOffsetOnly v.vtype (Offset.Z.to_cil o) in (* TODO: better way to compute this? as Z.t not int *)
18+
let index2: IndexDomain.t = ValueDomain.Offs.to_index ~typ:v.vtype o' in
1919
match IndexDomain.equal_to (Z.of_int index1) index2 with
2020
| `Eq -> Some true
2121
| `Neq -> Some false

src/common/util/cilfacade.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@ let typeSigBlendAttributes baseAttrs =
381381
typeSigAddAttrs contageous
382382

383383

384+
let bytesSizeOf t =
385+
let bits = bitsSizeOf t in
386+
assert (bits mod 8 = 0);
387+
bits / 8
388+
389+
let bytesOffsetOnly t o =
390+
let bits_offset, _ = bitsOffset t o in
391+
assert (bits_offset mod 8 = 0);
392+
bits_offset / 8
393+
394+
384395
(** {!Cil.mkCast} using our {!typeOf}. *)
385396
let mkCast ~(e: exp) ~(newt: typ) =
386397
let oldt =

0 commit comments

Comments
 (0)