heavy.lua 4.4 KB

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