Skip to content

Commit 51d03c1

Browse files
committed
Add extra diagnostics in JitCall::AreArgumentsValid
This catches 0 objects to construct/destruct and attempting array new with requried initialization parameters
1 parent 9b12e7f commit 51d03c1

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

include/CppInterOp/CppInterOp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ class JitCall {
140140
: m_DestructorCall(C), m_Kind(K), m_FD(Dtor) {}
141141

142142
/// Checks if the passed arguments are valid for the given function.
143-
CPPINTEROP_API bool AreArgumentsValid(void* result, ArgList args,
144-
void* self) const;
143+
CPPINTEROP_API bool AreArgumentsValid(void* result, ArgList args, void* self,
144+
size_t nary = 1UL) const;
145145

146146
/// This function is used for debugging, it reports when the function was
147147
/// called.
@@ -221,7 +221,7 @@ class JitCall {
221221
ArgList args = {}, void* is_arena = nullptr) const {
222222
assert(m_Kind == kConstructorCall && "Wrong overload!");
223223
#ifndef NDEBUG
224-
assert(AreArgumentsValid(result, args, nullptr, nary) && "Invalid args!");
224+
assert(AreArgumentsValid(result, args, /*self=*/nullptr, nary) && "Invalid args!");
225225
ReportInvokeStart(result, args, nullptr);
226226
#endif // NDEBUG
227227
m_ConstructorCall(result, nary, args.m_ArgSize, args.m_Args, is_arena);

lib/CppInterOp/CppInterOp.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ static clang::Sema& getSema() { return getInterp().getCI()->getSema(); }
133133
static clang::ASTContext& getASTContext() { return getSema().getASTContext(); }
134134

135135
#define DEBUG_TYPE "jitcall"
136-
bool JitCall::AreArgumentsValid(void* result, ArgList args, void* self) const {
136+
bool JitCall::AreArgumentsValid(void* result, ArgList args, void* self,
137+
size_t nary) const {
137138
bool Valid = true;
138139
if (Cpp::IsConstructor(m_FD)) {
139140
assert(result && "Must pass the location of the created object!");
@@ -157,6 +158,18 @@ bool JitCall::AreArgumentsValid(void* result, ArgList args, void* self) const {
157158
assert(0 && "We are discarding the return type of the function!");
158159
Valid = false;
159160
}
161+
if (Cpp::IsConstructor(m_FD) && nary == 0UL) {
162+
assert(0 && "Number of objects to construct should be atleast 1");
163+
Valid = false;
164+
}
165+
if (Cpp::IsConstructor(m_FD)) {
166+
const auto* CD = cast<CXXConstructorDecl>((const Decl*)m_FD);
167+
if (CD->getMinRequiredArguments() != 0 && nary > 1) {
168+
assert(0 &&
169+
"Cannot pass initialization parameters to array new construction");
170+
Valid = false;
171+
}
172+
}
160173
assert(m_Kind != kDestructorCall && "Wrong overload!");
161174
Valid &= m_Kind != kDestructorCall;
162175
return Valid;

0 commit comments

Comments
 (0)