Example #1
0
int main() {
  crazy_context_t* context = crazy_context_create();
  crazy_library_t* library;

  // Expect to find the library in the same directory than this executable.
  crazy_context_add_search_path_for_address(context, (void*)&main);

  crazy_context_set_java_vm(context, kJavaVM, JNI_VERSION_1_2);

  // Load libjni_lib.so, this should invoke its JNI_OnLoad() function
  // automatically.
  setenv(VARNAME, "INIT", 1);
  if (!crazy_library_open(&library, kJniLibName, context))
    Panic("Could not open library: %s\n", crazy_context_get_error(context));

  const char* env = getenv(VARNAME);
  if (strcmp(env, "LOADED"))
    Panic("JNI_OnLoad() hook was not called! %s is %s\n", VARNAME, env);

  crazy_library_close(library);
  env = getenv(VARNAME);
  if (strcmp(env, "UNLOADED"))
    Panic("JNI_OnUnload() hook was not called! %s is %s\n", VARNAME, env);

  // Now, change the minimum JNI version to JNI_VERSION_1_6, which should
  // prevent loading the library properly, since it only supports 1.2.
  crazy_context_set_java_vm(context, kJavaVM, JNI_VERSION_1_6);

  setenv(VARNAME, "INIT", 1);
  if (crazy_library_open(&library, kJniLibName, context))
    Panic("Could load the library with JNI_VERSION_1_6 > JNI_VERSION_1_2.");

  // Disable the feature, this shall load the library, but not call the
  // JNI_OnLoad() hook.
  crazy_context_set_java_vm(context, NULL, 0);

  setenv(VARNAME, "INIT", 1);
  if (!crazy_library_open(&library, kJniLibName, context))
    Panic("Could not load the library without a JavaVM handle !?\n");

  env = getenv(VARNAME);
  if (strcmp(env, "INIT"))
    Panic("JNI_OnLoad() was called, %s is %s (expected INIT)\n", VARNAME, env);

  crazy_library_close(library);
  env = getenv(VARNAME);
  if (strcmp(env, "INIT"))
    Panic(
        "JNI_OnUnload() was called, %s is %s (expected INIT)\n", VARNAME, env);

  crazy_context_destroy(context);

  return 0;
}
Example #2
0
File: load2.cpp Project: IRebo/utek
void ANativeActivity_onCreate(ANativeActivity * app, void * ud, size_t udsize) {
//int main() {
  crazy_context_t* context = crazy_context_create();
  crazy_library_t* library;
  crazy_library_t* library2;

 crazy_context_add_search_path_for_address(context,
                                            reinterpret_cast<void*>(&ANativeActivity_onCreate));

    LOGI("Loaded boostrap");
  // DEBUG
  //crazy_context_set_load_address(context, 0x20000000);

  if (!crazy_library_open(&library2, "libbass.so", context)) {
    LOGE("Could not open library: %s", crazy_context_get_error(context));
    exit(1);
//    Panic("Could not open library: %s\n", crazy_context_get_error(context));
  }
    LOGI("Loaded boostrap bass in");

  // Load libfoo.so
  if (!crazy_library_open(&library, "libutekxp-armeabi.so", context)) {
    LOGE("Could not open library: %s", crazy_context_get_error(context));
    exit(1);
//    Panic("Could not open library: %s\n", crazy_context_get_error(context));
  }

    LOGI("Loaded boostrap 2");
  // Find the "Foo" symbol.
  FunctionPtr foo_func;
  if (!crazy_library_find_symbol(
           library, "ANativeActivity_onCreate", reinterpret_cast<void**>(&foo_func))) {
    LOGE("Could not find 'Foo' in libfoo.so");
    exit(1);
    //Panic("Could not find 'Foo' in libfoo.so\n");
  }
    LOGI("Loaded boostrap3");

  // Call it.
  (*foo_func)(app, ud, udsize);

    LOGI("Loaded boostrap4");

  // Close the library.
 /* printf("Closing libfoo.so\n");
  crazy_library_close(library);

  crazy_context_destroy(context);*/

}
int main() {
  String exe_path = GetCurrentExecutableDirectory();

  TempDirectory temp_dir_1;
  TempDirectory temp_dir_2;

  // List of symbols in original libfoo.so and libfoo2.so, respectively.
  static const char* const kFooSymbols[] = {"Foo", NULL};
  static const char* const kFoo2Symbols[] = {"Foo2", NULL};

  // Copy libfoo.so to $TMPDIR1/libfoo.so
  CopyFile("libfoo.so", exe_path.c_str(), "libfoo.so", temp_dir_1.path());

  // Copy libfoo2.so to $TMPDIR2/libfoo.so
  CopyFile("libfoo2.so", exe_path.c_str(), "libfoo.so", temp_dir_2.path());

  // Create a new context object.
  crazy_context_t* context = crazy_context_create();
  crazy_library_t* library;

  // Reset search paths to a non-existing directory. Check that the library
  // can't be loaded.
  setenv("LD_LIBRARY_PATH", "/this-directory-does-not-exist", 1);
  crazy_context_reset_search_paths(context);
  CheckLibraryCantLoad("libfoo.so", context);

  // Add the search path to the current executable, this should load the
  // original
  // libfoo.so.
  crazy_context_add_search_path_for_address(context, (void*)&main);
  CheckLibrary("libfoo.so", kFooSymbols, kFoo2Symbols, context);

  // Reset search paths to use $TMPDIR2 then $TMPDIR1
  setenv("LD_LIBRARY_PATH", temp_dir_1.path(), 1);
  crazy_context_reset_search_paths(context);
  crazy_context_add_search_path(context, temp_dir_2.path());

  // Check that the copy of libfoo2.so is loaded.
  CheckLibrary("libfoo.so", kFoo2Symbols, kFooSymbols, context);

  // Reset search paths to use only $TMPDIR1
  crazy_context_reset_search_paths(context);

  // Check that the copy of libfoo.so is loaded.
  CheckLibrary("libfoo.so", kFooSymbols, kFoo2Symbols, context);

  crazy_context_destroy(context);

  return 0;
}
int main(int argc, char** argv) {
  const char* library_path = "libfoo.so";
  if (argc >= 2)
    library_path = argv[1];

  { ScopedTimer null_timer("empty"); }

  // Load the library with dlopen().
  void* lib;
  drop_caches();
  {
    ScopedTimer timer("dlopen");
    lib = dlopen(library_path, RTLD_NOW);
  }
  if (!lib)
    Panic("Could not load library with dlopen(): %s\n", dlerror());

  dlclose(lib);

  crazy_library_t* library;
  crazy_context_t* context = crazy_context_create();

  // Ensure the program looks in its own directory too.
  crazy_context_add_search_path_for_address(context,
                                            reinterpret_cast<void*>(&main));

  // Load the library with the crazy linker.
  drop_caches();
  {
    ScopedTimer timer("crazy_linker");
    // Load libfoo.so
    if (!crazy_library_open(&library, library_path, context)) {
      Panic("Could not open library: %s\n", crazy_context_get_error(context));
    }
  }
  crazy_library_close(library);

  // Load the library with the crazy linker. Preload libOpenSLES.so
  drop_caches();
  void* sles_lib = dlopen("libOpenSLES.so", RTLD_NOW);
  {
    ScopedTimer timer("crazy_linker (preload libOpenSLES.so)");
    // Load libfoo.so
    if (!crazy_library_open(&library, library_path, context)) {
      Panic("Could not open library: %s\n", crazy_context_get_error(context));
    }
  }
  crazy_library_close(library);
  dlclose(sles_lib);

  // Load the library with the crazy linker. Preload libOpenSLES.so
  {
    drop_caches();
    void* sys1_lib = dlopen("libandroid.so", RTLD_NOW);
    void* sys2_lib = dlopen("libjnigraphics.so", RTLD_NOW);
    void* sys3_lib = dlopen("libOpenSLES.so", RTLD_NOW);
    {
      ScopedTimer timer("crazy_linker (preload 3 system libs)");
      // Load libfoo.so
      if (!crazy_library_open(&library, library_path, context)) {
        Panic("Could not open library: %s\n", crazy_context_get_error(context));
      }
    }
    crazy_library_close(library);
    dlclose(sys3_lib);
    dlclose(sys2_lib);
    dlclose(sys1_lib);
  }

  // Load the library with the crazy linker. Create a shared RELRO as well.
  drop_caches();
  {
    ScopedTimer timer("crazy_linker (with RELRO)");
    // Load libfoo.so
    if (!crazy_library_open(&library, library_path, context)) {
      Panic("Could not open library: %s\n", crazy_context_get_error(context));
    }

    if (!crazy_library_enable_relro_sharing(library, context)) {
      Panic("Could not create shared RELRO: %s\n",
            crazy_context_get_error(context));
    }
  }
  crazy_library_close(library);

  printf("OK\n");
  return 0;
}