Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning non-standard sized packed struct from a function can lead to data corruption #6561

Closed
frmdstryr opened this issue Oct 5, 2020 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@frmdstryr
Copy link
Contributor

In the following code, the "State" struct (with three u12 fields) becomes invalid if it's defined using a function:

const std = @import("std");

pub const CtrlRegister = packed struct {
    ENBL: bool,
    RDIR: bool,
    RSTEP: bool, // Cleared after write
    MODE: u4,
    EXSTALL: bool,
    ISGAIN: u2,
    DTIME: u2,
};

pub const TorqueRegister = packed struct {
    TORQUE: u8,
    SMPLTH: u3,
    reserved: u1,
};

pub const OffRegister = packed struct {
    TOFF: u8,
    PWMMODE: u1,
    reserved: u3,
};

// Note: Works if the struct is not packed!
pub const State = packed struct {
    ctrl: CtrlRegister,
    torque: TorqueRegister,
    off: OffRegister,
};

pub fn loadState(a: u16, b: u16, c: u16) State {
    return State{
        .ctrl = @bitCast(CtrlRegister, @truncate(u12, a)),
        .torque = @bitCast(TorqueRegister, @truncate(u12, b)),
        .off = @bitCast(TorqueRegister, @truncate(u12, c)),
    };
}

pub export fn main() void {
    const a = @truncate(u12, 0x8C10);
    const b = @truncate(u12, 0x01FF);
    const c = @truncate(u12, 0x0030);
    
    // This works fine
    const x = State{
        .ctrl = @bitCast(CtrlRegister, @truncate(u12, a)),
        .torque = @bitCast(TorqueRegister, @truncate(u12, b)),
        .off = @bitCast(TorqueRegister, @truncate(u12, c)),
    };
    std.debug.warn("{}\n", .{x});    

    // When I load in a fn.. 
    const s  = loadState(a, b, c);
    
    // The ctrl resgister is now off!!!!
    std.debug.warn("{}\n", .{s});    

}

https://godbolt.org/z/5hvjvz

After some messing around I found that if I change State to not be a packed struct it works (which is fine for my use case). Just wanted to report it.

// This is correct
State{ .ctrl = CtrlRegister{ .ENBL = false, .RDIR = false, .RSTEP = false, .MODE = 2, .EXSTALL = false, .ISGAIN = 0, .DTIME = 3 }, .torque = TorqueRegister{ .TORQUE = 255, .SMPLTH = 1, .reserved = 0 }, .off = OffRegister{ .TOFF = 48, .PWMMODE = 0, .reserved = 0 } }

// This is not!!!
State{ .ctrl = CtrlRegister{ .ENBL = false, .RDIR = false, .RSTEP = false, .MODE = 6, .EXSTALL = false, .ISGAIN = 0, .DTIME = 0 }, .torque = TorqueRegister{ .TORQUE = 0, .SMPLTH = 2, .reserved = 0 }, .off = OffRegister{ .TOFF = 0, .PWMMODE = 0, .reserved = 0 } }

@fengb
Copy link
Contributor

fengb commented Oct 5, 2020

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Oct 13, 2020
@andrewrk andrewrk added this to the 0.8.0 milestone Oct 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@Vexu
Copy link
Member

Vexu commented Mar 28, 2024

Fixed in master.

@Vexu Vexu closed this as completed Mar 28, 2024
@Vexu Vexu modified the milestones: 0.15.0, 0.12.0 Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

4 participants