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

Fix out of bounds array access. #1167

Merged
merged 1 commit into from
Mar 2, 2021
Merged

Conversation

vvilpas
Copy link
Contributor

@vvilpas vvilpas commented Mar 1, 2021

Summary

Fixes a bug in CopyIn/CopyOut operations where out of bounds array elemets
were accessed.
QubitvectorThrust has a number of qubits num_qubits. When move_to_vector or
copy_to_vector functions are called a temporary array or vector of size
(1 << num_qubits) is created and passed to chunk_ CopyOut function. There, a number
of elements (1 << chunk_qubits_) of the underlying structure are copied to
this array/vector. The problem is that chunk_qubits_ can be larger that num_qubits,
accessing values in the array or vector out of bounds. This is what was making
PR #1132 failing in CI.

Details and comments

chunk_qubits_ is initialized from maximum number of qubits in a list of circuit whereas num_qubits is initilaized as the number of qubits of a given circuit.

Fixes a bug in CopyIn/CopyOut operations where out of bounds array elemets
were accessed.
Qubitvector thrust has a number of qubits num_qubits. When move_to_vector or
copy_to_vector functions are called a temporary array or vector of size
(1 << num_qubits) is created and passed to chunk_ CopyOut function. There, a number
of elements (1 << chunk_qubits_) of the underlying structure are copied to
this array/vector. The problem is that chunk_qubits can be larger that num_qubits,
accessing values in the array or vector out of bounds. This is what was making
PR Qiskit#1132 failing in CI.
@vvilpas
Copy link
Contributor Author

vvilpas commented Mar 1, 2021

@doichanj This a patch to fix temporarily the issue, as I got a bit lost in the structure of chunks, etc. Could you take a look to check if you see a better way to solve it?

@doichanj
Copy link
Collaborator

doichanj commented Mar 2, 2021

I agree with checking size to be copied.
However the problem of chunk bits should be also fixed and this problem is already fixed in PR #1149 (424ae67)

@chriseclectic
Copy link
Member

I accidentally merged this one when I meant to merge #1168. Should I revert or is it ok as is? @doichanj

@vvilpas
Copy link
Contributor Author

vvilpas commented Mar 2, 2021

I accidentally merged this one when I meant to merge #1168. Should I revert or is it ok as is? @doichanj

Both are needed. This one fixes a possible out of bounds write in the array. That was causing the memory corruption we saw in Merav's PR. The other one fixes a memory leak, some memory was not freed when it should have been. It doesn't cause wrong behavior but after some time it could let the program without memory.

@doichanj
Copy link
Collaborator

doichanj commented Mar 3, 2021

I agree with @vvilpas, and I think there is no problem to merge both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants