crash in ngx_add_timer

胡聪 (hucc) hucong.c at foxmail.com
Wed Apr 19 01:53:32 UTC 2017


Hi,


At the first glance, the ev->log should be assigned a valid value.




------------------ Original ------------------
From:  "Dk Jack";<dnj0496 at gmail.com>;
Send time: Wednesday, Apr 19, 2017 8:23 AM
To: "nginx-devel"<nginx-devel at nginx.org>; 

Subject:  Re: crash in ngx_add_timer



resending since the attachment which had the code didn't make it...

Dk.


#include <assert.h>
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>


static ngx_event_t ev1;
static ngx_event_t ev2;
static ngx_event_t ev3;


char ev1_data[] = "event1";
char ev2_data[] = "event2";
char ev3_data[] = "event3";


static ngx_int_t
ngx_test_handler(ngx_http_request_t *r)
{
  if (r->main->internal) {
    return NGX_DECLINED;
  }


  r->main->internal = 1;


  ngx_table_elt_t *h;
  h = ngx_list_push(&r->headers_out.headers);
  h->hash = 1;
  ngx_str_set(&h->key, "X-NGINX-Test");
  ngx_str_set(&h->value, "Hello World!");


  return NGX_DECLINED;
}


static void
ev1_handler(ngx_event_t* ev)
{
  ngx_log_error(NGX_LOG_ERR, ev->log, 0, "received event: %s", (char *) ev->data);
  ngx_add_timer(&ev1, 5);
}


static void
ev2_handler(ngx_event_t* ev)
{
  ngx_log_error(NGX_LOG_ERR, ev->log, 0, "received event: %s", (char *) ev->data);
  ngx_add_timer(&ev2, 6);
}


static void
ev3_handler(ngx_event_t* ev)
{
  ngx_log_error(NGX_LOG_ERR, ev->log, 0, "received event: %s", (char *) ev->data);
  ngx_add_timer(&ev3, 7);
}


static ngx_int_t
ngx_test_init (ngx_conf_t *cf)
{
  ngx_http_handler_pt *h;
  ngx_http_core_main_conf_t *cmcf;


  cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);


  if (h == NULL) {
    ngx_log_error(NGX_LOG_ERR, cf->log, 0, "Cannot retrieve Nginx access handler pointer");
    return NGX_ERROR;
  }


  *h = ngx_test_handler;
  ngx_log_error(NGX_LOG_INFO, cf->log, 0, "[test] Installed test handler");


  ev1.handler = ev1_handler;
  ev1.data = ev1_data;


  ev2.handler = ev2_handler;
  ev2.data = ev2_data;


  ev3.handler = ev3_handler;
  ev3.data = ev3_data;


  ngx_add_timer(&ev1, 5);
  ngx_add_timer(&ev2, 6);
  ngx_add_timer(&ev3, 7);


  return NGX_OK;
}


static char *
parse_test_configuration (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
  ngx_str_t *value;


  if (cf->args->nelts != 2) {
    ngx_log_error(NGX_LOG_ERR, cf->log, 0
                  , "[ss-config] invalid params for config_file. Expected 2 received %d"
                  , cf->args->nelts);
    return NGX_CONF_ERROR;
  }


  value = cf->args->elts;


  ngx_log_error(NGX_LOG_ERR, cf->log, 0, "file-path: %V", &value[1]);


  return NGX_CONF_OK;
}




static ngx_http_module_t ngx_test_module_ctx = {
  NULL,                                 /* preconfiguration */
  ngx_test_init,              					/* postconfiguration */
  NULL,                                 /* create main configuration */
  NULL,                                 /* init main configuration */
  NULL,                                 /* create server configuration */
  NULL,                                 /* merge server configuration */
  NULL,       												/* create location configuration */
  NULL         												/* merge location configuration */
};


static ngx_command_t ngx_test_module_cmds[] = {
  {
    ngx_string("config_file"),
    NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
    parse_test_configuration,
    0,
    0,
    NULL
  }
  , ngx_null_command
};


ngx_module_t nginx_test_module = {
  NGX_MODULE_V1,
  &ngx_test_module_ctx,  					 /* module context */
  ngx_test_module_cmds,  					 /* module directives */
  NGX_HTTP_MODULE,                 /* module type */
  NULL,                            /* init master */
  NULL,                            /* init module */
  NULL,                            /* init process */
  NULL,                            /* init thread */
  NULL,                            /* exit thread */
  NULL,                            /* exit process */
  NULL,                            /* exit master */
  NGX_MODULE_V1_PADDING
};



On Tue, Apr 18, 2017 at 5:12 PM, Dk Jack <dnj0496 at gmail.com> wrote:
Hi,I am new to NGinx modules and I trying to write an nginx module. I am having an issue
where my module crashes when adding multiple timers. The first timer adds fine.
However, when I go to add the second timer, it crashes. I've simplified the code to a very
basic module (which is attached to this email). The backtrace is provided as well.
Do I need to do some sort of initialization before I add multiple timers. Any advice/help
is appreciated. Thanks.


Dk.


Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x000000000040d68b in ngx_rbtree_insert (tree=0x69dda0 <ngx_event_timer_rbtree>,
    node=node at entry=0x69db08 <ev2+40>) at src/core/ngx_rbtree.c:44
#2  0x000000000046ceb2 in ngx_event_add_timer (timer=6, ev=0x69dae0 <ev2>)
    at src/event/ngx_event_timer.h:84
#3  ngx_test_init (cf=<optimized out>)
    at /home/ubuntu/git/mitigator/nginx/build/../modules/nginx_mitigator_module/platform/nginx-test-module.c:81
#4  0x0000000000424e83 in ngx_http_block (cf=0x7fffffffe0a0, cmd=<optimized out>,
    conf=<optimized out>) at src/http/ngx_http.c:315
#5  0x000000000041446b in ngx_conf_handler (last=1, cf=0x7fffffffe0a0)
    at src/core/ngx_conf_file.c:427
#6  ngx_conf_parse (cf=cf at entry=0x7fffffffe0a0, filename=filename at entry=0x6ad570)
    at src/core/ngx_conf_file.c:283
#7  0x0000000000411e1a in ngx_init_cycle (old_cycle=old_cycle at entry=0x7fffffffe260)
    at src/core/ngx_cycle.c:268
#8  0x0000000000404208 in main (argc=<optimized out>, argv=<optimized out>) at src/core/nginx.c:268
(gdb)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20170419/e8284948/attachment-0001.html>


More information about the nginx-devel mailing list