Skip to content

Commit 8850839

Browse files
committed
feature: switch dune to use blake3
<!-- ps-id: 4f9dee83-85dd-4b47-ba4f-920087ea5851 --> Signed-off-by: Rudi Grinberg <[email protected]>
1 parent 4d75f93 commit 8850839

File tree

33 files changed

+260
-298
lines changed

33 files changed

+260
-298
lines changed

.github/workflows/workflow.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ jobs:
8181
# Install ocamlfind-secondary and ocaml-secondary-compiler, if needed
8282
- run: opam install ./dune.opam --deps-only --with-test
8383

84+
- name: Print config
85+
run: opam exec -- ocamlopt -config
86+
8487
- name: Install system deps on macOS
8588
run: brew install coreutils pkg-config file
8689
if: ${{ matrix.os == 'macos-latest' }}

doc/changes/11735.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Switch from MD5 to BLAKE3 for digesting targets and rules. BLAKE3 is both
2+
more performant and difficult to break than MD5 (#11735, @rgrinberg,
3+
@Alizter)

src/dune_digest/cached_digest.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module P = Persistent.Make (struct
5757
type nonrec t = t
5858

5959
let name = "DIGEST-DB"
60-
let version = 6
60+
let version = 7
6161
let to_dyn = to_dyn
6262

6363
let test_example () =

src/dune_digest/digest.ml

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
open Stdune
22

3-
type t = string
3+
module T = struct
4+
type t = Blake3_mini.Digest.t
45

5-
external md5_fd : Unix.file_descr -> string = "dune_md5_fd"
6+
let to_string = Blake3_mini.Digest.to_hex
7+
let to_dyn s = Dyn.variant "digest" [ String (to_string s) ]
8+
let compare x y = Ordering.of_int (Blake3_mini.Digest.compare x y)
9+
end
610

7-
module D = Stdlib.Digest
8-
module Set = String.Set
9-
module Map = String.Map
11+
include T
12+
module C = Comparable.Make (T)
13+
module Set = C.Set
14+
module Map = C.Map
1015
module Metrics = Dune_metrics
1116

1217
let file file =
@@ -21,24 +26,24 @@ let file file =
2126
raise (Sys_error (sprintf "%s: Permission denied" file))
2227
| exception exn -> reraise exn
2328
in
24-
Exn.protectx fd ~f:md5_fd ~finally:Unix.close
29+
Exn.protectx fd ~f:Blake3_mini.fd ~finally:Unix.close
2530
;;
2631

32+
let equal = Blake3_mini.Digest.equal
2733
let hash = Poly.hash
28-
let equal = String.equal
2934
let file p = file (Path.to_string p)
30-
let compare x y = Ordering.of_int (D.compare x y)
31-
let to_string = D.to_hex
32-
let to_dyn s = Dyn.variant "digest" [ String (to_string s) ]
33-
34-
let from_hex s =
35-
match D.from_hex s with
36-
| s -> Some s
37-
| exception Invalid_argument _ -> None
35+
let from_hex s = Blake3_mini.Digest.of_hex s
36+
let hasher = lazy (Blake3_mini.create ())
37+
38+
let string s =
39+
let hasher = Lazy.force hasher in
40+
Blake3_mini.feed_string hasher s ~pos:0 ~len:(String.length s);
41+
let res = Blake3_mini.digest hasher in
42+
Blake3_mini.reset hasher;
43+
res
3844
;;
3945

40-
let string = D.string
41-
let to_string_raw s = s
46+
let to_string_raw s = Blake3_mini.Digest.to_binary s
4247

4348
(* We use [No_sharing] to avoid generating different digests for inputs that
4449
differ only in how they share internal values. Without [No_sharing], if a
@@ -54,7 +59,7 @@ let generic a =
5459
let path_with_executable_bit =
5560
(* We follow the digest scheme used by Jenga. *)
5661
let string_and_bool ~digest_hex ~bool =
57-
string (digest_hex ^ if bool then "\001" else "\000")
62+
string (Blake3_mini.Digest.to_hex digest_hex ^ if bool then "\001" else "\000")
5863
in
5964
fun ~executable ~content_digest ->
6065
string_and_bool ~digest_hex:content_digest ~bool:executable
@@ -86,15 +91,15 @@ end
8691

8792
exception E of Path_digest_error.t
8893

89-
let directory_digest_version = 2
94+
let directory_digest_version = 3
9095

9196
let path_with_stats ~allow_dirs path (stats : Stats_for_digest.t) =
9297
let rec loop path (stats : Stats_for_digest.t) =
9398
match stats.st_kind with
9499
| S_LNK ->
95100
Dune_filesystem_stubs.Unix_error.Detailed.catch
96101
(fun path ->
97-
let contents = Unix.readlink (Path.to_string path) in
102+
let contents = Path.to_string path |> Unix.readlink |> string in
98103
path_with_executable_bit ~executable:stats.executable ~content_digest:contents)
99104
path
100105
|> Result.map_error ~f:(fun x -> Path_digest_error.Unix_error x)

src/dune_digest/digest.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
open Stdune
22

3-
(** Digests (MD5) *)
3+
(** Digests (BLAKE3) *)
44

55
type t
66

src/dune_digest/dune

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@
77
dune_console
88
dune_util
99
stdune
10-
unix)
11-
(foreign_stubs
12-
(names dune_digest_stubs)
13-
(language c)))
10+
unix))

src/dune_digest/dune_digest_stubs.c

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/dune_engine/build_system.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ end = struct
267267

268268
(* The current version of the rule digest scheme. We should increment it when
269269
making any changes to the scheme, to avoid collisions. *)
270-
let rule_digest_version = 22
270+
let rule_digest_version = 23
271271

272272
let compute_rule_digest
273273
(rule : Rule.t)
@@ -744,7 +744,7 @@ end = struct
744744

745745
(* The current version of the action digest scheme. We should increment it when
746746
making any changes to the scheme, to avoid collisions. *)
747-
let action_digest_version = 2
747+
let action_digest_version = 3
748748

749749
let execute_action_generic
750750
~observing_facts

src/dune_engine/rule_cache.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module Workspace_local = struct
3131
type nonrec t = t
3232

3333
let name = "INCREMENTAL-DB"
34-
let version = 5
34+
let version = 6
3535
let to_dyn = to_dyn
3636

3737
let test_example () =

src/dune_engine/target_promotion.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module To_delete = struct
1212
type t = Path.Source.Set.t
1313

1414
let name = "PROMOTED-TO-DELETE"
15-
let version = 2
15+
let version = 3
1616
let to_dyn = Path.Source.Set.to_dyn
1717
let test_example () = Path.Source.Set.empty
1818
end)

0 commit comments

Comments
 (0)