mirror of
https://github.com/ldc-developers/ldc.git
synced 2025-05-01 07:30:43 +03:00
Use new druntime hooks _d_arraybounds_{slice,index} for more informative RangeErrors
Fixing dmd-testsuite's runnable/testbounds.d.
This commit is contained in:
parent
64d79ef753
commit
fab82436dd
6 changed files with 75 additions and 28 deletions
|
@ -88,10 +88,8 @@ DLValue *DtoAAIndex(const Loc &loc, Type *type, DValue *aa, DValue *key,
|
|||
gIR->ir->CreateCondBr(cond, okbb, failbb);
|
||||
|
||||
// set up failbb to call the array bounds error runtime function
|
||||
|
||||
gIR->ir->SetInsertPoint(failbb);
|
||||
|
||||
DtoBoundsCheckFailCall(gIR, loc);
|
||||
emitRangeError(gIR, loc);
|
||||
|
||||
// if ok, proceed in okbb
|
||||
gIR->ir->SetInsertPoint(okbb);
|
||||
|
|
|
@ -1326,13 +1326,14 @@ void DtoIndexBoundsCheck(const Loc &loc, DValue *arr, DValue *index) {
|
|||
}
|
||||
|
||||
if (arrty->ty == TY::Tpointer) {
|
||||
// Length of pointers is unknown, ingore.
|
||||
// Length of pointers is unknown, ignore.
|
||||
return;
|
||||
}
|
||||
|
||||
llvm::ICmpInst::Predicate cmpop = llvm::ICmpInst::ICMP_ULT;
|
||||
llvm::Value *cond = gIR->ir->CreateICmp(cmpop, DtoRVal(index),
|
||||
DtoArrayLen(arr), "bounds.cmp");
|
||||
LLValue *const llIndex = DtoRVal(index);
|
||||
LLValue *const llLength = DtoArrayLen(arr);
|
||||
LLValue *const cond = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_ULT, llIndex,
|
||||
llLength, "bounds.cmp");
|
||||
|
||||
llvm::BasicBlock *okbb = gIR->insertBB("bounds.ok");
|
||||
llvm::BasicBlock *failbb = gIR->insertBBAfter(okbb, "bounds.fail");
|
||||
|
@ -1340,24 +1341,54 @@ void DtoIndexBoundsCheck(const Loc &loc, DValue *arr, DValue *index) {
|
|||
|
||||
// set up failbb to call the array bounds error runtime function
|
||||
gIR->ir->SetInsertPoint(failbb);
|
||||
DtoBoundsCheckFailCall(gIR, loc);
|
||||
emitArrayIndexError(gIR, loc, llIndex, llLength);
|
||||
|
||||
// if ok, proceed in okbb
|
||||
gIR->ir->SetInsertPoint(okbb);
|
||||
}
|
||||
|
||||
void DtoBoundsCheckFailCall(IRState *irs, const Loc &loc) {
|
||||
static void emitRangeErrorImpl(IRState *irs, const Loc &loc,
|
||||
const char *cAssertMsg, const char *dFnName,
|
||||
llvm::ArrayRef<LLValue *> extraArgs) {
|
||||
Module *const module = irs->func()->decl->getModule();
|
||||
|
||||
if (global.params.checkAction == CHECKACTION_C) {
|
||||
DtoCAssert(module, loc, DtoConstCString("array overflow"));
|
||||
} else {
|
||||
llvm::Function *errorfn =
|
||||
getRuntimeFunction(loc, irs->module, "_d_arraybounds");
|
||||
irs->CreateCallOrInvoke(errorfn, DtoModuleFileName(module, loc),
|
||||
DtoConstUint(loc.linnum));
|
||||
|
||||
// the function does not return
|
||||
switch (global.params.checkAction) {
|
||||
case CHECKACTION_C:
|
||||
DtoCAssert(module, loc, DtoConstCString(cAssertMsg));
|
||||
break;
|
||||
case CHECKACTION_halt:
|
||||
irs->ir->CreateCall(GET_INTRINSIC_DECL(trap), {});
|
||||
irs->ir->CreateUnreachable();
|
||||
break;
|
||||
case CHECKACTION_context:
|
||||
case CHECKACTION_D: {
|
||||
auto fn = getRuntimeFunction(loc, irs->module, dFnName);
|
||||
LLSmallVector<LLValue *, 5> args;
|
||||
args.reserve(2 + extraArgs.size());
|
||||
args.push_back(DtoModuleFileName(module, loc));
|
||||
args.push_back(DtoConstUint(loc.linnum));
|
||||
args.insert(args.end(), extraArgs.begin(), extraArgs.end());
|
||||
irs->CreateCallOrInvoke(fn, args);
|
||||
irs->ir->CreateUnreachable();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
llvm_unreachable("Unhandled checkAction");
|
||||
}
|
||||
}
|
||||
|
||||
void emitRangeError(IRState *irs, const Loc &loc) {
|
||||
emitRangeErrorImpl(irs, loc, "array overflow", "_d_arraybounds", {});
|
||||
}
|
||||
|
||||
void emitArraySliceError(IRState *irs, const Loc &loc, LLValue *lower,
|
||||
LLValue *upper, LLValue *length) {
|
||||
emitRangeErrorImpl(irs, loc, "array slice out of bounds",
|
||||
"_d_arraybounds_slice", {lower, upper, length});
|
||||
}
|
||||
|
||||
void emitArrayIndexError(IRState *irs, const Loc &loc, LLValue *index,
|
||||
LLValue *length) {
|
||||
emitRangeErrorImpl(irs, loc, "array index out of bounds",
|
||||
"_d_arraybounds_index", {index, length});
|
||||
}
|
||||
|
|
|
@ -88,4 +88,8 @@ void DtoIndexBoundsCheck(const Loc &loc, DValue *arr, DValue *index);
|
|||
|
||||
/// Inserts a call to the druntime function that throws the range error, with
|
||||
/// the given location.
|
||||
void DtoBoundsCheckFailCall(IRState *p, const Loc &loc);
|
||||
void emitRangeError(IRState *irs, const Loc &loc);
|
||||
void emitArraySliceError(IRState *irs, const Loc &loc, LLValue *lower,
|
||||
LLValue *upper, LLValue *length);
|
||||
void emitArrayIndexError(IRState *irs, const Loc &loc, LLValue *index,
|
||||
LLValue *length);
|
||||
|
|
|
@ -559,6 +559,17 @@ static void buildRuntimeModule() {
|
|||
createFwdDecl(LINK::c, voidTy, {"_d_assert_msg"}, {stringTy, stringTy, uintTy},
|
||||
{}, Attr_Cold_NoReturn);
|
||||
|
||||
// void _d_arraybounds_slice(string file, uint line, size_t lower,
|
||||
// size_t upper, size_t length)
|
||||
createFwdDecl(LINK::c, voidTy, {"_d_arraybounds_slice"},
|
||||
{stringTy, uintTy, sizeTy, sizeTy, sizeTy}, {},
|
||||
Attr_Cold_NoReturn);
|
||||
|
||||
// void _d_arraybounds_index(string file, uint line, size_t index,
|
||||
// size_t length)
|
||||
createFwdDecl(LINK::c, voidTy, {"_d_arraybounds_index"},
|
||||
{stringTy, uintTy, sizeTy, sizeTy}, {}, Attr_Cold_NoReturn);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
|
19
gen/toir.cpp
19
gen/toir.cpp
|
@ -1120,22 +1120,24 @@ public:
|
|||
LLValue *vup = DtoRVal(e->upr);
|
||||
p->arrays.pop_back();
|
||||
|
||||
const bool needCheckUpper =
|
||||
(etype->ty != TY::Tpointer) && !e->upperIsInBounds;
|
||||
const bool hasLength = etype->ty != TY::Tpointer;
|
||||
const bool needCheckUpper = hasLength && !e->upperIsInBounds;
|
||||
const bool needCheckLower = !e->lowerIsLessThanUpper;
|
||||
if (p->emitArrayBoundsChecks() && (needCheckUpper || needCheckLower)) {
|
||||
llvm::BasicBlock *okbb = p->insertBB("bounds.ok");
|
||||
llvm::BasicBlock *failbb = p->insertBBAfter(okbb, "bounds.fail");
|
||||
|
||||
llvm::Value *okCond = nullptr;
|
||||
LLValue *const vlen = hasLength ? DtoArrayLen(v) : nullptr;
|
||||
|
||||
LLValue *okCond = nullptr;
|
||||
if (needCheckUpper) {
|
||||
okCond = p->ir->CreateICmp(llvm::ICmpInst::ICMP_ULE, vup,
|
||||
DtoArrayLen(v), "bounds.cmp.lo");
|
||||
okCond = p->ir->CreateICmp(llvm::ICmpInst::ICMP_ULE, vup, vlen,
|
||||
"bounds.cmp.up");
|
||||
}
|
||||
|
||||
if (needCheckLower) {
|
||||
llvm::Value *cmp = p->ir->CreateICmp(llvm::ICmpInst::ICMP_ULE, vlo,
|
||||
vup, "bounds.cmp.up");
|
||||
LLValue *cmp = p->ir->CreateICmp(llvm::ICmpInst::ICMP_ULE, vlo, vup,
|
||||
"bounds.cmp.lo");
|
||||
if (okCond) {
|
||||
okCond = p->ir->CreateAnd(okCond, cmp);
|
||||
} else {
|
||||
|
@ -1146,7 +1148,8 @@ public:
|
|||
p->ir->CreateCondBr(okCond, okbb, failbb);
|
||||
|
||||
p->ir->SetInsertPoint(failbb);
|
||||
DtoBoundsCheckFailCall(p, e->loc);
|
||||
emitArraySliceError(p, e->loc, vlo, vup,
|
||||
vlen ? vlen : DtoConstSize_t(0));
|
||||
|
||||
p->ir->SetInsertPoint(okbb);
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit a9d48bee11c50e3b07bfccc253091c6c7fdd4008
|
||||
Subproject commit de5a8a3bb6466f53259e83de65a6894fc4cddf6e
|
Loading…
Add table
Add a link
Reference in a new issue