Esempio n. 1
0
File: load2.cpp Progetto: 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);*/

}
Esempio n. 2
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;
}
int main() {
  crazy_context_t* context = crazy_context_create();
  crazy_library_t* library;

  putenv("TEST_VAR=INIT");

  // DEBUG
  crazy_context_set_load_address(context, 0x20000000);

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

  const char* env = getenv("TEST_VAR");
  if (!env || strcmp(env, "LOADED"))
    Panic(
        "Constructors not run when loading libfoo_with_static_constructor.so");

  // Find the "Foo" symbol.
  FunctionPtr foo_func;
  if (!crazy_library_find_symbol(
           library, "Foo", reinterpret_cast<void**>(&foo_func))) {
    Panic("Could not find 'Foo' in libfoo.so\n");
  }

  // Call it.
  (*foo_func)();

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

  env = getenv("TEST_VAR");
  if (!env || strcmp(env, "UNLOADED"))
    Panic(
        "Destructors not run when unloading libfoo_with_static_constructor.so");

  crazy_context_destroy(context);

  return 0;
}
int main() {
  crazy_context_t* context = crazy_context_create();
  crazy_library_t* library;

  // DEBUG
  crazy_context_set_load_address(context, 0x20000000);

  // Load libbar.so
  if (!crazy_library_open(&library, "libbar.so", context)) {
    Panic("Could not open library: %s\n", crazy_context_get_error(context));
  }

  // Find the "Bar" symbol.
  FunctionPtr bar_func;
  if (!crazy_library_find_symbol(
           library, "Bar", reinterpret_cast<void**>(&bar_func))) {
    Panic("Could not find 'Bar' in libbar.so\n");
  }

  // Call it.
  (*bar_func)();

  // Find the "Foo" symbol from libbar.so
  FunctionPtr foo_func;
  if (!crazy_library_find_symbol(
           library, "Foo", reinterpret_cast<void**>(&foo_func))) {
    Panic("Could not find 'Foo' from libbar.so\n");
  }

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

  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;
}