2
0

errors.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. -- $Id: testes/errors.lua $
  2. -- See Copyright Notice in file all.lua
  3. print("testing errors")
  4. local debug = require"debug"
  5. -- avoid problems with 'strict' module (which may generate other error messages)
  6. local mt = getmetatable(_G) or {}
  7. local oldmm = mt.__index
  8. mt.__index = nil
  9. local function checkerr (msg, f, ...)
  10. local st, err = pcall(f, ...)
  11. assert(not st and string.find(err, msg))
  12. end
  13. local function doit (s)
  14. local f, msg = load(s)
  15. if not f then return msg end
  16. local cond, msg = pcall(f)
  17. return (not cond) and msg
  18. end
  19. local function checkmessage (prog, msg)
  20. local m = doit(prog)
  21. assert(string.find(m, msg, 1, true))
  22. end
  23. local function checksyntax (prog, extra, token, line)
  24. local msg = doit(prog)
  25. if not string.find(token, "^<%a") and not string.find(token, "^char%(")
  26. then token = "'"..token.."'" end
  27. token = string.gsub(token, "(%p)", "%%%1")
  28. local pt = string.format([[^%%[string ".*"%%]:%d: .- near %s$]],
  29. line, token)
  30. assert(string.find(msg, pt))
  31. assert(string.find(msg, msg, 1, true))
  32. end
  33. -- test error message with no extra info
  34. assert(doit("error('hi', 0)") == 'hi')
  35. -- test error message with no info
  36. assert(doit("error()") == nil)
  37. -- test common errors/errors that crashed in the past
  38. assert(doit("table.unpack({}, 1, n=2^30)"))
  39. assert(doit("a=math.sin()"))
  40. assert(not doit("tostring(1)") and doit("tostring()"))
  41. assert(doit"tonumber()")
  42. assert(doit"repeat until 1; a")
  43. assert(doit"return;;")
  44. assert(doit"assert(false)")
  45. assert(doit"assert(nil)")
  46. assert(doit("function a (... , ...) end"))
  47. assert(doit("function a (, ...) end"))
  48. assert(doit("local t={}; t = t[#t] + 1"))
  49. checksyntax([[
  50. local a = {4
  51. ]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
  52. if not T then
  53. (Message or print)
  54. ('\n >>> testC not active: skipping memory message test <<<\n')
  55. else
  56. print "testing memory error message"
  57. local a = {}
  58. for i = 1, 10000 do a[i] = true end -- preallocate array
  59. collectgarbage()
  60. T.totalmem(T.totalmem() + 10000)
  61. -- force a memory error (by a small margin)
  62. local st, msg = pcall(function()
  63. for i = 1, 100000 do a[i] = tostring(i) end
  64. end)
  65. T.totalmem(0)
  66. assert(not st and msg == "not enough" .. " memory")
  67. end
  68. -- tests for better error messages
  69. checkmessage("a = {} + 1", "arithmetic")
  70. checkmessage("a = {} | 1", "bitwise operation")
  71. checkmessage("a = {} < 1", "attempt to compare")
  72. checkmessage("a = {} <= 1", "attempt to compare")
  73. checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'")
  74. checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'")
  75. checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
  76. assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'"))
  77. checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number")
  78. checkmessage("a=(1)..{}", "a table value")
  79. -- tail calls
  80. checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'")
  81. checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'")
  82. checkmessage("a = #print", "length of a function value")
  83. checkmessage("a = #3", "length of a number value")
  84. aaa = nil
  85. checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
  86. checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
  87. checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
  88. checkmessage("local a,b,c; (function () a = b+1.1 end)()", "upvalue 'b'")
  89. assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)")
  90. -- upvalues being indexed do not go to the stack
  91. checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'")
  92. checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'")
  93. checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
  94. checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'")
  95. checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
  96. checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'")
  97. checkmessage("aaa={}; x=-aaa", "global 'aaa'")
  98. -- short circuit
  99. checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)",
  100. "local 'bbbb'")
  101. checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'")
  102. checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
  103. checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
  104. assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
  105. assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'"))
  106. checkmessage("print(print < 10)", "function with number")
  107. checkmessage("print(print < print)", "two function values")
  108. checkmessage("print('10' < 10)", "string with number")
  109. checkmessage("print(10 < '23')", "number with string")
  110. -- float->integer conversions
  111. checkmessage("local a = 2.0^100; x = a << 2", "local a")
  112. checkmessage("local a = 1 >> 2.0^100", "has no integer representation")
  113. checkmessage("local a = 10.1 << 2.0^100", "has no integer representation")
  114. checkmessage("local a = 2.0^100 & 1", "has no integer representation")
  115. checkmessage("local a = 2.0^100 & 1e100", "has no integer representation")
  116. checkmessage("local a = 2.0 | 1e40", "has no integer representation")
  117. checkmessage("local a = 2e100 ~ 1", "has no integer representation")
  118. checkmessage("string.sub('a', 2.0^100)", "has no integer representation")
  119. checkmessage("string.rep('a', 3.3)", "has no integer representation")
  120. checkmessage("return 6e40 & 7", "has no integer representation")
  121. checkmessage("return 34 << 7e30", "has no integer representation")
  122. checkmessage("return ~-3e40", "has no integer representation")
  123. checkmessage("return ~-3.009", "has no integer representation")
  124. checkmessage("return 3.009 & 1", "has no integer representation")
  125. checkmessage("return 34 >> {}", "table value")
  126. checkmessage("a = 24 // 0", "divide by zero")
  127. checkmessage("a = 1 % 0", "'n%0'")
  128. -- numeric for loops
  129. checkmessage("for i = {}, 10 do end", "table")
  130. checkmessage("for i = io.stdin, 10 do end", "FILE")
  131. checkmessage("for i = {}, 10 do end", "initial value")
  132. checkmessage("for i = 1, 'x', 10 do end", "string")
  133. checkmessage("for i = 1, {}, 10 do end", "limit")
  134. checkmessage("for i = 1, {} do end", "limit")
  135. checkmessage("for i = 1, 10, print do end", "step")
  136. checkmessage("for i = 1, 10, print do end", "function")
  137. -- passing light userdata instead of full userdata
  138. _G.D = debug
  139. checkmessage([[
  140. -- create light udata
  141. local x = D.upvalueid(function () return debug end, 1)
  142. D.setuservalue(x, {})
  143. ]], "light userdata")
  144. _G.D = nil
  145. do -- named objects (field '__name')
  146. checkmessage("math.sin(io.input())", "(number expected, got FILE*)")
  147. _G.XX = setmetatable({}, {__name = "My Type"})
  148. assert(string.find(tostring(XX), "^My Type"))
  149. checkmessage("io.input(XX)", "(FILE* expected, got My Type)")
  150. checkmessage("return XX + 1", "on a My Type value")
  151. checkmessage("return ~io.stdin", "on a FILE* value")
  152. checkmessage("return XX < XX", "two My Type values")
  153. checkmessage("return {} < XX", "table with My Type")
  154. checkmessage("return XX < io.stdin", "My Type with FILE*")
  155. _G.XX = nil
  156. end
  157. -- global functions
  158. checkmessage("(io.write or print){}", "io.write")
  159. checkmessage("(collectgarbage or print){}", "collectgarbage")
  160. -- errors in functions without debug info
  161. do
  162. local f = function (a) return a + 1 end
  163. f = assert(load(string.dump(f, true)))
  164. assert(f(3) == 4)
  165. checkerr("^%?:%-1:", f, {})
  166. -- code with a move to a local var ('OP_MOV A B' with A<B)
  167. f = function () local a; a = {}; return a + 2 end
  168. -- no debug info (so that 'a' is unknown)
  169. f = assert(load(string.dump(f, true)))
  170. -- symbolic execution should not get lost
  171. checkerr("^%?:%-1:.*table value", f)
  172. end
  173. -- tests for field accesses after RK limit
  174. local t = {}
  175. for i = 1, 1000 do
  176. t[i] = "a = x" .. i
  177. end
  178. local s = table.concat(t, "; ")
  179. t = nil
  180. checkmessage(s.."; a = bbb + 1", "global 'bbb'")
  181. checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'")
  182. checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'")
  183. checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
  184. checkmessage([[aaa=9
  185. repeat until 3==3
  186. local x=math.sin(math.cos(3))
  187. if math.sin(1) == x then return math.sin(1) end -- tail call
  188. local a,b = 1, {
  189. {x='a'..'b'..'c', y='b', z=x},
  190. {1,2,3,4,5} or 3+3<=3+3,
  191. 3+1>3+1,
  192. {d = x and aaa[x or y]}}
  193. ]], "global 'aaa'")
  194. checkmessage([[
  195. local x,y = {},1
  196. if math.sin(1) == 0 then return 3 end -- return
  197. x.a()]], "field 'a'")
  198. checkmessage([[
  199. prefix = nil
  200. insert = nil
  201. while 1 do
  202. local a
  203. if nil then break end
  204. insert(prefix, a)
  205. end]], "global 'insert'")
  206. checkmessage([[ -- tail call
  207. return math.sin("a")
  208. ]], "'sin'")
  209. checkmessage([[collectgarbage("nooption")]], "invalid option")
  210. checkmessage([[x = print .. "a"]], "concatenate")
  211. checkmessage([[x = "a" .. false]], "concatenate")
  212. checkmessage([[x = {} .. 2]], "concatenate")
  213. checkmessage("getmetatable(io.stdin).__gc()", "no value")
  214. checkmessage([[
  215. local Var
  216. local function main()
  217. NoSuchName (function() Var=0 end)
  218. end
  219. main()
  220. ]], "global 'NoSuchName'")
  221. print'+'
  222. a = {}; setmetatable(a, {__index = string})
  223. checkmessage("a:sub()", "bad self")
  224. checkmessage("string.sub('a', {})", "#2")
  225. checkmessage("('a'):sub{}", "#1")
  226. checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
  227. checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
  228. -- tests for errors in coroutines
  229. local function f (n)
  230. local c = coroutine.create(f)
  231. local a,b = coroutine.resume(c)
  232. return b
  233. end
  234. assert(string.find(f(), "C stack overflow"))
  235. checkmessage("coroutine.yield()", "outside a coroutine")
  236. f = coroutine.wrap(function () table.sort({1,2,3}, coroutine.yield) end)
  237. checkerr("yield across", f)
  238. -- testing size of 'source' info; size of buffer for that info is
  239. -- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
  240. idsize = 60 - 1
  241. local function checksize (source)
  242. -- syntax error
  243. local _, msg = load("x", source)
  244. msg = string.match(msg, "^([^:]*):") -- get source (1st part before ':')
  245. assert(msg:len() <= idsize)
  246. end
  247. for i = 60 - 10, 60 + 10 do -- check border cases around 60
  248. checksize("@" .. string.rep("x", i)) -- file names
  249. checksize(string.rep("x", i - 10)) -- string sources
  250. checksize("=" .. string.rep("x", i)) -- exact sources
  251. end
  252. -- testing line error
  253. local function lineerror (s, l)
  254. local err,msg = pcall(load(s))
  255. local line = tonumber(string.match(msg, ":(%d+):"))
  256. assert(line == l or (not line and not l))
  257. end
  258. lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
  259. lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3)
  260. lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4)
  261. lineerror("function a.x.y ()\na=a+1\nend", 1)
  262. lineerror("a = \na\n+\n{}", 3)
  263. lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6)
  264. lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3)
  265. lineerror("a\n=\n-\n\nprint\n;", 3)
  266. lineerror([[
  267. a
  268. (
  269. 23)
  270. ]], 1)
  271. lineerror([[
  272. local a = {x = 13}
  273. a
  274. .
  275. x
  276. (
  277. 23
  278. )
  279. ]], 2)
  280. lineerror([[
  281. local a = {x = 13}
  282. a
  283. .
  284. x
  285. (
  286. 23 + a
  287. )
  288. ]], 6)
  289. local p = [[
  290. function g() f() end
  291. function f(x) error('a', X) end
  292. g()
  293. ]]
  294. X=3;lineerror((p), 3)
  295. X=0;lineerror((p), false)
  296. X=1;lineerror((p), 2)
  297. X=2;lineerror((p), 1)
  298. lineerror([[
  299. local b = false
  300. if not b then
  301. error 'test'
  302. end]], 3)
  303. lineerror([[
  304. local b = false
  305. if not b then
  306. if not b then
  307. if not b then
  308. error 'test'
  309. end
  310. end
  311. end]], 5)
  312. if not _soft then
  313. -- several tests that exaust the Lua stack
  314. collectgarbage()
  315. print"testing stack overflow"
  316. C = 0
  317. -- get line where stack overflow will happen
  318. local l = debug.getinfo(1, "l").currentline + 1
  319. local function auxy () C=C+1; auxy() end -- produce a stack overflow
  320. function y ()
  321. collectgarbage("stop") -- avoid running finalizers without stack space
  322. auxy()
  323. collectgarbage("restart")
  324. end
  325. local function checkstackmessage (m)
  326. print("(expected stack overflow after " .. C .. " calls)")
  327. C = 0 -- prepare next count
  328. return (string.find(m, "stack overflow"))
  329. end
  330. -- repeated stack overflows (to check stack recovery)
  331. assert(checkstackmessage(doit('y()')))
  332. assert(checkstackmessage(doit('y()')))
  333. assert(checkstackmessage(doit('y()')))
  334. -- error lines in stack overflow
  335. local l1
  336. local function g(x)
  337. l1 = debug.getinfo(x, "l").currentline + 2
  338. collectgarbage("stop") -- avoid running finalizers without stack space
  339. auxy()
  340. collectgarbage("restart")
  341. end
  342. local _, stackmsg = xpcall(g, debug.traceback, 1)
  343. print('+')
  344. local stack = {}
  345. for line in string.gmatch(stackmsg, "[^\n]*") do
  346. local curr = string.match(line, ":(%d+):")
  347. if curr then table.insert(stack, tonumber(curr)) end
  348. end
  349. local i=1
  350. while stack[i] ~= l1 do
  351. assert(stack[i] == l)
  352. i = i+1
  353. end
  354. assert(i > 15)
  355. -- error in error handling
  356. local res, msg = xpcall(error, error)
  357. assert(not res and type(msg) == 'string')
  358. print('+')
  359. local function f (x)
  360. if x==0 then error('a\n')
  361. else
  362. local aux = function () return f(x-1) end
  363. local a,b = xpcall(aux, aux)
  364. return a,b
  365. end
  366. end
  367. f(3)
  368. local function loop (x,y,z) return 1 + loop(x, y, z) end
  369. local res, msg = xpcall(loop, function (m)
  370. assert(string.find(m, "stack overflow"))
  371. checkerr("error handling", loop)
  372. assert(math.sin(0) == 0)
  373. return 15
  374. end)
  375. assert(msg == 15)
  376. local f = function ()
  377. for i = 999900, 1000000, 1 do table.unpack({}, 1, i) end
  378. end
  379. checkerr("too many results", f)
  380. end
  381. do
  382. -- non string messages
  383. local t = {}
  384. local res, msg = pcall(function () error(t) end)
  385. assert(not res and msg == t)
  386. res, msg = pcall(function () error(nil) end)
  387. assert(not res and msg == nil)
  388. local function f() error{msg='x'} end
  389. res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
  390. assert(msg.msg == 'xy')
  391. -- 'assert' with extra arguments
  392. res, msg = pcall(assert, false, "X", t)
  393. assert(not res and msg == "X")
  394. -- 'assert' with no message
  395. res, msg = pcall(function () assert(false) end)
  396. local line = string.match(msg, "%w+%.lua:(%d+): assertion failed!$")
  397. assert(tonumber(line) == debug.getinfo(1, "l").currentline - 2)
  398. -- 'assert' with non-string messages
  399. res, msg = pcall(assert, false, t)
  400. assert(not res and msg == t)
  401. res, msg = pcall(assert, nil, nil)
  402. assert(not res and msg == nil)
  403. -- 'assert' without arguments
  404. res, msg = pcall(assert)
  405. assert(not res and string.find(msg, "value expected"))
  406. end
  407. -- xpcall with arguments
  408. a, b, c = xpcall(string.find, error, "alo", "al")
  409. assert(a and b == 1 and c == 2)
  410. a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
  411. assert(not a and type(b) == "table" and c == nil)
  412. print("testing tokens in error messages")
  413. checksyntax("syntax error", "", "error", 1)
  414. checksyntax("1.000", "", "1.000", 1)
  415. checksyntax("[[a]]", "", "[[a]]", 1)
  416. checksyntax("'aa'", "", "'aa'", 1)
  417. checksyntax("while << do end", "", "<<", 1)
  418. checksyntax("for >> do end", "", ">>", 1)
  419. -- test invalid non-printable char in a chunk
  420. checksyntax("a\1a = 1", "", "<\\1>", 1)
  421. -- test 255 as first char in a chunk
  422. checksyntax("\255a = 1", "", "<\\255>", 1)
  423. doit('I = load("a=9+"); a=3')
  424. assert(a==3 and not I)
  425. print('+')
  426. lim = 1000
  427. if _soft then lim = 100 end
  428. for i=1,lim do
  429. doit('a = ')
  430. doit('a = 4+nil')
  431. end
  432. -- testing syntax limits
  433. local function testrep (init, rep, close, repc, finalresult)
  434. local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100)
  435. local res, msg = load(s)
  436. assert(res) -- 100 levels is OK
  437. if (finalresult) then
  438. assert(res() == finalresult)
  439. end
  440. s = init .. string.rep(rep, 500)
  441. local res, msg = load(s) -- 500 levels not ok
  442. assert(not res and (string.find(msg, "too many") or
  443. string.find(msg, "overflow")))
  444. end
  445. testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
  446. testrep("local a; a=", "{", "0", "}")
  447. testrep("return ", "(", "2", ")", 2)
  448. testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2)
  449. testrep("", "do ", "", " end")
  450. testrep("", "while a do ", "", " end")
  451. testrep("local a; ", "if a then else ", "", " end")
  452. testrep("", "function foo () ", "", " end")
  453. testrep("local a = ''; return ", "a..", "'a'", "", "a")
  454. testrep("local a = 1; return ", "a^", "a", "", 1)
  455. checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
  456. -- testing other limits
  457. -- upvalues
  458. local lim = 127
  459. local s = "local function fooA ()\n local "
  460. for j = 1,lim do
  461. s = s.."a"..j..", "
  462. end
  463. s = s.."b,c\n"
  464. s = s.."local function fooB ()\n local "
  465. for j = 1,lim do
  466. s = s.."b"..j..", "
  467. end
  468. s = s.."b\n"
  469. s = s.."function fooC () return b+c"
  470. local c = 1+2
  471. for j = 1,lim do
  472. s = s.."+a"..j.."+b"..j
  473. c = c + 2
  474. end
  475. s = s.."\nend end end"
  476. local a,b = load(s)
  477. assert(c > 255 and string.find(b, "too many upvalues") and
  478. string.find(b, "line 5"))
  479. -- local variables
  480. s = "\nfunction foo ()\n local "
  481. for j = 1,300 do
  482. s = s.."a"..j..", "
  483. end
  484. s = s.."b\n"
  485. local a,b = load(s)
  486. assert(string.find(b, "line 2") and string.find(b, "too many local variables"))
  487. mt.__index = oldmm
  488. print('OK')