Skip to content

Commit

Permalink
Fix a memory leak in equi_setstate()
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jan 10, 2024
1 parent 838d1e9 commit ee069c1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/equihash/src/tromp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ unsafe fn worker(eq: *mut CEqui, p: verify::Params, curr_state: &State) -> Vec<V
// - each solution contains `solution_len` u32 values.
// - the temporary slices are shared refs to a valid `eq` instance supplied by the caller.
// - the bytes in the shared ref are copied before they are returned.
// - dropping `solutions: &[u32]` does not drop the underlying memory owned by `eq`.
let solutions = (&mut chunks)
.map(|solution| solution.to_vec())
.collect::<Vec<_>>();
Expand Down
13 changes: 13 additions & 0 deletions components/equihash/tromp/equi_miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,19 @@ typedef struct equi equi;
eq->blake2b_free = blake2b_free;
eq->blake2b_update = blake2b_update;
eq->blake2b_finalize = blake2b_finalize;

const int err = pthread_barrier_init(&eq->barry, NULL, eq->nthreads);
assert(!err);

alloctrees(&eq->hta);
eq->nslots = (bsizes *)htalloc_alloc(&eq->hta, 2 * NBUCKETS, sizeof(au32));
eq->sols = (proof *)htalloc_alloc(&eq->hta, MAXSOLS, sizeof(proof));

// C malloc() does not guarantee zero-initialized memory (but calloc() does)
eq->blake_ctx = NULL;
eq->nsols = 0;
equi_clearslots(eq);

return eq;
}
void equi_free(equi *eq) {
Expand All @@ -276,6 +284,7 @@ typedef struct equi equi;
}

dealloctrees(&eq->hta);

free(eq->nslots);
free(eq->sols);
eq->blake2b_free(eq->blake_ctx);
Expand All @@ -287,6 +296,10 @@ typedef struct equi equi;
free(eq);
}
void equi_setstate(equi *eq, const BLAKE2bState *ctx) {
if (eq->blake_ctx) {
eq->blake2b_free(eq->blake_ctx);
}

eq->blake_ctx = eq->blake2b_clone(ctx);
memset(eq->nslots, 0, NBUCKETS * sizeof(au32)); // only nslots[0] needs zeroing
eq->nsols = 0;
Expand Down

0 comments on commit ee069c1

Please sign in to comment.