Skip to content

Conversation

@dlech
Copy link
Member

@dlech dlech commented Jul 19, 2025

Fix strict aliasing issues between block drivers and pbsys storage.

Casting a uint8_t* to a struct pointer violates strict aliasing rules, which can lead to hard-to-find bugs. We need to ensure that the memory being pointed to has the same alignment, etc. as the struct it is being cast to. The usual way to do this is to use a union of the struct and the other type we want to use (in this case, uint8_t array).

To do this, we need to move the storage data struct definition to a header file that can be included by both pbdrv/block_device and pbsys/storage.

Additionally, in block_device_test, the size of the ramdisk block was wrong and was causing the NXT to crash sometime after starting the REPL. It also incorrectly had the .noinit section attribute, but depends on being initialized to zero, so that is removed.

Fixes: pybricks/support#2272
Fixes: ce4253a ("pbio/sys: Move block device read to driver level.")

Fix strict aliasing issues between block drivers and pbsys storage.

Casting a uint8_t* to a struct pointer violates strict aliasing rules,
which can lead to hard-to-find bugs. We need to ensure that the memory
being pointed to has the same alignment, etc. as the struct it is being
cast to. The usual way to do this is to use a union of the struct and
the other type we want to use (in this case, uint8_t array).

To do this, we need to move the storage data struct definition to a
header file that can be included by both pbdrv/block_device and
pbsys/storage.

Additionally, in block_device_test, the size of the ramdisk block was
wrong and was causing the NXT to crash sometime after starting the REPL.
It also incorrectly had the .noinit section attribute, but depends on
being initialized to zero, so that is removed.

Fixes: pybricks/support#2272
Fixes: ce4253a ("pbio/sys: Move block device read to driver level.")
@dlech dlech force-pushed the fix-block-device-test-nxt branch from fbfca4c to 5023437 Compare July 19, 2025 22:24
@coveralls
Copy link

coveralls commented Jul 19, 2025

Coverage Status

coverage: 57.481% (+0.02%) from 57.466%
when pulling 5023437 on dlech:fix-block-device-test-nxt
into 96cdd12 on pybricks:master.

@dlech dlech merged commit fdb0e68 into pybricks:master Jul 19, 2025
17 checks passed
@dlech dlech deleted the fix-block-device-test-nxt branch July 19, 2025 22:30
@laurensvalk
Copy link
Member

laurensvalk commented Jul 20, 2025

Could we leave a little more time to review, please?

Is there a way to do this without including pbsys from pbdrv?

@BertLindeman
Copy link
Contributor

This fix helps to have a more stable REPL on the NXT, but the EV3 now suffers something like issue 2272

Went back too github hash 96cdd12 to get a usable REPL.

@dlech
Copy link
Member Author

dlech commented Jul 20, 2025

Is there a way to do this without including pbsys from pbdrv?

We could, but we would lose the strict safety that this provides.

@dlech
Copy link
Member Author

dlech commented Jul 20, 2025

Maybe it works to move the union to the pbsys side of things? We would probably need to change the array type from uint8_t to uint32_t though to get the right alignment in the block drivers. And when we have multiple slots, we need to make sure each slot is 4-byte aligned as well. I'll see if I can come up with something.

@laurensvalk
Copy link
Member

Fwiw, we might be dropping slots at the RAM level, so if these overcomplicate things you could skip this aspect of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] NXT REPL hangs or craches after a command is entered

4 participants