| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // modification, are permitted provided that the following conditions are | 3 // found in the LICENSE file. |
| 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 | 4 |
| 28 #include <stdlib.h> | 5 #include "include/v8stdint.h" |
| 29 | 6 #include "src/base/build_config.h" |
| 30 #include "src/base/platform/platform.h" | 7 #include "src/base/platform/platform.h" |
| 31 #include "test/cctest/cctest.h" | 8 #include "test/cctest/cctest.h" |
| 32 | 9 |
| 33 using namespace ::v8::internal; | 10 #ifdef V8_CC_GNU |
| 34 | 11 |
| 35 #ifdef __GNUC__ | 12 static uintptr_t sp_addr = 0; |
| 36 #define ASM __asm__ __volatile__ | |
| 37 | 13 |
| 38 #if defined(_M_X64) || defined(__x86_64__) | 14 void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 39 #define GET_STACK_POINTER() \ | 15 #if V8_HOST_ARCH_X64 |
| 40 static int sp_addr = 0; \ | 16 __asm__ __volatile__("mov %%rsp, %0" : "=g"(sp_addr)); |
| 41 do { \ | 17 #elif V8_HOST_ARCH_IA32 |
| 42 ASM("mov %%rsp, %0" : "=g" (sp_addr)); \ | 18 __asm__ __volatile__("mov %%esp, %0" : "=g"(sp_addr)); |
| 43 } while (0) | 19 #elif V8_HOST_ARCH_ARM |
| 44 #elif defined(_M_IX86) || defined(__i386__) | 20 __asm__ __volatile__("str %%sp, %0" : "=g"(sp_addr)); |
| 45 #define GET_STACK_POINTER() \ | 21 #elif V8_HOST_ARCH_ARM64 |
| 46 static int sp_addr = 0; \ | 22 __asm__ __volatile__("mov x16, sp; str x16, %0" : "=g"(sp_addr)); |
| 47 do { \ | 23 #elif V8_HOST_ARCH_MIPS |
| 48 ASM("mov %%esp, %0" : "=g" (sp_addr)); \ | 24 __asm__ __volatile__("sw $sp, %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(__AARCH64EL__) | |
| 57 #define GET_STACK_POINTER() \ | |
| 58 static int sp_addr = 0; \ | |
| 59 do { \ | |
| 60 ASM("mov x16, sp; str x16, %0" : "=g" (sp_addr)); \ | |
| 61 } while (0) | |
| 62 #elif defined(__MIPSEB__) || defined(__MIPSEL__) | |
| 63 #define GET_STACK_POINTER() \ | |
| 64 static int sp_addr = 0; \ | |
| 65 do { \ | |
| 66 ASM("sw $sp, %0" : "=g" (sp_addr)); \ | |
| 67 } while (0) | |
| 68 #else | 25 #else |
| 69 #error Host architecture was not detected as supported by v8 | 26 #error Host architecture was not detected as supported by v8 |
| 70 #endif | 27 #endif |
| 71 | 28 |
| 72 void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) { | 29 args.GetReturnValue().Set(v8::Integer::NewFromUnsigned( |
| 73 GET_STACK_POINTER(); | 30 args.GetIsolate(), static_cast<uint32_t>(sp_addr))); |
| 74 args.GetReturnValue().Set(v8_num(sp_addr)); | |
| 75 } | 31 } |
| 76 | 32 |
| 77 | 33 |
| 78 TEST(StackAlignment) { | 34 TEST(StackAlignment) { |
| 79 v8::Isolate* isolate = CcTest::isolate(); | 35 v8::Isolate* isolate = CcTest::isolate(); |
| 80 v8::HandleScope handle_scope(isolate); | 36 v8::HandleScope handle_scope(isolate); |
| 81 v8::Handle<v8::ObjectTemplate> global_template = | 37 v8::Handle<v8::ObjectTemplate> global_template = |
| 82 v8::ObjectTemplate::New(isolate); | 38 v8::ObjectTemplate::New(isolate); |
| 83 global_template->Set(v8_str("get_stack_pointer"), | 39 global_template->Set(v8_str("get_stack_pointer"), |
| 84 v8::FunctionTemplate::New(isolate, GetStackPointer)); | 40 v8::FunctionTemplate::New(isolate, GetStackPointer)); |
| 85 | 41 |
| 86 LocalContext env(NULL, global_template); | 42 LocalContext env(NULL, global_template); |
| 87 CompileRun( | 43 CompileRun( |
| 88 "function foo() {" | 44 "function foo() {" |
| 89 " return get_stack_pointer();" | 45 " return get_stack_pointer();" |
| 90 "}"); | 46 "}"); |
| 91 | 47 |
| 92 v8::Local<v8::Object> global_object = env->Global(); | 48 v8::Local<v8::Object> global_object = env->Global(); |
| 93 v8::Local<v8::Function> foo = | 49 v8::Local<v8::Function> foo = |
| 94 v8::Local<v8::Function>::Cast(global_object->Get(v8_str("foo"))); | 50 v8::Local<v8::Function>::Cast(global_object->Get(v8_str("foo"))); |
| 95 | 51 |
| 96 v8::Local<v8::Value> result = foo->Call(global_object, 0, NULL); | 52 v8::Local<v8::Value> result = foo->Call(global_object, 0, NULL); |
| 97 CHECK_EQ(0, result->Int32Value() % v8::base::OS::ActivationFrameAlignment()); | 53 CHECK_EQ(0, result->Uint32Value() % v8::base::OS::ActivationFrameAlignment()); |
| 98 } | 54 } |
| 99 | 55 |
| 100 #undef GET_STACK_POINTERS | 56 #endif // V8_CC_GNU |
| 101 #undef ASM | |
| 102 #endif // __GNUC__ | |
| OLD | NEW |