comparison src/core/ngx_regex.c @ 4423:196c3dacff0d

Fixed memory leak on HUP signal when PCRE JIT was used. The PCRE JIT compiler uses mmap to allocate memory for its executable codes, so we have to explicitly call the pcre_free_study() function to free this memory.
author Valentin Bartenev <vbart@nginx.com>
date Mon, 30 Jan 2012 12:53:57 +0000
parents 23ea4e72c85a
children 6e1a48bcf915
comparison
equal deleted inserted replaced
4422:b1a9d725ab69 4423:196c3dacff0d
14 } ngx_regex_conf_t; 14 } ngx_regex_conf_t;
15 15
16 16
17 static void * ngx_libc_cdecl ngx_regex_malloc(size_t size); 17 static void * ngx_libc_cdecl ngx_regex_malloc(size_t size);
18 static void ngx_libc_cdecl ngx_regex_free(void *p); 18 static void ngx_libc_cdecl ngx_regex_free(void *p);
19 #if (NGX_HAVE_PCRE_JIT)
20 static void ngx_pcre_free_studies(void *data);
21 #endif
19 22
20 static ngx_int_t ngx_regex_module_init(ngx_cycle_t *cycle); 23 static ngx_int_t ngx_regex_module_init(ngx_cycle_t *cycle);
21 24
22 static void *ngx_regex_create_conf(ngx_cycle_t *cycle); 25 static void *ngx_regex_create_conf(ngx_cycle_t *cycle);
23 static char *ngx_regex_init_conf(ngx_cycle_t *cycle, void *conf); 26 static char *ngx_regex_init_conf(ngx_cycle_t *cycle, void *conf);
272 { 275 {
273 return; 276 return;
274 } 277 }
275 278
276 279
280 #if (NGX_HAVE_PCRE_JIT)
281
282 static void
283 ngx_pcre_free_studies(void *data)
284 {
285 ngx_list_t *studies = data;
286
287 ngx_uint_t i;
288 ngx_list_part_t *part;
289 ngx_regex_elt_t *elts;
290
291 part = &studies->part;
292 elts = part->elts;
293
294 for (i = 0 ; /* void */ ; i++) {
295
296 if (i >= part->nelts) {
297 if (part->next == NULL) {
298 break;
299 }
300
301 part = part->next;
302 elts = part->elts;
303 i = 0;
304 }
305
306 if (elts[i].regex->extra != NULL) {
307 pcre_free_study(elts[i].regex->extra);
308 }
309 }
310 }
311
312 #endif
313
314
277 static ngx_int_t 315 static ngx_int_t
278 ngx_regex_module_init(ngx_cycle_t *cycle) 316 ngx_regex_module_init(ngx_cycle_t *cycle)
279 { 317 {
280 int opt; 318 int opt;
281 const char *errstr; 319 const char *errstr;
285 323
286 opt = 0; 324 opt = 0;
287 325
288 #if (NGX_HAVE_PCRE_JIT) 326 #if (NGX_HAVE_PCRE_JIT)
289 { 327 {
290 ngx_regex_conf_t *rcf; 328 ngx_regex_conf_t *rcf;
329 ngx_pool_cleanup_t *cln;
291 330
292 rcf = (ngx_regex_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_regex_module); 331 rcf = (ngx_regex_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_regex_module);
293 332
294 if (rcf->pcre_jit) { 333 if (rcf->pcre_jit) {
295 opt = PCRE_STUDY_JIT_COMPILE; 334 opt = PCRE_STUDY_JIT_COMPILE;
335
336 /*
337 * The PCRE JIT compiler uses mmap for its executable codes, so we
338 * have to explicitly call the pcre_free_study() function to free
339 * this memory.
340 */
341
342 cln = ngx_pool_cleanup_add(cycle->pool, 0);
343 if (cln == NULL) {
344 return NGX_ERROR;
345 }
346
347 cln->handler = ngx_pcre_free_studies;
348 cln->data = ngx_pcre_studies;
296 } 349 }
297 } 350 }
298 #endif 351 #endif
299 352
300 ngx_regex_malloc_init(cycle->pool); 353 ngx_regex_malloc_init(cycle->pool);