Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / toolchain / stackalign_test.c
1 /*
2  * Copyright (c) 2014 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include <stdio.h>
8
9 #include "native_client/src/include/nacl_assert.h"
10
11 void call_with_misaligned_stack(void (*func)(void));
12
13 volatile uintptr_t addrf;
14
15 /*
16  * Test that locals are properly aligned, even if functions are called with a
17  * misaligned stack. This tests the effect of LLC's -force-align-stack (and also
18  * the fact that doubles must be 8-byte aligned). This test should only pass
19  * when using a flag that forces stack realignment.
20 */
21 void testfunc(void) {
22   double f;
23   /*
24    * Smart compiler will optimize away the test (it assumes the alignment is
25    * correct) unless we write it to a volatile.
26    */
27   addrf = (uintptr_t) &f;
28   ASSERT_EQ(addrf % 8, 0);
29 }
30
31 int main(void) {
32   printf("Calling testfunc with properly aligned stack\n");
33   testfunc();
34   printf("Calling testfunc with misaligned stack\n");
35   call_with_misaligned_stack(testfunc);
36   return 0;
37 }