1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <sys/resource.h>
17 #include "ecmascript/platform/asm_stack.h"
18 #include "ecmascript/js_thread.h"
19
20 namespace panda::ecmascript {
21
GetAsmStackLimit()22 size_t GetAsmStackLimit()
23 {
24 // js stack limit
25 uintptr_t currentStackPos = JSThread::GetCurrentStackPosition();
26 size_t defaultStackSize = EcmaParamConfiguration::GetDefalutStackSize();
27 size_t result = currentStackPos > defaultStackSize ? currentStackPos - defaultStackSize : 0;
28 int ret = -1;
29 void *stackAddr = nullptr;
30 size_t size = 0;
31 #if defined(ENABLE_FFRT_INTERFACES)
32 if (!ffrt_get_current_coroutine_stack(&stackAddr, &size)) {
33 pthread_attr_t attr;
34 ret = pthread_getattr_np(pthread_self(), &attr);
35 if (ret != 0) {
36 LOG_ECMA(ERROR) << "Get current thread attr failed";
37 return result;
38 }
39 ret = pthread_attr_getstack(&attr, &stackAddr, &size);
40 if (pthread_attr_destroy(&attr) != 0) {
41 LOG_ECMA(ERROR) << "Destroy current thread attr failed";
42 }
43 if (ret != 0) {
44 LOG_ECMA(ERROR) << "Get current thread stack size failed";
45 return result;
46 }
47 }
48 #else
49 pthread_attr_t attr;
50 ret = pthread_getattr_np(pthread_self(), &attr);
51 if (ret != 0) {
52 LOG_ECMA(ERROR) << "Get current thread attr failed";
53 return result;
54 }
55 ret = pthread_attr_getstack(&attr, &stackAddr, &size);
56 if (pthread_attr_destroy(&attr) != 0) {
57 LOG_ECMA(ERROR) << "Destroy current thread attr failed";
58 }
59 if (ret != 0) {
60 LOG_ECMA(ERROR) << "Get current thread stack size failed";
61 return result;
62 }
63 #endif
64
65 bool isMainThread = IsMainThread();
66 uintptr_t threadStackLimit = reinterpret_cast<uintptr_t>(stackAddr);
67 uintptr_t threadStackStart = threadStackLimit + size;
68 if (isMainThread) {
69 struct rlimit rl;
70 ret = getrlimit(RLIMIT_STACK, &rl);
71 if (ret != 0) {
72 LOG_ECMA(ERROR) << "Get current thread stack size failed";
73 return result;
74 }
75 if (rl.rlim_cur > JSThread::DEFAULT_MAX_SYSTEM_STACK_SIZE) {
76 LOG_ECMA(ERROR) << "Get current thread stack size exceed " << JSThread::DEFAULT_MAX_SYSTEM_STACK_SIZE
77 << " : " << rl.rlim_cur;
78 return result;
79 }
80 threadStackLimit = threadStackStart - rl.rlim_cur;
81 }
82
83 if (result < threadStackLimit) {
84 result = threadStackLimit;
85 }
86 LOG_INTERPRETER(DEBUG) << "Current thread stack start: " << reinterpret_cast<void *>(threadStackStart);
87 LOG_INTERPRETER(DEBUG) << "Used stack before js stack start: "
88 << reinterpret_cast<void *>(threadStackStart - currentStackPos);
89 LOG_INTERPRETER(DEBUG) << "Current thread asm stack limit: " << reinterpret_cast<void *>(result);
90 uintptr_t currentThreadAsmStackLimit = result;
91 // To avoid too much times of stack overflow checking, we only check stack overflow before push vregs or
92 // parameters of variable length. So we need a reserved size of stack to make sure stack won't be overflowed
93 // when push other data.
94 result += EcmaParamConfiguration::GetDefaultReservedStackSize();
95 if (threadStackStart <= result) {
96 LOG_FULL(FATAL) << "Too small stackSize to run jsvm"
97 << ", CurrentStackPosition: " << reinterpret_cast<void *>(currentStackPos)
98 << ", StackAddr: " << stackAddr << ", Size: " << reinterpret_cast<void *>(size)
99 << ", ThreadStackLimit: " << reinterpret_cast<void *>(threadStackLimit)
100 << ", ThreadStackStart: " << reinterpret_cast<void *>(threadStackStart)
101 << ", Used stack before js stack start: "
102 << reinterpret_cast<void *>(threadStackStart - currentStackPos)
103 << ", Current thread asm stack limit: " << reinterpret_cast<void *>(currentThreadAsmStackLimit)
104 << ", Result: " << reinterpret_cast<void *>(result);
105 }
106 return result;
107 }
108
IsMainThread()109 bool IsMainThread()
110 {
111 return getpid() == syscall(SYS_gettid);
112 }
113 }