Skip to content

Commit 82eb871

Browse files
committed
add stack-probes to make stack-overflow detection more reliable
Supported platforms are currently X86, PowerPC, and SystemZ. Fixes #25523 Fixes #36170 Closes #28577 Closes #30892
1 parent ae0dc54 commit 82eb871

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/codegen.cpp

+15-20
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,14 @@ static void jl_init_function(Function *F)
17191719
F->addFnAttr("no-frame-pointer-elim", "true");
17201720
#endif
17211721
#endif
1722+
#if JL_LLVM_VERSION >= 110000 && !defined(JL_ASAN_ENABLED) && !defined(_OS_WINDOWS_)
1723+
// ASAN won't like us accessing undefined memory causing spurious issues,
1724+
// and Windows has platform-specific handling which causes it to mishandle
1725+
// this annotation. Other platforms should just ignore this if they don't
1726+
// implement it.
1727+
F->addFnAttr("probe-stack", "inline-asm");
1728+
//F->addFnAttr("stack-probe-size", 4096); // can use this to change the default
1729+
#endif
17221730
}
17231731

17241732
static std::pair<bool, bool> uses_specsig(jl_method_instance_t *lam, jl_value_t *rettype, bool prefer_specsig)
@@ -6297,28 +6305,15 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
62976305
}
62986306
}
62996307

6300-
/*
6301-
// step 6. (optional) check for stack overflow (the slower way)
6302-
Value *cur_sp =
6303-
ctx.builder.CreateCall(Intrinsic::getDeclaration(M,
6304-
Intrinsic::frameaddress),
6305-
ConstantInt::get(T_int32, 0));
6306-
Value *sp_ok =
6307-
ctx.builder.CreateICmpUGT(cur_sp,
6308-
ConstantInt::get(T_size,
6309-
(uptrint_t)jl_stack_lo));
6310-
error_unless(ctx, sp_ok, "stack overflow");
6311-
*/
6312-
6313-
// step 7. set up GC frame
6308+
// step 6. set up GC frame
63146309
allocate_gc_frame(ctx, b0);
63156310
Value *last_age = NULL;
63166311
emit_last_age_field(ctx);
63176312
if (toplevel || ctx.is_opaque_closure) {
63186313
last_age = tbaa_decorate(tbaa_gcframe, ctx.builder.CreateAlignedLoad(ctx.world_age_field, Align(sizeof(size_t))));
63196314
}
63206315

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

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

64426437
auto get_specsig_arg = [&](jl_value_t *argType, Type *llvmArgType, bool isboxed) {
@@ -6566,7 +6561,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
65666561
}
65676562
}
65686563

6569-
// step 10. allocate rest argument
6564+
// step 9. allocate rest argument
65706565
CallInst *restTuple = NULL;
65716566
if (va && ctx.vaSlot != -1) {
65726567
jl_varinfo_t &vi = ctx.slots[ctx.vaSlot];
@@ -6608,7 +6603,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
66086603
}
66096604
}
66106605

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

67326727

6733-
// step 12. Do codegen in control flow order
6728+
// step 11. Do codegen in control flow order
67346729
std::vector<int> workstack;
67356730
std::map<int, BasicBlock*> BB;
67366731
std::map<size_t, BasicBlock*> come_from_bb;
@@ -7288,7 +7283,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
72887283
PN->eraseFromParent();
72897284
}
72907285

7291-
// step 13. Perform any delayed instantiations
7286+
// step 12. Perform any delayed instantiations
72927287
if (ctx.debug_enabled) {
72937288
bool in_prologue = true;
72947289
for (auto &BB : *ctx.f) {

test/llvmpasses/noinline.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ include(joinpath("..", "testhelpers", "llvmpasses.jl"))
1717
return A + B
1818
end
1919

20-
# CHECK: attributes #{{[0-9]+}} = {{{([a-z]+ )*}} noinline {{([a-z]+ )*}}}
20+
# CHECK: attributes #{{[0-9]+}} = {{{[^}]*}} noinline {{[^}]*}}}
2121
emit(simple_noinline, Float64, Float64)

0 commit comments

Comments
 (0)