Skip to content

Commit

Permalink
Fix handling of pcre overflow expressions
Browse files Browse the repository at this point in the history
Summary: Couple of fixes to the handling of pcre compiled expressions that overflow the default pcre cache:

s_pcre_globals should smart-free m_overflow at the end of the request, and should free() the set of overflow expressions themselves.

Reviewed By: @markw65

Differential Revision: D1503955
  • Loading branch information
aravind authored and hhvm-bot committed Aug 26, 2014
1 parent 8fc034e commit 968dfa2
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 1 deletion.
13 changes: 12 additions & 1 deletion hphp/runtime/base/preg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,19 @@ pcre_cache_entry::~pcre_cache_entry() {
pcre_free(re);
}

void PCREglobals::onSessionExit() {
for (auto entry: m_overflow) {
delete entry;
}
smart::vector<const pcre_cache_entry*>().swap(m_overflow);
}

PCREglobals::~PCREglobals() {
m_overflow.clear();
onSessionExit();
}

void pcre_session_exit() {
s_pcre_globals->onSessionExit();
}

void PCREglobals::cleanupOnRequestEnd(const pcre_cache_entry* ent) {
Expand Down
1 change: 1 addition & 0 deletions hphp/runtime/base/preg.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class PCREglobals {
PCREglobals() { }
~PCREglobals();
void cleanupOnRequestEnd(const pcre_cache_entry* ent);
void onSessionExit();
// pcre ini_settings
int64_t m_preg_backtrace_limit;
int64_t m_preg_recursion_limit;
Expand Down
3 changes: 3 additions & 0 deletions hphp/runtime/base/program-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,9 @@ void hphp_session_exit() {
// reinitialize g_context here.
g_context.getCheck();

// Clean up pcre state at the end of the request.
pcre_session_exit();

mm.sweep();

// Destroy g_context again because ExecutionContext has
Expand Down
1 change: 1 addition & 0 deletions hphp/runtime/base/program-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class ExecutionContext;

void pcre_init();
void pcre_reinit();
void pcre_session_exit();
void hphp_process_init();
void hphp_session_init();

Expand Down
17 changes: 17 additions & 0 deletions hphp/test/slow/ext_preg/preg_cache_overflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$matches = 1;
for ($i=1 ; $i < 1050 ; $i++) {
$db_name = 'dbs.'.rand();

if (preg_match("/^dbs\.(\d+)$/", $db_name, $match)) {
$db_num = $match[1];
$printable_db_name = preg_replace('/' .$db_num.'/', '%d', $db_name);
if (!$printable_db_name) {
var_dump("preg_replace returned false");
break;
}
++$matches;
}
}
var_dump(sprintf("%d matches", $matches));
5 changes: 5 additions & 0 deletions hphp/test/slow/ext_preg/preg_cache_overflow.php.expectf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
string(14) "100000 matches"
string(14) "100000 matches"
string(14) "100000 matches"
string(14) "100000 matches"
string(14) "100000 matches"
1 change: 1 addition & 0 deletions hphp/test/slow/ext_preg/preg_cache_overflow.php.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-vEval.PCRETableSize=128 --count 5

0 comments on commit 968dfa2

Please sign in to comment.