Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@dev.tarantool.org, korablev@tarantool.org,
	tsafin@tarantool.org, alyapunov@tarantool.org,
	gorcunov@gmail.com
Subject: [Tarantool-patches] [PATCH 05/10] sql: make BtCursor's memory aligned
Date: Thu, 21 May 2020 22:37:28 +0200	[thread overview]
Message-ID: <7bdb9ea8c65f01e3ca1f0cc70a20e9e60fd207e1.1590093222.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1590093222.git.v.shpilevoy@tarantool.org>

Vdbe at runtime allocates VdbeCursor structure using
allocateCursor() function. Inside there is a pointer at BtCursor
structure. To make the allocation faster and improve cache
locality, both cursors are allocated in one memory block + some
extra memory for uint32_t array, where BtCursor followed
VdbeCursor and the array without any padding:

   VdbeCursor + uint32_t * N + BtCursor

The problem is that BtCursor needs 8 byte alignment. When it
followed VdbeCursor (aligned by 8) + some uint32_t values, its
actual alignment could become 4 bytes. That led to a crash when
alignment sanitizer is enabled in clang.

The patch makes BtCursor offset aligned by 8 bytes.

Part of #4609
---
 src/box/sql/vdbe.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 724bc188b..7a42602a2 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -234,10 +234,9 @@ allocateCursor(
 	 */
 	Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
 
-	int nByte;
 	VdbeCursor *pCx = 0;
-	nByte =
-		ROUND8(sizeof(VdbeCursor)) + sizeof(u32)*nField +
+	int bt_offset = ROUND8(sizeof(VdbeCursor) + sizeof(uint32_t) * nField);
+	int nByte = bt_offset +
 		(eCurType==CURTYPE_TARANTOOL ? ROUND8(sizeof(BtCursor)) : 0);
 
 	assert(iCur>=0 && iCur<p->nCursor);
@@ -251,8 +250,7 @@ allocateCursor(
 		pCx->eCurType = eCurType;
 		pCx->nField = nField;
 		if (eCurType==CURTYPE_TARANTOOL) {
-			pCx->uc.pCursor = (BtCursor*)
-				&pMem->z[ROUND8(sizeof(VdbeCursor))+sizeof(u32)*nField];
+			pCx->uc.pCursor = (BtCursor*)&pMem->z[bt_offset];
 			sqlCursorZero(pCx->uc.pCursor);
 		}
 	}
-- 
2.21.1 (Apple Git-122.3)

  parent reply	other threads:[~2020-05-21 20:37 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-21 20:37 [Tarantool-patches] [PATCH 00/10] Sanitize unaligned access Vladislav Shpilevoy
2020-05-21 20:37 ` [Tarantool-patches] [PATCH 01/10] small: sanitized rlist and new region API Vladislav Shpilevoy
2020-06-08 12:17   ` Cyrill Gorcunov
2020-05-21 20:37 ` [Tarantool-patches] [PATCH 10/10] xrow: use unaligned store operation in xrow_to_iovec() Vladislav Shpilevoy
2020-06-08 12:26   ` Cyrill Gorcunov
2020-05-21 20:37 ` [Tarantool-patches] [PATCH 02/10] cmake: ignore warnings on alignof() and offsetof() Vladislav Shpilevoy
2020-06-08 12:52   ` Cyrill Gorcunov
2020-05-21 20:37 ` [Tarantool-patches] [PATCH 03/10] cmake: add option ENABLE_UB_SANITIZER Vladislav Shpilevoy
2020-06-08 12:53   ` Cyrill Gorcunov
2020-05-21 20:37 ` [Tarantool-patches] [PATCH 04/10] crc32: disable align sanitizer Vladislav Shpilevoy
2020-06-08 13:58   ` Cyrill Gorcunov
2020-05-21 20:37 ` Vladislav Shpilevoy [this message]
2020-06-08 13:58   ` [Tarantool-patches] [PATCH 05/10] sql: make BtCursor's memory aligned Cyrill Gorcunov
2020-05-21 20:37 ` [Tarantool-patches] [PATCH 06/10] region: use aligned allocations where necessary Vladislav Shpilevoy
2020-06-08 14:00   ` Cyrill Gorcunov
2020-05-21 20:37 ` [Tarantool-patches] [PATCH 07/10] vinyl: align statements and bps tree extents Vladislav Shpilevoy
2020-06-08 14:02   ` Cyrill Gorcunov
2020-05-21 20:37 ` [Tarantool-patches] [PATCH 08/10] tuple: use unaligned store-load for field map Vladislav Shpilevoy
2020-06-08 14:04   ` Cyrill Gorcunov
2020-05-21 20:37 ` [Tarantool-patches] [PATCH 09/10] port: make port_c_entry not PACKED Vladislav Shpilevoy
2020-06-08 14:04   ` Cyrill Gorcunov
2020-05-21 22:25 ` [Tarantool-patches] [PATCH 00/10] Sanitize unaligned access Sergey Bronnikov
2020-05-27 23:33   ` Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7bdb9ea8c65f01e3ca1f0cc70a20e9e60fd207e1.1590093222.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=alyapunov@tarantool.org \
    --cc=gorcunov@gmail.com \
    --cc=korablev@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=tsafin@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 05/10] sql: make BtCursor'\''s memory aligned' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox