Skip to content

Incorrect use of reinterpret #38

@yuyichao

Description

@yuyichao

The corrent code contains invalid use of reinterpret to cast an array of less alignment to an array of larger alignment causing test failure after JuliaLang/julia#21831

reinterpret: Error During Test
  Test threw an exception of type ArgumentError
  Expression: size(reinterpret(UInt32, rand(BGRA{N0f8}, 5, 5))) == (5, 5)
  ArgumentError: reinterpret from alignment 1 to alignment 4 not allowed
  Stacktrace:
   [1] reinterpret(::Type{UInt32}, ::Array{ColorTypes.BGRA{FixedPointNumbers.Normed{UInt8,8}},2}, ::Tuple{Int64,Int64}) at ./array.jl:153
   [2] reinterpret at /home/yuyichao/.julia/v0.7/ImageCore/src/convert_reinterpret.jl:28 [inlined]
   [3] macro expansion at /home/yuyichao/projects/contrib/ImageCore.jl/test/convert_reinterpret.jl:129 [inlined]
   [4] macro expansion at ./test.jl:856 [inlined]
   [5] anonymous at ./<missing>:?
reinterpret: Error During Test
  Test threw an exception of type ArgumentError
  Expression: reinterpret(UInt32, a) == UInt32[0xf0884422, 0x04030201]
  ArgumentError: reinterpret from alignment 1 to alignment 4 not allowed
  Stacktrace:
   [1] reinterpret(::Type{UInt32}, ::Array{ColorTypes.BGRA{FixedPointNumbers.Normed{UInt8,8}},1}, ::Tuple{Int64}) at ./array.jl:153
   [2] reinterpret(::Type{UInt32}, ::Array{ColorTypes.BGRA{FixedPointNumbers.Normed{UInt8,8}},1}) at /home/yuyichao/.julia/v0.7/ImageCore/src/convert_reinterpret.jl:24
   [3] macro expansion at /home/yuyichao/projects/contrib/ImageCore.jl/test/convert_reinterpret.jl:133 [inlined]
   [4] macro expansion at ./test.jl:856 [inlined]
   [5] anonymous at ./<missing>:?
Test Summary: | Pass  Error  Total
reinterpret   |  125      2    127
ERROR: LoadError: LoadError: Some tests did not pass: 125 passed, 0 failed, 2 errored, 0 broken.
while loading /home/yuyichao/projects/contrib/ImageCore.jl/test/convert_reinterpret.jl, in expression starting on line 4
while loading /home/yuyichao/projects/contrib/ImageCore.jl/test/runtests.jl, in expression starting on line 11

I'm not sure what is the right fix here. If this is a public interface that should be maintained, the reinterpret needs to be done differently. The load cannot be a getindex(::Array), which must have the right alignment, but needs to be constructed from smaller loads or use unaligned load i.e. unsafe_load(::Ptr). I put a UnalignedVector implementation in CRC.jl that is barely good enough for the private usecase in that package but should demonstrate the right way to deal with the alignment issue. I imagine the Array experts here should be able to implement a better interface/more complete array from there if necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions