Skip to content

[IRGen] Prevent overflow in RecordField with large explosions #64828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsIRGen.def
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ ERROR(temporary_allocation_alignment_not_positive,none,
ERROR(temporary_allocation_alignment_not_power_of_2,none,
"alignment value must be a power of two", ())

ERROR(explosion_size_oveflow,none,
"explosion size too large", ())

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
13 changes: 10 additions & 3 deletions lib/IRGen/GenRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#include "Outlining.h"
#include "TypeInfo.h"
#include "StructLayout.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/TrailingObjects.h"
#include "swift/AST/DiagnosticsIRGen.h"

namespace swift {
namespace irgen {
Expand All @@ -42,8 +44,8 @@ template <class FieldImpl> class RecordField {
template <class, class, class> friend class RecordTypeBuilder;

/// Begin/End - the range of explosion indexes for this element
unsigned Begin : 16;
unsigned End : 16;
unsigned Begin;
unsigned End;

protected:
explicit RecordField(const TypeInfo &elementTI)
Expand Down Expand Up @@ -883,7 +885,12 @@ class RecordTypeBuilder {

auto &fieldInfo = fields.back();
fieldInfo.Begin = explosionSize;
explosionSize += loadableFieldTI->getExplosionSize();
bool overflow = false;
explosionSize = llvm::SaturatingAdd(explosionSize, loadableFieldTI->getExplosionSize(), &overflow);
if (overflow) {
IGM.Context.Diags.diagnose(SourceLoc(), diag::explosion_size_oveflow);
}

fieldInfo.End = explosionSize;
}

Expand Down