Skip to content

Commit 72e8c6b

Browse files
authored
Prevent initializing stereotool with two different libraries. (#4182)
1 parent 3d2cb3b commit 72e8c6b

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/core/operators/stereotool_op.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,18 @@ let _ =
171171
let preset = Lang.to_valued_option Lang.to_string preset_val in
172172
let handler =
173173
let library = Utils.check_readable ~pos:(Lang.pos p) library in
174-
try Stereotool.init ?license_key ~filename:library ()
175-
with Stereotool.Library_not_found ->
176-
Runtime_error.raise ~pos:(Lang.pos p)
177-
~message:"Invalid stereotool library" "invalid"
174+
try Stereotool.init ?license_key ~filename:library () with
175+
| Stereotool.Library_not_found ->
176+
Runtime_error.raise ~pos:(Lang.pos p)
177+
~message:"Invalid stereotool library" "invalid"
178+
| Stereotool.Library_initialized f ->
179+
Runtime_error.raise ~pos:(Lang.pos p)
180+
~message:
181+
(Printf.sprintf
182+
"Stereotool already initialized with a different library: \
183+
%s"
184+
(Lang_string.quote_string f))
185+
"invalid"
178186
in
179187
(match preset with
180188
| None -> ()

src/stereotool/stereotool.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module type Config = sig
1010
end
1111

1212
exception Library_not_found
13+
exception Library_initialized of string
1314

1415
let strnlen = foreign "strnlen" (ocaml_bytes @-> int @-> returning int)
1516

@@ -78,7 +79,12 @@ let int_of_load_type = function
7879
| `Repair_no_pnr -> 11069
7980
| `Sublevel_pnr -> 10699
8081

82+
let initialized = Atomic.make None
83+
8184
let init ?license_key ~filename () =
85+
(match Atomic.get initialized with
86+
| Some f when f <> filename -> raise (Library_initialized f)
87+
| _ -> Atomic.set initialized (Some filename));
8288
try
8389
let module C = C (struct
8490
let filename = filename

src/stereotool/stereotool.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type load_type =
1515
| `Sublevel_pnr ]
1616

1717
exception Library_not_found
18+
exception Library_initialized of string
1819

1920
val init : ?license_key:string -> filename:string -> unit -> t
2021
val software_version : t -> int

0 commit comments

Comments
 (0)