Line data Source code
1 : // 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 :
5 : #include <stdint.h>
6 : #include "src/base/build_config.h"
7 : #include "src/base/platform/platform.h"
8 : #include "test/cctest/cctest.h"
9 :
10 : using OS = v8::base::OS;
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : #ifdef V8_CC_GNU
16 : static uintptr_t sp_addr = 0;
17 :
18 0 : void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) {
19 : #if V8_HOST_ARCH_X64
20 0 : __asm__ __volatile__("mov %%rsp, %0" : "=g"(sp_addr));
21 : #elif V8_HOST_ARCH_IA32
22 : __asm__ __volatile__("mov %%esp, %0" : "=g"(sp_addr));
23 : #elif V8_HOST_ARCH_ARM
24 : __asm__ __volatile__("str sp, %0" : "=g"(sp_addr));
25 : #elif V8_HOST_ARCH_ARM64
26 : __asm__ __volatile__("mov x16, sp; str x16, %0" : "=g"(sp_addr));
27 : #elif V8_HOST_ARCH_MIPS
28 : __asm__ __volatile__("sw $sp, %0" : "=g"(sp_addr));
29 : #elif V8_HOST_ARCH_MIPS64
30 : __asm__ __volatile__("sd $sp, %0" : "=g"(sp_addr));
31 : #elif defined(__s390x__) || defined(_ARCH_S390X)
32 : __asm__ __volatile__("stg %%r15, %0" : "=m"(sp_addr));
33 : #elif defined(__s390__) || defined(_ARCH_S390)
34 : __asm__ __volatile__("st 15, %0" : "=m"(sp_addr));
35 : #elif defined(__PPC64__) || defined(_ARCH_PPC64)
36 : __asm__ __volatile__("std 1, %0" : "=g"(sp_addr));
37 : #elif defined(__PPC__) || defined(_ARCH_PPC)
38 : __asm__ __volatile__("stw 1, %0" : "=g"(sp_addr));
39 : #else
40 : #error Host architecture was not detected as supported by v8
41 : #endif
42 :
43 0 : args.GetReturnValue().Set(v8::Integer::NewFromUnsigned(
44 : args.GetIsolate(), static_cast<uint32_t>(sp_addr)));
45 0 : }
46 :
47 26639 : TEST(StackAlignment) {
48 0 : v8::Isolate* isolate = CcTest::isolate();
49 0 : v8::HandleScope handle_scope(isolate);
50 : v8::Local<v8::ObjectTemplate> global_template =
51 0 : v8::ObjectTemplate::New(isolate);
52 0 : global_template->Set(v8_str("get_stack_pointer"),
53 0 : v8::FunctionTemplate::New(isolate, GetStackPointer));
54 :
55 0 : LocalContext env(nullptr, global_template);
56 : CompileRun(
57 : "function foo() {"
58 : " return get_stack_pointer();"
59 : "}");
60 :
61 0 : v8::Local<v8::Object> global_object = env->Global();
62 : v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
63 0 : global_object->Get(isolate->GetCurrentContext(), v8_str("foo"))
64 : .ToLocalChecked());
65 :
66 : v8::Local<v8::Value> result =
67 0 : foo->Call(isolate->GetCurrentContext(), global_object, 0, nullptr)
68 : .ToLocalChecked();
69 0 : CHECK_EQ(0u, result->Uint32Value(isolate->GetCurrentContext()).FromJust() %
70 : OS::ActivationFrameAlignment());
71 0 : }
72 : #endif // V8_CC_GNU
73 :
74 : } // namespace internal
75 79917 : } // namespace v8
|