• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 Code Intelligence GmbH
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <dlfcn.h>
16 #include <jni.h>
17 
18 #include <cstdlib>
19 #include <cstring>
20 #include <iostream>
21 
22 #include "com_code_intelligence_jazzer_android_AndroidRuntime.h"
23 
24 const char *RUNTIME_LIBRARY = "libandroid_runtime.so";
25 
26 // Register native methods from the Android Runtime (ART) framework.
27 [[maybe_unused]] jint
Java_com_code_1intelligence_jazzer_android_AndroidRuntime_registerNatives(JNIEnv * env,jclass clazz)28 Java_com_code_1intelligence_jazzer_android_AndroidRuntime_registerNatives(
29     JNIEnv *env, jclass clazz) {
30   void *handle = nullptr;
31   handle = dlopen(RUNTIME_LIBRARY, RTLD_LAZY);
32 
33   if (handle == nullptr) {
34     std::cerr
35         << "ERROR: Unable to locate runtime library. Check LD_LIBRARY_PATH."
36         << std::endl;
37     exit(1);
38   }
39   // reset errors
40   dlerror();
41 
42   // Load the symbol from library
43   typedef jint (*Register_Frameworks_t)(JNIEnv *);
44   Register_Frameworks_t Register_Frameworks;
45 
46   Register_Frameworks = reinterpret_cast<Register_Frameworks_t>(
47       dlsym(handle, "registerFrameworkNatives"));
48   const char *dlsym_error = dlerror();
49   if (dlsym_error) {
50     std::cerr << "ERROR: Unable to invoke registerFrameworkNatives."
51               << std::endl;
52     exit(1);
53   }
54 
55   if (Register_Frameworks == nullptr) {
56     std::cerr << "ERROR: Register_Frameworks is null." << std::endl;
57     exit(1);
58   }
59 
60   return Register_Frameworks(env);
61 }
62