[dart/fuzzer] Add kbc execution mode

Rationale:
This enables testing of bytecode interpretation,
compilation, or mixed mode.


Change-Id: Ie4322c62f1f6a1abe4896ec98c4edf40d25b7135
Reviewed-on: https://dart-review.googlesource.com/c/78186
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Aart Bik <ajcbik@google.com>
diff --git a/runtime/tools/dartfuzz/README.md b/runtime/tools/dartfuzz/README.md
index cfd8b5e..5e05c29 100644
--- a/runtime/tools/dartfuzz/README.md
+++ b/runtime/tools/dartfuzz/README.md
@@ -48,13 +48,16 @@
     --dart-top        : sets DART_TOP explicitly through command line
     --mode1           : m1
     --mode2           : m2, and values one of
-        jit-[debug-]ia32  = Dart JIT (ia32)
-        jit-[debug-]x64   = Dart JIT (x64)
-        jit-[debug-]arm32 = Dart JIT (simarm)
-        jit-[debug-]arm64 = Dart JIT (simarm64)
-        aot-[debug-]x64   = Dart AOT (x64)
-        aot-[debug-]arm64 = Dart AOT (simarm64)
-        js                = dart2js + JS
+        jit-[debug-]ia32    = Dart JIT (ia32)
+        jit-[debug-]x64     = Dart JIT (x64)
+        jit-[debug-]arm32   = Dart JIT (simarm)
+        jit-[debug-]arm64   = Dart JIT (simarm64)
+        aot-[debug-]x64     = Dart AOT (x64)
+        aot-[debug-]arm64   = Dart AOT (simarm64)
+        kbc-int-[debug-]x64 = Dart KBC (interpreted bytecode)
+        kbc-mix-[debug-]x64 = Dart KBC (mixed-mode bytecode)
+        kbc-cmp-[debug-]x64 = Dart KBC (compiled bytecode)
+        js                  = dart2js + JS
 
 If no modes are given, a random JIT and/or AOT combination is used.
 
diff --git a/runtime/tools/dartfuzz/dartfuzz_test.dart b/runtime/tools/dartfuzz/dartfuzz_test.dart
index 2c36548..024da6a 100644
--- a/runtime/tools/dartfuzz/dartfuzz_test.dart
+++ b/runtime/tools/dartfuzz/dartfuzz_test.dart
@@ -55,6 +55,8 @@
       return new TestRunnerJIT(getTag(mode), top, tmp, env);
     if (mode.startsWith('aot'))
       return new TestRunnerAOT(getTag(mode), top, tmp, env);
+    if (mode.startsWith('kbc'))
+      return new TestRunnerKBC(mode, getTag(mode), top, tmp, env);
     if (mode.startsWith('js')) return new TestRunnerJS(top, tmp, env);
     throw ('unknown runner in mode: $mode');
   }
@@ -120,6 +122,51 @@
   Map<String, String> env;
 }
 
+/// Concrete test runner of bytecode.
+class TestRunnerKBC implements TestRunner {
+  TestRunnerKBC(
+      String mode, String tag, String top, String tmp, Map<String, String> e) {
+    generate = '$top/pkg/vm/tool/gen_kernel';
+    platform = '--platform=$top/out/$tag/vm_platform_strong.dill';
+    dill = '$tmp/out.dill';
+    dart = '$top/out/$tag/dart';
+    fileName = '$tmp/fuzz.dart';
+    env = e;
+    cmd = [dart];
+    if (mode.startsWith('kbc-int')) {
+      description = 'KBC-INT-${tag}';
+      cmd += ['--enable-interpreter', '--compilation-counter-threshold=-1'];
+    } else if (mode.startsWith('kbc-mix')) {
+      description = 'KBC-MIX-${tag}';
+      cmd += ['--enable-interpreter'];
+    } else if (mode.startsWith('kbc-cmp')) {
+      description = 'KBC-CMP-${tag}';
+      cmd += ['--use-bytecode-compiler'];
+    } else {
+      throw ('unknown KBC mode: $mode');
+    }
+    cmd += [dill];
+  }
+
+  TestResult run() {
+    TestResult result = runCommand(
+        [generate, '--gen-bytecode', platform, '-o', dill, fileName], env);
+    if (result.code != ResultCode.success) {
+      return result;
+    }
+    return runCommand(cmd, env);
+  }
+
+  String description;
+  String generate;
+  String platform;
+  String dill;
+  String dart;
+  String fileName;
+  List<String> cmd;
+  Map<String, String> env;
+}
+
 /// Concrete test runner of Dart2JS.
 class TestRunnerJS implements TestRunner {
   TestRunnerJS(String top, String tmp, Map<String, String> e) {
@@ -349,7 +396,7 @@
     // Random when not set.
     if (mode == null || mode == '') {
       // Pick a mode at random (cluster), different from other.
-      const cluster_modes = 10;
+      const cluster_modes = 16;
       Random rand = new Random();
       do {
         mode = modes[rand.nextInt(cluster_modes)];
@@ -387,6 +434,12 @@
     'jit-arm64',
     'aot-debug-x64',
     'aot-x64',
+    'kbc-int-debug-x64',
+    'kbc-cmp-debug-x64',
+    'kbc-mix-debug-x64',
+    'kbc-int-x64',
+    'kbc-cmp-x64',
+    'kbc-mix-x64',
     // Times out often:
     'aot-arm64',
     'aot-debug-arm64',