heavy.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. -- $Id: testes/heavy.lua,v $
  2. -- See Copyright Notice in file lua.h
  3. local function teststring ()
  4. print("creating a string too long")
  5. do
  6. local a = "x"
  7. local st, msg = pcall(function ()
  8. while true do
  9. a = a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  10. .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  11. .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  12. .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  13. .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  14. .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  15. .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  16. .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  17. .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  18. .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
  19. print(string.format("string with %d bytes", #a))
  20. end
  21. end)
  22. assert(not st and
  23. (string.find(msg, "string length overflow") or
  24. string.find(msg, "not enough memory")))
  25. print("string length overflow with " .. #a * 100)
  26. end
  27. print('+')
  28. end
  29. local function loadrep (x, what)
  30. local p = 1<<20
  31. local s = string.rep(x, p)
  32. local count = 0
  33. local function f()
  34. count = count + p
  35. if count % (0x80*p) == 0 then
  36. io.stderr:write("(", count // 2^20, " M)")
  37. end
  38. return s
  39. end
  40. local st, msg = load(f, "=big")
  41. print("\nmemory: ", collectgarbage'count' * 1024)
  42. msg = string.match(msg, "^[^\n]+") -- get only first line
  43. print(string.format("total: 0x%x %s ('%s')", count, what, msg))
  44. return st, msg
  45. end
  46. function controlstruct ()
  47. print("control structure too long")
  48. local lim = ((1 << 24) - 2) // 3
  49. local s = string.rep("a = a + 1\n", lim)
  50. s = "while true do " .. s .. "end"
  51. assert(load(s))
  52. print("ok with " .. lim .. " lines")
  53. lim = lim + 3
  54. s = string.rep("a = a + 1\n", lim)
  55. s = "while true do " .. s .. "end"
  56. local st, msg = load(s)
  57. assert(not st and string.find(msg, "too long"))
  58. print(msg)
  59. end
  60. function manylines ()
  61. print("loading chunk with too many lines")
  62. local st, msg = loadrep("\n", "lines")
  63. assert(not st and string.find(msg, "too many lines"))
  64. print('+')
  65. end
  66. function hugeid ()
  67. print("loading chunk with huge identifier")
  68. local st, msg = loadrep("a", "chars")
  69. assert(not st and
  70. (string.find(msg, "lexical element too long") or
  71. string.find(msg, "not enough memory")))
  72. print('+')
  73. end
  74. function toomanyinst ()
  75. print("loading chunk with too many instructions")
  76. local st, msg = loadrep("a = 10; ", "instructions")
  77. print('+')
  78. end
  79. local function loadrepfunc (prefix, f)
  80. local count = -1
  81. local function aux ()
  82. count = count + 1
  83. if count == 0 then
  84. return prefix
  85. else
  86. if count % (0x100000) == 0 then
  87. io.stderr:write("(", count // 2^20, " M)")
  88. end
  89. return f(count)
  90. end
  91. end
  92. local st, msg = load(aux, "k")
  93. print("\nmemory: ", collectgarbage'count' * 1024)
  94. msg = string.match(msg, "^[^\n]+") -- get only first line
  95. print("expected error: ", msg)
  96. end
  97. function toomanyconst ()
  98. print("loading function with too many constants")
  99. loadrepfunc("function foo () return {0,",
  100. function (n)
  101. -- convert 'n' to a string in the format [["...",]],
  102. -- where '...' is a kind of number in base 128
  103. -- (in a range that does not include either the double quote
  104. -- and the escape.)
  105. return string.char(34,
  106. ((n // 128^0) & 127) + 128,
  107. ((n // 128^1) & 127) + 128,
  108. ((n // 128^2) & 127) + 128,
  109. ((n // 128^3) & 127) + 128,
  110. ((n // 128^4) & 127) + 128,
  111. 34, 44)
  112. end)
  113. end
  114. function toomanystr ()
  115. local a = {}
  116. local st, msg = pcall(function ()
  117. for i = 1, math.huge do
  118. if i % (0x100000) == 0 then
  119. io.stderr:write("(", i // 2^20, " M)")
  120. end
  121. a[i] = string.pack("I", i)
  122. end
  123. end)
  124. local size = #a
  125. a = collectgarbage'count'
  126. print("\nmemory:", a * 1024)
  127. print("expected error:", msg)
  128. print("size:", size)
  129. end
  130. function toomanyidx ()
  131. local a = {}
  132. local st, msg = pcall(function ()
  133. for i = 1, math.huge do
  134. if i % (0x100000) == 0 then
  135. io.stderr:write("(", i // 2^20, " M)")
  136. end
  137. a[i] = i
  138. end
  139. end)
  140. print("\nmemory: ", collectgarbage'count' * 1024)
  141. print("expected error: ", msg)
  142. print("size:", #a)
  143. end
  144. -- teststring()
  145. -- controlstruct()
  146. -- manylines()
  147. -- hugeid()
  148. -- toomanyinst()
  149. -- toomanyconst()
  150. -- toomanystr()
  151. toomanyidx()
  152. print "OK"