BUG/MEDIUM: lua: Always init the lua stack before referencing the context
When a lua context is allocated, its stack must be initialized to NULL
before attaching it to its owner (task, stream or applet). Otherwise, if
the watchdog is fired before the stack is really created, that may lead to a
segfault because we try to dump the traceback of an uninitialized lua stack.
It is easy to trigger this bug if a lua script do a blocking call while
another thread try to initialize a new lua context. Because of the global
lua lock, the init is blocked before the stack creation. Of course, it only
happens if the script is executed in the shared global context.
This patch must be backported as far as 2.0.
(cherry picked from commit 1e8433f594de4b860e5205fdd6cb40d91ff58f17)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/hlua.c b/src/hlua.c
index 228bd67..79446f9 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -6275,6 +6275,7 @@
hlua = pool_alloc(pool_head_hlua);
if (!hlua)
WILL_LJMP(luaL_error(L, "Lua out of memory error."));
+ HLUA_INIT(hlua);
task = task_new(MAX_THREADS_MASK);
if (!task)
@@ -6315,11 +6316,15 @@
* Lua initialization cause 5% performances loss.
*/
if (!stream->hlua) {
- stream->hlua = pool_alloc(pool_head_hlua);
- if (!stream->hlua) {
+ struct hlua *hlua;
+
+ hlua = pool_alloc(pool_head_hlua);
+ if (!hlua) {
SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
return 0;
}
+ HLUA_INIT(hlua);
+ stream->hlua = hlua;
if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
return 0;
@@ -6448,11 +6453,15 @@
* Lua initialization cause 5% performances loss.
*/
if (!stream->hlua) {
- stream->hlua = pool_alloc(pool_head_hlua);
- if (!stream->hlua) {
+ struct hlua *hlua;
+
+ hlua = pool_alloc(pool_head_hlua);
+ if (!hlua) {
SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
return 0;
}
+ hlua->T = NULL;
+ stream->hlua = hlua;
if (!hlua_ctx_init(stream->hlua, stream->task, 0)) {
SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
return 0;
@@ -6743,12 +6752,16 @@
* Lua initialization cause 5% performances loss.
*/
if (!s->hlua) {
- s->hlua = pool_alloc(pool_head_hlua);
- if (!s->hlua) {
+ struct hlua *hlua;
+
+ hlua = pool_alloc(pool_head_hlua);
+ if (!hlua) {
SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
rule->arg.hlua_rule->fcn.name);
goto end;
}
+ HLUA_INIT(hlua);
+ s->hlua = hlua;
if (!hlua_ctx_init(s->hlua, s->task, 0)) {
SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
rule->arg.hlua_rule->fcn.name);