From 1865f2965b875c21b4bcba6d90d59379c9e6f813 Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Sat, 9 Mar 2019 23:18:42 +0100 Subject: [PATCH] Add weka option -template-codegen-depth= to prevent crashes on stack exhaustion upon deep recursion --- dmd/dtemplate.d | 10 ++++++++++ dmd/globals.d | 1 + dmd/globals.h | 1 + driver/cl_options.cpp | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/dmd/dtemplate.d b/dmd/dtemplate.d index f8fc3b28181..86f79da5362 100644 --- a/dmd/dtemplate.d +++ b/dmd/dtemplate.d @@ -6217,6 +6217,16 @@ extern (C++) class TemplateInstance : ScopeDsymbol */ final bool needsCodegen() { + version (IN_WEKA) + { + static uint callDepth; + callDepth++; + scope(exit) callDepth--; + + if (global.params.templateCodegenDepth && (callDepth > global.params.templateCodegenDepth)) + return false; + } + // Now -allInst is just for the backward compatibility. if (global.params.allInst) { diff --git a/dmd/globals.d b/dmd/globals.d index a8958f939dc..e996542de9c 100644 --- a/dmd/globals.d +++ b/dmd/globals.d @@ -287,6 +287,7 @@ struct Param version (IN_WEKA) { bool enableWekaMods; // Enable specific Weka mods like the template instantiation mods + uint templateCodegenDepth; // Don't codegen templates beyond this recusion depth. } } } diff --git a/dmd/globals.h b/dmd/globals.h index c5430c7e835..2e894ff3f3e 100644 --- a/dmd/globals.h +++ b/dmd/globals.h @@ -255,6 +255,7 @@ struct Param #if IN_WEKA bool enableWekaMods; // Enable specific Weka mods like the template instantiation mods + uint32_t templateCodegenDepth; #endif #endif }; diff --git a/driver/cl_options.cpp b/driver/cl_options.cpp index 58d4aae2932..f3008796804 100644 --- a/driver/cl_options.cpp +++ b/driver/cl_options.cpp @@ -432,6 +432,10 @@ static cl::opt> wekaMods( cl::desc( "(*) Enable specific Weka mods like the template instantiation mods"), cl::location(global.params.enableWekaMods), cl::init(true)); +static cl::opt + templateCodegenDepth("template-codegen-depth", + cl::desc("Don't codegen templates beyond this recusion depth (0 = off)."), + cl::location(global.params.templateCodegenDepth), cl::init(0)); #endif cl::opt ltoMode(