OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // This test reflectively enumerates all the methods in the system and tries to | 5 // This test reflectively enumerates all the methods in the system and tries to |
6 // invoke them will all nulls. This may result in Dart exceptions or hangs, but | 6 // invoke them will all nulls. This may result in Dart exceptions or hangs, but |
7 // should never result in crashes or JavaScript exceptions. | 7 // should never result in crashes or JavaScript exceptions. |
8 | 8 |
9 library test.invoke_natives; | 9 library test.invoke_natives; |
10 | 10 |
11 import 'dart:mirrors'; | 11 import 'dart:mirrors'; |
12 import 'dart:async'; | 12 import 'dart:async'; |
13 import 'package:expect/expect.dart'; | 13 import 'package:expect/expect.dart'; |
14 | 14 |
15 // Methods to be skipped, by qualified name. | 15 // Methods to be skipped, by qualified name. |
16 var blacklist = [ | 16 var blacklist = [ |
17 // Don't recurse on this test. | 17 // These prevent the test from exiting, typically by spawning another isolate. |
18 'test.invoke_natives', | 18 'dart.async._scheduleAsyncCallback', |
| 19 'dart.io._IOService.dispatch', |
| 20 'dart.isolate.RawReceivePort.RawReceivePort', |
| 21 'dart.isolate.ReceivePort.ReceivePort', |
| 22 'dart.isolate.ReceivePort.ReceivePort.fromRawReceivePort', |
| 23 'dart.isolate.ReceivePort.sendPort', |
| 24 'dart.isolate.ReceivePort.close', |
| 25 'dart.isolate.ReceivePort.listen', |
| 26 'dart.isolate.RawReceivePort.sendPort', |
| 27 'dart.isolate.RawReceivePort.close', |
| 28 'dart.isolate.RawReceivePort.handler=', |
19 | 29 |
20 // Don't exit the test pre-maturely. | 30 // These "crash" the VM (throw uncatchable API errors). |
21 'dart.io.exit', | 31 // TODO(15274): Fill in this list to make the test pass and provide coverage |
22 | 32 // against addition of new natives. |
23 // Don't run blocking io calls. | |
24 new RegExp(r".*Sync$"), | |
25 | |
26 // These prevent the test from exiting. | |
27 'dart.async._scheduleAsyncCallback', | |
28 'dart.async._setTimerFactoryClosure', | |
29 | |
30 'dart.isolate._startIsolate', | |
31 'dart.io.sleep', | |
32 'dart.io.HttpServer.HttpServer.listenOn', | |
33 | |
34 // These either cause the VM to segfault or throw uncatchable API errors. | |
35 // TODO(15274): Fix them and remove from blacklist. | |
36 'dart.io._IOService.dispatch', | |
37 new RegExp(r'.*_RandomAccessFile.*'), | |
38 'dart.io._StdIOUtils._socketType', | |
39 'dart.io._StdIOUtils._getStdioOutputStream', | |
40 'dart.io._Filter.newZLibInflateFilter', | |
41 'dart.io._Filter.newZLibDeflateFilter', | |
42 'dart.io._FileSystemWatcher._listenOnSocket', | |
43 ]; | 33 ]; |
44 | 34 |
45 bool isBlacklisted(Symbol qualifiedSymbol) { | |
46 var qualifiedString = MirrorSystem.getName(qualifiedSymbol); | |
47 for (var pattern in blacklist) { | |
48 if (qualifiedString.contains(pattern)) return true; | |
49 } | |
50 return false; | |
51 } | |
52 | |
53 class Task { | 35 class Task { |
54 var name; | 36 var name; |
55 var action; | 37 var action; |
56 } | 38 } |
57 var queue = new List(); | 39 var queue = new List(); |
58 | 40 |
59 checkMethod(MethodMirror m, ObjectMirror target, [origin]) { | 41 checkMethod(MethodMirror m, ObjectMirror target, [origin]) { |
60 if (isBlacklisted(m.qualifiedName)) return; | 42 if (blacklist.contains(MirrorSystem.getName(m.qualifiedName))) return; |
61 | 43 |
62 var task = new Task(); | 44 var task = new Task(); |
63 task.name = '${MirrorSystem.getName(m.qualifiedName)} from $origin'; | 45 task.name = '${MirrorSystem.getName(m.qualifiedName)} from $origin'; |
64 | 46 |
65 if (m.isRegularMethod) { | 47 if (m.isRegularMethod) { |
66 task.action = | 48 task.action = |
67 () => target.invoke(m.simpleName, new List(m.parameters.length)); | 49 () => target.invoke(m.simpleName, new List(m.parameters.length)); |
68 } else if (m.isGetter) { | 50 } else if (m.isGetter) { |
69 task.action = | 51 task.action = |
70 () => target.getField(m.simpleName); | 52 () => target.getField(m.simpleName); |
71 } else if (m.isSetter) { | 53 } else if (m.isSetter) { |
72 task.action = | 54 task.action = |
73 () => target.setField(m.simpleName, null); | 55 () => target.setField(m.simpleName, null); |
74 } else if (m.isConstructor) { | 56 } else if (m.isConstructor) { |
75 return; | 57 return; |
76 } else { | 58 } else { |
77 throw "Unexpected method kind"; | 59 throw "Unexpected method kind"; |
78 } | 60 } |
79 | 61 |
80 queue.add(task); | 62 queue.add(task); |
81 } | 63 } |
82 | 64 |
83 checkInstance(instanceMirror, origin) { | 65 checkInstance(instanceMirror, origin) { |
84 ClassMirror klass = instanceMirror.type; | 66 instanceMirror.type.declarations.values |
85 while (klass != null) { | 67 .where((d) => d is MethodMirror) |
86 instanceMirror.type.declarations.values | 68 .forEach((m) => checkMethod(m, instanceMirror, origin)); |
87 .where((d) => d is MethodMirror && !d.isStatic) | |
88 .forEach((m) => checkMethod(m, instanceMirror, origin)); | |
89 klass = klass.superclass; | |
90 } | |
91 } | 69 } |
92 | 70 |
93 checkClass(classMirror) { | 71 checkClass(classMirror) { |
94 classMirror.declarations.values | 72 classMirror.declarations.values |
95 .where((d) => d is MethodMirror && d.isStatic) | 73 .where((d) => d is MethodMirror) |
96 .forEach((m) => checkMethod(m, classMirror)); | 74 .forEach((m) => checkMethod(m, classMirror)); |
97 | 75 |
98 classMirror.declarations.values | 76 classMirror.declarations.values |
99 .where((d) => d is MethodMirror && d.isConstructor) | 77 .where((d) => d is MethodMirror) |
100 .forEach((m) { | 78 .forEach((m) { |
101 if (isBlacklisted(m.qualifiedName)) return; | 79 if (blacklist.contains(MirrorSystem.getName(m.qualifiedName))) return; |
| 80 if (!m.isConstructor) return; |
102 var task = new Task(); | 81 var task = new Task(); |
103 task.name = MirrorSystem.getName(m.qualifiedName); | 82 task.name = MirrorSystem.getName(m.qualifiedName); |
104 | 83 |
105 task.action = () { | 84 task.action = () { |
106 var instance = classMirror.newInstance(m.constructorName, | 85 var instance = classMirror.newInstance(m.constructorName, |
107 new List(m.parameters.length)); | 86 new List(m.parameters.length)); |
108 checkInstance(instance, task.name); | 87 checkInstance(instance, task.name); |
109 }; | 88 }; |
110 queue.add(task); | 89 queue.add(task); |
111 }); | 90 }); |
112 } | 91 } |
113 | 92 |
114 checkLibrary(libraryMirror) { | 93 checkLibrary(libraryMirror) { |
115 print(libraryMirror.simpleName); | 94 // Don't recurse on this test. |
116 if (isBlacklisted(libraryMirror.qualifiedName)) return; | 95 if (libraryMirror.simpleName == #test.invoke_natives) return; |
117 | 96 |
118 libraryMirror.declarations.values | 97 libraryMirror.declarations.values |
119 .where((d) => d is ClassMirror) | 98 .where((d) => d is ClassMirror) |
120 .forEach(checkClass); | 99 .forEach(checkClass); |
121 | 100 |
122 libraryMirror.declarations.values | 101 libraryMirror.declarations.values |
123 .where((d) => d is MethodMirror) | 102 .where((d) => d is MethodMirror) |
124 .forEach((m) => checkMethod(m, libraryMirror)); | 103 .forEach((m) => checkMethod(m, libraryMirror)); |
125 } | 104 } |
126 | 105 |
127 var testZone; | 106 var testZone; |
| 107 var debug = true; |
128 | 108 |
129 doOneTask() { | 109 doOneTask() { |
130 if (queue.length == 0) { | 110 if (queue.length == 0) { |
131 print('Done'); | 111 if (debug) print('Done'); |
132 return; | 112 return; |
133 } | 113 } |
134 | 114 |
135 var task = queue.removeLast(); | 115 var task = queue.removeLast(); |
136 print(task.name); | 116 if (debug) print(task.name); |
137 try { | 117 try { |
138 task.action(); | 118 task.action(); |
139 } catch(e) {} | 119 } catch(e) {} |
140 | |
141 // Register the next task in a timer callback so as to yield to async code | 120 // Register the next task in a timer callback so as to yield to async code |
142 // scheduled in the current task. This isn't necessary for the test itself, | 121 // scheduled in the current task. This isn't necessary for the test itself, |
143 // but is helpful when trying to figure out which function is responsible for | 122 // but is helpful when trying to figure out which function is responsible for |
144 // a crash. | 123 // a crash. |
145 testZone.createTimer(Duration.ZERO, doOneTask); | 124 testZone.createTimer(Duration.ZERO, doOneTask); |
146 } | 125 } |
147 | 126 |
148 main([args]) { | 127 main() { |
149 currentMirrorSystem().libraries.values.forEach(checkLibrary); | 128 currentMirrorSystem().libraries.values.forEach(checkLibrary); |
150 | 129 |
151 var valueObjects = | |
152 [true, false, null, | |
153 0, 0xEFFFFFF, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF, | |
154 "foo", 'blåbærgrød', 'Îñţérñåţîöñåļîžåţîờñ']; | |
155 valueObjects.forEach((v) => checkInstance(reflect(v), 'value object')); | |
156 | |
157 uncaughtErrorHandler(self, parent, zone, error, stack) {}; | 130 uncaughtErrorHandler(self, parent, zone, error, stack) {}; |
158 var zoneSpec = | 131 var zoneSpec = |
159 new ZoneSpecification(handleUncaughtError: uncaughtErrorHandler); | 132 new ZoneSpecification(handleUncaughtError: uncaughtErrorHandler); |
160 testZone = Zone.current.fork(specification: zoneSpec); | 133 testZone = Zone.current.fork(specification: zoneSpec); |
161 testZone.createTimer(Duration.ZERO, doOneTask); | 134 testZone.createTimer(Duration.ZERO, doOneTask); |
162 } | 135 } |
OLD | NEW |