Open JamesWrigley opened 2 weeks ago
The problem is coming from codegen where we just use the field offset from Clang, which presumably returns the field offset on the host system:
Does this happen when a multiplatform setup is used? e.g. https://github.com/JuliaGPU/VulkanCore.jl/tree/master/lib
Ah hah, no it doesn't :) Maybe we should warn users that if they're not generating code for all targets they must at least generate a 64bit and 32bit target?
Yes, we should issue a warning whenever _emit_getproperty_ptr!
is called. However, we only provide two options: generating a single target(unsafe) or generating all targets(extremely safe) that Julia currently runs on.
I came across a bug when testing some code on 32bit CI:
c_attrs
is a struct that has beenunsafe_load()
'd from a pointer.c_attrs.owner
is a pointer field ofc_attrs
, andx
is the result of callingunsafe_load(getproperty(::Ptr{sftp_attributes_struct}, :owner))
. The problem is thatc_attrs.owner
andx
do not have the same value, and I believe this is because ourgetproperty()
implementations don't take into account the fact thatsizeof(Ptr)
is different on 32bit/64bit platforms.This is the struct definition generated by Clang.jl:
Code
```julia mutable struct sftp_attributes_struct name::Ptr{Cchar} longname::Ptr{Cchar} flags::UInt32 type::UInt8 size::UInt64 uid::UInt32 gid::UInt32 owner::Ptr{Cchar} group::Ptr{Cchar} permissions::UInt32 atime64::UInt64 atime::UInt32 atime_nseconds::UInt32 createtime::UInt64 createtime_nseconds::UInt32 mtime64::UInt64 mtime::UInt32 mtime_nseconds::UInt32 acl::ssh_string extended_count::UInt32 extended_type::ssh_string extended_data::ssh_string end function Base.getproperty(x::Ptr{sftp_attributes_struct}, f::Symbol) f === :name && return Ptr{Ptr{Cchar}}(x + 0) f === :longname && return Ptr{Ptr{Cchar}}(x + 8) f === :flags && return Ptr{UInt32}(x + 16) f === :type && return Ptr{UInt8}(x + 20) f === :size && return Ptr{UInt64}(x + 24) f === :uid && return Ptr{UInt32}(x + 32) f === :gid && return Ptr{UInt32}(x + 36) f === :owner && return Ptr{Ptr{Cchar}}(x + 40) f === :group && return Ptr{Ptr{Cchar}}(x + 48) f === :permissions && return Ptr{UInt32}(x + 56) f === :atime64 && return Ptr{UInt64}(x + 64) f === :atime && return Ptr{UInt32}(x + 72) f === :atime_nseconds && return Ptr{UInt32}(x + 76) f === :createtime && return Ptr{UInt64}(x + 80) f === :createtime_nseconds && return Ptr{UInt32}(x + 88) f === :mtime64 && return Ptr{UInt64}(x + 96) f === :mtime && return Ptr{UInt32}(x + 104) f === :mtime_nseconds && return Ptr{UInt32}(x + 108) f === :acl && return Ptr{ssh_string}(x + 112) f === :extended_count && return Ptr{UInt32}(x + 120) f === :extended_type && return Ptr{ssh_string}(x + 128) f === :extended_data && return Ptr{ssh_string}(x + 136) return getfield(x, f) end function Base.setproperty!(x::Ptr{sftp_attributes_struct}, f::Symbol, v) unsafe_store!(getproperty(x, f), v) end ```The first field is a pointer and the offset for the second field is hardcoded to 8 bytes, but that's only valid on 64bit systems (which the bindings were generated on). The problem is coming from codegen where we just use the field offset from Clang, which presumably returns the field offset on the host system: https://github.com/JuliaInterop/Clang.jl/blob/8ed2dd214fd89748b92a13b656b5f00b3fd71a18/src/generator/codegen.jl#L311-L320
It's slightly annoying to fix since we'll need to keep track of how many pointer fields there are and take into account all of their offsets. But I'm guessing not many people use 32bit machines anymore, especially in scientific computing, so this probably isn't very high-priority.