Skip to content

Commit

Permalink
[scudo] Compute the default aligned pointer without tag (#92989)
Browse files Browse the repository at this point in the history
llvm/llvm-project#83493 slightly
changed the order of computation of block addresses and
pointers, causing the value of DefaultAlignedPtr to
include the MTE tag. Move this computation earlier so it
matches the old behavior.

This fixes a UBSan failure in Trusty:
secure os: UBSan: (overflow:-)
external/scudo/standalone/combined.h:1070:35
secure os: Details: unsigned integer overflow: 8988807738704 -
144124176883594576 cannot be represented in type 'uptr'

GitOrigin-RevId: b17d44558ba4c30a3005089b334f68593d6a9c7c
Change-Id: Ie86f195d79144e0539684a71dbedaa0c8b961729
  • Loading branch information
ahomescu authored and copybara-github committed May 23, 2024
1 parent 7fc8017 commit d3f967b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions standalone/combined.h
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,10 @@ class Allocator {
void *Block, const uptr UserPtr,
const uptr SizeOrUnusedBytes,
const FillContentsMode FillContents) {
// Compute the default pointer before adding the header tag
const uptr DefaultAlignedPtr =
reinterpret_cast<uptr>(Block) + Chunk::getHeaderSize();

Block = addHeaderTag(Block);
// Only do content fill when it's from primary allocator because secondary
// allocator has filled the content.
Expand All @@ -1064,8 +1068,6 @@ class Allocator {

Chunk::UnpackedHeader Header = {};

const uptr DefaultAlignedPtr =
reinterpret_cast<uptr>(Block) + Chunk::getHeaderSize();
if (UNLIKELY(DefaultAlignedPtr != UserPtr)) {
const uptr Offset = UserPtr - DefaultAlignedPtr;
DCHECK_GE(Offset, 2 * sizeof(u32));
Expand Down Expand Up @@ -1096,6 +1098,10 @@ class Allocator {
const Options Options = Primary.Options.load();
DCHECK(useMemoryTagging<AllocatorConfig>(Options));

// Compute the default pointer before adding the header tag
const uptr DefaultAlignedPtr =
reinterpret_cast<uptr>(Block) + Chunk::getHeaderSize();

void *Ptr = reinterpret_cast<void *>(UserPtr);
void *TaggedPtr = Ptr;

Expand Down Expand Up @@ -1194,8 +1200,6 @@ class Allocator {

Chunk::UnpackedHeader Header = {};

const uptr DefaultAlignedPtr =
reinterpret_cast<uptr>(Block) + Chunk::getHeaderSize();
if (UNLIKELY(DefaultAlignedPtr != UserPtr)) {
const uptr Offset = UserPtr - DefaultAlignedPtr;
DCHECK_GE(Offset, 2 * sizeof(u32));
Expand Down

0 comments on commit d3f967b

Please sign in to comment.