firebird icon indicating copy to clipboard operation
firebird copied to clipboard

[FB3+] Building a result can cause memory overload

Open dmitry-lipetsk opened this issue 2 years ago • 0 comments

Hello.

Remote protocol 13 (FB3+) allows to operate with 32bit number of buffer sizes.

To say more exactly, the maximum size of data can be up to 0xFFFF0000-1 bytes.

It allows within one roundtrip send and receive more data that it was allowed in P12- protocols.

For example, we can define large number in p_sqlst_buffer_length and get a definition of few thousand columns at one call to server.

https://github.com/FirebirdSQL/firebird/blob/c854b3d976468737e092efd5851167a92a682373/src/remote/protocol.h#L553-L567

It is assumed that server will use this number to limit a size of result but not allocate all the memory for result buffer immediately.

Unfortunately it do it.

See above my results of multithreaded test (8 threads) with FB4.

When I use max buffer sizes equal to USHRT_MAX (65535 bytes), server allocates for about 0.5GB:

image

When I define the maximum size of result buffers (0xFFFF0000-1 bytes), server allocates more than 32GB memory.

image

I think, server should use a more smart code for allocating memory for results.


You can use this project to play with different sizes of result buffers.

See the C_INFO_BUF_SIZE_P13 constant.

/// <summary>
///  It is a maximum length that can be passed through ULONG-length.
/// </summary>
/// 
///  This limitation is linked with a defect in FB implementation.
///  See fixup fixupLength in protocol.cpp of FB3 source code.
/// 
static const P_ULONG C_CSTRING_V2_MAX_LENGTH_P13=0xFFFF0000-1;

////////////////////////////////////////////////////////////////////////////////

static const P_ULONG C_INFO_BUF_SIZE_P13=USHRT_MAX;

https://github.com/ibprovider/TechDemo-IBProvider.FirebirdClient-v01/blob/c7f587dde022fec824e2e07bbac0cef8283be899/projects/IBProvider_v5/provider/source/db_client/remote_fb/protocol/set02/remote_fb__protocol_set02.h#L239-L252

I got the second result when I had defined C_INFO_BUF_SIZE_P13=C_CSTRING_V2_MAX_LENGTH_P13.

dmitry-lipetsk avatar Dec 08 '23 06:12 dmitry-lipetsk