#offset access

1 messages · Page 1 of 1 (latest)

spare wing
#

i have a big struct that i will not be able to change other then how i address the data, my problem is that offsets of items are wrong
century offset: 108
iapc_boot_arch_flags offset: 110

    virtio.printf("{any} offset: {any}\n", .{ LocalAcpiTables.fadt.iapc_boot_arch_flags, @offsetOf(acpi.FADT, "century") });
    virtio.printf("{any} offset: {any}\n", .{ LocalAcpiTables.fadt.iapc_boot_arch_flags, @offsetOf(acpi.FADT, "iapc_boot_arch_flags") });

also this is the struct:

pub const FADT align(1) = extern struct {
    h: SDTHeader,
    firmware_control: u32,
    dsdt: u32,

    reserved: u8,

    preferred_pm_profile: u8,
    sci_interrupt: u16,
    smi_command_port: u32,
    acpi_enable: u8,
    acpi_disable: u8,
    s4bios_req: u8,
    pstate_control: u8,

    pm1a_event_block: u32,
    pm1b_event_block: u32,
    pm1a_control_block: u32,
    pm1b_control_block: u32,
    pm2_control_block: u32,
    pm_timer_block: u32,
    gpe0_block: u32,
    gpe1_block: u32,

    pm1_event_length: u8,
    pm1_control_length: u8,
    pm2_control_length: u8,
    pm_timer_length: u8,
    gpe0_length: u8,
    gpe1_length: u8,
    gpe1_base: u8,
    cstate_control: u8,
    worst_c2_latency: u16,
    worst_c3_latency: u16,
    flush_size: u16,
    flush_stride: u16,
    duty_offset: u8,
    duty_width: u8,
    day_alarm: u8,
    month_alarm: u8,
    century: u8,

    iapc_boot_arch_flags: u16,

    reserved2: u8,
    flags: u32,

    reset_reg: genericAddressStructure,

    reset_value: u8,
    reserved3: [3]u8,

    x_firmware_control: u64,
    x_dsdt: u64,

    x_pm1a_event_block: genericAddressStructure,
    x_pm1b_event_block: genericAddressStructure,
    x_pm1a_control_block: genericAddressStructure,
    x_pm1b_control_block: genericAddressStructure,
    x_pm2_control_block: genericAddressStructure,
    x_pm_timer_block: genericAddressStructure,
    x_gpe0_block: genericAddressStructure,
    x_gpe1_block: genericAddressStructure,
};
#

the offset of iapc_boot_arch_flags should be 109

#

but maybe because it is a 16bit i cant address it through an uneven address?

#

do i need to make it into an array?

leaden dust
#

Why do you expect the offset to be 109?

#

Can you expand a little more that

spare wing
#

to check if the 1 bit meaning when i and the byte in that offset it needs to be equal to 2

#

to check hardware

#

that is the specefication

#

¯\_(ツ)_/¯

leaden dust
#

No I mean

#

I don't know why you expect that to be the offset it's at

spare wing
#

i used the @offsetof

leaden dust
#

You mention the century field - do you expect this struct to not have any padding?

spare wing
#

yes?

leaden dust
#

Well you haven't specified that to Zig, is the thing

spare wing
#

do i need to say packed?

leaden dust
#

If you don't want any padding, you must use extern struct, as you are, and then also put name: Type align(1), on every field.

#

packed struct is a C bitfield, so it's a very different thing

#

(Kind of)

#

(It's not actually - it's actually just an integer that you want to act like a struct)

spare wing
#

i used the alighn(1) on the struct is that not good enopugh?

leaden dust
#

That just aligns the struct itself

spare wing
#

ohhhhh

#

that makes sense

leaden dust
#

It's equivalent to putting align(1) on the first field only

#

Though... that's odd. I didn't think that worked

spare wing
#

ok let me check

#

ya it works tyyy

leaden dust
#

I feel like that might actually be aligning the FADT constant in memory itself, which makes no sense because it's a constant and it's a type.

#

If that's true, that might be a missing error

spare wing
#

i think it just makes it so the struct can be on every offset

leaden dust
#

Perhaps @manic hill can speak to this 😄

leaden dust
#

You had to just align the first field instead

spare wing
#

:0

#

then what

#

:0

leaden dust
#

And, var buf: [4096]u8 align(4) = undefined; is a thing

#

And that does align buf

spare wing
#

did i found a bug???? yippe

leaden dust
#

There is an outside possibility that femboys eat bugs 🤣

spare wing
#

:3

#

tyty

leaden dust
spare wing
#

let me check in assolated area

#

like plain zig file

manic hill
#

though even if this is intentional, it still stands that align on the type declaration it's assigned to has nothing to do with the struct definition

leaden dust
#

I think that means that it is a bug(?) 🤣

manic hill
#

well, I wouldn't definitively say it's a bug

leaden dust
#

Like - aligning a constant that only exists at comptime doesn't make much sense

manic hill
#

though it is somewhat confusing behavior

manic hill
#
const foo: u8 align(16) = 0; // only exists at compile time, but if you take its address it will refer to a 16 byte aligned location
#

though one second let me check something

leaden dust
#

I think you agree with me then

#

Maybe only in part(?)

#

It's late lol

spare wing
manic hill
#

alright confirmed: as weird as it is, *type can be *align(n) type, meaning alignment is a property of type values as much as it is any other pointed-to-value, as useless as that is

spare wing
#

~/Downloads zig run test.zig
108
110

leaden dust
spare wing
#

then should it worl like what i thought it would? or just be somewhat useless?

leaden dust
#

No

manic hill
leaden dust
#

Right - I would be inclined to make that an error since you literally never(*) want that.

manic hill
#

types are treated like any other value, which just happens to only exist at comptime

spare wing
#

; - ;

#

it is weird

leaden dust
#

And allowing it makes for the potential of some confusing debugging 😄

manic hill
#

it is a bit odd, yes, wouldn't disagree

tardy ridge
spare wing
#

yippe second bug i found :3

manic hill
leaden dust
manic hill
#

you can pass in a mutable pointer to a type value and mutate it

#

and iiuc alignment is validated for any pointer at compile time

tardy ridge
leaden dust
#

Leads to the interesting question of what the @alignOf a type is.

tardy ridge
#

i got what you're saying

manic hill
#

"fake" addresses, but addressable nontheless

manic hill
#

compile error probably

tardy ridge
#

tis one

leaden dust
#

If it is that's even more weird lol

#

That is interesting

manic hill
#

makes sense

leaden dust
#

I wonder if it increases with the number of types in your program

manic hill
#

lol

spare wing
#

yippe i am proud

leaden dust
#

Less than 255? 1.

#

Less than 65536? 2

manic hill
#

that would be pretty cursed

leaden dust
#

535 even

#

I would not put things past Zig sometimes 😄

manic hill
#

certainly some details to iron out lol

leaden dust
#

Ta for the aid o7

tardy ridge
#

that would be crazy

spare wing
#

who should report it?

#

or u want to descast it more?

#

or move it to other place?

#

or nothing to do?

#

also solution that i found: just align the spesific feild to 1

#

every time i do something in zig i get the most unexpected response i think i attract bugs

#

and weirdness

leaden dust
#

That only removes padding for that field

#

A C packed struct is equivalent to a Zig extern struct with align(1) on all fields

spare wing
#

i will fix it latter

#

:3

spare wing
#

if u can?

manic hill
#

by doing what they said: align(1) on all fields

spare wing
#

that sounds so bad