Skip to content

Commit 6843597

Browse files
authored
Merge pull request simonjbeaumont#2 from psafont/opt
CA-266936: Move pci lookups to string_opt to prevent some segfaults
2 parents 6c5a563 + c92ed04 commit 6843597

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

bindings/ffi_bindings.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ module Bindings (F : Cstubs.FOREIGN) = struct
264264

265265
let pci_lookup_name_1_ary =
266266
foreign "pci_lookup_name"
267-
(Pci_access.t @-> ptr char @-> int @-> int @-> int @-> returning string)
267+
(Pci_access.t @-> ptr char @-> int @-> int @-> int @-> returning string_opt)
268268

269269
let pci_lookup_name_2_ary =
270270
foreign "pci_lookup_name"
271-
(Pci_access.t @-> ptr char @-> int @-> int @-> int @-> int @-> returning string)
271+
(Pci_access.t @-> ptr char @-> int @-> int @-> int @-> int @-> returning string_opt)
272272

273273
let pci_lookup_name_4_ary =
274274
foreign "pci_lookup_name"
275-
(Pci_access.t @-> ptr char @-> int @-> int @-> int @-> int @-> int @-> int @-> returning string)
275+
(Pci_access.t @-> ptr char @-> int @-> int @-> int @-> int @-> int @-> int @-> returning string_opt)
276276

277277
let pci_load_name_list =
278278
foreign "pci_load_name_list" (Pci_access.t @-> returning int)

examples/lspci.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@ open Pci
22

33
(* This should be equivalent to `lspci -nnnDv` *)
44
let lspci_nnnDv pci_access =
5+
let default v = match v with Some v -> v | None -> "" in
56
let devs = get_devices pci_access in
67
List.iter (fun d ->
78
let open Pci_dev in
89
Printf.printf "Device: %04x:%02x:%02x.%d\n"
910
d.domain d.bus d.dev d.func;
1011
Printf.printf "Class: %s [%04x]\n"
11-
(lookup_class_name pci_access d.device_class) d.device_class;
12+
(lookup_class_name pci_access d.device_class |> default) d.device_class;
1213
Printf.printf "Vendor: %s [%04x]\n"
13-
(lookup_vendor_name pci_access d.vendor_id) d.vendor_id;
14+
(lookup_vendor_name pci_access d.vendor_id |> default) d.vendor_id;
1415
Printf.printf "Device: %s [%04x]\n"
15-
(lookup_device_name pci_access d.vendor_id d.device_id) d.device_id;
16+
(lookup_device_name pci_access d.vendor_id d.device_id |> default) d.device_id;
1617
begin match d.subsystem_id with
1718
| Some (sv_id, sd_id) ->
1819
Printf.printf "SVendor:\t%s [%04x]\n"
19-
(lookup_subsystem_vendor_name pci_access sv_id) sv_id;
20+
(lookup_subsystem_vendor_name pci_access sv_id |> default) sv_id;
2021
Printf.printf "SDevice:\t%s [%04x]\n"
21-
(lookup_subsystem_device_name pci_access d.vendor_id d.device_id sv_id sd_id) sd_id
22+
(lookup_subsystem_device_name pci_access d.vendor_id d.device_id sv_id sd_id |> default) sd_id
2223
| None -> ()
2324
end;
2425
begin match d.phy_slot with
@@ -43,7 +44,8 @@ let lspci_nnnDv pci_access =
4344
let nv_vid = 0x10de
4445
and k1_did = 0x0ff7
4546
and id_160 = 0x113b in
46-
let n = lookup_subsystem_device_name pci_access nv_vid k1_did nv_vid id_160 in
47+
let n = lookup_subsystem_device_name pci_access nv_vid k1_did nv_vid id_160
48+
|> default in
4749
Printf.printf "\"%s\"\n" n
4850

4951
let () = with_access lspci_nnnDv

lib/pci.mli

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,29 @@ module Pci_access : sig
3232
type t
3333
end
3434

35-
val lookup_class_name : Pci_access.t -> int -> string
35+
val lookup_class_name : Pci_access.t -> int -> string option
3636
(** [lookup_class_name a id] wraps pci_lookup_name with the right flags to
3737
lookup the name for the class whose identifier is [id] using the access
3838
value [a]. If [libpci] cannot find a match it returns "Class [id]". *)
3939

40-
val lookup_progif_name : Pci_access.t -> int -> int -> string
40+
val lookup_progif_name : Pci_access.t -> int -> int -> string option
4141
(** [lookup_progif_name a c_id id] is like {!lookup_class_name} but returns
4242
the name of the programming interface with ID [id] within the class with ID
4343
[c_id]. *)
4444

45-
val lookup_vendor_name : Pci_access.t -> int -> string
45+
val lookup_vendor_name : Pci_access.t -> int -> string option
4646
(** [lookup_vendor_name a id] is like {!lookup_class_name} but returns
4747
the name of the PCI vendor with ID [id]. *)
4848

49-
val lookup_device_name : Pci_access.t -> int -> int -> string
49+
val lookup_device_name : Pci_access.t -> int -> int -> string option
5050
(** [lookup_device_name a v_id id] is like {!lookup_class_name} but returns
5151
the name of the device with ID [id] by the vendor with ID [v_id]. *)
5252

53-
val lookup_subsystem_vendor_name : Pci_access.t -> int -> string
53+
val lookup_subsystem_vendor_name : Pci_access.t -> int -> string option
5454
(** [lookup_subsystem_vendor_name a id] is like {!lookup_class_name} but
5555
returns the name of the PCI vendor with ID [id]. *)
5656

57-
val lookup_subsystem_device_name : Pci_access.t -> int -> int -> int -> int -> string
57+
val lookup_subsystem_device_name : Pci_access.t -> int -> int -> int -> int -> string option
5858
(** [lookup_subsystem_device_name a v_id d_id sv_id sd_id] is like
5959
{!lookup_class_name} but returns the name of the PCI subsystem of a device
6060
with ID [d_id] made by vendor with ID [v_id] whose subvendor and subdevice

lib_test/test_pci.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ let test_lookup_functions () =
4949
SVendor: Red Hat, Inc [1af4]
5050
SDevice: Qemu virtual machine [1100] *)
5151
let test_lookup = assert_equal ~printer:(fun x -> x) in
52+
let default v = match v with Some v -> v | None -> "" in
5253
with_dump (fun acc ->
53-
test_lookup "Bridge" @@ lookup_class_name acc 0x0680;
54-
test_lookup "Intel Corporation" @@ lookup_vendor_name acc 0x8086;
55-
test_lookup "82371AB/EB/MB PIIX4 ACPI" @@ lookup_device_name acc 0x8086 0x7113;
56-
test_lookup "Red Hat, Inc." @@ lookup_subsystem_vendor_name acc 0x1af4;
57-
test_lookup "Qemu virtual machine" @@ lookup_subsystem_device_name acc 0x8086 0x7113 0x1af4 0x1100;
58-
test_lookup "VGA compatible controller" @@ lookup_class_name acc 0x0300;
59-
test_lookup "VGA controller" @@ lookup_progif_name acc 0x0300 0x00;
54+
test_lookup "Bridge" @@ (lookup_class_name acc 0x0680 |> default);
55+
test_lookup "Intel Corporation" @@ (lookup_vendor_name acc 0x8086 |> default);
56+
test_lookup "82371AB/EB/MB PIIX4 ACPI" @@ (lookup_device_name acc 0x8086 0x7113 |> default);
57+
test_lookup "Red Hat, Inc." @@ (lookup_subsystem_vendor_name acc 0x1af4 |> default);
58+
test_lookup "Qemu virtual machine" @@ (lookup_subsystem_device_name acc 0x8086 0x7113 0x1af4 0x1100 |> default);
59+
test_lookup "VGA compatible controller" @@ (lookup_class_name acc 0x0300 |> default);
60+
test_lookup "VGA controller" @@ (lookup_progif_name acc 0x0300 0x00 |> default);
6061
)
6162

6263
let _ =

0 commit comments

Comments
 (0)