OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
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 | |
7 // should never result in crashes or JavaScript exceptions. | |
8 | |
9 library test.invoke_natives; | |
10 | |
11 import 'dart:mirrors'; | |
12 import 'dart:async'; | |
13 import 'package:expect/expect.dart'; | |
14 | |
15 // Methods to be skipped, by qualified name. | |
16 var blacklist = [ | |
17 // Don't recurse on this test. | |
18 'test.invoke_natives', | |
19 | |
20 // Don't exit the test pre-maturely. | |
21 'dart.io.exit', | |
22 | |
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 ]; | |
44 | |
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 { | |
54 var name; | |
55 var action; | |
56 } | |
57 var queue = new List(); | |
58 | |
59 checkMethod(MethodMirror m, ObjectMirror target, [origin]) { | |
60 if (isBlacklisted(m.qualifiedName)) return; | |
61 | |
62 var task = new Task(); | |
63 task.name = '${MirrorSystem.getName(m.qualifiedName)} from $origin'; | |
64 | |
65 if (m.isRegularMethod) { | |
66 task.action = | |
67 () => target.invoke(m.simpleName, new List(m.parameters.length)); | |
68 } else if (m.isGetter) { | |
69 task.action = | |
70 () => target.getField(m.simpleName); | |
71 } else if (m.isSetter) { | |
72 task.action = | |
73 () => target.setField(m.simpleName, null); | |
74 } else if (m.isConstructor) { | |
75 return; | |
76 } else { | |
77 throw "Unexpected method kind"; | |
78 } | |
79 | |
80 queue.add(task); | |
81 } | |
82 | |
83 checkInstance(instanceMirror, origin) { | |
84 ClassMirror klass = instanceMirror.type; | |
85 while (klass != null) { | |
86 instanceMirror.type.declarations.values | |
87 .where((d) => d is MethodMirror && !d.isStatic) | |
88 .forEach((m) => checkMethod(m, instanceMirror, origin)); | |
89 klass = klass.superclass; | |
90 } | |
91 } | |
92 | |
93 checkClass(classMirror) { | |
94 classMirror.declarations.values | |
95 .where((d) => d is MethodMirror && d.isStatic) | |
96 .forEach((m) => checkMethod(m, classMirror)); | |
97 | |
98 classMirror.declarations.values | |
99 .where((d) => d is MethodMirror && d.isConstructor) | |
100 .forEach((m) { | |
101 if (isBlacklisted(m.qualifiedName)) return; | |
102 var task = new Task(); | |
103 task.name = MirrorSystem.getName(m.qualifiedName); | |
104 | |
105 task.action = () { | |
106 var instance = classMirror.newInstance(m.constructorName, | |
107 new List(m.parameters.length)); | |
108 checkInstance(instance, task.name); | |
109 }; | |
110 queue.add(task); | |
111 }); | |
112 } | |
113 | |
114 checkLibrary(libraryMirror) { | |
115 print(libraryMirror.simpleName); | |
116 if (isBlacklisted(libraryMirror.qualifiedName)) return; | |
117 | |
118 libraryMirror.declarations.values | |
119 .where((d) => d is ClassMirror) | |
120 .forEach(checkClass); | |
121 | |
122 libraryMirror.declarations.values | |
123 .where((d) => d is MethodMirror) | |
124 .forEach((m) => checkMethod(m, libraryMirror)); | |
125 } | |
126 | |
127 var testZone; | |
128 | |
129 doOneTask() { | |
130 if (queue.length == 0) { | |
131 print('Done'); | |
132 return; | |
133 } | |
134 | |
135 var task = queue.removeLast(); | |
136 print(task.name); | |
137 try { | |
138 task.action(); | |
139 } catch(e) {} | |
140 | |
141 // 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, | |
143 // but is helpful when trying to figure out which function is responsible for | |
144 // a crash. | |
145 testZone.createTimer(Duration.ZERO, doOneTask); | |
146 } | |
147 | |
148 main([args]) { | |
149 currentMirrorSystem().libraries.values.forEach(checkLibrary); | |
150 | |
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) {}; | |
158 var zoneSpec = | |
159 new ZoneSpecification(handleUncaughtError: uncaughtErrorHandler); | |
160 testZone = Zone.current.fork(specification: zoneSpec); | |
161 testZone.createTimer(Duration.ZERO, doOneTask); | |
162 } | |
OLD | NEW |