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 30AAC6EC56; Sat, 20 Mar 2021 03:43:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 30AAC6EC56 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616201034; bh=RvGm01s3wc8woptYIBqMGj1pqg0fGyNQ7Lex5D6fUTE=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=WVwkGLhsMJ8fQKFcRp46Jroj+CiD6gfuw3vqhykr7keQ+OK7s1sNQO0iqYfv/4Duv vlEp9C2BBUOtDbzEgvbfDuRa3pMrDynUlAYfm5uRfXABz2N3UtsFc5fOIjt3q8C2qd q2oY6jQI3368z06fKIkb5KQM6brLk8ci024z1lUY= Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 9BEA96EC6F for ; Sat, 20 Mar 2021 03:42:50 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9BEA96EC6F Received: by smtpng3.m.smailru.net with esmtpa (envelope-from ) id 1lNPhZ-00076U-TQ; Sat, 20 Mar 2021 03:42:50 +0300 To: tarantool-patches@dev.tarantool.org, gorcunov@gmail.com, sergepetrenko@tarantool.org Date: Sat, 20 Mar 2021 01:42:32 +0100 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F1C82687294EF6886B62CC997B0B8C95C4182A05F5380850407A354DB42A5E29FBC6AB656AD51A8A52279BF3F30A26EABD3A96C1B09C27C6FB X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE714773D61402E8DE9EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637CE3A619BB4CB99268638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C81A23F326053F827BB005E401292D090899EB50D099FF8AFA471835C12D1D9774AD6D5ED66289B5278DA827A17800CE759A2DA0C93DFCD719FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C397DCCBFEAAA0BC6A117882F4460429728AD0CFFFB425014E868A13BD56FB6657A7F4EDE966BC389F9E8FC8737B5C2249753C3A5E0A5AB5B7089D37D7C0E48F6CCF19DD082D7633A0E7DDDDC251EA7DABAAAE862A0553A39223F8577A6DFFEA7C831C17AB68599A1243847C11F186F3C5E7DDDDC251EA7DABCC89B49CDF41148F90DBEB212AEC08F22623479134186CDE6BA297DBC24807EABDAD6C7F3747799A X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2BBE337FB72E923155C0AF1600DCBC20BD3311767B7455A33ABF0BBF8C0A96775 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C81A23F326053F827BB005E401292D090899EB50D099FF8AF9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFF532FBFD8162E58C699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D347324AA9FA07FF01E61C1A3DA69AD00FD8E7CDE4D30C8A7C9B9A29100F07A2F2997189052EBAD70071D7E09C32AA3244CE02C34CA26DE7247CE224EF413BB60433FD9C8CA1B0515E0FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj8xHC0Ak4ylZSIlEDjp02iQ== X-Mailru-Sender: 689FA8AB762F73936BC43F508A063822E37FBA0FE937F514A16FB856E8B4D3BD3841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH 10/16] uri: replace static_alloc with ffi stash and ibuf 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: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" static_alloc() appears not to be safe to use in Lua, because it does not provide any ownership protection for the returned values. The problem appears when something is allocated, then Lua GC starts, and some __gc handlers might also use static_alloc(). In Lua and in C - both lead to the buffer being corrupted in its original usage place. The patch is a part of activity of getting rid of static_alloc() in Lua. It removes it from uri Lua module and makes it use the new FFI stash feature, which helps to cache frequently used and heavy to allocate FFI values. In one place static_alloc() was used for an actual buffer - it was replaced with cord_ibuf which is equally fast when preallocated. ffi.new() for temporary struct uri is not used, because - It produces a new GC object; - ffi.new('struct uri') costs around 20ns while FFI stash costs around 0.8ns. The hack with 'struct uri[1]' does not help because size of uri is > 128 bytes; - Without JIT ffi.new() costs about the same as the stash, not better as well; The patch makes uri perf a bit better in the places where static_alloc() was used, because its cost was around 7ns for one allocation. --- src/lua/uri.lua | 21 ++++++-- test/app-tap/gh-5632-gc-buf-reuse.test.lua | 60 +++++++++++++++++++++- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/lua/uri.lua b/src/lua/uri.lua index e08e20675..98f4e02ec 100644 --- a/src/lua/uri.lua +++ b/src/lua/uri.lua @@ -1,7 +1,7 @@ -- uri.lua (internal file) local ffi = require('ffi') -local static_alloc = require('buffer').static_alloc +local buffer = require('buffer') ffi.cdef[[ struct uri { @@ -32,13 +32,19 @@ uri_format(char *str, size_t len, struct uri *uri, bool write_password); ]] local builtin = ffi.C; +local uri_stash = buffer.ffi_stash_new('struct uri') +local uri_stash_take = uri_stash.take +local uri_stash_put = uri_stash.put +local cord_ibuf_take = buffer.internal.cord_ibuf_take +local cord_ibuf_put = buffer.internal.cord_ibuf_put local function parse(str) if str == nil then error("Usage: uri.parse(string)") end - local uribuf = static_alloc('struct uri') + local uribuf = uri_stash_take() if builtin.uri_parse(uribuf, str) ~= 0 then + uri_stash_put(uribuf) return nil end local result = {} @@ -55,11 +61,12 @@ local function parse(str) elseif uribuf.host_hint == 3 then result.unix = result.service end + uri_stash_put(uribuf) return result end local function format(uri, write_password) - local uribuf = static_alloc('struct uri') + local uribuf = uri_stash_take() uribuf.scheme = uri.scheme uribuf.scheme_len = string.len(uri.scheme or '') uribuf.login = uri.login @@ -76,9 +83,13 @@ local function format(uri, write_password) uribuf.query_len = string.len(uri.query or '') uribuf.fragment = uri.fragment uribuf.fragment_len = string.len(uri.fragment or '') - local str = static_alloc('char', 1024) + local ibuf = cord_ibuf_take() + local str = ibuf:alloc(1024) local len = builtin.uri_format(str, 1024, uribuf, write_password and 1 or 0) - return ffi.string(str, len) + uri_stash_put(uribuf) + str = ffi.string(str, len) + cord_ibuf_put(ibuf) + return str end return { diff --git a/test/app-tap/gh-5632-gc-buf-reuse.test.lua b/test/app-tap/gh-5632-gc-buf-reuse.test.lua index 8fe662d3f..bfa86615f 100755 --- a/test/app-tap/gh-5632-gc-buf-reuse.test.lua +++ b/test/app-tap/gh-5632-gc-buf-reuse.test.lua @@ -10,6 +10,7 @@ local tap = require('tap') local ffi = require('ffi') local uuid = require('uuid') +local uri = require('uri') local function test_uuid(test) test:plan(1) @@ -42,8 +43,65 @@ local function test_uuid(test) test:ok(is_success, 'uuid in gc') end +local function test_uri(test) + test:plan(1) + + local gc_count = 100 + local iter_count = 1000 + local port = 1 + local ip = 1 + local login = 1 + local pass = 1 + local is_success = true + + local function uri_parse() + local loc_ip = ip + local loc_port = port + local loc_pass = pass + local loc_login = login + + ip = ip + 1 + port = port + 1 + pass = pass + 1 + login = login + 1 + if port > 60000 then + port = 1 + end + if ip > 255 then + ip = 1 + end + + loc_ip = string.format('127.0.0.%s', loc_ip) + loc_port = tostring(loc_port) + loc_pass = string.format('password%s', loc_pass) + loc_login = string.format('login%s', loc_login) + local host = string.format('%s:%s@%s:%s', loc_login, loc_pass, + loc_ip, loc_port) + local u = uri.parse(host) + if u.host ~= loc_ip or u.login ~= loc_login or u.service ~= loc_port or + u.password ~= loc_pass then + is_success = false + assert(false) + end + end + + local function create_gc() + for i = 1, gc_count do + ffi.gc(ffi.new('char[1]'), uri_parse) + end + end + + for i = 1, iter_count do + create_gc() + uri_parse() + end + + test:ok(is_success, 'uri in gc') +end + local test = tap.test('gh-5632-gc-buf-reuse') -test:plan(1) +test:plan(2) test:test('uuid in __gc', test_uuid) +test:test('uri in __gc', test_uri) os.exit(test:check() and 0 or 1) -- 2.24.3 (Apple Git-128)