Skip to content

tuple: allocate formats table statically#10289

Merged
locker merged 1 commit intotarantool:masterfrom
locker:vy-tuple-format-lookup-fix
Jul 22, 2024
Merged

tuple: allocate formats table statically#10289
locker merged 1 commit intotarantool:masterfrom
locker:vy-tuple-format-lookup-fix

Conversation

@locker
Copy link
Member

@locker locker commented Jul 22, 2024

The tuple formats table may be accessed with tuple_format_by_id() from any thread, not just tx. For example, it's accessed by a vinyl writer thread when it deletes a tuple. If a thread happens to access the table while it's being reallocated by tx, see tuple_format_register(), the accessing thread may crash with a use-after-free or NULL pointer dereference bug, like the one below:

 # 1  0x64bd45c09e22 in crash_signal_cb+162
 # 2  0x76ce74e45320 in __sigaction+80
 # 3  0x64bd45ab070c in vy_run_writer_append_stmt+700
 # 4  0x64bd45ada32a in vy_task_write_run+234
 # 5  0x64bd45ad84fe in vy_task_f+46
 # 6  0x64bd45a4aba0 in fiber_cxx_invoke(int (*)(__va_list_tag*), __va_list_tag*)+16
 # 7  0x64bd45c13e66 in fiber_loop+70
 # 8  0x64bd45e83b9c in coro_init+76

To avoid that, let's make the tuple formats table statically allocated. This shouldn't increase actual memory usage because system memory is allocated lazily, on page fault. The max number of tuple formats isn't that big (64K) to care about the increase in virtual memory usage.

Closes #10278

The tuple formats table may be accessed with `tuple_format_by_id()` from
any thread, not just tx. For example, it's accessed by a vinyl writer
thread when it deletes a tuple. If a thread happens to access the table
while it's being reallocated by tx, see `tuple_format_register()`,
the accessing thread may crash with a use-after-free or NULL pointer
dereference bug, like the one below:

```
 # 1  0x64bd45c09e22 in crash_signal_cb+162
 # 2  0x76ce74e45320 in __sigaction+80
 # 3  0x64bd45ab070c in vy_run_writer_append_stmt+700
 # 4  0x64bd45ada32a in vy_task_write_run+234
 # 5  0x64bd45ad84fe in vy_task_f+46
 # 6  0x64bd45a4aba0 in fiber_cxx_invoke(int (*)(__va_list_tag*), __va_list_tag*)+16
 # 7  0x64bd45c13e66 in fiber_loop+70
 # 8  0x64bd45e83b9c in coro_init+76
```

To avoid that, let's make the tuple formats table statically allocated.
This shouldn't increase actual memory usage because system memory is
allocated lazily, on page fault. The max number of tuple formats isn't
that big (64K) to care about the increase in virtual memory usage.

Closes tarantool#10278

NO_DOC=bug fix
NO_TEST=mt race
@locker locker requested a review from a team as a code owner July 22, 2024 10:23
@coveralls
Copy link

Coverage Status

coverage: 87.083% (-0.009%) from 87.092%
when pulling 9feb1c0 on locker:vy-tuple-format-lookup-fix
into b3db87d
on tarantool:master
.

Copy link
Contributor

@mkostoevr mkostoevr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Never knew that realloc behaves like malloc when NULL is passed as the first argument BTW.

@locker locker assigned locker and unassigned mkostoevr Jul 22, 2024
@locker locker added the full-ci Enables all tests for a pull request label Jul 22, 2024
@locker locker merged commit a2da1de into tarantool:master Jul 22, 2024
@locker locker deleted the vy-tuple-format-lookup-fix branch July 22, 2024 14:34
@locker
Copy link
Member Author

locker commented Jul 22, 2024

Cherry-picked to 3.1 and 2.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Enables all tests for a pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SIGSEGV in vy_run_writer_append_stmt

4 participants