From f1290371f2cf15b2abd645f5b292b6a11adfea28 Mon Sep 17 00:00:00 2001 From: Erik Mossberg Date: Thu, 22 May 2014 04:31:31 +0200 Subject: [PATCH 1/3] fix segfault and compilation error on OSX10.9 and FreeBSD 10 with clang --- crypto/oaes_lib.c | 2 ++ cryptonight.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crypto/oaes_lib.c b/crypto/oaes_lib.c index 2063b30e8..94133f782 100644 --- a/crypto/oaes_lib.c +++ b/crypto/oaes_lib.c @@ -36,7 +36,9 @@ static const char _NR[] = { #include #include #include +#if !((defined(__FreeBSD__) && __FreeBSD__ >= 10) || defined(__APPLE__)) #include +#endif #include #include #include diff --git a/cryptonight.c b/cryptonight.c index 6042af980..e0e7f5b5c 100644 --- a/cryptonight.c +++ b/cryptonight.c @@ -194,7 +194,7 @@ void cryptonight_hash_ctx(void* output, const void* input, size_t len, struct cr } void cryptonight_hash(void* output, const void* input, size_t len) { - cryptonight_hash_ctx(output, input, len, alloca(sizeof(struct cryptonight_ctx))); + cryptonight_hash_ctx(output, input, len, malloc(sizeof(struct cryptonight_ctx))); } void cryptonight_hash_ctx_aes_ni(void* output, const void* input, size_t len, struct cryptonight_ctx* ctx) { @@ -269,7 +269,7 @@ int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget, const uint32_t Htarg = ptarget[7]; uint32_t hash[HASH_SIZE / 4] __attribute__((aligned(32))); - struct cryptonight_ctx *ctx = alloca(sizeof(struct cryptonight_ctx)); + struct cryptonight_ctx *ctx = malloc(sizeof(struct cryptonight_ctx)); if (aes_ni) { do { From f507c1f78130ac06813e9391dd893ca4349dfc51 Mon Sep 17 00:00:00 2001 From: Erik Mossberg Date: Thu, 22 May 2014 05:50:31 +0200 Subject: [PATCH 2/3] add deallocation, doh --- cryptonight.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cryptonight.c b/cryptonight.c index e0e7f5b5c..492500c21 100644 --- a/cryptonight.c +++ b/cryptonight.c @@ -194,7 +194,9 @@ void cryptonight_hash_ctx(void* output, const void* input, size_t len, struct cr } void cryptonight_hash(void* output, const void* input, size_t len) { - cryptonight_hash_ctx(output, input, len, malloc(sizeof(struct cryptonight_ctx))); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); + cryptonight_hash_ctx(output, input, len, ctx); + free(ctx); } void cryptonight_hash_ctx_aes_ni(void* output, const void* input, size_t len, struct cryptonight_ctx* ctx) { @@ -269,7 +271,7 @@ int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget, const uint32_t Htarg = ptarget[7]; uint32_t hash[HASH_SIZE / 4] __attribute__((aligned(32))); - struct cryptonight_ctx *ctx = malloc(sizeof(struct cryptonight_ctx)); + struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx)); if (aes_ni) { do { @@ -290,6 +292,8 @@ int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget, } } while (likely((n <= max_nonce && !work_restart[thr_id].restart))); } + + free(ctx); *hashes_done = n - first_nonce + 1; return 0; } From 79a4772167cf22d90e411ae4f22656aebd52084d Mon Sep 17 00:00:00 2001 From: Erik Mossberg Date: Thu, 22 May 2014 16:15:36 +0200 Subject: [PATCH 3/3] make sure to free context in all paths --- cryptonight.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cryptonight.c b/cryptonight.c index 492500c21..4aab68ca6 100644 --- a/cryptonight.c +++ b/cryptonight.c @@ -279,6 +279,7 @@ int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget, cryptonight_hash_ctx_aes_ni(hash, pdata, 76, ctx); if (unlikely(hash[7] < ptarget[7])) { *hashes_done = n - first_nonce + 1; + free(ctx); return true; } } while (likely((n <= max_nonce && !work_restart[thr_id].restart))); @@ -288,6 +289,7 @@ int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget, cryptonight_hash_ctx(hash, pdata, 76, ctx); if (unlikely(hash[7] < ptarget[7])) { *hashes_done = n - first_nonce + 1; + free(ctx); return true; } } while (likely((n <= max_nonce && !work_restart[thr_id].restart)));