Skip to content

Commit 08ad0a9

Browse files
committed
Provide a Domain_shism module to be compatible with OCaml 5.0
1 parent 13bd919 commit 08ad0a9

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

config/cfg_domain.ml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let parse s = Scanf.sscanf s "%d.%d" (fun major minor -> (major, minor))
2+
3+
let () =
4+
let version = parse Sys.ocaml_version in
5+
if version >= (5, 0)
6+
then print_string "domain.stable.ml"
7+
else print_string "domain.pre500.ml"

config/dune

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
(executables
2-
(names cfg)
1+
(executable
2+
(name cfg)
3+
(modules cfg)
34
(libraries dune-configurator))
5+
6+
(executable
7+
(name cfg_domain)
8+
(modules cfg_domain))
9+
10+
(rule
11+
(with-stdout-to
12+
domain_shims
13+
(run ./cfg_domain.exe)))

src/cipher_block.ml

+13-8
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ module Counters = struct
8989
val unsafe_count_into : ctr -> Native.buffer -> int -> blocks:int -> unit
9090
end
9191

92+
let _tmp = Domain_shims.DLS.new_key (Fun.const (Bytes.make 16 '\000'))
93+
9294
module C64be = struct
9395
type ctr = int64
9496
let size = 8
9597
let of_cstruct cs = BE.get_uint64 cs 0
9698
let add = Int64.add
9799
let unsafe_count_into t buf off ~blocks =
98-
let _tmp = Bytes.make 16 '\x00' in
100+
let _tmp = Domain_shims.DLS.get _tmp in
99101
Bytes.set_int64_be _tmp 0 t;
100102
Native.count8be _tmp buf off ~blocks
101103
end
@@ -109,7 +111,7 @@ module Counters = struct
109111
let flip = if Int64.logxor w0 w0' < 0L then w0' > w0 else w0' < w0 in
110112
((if flip then Int64.succ w1 else w1), w0')
111113
let unsafe_count_into (w1, w0) buf off ~blocks =
112-
let _tmp = Bytes.make 16 '\x00' in
114+
let _tmp = Domain_shims.DLS.get _tmp in
113115
Bytes.set_int64_be _tmp 0 w1; Bytes.set_int64_be _tmp 8 w0;
114116
Native.count16be _tmp buf off ~blocks
115117
end
@@ -120,7 +122,7 @@ module Counters = struct
120122
let hi = 0xffffffff00000000L and lo = 0x00000000ffffffffL in
121123
(w1, Int64.(logor (logand hi w0) (add n w0 |> logand lo)))
122124
let unsafe_count_into (w1, w0) buf off ~blocks =
123-
let _tmp = Bytes.make 16 '\x00' in
125+
let _tmp = Domain_shims.DLS.get _tmp in
124126
Bytes.set_int64_be _tmp 0 w1; Bytes.set_int64_be _tmp 8 w0;
125127
Native.count16be4 _tmp buf off ~blocks
126128
end
@@ -240,11 +242,12 @@ module Modes = struct
240242
assert (cs.len >= tagsize);
241243
let k = Bytes.create keysize in
242244
Native.GHASH.keyinit cs.buffer cs.off k; k
245+
let _cs = Domain_shims.DLS.new_key (Fun.const (create_unsafe tagsize))
243246
let hash0 = Bytes.make tagsize '\x00'
244247
let digesti ~key i = (* Clobbers `_cs`! *)
245-
let _cs = create_unsafe tagsize in
246248
let res = Bytes.copy hash0 in
247249
i (fun cs -> Native.GHASH.ghash key res cs.buffer cs.off cs.len);
250+
let _cs = Domain_shims.DLS.get _cs in
248251
blit_from_bytes res 0 _cs 0 tagsize; _cs
249252
end
250253

@@ -257,17 +260,19 @@ module Modes = struct
257260

258261
let tag_size = GHASH.tagsize
259262
let key_sizes, block_size = C.(key, block)
263+
let z128 = Domain_shims.DLS.new_key (Fun.const (create block_size))
264+
let h = Domain_shims.DLS.new_key (Fun.const (create block_size))
260265

261266
let of_secret cs =
262267
let key = C.e_of_secret cs in
263-
let z128 = create block_size in
264-
let h = create block_size in
268+
let z128 = Domain_shims.DLS.get z128 in
269+
let h = Domain_shims.DLS.get h in
265270
C.encrypt ~key ~blocks:1 z128.buffer z128.off h.buffer h.off;
266271
{ key ; hkey = GHASH.derive h }
267272

268273
let bits64 cs = Int64.of_int (length cs * 8)
269-
let pack64s = fun a b ->
270-
let _cs = create_unsafe 16 in
274+
let _cs = Domain_shims.DLS.new_key (Fun.const (create_unsafe 16))
275+
let pack64s = let _cs = Domain_shims.DLS.get _cs in fun a b ->
271276
BE.set_uint64 _cs 0 a; BE.set_uint64 _cs 8 b; _cs
272277

273278
let counter ~hkey nonce = match length nonce with

src/domain.pre500.ml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module DLS = struct
2+
type 'a key =
3+
{ init : unit -> 'a
4+
; cell : 'a option ref }
5+
6+
let new_key ?split_from_parent:_ fn =
7+
{ init= fn; cell= ref None }
8+
9+
let get { init; cell; } = match !cell with
10+
| None -> cell := Some (init ()); Option.get !cell
11+
| Some value -> value
12+
13+
let set { cell; _ } value = cell := Some value
14+
end

src/domain.stable.ml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include Stdlib.Domain

src/dune

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
(:standard)
2020
(:include cflags.sexp))))
2121

22+
(rule
23+
(copy %{read:../config/domain_shims} domain_shims.ml))
24+
2225
(env
2326
(dev
2427
(c_flags (:include cflags_warn.sexp))))

0 commit comments

Comments
 (0)