Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
lua/testes/cstack.lua at master · lua/lua · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
lua
lua
Repository navigation
Code
Actions
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
lua
/
testes
/
cstack.lua
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
197 lines (167 loc) · 5.26 KB
master
Breadcrumbs
lua
/
testes
/
cstack.lua
Copy path
Top
File metadata and controls
Code
Blame
197 lines (167 loc) · 5.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
--
$Id: testes/cstack.lua $
--
See Copyright Notice in file lua.h
local
tracegc
=
require
"
tracegc
"
print
"
testing stack overflow detection
"
--
Segmentation faults in these tests probably result from a C-stack
--
overflow. To avoid these errors, you should set a smaller limit for
--
the use of C stack by Lua, by changing the constant 'LUAI_MAXCCALLS'.
--
Alternatively, you can ensure a larger stack for the program.
local
function
checkerror
(
msg
,
f
, ...)
local
s
,
err
=
pcall
(
f
,
...
)
assert
(
not
s
and
string.find
(
err
,
msg
))
end
do
print
(
"
testing stack overflow in message handling
"
)
local
count
=
0
local
function
loop
(
x
,
y
,
z
)
count
=
count
+
1
return
1
+
loop
(
x
,
y
,
z
)
end
tracegc
.
stop
()
--
__gc should not be called with a full stack
local
res
,
msg
=
xpcall
(
loop
,
loop
)
tracegc
.
start
()
assert
(
msg
==
"
error in error handling
"
)
print
(
"
final count:
"
,
count
)
end
--
bug since 2.5 (C-stack overflow in recursion inside pattern matching)
do
print
(
"
testing recursion inside pattern matching
"
)
local
function
f
(
size
)
local
s
=
string.rep
(
"
a
"
,
size
)
local
p
=
string.rep
(
"
.?
"
,
size
)
return
string.match
(
s
,
p
)
end
local
m
=
f
(
80
)
assert
(
#
m
==
80
)
checkerror
(
"
too complex
"
,
f
,
2000
)
end
do
print
(
"
testing stack-overflow in recursive 'gsub'
"
)
local
count
=
0
local
function
foo
()
count
=
count
+
1
string.gsub
(
"
a
"
,
"
.
"
,
foo
)
end
checkerror
(
"
stack overflow
"
,
foo
)
print
(
"
final count:
"
,
count
)
print
(
"
testing stack-overflow in recursive 'gsub' with metatables
"
)
local
count
=
0
local
t
=
setmetatable
({}, {
__index
=
foo
})
foo
=
function
()
count
=
count
+
1
string.gsub
(
"
a
"
,
"
.
"
,
t
)
end
checkerror
(
"
stack overflow
"
,
foo
)
print
(
"
final count:
"
,
count
)
end
do
--
bug in 5.4.0
print
(
"
testing limits in coroutines inside deep calls
"
)
local
count
=
0
local
lim
=
1000
local
function
stack
(
n
)
if
n
>
0
then
return
stack
(
n
-
1
)
+
1
else
coroutine.wrap
(
function
()
count
=
count
+
1
stack
(
lim
)
end
)()
end
end
local
st
,
msg
=
xpcall
(
stack
,
function
()
return
"
ok
"
end
,
lim
)
assert
(
not
st
and
msg
==
"
ok
"
)
print
(
"
final count:
"
,
count
)
end
do
--
bug since 5.4.0
local
count
=
0
print
(
"
chain of 'coroutine.close'
"
)
--
create N coroutines forming a list so that each one, when closed,
--
closes the previous one. (With a large enough N, previous Lua
--
versions crash in this test.)
local
coro
=
false
for
i
=
1
,
1000
do
local
previous
=
coro
coro
=
coroutine.create
(
function
()
local
cc
<close>
=
setmetatable
({}, {
__close
=
function
()
count
=
count
+
1
if
previous
then
assert
(
coroutine.close
(
previous
))
end
end
})
coroutine.yield
()
--
leaves 'cc' pending to be closed
end
)
assert
(
coroutine.resume
(
coro
))
--
start it and run until it yields
end
local
st
,
msg
=
coroutine.close
(
coro
)
assert
(
not
st
and
string.find
(
msg
,
"
C stack overflow
"
))
print
(
"
final count:
"
,
count
)
end
do
print
(
"
nesting of resuming yielded coroutines
"
)
local
count
=
0
local
function
body
()
coroutine.yield
()
local
f
=
coroutine.wrap
(
body
)
f
();
--
start new coroutine (will stop in previous yield)
count
=
count
+
1
f
()
--
call it recursively
end
local
f
=
coroutine.wrap
(
body
)
f
()
assert
(
not
pcall
(
f
))
print
(
"
final count:
"
,
count
)
end
do
--
bug in 5.4.2
print
(
"
nesting coroutines running after recoverable errors
"
)
local
count
=
0
local
function
foo
()
count
=
count
+
1
pcall
(
1
)
--
create an error
--
running now inside 'precover' ("protected recover")
coroutine.wrap
(
foo
)()
--
call another coroutine
end
checkerror
(
"
C stack overflow
"
,
foo
)
print
(
"
final count:
"
,
count
)
end
if
T
then
print
(
"
testing stack recovery
"
)
local
N
=
0
--
trace number of calls
local
LIM
=
-
1
--
will store N just before stack overflow
--
trace stack size; after stack overflow, it should be
--
the maximum allowed stack size.
local
stack1
local
dummy
local
function
err
(
msg
)
assert
(
string.find
(
msg
,
"
stack overflow
"
))
local
_
,
stacknow
=
T
.
stacklevel
()
assert
(
stacknow
==
stack1
+
200
)
end
--
When LIM==-1, the 'if' is not executed, so this function only
--
counts and stores the stack limits up to overflow. Then, LIM
--
becomes N, and then the 'if' code is run when the stack is
--
full. Then, there is a stack overflow inside 'xpcall', after which
--
the stack must have been restored back to its maximum normal size.
local
function
f
()
dummy
,
stack1
=
T
.
stacklevel
()
if
N
==
LIM
then
xpcall
(
f
,
err
)
local
_
,
stacknow
=
T
.
stacklevel
()
assert
(
stacknow
==
stack1
)
return
end
N
=
N
+
1
f
()
end
local
topB
,
sizeB
--
top and size Before overflow
local
topA
,
sizeA
--
top and size After overflow
topB
,
sizeB
=
T
.
stacklevel
()
tracegc
.
stop
()
--
__gc should not be called with a full stack
xpcall
(
f
,
err
)
tracegc
.
start
()
topA
,
sizeA
=
T
.
stacklevel
()
--
sizes should be comparable
assert
(
topA
==
topB
and
sizeA
<
sizeB
*
2
)
print
(
string.format
(
"
maximum stack size: %d
"
,
stack1
))
LIM
=
N
--
will stop recursion at maximum level
N
=
0
--
to count again
tracegc
.
stop
()
--
__gc should not be called with a full stack
f
()
tracegc
.
start
()
print
"
+
"
end
print
'
OK
'
You can’t perform that action at this time.