Skip to content

Add jit_stack_size option #782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
dd7128f
WIP: Add jit_stack_size option
alubbe May 31, 2016
2a47582
Fix build, add guards and extract magic numbers
alubbe Jun 2, 2016
b74c736
Add ffi implementation
alubbe Jun 11, 2016
3fe572b
Add non-ffi implementation
alubbe Jun 11, 2016
ff91139
Add tests
alubbe Jun 11, 2016
188935f
Fix segfault by introducing a memory leak
alubbe Jun 11, 2016
5f5b6ca
Fix compiler warnings
alubbe Jun 11, 2016
b7c71f5
Update 062-count test to reflect that ngx.re now has 6 methods
alubbe Jun 11, 2016
5ff6a75
Add back empty space that was removed by editor settings
alubbe Jun 11, 2016
1529aaa
Default lmcf->jit_stack to NULL, fix memory leak and add error messages
alubbe Jun 15, 2016
87838da
Expose ngx_http_lua_set_jit_stack_size so that ffi can call it
alubbe Jun 15, 2016
db057fb
Remove duplicate # blocks
alubbe Jun 15, 2016
7c3f7d8
Disallow the changing of jit_stack_size once regex cache is populated
alubbe Jun 30, 2016
a9b52cb
Add test "passing unknown options to ngx.re.opt throws an error"
alubbe Jul 7, 2016
c10f4d5
Style: Align strings and keep line length under 80 chars
alubbe Nov 13, 2016
ef13eb2
Use *_by_lua_block in new tests
alubbe Nov 13, 2016
2c261c9
Incorporate style feedback
alubbe Nov 13, 2016
2337c8c
Add more pcre jit guards
alubbe Nov 13, 2016
15a7a0d
Avoid magic numbers
alubbe Nov 13, 2016
aafc6e1
Pass a length to ngx_strncmp
alubbe Nov 13, 2016
87a8b61
travis
alubbe Nov 13, 2016
9498bf4
Add documentation for ngx.re.opt
alubbe Nov 13, 2016
1d2e80c
Avoid more magic numbers
alubbe Nov 14, 2016
a56355a
Add documentation to wiki file
alubbe Nov 14, 2016
7e858bd
Update tests to check ngx.re.opt in content_by_lua
alubbe Nov 14, 2016
d649809
Fix return value of ngx_http_lua_ngx_re_opt
alubbe Nov 14, 2016
d947745
Call ngx_http_lua_pcre_malloc_{init,done} around pcre_{malloc,free}
alubbe Nov 15, 2016
d8c8968
Mark ngx_http_lua_set_jit_stack_size as ffi
alubbe Nov 15, 2016
cad4ee3
Test 33: Use grep_error_log to pass the second test iteration
alubbe Nov 22, 2016
becfeda
Simplify test 33
alubbe Nov 22, 2016
f1001a9
Fix test 33
alubbe Nov 22, 2016
cd9bb70
Repair test 33
alubbe Dec 16, 2016
e024b8f
Repair rebase artifacts
alubbe Dec 16, 2016
4c735a5
Remove Lua-5.1 api
alubbe Dec 16, 2016
c4c74de
Remove temporary file
alubbe Dec 16, 2016
3b2356c
Remove check that no regexs have been compiled
alubbe Dec 19, 2016
89318e5
Guard against 'sd' being NULL
alubbe Jan 9, 2017
9a00386
Return error in ngx_http_lua_ffi_set_jit_stack_size if no JIT is avai…
alubbe Jan 9, 2017
bbcc991
Spacing
alubbe Jan 12, 2017
cc6564b
Return NGX_DECLINED when pcre jit is not found
alubbe Jan 12, 2017
5a6cd87
Pass error messages from C to Lua
alubbe Apr 14, 2017
e40dd9a
Add missing pointer declaration
alubbe Apr 14, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/ngx_http_lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
#include <lauxlib.h>


#if (NGX_PCRE)

#include <pcre.h>

#if (PCRE_MAJOR > 8) || (PCRE_MAJOR == 8 && PCRE_MINOR >= 21)
# define LUA_HAVE_PCRE_JIT 1
#else
# define LUA_HAVE_PCRE_JIT 0
#endif

#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we define these macros in this common.h header that is included in all the other compilation units (directly or indirectly), we should remove the duplicate definitions from regex.c I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.



#if !defined(nginx_version) || (nginx_version < 1006000)
#error at least nginx 1.6.0 is required but found an older version
#endif
Expand Down Expand Up @@ -168,6 +181,11 @@ struct ngx_http_lua_main_conf_s {
ngx_int_t regex_cache_entries;
ngx_int_t regex_cache_max_entries;
ngx_int_t regex_match_limit;

#if (LUA_HAVE_PCRE_JIT)
pcre_jit_stack *jit_stack;
#endif

#endif

ngx_array_t *shm_zones; /* of ngx_shm_zone_t* */
Expand Down
82 changes: 71 additions & 11 deletions src/ngx_http_lua_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@
#include "ngx_http_lua_script.h"
#include "ngx_http_lua_pcrefix.h"
#include "ngx_http_lua_util.h"
#include <pcre.h>


#if (PCRE_MAJOR > 8) || (PCRE_MAJOR == 8 && PCRE_MINOR >= 21)
# define LUA_HAVE_PCRE_JIT 1
#else
# define LUA_HAVE_PCRE_JIT 0
#endif


#if (PCRE_MAJOR >= 6)
Expand All @@ -42,6 +34,8 @@

#define NGX_LUA_RE_DFA_MODE_WORKSPACE_COUNT (100)

#define NGX_LUA_RE_MIN_JIT_STACK_SIZE 32 * 1024


typedef struct {
#ifndef NGX_LUA_NO_FFI_API
Expand Down Expand Up @@ -364,6 +358,10 @@ ngx_http_lua_ngx_re_match_helper(lua_State *L, int wantcaps)

sd = pcre_study(re_comp.regex, PCRE_STUDY_JIT_COMPILE, &msg);

if (sd && lmcf->jit_stack) {
pcre_assign_jit_stack(sd, NULL, lmcf->jit_stack);
}

ngx_http_lua_pcre_malloc_done(old_pool);

# if (NGX_DEBUG)
Expand Down Expand Up @@ -826,6 +824,10 @@ ngx_http_lua_ngx_re_gmatch(lua_State *L)

sd = pcre_study(re_comp.regex, PCRE_STUDY_JIT_COMPILE, &msg);

if (sd && lmcf->jit_stack) {
pcre_assign_jit_stack(sd, NULL, lmcf->jit_stack);
}

ngx_http_lua_pcre_malloc_done(old_pool);

# if (NGX_DEBUG)
Expand Down Expand Up @@ -1922,6 +1924,60 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)
}


ngx_int_t
ngx_http_lua_ffi_set_jit_stack_size(int size, u_char *errstr,
size_t *errstr_size)
{
#if LUA_HAVE_PCRE_JIT

ngx_http_lua_main_conf_t *lmcf;
ngx_pool_t *pool, *old_pool;

lmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_lua_module);

if (size < NGX_LUA_RE_MIN_JIT_STACK_SIZE) {
size = NGX_LUA_RE_MIN_JIT_STACK_SIZE;
}

pool = lmcf->pool;

dd("server pool %p", lmcf->pool);

if (lmcf->jit_stack) {
old_pool = ngx_http_lua_pcre_malloc_init(pool);

pcre_jit_stack_free(lmcf->jit_stack);

ngx_http_lua_pcre_malloc_done(old_pool);
}

old_pool = ngx_http_lua_pcre_malloc_init(pool);

lmcf->jit_stack = pcre_jit_stack_alloc(NGX_LUA_RE_MIN_JIT_STACK_SIZE,
size);

ngx_http_lua_pcre_malloc_done(old_pool);

if (lmcf->jit_stack == NULL) {
*errstr_size = ngx_snprintf(errstr, *errstr_size,
"pcre jit stack allocation failed")
- errstr;
return NGX_ERROR;
}

return NGX_OK;

#else /* LUA_HAVE_PCRE_JIT */

*errstr_size = ngx_snprintf(errstr, *errstr_size,
"no pcre jit support found") - errstr;
return NGX_ERROR;

#endif /* LUA_HAVE_PCRE_JIT */
}


void
ngx_http_lua_inject_regex_api(lua_State *L)
{
Expand Down Expand Up @@ -2170,6 +2226,9 @@ ngx_http_lua_ffi_compile_regex(const unsigned char *pat, size_t pat_len,
goto error;
}

lmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_lua_module);

#if (LUA_HAVE_PCRE_JIT)

if (flags & NGX_LUA_RE_MODE_JIT) {
Expand Down Expand Up @@ -2205,10 +2264,11 @@ ngx_http_lua_ffi_compile_regex(const unsigned char *pat, size_t pat_len,
ngx_http_lua_pcre_malloc_done(old_pool);
}

#endif /* LUA_HAVE_PCRE_JIT */
if (sd && lmcf->jit_stack) {
pcre_assign_jit_stack(sd, NULL, lmcf->jit_stack);
}

lmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_lua_module);
#endif /* LUA_HAVE_PCRE_JIT */

if (sd && lmcf && lmcf->regex_match_limit > 0) {
sd->flags |= PCRE_EXTRA_MATCH_LIMIT;
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_lua_regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#if (NGX_PCRE)
void ngx_http_lua_inject_regex_api(lua_State *L);
ngx_int_t ngx_http_lua_ffi_set_jit_stack_size(int size, u_char *errstr,
size_t *errstr_size);
#endif


Expand Down