-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Restrict unsafe blocks to single statements where possible.
#15806
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
base: main
Are you sure you want to change the base?
Changes from all commits
c271174
3e58fda
7fd5489
72033f6
19d5a1f
3d9fe91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,9 +13,10 @@ | |||||
| use anyhow::Error; | ||||||
| use hashbrown::HashMap; | ||||||
| use num_complex::Complex64; | ||||||
| use smallvec::smallvec; | ||||||
| use smallvec::SmallVec; | ||||||
|
|
||||||
| use crate::exit_codes::ExitCode; | ||||||
| use crate::transpiler::target::parse_params; | ||||||
| use qiskit_circuit::bit::{ClassicalRegister, QuantumRegister}; | ||||||
| use qiskit_circuit::circuit_data::CircuitData; | ||||||
| use qiskit_circuit::dag_circuit::{DAGCircuit, NodeIndex, NodeType}; | ||||||
|
|
@@ -503,76 +504,39 @@ pub unsafe extern "C" fn qk_dag_apply_gate( | |||||
| ) -> u32 { | ||||||
| // SAFETY: Per documentation, the pointer is to valid data. | ||||||
| let dag = unsafe { mut_ptr_as_ref(dag) }; | ||||||
| // SAFETY: Per the documentation the qubits and params pointers are arrays of num_qubits() | ||||||
| // and num_params() elements respectively. | ||||||
| unsafe { | ||||||
| let qargs: &[Qubit] = match gate.num_qubits() { | ||||||
| 0 => &[], | ||||||
| 1 => &[Qubit(*qubits.wrapping_add(0))], | ||||||
| 2 => &[ | ||||||
| Qubit(*qubits.wrapping_add(0)), | ||||||
| Qubit(*qubits.wrapping_add(1)), | ||||||
| ], | ||||||
| 3 => &[ | ||||||
| Qubit(*qubits.wrapping_add(0)), | ||||||
| Qubit(*qubits.wrapping_add(1)), | ||||||
| Qubit(*qubits.wrapping_add(2)), | ||||||
| ], | ||||||
| 4 => &[ | ||||||
| Qubit(*qubits.wrapping_add(0)), | ||||||
| Qubit(*qubits.wrapping_add(1)), | ||||||
| Qubit(*qubits.wrapping_add(2)), | ||||||
| Qubit(*qubits.wrapping_add(3)), | ||||||
| ], | ||||||
| // There are no ``QkGate``s > 4 qubits | ||||||
| _ => panic!(), | ||||||
| }; | ||||||
| let params = match gate.num_params() { | ||||||
| 0 => None, | ||||||
| 1 => Some(smallvec![(*params.wrapping_add(0)).into()]), | ||||||
| 2 => Some(smallvec![ | ||||||
| (*params.wrapping_add(0)).into(), | ||||||
| (*params.wrapping_add(1)).into(), | ||||||
| ]), | ||||||
| 3 => Some(smallvec![ | ||||||
| (*params.wrapping_add(0)).into(), | ||||||
| (*params.wrapping_add(1)).into(), | ||||||
| (*params.wrapping_add(2)).into(), | ||||||
| ]), | ||||||
| 4 => Some(smallvec![ | ||||||
| (*params.wrapping_add(0)).into(), | ||||||
| (*params.wrapping_add(1)).into(), | ||||||
| (*params.wrapping_add(2)).into(), | ||||||
| (*params.wrapping_add(3)).into(), | ||||||
| ]), | ||||||
| // There are no ``QkGate``s that take > 4 params | ||||||
| _ => panic!(), | ||||||
| }; | ||||||
| let new_node = if front { | ||||||
| dag.apply_operation_front( | ||||||
| gate.into(), | ||||||
| qargs, | ||||||
| &[], | ||||||
| params.map(Parameters::Params), | ||||||
| None, | ||||||
| #[cfg(feature = "cache_pygates")] | ||||||
| None, | ||||||
| ) | ||||||
| .unwrap() | ||||||
| } else { | ||||||
| dag.apply_operation_back( | ||||||
| gate.into(), | ||||||
| qargs, | ||||||
| &[], | ||||||
| params.map(Parameters::Params), | ||||||
| None, | ||||||
| #[cfg(feature = "cache_pygates")] | ||||||
| None, | ||||||
| ) | ||||||
| .unwrap() | ||||||
| }; | ||||||
| new_node.index() as u32 | ||||||
| } | ||||||
| // SAFETY: Per the documentation the qubits pointer is an arrays of num_qubits() elements | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| let qargs: &[Qubit] = | ||||||
| unsafe { ::std::slice::from_raw_parts(qubits as *const Qubit, gate.num_qubits() as usize) }; | ||||||
| let params: Option<SmallVec<[Param; 3]>> = if gate.num_params() == 0 { | ||||||
| None | ||||||
| } else { | ||||||
| // SAFETY: Per the documentation, params is readable for num_params elements of f64 | ||||||
| Some(unsafe { parse_params(gate, params) }) | ||||||
| }; | ||||||
| let new_node = if front { | ||||||
| dag.apply_operation_front( | ||||||
| gate.into(), | ||||||
| qargs, | ||||||
| &[], | ||||||
| params.map(Parameters::Params), | ||||||
| None, | ||||||
| #[cfg(feature = "cache_pygates")] | ||||||
| None, | ||||||
| ) | ||||||
| .unwrap() | ||||||
| } else { | ||||||
| dag.apply_operation_back( | ||||||
| gate.into(), | ||||||
| qargs, | ||||||
| &[], | ||||||
| params.map(Parameters::Params), | ||||||
| None, | ||||||
| #[cfg(feature = "cache_pygates")] | ||||||
| None, | ||||||
| ) | ||||||
| .unwrap() | ||||||
| }; | ||||||
| new_node.index() as u32 | ||||||
| } | ||||||
|
|
||||||
| /// @ingroup QkDag | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,7 +69,8 @@ pub struct CNeighbors { | |
| #[unsafe(no_mangle)] | ||
| pub unsafe extern "C" fn qk_neighbors_is_all_to_all(neighbors: *const CNeighbors) -> bool { | ||
| // SAFETY: per documentation, `neighbors` points to a valid initialized `CNeighbors`. | ||
| unsafe { (*neighbors).neighbors.is_null() && (*neighbors).partition.is_null() } | ||
| let neighbors = unsafe { const_ptr_as_ref(neighbors) }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this change the requirements for
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm.. isn't that an oversight from before, though? I thought that the pointer needed to be aligned to be dereferenced safely. |
||
| neighbors.neighbors.is_null() && neighbors.partition.is_null() | ||
| } | ||
|
|
||
| /// @ingroup QkNeighbors | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ pub unsafe extern "C" fn qk_transpiler_standalone_optimize_1q_sequences( | |
| circuit: *mut CircuitData, | ||
| target: *const Target, | ||
| ) { | ||
| // SAFETY: Per documentation, the pointers are valid and non-null. | ||
| unsafe { qk_transpiler_pass_standalone_optimize_1q_sequences(circuit, target) }; | ||
| } | ||
|
|
||
|
|
@@ -63,13 +64,11 @@ pub unsafe extern "C" fn qk_transpiler_pass_standalone_optimize_1q_sequences( | |
| circuit: *mut CircuitData, | ||
| target: *const Target, | ||
| ) { | ||
| // SAFETY: Per documentation, the pointer is non-null and aligned. | ||
| let target = unsafe { | ||
| if target.is_null() { | ||
| None | ||
| } else { | ||
| Some(const_ptr_as_ref(target)) | ||
| } | ||
| let target = if target.is_null() { | ||
| None | ||
| } else { | ||
| // SAFETY: Per documentation, the pointer is non-null and aligned. | ||
| Some(unsafe { const_ptr_as_ref(target) }) | ||
| }; | ||
| // SAFETY: Per documentation, the pointer is non-null and aligned. | ||
| let circuit = unsafe { mut_ptr_as_ref(circuit) }; | ||
|
|
@@ -146,12 +145,10 @@ pub unsafe extern "C" fn qk_transpiler_pass_optimize_1q_sequences( | |
| target: *const Target, | ||
| ) { | ||
| // SAFETY: Per documentation, the pointer is non-null and aligned. | ||
| let target = unsafe { | ||
| if target.is_null() { | ||
| None | ||
| } else { | ||
| Some(const_ptr_as_ref(target)) | ||
| } | ||
| let target = if target.is_null() { | ||
| None | ||
| } else { | ||
| Some(unsafe { const_ptr_as_ref(target) }) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the other places you moved the safety comment inside the else body to be with the reduced scope unsafe calls. |
||
| }; | ||
| // SAFETY: Per documentation, the pointer is non-null and aligned. | ||
| let dag = unsafe { mut_ptr_as_ref(dag) }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this causing a memory leak now because we're no longer dropping the existing string pointer? Don't we need rust to take ownership of the string again so it frees the allocation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this looks like a merge gone wrong, I didn't mean to remove that... thanks for catching that!