From dfc73e7475ba36998956da9b2f8ce58f8f2482d8 Mon Sep 17 00:00:00 2001 From: Michael Santos Date: Thu, 18 Mar 2010 20:08:10 -0400 Subject: Fix check for compile workspace overflow Patch from: http://vcs.pcre.org/viewvc/code/trunk/pcre_compile.c?r1=504&r2=505&view=patch Test case: N = 819, re:compile([lists:duplicate(N, $(), lists:duplicate(N, $))]). Compiling large regular expressions could overflow the workspace buffer. Modify the test to check for a value smaller than the buffer size. --- erts/emulator/pcre/pcre_compile.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/erts/emulator/pcre/pcre_compile.c b/erts/emulator/pcre/pcre_compile.c index 29743362d4..9508c5a697 100644 --- a/erts/emulator/pcre/pcre_compile.c +++ b/erts/emulator/pcre/pcre_compile.c @@ -92,6 +92,11 @@ is 4 there is plenty of room. */ #define COMPILE_WORK_SIZE (4096) +/* The overrun tests check for a slightly smaller size so that they detect the +overrun before it actually does run off the end of the data block. */ + +#define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100) + /* Table for handling escaped characters in the range '0'-'z'. Positive returns are simple data values; negative values are for special things like \d and so @@ -2445,7 +2450,7 @@ for (;; ptr++) #ifdef DEBUG if (code > cd->hwm) cd->hwm = code; /* High water info */ #endif - if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */ + if (code > cd->start_workspace + WORK_SIZE_CHECK) /* Check for overrun */ { *errorcodeptr = ERR52; goto FAILED; @@ -2494,7 +2499,7 @@ for (;; ptr++) /* In the real compile phase, just check the workspace used by the forward reference list. */ - else if (cd->hwm > cd->start_workspace + COMPILE_WORK_SIZE) + else if (cd->hwm > cd->start_workspace + WORK_SIZE_CHECK) { *errorcodeptr = ERR52; goto FAILED; -- cgit v1.2.3 From 1985e9d11c9cf409a935e648480bd275214adc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Wed, 14 Apr 2010 08:00:29 +0200 Subject: MacOS X: Boost default stack size The default stack size on MacOS X for the SMP emulator is too small to support all uses of pcre. For example, the following expression N = 819, re:compile([lists:duplicate(N, $(), lists:duplicate(N, $))]). will cause a stack overflow. By bisection of different values for the +sss option, I found that 166 is the smallest value that avoids the crash. Round that up to 256 to give a nice, round power of 2 and a resonable safety margin. Use that value as a default stack size on MacOS X only. --- erts/emulator/beam/erl_init.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/erts/emulator/beam/erl_init.c b/erts/emulator/beam/erl_init.c index e97ab328cd..41cfcd74aa 100644 --- a/erts/emulator/beam/erl_init.c +++ b/erts/emulator/beam/erl_init.c @@ -819,7 +819,13 @@ erl_start(int argc, char **argv) if (erts_sys_getenv("ERL_THREAD_POOL_SIZE", envbuf, &envbufsz) == 0) { async_max_threads = atoi(envbuf); } - + +#if (defined(__APPLE__) && defined(__MACH__)) || defined(__DARWIN__) + /* + * The default stack size on MacOS X is too small for pcre. + */ + erts_sched_thread_suggested_stack_size = 256; +#endif #ifdef DEBUG verbose = DEBUG_DEFAULT; -- cgit v1.2.3 From f43718785523cb4635d1c22a78a7620ed5e86609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Fri, 19 Mar 2010 13:29:33 +0100 Subject: re_SUITE: Add pcre_compile_workspace_overflow/1 --- lib/stdlib/test/re_SUITE.erl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/stdlib/test/re_SUITE.erl b/lib/stdlib/test/re_SUITE.erl index 02683f9f1a..4e78568a87 100644 --- a/lib/stdlib/test/re_SUITE.erl +++ b/lib/stdlib/test/re_SUITE.erl @@ -18,12 +18,12 @@ %% -module(re_SUITE). --export([all/1, pcre/1,compile_options/1,run_options/1,combined_options/1,replace_autogen/1,global_capture/1,replace_input_types/1,replace_return/1,split_autogen/1,split_options/1,split_specials/1,error_handling/1,pcre_cve_2008_2371/1]). +-export([all/1, pcre/1,compile_options/1,run_options/1,combined_options/1,replace_autogen/1,global_capture/1,replace_input_types/1,replace_return/1,split_autogen/1,split_options/1,split_specials/1,error_handling/1,pcre_cve_2008_2371/1,pcre_compile_workspace_overflow/1]). -include("test_server.hrl"). -include_lib("kernel/include/file.hrl"). -all(suite) -> [pcre,compile_options,run_options,combined_options,replace_autogen,global_capture,replace_input_types,replace_return,split_autogen,split_options,split_specials,error_handling,pcre_cve_2008_2371]. +all(suite) -> [pcre,compile_options,run_options,combined_options,replace_autogen,global_capture,replace_input_types,replace_return,split_autogen,split_options,split_specials,error_handling,pcre_cve_2008_2371,pcre_compile_workspace_overflow]. pcre(doc) -> ["Run all applicable tests from the PCRE testsuites."]; @@ -544,3 +544,11 @@ pcre_cve_2008_2371(Config) when is_list(Config) -> %% Make sure it doesn't crash the emulator. re:compile(<<"(?i)[\xc3\xa9\xc3\xbd]|[\xc3\xa9\xc3\xbdA]">>, [unicode]), ok. + +pcre_compile_workspace_overflow(doc) -> + "Patch from http://vcs.pcre.org/viewvc/code/trunk/pcre_compile.c?r1=504&r2=505&view=patch"; +pcre_compile_workspace_overflow(Config) when is_list(Config) -> + N = 819, + ?line {error,{"internal error: overran compiling workspace",799}} = + re:compile([lists:duplicate(N, $(), lists:duplicate(N, $))]), + ok. -- cgit v1.2.3