rust-lang / rust-lang/rust-bindgen

bad bindings for weird alignments

Open
#1,695 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

I'm getting a fail test for the Kangaroo Twelve hash function. The thing has quite some weird alignments depending on the architecture targeted so I'm not surprised (had similar issues when trying to write the bindings by hand):

#[test]
fn bindgen_test_layout_KangarooTwelve_Instance() {
    assert_eq!(
        ::std::mem::size_of::<KangarooTwelve_Instance>(),
        456usize,
        concat!("Size of: ", stringify!(KangarooTwelve_Instance))
    );

to replicate the problem:

  1. git clone https://github.com/mimoo/xkcp-sys.git
  2. git checkout bindgen
  3. cargo build or cargo test
  4. to check the .h note the OUT_DIR in the failed test: /home/vagrant/work/xkcp-sys/target/debug/build/<sys-xkcp-OUT_DIR>/out/XKCP/bin/generic64/libkeccak.a.headers/

if you check the .h files you'll see this something like this:

#define KeccakP1600_stateSizeInBytes    200
#define KeccakP1600_stateAlignment      8

// ...

typedef struct {
    KeccakWidth1600_12rounds_SpongeInstance queueNode;
    KeccakWidth1600_12rounds_SpongeInstance finalNode;
    size_t fixedOutputLength;
    size_t blockNumber;
    unsigned int queueAbsorbedLen;
    KangarooTwelve_Phases phase;
} KangarooTwelve_Instance;

// ...

KCP_DeclareSpongeStructure(KeccakWidth1600_12rounds, KeccakP1600_stateSizeInBytes, KeccakP1600_stateAlignment)
    KCP_DeclareSpongeFunctions(KeccakWidth1600_12rounds)

// ...

#define KCP_DeclareSpongeStructure(prefix, size, alignment) \
    ALIGN(alignment) typedef struct prefix##_SpongeInstanceStruct { \
        unsigned char state[size]; \
        unsigned int rate; \
        unsigned int byteIOIndex; \
        int squeezing; \
    } prefix##_SpongeInstance;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure in xkcp-sys on the bindgen branch with cargo build or cargo test, then inspect the generated headers under OUT_DIR and the bindgen_test_layout_KangarooTwelve_Instance assertion. Compare the C alignment and structure declarations with the generated Rust layout; done means the reported layout failure is resolved for the affected target architectures.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.