Skip to content

Commit

Permalink
Don't access caml values without acquired runtime
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
Christopher Zimmermann authored and djs55 committed Jan 2, 2018
1 parent 6036784 commit 6c82c41
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion sha1_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef SSIZE_T ssize_t;
#else
#include <unistd.h>
#endif
#include <string.h>
#include <fcntl.h>
#include <string.h>
#include "sha1.h"
Expand Down Expand Up @@ -87,12 +88,15 @@ CAMLprim value stub_sha1_update(value ctx, value data, value ofs, value len)
CAMLprim value stub_sha1_update_bigarray(value ctx, value buf)
{
CAMLparam2(ctx, buf);
struct sha1_ctx ctx_dup;
unsigned char *data = Data_bigarray_val(buf);
size_t len = Bigarray_val(buf)->dim[0];

ctx_dup = *GET_CTX_STRUCT(ctx);
caml_release_runtime_system();
sha1_update(GET_CTX_STRUCT(ctx), data, len);
sha1_update(&ctx_dup, data, len);
caml_acquire_runtime_system();
*GET_CTX_STRUCT(ctx) = ctx_dup;

CAMLreturn(Val_unit);
}
Expand Down
6 changes: 5 additions & 1 deletion sha256_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef SSIZE_T ssize_t;
#else
#include <unistd.h>
#endif
#include <string.h>
#include <fcntl.h>
#include <string.h>
#include "sha256.h"
Expand Down Expand Up @@ -86,12 +87,15 @@ CAMLprim value stub_sha256_update(value ctx, value data, value ofs, value len)
CAMLprim value stub_sha256_update_bigarray(value ctx, value buf)
{
CAMLparam2(ctx, buf);
struct sha256_ctx ctx_dup;
unsigned char *data = Data_bigarray_val(buf);
size_t len = Bigarray_val(buf)->dim[0];

ctx_dup = *GET_CTX_STRUCT(ctx);
caml_release_runtime_system();
sha256_update(GET_CTX_STRUCT(ctx), data, len);
sha256_update(&ctx_dup, data, len);
caml_acquire_runtime_system();
*GET_CTX_STRUCT(ctx) = ctx_dup;

CAMLreturn(Val_unit);
}
Expand Down
6 changes: 5 additions & 1 deletion sha512_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef SSIZE_T ssize_t;
#else
#include <unistd.h>
#endif
#include <string.h>
#include <fcntl.h>
#include <string.h>
#include "sha512.h"
Expand Down Expand Up @@ -86,12 +87,15 @@ CAMLprim value stub_sha512_update(value ctx, value data, value ofs, value len)
CAMLprim value stub_sha512_update_bigarray(value ctx, value buf)
{
CAMLparam2(ctx, buf);
struct sha512_ctx ctx_dup;
unsigned char *data = Data_bigarray_val(buf);
size_t len = Bigarray_val(buf)->dim[0];

ctx_dup = *GET_CTX_STRUCT(ctx);
caml_release_runtime_system();
sha512_update(GET_CTX_STRUCT(ctx), data, len);
sha512_update(&ctx_dup, data, len);
caml_acquire_runtime_system();
*GET_CTX_STRUCT(ctx) = ctx_dup;

CAMLreturn(Val_unit);
}
Expand Down

0 comments on commit 6c82c41

Please sign in to comment.