Skip to content

Commit 3895908

Browse files
committed
Sanity: Don't spend too much time (or memory) on rediculously large enums
RemoteMirror gets called on lots of malformed type information, due to memory corruption bugs or even clients that ask RemoteMirror to decode a chunk of memory to test whether or not it might be valid type data. In any case, we need to be a little cautious here. In this case, I've chosen to ignore any enum whose in-memory size (according to the metadata) is over 1 MiB. We can easily adjust this limit up if experience shows there really are legitimate enums this large in the wild.
1 parent 346d5b2 commit 3895908

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

stdlib/public/RemoteInspection/TypeLowering.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,15 @@ class EnumTypeInfoBuilder {
19871987
default: Kind = EnumKind::MultiPayloadEnum; break;
19881988
}
19891989

1990+
// Sanity: Ignore any enum that claims to have a size more than 1MiB
1991+
// This avoids allocating lots of memory for spare bit mask calculations
1992+
// when clients try to interpret random chunks of memory as type descriptions.
1993+
if (Size > (1024ULL * 1024)) {
1994+
unsigned Stride = ((Size + Alignment - 1) & ~(Alignment - 1));
1995+
return TC.makeTypeInfo<UnsupportedEnumTypeInfo>(
1996+
Size, Alignment, Stride, NumExtraInhabitants, BitwiseTakable, Kind, Cases);
1997+
}
1998+
19901999
if (Cases.size() == 1) {
19912000
if (EffectivePayloadCases == 0) {
19922001
// Zero-sized enum with only one empty case

0 commit comments

Comments
 (0)