From 0aad5e95bfe09734f34dba7b8e67b57e599bded0 Mon Sep 17 00:00:00 2001 From: Paul Tarjan Date: Sat, 27 Apr 2013 12:33:30 -0700 Subject: [PATCH] support pcre ini options --- hphp/runtime/base/execution_context.cpp | 2 ++ hphp/runtime/base/execution_context.h | 4 ++++ hphp/runtime/base/preg.cpp | 18 ++++++++++++++---- hphp/runtime/base/runtime_option.cpp | 8 ++++---- hphp/runtime/base/runtime_option.h | 4 ++-- hphp/runtime/ext/ext_preg.cpp | 1 + hphp/test/quick/pcre_limit.php | 10 ++++++++++ hphp/test/quick/pcre_limit.php.expectf | 3 +++ 8 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 hphp/test/quick/pcre_limit.php create mode 100644 hphp/test/quick/pcre_limit.php.expectf diff --git a/hphp/runtime/base/execution_context.cpp b/hphp/runtime/base/execution_context.cpp index 2a18086d9..2dea3e17c 100644 --- a/hphp/runtime/base/execution_context.cpp +++ b/hphp/runtime/base/execution_context.cpp @@ -72,6 +72,8 @@ BaseExecutionContext::BaseExecutionContext() : } VMExecutionContext::VMExecutionContext() : + m_preg_backtrace_limit(RuntimeOption::PregBacktraceLimit), + m_preg_recursion_limit(RuntimeOption::PregRecursionLimit), m_lambdaCounter(0), m_nesting(0), m_injTables(nullptr), m_breakPointFilter(nullptr), m_lastLocFilter(nullptr), m_interpreting(false), m_dbgNoBreak(false), diff --git a/hphp/runtime/base/execution_context.h b/hphp/runtime/base/execution_context.h index 60ea5fe14..4d00145fa 100644 --- a/hphp/runtime/base/execution_context.h +++ b/hphp/runtime/base/execution_context.h @@ -421,6 +421,10 @@ public: typedef std::set LiveObjSet; LiveObjSet m_liveBCObjs; + // pcre ini_settings + long m_preg_backtrace_limit; + long m_preg_recursion_limit; + public: void requestInit(); void requestExit(); diff --git a/hphp/runtime/base/preg.cpp b/hphp/runtime/base/preg.cpp index efb4f77ff..e77441847 100644 --- a/hphp/runtime/base/preg.cpp +++ b/hphp/runtime/base/preg.cpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #define PREG_PATTERN_ORDER 1 @@ -107,6 +109,14 @@ static __thread int t_last_error_code; namespace { +static void preg_init_thread_locals() { + IniSetting::Bind("pcre.backtrack_limit", "1000000", ini_on_update_long, + &g_context->m_preg_backtrace_limit); + IniSetting::Bind("pcre.recursion_limit", "100000", ini_on_update_long, + &g_context->m_preg_recursion_limit); +} +InitFiniNode init(preg_init_thread_locals, InitFiniNode::ThreadInit); + template struct FreeHelperImpl : private boost::noncopyable { explicit FreeHelperImpl(void* p) : p(p) {} @@ -284,8 +294,8 @@ static void set_extra_limits(pcre_extra*& extra) { PCRE_EXTRA_MATCH_LIMIT_RECURSION; extra = &extra_data; } - extra->match_limit = RuntimeOption::PregBacktraceLimit; - extra->match_limit_recursion = RuntimeOption::PregRecursionLimit; + extra->match_limit = g_context->m_preg_backtrace_limit; + extra->match_limit_recursion = g_context->m_preg_recursion_limit; } static int *create_offset_array(const pcre_cache_entry *pce, @@ -356,10 +366,10 @@ static void pcre_log_error(const char *func, int line, int pcre_code, "UNKNOWN"; raise_debugging( "REGEXERR: %s/%d: err=%d(%s), pattern='%s', subject='%s', repl='%s', " - "limits=(%d, %d), extra=(%d, %d, %d, %d)", + "limits=(%ld, %ld), extra=(%d, %d, %d, %d)", func, line, pcre_code, errString, escapedPattern, escapedSubject, escapedRepl, - RuntimeOption::PregBacktraceLimit, RuntimeOption::PregRecursionLimit, + g_context->m_preg_backtrace_limit, g_context->m_preg_recursion_limit, arg1, arg2, arg3, arg4); free((void *)escapedPattern); free((void *)escapedSubject); diff --git a/hphp/runtime/base/runtime_option.cpp b/hphp/runtime/base/runtime_option.cpp index ed4671269..6d3015c94 100644 --- a/hphp/runtime/base/runtime_option.cpp +++ b/hphp/runtime/base/runtime_option.cpp @@ -435,8 +435,8 @@ std::string RuntimeOption::DebuggerUsageLogFile; std::string RuntimeOption::SendmailPath; std::string RuntimeOption::MailForceExtraParameters; -int RuntimeOption::PregBacktraceLimit = 100000; -int RuntimeOption::PregRecursionLimit = 100000; +long RuntimeOption::PregBacktraceLimit = 1000000; +long RuntimeOption::PregRecursionLimit = 100000; bool RuntimeOption::EnablePregErrorLog = true; bool RuntimeOption::EnableHotProfiler = true; @@ -1251,8 +1251,8 @@ void RuntimeOption::Load(Hdf &config, StringVec *overwrites /* = NULL */, } { Hdf preg = config["Preg"]; - PregBacktraceLimit = preg["BacktraceLimit"].getInt32(100000); - PregRecursionLimit = preg["RecursionLimit"].getInt32(100000); + PregBacktraceLimit = preg["BacktraceLimit"].getInt64(1000000); + PregRecursionLimit = preg["RecursionLimit"].getInt64(100000); EnablePregErrorLog = preg["ErrorLog"].getBool(true); } diff --git a/hphp/runtime/base/runtime_option.h b/hphp/runtime/base/runtime_option.h index 3c5997f60..32c22d37d 100644 --- a/hphp/runtime/base/runtime_option.h +++ b/hphp/runtime/base/runtime_option.h @@ -495,8 +495,8 @@ public: static std::string MailForceExtraParameters; // preg stack depth and debug support options - static int PregBacktraceLimit; - static int PregRecursionLimit; + static long PregBacktraceLimit; + static long PregRecursionLimit; static bool EnablePregErrorLog; // Convenience switch to turn on/off code alternatives via command-line diff --git a/hphp/runtime/ext/ext_preg.cpp b/hphp/runtime/ext/ext_preg.cpp index 8f6d84711..acc83220d 100644 --- a/hphp/runtime/ext/ext_preg.cpp +++ b/hphp/runtime/ext/ext_preg.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include namespace HPHP { diff --git a/hphp/test/quick/pcre_limit.php b/hphp/test/quick/pcre_limit.php new file mode 100644 index 000000000..7d03dfe33 --- /dev/null +++ b/hphp/test/quick/pcre_limit.php @@ -0,0 +1,10 @@ +