ExecutionConstraintTests.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.Threading;
  3. using Jint.Runtime;
  4. using Xunit;
  5. namespace Jint.Tests.Runtime
  6. {
  7. public class ExecutionConstraintTests
  8. {
  9. [Fact]
  10. public void ShouldThrowStatementCountOverflow()
  11. {
  12. Assert.Throws<StatementsCountOverflowException>(
  13. () => new Engine(cfg => cfg.MaxStatements(100)).Evaluate("while(true);")
  14. );
  15. }
  16. [Fact]
  17. public void ShouldThrowMemoryLimitExceeded()
  18. {
  19. Assert.Throws<MemoryLimitExceededException>(
  20. () => new Engine(cfg => cfg.LimitMemory(2048)).Evaluate("a=[]; while(true){ a.push(0); }")
  21. );
  22. }
  23. [Fact]
  24. public void ShouldThrowTimeout()
  25. {
  26. Assert.Throws<TimeoutException>(
  27. () => new Engine(cfg => cfg.TimeoutInterval(new TimeSpan(0, 0, 0, 0, 500))).Evaluate("while(true);")
  28. );
  29. }
  30. [Fact]
  31. public void ShouldThrowExecutionCanceled()
  32. {
  33. Assert.Throws<ExecutionCanceledException>(
  34. () =>
  35. {
  36. using (var tcs = new CancellationTokenSource())
  37. using (var waitHandle = new ManualResetEvent(false))
  38. {
  39. var engine = new Engine(cfg => cfg.CancellationToken(tcs.Token));
  40. ThreadPool.QueueUserWorkItem(state =>
  41. {
  42. waitHandle.WaitOne();
  43. tcs.Cancel();
  44. });
  45. engine.SetValue("waitHandle", waitHandle);
  46. engine.Evaluate(@"
  47. function sleep(millisecondsTimeout) {
  48. var totalMilliseconds = new Date().getTime() + millisecondsTimeout;
  49. while (new Date() < totalMilliseconds) { }
  50. }
  51. sleep(100);
  52. waitHandle.Set();
  53. sleep(5000);
  54. ");
  55. }
  56. }
  57. );
  58. }
  59. [Fact]
  60. public void CanDiscardRecursion()
  61. {
  62. var script = @"var factorial = function(n) {
  63. if (n>1) {
  64. return n * factorial(n - 1);
  65. }
  66. };
  67. var result = factorial(500);
  68. ";
  69. Assert.Throws<RecursionDepthOverflowException>(
  70. () => new Engine(cfg => cfg.LimitRecursion()).Execute(script)
  71. );
  72. }
  73. [Fact]
  74. public void ShouldDiscardHiddenRecursion()
  75. {
  76. var script = @"var renamedFunc;
  77. var exec = function(callback) {
  78. renamedFunc = callback;
  79. callback();
  80. };
  81. var result = exec(function() {
  82. renamedFunc();
  83. });
  84. ";
  85. Assert.Throws<RecursionDepthOverflowException>(
  86. () => new Engine(cfg => cfg.LimitRecursion()).Execute(script)
  87. );
  88. }
  89. [Fact]
  90. public void ShouldRecognizeAndDiscardChainedRecursion()
  91. {
  92. var script = @" var funcRoot, funcA, funcB, funcC, funcD;
  93. var funcRoot = function() {
  94. funcA();
  95. };
  96. var funcA = function() {
  97. funcB();
  98. };
  99. var funcB = function() {
  100. funcC();
  101. };
  102. var funcC = function() {
  103. funcD();
  104. };
  105. var funcD = function() {
  106. funcRoot();
  107. };
  108. funcRoot();
  109. ";
  110. Assert.Throws<RecursionDepthOverflowException>(
  111. () => new Engine(cfg => cfg.LimitRecursion()).Execute(script)
  112. );
  113. }
  114. [Fact]
  115. public void ShouldProvideCallChainWhenDiscardRecursion()
  116. {
  117. var script = @" var funcRoot, funcA, funcB, funcC, funcD;
  118. var funcRoot = function() {
  119. funcA();
  120. };
  121. var funcA = function() {
  122. funcB();
  123. };
  124. var funcB = function() {
  125. funcC();
  126. };
  127. var funcC = function() {
  128. funcD();
  129. };
  130. var funcD = function() {
  131. funcRoot();
  132. };
  133. funcRoot();
  134. ";
  135. RecursionDepthOverflowException exception = null;
  136. try
  137. {
  138. new Engine(cfg => cfg.LimitRecursion()).Execute(script);
  139. }
  140. catch (RecursionDepthOverflowException ex)
  141. {
  142. exception = ex;
  143. }
  144. Assert.NotNull(exception);
  145. Assert.Equal("funcRoot->funcA->funcB->funcC->funcD", exception.CallChain);
  146. Assert.Equal("funcRoot", exception.CallExpressionReference);
  147. }
  148. [Fact]
  149. public void ShouldAllowShallowRecursion()
  150. {
  151. var script = @"var factorial = function(n) {
  152. if (n>1) {
  153. return n * factorial(n - 1);
  154. }
  155. };
  156. var result = factorial(8);
  157. ";
  158. new Engine(cfg => cfg.LimitRecursion(20)).Execute(script);
  159. }
  160. [Fact]
  161. public void ShouldDiscardDeepRecursion()
  162. {
  163. var script = @"var factorial = function(n) {
  164. if (n>1) {
  165. return n * factorial(n - 1);
  166. }
  167. };
  168. var result = factorial(38);
  169. ";
  170. Assert.Throws<RecursionDepthOverflowException>(
  171. () => new Engine(cfg => cfg.LimitRecursion(20)).Execute(script)
  172. );
  173. }
  174. [Fact]
  175. public void ShouldAllowRecursionLimitWithoutReferencedName()
  176. {
  177. const string input = @"(function () {
  178. var factorial = function(n) {
  179. if (n>1) {
  180. return n * factorial(n - 1);
  181. }
  182. };
  183. var result = factorial(38);
  184. })();
  185. ";
  186. var engine = new Engine(o => o.LimitRecursion(20));
  187. Assert.Throws<RecursionDepthOverflowException>(() => engine.Execute(input));
  188. }
  189. [Fact]
  190. public void ShouldLimitRecursionWithAllFunctionInstances()
  191. {
  192. var engine = new Engine(cfg =>
  193. {
  194. // Limit recursion to 5 invocations
  195. cfg.LimitRecursion(5);
  196. cfg.Strict();
  197. });
  198. var ex = Assert.Throws<RecursionDepthOverflowException>(() => engine.Evaluate(@"
  199. var myarr = new Array(5000);
  200. for (var i = 0; i < myarr.length; i++) {
  201. myarr[i] = function(i) {
  202. myarr[i + 1](i + 1);
  203. }
  204. }
  205. myarr[0](0);
  206. "));
  207. }
  208. [Fact]
  209. public void ShouldLimitRecursionWithGetters()
  210. {
  211. const string code = @"var obj = { get test() { return this.test + '2'; } }; obj.test;";
  212. var engine = new Engine(cfg => cfg.LimitRecursion(10));
  213. Assert.Throws<RecursionDepthOverflowException>(() => engine.Evaluate(code));
  214. }
  215. [Fact]
  216. public void ShouldLimitArraySizeForConcat()
  217. {
  218. var engine = new Engine(o => o.MaxStatements(1_000).MaxArraySize(1_000_000));
  219. Assert.Throws<MemoryLimitExceededException>(() => engine.Evaluate("for (let a = [1, 2, 3];; a = a.concat(a)) ;"));
  220. }
  221. [Fact]
  222. public void ShouldLimitArraySizeForFill()
  223. {
  224. var engine = new Engine(o => o.MaxStatements(1_000).MaxArraySize(1_000_000));
  225. Assert.Throws<MemoryLimitExceededException>(() => engine.Evaluate("var arr = Array(1000000000).fill(new Array(1000000000));"));
  226. }
  227. [Fact]
  228. public void ShouldLimitArraySizeForJoin()
  229. {
  230. var engine = new Engine(o => o.MaxStatements(1_000).MaxArraySize(1_000_000));
  231. Assert.Throws<MemoryLimitExceededException>(() => engine.Evaluate("new Array(2147483647).join('*')"));
  232. }
  233. }
  234. }