aboutsummaryrefslogtreecommitdiff
path: root/driver/jvm_tooling.cpp
diff options
context:
space:
mode:
authorFabian Meumertzheim <fabian@meumertzhe.im>2022-08-04 11:24:37 +0200
committerFabian Meumertzheim <fabian@meumertzhe.im>2022-08-10 12:50:59 +0200
commitfb7cb0c484f2158754c95c320b91c30d1b70352d (patch)
tree629193de8e593bcf7de326db251d481eb9ca5c51 /driver/jvm_tooling.cpp
parent33bfc7db10bb7adbf3dfc944cbb3711f19bf181b (diff)
downloadjazzer-api-fb7cb0c484f2158754c95c320b91c30d1b70352d.tar.gz
driver: Remove now unused code
Test uses of JVM methods have been replaced with the equivalent standard JNI function.
Diffstat (limited to 'driver/jvm_tooling.cpp')
-rw-r--r--driver/jvm_tooling.cpp167
1 files changed, 2 insertions, 165 deletions
diff --git a/driver/jvm_tooling.cpp b/driver/jvm_tooling.cpp
index c69c044f..05b118ab 100644
--- a/driver/jvm_tooling.cpp
+++ b/driver/jvm_tooling.cpp
@@ -101,8 +101,8 @@ DECLARE_bool(fake_pcs);
#define ARG_SEPARATOR ":"
#endif
-extern "C" JNIEXPORT jint JNICALL JNI_OnLoad_jazzer_initialize(JavaVM *vm,
- void *) {
+extern "C" [[maybe_unused]] JNIEXPORT jint JNICALL
+JNI_OnLoad_jazzer_initialize(JavaVM *vm, void *) {
return JNI_VERSION_1_8;
}
@@ -344,167 +344,4 @@ JVM::JVM(std::string_view executable_path, std::string_view seed) {
JNIEnv &JVM::GetEnv() const { return *env_; }
JVM::~JVM() { jvm_->DestroyJavaVM(); }
-
-jclass JVM::FindClass(std::string class_name) const {
- auto &env = GetEnv();
- std::replace(class_name.begin(), class_name.end(), '.', '/');
- const auto ret = env.FindClass(class_name.c_str());
- if (ret == nullptr) {
- if (env.ExceptionCheck()) {
- env.ExceptionDescribe();
- throw std::runtime_error(
- absl::StrFormat("Could not find class %s", class_name));
- } else {
- throw std::runtime_error(absl::StrFormat(
- "Java class '%s' not found without exception", class_name));
- }
- }
- return ret;
-}
-
-jmethodID JVM::GetStaticMethodID(jclass jclass, const std::string &jmethod,
- const std::string &signature,
- bool is_required) const {
- auto &env = GetEnv();
- const auto ret =
- env.GetStaticMethodID(jclass, jmethod.c_str(), signature.c_str());
- if (ret == nullptr) {
- if (is_required) {
- if (env.ExceptionCheck()) {
- env.ExceptionDescribe();
- }
- throw std::runtime_error(
- absl::StrFormat("Static method '%s' not found", jmethod));
- } else {
- LOG(INFO) << "did not find method " << jmethod << " with signature "
- << signature;
- env.ExceptionClear();
- }
- }
- return ret;
-}
-
-jmethodID JVM::GetMethodID(jclass jclass, const std::string &jmethod,
- const std::string &signature) const {
- auto &env = GetEnv();
- const auto ret = env.GetMethodID(jclass, jmethod.c_str(), signature.c_str());
- if (ret == nullptr) {
- if (env.ExceptionCheck()) {
- env.ExceptionDescribe();
- }
- throw std::runtime_error(absl::StrFormat("Method '%s' not found", jmethod));
- }
- return ret;
-}
-
-jfieldID JVM::GetStaticFieldID(jclass class_id, const std::string &field_name,
- const std::string &type) const {
- auto &env = GetEnv();
- const auto ret =
- env.GetStaticFieldID(class_id, field_name.c_str(), type.c_str());
- if (ret == nullptr) {
- if (env.ExceptionCheck()) {
- env.ExceptionDescribe();
- }
- throw std::runtime_error(
- absl::StrFormat("Field '%s' not found", field_name));
- }
- return ret;
-}
-
-ExceptionPrinter::ExceptionPrinter(JVM &jvm)
- : jvm_(jvm),
- string_writer_class_(jvm.FindClass("java/io/StringWriter")),
- string_writer_constructor_(
- jvm.GetMethodID(string_writer_class_, "<init>", "()V")),
- string_writer_to_string_method_(jvm.GetMethodID(
- string_writer_class_, "toString", "()Ljava/lang/String;")),
- print_writer_class_(jvm.FindClass("java/io/PrintWriter")),
- print_writer_constructor_(jvm.GetMethodID(print_writer_class_, "<init>",
- "(Ljava/io/Writer;)V")) {
- auto throwable_class = jvm.FindClass("java/lang/Throwable");
- print_stack_trace_method_ = jvm.GetMethodID(
- throwable_class, "printStackTrace", "(Ljava/io/PrintWriter;)V");
- if (FLAGS_hooks) {
- exception_utils_ = jvm.FindClass(kExceptionUtilsClassName);
- compute_dedup_token_method_ = jvm.GetStaticMethodID(
- exception_utils_, "computeDedupToken", "(Ljava/lang/Throwable;)J");
- preprocess_throwable_method_ =
- jvm.GetStaticMethodID(exception_utils_, "preprocessThrowable",
- "(Ljava/lang/Throwable;)Ljava/lang/Throwable;");
- }
-}
-
-// The JNI way of writing:
-// StringWriter stringWriter = new StringWriter();
-// PrintWriter printWriter = new PrintWriter(stringWriter);
-// e.printStackTrace(printWriter);
-// return stringWriter.toString();
-std::string ExceptionPrinter::getStackTrace(jthrowable exception) const {
- auto &env = jvm_.GetEnv();
- if (exception == nullptr) {
- return "";
- }
-
- auto string_writer =
- env.NewObject(string_writer_class_, string_writer_constructor_);
- if (string_writer == nullptr) {
- env.ExceptionDescribe();
- return "";
- }
- auto print_writer = env.NewObject(print_writer_class_,
- print_writer_constructor_, string_writer);
- if (print_writer == nullptr) {
- env.ExceptionDescribe();
- return "";
- }
-
- env.CallVoidMethod(exception, print_stack_trace_method_, print_writer);
- env.DeleteLocalRef(print_writer);
- if (env.ExceptionCheck()) {
- env.ExceptionDescribe();
- return "";
- }
- auto exception_string_object = reinterpret_cast<jstring>(
- env.CallObjectMethod(string_writer, string_writer_to_string_method_));
- env.DeleteLocalRef(string_writer);
- if (env.ExceptionCheck()) {
- env.ExceptionDescribe();
- return "";
- }
-
- auto char_pointer = env.GetStringUTFChars(exception_string_object, nullptr);
- std::string exception_string(char_pointer);
- env.ReleaseStringUTFChars(exception_string_object, char_pointer);
- env.DeleteLocalRef(exception_string_object);
- return exception_string;
-}
-
-jthrowable ExceptionPrinter::preprocessException(jthrowable exception) const {
- if (exception == nullptr) return nullptr;
- auto &env = jvm_.GetEnv();
- if (!FLAGS_hooks || !preprocess_throwable_method_) return exception;
- auto processed_exception = (jthrowable)(env.CallStaticObjectMethod(
- exception_utils_, preprocess_throwable_method_, exception));
- if (env.ExceptionCheck()) {
- env.ExceptionDescribe();
- return exception;
- }
- return processed_exception;
-}
-
-jlong ExceptionPrinter::computeDedupToken(jthrowable exception) const {
- auto &env = jvm_.GetEnv();
- if (!FLAGS_hooks || exception == nullptr ||
- compute_dedup_token_method_ == nullptr)
- return 0;
- const auto dedup_token = env.CallStaticLongMethod(
- exception_utils_, compute_dedup_token_method_, exception);
- if (env.ExceptionCheck()) {
- env.ExceptionDescribe();
- return 0;
- }
- return dedup_token;
-}
-
} // namespace jazzer