1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include <stdlib.h>
29
30 #include "cctest.h"
31 #include "platform.h"
32
33 using namespace ::v8::internal;
34
35 #ifdef __GNUC__
36 #define ASM __asm__ __volatile__
37
38 #if defined(_M_X64) || defined(__x86_64__)
39 #define GET_STACK_POINTER() \
40 static int sp_addr = 0; \
41 do { \
42 ASM("mov %%rsp, %0" : "=g" (sp_addr)); \
43 } while (0)
44 #elif defined(_M_IX86) || defined(__i386__)
45 #define GET_STACK_POINTER() \
46 static int sp_addr = 0; \
47 do { \
48 ASM("mov %%esp, %0" : "=g" (sp_addr)); \
49 } while (0)
50 #elif defined(__ARMEL__)
51 #define GET_STACK_POINTER() \
52 static int sp_addr = 0; \
53 do { \
54 ASM("str %%sp, %0" : "=g" (sp_addr)); \
55 } while (0)
56 #elif defined(__MIPSEL__)
57 #define GET_STACK_POINTER() \
58 static int sp_addr = 0; \
59 do { \
60 ASM("sw $sp, %0" : "=g" (sp_addr)); \
61 } while (0)
62 #else
63 #error Host architecture was not detected as supported by v8
64 #endif
65
GetStackPointer(const v8::FunctionCallbackInfo<v8::Value> & args)66 void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) {
67 GET_STACK_POINTER();
68 args.GetReturnValue().Set(v8_num(sp_addr));
69 }
70
71
TEST(StackAlignment)72 TEST(StackAlignment) {
73 v8::Isolate* isolate = CcTest::isolate();
74 v8::HandleScope handle_scope(isolate);
75 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
76 global_template->Set(v8_str("get_stack_pointer"),
77 v8::FunctionTemplate::New(GetStackPointer));
78
79 LocalContext env(NULL, global_template);
80 CompileRun(
81 "function foo() {"
82 " return get_stack_pointer();"
83 "}");
84
85 v8::Local<v8::Object> global_object = env->Global();
86 v8::Local<v8::Function> foo =
87 v8::Local<v8::Function>::Cast(global_object->Get(v8_str("foo")));
88
89 v8::Local<v8::Value> result = foo->Call(global_object, 0, NULL);
90 CHECK_EQ(0, result->Int32Value() % OS::ActivationFrameAlignment());
91 }
92
93 #undef GET_STACK_POINTERS
94 #undef ASM
95 #endif // __GNUC__
96