Skip to content

Commit db7475a

Browse files
authored
[clang][bytecode] Ignore overflow in unary operators if requested (#132557)
Add PreInc and PreDec ops for this purpose and ignore the overflow if UnaryOperator::canOverflow() returns false.
1 parent ccc18ca commit db7475a

File tree

4 files changed

+80
-35
lines changed

4 files changed

+80
-35
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,7 +3518,7 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
35183518
// ++Iter;
35193519
if (!this->emitGetPtrLocal(Iter, E))
35203520
return false;
3521-
if (!this->emitIncPop(SizeT, E))
3521+
if (!this->emitIncPop(SizeT, false, E))
35223522
return false;
35233523

35243524
if (!this->jump(StartLabel))
@@ -5957,7 +5957,8 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
59575957
: this->emitIncf(getFPOptions(E), E);
59585958
}
59595959

5960-
return DiscardResult ? this->emitIncPop(*T, E) : this->emitInc(*T, E);
5960+
return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
5961+
: this->emitInc(*T, E->canOverflow(), E);
59615962
}
59625963
case UO_PostDec: { // x--
59635964
if (!Ctx.getLangOpts().CPlusPlus14)
@@ -5980,7 +5981,8 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
59805981
: this->emitDecf(getFPOptions(E), E);
59815982
}
59825983

5983-
return DiscardResult ? this->emitDecPop(*T, E) : this->emitDec(*T, E);
5984+
return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
5985+
: this->emitDec(*T, E->canOverflow(), E);
59845986
}
59855987
case UO_PreInc: { // ++x
59865988
if (!Ctx.getLangOpts().CPlusPlus14)
@@ -6005,7 +6007,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60056007
if (DiscardResult) {
60066008
if (T == PT_Float)
60076009
return this->emitIncfPop(getFPOptions(E), E);
6008-
return this->emitIncPop(*T, E);
6010+
return this->emitIncPop(*T, E->canOverflow(), E);
60096011
}
60106012

60116013
if (T == PT_Float) {
@@ -6020,13 +6022,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60206022
return false;
60216023
} else {
60226024
assert(isIntegralType(*T));
6023-
if (!this->emitLoad(*T, E))
6024-
return false;
6025-
if (!this->emitConst(1, E))
6026-
return false;
6027-
if (!this->emitAdd(*T, E))
6028-
return false;
6029-
if (!this->emitStore(*T, E))
6025+
if (!this->emitPreInc(*T, E->canOverflow(), E))
60306026
return false;
60316027
}
60326028
return E->isGLValue() || this->emitLoadPop(*T, E);
@@ -6054,7 +6050,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60546050
if (DiscardResult) {
60556051
if (T == PT_Float)
60566052
return this->emitDecfPop(getFPOptions(E), E);
6057-
return this->emitDecPop(*T, E);
6053+
return this->emitDecPop(*T, E->canOverflow(), E);
60586054
}
60596055

60606056
if (T == PT_Float) {
@@ -6069,13 +6065,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60696065
return false;
60706066
} else {
60716067
assert(isIntegralType(*T));
6072-
if (!this->emitLoad(*T, E))
6073-
return false;
6074-
if (!this->emitConst(1, E))
6075-
return false;
6076-
if (!this->emitSub(*T, E))
6077-
return false;
6078-
if (!this->emitStore(*T, E))
6068+
if (!this->emitPreDec(*T, E->canOverflow(), E))
60796069
return false;
60806070
}
60816071
return E->isGLValue() || this->emitLoadPop(*T, E);

clang/lib/AST/ByteCode/Interp.h

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,8 @@ enum class IncDecOp {
765765
};
766766

767767
template <typename T, IncDecOp Op, PushVal DoPush>
768-
bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
768+
bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
769+
bool CanOverflow) {
769770
assert(!Ptr.isDummy());
770771

771772
if constexpr (std::is_same_v<T, Boolean>) {
@@ -780,16 +781,17 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
780781
S.Stk.push<T>(Value);
781782

782783
if constexpr (Op == IncDecOp::Inc) {
783-
if (!T::increment(Value, &Result)) {
784+
if (!T::increment(Value, &Result) || !CanOverflow) {
784785
Ptr.deref<T>() = Result;
785786
return true;
786787
}
787788
} else {
788-
if (!T::decrement(Value, &Result)) {
789+
if (!T::decrement(Value, &Result) || !CanOverflow) {
789790
Ptr.deref<T>() = Result;
790791
return true;
791792
}
792793
}
794+
assert(CanOverflow);
793795

794796
// Something went wrong with the previous operation. Compute the
795797
// result with another bit of precision.
@@ -812,7 +814,6 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
812814
<< Trunc << Type << E->getSourceRange();
813815
return true;
814816
}
815-
816817
return handleOverflow(S, OpPC, APResult);
817818
}
818819

@@ -821,49 +822,69 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
821822
/// 3) Writes the value increased by one back to the pointer
822823
/// 4) Pushes the original (pre-inc) value on the stack.
823824
template <PrimType Name, class T = typename PrimConv<Name>::T>
824-
bool Inc(InterpState &S, CodePtr OpPC) {
825+
bool Inc(InterpState &S, CodePtr OpPC, bool CanOverflow) {
825826
const Pointer &Ptr = S.Stk.pop<Pointer>();
826827
if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
827828
return false;
828829

829-
return IncDecHelper<T, IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr);
830+
return IncDecHelper<T, IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr,
831+
CanOverflow);
830832
}
831833

832834
/// 1) Pops a pointer from the stack
833835
/// 2) Load the value from the pointer
834836
/// 3) Writes the value increased by one back to the pointer
835837
template <PrimType Name, class T = typename PrimConv<Name>::T>
836-
bool IncPop(InterpState &S, CodePtr OpPC) {
838+
bool IncPop(InterpState &S, CodePtr OpPC, bool CanOverflow) {
837839
const Pointer &Ptr = S.Stk.pop<Pointer>();
838840
if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
839841
return false;
840842

841-
return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr);
843+
return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow);
844+
}
845+
846+
template <PrimType Name, class T = typename PrimConv<Name>::T>
847+
bool PreInc(InterpState &S, CodePtr OpPC, bool CanOverflow) {
848+
const Pointer &Ptr = S.Stk.peek<Pointer>();
849+
if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
850+
return false;
851+
852+
return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow);
842853
}
843854

844855
/// 1) Pops a pointer from the stack
845856
/// 2) Load the value from the pointer
846857
/// 3) Writes the value decreased by one back to the pointer
847858
/// 4) Pushes the original (pre-dec) value on the stack.
848859
template <PrimType Name, class T = typename PrimConv<Name>::T>
849-
bool Dec(InterpState &S, CodePtr OpPC) {
860+
bool Dec(InterpState &S, CodePtr OpPC, bool CanOverflow) {
850861
const Pointer &Ptr = S.Stk.pop<Pointer>();
851862
if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
852863
return false;
853864

854-
return IncDecHelper<T, IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr);
865+
return IncDecHelper<T, IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr,
866+
CanOverflow);
855867
}
856868

857869
/// 1) Pops a pointer from the stack
858870
/// 2) Load the value from the pointer
859871
/// 3) Writes the value decreased by one back to the pointer
860872
template <PrimType Name, class T = typename PrimConv<Name>::T>
861-
bool DecPop(InterpState &S, CodePtr OpPC) {
873+
bool DecPop(InterpState &S, CodePtr OpPC, bool CanOverflow) {
862874
const Pointer &Ptr = S.Stk.pop<Pointer>();
863875
if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
864876
return false;
865877

866-
return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr);
878+
return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow);
879+
}
880+
881+
template <PrimType Name, class T = typename PrimConv<Name>::T>
882+
bool PreDec(InterpState &S, CodePtr OpPC, bool CanOverflow) {
883+
const Pointer &Ptr = S.Stk.peek<Pointer>();
884+
if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
885+
return false;
886+
887+
return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow);
867888
}
868889

869890
template <IncDecOp Op, PushVal DoPush>

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,18 @@ def Shr : Opcode {
593593
def Inv: Opcode;
594594

595595
// Increment and decrement.
596-
def Inc: AluOpcode;
597-
def IncPop : AluOpcode;
598-
def Dec: AluOpcode;
599-
def DecPop: AluOpcode;
596+
class OverflowOpcode : Opcode {
597+
let Types = [AluTypeClass];
598+
let Args = [ArgBool];
599+
let HasGroup = 1;
600+
}
601+
602+
def Inc : OverflowOpcode;
603+
def IncPop : OverflowOpcode;
604+
def PreInc : OverflowOpcode;
605+
def Dec : OverflowOpcode;
606+
def DecPop : OverflowOpcode;
607+
def PreDec : OverflowOpcode;
600608

601609
// Float increment and decrement.
602610
def Incf: FloatOpcode;

clang/test/AST/ByteCode/literals.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,32 @@ namespace IncDec {
598598
static_assert(UnderFlow() == -1, ""); // both-error {{not an integral constant expression}} \
599599
// both-note {{in call to 'UnderFlow()'}}
600600

601+
/// This UnaryOperator can't overflow, so we shouldn't diagnose any overflow.
602+
constexpr int CanOverflow() {
603+
char c = 127;
604+
char p;
605+
++c;
606+
c++;
607+
p = ++c;
608+
p = c++;
609+
610+
c = -128;
611+
--c;
612+
c--;
613+
p = --c;
614+
p = ++c;
615+
616+
return 0;
617+
}
618+
static_assert(CanOverflow() == 0, "");
619+
620+
constexpr char OverflownChar() {
621+
char c = 127;
622+
c++;
623+
return c;
624+
}
625+
static_assert(OverflownChar() == -128, "");
626+
601627
constexpr int getTwo() {
602628
int i = 1;
603629
return (i += 1);

0 commit comments

Comments
 (0)