Skip to content

Commit bc8b8d8

Browse files
committed
[scudo] Compute the default aligned pointer without tag
#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'
1 parent 97025bd commit bc8b8d8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

compiler-rt/lib/scudo/standalone/combined.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,10 @@ class Allocator {
10521052
void *Block, const uptr UserPtr,
10531053
const uptr SizeOrUnusedBytes,
10541054
const FillContentsMode FillContents) {
1055+
// Compute the default pointer before adding the header tag
1056+
const uptr DefaultAlignedPtr =
1057+
reinterpret_cast<uptr>(Block) + Chunk::getHeaderSize();
1058+
10551059
Block = addHeaderTag(Block);
10561060
// Only do content fill when it's from primary allocator because secondary
10571061
// allocator has filled the content.
@@ -1064,8 +1068,6 @@ class Allocator {
10641068

10651069
Chunk::UnpackedHeader Header = {};
10661070

1067-
const uptr DefaultAlignedPtr =
1068-
reinterpret_cast<uptr>(Block) + Chunk::getHeaderSize();
10691071
if (UNLIKELY(DefaultAlignedPtr != UserPtr)) {
10701072
const uptr Offset = UserPtr - DefaultAlignedPtr;
10711073
DCHECK_GE(Offset, 2 * sizeof(u32));
@@ -1096,6 +1098,10 @@ class Allocator {
10961098
const Options Options = Primary.Options.load();
10971099
DCHECK(useMemoryTagging<AllocatorConfig>(Options));
10981100

1101+
// Compute the default pointer before adding the header tag
1102+
const uptr DefaultAlignedPtr =
1103+
reinterpret_cast<uptr>(Block) + Chunk::getHeaderSize();
1104+
10991105
void *Ptr = reinterpret_cast<void *>(UserPtr);
11001106
void *TaggedPtr = Ptr;
11011107

@@ -1194,8 +1200,6 @@ class Allocator {
11941200

11951201
Chunk::UnpackedHeader Header = {};
11961202

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

0 commit comments

Comments
 (0)