Browse Source

ethash.go, libethash: pass hashes and return values on the stack

Passing Go pointers into C is not safe. See https://golang.org/issue/8310
for more background. This commit removes uses like the ones below and
passes hashes and return values on the stack instead.

    var h common.Hash
    C.function(&h[0], ...)

    var ret C.struct_ethash_return_value
    C.function(&ret)

This requires changes in the internal C API.
cl-refactor
Felix Lange 10 years ago
parent
commit
d3396ec513
  1. 34
      internal.c
  2. 9
      internal.h

34
internal.c

@ -335,22 +335,19 @@ void ethash_light_delete(ethash_light_t light)
free(light); free(light);
} }
bool ethash_light_compute_internal( ethash_return_value_t ethash_light_compute_internal(
ethash_return_value_t* ret,
ethash_light_t light, ethash_light_t light,
uint64_t full_size, uint64_t full_size,
ethash_h256_t const header_hash, ethash_h256_t const header_hash,
uint64_t nonce uint64_t nonce
) )
{ {
return ethash_hash( ethash_return_value_t ret;
ret, ret.success = true;
NULL, if (!ethash_hash(&ret, NULL, light, full_size, header_hash, nonce)) {
light, ret.success = false;
full_size, }
header_hash, return ret;
nonce
);
} }
ethash_return_value_t ethash_light_compute( ethash_return_value_t ethash_light_compute(
@ -359,17 +356,10 @@ ethash_return_value_t ethash_light_compute(
uint64_t nonce uint64_t nonce
) )
{ {
ethash_return_value_t ret;
ret.success = true;
uint64_t full_size = ethash_get_datasize(light->block_number); uint64_t full_size = ethash_get_datasize(light->block_number);
if (!ethash_light_compute_internal(&ret, light, full_size, header_hash, nonce)) { return ethash_light_compute_internal(light, full_size, header_hash, nonce);
ret.success = false;
}
return ret;
} }
static bool ethash_mmap(struct ethash_full* ret, FILE* f) static bool ethash_mmap(struct ethash_full* ret, FILE* f)
{ {
int fd; int fd;
@ -395,7 +385,7 @@ static bool ethash_mmap(struct ethash_full* ret, FILE* f)
ethash_full_t ethash_full_new_internal( ethash_full_t ethash_full_new_internal(
char const* dirname, char const* dirname,
ethash_h256_t const* seed_hash, ethash_h256_t const seed_hash,
uint64_t full_size, uint64_t full_size,
ethash_light_t const light, ethash_light_t const light,
ethash_callback_t callback ethash_callback_t callback
@ -408,7 +398,7 @@ ethash_full_t ethash_full_new_internal(
return NULL; return NULL;
} }
ret->file_size = (size_t)full_size; ret->file_size = (size_t)full_size;
switch (ethash_io_prepare(dirname, *seed_hash, &f, (size_t)full_size, false)) { switch (ethash_io_prepare(dirname, seed_hash, &f, (size_t)full_size, false)) {
case ETHASH_IO_FAIL: case ETHASH_IO_FAIL:
goto fail_free_full; goto fail_free_full;
case ETHASH_IO_MEMO_MATCH: case ETHASH_IO_MEMO_MATCH:
@ -418,7 +408,7 @@ ethash_full_t ethash_full_new_internal(
return ret; return ret;
case ETHASH_IO_MEMO_SIZE_MISMATCH: case ETHASH_IO_MEMO_SIZE_MISMATCH:
// if a DAG of same filename but unexpected size is found, silently force new file creation // if a DAG of same filename but unexpected size is found, silently force new file creation
if (ethash_io_prepare(dirname, *seed_hash, &f, (size_t)full_size, true) != ETHASH_IO_MEMO_MISMATCH) { if (ethash_io_prepare(dirname, seed_hash, &f, (size_t)full_size, true) != ETHASH_IO_MEMO_MISMATCH) {
goto fail_free_full; goto fail_free_full;
} }
// fallthrough to the mismatch case here, DO NOT go through match // fallthrough to the mismatch case here, DO NOT go through match
@ -462,7 +452,7 @@ ethash_full_t ethash_full_new(ethash_light_t light, ethash_callback_t callback)
} }
uint64_t full_size = ethash_get_datasize(light->block_number); uint64_t full_size = ethash_get_datasize(light->block_number);
ethash_h256_t seedhash = ethash_get_seedhash(light->block_number); ethash_h256_t seedhash = ethash_get_seedhash(light->block_number);
return ethash_full_new_internal(strbuf, &seedhash, full_size, light, callback); return ethash_full_new_internal(strbuf, seedhash, full_size, light, callback);
} }
void ethash_full_delete(ethash_full_t full) void ethash_full_delete(ethash_full_t full)

9
internal.h

@ -89,16 +89,13 @@ ethash_light_t ethash_light_new_internal(uint64_t cache_size, ethash_h256_t cons
/** /**
* Calculate the light client data. Internal version. * Calculate the light client data. Internal version.
* *
* @param ret An object of ethash_return_value to hold the return value
* @param light The light client handler * @param light The light client handler
* @param full_size The size of the full data in bytes. * @param full_size The size of the full data in bytes.
* @param header_hash The header hash to pack into the mix * @param header_hash The header hash to pack into the mix
* @param nonce The nonce to pack into the mix * @param nonce The nonce to pack into the mix
* @return true if all went well and false if there were invalid * @return The resulting hash.
* parameters given.
*/ */
bool ethash_light_compute_internal( ethash_return_value_t ethash_light_compute_internal(
ethash_return_value_t* ret,
ethash_light_t light, ethash_light_t light,
uint64_t full_size, uint64_t full_size,
ethash_h256_t const header_hash, ethash_h256_t const header_hash,
@ -130,7 +127,7 @@ struct ethash_full {
*/ */
ethash_full_t ethash_full_new_internal( ethash_full_t ethash_full_new_internal(
char const* dirname, char const* dirname,
ethash_h256_t const* seed_hash, ethash_h256_t const seed_hash,
uint64_t full_size, uint64_t full_size,
ethash_light_t const light, ethash_light_t const light,
ethash_callback_t callback ethash_callback_t callback

Loading…
Cancel
Save