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 285E1498A3C; Wed, 31 May 2023 16:28:14 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 285E1498A3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1685539694; bh=Alt5sFuroyEs35Bk9jTweLUa5WuMgcoCcdmBftod5xQ=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=B6oEjVP3qd4/XhekW0psk+UD4pNfRXLeOcytxq87VQXld5b1CpX7/fFGJdIasIdMe HdVQw8qZfWHZv8z7KJRz+JCteREXeT09ogHjWybFkAWJyl8pwlpbQ9QWU1fvbNDwkC a6LXF4RQXLLC/xbchaR87E5kMm14Isstb4hfy31Y= Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (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 4A509385601 for ; Wed, 31 May 2023 16:28:12 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4A509385601 Received: by mail-lf1-f51.google.com with SMTP id 2adb3069b0e04-4f4b0a0b557so6584046e87.1 for ; Wed, 31 May 2023 06:28:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685539691; x=1688131691; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=K508jO8jbbgFiF7XdaWn2bzUormnqTyitm1b33LdzMg=; b=YT9MPPGksYPHk15X/gFmfqmKH+vilRZU9gtMIn8ODFMMoi3YEkrRwhJT/DXDul6lAR YPuNiJmrOtyVwgp4NaxNyOS3yFCLTKdNPScCz3Buug3Slapr/Gl++muRbql+xOkoAp7Q Nnhr7YVlzT6KafnTlf71I1u5dgHHxCvDz9Zma+5rQO76s115TztqKmiJ6Rvl7fMB5HsT et4yH06/PkqTBEnKZLoEkii8SFXijqqtzl+9u6EUVAiZyfrS+OIpYYzeiwFsN1JI5Mis LGle9lZGuIRCP1qtCev0sAdt6dL7rhQDkATKhLU/u9CjCFJa52SxmIP5vkF5qaXm6hz/ PYEA== X-Gm-Message-State: AC+VfDxTirMBUwcPpKI/ug4QQTAKcKyg1+YnYSSuV+Hhub1C3khdVpiu 6qNcsDdbjjs31Edq8ntuGDokKtEwJiPzzA== X-Google-Smtp-Source: ACHHUZ7ULHaiEJEm2x7S5DxJncNodxLKixD9DLmVOWRCWgb3v3Lh42zSXsJmiypRwQuXK/9hHs+WLw== X-Received: by 2002:ac2:599e:0:b0:4ec:8fb0:9fac with SMTP id w30-20020ac2599e000000b004ec8fb09facmr2854477lfn.47.1685539690941; Wed, 31 May 2023 06:28:10 -0700 (PDT) Received: from fckxorg.mail.msk ([2a00:1148:b0ba:16:a3e8:bdc1:dbed:dbc8]) by smtp.gmail.com with ESMTPSA id d4-20020ac25444000000b004f155762085sm721364lfn.122.2023.05.31.06.28.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 06:28:10 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Wed, 31 May 2023 16:28:04 +0300 Message-Id: <20230531132806.216178-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] Fix memory probing allocator to check for valid end address, too. 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" From: Mike Pall (cherry-picked from commit 646148e747759f0af3b47f9bd287cedd7e174631) Before the patch `mmap_probe` only checked if the allocated chunk start was within the 2^LJ_ALLOC_MBITS bytes region. However, if the chunk is big enough, its end can reach outside of that region. This patch adds the corresponding check, to avoid such situations. Maxim Kokryashkin: * added the description and the test for the problem Part of tarantool/tarantool#8516 --- Branch: https://github.com/tarantool/luajit/tree/fckxorg/lj-445-fix-memory-probing-allocator PR: https://github.com/tarantool/tarantool/pull/8720 LuaJIT issue: https://github.com/LuaJIT/LuaJIT/issues/445 src/lj_alloc.c | 3 +- ...-445-fix-memory-probing-allocator.test.lua | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-tests/lj-445-fix-memory-probing-allocator.test.lua diff --git a/src/lj_alloc.c b/src/lj_alloc.c index ffcd019b..f7039b5b 100644 --- a/src/lj_alloc.c +++ b/src/lj_alloc.c @@ -255,7 +255,8 @@ static void *mmap_probe(size_t size) for (retry = 0; retry < LJ_ALLOC_MMAP_PROBE_MAX; retry++) { void *p = mmap((void *)hint_addr, size, MMAP_PROT, MMAP_FLAGS_PROBE, -1, 0); uintptr_t addr = (uintptr_t)p; - if ((addr >> LJ_ALLOC_MBITS) == 0 && addr >= LJ_ALLOC_MMAP_PROBE_LOWER) { + if ((addr >> LJ_ALLOC_MBITS) == 0 && addr >= LJ_ALLOC_MMAP_PROBE_LOWER && + ((addr + size) >> LJ_ALLOC_MBITS) == 0) { /* We got a suitable address. Bump the hint address. */ hint_addr = addr + size; errno = olderr; diff --git a/test/tarantool-tests/lj-445-fix-memory-probing-allocator.test.lua b/test/tarantool-tests/lj-445-fix-memory-probing-allocator.test.lua new file mode 100644 index 00000000..44763e38 --- /dev/null +++ b/test/tarantool-tests/lj-445-fix-memory-probing-allocator.test.lua @@ -0,0 +1,32 @@ +local tap = require('tap') +local ffi = require('ffi') +local test = tap.test('lj-445-fix-memory-probing-allocator'):skipcond({ + ['Unlikely to hit beyond the upper bound for GC64'] = ffi.abi('gc64'), +}) + +local bit = require('bit') +local shr = bit.rshift +local uintptr_t = ffi.typeof('uintptr_t') + +-- Due to limitations in the x64 compiler backend, max memory limit is +-- two times lower when JIT is not disabled entirely. +local HAS_JIT = jit.status() +local LJ_ALLOC_MBITS = HAS_JIT and 31 or 32 +local MAX_GB = HAS_JIT and 2 or 4 + +test:plan(MAX_GB) + +-- Chomp memory in currently allocated GC space. +collectgarbage('stop') + +-- Every allocation must either result in a chunk that fits into the +-- `MAX_GB`-sized region entirely or return an OOM error. +for _ = 1, MAX_GB do + local status, result = pcall(ffi.new, 'char[?]', 1024 * 1024 * 1024) + if status then + local upper_bound = ffi.cast(uintptr_t, result) + ffi.sizeof(result) + test:ok(shr(upper_bound, LJ_ALLOC_MBITS) == 0, 'non-extended address') + else + test:ok(result == 'not enough memory', 'OOM encountered') + end +end -- 2.40.1