proxybug.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function test1() {
  6. var sc4 = WScript.LoadScript('function test(){ obj2.prop4 = {needMarshal:true}; }', 'samethread');
  7. var obj1 = new Proxy({}, { set: function (target, property, value) { Reflect.set(value); } })
  8. sc4.obj2 = obj1;
  9. sc4.test();
  10. obj1.prop4 = { needMarshal: false };
  11. obj1.prop5 = { needMarshal: false };
  12. };
  13. test1();
  14. function test2() {
  15. var bug = new Proxy(new Array(1), { has: () => true });
  16. var a = bug.concat();
  17. if (a[0] !== undefined || a.length !== 1) {
  18. print('FAIL');
  19. }
  20. }
  21. test2();
  22. function test3() {
  23. var obj1 = {};
  24. var arrObj0 = {};
  25. var x = 1
  26. var proxyHandler = {};
  27. proxyHandler['get'] = function () { };
  28. proxyHandler['defineProperty'] = function (target, property, descriptor) {
  29. return Reflect.defineProperty(target, property, descriptor);
  30. };
  31. proxyHandler['isExtensible'] = function (target) {
  32. arrObj0.prop0;
  33. arrObj0 = new Proxy(arrObj0, proxyHandler);
  34. return Reflect.isExtensible(target);
  35. };
  36. arrObj0 = new Proxy(arrObj0, proxyHandler);
  37. arrObj0 = new Proxy(arrObj0, proxyHandler);
  38. do {
  39. var sc3 = WScript.LoadScript('function test(){arrObj0.length = arrObj0[obj1];}', 'samethread');
  40. sc3.obj1 = obj1;
  41. sc3.arrObj0 = arrObj0;
  42. sc3.test();
  43. } while (x--);
  44. }
  45. test3();
  46. function test4() {
  47. var func3 = function () { };
  48. var ary = Array();
  49. var proxyHandler = {};
  50. var ownkeys = Reflect.ownKeys(ary);
  51. proxyHandler['ownKeys'] = function () {
  52. func3() == 0;
  53. return ownkeys;
  54. };
  55. ary = new Proxy(ary, proxyHandler);
  56. var sc2 = WScript.LoadScript('function test(){for (var x in ary);}', 'samethread');
  57. sc2.ary = ary;
  58. sc2.func3 = func3;
  59. sc2.test();
  60. }
  61. test4();
  62. function test5() {
  63. function makeArrayLength() {
  64. }
  65. function leaf() {
  66. }
  67. var obj1 = {};
  68. var arrObj0 = {};
  69. var func1 = function () {
  70. };
  71. obj1.method0 = func1;
  72. obj1.method1 = func1;
  73. var protoObj1 = Object();
  74. var proxyHandler = {};
  75. var v0 = new Proxy(obj1.method0, proxyHandler);
  76. var sc9 = WScript.LoadScript('', 'samethread');
  77. sc9.arrObj0 = arrObj0;
  78. sc9.obj1 = obj1;
  79. sc9.protoObj1 = protoObj1;
  80. sc9.v0 = v0;
  81. sc9.makeArrayLength = makeArrayLength;
  82. sc9.leaf = leaf;
  83. var sc9_cctx = sc9.WScript.LoadScript('function test() {var b = 1; arrObj0.length= makeArrayLength((arrObj0.length != arrObj0.prop4));\n var d = obj1.method1.call(protoObj1 , (((new v0((protoObj1.length >>>= -866043558),(protoObj1.prop0 = 1),leaf,leaf)) , (b ? 1 : 16678541)) & (typeof(arrObj0.prop4) == \'number\') ), ((1) * (d %= (1 * obj1.length - b)) + obj1.method0.call(arrObj0 , ("a" instanceof ((typeof RegExp == \'function\' ) ? RegExp : Object)), (\'prop0\' in arrObj0), leaf, leaf)), leaf, leaf);\n var uniqobj21 = Object.create(arrObj0);\n arrObj0.length= makeArrayLength(((1 ) % -520343586));\n ;\n }');
  84. sc9_cctx.test();
  85. }
  86. test5();
  87. function test6() {
  88. var global = WScript.LoadScript("", "samethread");
  89. var OProxy = global.Proxy;
  90. var desc;
  91. var p = new OProxy({}, {
  92. defineProperty: function(_, __, _desc) {
  93. desc = _desc;
  94. return desc;
  95. }
  96. });
  97. p.a = 0;
  98. }
  99. test6();
  100. function test7() {
  101. var obj0 = {};
  102. var arrObj0 = {};
  103. var func3 = function () {
  104. return typeof func3.caller == 'object';
  105. };
  106. obj0.method0 = func3;
  107. var protoObj0 = Object(obj0);
  108. var proxyHandler = {};
  109. do {
  110. } while (protoObj0.method0());
  111. var v0 = new Proxy(obj0.method0, proxyHandler);
  112. var sc0 = WScript.LoadScript('', 'samethread');
  113. sc0.v0 = v0;
  114. sc0.arrObj0 = arrObj0;
  115. sc0.obj0 = obj0;
  116. sc0.protoObj0 = protoObj0;
  117. var sc0_cctx = sc0.WScript.LoadScript('function foo() { var _oo1obj = (function(_oo1a) {\n var _oo1obj = {};\n _oo1obj.prop1 = v0(arrObj0);\n return _oo1obj;\n})(typeof(arrObj0.prop0) == \'object\') ;\n }');
  118. sc0_cctx.foo();
  119. }
  120. test7();
  121. // In Chakra when calling "foo(arg1, arg2, ...)", we assume all the
  122. // participating components -- "foo", "arg1", "arg2"... -- are either from
  123. // executing context (caller) or properly cross-site marshalled.
  124. //
  125. // BUG: JavascriptProxy implementation has a lot of direct calls to
  126. // trap(handler, target, ...)
  127. // "handler", "target" were based on proxy context at proxy creation time.
  128. // They both need marshalling to executing context.
  129. // Except: apply & construct traps are behind CrossSiteProxyCallTrap thus they
  130. // work fine without marshalling themselves.
  131. // defineProperty trap needs handler/target marshalling
  132. (function() {
  133. var g = WScript.LoadScript(
  134. 'function test(x, name, desc) { return Object.defineProperty(x, name, desc) }',
  135. 'samethread');
  136. var p = new Proxy({}, {
  137. defineProperty: g.test
  138. });
  139. g.test(p, 'abc', {value: 1});
  140. //
  141. // This call invokes the trap in executing "g" context. Since the trap (value
  142. // g.test) itself is also from "g" context, CrossSite::CommonThunk determines
  143. // no marshalling required. However, handler/target args were from "p"
  144. // context and need marshalling.
  145. })();
  146. // deleteProperty trap needs handler/target marshalling
  147. (function() {
  148. var g = WScript.LoadScript(
  149. 'function test(x, name) { delete x.name }', 'samethread');
  150. var p = new Proxy({}, {
  151. deleteProperty: g.test
  152. });
  153. g.test(p);
  154. })();
  155. // get trap needs handler/target marshalling
  156. (function() {
  157. var g = WScript.LoadScript(
  158. 'function test(x) { x.name }', 'samethread');
  159. var p = new Proxy({}, {
  160. get: g.test
  161. });
  162. g.test(p);
  163. })();
  164. // getOwnPropertyDescriptor trap needs handler/target marshalling
  165. (function() {
  166. var g = WScript.LoadScript(
  167. 'function test(x, name) { Object.getOwnPropertyDescriptor(x, name) }',
  168. 'samethread');
  169. var p = new Proxy({}, {
  170. getOwnPropertyDescriptor: g.test
  171. });
  172. g.test(p);
  173. })();
  174. // getPrototypeOf trap needs handler/target marshalling
  175. (function() {
  176. var g = WScript.LoadScript(
  177. 'function test(x) { x.name; return x.__proto__ }', 'samethread');
  178. var p = new Proxy({}, {
  179. getPrototypeOf: g.test
  180. });
  181. g.test({name: p});
  182. //
  183. // This test is a bit different to others because getPrototypeOf is called
  184. // implicitly when marshalling object and prototype chain.
  185. //
  186. // Above call passes a crosss-site object to "g" context. That call accesses
  187. // x.name (the proxy) in "g" context, thus will marshal value proxy p and
  188. // its prototype chain to "g" context. Accessing prototype chain calls the
  189. // trap (value g.test) in "g" context. Since the trap itself is also from "g"
  190. // context, CrossSite::CommonThunk determines no marshalling required.
  191. // However, handler/target args were from "p" context and need marshalling.
  192. })();
  193. // has trap needs handler/target marshalling
  194. (function() {
  195. var g = WScript.LoadScript(
  196. 'function test(x, name) { name in x }', 'samethread');
  197. var p = new Proxy({}, {
  198. has: g.test
  199. });
  200. g.test(p);
  201. })();
  202. // isExtensible trap needs handler/target marshalling
  203. (function() {
  204. var g = WScript.LoadScript(
  205. 'function test(x) { return Object.isExtensible(x) }', 'samethread');
  206. var p = new Proxy({}, {
  207. isExtensible: g.test
  208. });
  209. g.test(p);
  210. })();
  211. // ownKeys trap needs handler/target marshalling
  212. (function() {
  213. var g = WScript.LoadScript(
  214. 'function test(x) { return Object.getOwnPropertyNames(x) }',
  215. 'samethread');
  216. var p = new Proxy(function() {}, {
  217. ownKeys: g.test
  218. });
  219. g.test(p);
  220. })();
  221. // preventExtensions trap needs handler/target marshalling
  222. (function() {
  223. var g = WScript.LoadScript(
  224. 'function test(x) { return Object.preventExtensions(x) }', 'samethread');
  225. var p = new Proxy({}, {
  226. preventExtensions: g.test
  227. });
  228. g.test(p);
  229. })();
  230. // set trap needs handler/target marshalling
  231. (function() {
  232. var g = WScript.LoadScript(
  233. 'function test(x, name, val) { x[name] = val }', 'samethread');
  234. var p = new Proxy({}, {
  235. set: g.test
  236. });
  237. g.test(p, 'abc', 1);
  238. })();
  239. // setPrototypeOf trap needs handler/target marshalling
  240. (function() {
  241. var g = WScript.LoadScript(
  242. 'function test(x, proto) { return Object.setPrototypeOf(x, proto) }',
  243. 'samethread');
  244. var p = new Proxy({}, {
  245. setPrototypeOf: g.test
  246. });
  247. g.test(p, {});
  248. })();
  249. print('PASS');