Skip to content

Commit 7ff8436

Browse files
authored
Merge pull request #3164 from compnerd/misalignment
CoreFoundation: correct alignment mis-assumption
2 parents 365bb75 + 2d3636b commit 7ff8436

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

CoreFoundation/Base.subproj/CFRuntime.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,21 @@ CFTypeRef _CFRuntimeCreateInstance(CFAllocatorRef allocator, CFTypeID typeID, CF
443443
uintptr_t isa = __CFRuntimeObjCClassTable[typeID];
444444
CFIndex size = sizeof(CFRuntimeBase) + extraBytes;
445445
const CFRuntimeClass *cls = __CFRuntimeClassTable[typeID];
446-
size_t align = (cls->version & _kCFRuntimeRequiresAlignment) ? cls->requiredAlignment : 16;
446+
447+
#if !defined(__APPLE__) && (defined(__i686__) || (defined(__arm__) && !defined(__aarch64__)) || defined(_M_IX86) || defined(_M_ARM))
448+
// Linux and Windows 32-bit targets perform 8-byte alignment by default.
449+
static const kDefaultAlignment = 8;
450+
#else
451+
static const kDefaultAlignment = 16;
452+
#endif
453+
454+
// Ensure that we get the alignment correct for various targets. In the
455+
// case that we are over-aligned `swift_allocObject` will go through a
456+
// different allocator to ensure that the pointer is suitably aligned. When
457+
// we subsequently release the pointer we do not tag that release to go
458+
// through the overalign'ed path. This may result in a cross-domainf free
459+
// and a resultant heap corruption.
460+
size_t align = (cls->version & _kCFRuntimeRequiresAlignment) ? cls->requiredAlignment : kDefaultAlignment;
447461

448462
CFRuntimeBase *memory = (CFRuntimeBase *)swift_allocObject(isa, size, align - 1);
449463

0 commit comments

Comments
 (0)