Skip to content

Commit fe6dba9

Browse files
author
Christopher Zimmermann
committed
Don't access caml values without acquired runtime
We mustn't access the context in a custom block while the runtime is released. It might be relocated from minor to major heap or during major heap compaction. Also include string.h for memcpy().
1 parent 077d171 commit fe6dba9

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

sha1_stubs.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#define _GNU_SOURCE
1717
#include <unistd.h>
18+
#include <string.h>
1819
#include <fcntl.h>
1920
#include "sha1.h"
2021

@@ -73,12 +74,15 @@ CAMLprim value stub_sha1_update(value ctx, value data, value ofs, value len)
7374
CAMLprim value stub_sha1_update_bigarray(value ctx, value buf)
7475
{
7576
CAMLparam2(ctx, buf);
77+
struct sha1_ctx ctx_dup;
7678
unsigned char *data = Data_bigarray_val(buf);
7779
size_t len = Bigarray_val(buf)->dim[0];
7880

81+
ctx_dup = *GET_CTX_STRUCT(ctx);
7982
caml_release_runtime_system();
80-
sha1_update(GET_CTX_STRUCT(ctx), data, len);
83+
sha1_update(&ctx_dup, data, len);
8184
caml_acquire_runtime_system();
85+
*GET_CTX_STRUCT(ctx) = ctx_dup;
8286

8387
CAMLreturn(Val_unit);
8488
}

sha256_stubs.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#define _GNU_SOURCE
1717
#include <unistd.h>
18+
#include <string.h>
1819
#include <fcntl.h>
1920
#include "sha256.h"
2021

@@ -72,12 +73,15 @@ CAMLprim value stub_sha256_update(value ctx, value data, value ofs, value len)
7273
CAMLprim value stub_sha256_update_bigarray(value ctx, value buf)
7374
{
7475
CAMLparam2(ctx, buf);
76+
struct sha256_ctx ctx_dup;
7577
unsigned char *data = Data_bigarray_val(buf);
7678
size_t len = Bigarray_val(buf)->dim[0];
7779

80+
ctx_dup = *GET_CTX_STRUCT(ctx);
7881
caml_release_runtime_system();
79-
sha256_update(GET_CTX_STRUCT(ctx), data, len);
82+
sha256_update(&ctx_dup, data, len);
8083
caml_acquire_runtime_system();
84+
*GET_CTX_STRUCT(ctx) = ctx_dup;
8185

8286
CAMLreturn(Val_unit);
8387
}

sha512_stubs.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#define _GNU_SOURCE
1717
#include <unistd.h>
18+
#include <string.h>
1819
#include <fcntl.h>
1920
#include "sha512.h"
2021

@@ -72,12 +73,15 @@ CAMLprim value stub_sha512_update(value ctx, value data, value ofs, value len)
7273
CAMLprim value stub_sha512_update_bigarray(value ctx, value buf)
7374
{
7475
CAMLparam2(ctx, buf);
76+
struct sha512_ctx ctx_dup;
7577
unsigned char *data = Data_bigarray_val(buf);
7678
size_t len = Bigarray_val(buf)->dim[0];
7779

80+
ctx_dup = *GET_CTX_STRUCT(ctx);
7881
caml_release_runtime_system();
79-
sha512_update(GET_CTX_STRUCT(ctx), data, len);
82+
sha512_update(&ctx_dup, data, len);
8083
caml_acquire_runtime_system();
84+
*GET_CTX_STRUCT(ctx) = ctx_dup;
8185

8286
CAMLreturn(Val_unit);
8387
}

0 commit comments

Comments
 (0)