Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align stack correctly when initializing a context #230

Open
wants to merge 2 commits into
base: 3.3
Choose a base branch
from

Conversation

peadar
Copy link

@peadar peadar commented Aug 17, 2023

In my_context_spawn, assuming the allocated stack pointer is 16-bye aligned (as it will be from malloc, etc), align the stack correctly before calling the target function of the context.

For i686 platform, The Linux ABI requires that the stack is aligned on a 16-byte boundary at the time that a call is made, and the compiler attempts to maintain that alignment by adjusting the stack as required to meet the alignment requirements for data, and any subsequent calls.

This is particularly important when compiling for i386 platform with -msse2, The compiler can use SSE2 instructions like movaps, This instruction will actually fault on unalgned access. This causes the mariadb test suite to fail with the following error, for example:

main.non_blocking_api                    w4 [ fail ]
        Test ended at 2023-08-18 00:33:40

CURRENT_TEST: main.non_blocking_api
mysqltest got signal 11
read_command_buf (0x89a2a88): connect (con_nonblock,localhost,root,,test)

conn->name (0x89c7208):

Attempting backtrace...
stack_bottom = 0x0 thread_stack 0x10000
mysys/stacktrace.c:213(my_print_stacktrace)[0x80a7348]
bits/stdio2.h:97(fprintf)[0x80615f2]
addr2line: '': No such file
[0xf7ee8560]
??:0(BIO_f_buffer)[0xf7bc1ca1]
??:0(BIO_vsnprintf)[0xf7bc2aa1]
??:0(BIO_snprintf)[0xf7bc2af4]
??:0(ASN1_UTCTIME_adj)[0xf7be0875]
??:0(X509_time_adj_ex)[0xf7c062dd]
??:0(X509_time_adj)[0xf7c06312]
??:0(X509_cmp_time)[0xf7c06516]
??:0(X509_cmp_time)[0xf7c06720]
??:0(X509_verify_cert)[0xf7c08641]
??:0(ssl_verify_cert_chain)[0xf7d172e1]
??:0(ssl3_get_server_certificate)[0xf7cefc96]
??:0(ssl3_connect)[0xf7cf520b]
??:0(ssl23_connect)[0xf7cfee42]
secure/openssl.c:487(ma_tls_connect)[0x808ac22]
libmariadb/ma_tls.c:83(ma_pvio_tls_connect)[0x807d82d]
libmariadb/ma_pvio.c:531(ma_pvio_start_ssl)[0x807d553]
auth/my_auth.c:318(send_client_reply_packet)[0x808f5db]
auth/my_auth.c:94(native_password_auth_client)[0x808f84e]
auth/my_auth.c:620(run_plugin_auth)[0x808fb5c]
libmariadb/mariadb_lib.c:1665(mthd_my_real_connect)[0x807a121]
libmariadb/mariadb_async.c:332(mysql_real_connect_start_internal)[0x808b3d9]
libmariadb/ma_context.c:440(my_context_spawn)[0x808ed69]
libmariadb/mariadb_lib.c:3927(mariadb_get_socket)[0x8078c6f]
tests/nonblock-wrappers.h:96(wait_for_mysql(st_mysql*, int))[0x806117e]

In this case, the eventual crash is in openssl code, but this is a conseqeunce of a movaps instruction in BIO_f_buffer acting on an unaligned stack location, which is in turn a result of the unaligned stack from my_context_spawn. Any use of SSE2 instructions requiring alignment will fail in the same way

peadar added 2 commits August 18, 2023 18:41
For now, this just tests stack alignment in a context's entry point

This test shows a problem when building on i386 with `-msse2`, where the
compiler is allowed use some instructions that move data in and out of
the xmm0-7 registers, requiring them to be aligned on 16-byte
boundaries. Because the compiler will generally assume the stack is
correctly aligned on entering a function, the misalignment will
propagate through the stack until something uses an instruction that
requires 16-byte alignment, and we get a segmentation fault.

The misalignment happens because the stack is heap-allocated (and the
heap allocator provides a properly aligned pointer), but we push the
argument for the async task onto the stack as we call it, leaving the
stack 4 bytes below proper alignment.

The test is equally valid on x86_64, but the stack alignment is correct
there - nothing is pushed onto the stack prior to calling the task
function, as it's passed in a register.
In `my_context_spawn`, assuming the allocated stack pointer is 16-bye
aligned (as it will be from malloc, etc), align the stack correctly
before calling the target function of the context.

For i686 platform, The Linux ABI requires that the stack is aligned on a
16-byte boundary at the time that a call is made, and the compiler
attempts to maintain that alignment by adjusting the stack as required
to meet the alignment requirements for data, and any subsequent calls.

This is particularly important when compiling for i386 platform with
-msse2, The compiler can emit code to use SSE2 instructions like
`movaps`, which will actually fault on unalgned access. This causes the
mariadb test suite to fail with the following error, for example:

```
main.non_blocking_api                    w4 [ fail ]
        Test ended at 2023-08-18 00:33:40

CURRENT_TEST: main.non_blocking_api
mysqltest got signal 11
read_command_buf (0x89a2a88): connect (con_nonblock,localhost,root,,test)

conn->name (0x89c7208):

Attempting backtrace...
stack_bottom = 0x0 thread_stack 0x10000
mysys/stacktrace.c:213(my_print_stacktrace)[0x80a7348]
bits/stdio2.h:97(fprintf)[0x80615f2]
addr2line: '': No such file
[0xf7ee8560]
??:0(BIO_f_buffer)[0xf7bc1ca1]
??:0(BIO_vsnprintf)[0xf7bc2aa1]
??:0(BIO_snprintf)[0xf7bc2af4]
??:0(ASN1_UTCTIME_adj)[0xf7be0875]
??:0(X509_time_adj_ex)[0xf7c062dd]
??:0(X509_time_adj)[0xf7c06312]
??:0(X509_cmp_time)[0xf7c06516]
??:0(X509_cmp_time)[0xf7c06720]
??:0(X509_verify_cert)[0xf7c08641]
??:0(ssl_verify_cert_chain)[0xf7d172e1]
??:0(ssl3_get_server_certificate)[0xf7cefc96]
??:0(ssl3_connect)[0xf7cf520b]
??:0(ssl23_connect)[0xf7cfee42]
secure/openssl.c:487(ma_tls_connect)[0x808ac22]
libmariadb/ma_tls.c:83(ma_pvio_tls_connect)[0x807d82d]
libmariadb/ma_pvio.c:531(ma_pvio_start_ssl)[0x807d553]
auth/my_auth.c:318(send_client_reply_packet)[0x808f5db]
auth/my_auth.c:94(native_password_auth_client)[0x808f84e]
auth/my_auth.c:620(run_plugin_auth)[0x808fb5c]
libmariadb/mariadb_lib.c:1665(mthd_my_real_connect)[0x807a121]
libmariadb/mariadb_async.c:332(mysql_real_connect_start_internal)[0x808b3d9]
libmariadb/ma_context.c:440(my_context_spawn)[0x808ed69]
libmariadb/mariadb_lib.c:3927(mariadb_get_socket)[0x8078c6f]
tests/nonblock-wrappers.h:96(wait_for_mysql(st_mysql*, int))[0x806117e]
```

In this case, the eventual crash is in openssl code, but this is a
conseqeunce of a `movaps` instruction in `BIO_f_buffer` acting on an
unaligned stack location, which is in turn a result of the unaligned
stack from `my_context_spawn`. Any use of SSE2 instructions requiring
alignment will fail in the same way
@vuvova
Copy link
Member

vuvova commented Oct 18, 2023

@knielsen, would you like to take a look at that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants