Skip to content

Nondeterministic results when building as cdylib #79

@rmcgibbo

Description

@rmcgibbo

I seem to have xxh3 hashes that depend on some uninitialized variable (?) only when compiling into a cdylib. I initially noticed this in a python extension with pyo3, but I've managed to make the following simple reproduction:

# Cargo.toml
[package]
name = "foo"
version = "0.1.0"
authors = ["Robert T. McGibbon <rmcgibbo@gmail.com>"]
edition = "2018"

[lib]
name = "foo"
crate-type = ["cdylib"]

[dependencies]
twox-hash = {version = "1.6.1", default-features = false }
# src/lib.rs
use std::hash::Hasher;
use twox_hash::xxh3::HasherExt;

#[no_mangle]
pub extern "C"
fn hash_a_constant() {
    let seed = 0;
    let ss = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    let mut hasher = twox_hash::Xxh3Hash128::with_seed(seed);
    hasher.write(ss.as_bytes());
    println!("hash a constant of size {} {}", ss.len(), hasher.finish_ext());
}

Running with

cargo build
$ python -c 'import ctypes; ctypes.CDLL("target/debug/libfoo.so").hash_a_constant()'
hash a constant of size 257 233319518405983521442151793707413278448
$ python -c 'import ctypes; ctypes.CDLL("target/debug/libfoo.so").hash_a_constant()'
hash a constant of size 257 263704682723094846392981853651016498070
$ python -c 'import ctypes; ctypes.CDLL("target/debug/libfoo.so").hash_a_constant()'
hash a constant of size 257 278186113363433872561296712692319909969

I seem to get different results every time.

If instead I move the contents of the function hash_a_constant to fn main and run it as a rust binary, rather than calling the symbol in the .so, then I always get a stable value (295345945357457693424139354068657467622).

For what it's worth, this is on rustc 1.52.1 on x86_64-linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions