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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libmariadb/ma_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ my_context_spawn(struct my_context *c, void (*f)(void *), void *d)
*/
".cfi_escape 0x07, 8\n\t"
#endif
/*
* we'll push the arg on the stack. Skipping the first 12 bytes will mean the
* stack is re-aligned on a 16-byte address when we jump to "f".
*/
"sub $12, %%esp\n\t"
/* Push the parameter on the stack. */
"pushl %[d]\n\t"
"movl %%ebp, 4(%[save])\n\t"
Expand Down
2 changes: 1 addition & 1 deletion unittest/libmariadb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ INCLUDE_DIRECTORIES(${CC_SOURCE_DIR}/include
${CC_SOURCE_DIR}/unittest/libmariadb)
ADD_DEFINITIONS(-DLIBMARIADB)

SET(API_TESTS "conc336" "bulk1" "performance" "basic-t" "fetch" "charset" "logs" "cursor" "errors" "view" "ps" "ps_bugs" "sp" "result" "connection" "misc" "ps_new" "thread" "features-10_2")
SET(API_TESTS "conc336" "bulk1" "performance" "basic-t" "fetch" "charset" "logs" "cursor" "errors" "view" "ps" "ps_bugs" "sp" "result" "connection" "misc" "ps_new" "thread" "features-10_2" "context")
IF(WITH_DYNCOL)
SET(API_TESTS ${API_TESTS} "dyncol")
ENDIF()
Expand Down
44 changes: 44 additions & 0 deletions unittest/libmariadb/context.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Tests for ma_context functions.
*/

#include "my_test.h"
#include "mysql.h"
#include "ma_context.h"
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>

/*
* Ensures the context system aligns the stack correctly for i386 and x86_64
*/
void async_task(__attribute__((unused)) void *v) {
#if defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
/*
* The compiler assumes the stack is correctly aligned on a 16-byte
* boundary on entry to the function (actually, with a return address
* popped on top of the aligned address). Creating an object with a
* max_align_t type should also be aligned at that boundary, so we should
* be able to use movaps to move the first 128 bits into xmm0.
*/
union {
max_align_t aligned;
uint64_t data[2];
} aligned;
__asm__("movaps %0, %%xmm0" : : "m" (aligned.data) : "xmm0" );
#endif
}

static int test_stack_align() {
struct my_context ctx;
my_context_init(&ctx, 65536);
my_context_spawn(&ctx, async_task, &ctx);
my_context_destroy(&ctx);
return 0;
}

int main()
{
plan(1);
ok( test_stack_align() == 0, "stack alignment check" );
}