r/Zig • • 10d ago

Is this undefined behaviour?

Using ptrCast to get an unsigned pointer to a signed variable with a bitsize that's not a power of 2 shows nonsensical data.

Reproducible example (in debug):

const std = @import("std");

test "reading an i30 through a u30 pointer" {
    var x: i30 = -1;
    const view: *u30 = @ptrCast(&x);

    std.debug.print("bytes of x: {x}\n", .{std.mem.asBytes(&x)});
    try std.testing.expectEqual(std.math.maxInt(u30), view.*);
}

output:
bytes of x: ffffffff
expected 1073741823, found 4294967295

Somehow view.* is larger than the maximum value u30 should be able to hold. It is actually the u32 max value.

edit: I tried using an LLM to investigate this and the cause is actually very interesting.

The issue has to do with the padding bits. A signed write to a bit size with padding bits sets every padding bit to the sign bit. That's the case because it makes reads into something like an i30 very simple, you just load the four bytes and read them like you would an i32 without any adjustments (in two's complement negative numbers essentially count "down" from negative one which is 111..111 so adding additional 1 bits to a negative number is the same as adding additional 0 bits to a positive number).

Unsigned writes always write 0 into the padding bits for the same reason, it lets you read the full bytes with no masking and get the same values.

The problem is that this mechanism breaks when the type that was assumed when writing into memory isn't the one that's assumed when reading from it. The padding bits still don't get masked out and you get 32 bit data that doesn't make sense for 30 logical bits.

This edit is mainly me trying to explain this to myself. Sorry if the terminology is wrong, I don't have any formal CS education (though I should be starting my CS degree at October of next year).

24 Upvotes

6 comments sorted by

10

u/johan__A 10d ago edited 9d ago

this might be a bug with the self hosted backend, you can try -fllvm to force the llvm backend. I think it would not be undefined behaviour but the language doesn’t have a complete specification document yet so I cant say for sure.

4

u/LegenDrags 10d ago

its not a bug (or atleast thats what i infer from what mlugg told me)

zig self hosted backend intentionally does not zero extend arbitrary bit width integers atleast for undefined values, unlike llvm

it might be something similar done here, the backing integer's unused bits are not cleared when dereferencing (imo thatd be poor for performance and unnecessary in most cases, exceptions being things like this where you cast it). so unless they want to change that behavior, its gonna stay this way.

1

u/YuvalAmir 9d ago

I think you are pretty close. My understanding if that it zero extends when writing unsigned integers but extends the sign bit for signed writes. The idea is that with the padding set up like this there is no need to mask out the padding bits, you can just read an i30 as an i32 and with two's complement you get the same values this way.

The issue seems to be that this logic only holds when reading and writing with the same type. The u30 pointer tries to read the entire four bytes without any masking because it makes the assumption that bits 31 and 32 are zeros and there is no need to mask them out. That's why the -1 read gives the u32 max int instead of the u30 one.

I added an edit to the post with this.

4

u/LegenDrags 10d ago

actually i misread, i did not notice that in the question both types are 30bit so that shouldnt happen, my bad

1

u/YuvalAmir 9d ago edited 9d ago

It works for the debug build, but you can get a similar issue in reverse on release fast like this:

const std = ("std");

noinline fn writeMinusOne(x: *i30) void {
    const view: *u30 = (x);
    view.* = @bitCast(@as(i30, -1));
}

test "i30 written through a u30 pointer" {
    var x: i30 = 0;
    writeMinusOne(&x);

    try std.testing.expect(x < 0);
}

I added an edit to the post btw

0

u/TheSodesa 9d ago

You cannot have undefined behaviour if your language does not have a standard.