Skip to content

Bugfix: Use Base.aligned_sizeof instead of sizeof in Mmap.mmap #58998

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JonasIsensee
Copy link
Contributor

@JonasIsensee JonasIsensee commented Jul 14, 2025

The purpose of this PR is to fix #58982 .

The current (bugged) behaviour is:

julia> primitive type MyPrim9 9*8 end

julia> sizeof(MyPrim9)
9

julia> Base.aligned_sizeof(MyPrim9)
16


julia> arr = Vector{MyPrim9}(undef, 2)
2-element Vector{MyPrim9}:
 MyPrim9(0x700000000000000002)
 MyPrim9(0xd000007fb7705a2c30)

julia> write("file", arr)
32

julia> using Mmap

julia> mmap("file", Vector{MyPrim9})
3-element Vector{MyPrim9}:
 MyPrim9(0x700000000000000002)
 MyPrim9(0xd000007fb7705a2c30)
 MyPrim9(0x000000000000000000)

This is because Mmap erroneously uses sizeof instead of Base.aligned_sizeof to compute the number of elements (32 ÷ 9 == 3).
This has gone undetected for a long time because sizeof(T) == Base.aligned_sizeof(T) is true for most types including structs. (Padding is to a multiple of 8 bytes is included in sizeof(T))

closes #58982

edit:
Previously, sizeof would throw an error exception when presented with a non-mappable type such as Ref.
This occurred within the default argument generation. Base.aligned_sizeof does not error here, and this is instead caught in the dedicated isbits check.
This seems desirable and I changed the test to require an ArgumentError.

@oscardssmith oscardssmith added the bugfix This change fixes an existing bug label Jul 14, 2025
@giordano giordano added backport 1.10 Change should be backported to the 1.10 release backport 1.11 Change should be backported to release-1.11 backport 1.12 Change should be backported to release-1.12 labels Jul 14, 2025
@JeffBezanson
Copy link
Member

Very nice, thank you for finding and fixing this.

@@ -343,6 +343,18 @@ end
GC.gc()
rm(file)

# test for #58982
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be put into a @testset to make failures here more localized?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can wrap it into a testset.
I just tried to match the (oldish) style of the test file as closely as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 1.10 Change should be backported to the 1.10 release backport 1.11 Change should be backported to release-1.11 backport 1.12 Change should be backported to release-1.12 bugfix This change fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Silently wrong Mmap of primitive types
5 participants