2013-02-15 12:38:59 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
2014-09-16 09:23:27 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-02-15 12:38:59 +00:00
|
|
|
|
2014-10-21 08:25:14 +00:00
|
|
|
#include <stdint.h>
|
2014-09-16 09:23:27 +00:00
|
|
|
#include "src/base/build_config.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2013-02-15 12:38:59 +00:00
|
|
|
|
2014-09-16 09:23:27 +00:00
|
|
|
#ifdef V8_CC_GNU
|
2013-02-15 12:38:59 +00:00
|
|
|
|
2014-09-16 09:23:27 +00:00
|
|
|
static uintptr_t sp_addr = 0;
|
2013-07-05 08:34:17 +00:00
|
|
|
|
2014-09-16 09:23:27 +00:00
|
|
|
void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
#if V8_HOST_ARCH_X64
|
|
|
|
__asm__ __volatile__("mov %%rsp, %0" : "=g"(sp_addr));
|
|
|
|
#elif V8_HOST_ARCH_IA32
|
|
|
|
__asm__ __volatile__("mov %%esp, %0" : "=g"(sp_addr));
|
|
|
|
#elif V8_HOST_ARCH_ARM
|
2016-06-01 14:14:28 +00:00
|
|
|
__asm__ __volatile__("str sp, %0" : "=g"(sp_addr));
|
2014-09-16 09:23:27 +00:00
|
|
|
#elif V8_HOST_ARCH_ARM64
|
|
|
|
__asm__ __volatile__("mov x16, sp; str x16, %0" : "=g"(sp_addr));
|
|
|
|
#elif V8_HOST_ARCH_MIPS
|
|
|
|
__asm__ __volatile__("sw $sp, %0" : "=g"(sp_addr));
|
2014-09-16 20:16:11 +00:00
|
|
|
#elif V8_HOST_ARCH_MIPS64
|
|
|
|
__asm__ __volatile__("sd $sp, %0" : "=g"(sp_addr));
|
2016-03-15 18:07:43 +00:00
|
|
|
#elif defined(__s390x__) || defined(_ARCH_S390X)
|
2016-07-16 15:35:28 +00:00
|
|
|
__asm__ __volatile__("stg 15, %0" : "=m"(sp_addr));
|
2016-03-15 18:07:43 +00:00
|
|
|
#elif defined(__s390__) || defined(_ARCH_S390)
|
2016-07-16 15:35:28 +00:00
|
|
|
__asm__ __volatile__("st 15, %0" : "=m"(sp_addr));
|
2015-01-16 07:42:00 +00:00
|
|
|
#elif defined(__PPC64__) || defined(_ARCH_PPC64)
|
|
|
|
__asm__ __volatile__("std 1, %0" : "=g"(sp_addr));
|
|
|
|
#elif defined(__PPC__) || defined(_ARCH_PPC)
|
|
|
|
__asm__ __volatile__("stw 1, %0" : "=g"(sp_addr));
|
2013-07-05 08:34:17 +00:00
|
|
|
#else
|
|
|
|
#error Host architecture was not detected as supported by v8
|
|
|
|
#endif
|
|
|
|
|
2014-09-16 09:23:27 +00:00
|
|
|
args.GetReturnValue().Set(v8::Integer::NewFromUnsigned(
|
|
|
|
args.GetIsolate(), static_cast<uint32_t>(sp_addr)));
|
2013-07-05 08:34:17 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-07-05 08:34:17 +00:00
|
|
|
TEST(StackAlignment) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
2013-07-05 08:34:17 +00:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global_template =
|
2014-01-08 06:53:31 +00:00
|
|
|
v8::ObjectTemplate::New(isolate);
|
2013-07-05 08:34:17 +00:00
|
|
|
global_template->Set(v8_str("get_stack_pointer"),
|
2013-12-18 10:31:42 +00:00
|
|
|
v8::FunctionTemplate::New(isolate, GetStackPointer));
|
2013-07-05 08:34:17 +00:00
|
|
|
|
|
|
|
LocalContext env(NULL, global_template);
|
|
|
|
CompileRun(
|
|
|
|
"function foo() {"
|
|
|
|
" return get_stack_pointer();"
|
|
|
|
"}");
|
|
|
|
|
|
|
|
v8::Local<v8::Object> global_object = env->Global();
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
|
|
|
|
global_object->Get(isolate->GetCurrentContext(), v8_str("foo"))
|
|
|
|
.ToLocalChecked());
|
2013-07-05 08:34:17 +00:00
|
|
|
|
2015-10-08 09:48:05 +00:00
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
foo->Call(isolate->GetCurrentContext(), global_object, 0, NULL)
|
|
|
|
.ToLocalChecked();
|
|
|
|
CHECK_EQ(0u, result->Uint32Value(isolate->GetCurrentContext()).FromJust() %
|
|
|
|
v8::base::OS::ActivationFrameAlignment());
|
2013-07-05 08:34:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 09:23:27 +00:00
|
|
|
#endif // V8_CC_GNU
|