Skip to content

Commit

Permalink
add stack-probes to make stack-overflow detection more reliable
Browse files Browse the repository at this point in the history
Supported platforms are currently X86, PowerPC, and SystemZ.
  • Loading branch information
vtjnash committed Mar 16, 2021
1 parent d7da2a4 commit f449adb
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,10 @@ static void jl_init_function(Function *F)
F->addFnAttr("no-frame-pointer-elim", "true");
#endif
#endif
#if JL_LLVM_VERSION >= 110000 && !defined(JL_ASAN_ENABLED)
F->addFnAttr("probe-stack", "inline-asm");
//F->addFnAttr("stack-probe-size", 4096); // can use this to change the default
#endif
}

static std::pair<bool, bool> uses_specsig(jl_method_instance_t *lam, jl_value_t *rettype, bool prefer_specsig)
Expand Down Expand Up @@ -6297,28 +6301,15 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
}
}

/*
// step 6. (optional) check for stack overflow (the slower way)
Value *cur_sp =
ctx.builder.CreateCall(Intrinsic::getDeclaration(M,
Intrinsic::frameaddress),
ConstantInt::get(T_int32, 0));
Value *sp_ok =
ctx.builder.CreateICmpUGT(cur_sp,
ConstantInt::get(T_size,
(uptrint_t)jl_stack_lo));
error_unless(ctx, sp_ok, "stack overflow");
*/

// step 7. set up GC frame
// step 6. set up GC frame
allocate_gc_frame(ctx, b0);
Value *last_age = NULL;
emit_last_age_field(ctx);
if (toplevel || ctx.is_opaque_closure) {
last_age = tbaa_decorate(tbaa_gcframe, ctx.builder.CreateAlignedLoad(ctx.world_age_field, Align(sizeof(size_t))));
}

// step 8. allocate local variables slots
// step 7. allocate local variables slots
// must be in the first basic block for the llvm mem2reg pass to work
auto allocate_local = [&](jl_varinfo_t &varinfo, jl_sym_t *s) {
jl_value_t *jt = varinfo.value.typ;
Expand Down Expand Up @@ -6436,7 +6427,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
}
}

// step 9. move args into local variables
// step 8. move args into local variables
Function::arg_iterator AI = f->arg_begin();

auto get_specsig_arg = [&](jl_value_t *argType, Type *llvmArgType, bool isboxed) {
Expand Down Expand Up @@ -6566,7 +6557,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
}
}

// step 10. allocate rest argument
// step 9. allocate rest argument
CallInst *restTuple = NULL;
if (va && ctx.vaSlot != -1) {
jl_varinfo_t &vi = ctx.slots[ctx.vaSlot];
Expand Down Expand Up @@ -6608,7 +6599,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
}
}

// step 11. Compute properties for each statements
// step 10. Compute properties for each statements
// This needs to be computed by iterating in the IR order
// instead of control flow order.
auto in_user_mod = [] (jl_module_t *mod) {
Expand Down Expand Up @@ -6730,7 +6721,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
Instruction &prologue_end = ctx.builder.GetInsertBlock()->back();


// step 12. Do codegen in control flow order
// step 11. Do codegen in control flow order
std::vector<int> workstack;
std::map<int, BasicBlock*> BB;
std::map<size_t, BasicBlock*> come_from_bb;
Expand Down Expand Up @@ -7288,7 +7279,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
PN->eraseFromParent();
}

// step 13. Perform any delayed instantiations
// step 12. Perform any delayed instantiations
if (ctx.debug_enabled) {
bool in_prologue = true;
for (auto &BB : *ctx.f) {
Expand Down

0 comments on commit f449adb

Please sign in to comment.