Skip to content
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

prevent conj! into uninitialized memory #40481

Merged
merged 1 commit into from
Apr 15, 2021
Merged

Conversation

KristofferC
Copy link
Member

@KristofferC KristofferC commented Apr 14, 2021

From my understanding, the current matmul code loads some buffer memory and "casts" it to an array of the element type:

Btile = unsafe_wrap(Array, convert(Ptr{S}, pointer(Bbuf[Threads.threadid()])), sz)

stores some data into a part of that buffer:

copyto!(Btile, 1:mB, 1:nB, tB, B, 1:mB, 1:nB)

, but then goes on and conj! the full memory:

tM == 'C' && conj!(B)

Since this code runs for user defined types calling conj on uninitialized memory can have side effects (like throwing). I think this is what causes the errors in https://s3.amazonaws.com/julialang-reports/nanosoldier/pkgeval/by_hash/f2bc4d3_vs_9283b6c/NiceNumbers.1.6.1-pre-d190257cce.log. Tests passes with this PR.

Im not 100% sure my analysis here is correct so some more eyes on it would be nice.

@KristofferC
Copy link
Member Author

KristofferC commented Apr 14, 2021

This seems to come from f597671 (9 years ago!),

conj!(B)

so @timholy, any comments? :P

@KristofferC KristofferC added the backport 1.6 Change should be backported to release-1.6 label Apr 14, 2021
@mbauman mbauman added the linear algebra Linear algebra label Apr 14, 2021
Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

Nice catch! Seems correct to me. Not sure what performance implications might be. Thanks Kristoffer! :)

@mbauman
Copy link
Member

mbauman commented Apr 14, 2021

Yep, seems right to me. We limit this buffer use to bitstypes, and most bitstypes will happily conj over their entire possible bitsrepresentation domain unless someone gets real unlucky with a typemin(Int) value in a rational... or you have a fancier datatype that needs to do more computation on conj/construction like NiceNumbers. Simple REPL testing:

julia> struct BrokenInt <: Number
           i::Int
       end

julia> Base.:*(::BrokenInt, ::BrokenInt) = BrokenInt(42)

julia> Base.:+(::BrokenInt, ::BrokenInt) = BrokenInt(42)

julia> Base.zero(::BrokenInt) = BrokenInt(0)

julia> Base.conj(b::BrokenInt) = b.i == 42 ? b : error()

julia> fill(BrokenInt(42), 10,100)' * fill(BrokenInt(42), 100,10)'
ERROR:
Stacktrace:
  [1] error()
    @ Base ./error.jl:42
  [2] conj
    @ ./REPL[5]:1 [inlined]

Your patch fixes it.

@KristofferC
Copy link
Member Author

I'll add that as a test!

@KristofferC
Copy link
Member Author

The test caused some ambiguities so removed it for now. I'll open another PR with it.

@KristofferC KristofferC merged commit aa7d879 into master Apr 15, 2021
@KristofferC KristofferC deleted the kc/fix_matmul_uninit branch April 15, 2021 07:59
KristofferC added a commit that referenced this pull request Apr 15, 2021
@fkastner
Copy link
Contributor

Thanks!
This is the conclusion I came to, too: #39708.
Now I can close that issue.

@KristofferC
Copy link
Member Author

Oops, totally missed that issue.

@KristofferC KristofferC removed the backport 1.6 Change should be backported to release-1.6 label May 4, 2021
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this pull request May 4, 2021
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this pull request May 9, 2021
johanmon pushed a commit to johanmon/julia that referenced this pull request Jul 5, 2021
staticfloat pushed a commit that referenced this pull request Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants