Skip to content

Commit

Permalink
Hardness more global roots.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Dec 19, 2024
1 parent 107871f commit 6ef87c1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
=====
* Use generational global roots to protect
encoder and decoder callbacks.
* Harness more global roots.

0.5.0 (2023-09-05)
=====
Expand Down
2 changes: 1 addition & 1 deletion flac.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.5.0"
version: "0.5.1"
synopsis: "Bindings to libflac"
maintainer: ["The Savonet Team <[email protected]>"]
authors: ["The Savonet Team <[email protected]>"]
Expand Down
34 changes: 25 additions & 9 deletions src/flac_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,19 +357,25 @@ FLAC__StreamDecoderReadStatus static dec_read_callback(
caml_acquire_runtime_system();

int readlen = *bytes;
value data = caml_alloc_string(readlen);

value data = Val_unit;
value ret = Val_unit;
caml_register_generational_global_root(&data);
caml_register_generational_global_root(&ret);

value ret = caml_callback3_exn(Dec_read(callbacks->callbacks), data,
Val_int(0), Val_int(readlen));
data = caml_alloc_string(readlen);

ret = caml_callback3_exn(Dec_read(callbacks->callbacks), data, Val_int(0),
Val_int(readlen));

if (Is_exception_result(ret)) {
caml_remove_generational_global_root(&data);
caml_remove_generational_global_root(&ret);
caml_raise(Extract_exception(ret));
}

caml_remove_generational_global_root(&data);
caml_remove_generational_global_root(&ret);

memcpy(buffer, String_val(data), Int_val(ret));
*bytes = Int_val(ret);
Expand Down Expand Up @@ -407,9 +413,12 @@ dec_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
ocaml_flac_register_thread();
caml_acquire_runtime_system();

value data = caml_alloc_tuple(channels);

value data = Val_unit;
value ret = Val_unit;
caml_register_generational_global_root(&data);
caml_register_generational_global_root(&ret);

data = caml_alloc_tuple(channels);

int c, i;
for (c = 0; c < channels; c++) {
Expand All @@ -419,14 +428,16 @@ dec_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
sample_to_double(buffer[c][i], bps));
}

value ret = caml_callback_exn(Dec_write(callbacks->callbacks), data);
ret = caml_callback_exn(Dec_write(callbacks->callbacks), data);

if (Is_exception_result(ret)) {
caml_remove_generational_global_root(&data);
caml_remove_generational_global_root(&ret);
caml_raise(Extract_exception(ret));
}

caml_remove_generational_global_root(&data);
caml_remove_generational_global_root(&ret);

caml_release_runtime_system();

Expand Down Expand Up @@ -674,20 +685,25 @@ enc_write_callback(const FLAC__StreamEncoder *encoder,
ocaml_flac_register_thread();
caml_acquire_runtime_system();

value buf = caml_alloc_string(bytes);

value buf = Val_unit;
value res = Val_unit;
caml_register_generational_global_root(&buf);
caml_register_generational_global_root(&res);

buf = caml_alloc_string(bytes);

memcpy(Bytes_val(buf), buffer, bytes);

value res = caml_callback_exn(Enc_write(callbacks), buf);
res = caml_callback_exn(Enc_write(callbacks), buf);

if (Is_exception_result(res)) {
caml_remove_generational_global_root(&buf);
caml_remove_generational_global_root(&res);
caml_raise(Extract_exception(res));
}

caml_remove_generational_global_root(&buf);
caml_remove_generational_global_root(&res);

caml_release_runtime_system();

Expand Down

0 comments on commit 6ef87c1

Please sign in to comment.