rust-lang / rust-lang/rust-bindgen

Bitfield Signed Integer Handling Incorrect

Open
#1,160 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

A bitfield with a signed integer works as expected (including overflow) for all functionality except for the Rust getter which in all cases returned an unsigned value.

Input C/C++ Header
typedef struct {
    unsigned int a: 1;
    unsigned int b: 1;
    int c: 2;

} StructWithBitfields;
StructWithBitfields create_bitfield();
void walk_bitfield();
void print_bitfield(StructWithBitfields bfield);
C Static functions
#include <stdio.h>
#include "../wrapper.h"

StructWithBitfields create_bitfield() {
    StructWithBitfields bfield;
    bfield.a = 0;
    bfield.b = 0;
    bfield.c = 0;
    return bfield;
}

void walk_bitfield() {
    StructWithBitfields bfield = create_bitfield();
    print_bitfield(bfield);
    bfield.c = 1;
    print_bitfield(bfield);
    bfield.c = 2;
    print_bitfield(bfield);
    bfield.c = 3;
    print_bitfield(bfield);
    bfield.c = 4;
    print_bitfield(bfield);
    bfield.c = -1;
    print_bitfield(bfield);
    bfield.c = -2;
    print_bitfield(bfield);
    bfield.c = -3;
    print_bitfield(bfield);
    bfield.c = -4;
    print_bitfield(bfield);
}

void print_bitfield(StructWithBitfields bfield) {
    printf("StructWithBitfields: a:%d, b:%d, c:%d\n", bfield.a, bfield.b, bfield.c);
}
Bindgen Invocation
extern crate bindgen;
extern crate gcc;

use std::env;
use std::path::PathBuf;

fn main() {

    gcc::Config::new()
        .file("static/bitfields.c")
        .compile("bitfield-static-functions.a");

    let bindings = bindgen::Builder::default()
        .header("wrapper.h")
        .derive_default(true)
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}
Main Source
extern crate bitfields_sys;
use std::default::Default;
use bitfields_sys::{StructWithBitfields,create_bitfield, walk_bitfield, print_bitfield};

fn main() {
    println!("Print C walk");
    unsafe{walk_bitfield()};

    println!("Print Rust walk");
    let mut bfield = StructWithBitfields{
        _bitfield_1: StructWithBitfields::new_bitfield_1(0,0,0),
        ..Default::default()
    };
    println!("c set to {}", bfield.c());
    bfield.set_c(1);
    println!("c set to {}", bfield.c());
    bfield.set_c(2);
    println!("c set to {}", bfield.c());
    bfield.set_c(3);
    println!("c set to {}", bfield.c());
    bfield.set_c(4);
    println!("c set to {}", bfield.c());
    bfield.set_c(-1);
    println!("c set to {}", bfield.c());
    bfield.set_c(-2);
    println!("c set to {}", bfield.c());
    bfield.set_c(-3);
    println!("c set to {}", bfield.c());
    bfield.set_c(-4);
    println!("c set to {}", bfield.c());
}

Actual Results
Print Rust walk
c set to 0
c set to 1
c set to 2
c set to 3
c set to 0
c set to 3
c set to 2
c set to 1
c set to 0
Expected Results (Just the c field).
Print C walk
StructWithBitfields: a:0, b:0, c:0
StructWithBitfields: a:0, b:0, c:1
StructWithBitfields: a:0, b:0, c:-2
StructWithBitfields: a:0, b:0, c:-1
StructWithBitfields: a:0, b:0, c:0
StructWithBitfields: a:0, b:0, c:-1
StructWithBitfields: a:0, b:0, c:-2
StructWithBitfields: a:0, b:0, c:1
StructWithBitfields: a:0, b:0, c:0

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

Start with the bindgen invocation using wrapper.h and the StructWithBitfields declaration, then reproduce the signed c getter behavior with the Rust main source and static/bitfields.c. Compare the generated Rust getter and setter behavior against the C walk output; done means signed two-bit values produce the expected results for both positive overflow and negative inputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.