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 5ABA0CE9A65; Mon, 11 Mar 2024 13:41:06 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5ABA0CE9A65 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1710153666; bh=Ddg83MLTy87QIQpw3OhkwMC9FJdy/nRSYJnnAD14HlI=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=DtdRpWuJ+lZJv0X5avvyGNh45EZqr4b4LorUivnLpa81RDxEshFHqMuLvUqdhoo4U wzqL8nA6LJI7CMFwNQ+fNpBIAr5yljCpYWfHTk035J2vmMqX95JhmbnVdtTdgEH6JJ Xx98TwJEwcZoJc8lIsUmy88cx5EiJdAVOCmbHHs0= Received: from smtp50.i.mail.ru (smtp50.i.mail.ru [95.163.41.92]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 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 1B3AA71DF5A for ; Mon, 11 Mar 2024 13:41:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1B3AA71DF5A Received: by smtp50.i.mail.ru with esmtpa (envelope-from ) id 1rjd5Z-000000030lF-3byI; Mon, 11 Mar 2024 13:41:02 +0300 To: Maxim Kokryashkin , Sergey Bronnikov Date: Mon, 11 Mar 2024 13:37:01 +0300 Message-ID: <20240311103701.24502-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD987C0EE6E7F0A597D173D0CE202DA28435F3B31DD656B8ACF182A05F538085040AE09584D12ED462AA6D5EE0DB6E1EC8D0457E0C12425078250CE43922ECA5D9B7BC1A8A20AE16A52 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7387B291F9AC4D188EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637E2B4A35C1F0E329C8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D87FE53F9166468068D33EEAB74C8FB72054EA254FAB8D6F89CC7F00164DA146DAFE8445B8C89999728AA50765F79006370BDB19F53EE528DD389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8ED96AA85C75E140D117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CFC302137B26C5D76ABA3038C0950A5D36C8A9BA7A39EFB766D91E3A1F190DE8FDBA3038C0950A5D36D5E8D9A59859A8B6D31BE6AB0004A86A76E601842F6C81A1F004C906525384303E02D724532EE2C3F43C7A68FF6260569E8FC8737B5C2249957A4DEDD2346B42E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6D635BA3ABDB36C18089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A5D4F8E977119652AD5002B1117B3ED6961E127C8EF843C715A13BD6A4B0E00B96823CB91A9FED034534781492E4B8EEAD9B0B1F3F535763ECC79554A2A72441328621D336A7BC284946AD531847A6065A535571D14F44ED41 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF77DD89D51EBB7742D3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CFE2D45C5C62B15FAA2DB14CAAB88580881B07B5645C7B6FF0A67DE8B12622CAA50F85117B495E935B748ED927723DE83F83139D55F8669CAA3CAF1481CD484FC3DBB748A5E6C3A01EC226CC413062362A913E6812662D5F2A5EAB5682573093F7837F15F2B5E4A70B33F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojT+KSurEfQafZJ3SGx43/kw== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A7692D8B7A358C102759A6D5EE0DB6E1EC8D0457E0C124250782B7CBEF92542CD7C88B0A2698F12F5C9EC77752E0C033A69E86920BD37369036789A8C6A0E60D2BB63A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Handle stack reallocation in debug.setmetatable() and lua_setmetatable(). 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: Sergey Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall Thanks to Sergey Kaplun. (cherry picked from commit 88ed9fdbbba632d174a473a0a97c914089c2916d) When we use the aforementioned functions to set a metatable for types with one shared metatable, we must flush all traces since they are specialized to base metatables. If we have enabled vmevent handlers, they invoke a callback on trace flushing. This callback may reallocate the Lua stack. Thus invalidates the reference to the `TValue *` object `o` by the given index in the `lua_setmetatable()` and leads to a heap-use-after-free error. This patch fixes the behaviour by recalculating the address by the given index after possible stack reallocation. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#9595 --- Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1172-debug-handling-ref Tarantool PR: https://github.com/tarantool/tarantool/pull/9786 Related issues: * https://github.com/tarantool/tarantool/issues/9595 * https://github.com/LuaJIT/LuaJIT/issues/1172 src/lj_api.c | 1 + .../lj-1172-debug-handling-ref.test.lua | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 test/tarantool-tests/lj-1172-debug-handling-ref.test.lua diff --git a/src/lj_api.c b/src/lj_api.c index 3bacad33..2e915306 100644 --- a/src/lj_api.c +++ b/src/lj_api.c @@ -1067,6 +1067,7 @@ LUA_API int lua_setmetatable(lua_State *L, int idx) /* Flush cache, since traces specialize to basemt. But not during __gc. */ if (lj_trace_flushall(L)) lj_err_caller(L, LJ_ERR_NOGCMM); + o = index2adr(L, idx); /* Stack may have been reallocated. */ if (tvisbool(o)) { /* NOBARRIER: basemt is a GC root. */ setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt)); diff --git a/test/tarantool-tests/lj-1172-debug-handling-ref.test.lua b/test/tarantool-tests/lj-1172-debug-handling-ref.test.lua new file mode 100644 index 00000000..cac1c223 --- /dev/null +++ b/test/tarantool-tests/lj-1172-debug-handling-ref.test.lua @@ -0,0 +1,30 @@ +local tap = require('tap') + +-- Test file to demonstrate the heap-use-after-free, error for +-- `debug.setmetatable()` and enabled `jit.dump()`. +-- The test fails under ASAN. +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1172. + +local test = tap.test('lj-1172-debug-handling-ref'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), +}) + +local jdump = require('jit.dump') + +test:plan(1) + +jdump.start('t', '/dev/null') + +-- Use `coroutine.wrap()` to create a new Lua stack with a minimum +-- number of stack slots. +coroutine.wrap(function() + -- "TRACE flush" event handler causes stack reallocation and + -- leads to heap-use-after-free. This event handler is called + -- because all traces are specialized to base metatables, so + -- if we update any base metatable, we must flush all traces. + debug.setmetatable(1, {}) +end)() + +test:ok(true, 'no heap-use-after-free error') + +test:done(true) -- 2.44.0