Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4stacklimits_p.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:low-level-memory-management
4
5#ifndef QV4STACKLIMITS_P_H
6#define QV4STACKLIMITS_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qtqmlglobal_p.h>
20
21#ifndef Q_STACK_GROWTH_DIRECTION
22# ifdef Q_PROCESSOR_HPPA
23# define Q_STACK_GROWTH_DIRECTION (1)
24# else
25# define Q_STACK_GROWTH_DIRECTION (-1)
26# endif
27#endif
28
29QT_BEGIN_NAMESPACE
30
31namespace QV4 {
32
33// We may not be able to take the negative of the type
34// used to represent stack size, but we can always add
35// or subtract it to/from a quint8 pointer.
36
37template<typename Size>
38static const void *incrementStackPointer(const void *base, Size amount)
39{
41 return static_cast<const quint8 *>(base) + amount;
42#else
43 return static_cast<const quint8 *>(base) - amount;
44#endif
45}
46
47template<typename Size>
48static void *decrementStackPointer(void *base, Size amount)
49{
51 return static_cast<quint8 *>(base) - amount;
52#else
53 return static_cast<quint8 *>(base) + amount;
54#endif
55}
56
57// Note: This does not return a completely accurate stack pointer.
58// Depending on whether this function is inlined or not, we may get the address of
59// this function's stack frame or the caller's stack frame.
60// Always use a safety margin when determining stack limits.
61inline const void *currentStackPointer()
62{
63 // TODO: How often do we actually need the assembler mess below? Is that worth it?
64
65 void *stackPointer;
66#if defined(Q_CC_GNU) || __has_builtin(__builtin_frame_address)
67 stackPointer = __builtin_frame_address(0);
68#elif defined(Q_CC_MSVC)
69 stackPointer = &stackPointer;
70#elif defined(Q_PROCESSOR_X86_64)
71 __asm__ __volatile__("movq %%rsp, %0" : "=r"(stackPointer) : :);
72#elif defined(Q_PROCESSOR_X86)
73 __asm__ __volatile__("movl %%esp, %0" : "=r"(stackPointer) : :);
74#elif defined(Q_PROCESSOR_ARM_64) && defined(__ILP32__)
75 quint64 stackPointerRegister = 0;
76 __asm__ __volatile__("mov %0, sp" : "=r"(stackPointerRegister) : :);
77 stackPointer = reinterpret_cast<void *>(stackPointerRegister);
78#elif defined(Q_PROCESSOR_ARM_64) || defined(Q_PROCESSOR_ARM_32)
79 __asm__ __volatile__("mov %0, sp" : "=r"(stackPointer) : :);
80#else
81 stackPointer = &stackPointer;
82#endif
83 return stackPointer;
84}
85
87{
88 const void *base = nullptr;
89 const void *softLimit = nullptr;
90 const void *hardLimit = nullptr;
91};
92
94
95} // namespace QV4
96
97QT_END_NAMESPACE
98
99#endif // QV4STACKLIMITS_P_H
Definition qjsvalue.h:24
static void * decrementStackPointer(void *base, Size amount)
StackProperties stackProperties()
static const void * incrementStackPointer(const void *base, Size amount)
const void * currentStackPointer()
#define __has_builtin(x)
#define Q_STACK_GROWTH_DIRECTION