Emit retags in codegen to support BorrowSanitizer.#155965
Emit retags in codegen to support BorrowSanitizer.#155965icmccorm wants to merge 1 commit intorust-lang:mainfrom
Conversation
|
The GCC codegen subtree was changed |
|
r? @jieyouxu rustbot has assigned @jieyouxu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
2e97d21 to
8267fce
Compare
| /// Encoded type information used to determine the kind of permission | ||
| /// created by the retag. | ||
| pub flags: RetagFlags, | ||
| /// A constant array of (offset, size) pairs describing |
There was a problem hiding this comment.
A pointer to a constant array, right?
| Nothing, | ||
| /// Store the return value to the pointer. | ||
| Store(PlaceRef<'tcx, V>), | ||
| Store(PlaceRef<'tcx, V>, bool), |
There was a problem hiding this comment.
Mind giving these fields names? A plain bool doesn't explain what it means.
| &mut self, | ||
| ty: Self::Type, | ||
| cases: impl ExactSizeIterator<Item = (Self::BasicBlock, Self::Value)>, | ||
| ) -> Self::Value; |
There was a problem hiding this comment.
I intentionally remove the phi method a while back in b71c429 as not every codegen backend uses phi nodes for SSA. Cranelift for example uses block params instead. I replaced the only use of it with a single method representing the high level operation where it was previously whose cg_llvm implementation uses phi nodes: write_operand_repeatedly
There was a problem hiding this comment.
It sounds like we will need to introduce a temporary alloca, instead.
|
@rustbot reroll |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
Tracking issue: #154760
Retags are the core operation of Stacked and Tree Borrows—they update the tags of references within a place. Knowing when to retag requires information that is not available below the MIR level. BorrowSanitizer, and any similar tool, needs a way to represent retags in lower-level representations of Rust programs to be able to find aliasing bugs with native instrumentation.
This PR adds experimental support for emitting retags during codegen as function calls. This is enabled by the flag
-Zcodegen-emit-retag. These functions are not implemented anywhere. They are just a useful vehicle for carrying type information.There are two variants of the retag function. We use the first variant to retag pointers that are already loaded into a register. It returns an alias with the same address, but different provenance.
We use the second variant to retag pointers that are stored within a place. Its first parameter is a pointer to the place where the pointer that needs the retag is stored.
We need this for compatibility with LLVM's
readonlyannotation. Otherwise, we'd be loading the pointer, retagging it with theregvariant, and storing it back to the place that it came from, which would be undefined behavior. The other parameters are the same across both variants and are documented in the relevant module. These are slightly different than what's appeared in all prior proposals.These retags follow Tree Borrows semantics. We retag
Boxand reference-type arguments, return values, and rvalues of assignments. Raw pointers are not retagged. We recurse into fields and branch on the variants of each type.Questions/Concerns:
phinodes through codegen_ssa.Related:
drop_in_placelang item to&mut _#154327 - We don't need a special case for drop glue once it takes&mut _Rvalue::Use(_, WithRetag::No)cc: @RalfJung, @tmandry