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 with various basic values (nulls, ints, etc). This may result in |
| 7 // Dart exceptions or hangs, but should never result in crashes or JavaScript |
| 8 // exceptions. |
| 9 |
| 10 library test.invoke_natives; |
| 11 |
| 12 import 'dart:mirrors'; |
| 13 import 'dart:async'; |
| 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 change the exit code, which may fool the test harness. |
| 24 'dart.io.exitCode', |
| 25 |
| 26 // Don't kill random other processes. |
| 27 'dart.io.Process.killPid', |
| 28 |
| 29 // Don't break into the debugger. |
| 30 'dart.developer.debugger', |
| 31 |
| 32 // Don't run blocking io calls. |
| 33 new RegExp(r".*Sync$"), |
| 34 |
| 35 // These prevent the test from exiting. |
| 36 'dart.io.sleep', |
| 37 'dart.io.HttpServer.HttpServer.listenOn', |
| 38 |
| 39 // These either cause the VM to segfault or throw uncatchable API errors. |
| 40 // TODO(15274): Fix them and remove from blacklist. |
| 41 'dart.io.SystemEncoding.decode', // Windows only |
| 42 'dart.io.SystemEncoding.encode', // Windows only |
| 43 |
| 44 // These construct an object with an uninitialized native field. |
| 45 // TODO(23869): We could make this safer, but making the failure non-fatal |
| 46 // would we worthless aside from this test. |
| 47 'dart.io.X509Certificate.X509Certificate._', |
| 48 'dart.io._X509Impl._X509Impl', |
| 49 |
| 50 // Don't call private methods in dart.async as they may circumvent the zoned |
| 51 // error handling below. |
| 52 new RegExp(r"^dart\.async\._.*$"), |
| 53 ]; |
| 54 |
| 55 bool isBlacklisted(Symbol qualifiedSymbol) { |
| 56 var qualifiedString = MirrorSystem.getName(qualifiedSymbol); |
| 57 for (var pattern in blacklist) { |
| 58 if (qualifiedString.contains(pattern)) { |
| 59 print('Skipping $qualifiedString'); |
| 60 return true; |
| 61 } |
| 62 } |
| 63 return false; |
| 64 } |
| 65 |
| 66 class Task { |
| 67 var name; |
| 68 var action; |
| 69 } |
| 70 var queue = new List(); |
| 71 |
| 72 checkMethod(MethodMirror m, ObjectMirror target, [origin]) { |
| 73 if (isBlacklisted(m.qualifiedName)) return; |
| 74 |
| 75 var task = new Task(); |
| 76 task.name = '${MirrorSystem.getName(m.qualifiedName)} from $origin'; |
| 77 |
| 78 if (m.isRegularMethod) { |
| 79 task.action = |
| 80 () => target.invoke(m.simpleName, |
| 81 new List.filled(m.parameters.length, fuzzArgument)); |
| 82 } else if (m.isGetter) { |
| 83 task.action = |
| 84 () => target.getField(m.simpleName); |
| 85 } else if (m.isSetter) { |
| 86 task.action = |
| 87 () => target.setField(m.simpleName, null); |
| 88 } else if (m.isConstructor) { |
| 89 return; |
| 90 } else { |
| 91 throw "Unexpected method kind"; |
| 92 } |
| 93 |
| 94 queue.add(task); |
| 95 } |
| 96 |
| 97 checkInstance(instanceMirror, origin) { |
| 98 ClassMirror klass = instanceMirror.type; |
| 99 while (klass != null) { |
| 100 instanceMirror.type.declarations.values |
| 101 .where((d) => d is MethodMirror && !d.isStatic) |
| 102 .forEach((m) => checkMethod(m, instanceMirror, origin)); |
| 103 klass = klass.superclass; |
| 104 } |
| 105 } |
| 106 |
| 107 checkClass(classMirror) { |
| 108 classMirror.declarations.values |
| 109 .where((d) => d is MethodMirror && d.isStatic) |
| 110 .forEach((m) => checkMethod(m, classMirror)); |
| 111 |
| 112 classMirror.declarations.values |
| 113 .where((d) => d is MethodMirror && d.isConstructor) |
| 114 .forEach((m) { |
| 115 if (isBlacklisted(m.qualifiedName)) return; |
| 116 var task = new Task(); |
| 117 task.name = MirrorSystem.getName(m.qualifiedName); |
| 118 |
| 119 task.action = () { |
| 120 var instance = classMirror.newInstance( |
| 121 m.constructorName, |
| 122 new List.filled(m.parameters.length, fuzzArgument)); |
| 123 checkInstance(instance, task.name); |
| 124 }; |
| 125 queue.add(task); |
| 126 }); |
| 127 } |
| 128 |
| 129 checkLibrary(libraryMirror) { |
| 130 print(libraryMirror.simpleName); |
| 131 if (isBlacklisted(libraryMirror.qualifiedName)) return; |
| 132 |
| 133 libraryMirror.declarations.values |
| 134 .where((d) => d is ClassMirror) |
| 135 .forEach(checkClass); |
| 136 |
| 137 libraryMirror.declarations.values |
| 138 .where((d) => d is MethodMirror) |
| 139 .forEach((m) => checkMethod(m, libraryMirror)); |
| 140 } |
| 141 |
| 142 var testZone; |
| 143 |
| 144 doOneTask() { |
| 145 if (queue.length == 0) { |
| 146 print('Done'); |
| 147 return; |
| 148 } |
| 149 |
| 150 var task = queue.removeLast(); |
| 151 print(task.name); |
| 152 try { |
| 153 task.action(); |
| 154 } catch(e) {} |
| 155 |
| 156 // Register the next task in a timer callback so as to yield to async code |
| 157 // scheduled in the current task. This isn't necessary for the test itself, |
| 158 // but is helpful when trying to figure out which function is responsible for |
| 159 // a crash. |
| 160 testZone.createTimer(Duration.ZERO, doOneTask); |
| 161 } |
| 162 |
| 163 var fuzzArgument; |
| 164 |
| 165 main() { |
| 166 fuzzArgument = null; |
| 167 fuzzArgument = 1; /// smi: ok |
| 168 fuzzArgument = false; /// false: ok |
| 169 fuzzArgument = 'string'; /// string: ok |
| 170 fuzzArgument = new List(0); /// emptyarray: ok |
| 171 |
| 172 print('Fuzzing with $fuzzArgument'); |
| 173 |
| 174 currentMirrorSystem().libraries.values.forEach(checkLibrary); |
| 175 |
| 176 var valueObjects = |
| 177 [true, false, null, [], {}, dynamic, |
| 178 0, 0xEFFFFFF, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 3.14159, |
| 179 "foo", 'blåbærgrød', 'Îñţérñåţîöñåļîžåţîờñ', "\u{1D11E}", #symbol]; |
| 180 valueObjects.forEach((v) => checkInstance(reflect(v), 'value object')); |
| 181 |
| 182 uncaughtErrorHandler(self, parent, zone, error, stack) {}; |
| 183 var zoneSpec = |
| 184 new ZoneSpecification(handleUncaughtError: uncaughtErrorHandler); |
| 185 testZone = Zone.current.fork(specification: zoneSpec); |
| 186 testZone.createTimer(Duration.ZERO, doOneTask); |
| 187 } |
OLD | NEW |