Skip to content

Commit f09ac1b

Browse files
authored
[clang][bytecode] Fix an out-of-bounds access with ia32_pmul* (#154750)
... builtins. We used to access the I'th index of the output vector, but that doesn't work since the output vector is only half the size of the input vector.
1 parent a33e505 commit f09ac1b

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,9 +2683,10 @@ static bool interp__builtin_ia32_pmul(InterpState &S, CodePtr OpPC,
26832683
const auto *VT = Call->getArg(0)->getType()->castAs<VectorType>();
26842684
PrimType ElemT = *S.getContext().classify(VT->getElementType());
26852685
unsigned SourceLen = VT->getNumElements();
2686-
SmallVector<APValue, 4> ResultElements;
2687-
ResultElements.reserve(SourceLen / 2);
26882686

2687+
PrimType DstElemT = *S.getContext().classify(
2688+
Call->getType()->castAs<VectorType>()->getElementType());
2689+
unsigned DstElem = 0;
26892690
for (unsigned I = 0; I != SourceLen; I += 2) {
26902691
APSInt Elem1;
26912692
APSInt Elem2;
@@ -2699,16 +2700,19 @@ static bool interp__builtin_ia32_pmul(InterpState &S, CodePtr OpPC,
26992700
case clang::X86::BI__builtin_ia32_pmuludq128:
27002701
case clang::X86::BI__builtin_ia32_pmuludq256:
27012702
case clang::X86::BI__builtin_ia32_pmuludq512:
2702-
Result = APSInt(llvm::APIntOps::muluExtended(Elem1, Elem2), true);
2703+
Result = APSInt(llvm::APIntOps::muluExtended(Elem1, Elem2),
2704+
/*IsUnsigned=*/true);
27032705
break;
27042706
case clang::X86::BI__builtin_ia32_pmuldq128:
27052707
case clang::X86::BI__builtin_ia32_pmuldq256:
27062708
case clang::X86::BI__builtin_ia32_pmuldq512:
2707-
Result = APSInt(llvm::APIntOps::mulsExtended(Elem1, Elem2), false);
2709+
Result = APSInt(llvm::APIntOps::mulsExtended(Elem1, Elem2),
2710+
/*IsUnsigned=*/false);
27082711
break;
27092712
}
2710-
INT_TYPE_SWITCH_NO_BOOL(ElemT,
2711-
{ Dst.elem<T>(I) = static_cast<T>(Result); });
2713+
INT_TYPE_SWITCH_NO_BOOL(DstElemT,
2714+
{ Dst.elem<T>(DstElem) = static_cast<T>(Result); });
2715+
++DstElem;
27122716
}
27132717

27142718
Dst.initializeAllElements();
@@ -3204,6 +3208,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
32043208
case clang::X86::BI__builtin_ia32_pmuldq512:
32053209
case clang::X86::BI__builtin_ia32_pmuludq128:
32063210
case clang::X86::BI__builtin_ia32_pmuludq256:
3211+
case clang::X86::BI__builtin_ia32_pmuludq512:
32073212
return interp__builtin_ia32_pmul(S, OpPC, Call, BuiltinID);
32083213
case Builtin::BI__builtin_elementwise_fma:
32093214
return interp__builtin_elementwise_fma(S, OpPC, Call);

0 commit comments

Comments
 (0)