From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id E91ED4A153B; Mon, 5 Jun 2023 18:13:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E91ED4A153B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1685977985; bh=sV2DuJyWZz1TfQC0au9tPgQQ24JcM+VgSe9IkXaIwsU=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sgnuIpXvSV5cIsCXVASDBvCOHw01KcFvDFxfQM8ozJQ2CEsXphlSsdNlHXj2Mi0uw at482kikhKVwa3kTW1vhczoXaJaeu42ygj2h3BlyiVidogzcrjXm54MsllJitWgM2Z yfNEtkhhYgfTuXmUzwDsMBzUmJMgSPmEK0LTa92k= Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id A08224A153B for ; Mon, 5 Jun 2023 18:13:03 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A08224A153B Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-4f4c264f6c6so6308478e87.3 for ; Mon, 05 Jun 2023 08:13:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685977983; x=1688569983; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=q/21q4IuGdsdsR8+wjGkQ6v9GHqKU4oQ5Ls7mEy4Cyc=; b=DH3UCWC7wOzQd7kzlTEIzatLCkXiJ7ZqWEcOA8J6oVz2Tzcq17vkB009q2sUNfbSU6 mpiW1kWUrfDKDfyOaBVvZDk8g/3sAvaN0GJaAyKCEJSKVb6rv1OC79OoojiIl3x4KZBj HOM57T7zCkBSf3DV41eV6s7HM6Vghs4MStManhTV+sMbKorVyy2XQtl3s/UOYZY/Bswz pf1kE3VH+RVY87/hNsbk+Z/ZE1knBl3kWpZ3fUEAt/GDekqDQsERN3+iB/GLl9dkZftw Gx1aPcXu0bFlG0YhVhhEJwNDGlgIzA7cWURXUeiDqmBPghRENmxLbFVFcz30qamk/Da0 mafQ== X-Gm-Message-State: AC+VfDyrYiKCh3FQjAmFUVrRTFDEcRgU4It03RoHPErzrQoAwWRvQe8o O+hV9cDtOF/iT2+XWdwraZ7oT7DwnthmuA== X-Google-Smtp-Source: ACHHUZ7sXfPzW/nk1iGb40hmPoKcHegOGieA3kwiI1+Z1ghN8FURzWz+rAdZbUehQeZNQvTwvphWUg== X-Received: by 2002:ac2:59db:0:b0:4f4:dd5d:f3e4 with SMTP id x27-20020ac259db000000b004f4dd5df3e4mr5141363lfn.26.1685977982226; Mon, 05 Jun 2023 08:13:02 -0700 (PDT) Received: from fckxorg.mail.msk ([2a00:1148:b0ba:16:a3e8:bdc1:dbed:dbc8]) by smtp.gmail.com with ESMTPSA id m21-20020ac24255000000b004f612dddaa8sm1148017lfl.101.2023.06.05.08.13.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jun 2023 08:13:01 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Mon, 5 Jun 2023 18:12:57 +0300 Message-Id: <20230605151257.124621-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit] sysprof: fix crash during FFUNC stream X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Sometimes, the Lua stack can be inconsistent during the FFUNC execution, which may lead to a sysprof crash during the stack unwinding. This patch replaces the `top_frame` property of `global_State` with `lj_sysprof_topframe` structure, which contains `top_frame` and `ffid` properties. `ffid` property makes sense only when the LuaJIT VM state is set to `FFUNC`. That property is set to the ffid of the fast function that VM is about to execute. In the same time, `top_frame` property is not updated now, so the top frame of the Lua stack can be streamed based on the ffid, and the rest of the Lua stack can be streamed as usual. Resolves tarantool/tarantool#8594 --- Branch: https://github.com/tarantool/luajit/tree/fckxorg/gh-8594-sysprof-ffunc-crash PR: https://github.com/tarantool/tarantool/pull/8737 NB: coverage job is failing in tarantool C, but it is irrelevent to the patch. That fail is caused by the symtab update, which will be fixed in another patch. src/lj_obj.h | 7 +++- src/lj_sysprof.c | 26 +++++++++++--- src/vm_x64.dasc | 21 +++++++++-- src/vm_x86.dasc | 22 +++++++++--- .../gh-8594-sysprof-ffunc-crash.test.lua | 35 +++++++++++++++++++ 5 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 test/tarantool-tests/gh-8594-sysprof-ffunc-crash.test.lua diff --git a/src/lj_obj.h b/src/lj_obj.h index 45507e0d..186433a3 100644 --- a/src/lj_obj.h +++ b/src/lj_obj.h @@ -598,6 +598,11 @@ enum { GCSmax }; +struct lj_sysprof_topframe { + TValue *top_frame; /* Top frame for sysprof. */ + uint8_t ffid; /* FFID of the fast function VM is about to execute. */ +}; + typedef struct GCState { GCSize total; /* Memory currently allocated. */ GCSize threshold; /* Memory threshold. */ @@ -675,7 +680,7 @@ typedef struct global_State { MRef ctype_state; /* Pointer to C type state. */ GCRef gcroot[GCROOT_MAX]; /* GC roots. */ #ifdef LJ_HASSYSPROF - TValue *top_frame; /* Top frame for sysprof. */ + struct lj_sysprof_topframe top_frame_info; /* Top frame info for sysprof. */ #endif } global_State; diff --git a/src/lj_sysprof.c b/src/lj_sysprof.c index 2e9ed9b3..0a341e16 100644 --- a/src/lj_sysprof.c +++ b/src/lj_sysprof.c @@ -109,6 +109,12 @@ static void stream_epilogue(struct sysprof *sp) lj_wbuf_addbyte(&sp->out, LJP_EPILOGUE_BYTE); } +static void stream_ffunc_impl(struct lj_wbuf *buf, uint8_t ffid) +{ + lj_wbuf_addbyte(buf, LJP_FRAME_FFUNC); + lj_wbuf_addu64(buf, ffid); +} + static void stream_lfunc(struct lj_wbuf *buf, const GCfunc *func) { lua_assert(isluafunc(func)); @@ -129,8 +135,7 @@ static void stream_cfunc(struct lj_wbuf *buf, const GCfunc *func) static void stream_ffunc(struct lj_wbuf *buf, const GCfunc *func) { lua_assert(isffunc(func)); - lj_wbuf_addbyte(buf, LJP_FRAME_FFUNC); - lj_wbuf_addu64(buf, func->c.ffid); + stream_ffunc_impl(buf, func->c.ffid); } static void stream_frame_lua(struct lj_wbuf *buf, const cTValue *frame) @@ -148,7 +153,7 @@ static void stream_frame_lua(struct lj_wbuf *buf, const cTValue *frame) lua_assert(0); } -static void stream_backtrace_lua(struct sysprof *sp) +static void stream_backtrace_lua(struct sysprof *sp, uint32_t vmstate) { global_State *g = sp->g; struct lj_wbuf *buf = &sp->out; @@ -158,8 +163,19 @@ static void stream_backtrace_lua(struct sysprof *sp) lua_assert(g != NULL); L = gco2th(gcref(g->cur_L)); lua_assert(L != NULL); + /* + ** Lua stack may be inconsistent during the execution of a + ** fast-function, so instead of updating the `top_frame` for + ** it, its `ffid` is set instead. The first frame on the + ** result stack is streamed manually, and the rest of the + ** stack is streamed based on the previous `top_frame` value. + */ + if (vmstate == LJ_VMST_FFUNC) { + uint8_t ffid = g->top_frame_info.ffid; + stream_ffunc_impl(buf, ffid); + } - top_frame = g->top_frame - 1; //(1 + LJ_FR2) + top_frame = g->top_frame_info.top_frame - 1; bot = tvref(L->stack) + LJ_FR2; /* Traverse frames backwards */ @@ -234,7 +250,7 @@ static void stream_trace(struct sysprof *sp, uint32_t vmstate) static void stream_guest(struct sysprof *sp, uint32_t vmstate) { lj_wbuf_addbyte(&sp->out, (uint8_t)vmstate); - stream_backtrace_lua(sp); + stream_backtrace_lua(sp, vmstate); stream_backtrace_host(sp); } diff --git a/src/vm_x64.dasc b/src/vm_x64.dasc index 7b04b928..3a35b9f7 100644 --- a/src/vm_x64.dasc +++ b/src/vm_x64.dasc @@ -353,14 +353,29 @@ |// it syncs with the BASE register only when the control is passed to |// user code. So we need to sync the BASE on each vmstate change to |// keep it consistent. +|// The only execption are FFUNCs because sometimes even internal BASE +|// stash is inconsistent for them. To address that issue, their ffid +|// is stashed instead, so the corresponding frame can be streamed +|// manually. |.macro set_vmstate_sync_base, st |.if LJ_HASSYSPROF | set_vmstate INTERP // Guard for non-atomic VM context restoration -| mov qword [DISPATCH+DISPATCH_GL(top_frame)], BASE +| mov qword [DISPATCH+DISPATCH_GL(top_frame_info.top_frame)], BASE |.endif | set_vmstate st |.endmacro | +|.macro set_vmstate_ffunc +|.if LJ_HASSYSPROF +| set_vmstate INTERP +| mov TMPR, [BASE - 16] +| cleartp LFUNC:TMPR +| mov r10b, LFUNC:TMPR->ffid // r10b is the byte-sized part of TMPR +| mov byte [DISPATCH+DISPATCH_GL(top_frame_info.ffid)], r10b +|.endif +| set_vmstate FFUNC +|.endmacro +| |// Uses TMPRd (r10d). |.macro save_vmstate |.if not WIN @@ -376,7 +391,7 @@ | set_vmstate INTERP | mov TMPR, SAVE_L | mov TMPR, L:TMPR->base -| mov qword [DISPATCH+DISPATCH_GL(top_frame)], TMPR +| mov qword [DISPATCH+DISPATCH_GL(top_frame_info.top_frame)], TMPR |.endif | mov TMPRd, SAVE_VMSTATE | mov dword [DISPATCH+DISPATCH_GL(vmstate)], TMPRd @@ -1188,7 +1203,7 @@ static void build_subroutines(BuildCtx *ctx) | |.macro .ffunc, name |->ff_ .. name: - | set_vmstate_sync_base FFUNC + | set_vmstate_ffunc |.endmacro | |.macro .ffunc_1, name diff --git a/src/vm_x86.dasc b/src/vm_x86.dasc index bd1e940e..fabeec9f 100644 --- a/src/vm_x86.dasc +++ b/src/vm_x86.dasc @@ -451,14 +451,28 @@ |// it syncs with the BASE register only when the control is passed to |// user code. So we need to sync the BASE on each vmstate change to |// keep it consistent. +|// The only execption are FFUNCs because sometimes even internal BASE +|// stash is inconsistent for them. To address that issue, their ffid +|// is stashed instead, so the corresponding frame can be streamed +|// manually. |.macro set_vmstate_sync_base, st |.if LJ_HASSYSPROF | set_vmstate INTERP // Guard for non-atomic VM context restoration -| mov dword [DISPATCH+DISPATCH_GL(top_frame)], BASE +| mov dword [DISPATCH+DISPATCH_GL(top_frame_info.top_frame)], BASE |.endif | set_vmstate st |.endmacro | +|.macro set_vmstate_ffunc +|.if LJ_HASSYSPROF +| set_vmstate INTERP +| mov LFUNC:XCHGd, [BASE - 8] +| mov r11b, LFUNC:XCHGd->ffid // r11b is the byte-sized part of XCHGd +| mov byte [DISPATCH+DISPATCH_GL(top_frame_info.ffid)], r11b +|.endif +| set_vmstate FFUNC +|.endmacro +| |// Uses spilled ecx on x86 or XCHGd (r11d) on x64. |.macro save_vmstate |.if not WIN @@ -485,7 +499,7 @@ |.if LJ_HASSYSPROF | mov ecx, SAVE_L | mov ecx, L:ecx->base -| mov dword [DISPATCH+DISPATCH_GL(top_frame)], ecx +| mov dword [DISPATCH+DISPATCH_GL(top_frame_info.top_frame)], ecx |.endif | mov ecx, SAVE_VMSTATE | mov dword [DISPATCH+DISPATCH_GL(vmstate)], ecx @@ -494,7 +508,7 @@ |.if LJ_HASSYSPROF | mov XCHGd, SAVE_L | mov XCHGd, L:XCHGd->base -| mov dword [DISPATCH+DISPATCH_GL(top_frame)], XCHGd +| mov dword [DISPATCH+DISPATCH_GL(top_frame_info.top_frame)], XCHGd |.endif | mov XCHGd, SAVE_VMSTATE | mov dword [DISPATCH+DISPATCH_GL(vmstate)], XCHGd @@ -1488,7 +1502,7 @@ static void build_subroutines(BuildCtx *ctx) | |.macro .ffunc, name |->ff_ .. name: - | set_vmstate_sync_base FFUNC + | set_vmstate_ffunc |.endmacro | |.macro .ffunc_1, name diff --git a/test/tarantool-tests/gh-8594-sysprof-ffunc-crash.test.lua b/test/tarantool-tests/gh-8594-sysprof-ffunc-crash.test.lua new file mode 100644 index 00000000..5d4766e0 --- /dev/null +++ b/test/tarantool-tests/gh-8594-sysprof-ffunc-crash.test.lua @@ -0,0 +1,35 @@ +local tap = require('tap') +local test = tap.test('gh-8594-sysprof-ffunc-crash'):skipcond({ + ["Sysprof is implemented for x86_64 only"] = jit.arch ~= "x86" and + jit.arch ~= "x64", + ["Sysprof is implemented for Linux only"] = jit.os ~= "Linux", +}) + +test:plan(1) + +jit.off() +-- XXX: Run JIT tuning functions in a safe frame to avoid errors +-- thrown when LuaJIT is compiled with JIT engine disabled. +pcall(jit.flush) + +local profilename = require("utils").profilename +local TMP_BINFILE = profilename("sysprofdata.tmp.bin") + +local res, err = misc.sysprof.start{ + mode = 'C', + interval = 1, + path = TMP_BINFILE, +} +assert(res, err) + +for i = 1, 1e6 do + tostring(i) +end + +res, err = misc.sysprof.stop() +assert(res, err) + +test:ok(true, 'sysprof finished successfully') + +os.remove(TMP_BINFILE) +os.exit(test:check() and 0 or 1) -- 2.40.1