Skip to content

Commit 3ca19f6

Browse files
committed
Fortify check.
1 parent 4409a51 commit 3ca19f6

File tree

3 files changed

+32
-18
lines changed
  • compiler
    • rustc_codegen_cranelift/src/driver
    • rustc_codegen_ssa/src
    • rustc_query_system/src/dep_graph

3 files changed

+32
-18
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguR
522522
// know that later). If we are not doing LTO, there is only one optimized
523523
// version of each module, so we re-use that.
524524
let dep_node = cgu.codegen_dep_node(tcx);
525-
tcx.dep_graph.assert_nonexistent_node(&dep_node, || {
525+
tcx.dep_graph.assert_dep_node_not_yet_allocated_in_current_session(&dep_node, || {
526526
format!(
527527
"CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
528528
cgu.name()

compiler/rustc_codegen_ssa/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguR
10311031
// know that later). If we are not doing LTO, there is only one optimized
10321032
// version of each module, so we re-use that.
10331033
let dep_node = cgu.codegen_dep_node(tcx);
1034-
tcx.dep_graph.assert_nonexistent_node(&dep_node, || {
1034+
tcx.dep_graph.assert_dep_node_not_yet_allocated_in_current_session(&dep_node, || {
10351035
format!(
10361036
"CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
10371037
cgu.name()

compiler/rustc_query_system/src/dep_graph/graph.rs

+30-16
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl<K: DepKind> DepGraphData<K> {
349349
// in `DepGraph::try_mark_green()`.
350350
// 2. Two distinct query keys get mapped to the same `DepNode`
351351
// (see for example #48923).
352-
self.assert_nonexistent_node(&key, || {
352+
self.assert_dep_node_not_yet_allocated_in_current_session(&key, || {
353353
format!(
354354
"forcing query with already existing `DepNode`\n\
355355
- query-key: {arg:?}\n\
@@ -647,14 +647,19 @@ impl<K: DepKind> DepGraph<K> {
647647
}
648648

649649
impl<K: DepKind> DepGraphData<K> {
650-
fn assert_nonexistent_node<S: std::fmt::Display>(
650+
fn assert_dep_node_not_yet_allocated_in_current_session<S: std::fmt::Display>(
651651
&self,
652652
_dep_node: &DepNode<K>,
653653
_msg: impl FnOnce() -> S,
654654
) {
655655
#[cfg(debug_assertions)]
656-
if let Some(seen_dep_nodes) = &self.current.seen_dep_nodes {
657-
let seen = seen_dep_nodes.lock().contains(_dep_node);
656+
if let Some(prev_index) = self.previous.node_to_index_opt(_dep_node) {
657+
let current = self.current.prev_index_to_index.lock()[prev_index];
658+
assert!(current.is_none(), "{}", _msg())
659+
} else if let Some(nodes_newly_allocated_in_current_session) =
660+
&self.current.nodes_newly_allocated_in_current_session
661+
{
662+
let seen = nodes_newly_allocated_in_current_session.lock().contains(_dep_node);
658663
assert!(!seen, "{}", _msg());
659664
}
660665
}
@@ -871,7 +876,7 @@ impl<K: DepKind> DepGraphData<K> {
871876
let frame = MarkFrame { index: prev_dep_node_index, parent: frame };
872877

873878
#[cfg(not(parallel_compiler))]
874-
self.assert_nonexistent_node(dep_node, || {
879+
self.assert_dep_node_not_yet_allocated_in_current_session(dep_node, || {
875880
format!("trying to mark existing {dep_node:?} as green")
876881
});
877882
#[cfg(not(parallel_compiler))]
@@ -970,15 +975,15 @@ impl<K: DepKind> DepGraph<K> {
970975
self.node_color(dep_node).is_some_and(|c| c.is_green())
971976
}
972977

973-
pub fn assert_nonexistent_node<S: std::fmt::Display>(
978+
pub fn assert_dep_node_not_yet_allocated_in_current_session<S: std::fmt::Display>(
974979
&self,
975980
dep_node: &DepNode<K>,
976981
msg: impl FnOnce() -> S,
977982
) {
978983
if cfg!(debug_assertions)
979984
&& let Some(data) = &self.data
980985
{
981-
data.assert_nonexistent_node(dep_node, msg)
986+
data.assert_dep_node_not_yet_allocated_in_current_session(dep_node, msg)
982987
}
983988
}
984989

@@ -1120,8 +1125,11 @@ pub(super) struct CurrentDepGraph<K: DepKind> {
11201125

11211126
/// Used to verify the absence of hash collisions among DepNodes.
11221127
/// This field is only `Some` if the `-Z incremental_verify_ich` option is present.
1128+
///
1129+
/// The map contains all DepNodes that have been allocated in the current session so far and
1130+
/// for which there is no equivalent in the previous session.
11231131
#[cfg(debug_assertions)]
1124-
seen_dep_nodes: Option<Lock<FxHashSet<DepNode<K>>>>,
1132+
nodes_newly_allocated_in_current_session: Option<Lock<FxHashSet<DepNode<K>>>>,
11251133

11261134
/// Anonymous `DepNode`s are nodes whose IDs we compute from the list of
11271135
/// their edges. This has the beneficial side-effect that multiple anonymous
@@ -1205,12 +1213,16 @@ impl<K: DepKind> CurrentDepGraph<K> {
12051213
#[cfg(debug_assertions)]
12061214
fingerprints: Lock::new(IndexVec::from_elem_n(None, new_node_count_estimate)),
12071215
#[cfg(debug_assertions)]
1208-
seen_dep_nodes: session.opts.unstable_opts.incremental_verify_ich.then(|| {
1209-
Lock::new(FxHashSet::with_capacity_and_hasher(
1210-
new_node_count_estimate,
1211-
Default::default(),
1212-
))
1213-
}),
1216+
nodes_newly_allocated_in_current_session: session
1217+
.opts
1218+
.unstable_opts
1219+
.incremental_verify_ich
1220+
.then(|| {
1221+
Lock::new(FxHashSet::with_capacity_and_hasher(
1222+
new_node_count_estimate,
1223+
Default::default(),
1224+
))
1225+
}),
12141226
total_read_count: AtomicU64::new(0),
12151227
total_duplicate_read_count: AtomicU64::new(0),
12161228
node_intern_event_id,
@@ -1242,8 +1254,10 @@ impl<K: DepKind> CurrentDepGraph<K> {
12421254
self.record_edge(dep_node_index, key, current_fingerprint);
12431255

12441256
#[cfg(debug_assertions)]
1245-
if let Some(ref seen_dep_nodes) = self.seen_dep_nodes {
1246-
if !seen_dep_nodes.lock().insert(key) {
1257+
if let Some(ref nodes_newly_allocated_in_current_session) =
1258+
self.nodes_newly_allocated_in_current_session
1259+
{
1260+
if !nodes_newly_allocated_in_current_session.lock().insert(key) {
12471261
panic!("Found duplicate dep-node {key:?}");
12481262
}
12491263
}

0 commit comments

Comments
 (0)