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

Out of bounds write in sparse broadcast #31758

Closed
fredrikekre opened this issue Apr 18, 2019 · 0 comments
Closed

Out of bounds write in sparse broadcast #31758

fredrikekre opened this issue Apr 18, 2019 · 0 comments
Labels
broadcast Applying a function over a collection bug Indicates an unexpected problem or unintended behavior sparse Sparse arrays

Comments

@fredrikekre
Copy link
Member

Reduced from https://discourse.julialang.org/t/signal-11-segmentation-fault-11-with-differentialequations-jl/23264/:

y = sparsevec([2,7], [1., 2.], 10)
x1 = sparsevec(fill(1.0, 10))
x2 = sparsevec([2,7], [1., 2.], 10)
x3 = sparsevec(fill(1.0, 10))
f(x, y, z) = x == y == z == 0 ? 0.0 : NaN
bc = Broadcast.broadcasted(f, x1, x2, x3)

Broadcast.materialize!(y, bc)

where the last line causes a segfault (BoundsError with --check-bounds=yes). The problem is here:

Ck > spaceC && (spaceC = expandstorage!(C, Ck + min(length(C), _sumnnzs(As...)) - (sum(ks) - N)))
where we fail to expand the storage since the calculation for needed storage is wrong, and returns 1 in this particular case. From Debugger:

1|debug> n
In _map_zeropres!(f, C, As) at /julia/stdlib/v1.0/SparseArrays/src/higherorderfns.jl:372
 382  while activerow < rowsentinel
 383      vals, ks, rows = _fusedupdate_all(rowsentinel, activerow, rows, ks, stopks, As)
 384      Cx = f(vals...)
 385      if !_iszero(Cx)
>386          Ck > spaceC && (spaceC = expandstorage!(C, Ck + min(length(C), _sumnnzs(As...)) - (sum(ks) - N)))
 387          storedinds(C)[Ck] = activerow
 388          storedvals(C)[Ck] = Cx
 389          Ck += 1
 390      end

1|debug> w
1] Ck: 7
2] spaceC: 6
3] storedinds(C): [1, 2, 3, 4, 5, 6]
4] (Ck + min(length(C), _sumnnzs(As...))) - (sum(ks) - N): 1

1|debug> n
In _map_zeropres!(f, C, As) at /julia/stdlib/v1.0/SparseArrays/src/higherorderfns.jl:372
 383  vals, ks, rows = _fusedupdate_all(rowsentinel, activerow, rows, ks, stopks, As)
 384  Cx = f(vals...)
 385  if !_iszero(Cx)
 386      Ck > spaceC && (spaceC = expandstorage!(C, Ck + min(length(C), _sumnnzs(As...)) - (sum(ks) - N)))
>387      storedinds(C)[Ck] = activerow
 388      storedvals(C)[Ck] = Cx
 389      Ck += 1
 390  end
 391  activerow = min(rows...)

1|debug> w
1] Ck: 7
2] spaceC: 1                # <-- What??
3] storedinds(C): [1, 2, 3, 4, 5, 6]
4] (Ck + min(length(C), _sumnnzs(As...))) - (sum(ks) - N): 1
@fredrikekre fredrikekre added bug Indicates an unexpected problem or unintended behavior sparse Sparse arrays broadcast Applying a function over a collection labels Apr 18, 2019
This was referenced Apr 19, 2019
KristofferC pushed a commit that referenced this issue Apr 20, 2019
KristofferC pushed a commit that referenced this issue Apr 20, 2019
KristofferC pushed a commit that referenced this issue Apr 20, 2019
KristofferC pushed a commit that referenced this issue Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broadcast Applying a function over a collection bug Indicates an unexpected problem or unintended behavior sparse Sparse arrays
Projects
None yet
Development

No branches or pull requests

1 participant