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

wrong value loaded from sub-byte-aligned pointer to global variable #12363

Closed
andrewrk opened this issue Aug 8, 2022 · 0 comments · Fixed by #13168
Closed

wrong value loaded from sub-byte-aligned pointer to global variable #12363

andrewrk opened this issue Aug 8, 2022 · 0 comments · Fixed by #13168
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. miscompilation The compiler reports success but produces semantically incorrect code.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Aug 8, 2022

Zig Version: 0.10.0-dev.3477+e28567538

Passes for stage1, fails for stage3.

const std = @import("std");
const expect = std.testing.expect;

const BitField = packed struct {
    a: u3,
    b: u3,
    c: u2,
};

var foo = BitField{
    .a = 1,
    .b = 2,
    .c = 3,
};

test "pointer to non-byte-aligned field" {
    const ptr = &foo.b;
    try expect(ptr.* == 2);
}

Similar test that should also pass to close this issue:

const std = @import("std");
const expect = std.testing.expect;

const BitField = packed struct {
    a: u3,
    b: u3,
    c: u2,
};

var bit_field = BitField{
    .a = 1,
    .b = 2,
    .c = 3,
};

test "sub-byte-aligned fields share the same address" {
    try expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.b));
    try expect(@ptrToInt(&bit_field.a) == @ptrToInt(&bit_field.c));
}

Before closing this issue, be sure to remove {#backend_stage1#} from these two places in the language reference:

  • todo link here
  • todo link here
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Aug 8, 2022
@andrewrk andrewrk added this to the 0.10.0 milestone Aug 8, 2022
jacobly0 added a commit to jacobly0/zig that referenced this issue Oct 15, 2022
 * When a field starts at some bit offset within a byte you need to load
   starting from that byte and shift, not starting from the next byte,
   so a rounded-down divide is required here, not a rounded-up one.
 * Remove paragraph from doc that no longer relates to anything.

Closes ziglang#12363
@andrewrk andrewrk added the miscompilation The compiler reports success but produces semantically incorrect code. label Oct 15, 2022
andrewrk pushed a commit that referenced this issue Oct 15, 2022
 * When a field starts at some bit offset within a byte you need to load
   starting from that byte and shift, not starting from the next byte,
   so a rounded-down divide is required here, not a rounded-up one.
 * Remove paragraph from doc that no longer relates to anything.

Closes #12363
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 frontend Tokenization, parsing, AstGen, Sema, and Liveness. miscompilation The compiler reports success but produces semantically incorrect code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant