Skip to content

Commit 2f38fb4

Browse files
authored
fixes a bug in the method that computes C padding. (#1308)
For some reason the code assumes that `x mod m` is multitude of `m` and raises an exception when it doesn't happen (quite often in fact). The code is called when a structure size is computed. The main caveat here is that 1) this method has incorrect type as it constrains the padding size to be in the set of `8,16,32,64,128,256` but real padding may have any number of bits (it is their sum that should in this range). 2) this method shouldn't exist at all as the padding is fully defined by the alignment of the field and there is no need to override it. Therefore, the solution is to deprecated this method and compute padding using the alignment information only. The method is no longer used and any code that overrode it will get a warning.
1 parent a38af3a commit 2f38fb4

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/bap_c/bap_c_size.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ class base (m : model) = object(self)
5959
let align = Size.in_bits (self#alignment t) in
6060
match (align - offset mod align) mod align with
6161
| 0 -> None
62-
| n -> Some (Size.of_int_exn n)
62+
| n -> match Size.of_int n with
63+
| Error _ -> None
64+
| Ok s -> Some s
6365

6466
method alignment (t : Bap_c_type.t) : size =
6567
let byte = `r8 in
@@ -99,13 +101,12 @@ class base (m : model) = object(self)
99101

100102
method structure : compound unqualified -> Int.t option =
101103
fun {Spec.t={Compound.fields}} ->
104+
let padding t offset =
105+
let align = Size.in_bits (self#alignment t) in
106+
(align - offset mod align) mod align in
102107
List.fold fields ~init:(Some 0) ~f:(fun sz (_,field) -> match sz with
103108
| None -> None
104109
| Some sz -> match self#bits field with
105110
| None -> None
106-
| Some sz' ->
107-
let pad = match self#padding field sz with
108-
| None -> 0
109-
| Some sz -> Size.in_bits sz in
110-
Some (sz + sz' + pad))
111+
| Some sz' -> Some (sz + sz' + padding field sz))
111112
end

lib/bap_c/bap_c_size.mli

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ class base : model -> object
3434
- if type is void then alignment is 8 bits.*)
3535
method alignment : t -> size
3636

37-
(** [padding t off] computes a required padding at given offset
38-
that should be inserted before value of type [t] to satisfy
39-
the alignment restriction for [t], as determined by the
40-
[alignment] method. *)
37+
(* this method was deprecated as
38+
1) it has an incorrect type (padding can have any number of bits)
39+
2) padding is fully defined by the alignemnt and there is no
40+
need to parameterize it. *)
4141
method padding : t -> bits -> size option
42+
[@@deprecated "since [2021-05] this method is ignored"]
4243

4344

4445
(** [array spec] if array [spec] is complete, then returns a

0 commit comments

Comments
 (0)