diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-05-04 14:17:15 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-05-04 14:17:15 -0300 |
commit | 9a6f47f0edfded799f7cb6fd719bb0071b326100 (patch) | |
tree | ca7d98f9ce1b33611e5fbe168b074cee8739064a | |
parent | 948fb628d9d7242acfe1c3fe1f099ef228e38ead (diff) | |
download | lua-9a6f47f0edfded799f7cb6fd719bb0071b326100.tar.gz lua-9a6f47f0edfded799f7cb6fd719bb0071b326100.tar.bz2 lua-9a6f47f0edfded799f7cb6fd719bb0071b326100.zip |
C-Stack test does not assume minimum of 400 slots
-rw-r--r-- | testes/cstack.lua | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/testes/cstack.lua b/testes/cstack.lua index cd74fd28..e3e14f74 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua | |||
@@ -20,13 +20,14 @@ print"If this test crashes, see its file ('cstack.lua')" | |||
20 | 20 | ||
21 | 21 | ||
22 | -- get and print original limit | 22 | -- get and print original limit |
23 | local origlimit = debug.setcstacklimit(400) | 23 | local origlimit <const> = debug.setcstacklimit(400) |
24 | print("default stack limit: " .. origlimit) | 24 | print("default stack limit: " .. origlimit) |
25 | 25 | ||
26 | |||
26 | -- Do the tests using the original limit. Or else you may want to change | 27 | -- Do the tests using the original limit. Or else you may want to change |
27 | -- 'currentlimit' to lower values to avoid a seg. fault or to higher | 28 | -- 'currentlimit' to lower values to avoid a seg. fault or to higher |
28 | -- values to check whether they are reliable. | 29 | -- values to check whether they are reliable. |
29 | local currentlimit = origlimit | 30 | local currentlimit <const> = origlimit |
30 | debug.setcstacklimit(currentlimit) | 31 | debug.setcstacklimit(currentlimit) |
31 | print("current stack limit: " .. currentlimit) | 32 | print("current stack limit: " .. currentlimit) |
32 | 33 | ||
@@ -107,12 +108,16 @@ end | |||
107 | 108 | ||
108 | do print("testing changes in C-stack limit") | 109 | do print("testing changes in C-stack limit") |
109 | 110 | ||
111 | -- Just an alternative limit, different from the current one | ||
112 | -- (smaller to avoid stack overflows) | ||
113 | local alterlimit <const> = currentlimit * 8 // 10 | ||
114 | |||
110 | assert(not debug.setcstacklimit(0)) -- limit too small | 115 | assert(not debug.setcstacklimit(0)) -- limit too small |
111 | assert(not debug.setcstacklimit(50000)) -- limit too large | 116 | assert(not debug.setcstacklimit(50000)) -- limit too large |
112 | local co = coroutine.wrap (function () | 117 | local co = coroutine.wrap (function () |
113 | return debug.setcstacklimit(400) | 118 | return debug.setcstacklimit(alterlimit) |
114 | end) | 119 | end) |
115 | assert(co() == false) -- cannot change C stack inside coroutine | 120 | assert(not co()) -- cannot change C stack inside coroutine |
116 | 121 | ||
117 | local n | 122 | local n |
118 | local function foo () n = n + 1; foo () end | 123 | local function foo () n = n + 1; foo () end |
@@ -123,28 +128,32 @@ do print("testing changes in C-stack limit") | |||
123 | return n | 128 | return n |
124 | end | 129 | end |
125 | 130 | ||
126 | -- set limit to 400 | 131 | -- set limit to 'alterlimit' |
127 | assert(debug.setcstacklimit(400) == currentlimit) | 132 | assert(debug.setcstacklimit(alterlimit) == currentlimit) |
128 | local lim400 = check() | 133 | local limalter <const> = check() |
129 | -- set a very low limit (given that there are already several active | 134 | -- set a very low limit (given that there are already several active |
130 | -- calls to arrive here) | 135 | -- calls to arrive here) |
131 | local lowlimit = 38 | 136 | local lowlimit <const> = 38 |
132 | assert(debug.setcstacklimit(lowlimit) == 400) | 137 | assert(debug.setcstacklimit(lowlimit) == alterlimit) |
133 | assert(check() < lowlimit - 30) | 138 | -- usable limit is much lower, due to active calls |
134 | assert(debug.setcstacklimit(600) == lowlimit) | 139 | local actuallow = check() |
135 | local lim600 = check() | 140 | assert(actuallow < lowlimit - 30) |
136 | assert(lim600 == lim400 + 200) | 141 | -- now, add 'lowlimit' extra slots, which should all be available |
142 | assert(debug.setcstacklimit(lowlimit + lowlimit) == lowlimit) | ||
143 | local lim2 <const> = check() | ||
144 | assert(lim2 == actuallow + lowlimit) | ||
137 | 145 | ||
138 | 146 | ||
139 | -- 'setcstacklimit' works inside protected calls. (The new stack | 147 | -- 'setcstacklimit' works inside protected calls. (The new stack |
140 | -- limit is kept when 'pcall' returns.) | 148 | -- limit is kept when 'pcall' returns.) |
141 | assert(pcall(function () | 149 | assert(pcall(function () |
142 | assert(debug.setcstacklimit(400) == 600) | 150 | assert(debug.setcstacklimit(alterlimit) == lowlimit * 2) |
143 | assert(check() <= lim400) | 151 | assert(check() <= limalter) |
144 | end)) | 152 | end)) |
145 | 153 | ||
146 | assert(check() == lim400) | 154 | assert(check() == limalter) |
147 | assert(debug.setcstacklimit(origlimit) == 400) -- restore original limit | 155 | -- restore original limit |
156 | assert(debug.setcstacklimit(origlimit) == alterlimit) | ||
148 | end | 157 | end |
149 | 158 | ||
150 | 159 | ||