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
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.
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.
30 #include "src/base/platform/platform.h"
31 #include "test/cctest/cctest.h"
33 using namespace ::v8::internal;
36 #define ASM __asm__ __volatile__
38 #if defined(_M_X64) || defined(__x86_64__)
39 #define GET_STACK_POINTER() \
40 static int sp_addr = 0; \
42 ASM("mov %%rsp, %0" : "=g" (sp_addr)); \
44 #elif defined(_M_IX86) || defined(__i386__)
45 #define GET_STACK_POINTER() \
46 static int sp_addr = 0; \
48 ASM("mov %%esp, %0" : "=g" (sp_addr)); \
50 #elif defined(__ARMEL__)
51 #define GET_STACK_POINTER() \
52 static int sp_addr = 0; \
54 ASM("str %%sp, %0" : "=g" (sp_addr)); \
56 #elif defined(__AARCH64EL__)
57 #define GET_STACK_POINTER() \
58 static int sp_addr = 0; \
60 ASM("mov x16, sp; str x16, %0" : "=g" (sp_addr)); \
62 #elif defined(__MIPSEB__) || defined(__MIPSEL__)
63 #define GET_STACK_POINTER() \
64 static int sp_addr = 0; \
66 ASM("sw $sp, %0" : "=g" (sp_addr)); \
69 #error Host architecture was not detected as supported by v8
72 void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) {
74 args.GetReturnValue().Set(v8_num(sp_addr));
78 TEST(StackAlignment) {
79 v8::Isolate* isolate = CcTest::isolate();
80 v8::HandleScope handle_scope(isolate);
81 v8::Handle<v8::ObjectTemplate> global_template =
82 v8::ObjectTemplate::New(isolate);
83 global_template->Set(v8_str("get_stack_pointer"),
84 v8::FunctionTemplate::New(isolate, GetStackPointer));
86 LocalContext env(NULL, global_template);
89 " return get_stack_pointer();"
92 v8::Local<v8::Object> global_object = env->Global();
93 v8::Local<v8::Function> foo =
94 v8::Local<v8::Function>::Cast(global_object->Get(v8_str("foo")));
96 v8::Local<v8::Value> result = foo->Call(global_object, 0, NULL);
97 CHECK_EQ(0, result->Int32Value() % v8::base::OS::ActivationFrameAlignment());
100 #undef GET_STACK_POINTERS