blob: a0fbc21f46220a55d9b6c24e365cebd5feaa41d2 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005#include <stdint.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006#include "src/base/build_config.h"
7#include "src/base/platform/platform.h"
8#include "test/cctest/cctest.h"
9
10#ifdef V8_CC_GNU
11
12static uintptr_t sp_addr = 0;
13
14void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) {
15#if V8_HOST_ARCH_X64
16 __asm__ __volatile__("mov %%rsp, %0" : "=g"(sp_addr));
17#elif V8_HOST_ARCH_IA32
18 __asm__ __volatile__("mov %%esp, %0" : "=g"(sp_addr));
19#elif V8_HOST_ARCH_ARM
Ben Murdoch61f157c2016-09-16 13:49:30 +010020 __asm__ __volatile__("str sp, %0" : "=g"(sp_addr));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021#elif V8_HOST_ARCH_ARM64
22 __asm__ __volatile__("mov x16, sp; str x16, %0" : "=g"(sp_addr));
23#elif V8_HOST_ARCH_MIPS
24 __asm__ __volatile__("sw $sp, %0" : "=g"(sp_addr));
25#elif V8_HOST_ARCH_MIPS64
26 __asm__ __volatile__("sd $sp, %0" : "=g"(sp_addr));
Ben Murdochda12d292016-06-02 14:46:10 +010027#elif defined(__s390x__) || defined(_ARCH_S390X)
Ben Murdochc5610432016-08-08 18:44:38 +010028 __asm__ __volatile__("stg 15, %0" : "=m"(sp_addr));
Ben Murdochda12d292016-06-02 14:46:10 +010029#elif defined(__s390__) || defined(_ARCH_S390)
Ben Murdochc5610432016-08-08 18:44:38 +010030 __asm__ __volatile__("st 15, %0" : "=m"(sp_addr));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031#elif defined(__PPC64__) || defined(_ARCH_PPC64)
32 __asm__ __volatile__("std 1, %0" : "=g"(sp_addr));
33#elif defined(__PPC__) || defined(_ARCH_PPC)
34 __asm__ __volatile__("stw 1, %0" : "=g"(sp_addr));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035#else
36#error Host architecture was not detected as supported by v8
37#endif
38
39 args.GetReturnValue().Set(v8::Integer::NewFromUnsigned(
40 args.GetIsolate(), static_cast<uint32_t>(sp_addr)));
41}
42
43
44TEST(StackAlignment) {
45 v8::Isolate* isolate = CcTest::isolate();
46 v8::HandleScope handle_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047 v8::Local<v8::ObjectTemplate> global_template =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 v8::ObjectTemplate::New(isolate);
49 global_template->Set(v8_str("get_stack_pointer"),
50 v8::FunctionTemplate::New(isolate, GetStackPointer));
51
52 LocalContext env(NULL, global_template);
53 CompileRun(
54 "function foo() {"
55 " return get_stack_pointer();"
56 "}");
57
58 v8::Local<v8::Object> global_object = env->Global();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059 v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
60 global_object->Get(isolate->GetCurrentContext(), v8_str("foo"))
61 .ToLocalChecked());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063 v8::Local<v8::Value> result =
64 foo->Call(isolate->GetCurrentContext(), global_object, 0, NULL)
65 .ToLocalChecked();
66 CHECK_EQ(0u, result->Uint32Value(isolate->GetCurrentContext()).FromJust() %
67 v8::base::OS::ActivationFrameAlignment());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068}
69
70#endif // V8_CC_GNU