ethereum / ethash

190 stars 516 forks source link

munmap not delete the right address range #126

Open DinoStray opened 4 years ago

DinoStray commented 4 years ago

we use mmap map the whole file, and ret->data is only part of the file without ETHASH_DAG_MAGIC_NUM

static bool ethash_mmap(struct ethash_full* ret, FILE* f)
{
    int fd;
    char* mmapped_data;
    errno = 0;
    ret->file = f;
    if ((fd = ethash_fileno(ret->file)) == -1) {
        return false;
    }
    mmapped_data= mmap(
        NULL,
        (size_t)ret->file_size + ETHASH_DAG_MAGIC_NUM_SIZE,
        PROT_READ | PROT_WRITE,
        MAP_SHARED,
        fd,
        0
    );
    if (mmapped_data == MAP_FAILED) {
        return false;
    }
    ret->data = (node*)(mmapped_data + ETHASH_DAG_MAGIC_NUM_SIZE);
    return true;
}

finally we munmap the address only begin with ret->data, then munmap(..) != 0

munmap(full->data, (size_t)full->file_size);