Dynamically Generating Dalvik Bytecode into a running Dalvik/Android application - android

This question has been asked(and answered) many times about dynamically generating and loading java bytecodes at runtime into a running Dalvik VM, but is there any way to load dex files/bytecodes into an app at runtime?
Thanks

The Dalvik team would like to build a first-class runtime code generation library. We're tracking the feature request as Android bug 6322. Unfortunately, we have a very long list of performance and correctness issues, so I can't give you a timeline for when we'll spend time on this issue.
There are some alternatives, but they will all take some work:
Run your application on a standard JVM and exercise all runtime code generation there. Dump the .class files from memory to files, and then run dx on those files. If you're quite sophisticated, you could integrate all of this work into your build.
Include the open source dx tool as a project library, and execute it programatically from within your application, possibly in your application's classloader. This will bloat your application's binary.

is there any way to load dex
files/bytecodes into an app at
runtime?
Look at DexFile and DexClassLoader.

A related answer suggests Dexmaker for dynamic Dalvik bytecode generation.

I have used ASM and BCEL to generate Java classes and then I have converted them to Dex files.
Finally I have created jar files to load dynamically on device.
You can check out my code :)
https://github.com/sciruela/android

If inside any C or C++ program, you want to load and call into the DEX classes, you can see how the Dalvik VM is started, inside the AndroidRuntime - for example frameworks/base/cmds/app_process/app_main.cpp:
status_t app_init(const char* className, int argc, const char* const argv[])
{
LOGV("Entered app_init()!\n");
AndroidRuntime* jr = AndroidRuntime::getRuntime();
jr->callMain(className, argc, argv);
LOGV("Exiting app_init()!\n");
return NO_ERROR;
}
As "jr" AndroidRuntime is already started, callMain() will be called:
status_t AndroidRuntime::callMain(
const char* className, int argc, const char* const argv[])
{
JNIEnv* env;
jclass clazz;
jmethodID methodId;
LOGD("Calling main entry %s", className);
env = getJNIEnv();
if (env == NULL)
return UNKNOWN_ERROR;
clazz = findClass(env, className);
if (clazz == NULL) {
LOGE("ERROR: could not find class '%s'\n", className);
return UNKNOWN_ERROR;
}
methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
if (methodId == NULL) {
LOGE("ERROR: could not find method %s.main(String[])\n", className);
return UNKNOWN_ERROR;
}
<...>
env->CallStaticVoidMethod(clazz, methodId, strArray);
return NO_ERROR;
}
From above, we can see how the DEX classes' codes are loaded and CallStaticVoidMethod() will start interpreting the DEX codes.

Related

A simple way to build with the Android NDK

I started out writing a simple C++ program that tried to get the handle of a service using
defaultServiceManager()->getService(String16('ServiceName'));
Which has now snowballed into this terrible dependency recursion. Ultimately what I need is:
libbinder for defaultServiceManager and getService
libutils for String16
Neither of these are available in the NDK. Their sources are in the SDK along with all the appropriate headers. I can get everything to behave nicely until link time:
undefined reference to `android::defaultServiceManager()'
undefined reference to `android::String16::String16(char const*)'
undefined reference to `android::String16::~String16()'
undefined reference to `android::String16::~String16()'
Is it required to build this as a part of AOSP? Perhaps through something like ndk-build? What I am building is more of a toy application that only needs access to those (and related, e.g. service->transact APIs) APIs along with Parcel.
Perhaps building AOSP once and using -L to include a search path to the generated libs.
libbinder and libutils are not part of the app API surface. https://developer.android.com/ndk/reference/group/ndk-binder is the NDK binder API.
This (dirty hack) is working fine for me:
#include <android/binder_ibinder.h>
typedef AIBinder* (*AServiceManager_getService_func)(const char* instance);
extern "C"
JNIEXPORT void JNICALL
Java_com_irsl_greedybinder_MainActivity_testService(JNIEnv *env, jclass clazz) {
void* binder_ndk = dlopen("/system/lib/libbinder_ndk.so", RTLD_LAZY);
if (binder_ndk == nullptr) {
ALOGI("Unable to load libbinder_ndk.so");
return;
}
AServiceManager_getService_func AServiceManager_getService;
AServiceManager_getService = (AServiceManager_getService_func) dlsym(binder_ndk, "AServiceManager_getService");
if(AServiceManager_getService == nullptr) {
ALOGI("Failed to look up AServiceManager_getService");
return;
}
ALOGI("AServiceManager_getService symbol found at: %p", AServiceManager_getService);
AIBinder* binder = AServiceManager_getService("activity");
if (binder == nullptr) {
ALOGI("Unable to obtain Activity Manager service");
return;
}
ALOGI("We got the binder to the Activity Manager!: %p", binder);
}
Disclaimer by Dan Albert applies: They exist for vendor and APEX users. Those domains do not carry the same guarantees that are needed for apps to use them reliably, so they are not exposed to apps.

Providing a JVM for MediaStreamer2 Console Application on Android

I am working on an Android firmware for an embedded device which streams an encoded video signal using rtp. The underlying library is MediaStreamer2 because it comes with Android support, various codecs and libortp. Therefore I integrated libmediastreamer and its dependencies into my firmware build process.
As a second step, I wrote a simple Android command line application as a PoC which streams audio or video through the network. Unfortunatly, the first call to ms_init() fails due to:
bctbx-fatal-Calling ms_get_jni_env() while no jvm has been set using ms_set_jvm()
Digging a little deeper into the problem, it seems Androids version of libmediastreamer was designed from an NDK point of view: It can be called as a part of an Android app and therefore automatically gets a reference to the JVM (DVM?). Unfortunatly, this is not my use case.
I tried to to remove the dependencies (Querying Sdk version, hardware echo cancelation support, etc.) without success. So my next approach would be starting a VM manually and passing it to the library. I tried Oracles APIs like:
JNIEnv env;
JavaVM vm;
JavaVMInitArgs vm_args;
JavaVMOption options[4];
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = TRUE;
jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);
But the application quits with a simple "aborted". Nevertheless, I am not sure whether this is a way to go because its Android and Dalvik world.
Any suggestions?
It is possible to build executable for shell on Android on both rooted and non-rooted devices, see reference How to build an executable for Android shell
.
Try below code and build it using NDK to get an executable:
#include <jni.h>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char **argv) {
JavaVMOption jvmopt[1];
jvmopt[0].optionString = "-Djava.class.path=" + ".";
JavaVMInitArgs vmArgs;
vmArgs.version = JNI_VERSION_1_2;
vmArgs.nOptions = 1;
vmArgs.options = jvmopt;
vmArgs.ignoreUnrecognized = JNI_TRUE;
// Create the JVM
JavaVM *javaVM;
JNIEnv *jniEnv;
long flag = JNI_CreateJavaVM(&javaVM, (void**)
&jniEnv, &vmArgs);
if (flag == JNI_ERR) {
cout << "Error creating VM. Exiting...\n";
return 1;
}
/** ----------------------------------------------
* Put your own JNI related code from here if any.
* -----------------------------------------------
**/
javaVM->DestroyJavaVM();
return 0;
}
Do a check on <jni.h> about the interfaces you can use, e.g.
/*
* VM initialization functions.
*
* Note these are the only symbols exported for JNI by the VM.
*/
jint JNI_GetDefaultJavaVMInitArgs(void*);
jint JNI_CreateJavaVM(JavaVM**, JNIEnv**, void*);
jint JNI_GetCreatedJavaVMs(JavaVM**, jsize, jsize*);
You can refer to below to see if they are helpful:
how-to-create-a-jvm-instance-in-jni
https://calebfenton.github.io/2017/04/05/creating_java_vm_from_android_native_code/

CMAKE_BUILD_TYPE Release: strange results

We are creating an Android app to compare execution time between ART and native code. We are using Android Studio and CMake for compiling C/C++.
When in CMakeList.txt we set the flag
set(CMAKE_BUILD_TYPE Release)
in some algorithms (Primality Test and Fibonacci) the execution time drastically drop to 0ms for all different input.
Here the native lib
bool flag = false;
extern "C" JNIEXPORT void JNICALL Java_javacpp_cmr_com_sdkvsndk_MainActivity_cancel(JNIEnv *env, jobject obj) {
flag = true;
}
extern "C" JNIEXPORT jlong JNICALL Java_javacpp_cmr_com_sdkvsndk_MainActivity_primalityTest(JNIEnv *env, jobject obj, jlong r) {
if(r < 0) return -1L;
timeval start, stop;
long long t;
gettimeofday(&start, NULL);
bool prime = true;
unsigned long long sr = (unsigned long long) sqrt(r);
for (unsigned long long i = 2; (i < sr) && prime; i++) {
if (flag) return -1;
if (r % i == 0) prime = false;
}
gettimeofday(&stop, NULL);
t = (stop.tv_sec - start.tv_sec) * 1000;
t += (long long) ((stop.tv_usec - start.tv_usec) / 1000)
return (jlong) t;
}
flag is a flag that is set true when we terminate the asyncTask that execute the algorithm.
I can not figure how this is possible. Any suggestion? Thank you.
This is because by default your CMake project is build with Debug type. In this type the debugging information is generated as well as optimisations are disabled (-O0 -g flags to gcc).
This is to enable you to step trough your C++ code line by line as it was written by you. If you change the type to Release the optimisations are turned on and the debug info is not included with the binary.
The optimisations make the code run so fast, no matter how well you think you wrote something, the compiler is still ahead of you and will make it better. Those optimisations however will show erratic behaviour when the code is debugged, lines executed out of order, or not at all, variables not showing in watches or shown wrong, this is not nice for debugging.
The missing debug info means the binary is lighter but if you need to debug it better practice some assembly since any information on what line of C++ resulted in these assembly instructions is lost. As a side note there is also RelWithDebugInfo build type in case you really need to debug the optimised code.
Normally the Android Studio should take care of the appropriate build type for you so there is no need to fiddle with that.
You can dump the compilation commands used to build the C/C++ source files using:
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) in the CMakeLists.txt which will create a compile_commands.json in the build directory.
You can try different combination of the "CMAKE_BUILD_TYPE" and save the generated compile_commands.json for different build types for different compiler flags for optimizations, debugging etc.

PJSip Account Registration

I am trying to add PJSip to a project I am working on. I have this method for registering my account but a 'Fatal signal 11' error occurs everytime.
Here is the method
public int setRegistration() {
int status = pjsuaConstants.PJ_FALSE;
/* Register to SIP server by creating SIP account. */
int[] accId = new int[1];
accId[0] = 1;
String uName = getUserName();
String passwd = getPassword();
String server = getSIPServer();
pjsua_acc_config acc_cfg = new pjsua_acc_config();
pjsua.acc_config_default(acc_cfg);
acc_cfg.setId(pjsua.pj_str_copy("sip:" + uName + "#" + server));
acc_cfg.setReg_uri(pjsua.pj_str_copy("sip:" + server));
acc_cfg.setCred_count(1);
acc_cfg.getCred_info().setRealm(pjsua.pj_str_copy(server));
acc_cfg.getCred_info().setScheme(pjsua.pj_str_copy("digest"));
acc_cfg.getCred_info().setUsername(pjsua.pj_str_copy(uName));
acc_cfg.getCred_info().setData_type(pjsip_cred_data_type.PJSIP_CRED_DATA_PLAIN_PASSWD.swigValue());
acc_cfg.getCred_info().setData(pjsua.pj_str_copy(passwd));
Log.d("status", "acc is adding..");
status = pjsua.acc_add(acc_cfg, pjsuaConstants.PJ_TRUE, accId);
Log.d("status", "acc is added");
if (status == pjsuaConstants.PJ_SUCCESS) {
status = pjsua.acc_set_online_status(accId[0], 1);
Log.d("acc_set_online_status returned stauts=", String.valueOf(status));
} else {
Log.d("Error status=", String.valueOf(status));
}
return status;
}
I receive the error on the status = pjsua.acc_add(acc_cfg, pjsuaConstants.PJ_TRUE, accId); line. I know that the username, server, and password are not null. I have looked at multiple questions relating to this and no use.
How can I register my account?
Thanks
*****EDIT******
After tracking down this through blogs and forums I got passed this error but received another. The reason this error occurred was because pjsua_init was never successful. It was successful because it gave me this error
11-04 10:19:20.973: E/AndroidRuntime(2961): FATAL EXCEPTION: main
11-04 10:19:20.973: E/AndroidRuntime(2961): java.lang.UnsatisfiedLinkError: Native method not found: org.pjsip.pjsua.pjsuaJNI.init:(JLorg/pjsip/pjsua/pjsua_config;JLorg/pjsip/pjsua/pjsua_logging_config;JLorg/pjsip/pjsua/pjsua_media_config;)I
11-04 10:19:20.973: E/AndroidRuntime(2961): at org.pjsip.pjsua.pjsuaJNI.init(Native Method)
11-04 10:19:20.973: E/AndroidRuntime(2961): at org.pjsip.pjsua.pjsua.init(pjsua.java:812)
I have received this warning as well
No implementation found for native Lorg/pjsip/pjsua/pjsuaJNI;.init (JLorg/pjsip/pjsua/pjsua_config;JLorg/pjsip/pjsua/pjsua_logging_config;JLorg/pjsip/pjsua/pjsua_media_config;)I
Why isn't this a native method? I am looking into the libraries I have called but other than that I don't know why this isn't working.
Any help on this matter would be great.
Thanks
PJ Code
pjsua.java
public synchronized static int init(pjsua_config ua_cfg, pjsua_logging_config log_cfg, pjsua_media_config media_cfg) {
return pjsuaJNI.init(pjsua_config.getCPtr(ua_cfg), ua_cfg, pjsua_logging_config.getCPtr(log_cfg), log_cfg, pjsua_media_config.getCPtr(media_cfg), media_cfg);
}
pjsuaJNI.java
public final static native int init(long jarg1, pjsua_config jarg1_, long jarg2, pjsua_logging_config jarg2_, long jarg3, pjsua_media_config jarg3_);
pjsua_wrap.cpp
SWIGEXPORT jint JNICALL Java_org_pjsip_pjsua_pjsuaJNI_init(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jlong jarg2, jobject jarg2_, jlong jarg3, jobject jarg3_) {
jint jresult = 0 ;
pjsua_config *arg1 = (pjsua_config *) 0 ;
pjsua_logging_config *arg2 = (pjsua_logging_config *) 0 ;
pjsua_media_config *arg3 = (pjsua_media_config *) 0 ;
pj_status_t result;
(void)jenv;
(void)jcls;
(void)jarg1_;
(void)jarg2_;
(void)jarg3_;
arg1 = *(pjsua_config **)&jarg1;
arg2 = *(pjsua_logging_config **)&jarg2;
arg3 = *(pjsua_media_config **)&jarg3;
result = (pj_status_t)pjsua_init((pjsua_config const *)arg1,(pjsua_logging_config const *)arg2,(pjsua_media_config const *)arg3);
jresult = (jint)result;
return jresult;
}
{"init", "(JLorg/pjsip/pjsua/pjsua_config;JLorg/pjsip/pjsua/pjsua_logging_config;JLorg/pjsip/pjsua/pjsua_media_config;)I", (void*)& Java_org_pjsip_pjsua_pjsuaJNI_init},
EDIT 2
So after working on this I have gotten to a point of frustration. I am not seeing what I am doing wrong so I will put my entire process here to see if someone has a suggestion.
I start by getting the pjsip library: svn co http://svn.pjsip.org/repos/pjproject/trunk pjproject
run `./configure --prefix=/usr/local
make dep & make
sudo make install
I then get the pjjni code svn checkout svn://svn.code.sf.net/p/pjsip-jni/code/ pjsip-jni-code
I follow the Makefile instructions
After Makefile runs successfully (after some code cleanup) I have 2 .so files (libpjsua_jni.so and libpjsua_jni_x64.so)
Create jni folder with Android.mk file and .so libraries
Run ndk-build (How to load another .so file in your android project?)
Add to ADT
Close project. Change native support from Java to Android. Open project
(Convert existing project into Android project in Eclipse?)
Add that project to my TestPJ project (Android -> Library -> Add)
Call System.loadLibrary("pjsualib") -- Name of the new lib.so
Receive Error
11-22 13:55:44.784: W/dalvikvm(11464): No implementation found for native Lorg/pjsip/pjsua/pjsuaJNI;.swig_module_init:()V
11-22 13:55:48.792: W/dalvikvm(11464): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lorg/pjsip/pjsua/pjsuaJNI;
11-22 13:55:51.417: E/AndroidRuntime(11464): java.lang.UnsatisfiedLinkError: Native method not found: org.pjsip.pjsua.pjsuaJNI.swig_module_init:()V
11-22 13:55:51.417: E/AndroidRuntime(11464): at org.pjsip.pjsua.pjsuaJNI.swig_module_init(Native Method)
11-22 13:55:51.417: E/AndroidRuntime(11464): at org.pjsip.pjsua.pjsuaJNI.(pjsuaJNI.java:1450)
Any help would be great. Thanks!
An example of project which explores JNI calls from Java and from C can be found here.
The error mentioned in the question (java.lang.UnsatisfiedLinkError: Native method not found: org.pjsip.pjsua.pjsuaJNI) means one of the following problems:
- wrong native method name or/and its arguments/return value. If you have access to native code of the library than you can fix it. According to the error message and JNI considerations native method must have name Java_org_pjsip_pjsua_pjsuaJNI_init(JNIEnv *env, jobject obj, ..), where env is a pointer to JVM interface, obj is a "this" pointer and the remaining arguments can be determined from java init method of pjsuaJNI class of package org.pjsip.pjsua. Simple parameter types must be jint, jstring etc. Also return value must be correct as well. Fixing all these allows to use this method from pjsuaJNI class. Additional details can be found from Oracle Documentation to Oracle Documentation to Java 6 JNI (or Java 7 if you're using android 4.4).
- wrong java method name/signature/class name or package name. This case is almost reverse to the first one. Again, according to the mentioned error name of the method must be "init", class name pjsuaJNI and package org.pjsip.pjsua. If at least one of them is wrong the mentioned exception will happen. Signature or parameters must also be correct. In the boundaries of this error it can be considered as a parameters of the method (in addition in native JNIEnv* and jobject appears). So also must be checked and fixed if necessary.
In case of call from native code to java signature can be considered as a representation of java method with parameters as a string and can be viewed with javap -s *.class command applied to java *.class file. And in the last warning from question this signature of the method can be seen.
Also to use method pjsip library must be loaded with System.loadLibrary() in some static section of the Java class.
Unfortunately, this problem happens at runtime (it would be nice if it happened during compilation time).
It's a bit late but I'll try to help on this. I think that your problem should be related to your native method not been surrounded by extern "C"{} and name mangling in C++.
If you don't declare one native C function as extern "C", C++ build mangles it and JNI mechanism can not find native method matching provided signature. On the other hand, declaring it as external C function, the builder creates both, mangled and unmanged, versions and JNI can find the proper one.
Hope this helps.

rand() function Bionic vs glibc

I found that rand() function from bionic does't work without including stdlib.h
extern int rand(void);
static void foo()
{
int a = rand();
}
int main()
{
foo()
return 0;
}
Results for glibc:
Compilation successful
Results for bionic:
Compilation unsuccessful
error: undefined reference to 'rand'
In bionic sources we have following implementation:
static __inline__ int rand(void) {
return (int)lrand48();
}
Why it works for glibc but not for bionic
glibc's rand() is a "normal" function located in a library. I suspect you're getting a warning about the symbol not being defined from the compiler (which cannot find it), but since the function exists in a library, the link stage can still find and link to it.
bionic's rand() is a static inline in your header file, that means it's not even defined unless the header file is included. Since the compiler cannot find it without the include and it does not exist in any library that the linker can resolve it from, compilation will fail at the linking stage.
You will get the answer if you just compare bionic and glibc sources of rand function: https://github.com/android/platform_bionic/blob/master/libc/include/stdlib.h and
http://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/rand.c;hb=glibc-2.15#l26
You can see that in bionic library it inlined in header file so without .h file you can't use it.And in glibc it is separated like most of the functions. The reason is obvious I think - bionic library was developed specially for use in embedded and mobile devices with memory and speed limits, so less code and inline optimizations is useful.

Categories

Resources