| 32 | const std::string kJazzerClassName = "com/code_intelligence/jazzer/Jazzer"; |
| 33 | |
| 34 | void StartLibFuzzer(std::unique_ptr<jazzer::JVM> jvm, |
| 35 | std::vector<std::string> argv) { |
| 36 | JNIEnv &env = jvm->GetEnv(); |
| 37 | jclass runner = env.FindClass(kJazzerClassName.c_str()); |
| 38 | if (runner == nullptr) { |
| 39 | env.ExceptionDescribe(); |
| 40 | exit(1); |
| 41 | } |
| 42 | jmethodID startDriver = env.GetStaticMethodID(runner, "main", "([[B)V"); |
| 43 | if (startDriver == nullptr) { |
| 44 | env.ExceptionDescribe(); |
| 45 | exit(1); |
| 46 | } |
| 47 | jclass byteArrayClass = env.FindClass("[B"); |
| 48 | if (byteArrayClass == nullptr) { |
| 49 | env.ExceptionDescribe(); |
| 50 | exit(1); |
| 51 | } |
| 52 | jobjectArray args = env.NewObjectArray(argv.size(), byteArrayClass, nullptr); |
| 53 | if (args == nullptr) { |
| 54 | env.ExceptionDescribe(); |
| 55 | exit(1); |
| 56 | } |
| 57 | for (jsize i = 0; i < argv.size(); ++i) { |
| 58 | jint len = argv[i].size(); |
| 59 | jbyteArray arg = env.NewByteArray(len); |
| 60 | if (arg == nullptr) { |
| 61 | env.ExceptionDescribe(); |
| 62 | exit(1); |
| 63 | } |
| 64 | // startDriver expects UTF-8 encoded strings that are not null-terminated. |
| 65 | env.SetByteArrayRegion(arg, 0, len, |
| 66 | reinterpret_cast<const jbyte *>(argv[i].data())); |
| 67 | if (env.ExceptionCheck()) { |
| 68 | env.ExceptionDescribe(); |
| 69 | exit(1); |
| 70 | } |
| 71 | env.SetObjectArrayElement(args, i, arg); |
| 72 | if (env.ExceptionCheck()) { |
| 73 | env.ExceptionDescribe(); |
| 74 | exit(1); |
| 75 | } |
| 76 | env.DeleteLocalRef(arg); |
| 77 | } |
| 78 | env.CallStaticVoidMethod(runner, startDriver, args); |
| 79 | // Should not return. |
| 80 | if (env.ExceptionCheck()) { |
| 81 | env.ExceptionDescribe(); |
| 82 | } |
| 83 | exit(1); |
| 84 | } |
| 85 | } // namespace |
| 86 | |
| 87 | int main(int argc, char **argv) { |