开发者

JNI: jstring make the app crash on android

开发者 https://www.devze.com 2023-04-13 07:34 出处:网络
I\'m getting started with Android-NDK. So here we are, with a problem. This 开发者_运维百科is the C file:

I'm getting started with Android-NDK. So here we are, with a problem. This 开发者_运维百科is the C file:

#include <string.h>
#include <jni.h>
#include <stdio.h>

#define STRING "test"

jstring Java_com_gnufabio_ndk_test_testNDK_buildString(JNIEnv *env, jobject sThis){
    jstring str_ret = (*env)->NewStringUTF(env, STRING);
    return str_ret;
}

And here the Java file:

package com.gnufabio.ndk.test;

import android.app.Activity;
import android.os.Bundle;
import android.text.style.BulletSpan;
import android.util.Log;

public class TestNDK extends Activity {
    /** Called when the activity is first created. */

    static {
        System.loadLibrary("test");
    }

    public native String buildString();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start();
    }
    public void start (){
        Log.d("NDK-TEST", "Started at: " + System.currentTimeMillis());
        for (int i=0; i < 9999; i++){
            buildString();
        }
        Log.d("NDK-TEST", "Finished at: " + System.currentTimeMillis());
    }
}

The problem is that the app crashes. Here the logcat:

I/DEBUG   (   86): pid: 1635, tid: 1637  >>> com.gnufabio.ndk.test <<<
I/DEBUG   (   86): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad
I/DEBUG   (   86):  r0 deadbaad  r1 00000001  r2 a0000000  r3 00000000
I/DEBUG   (   86):  r4 00000000  r5 00000027  r6 0000f140  r7 40875418
I/DEBUG   (   86):  r8 100ffe5c  r9 100ffe88  10 000b3f50  fp acaa149c
I/DEBUG   (   86):  ip ffffffff  sp 100ffde0  lr aff194ad  pc aff15c1c  cpsr 60000030
I/DEBUG   (   86):  d0  400000003eaaaaab  d1  0000005f3f800000
I/DEBUG   (   86):  d2  4e194b6842be630c  d3  42c8000000670ff0
I/DEBUG   (   86):  d4  000001fd00621bc8  d5  3fe999999999999a
I/DEBUG   (   86):  d6  3ff0000000000000  d7  3eaaaaab3f800000
I/DEBUG   (   86):  d8  0000000000000000  d9  0000000000000000
I/DEBUG   (   86):  d10 0000000000000000  d11 0000000000000000
I/DEBUG   (   86):  d12 0000000000000000  d13 0000000000000000
I/DEBUG   (   86):  d14 0000000000000000  d15 0000000000000000
I/DEBUG   (   86):  scr 80000012
I/DEBUG   (   86): 
I/DEBUG   (   86):          #00  pc 00015c1c  /system/lib/libc.so
I/DEBUG   (   86):          #01  pc 00013efe  /system/lib/libc.so (dlfree)
I/DEBUG   (   86):          #02  pc 0001445c  /system/lib/libc.so (free)
I/DEBUG   (   86):          #03  pc 0004cfc8  /system/lib/libdvm.so (dexDataMapFree)
I/DEBUG   (   86):          #04  pc 0004cff4  /system/lib/libdvm.so (dvmHeapGetNextObjectFromLargeTable)
I/DEBUG   (   86):          #05  pc 0001a070  /system/lib/libdvm.so (dvmGetNextHeapWorkerObject)
I/DEBUG   (   86):          #06  pc 0004d530  /system/lib/libdvm.so
I/DEBUG   (   86):          #07  pc 0004d682  /system/lib/libdvm.so
I/DEBUG   (   86):          #08  pc 0004be0e  /system/lib/libdvm.so
I/DEBUG   (   86):          #09  pc 00011980  /system/lib/libc.so (__thread_entry)
I/DEBUG   (   86):          #10  pc 000114d4  /system/lib/libc.so (pthread_create)
I/DEBUG   (   86): 
I/DEBUG   (   86): libc base address: aff00000
I/DEBUG   (   86): 
I/DEBUG   (   86): code around pc:
I/DEBUG   (   86): aff15bfc 4623b15c 2c006824 e026d1fb b12368db 
I/DEBUG   (   86): aff15c0c 21014a17 6011447a 48124798 24002527 
I/DEBUG   (   86): aff15c1c f7f57005 2106eb8a ec18f7f6 460aa901 
I/DEBUG   (   86): aff15c2c f04f2006 94015380 94029303 efe2f7f5 
I/DEBUG   (   86): aff15c3c 4622a905 f7f52002 f7f5efec 2106eb76 
I/DEBUG   (   86): 
I/DEBUG   (   86): code around lr:
I/DEBUG   (   86): aff1948c 41f0e92d 46804c0c 447c2600 68a56824 
I/DEBUG   (   86): aff1949c e0076867 300cf9b5 dd022b00 47c04628 
I/DEBUG   (   86): aff194ac 35544306 37fff117 6824d5f4 d1ee2c00 
I/DEBUG   (   86): aff194bc e8bd4630 bf0081f0 0002802e 41f0e92d 
I/DEBUG   (   86): aff194cc fb01b086 9004f602 461f4815 4615460c 
I/DEBUG   (   86): 
I/DEBUG   (   86): stack:
I/DEBUG   (   86):     100ffda0  00000000  
I/DEBUG   (   86):     100ffda4  00000000  
I/DEBUG   (   86):     100ffda8  00000000  
I/DEBUG   (   86):     100ffdac  00000000  
I/DEBUG   (   86):     100ffdb0  aff41700  
I/DEBUG   (   86):     100ffdb4  aff41690  
I/DEBUG   (   86):     100ffdb8  00000000  
I/DEBUG   (   86):     100ffdbc  aff194ad  /system/lib/libc.so
I/DEBUG   (   86):     100ffdc0  00000000  
I/DEBUG   (   86):     100ffdc4  100ffdf4  
I/DEBUG   (   86):     100ffdc8  0000f140  
I/DEBUG   (   86):     100ffdcc  40875418  
I/DEBUG   (   86):     100ffdd0  100ffe5c  
I/DEBUG   (   86):     100ffdd4  aff18619  /system/lib/libc.so
I/DEBUG   (   86):     100ffdd8  df002777  
I/DEBUG   (   86):     100ffddc  e3a070ad  
I/DEBUG   (   86): #00 100ffde0  aca9b64c  
I/DEBUG   (   86):     100ffde4  aca57f6d  /system/lib/libdvm.so
I/DEBUG   (   86):     100ffde8  00139958  
I/DEBUG   (   86):     100ffdec  4087ca40  
I/DEBUG   (   86):     100ffdf0  00139958  
I/DEBUG   (   86):     100ffdf4  fffffbdf  
I/DEBUG   (   86):     100ffdf8  100ffe5c  
I/DEBUG   (   86):     100ffdfc  00000004  
I/DEBUG   (   86):     100ffe00  00000018  
I/DEBUG   (   86):     100ffe04  aff13f03  /system/lib/libc.so
I/DEBUG   (   86): #01 100ffe08  00139958  
I/DEBUG   (   86):     100ffe0c  4087ca40  
I/DEBUG   (   86):     100ffe10  00000000  
I/DEBUG   (   86):     100ffe14  402441e8  
I/DEBUG   (   86):     100ffe18  100ffe5c  
I/DEBUG   (   86):     100ffe1c  aff1445f  /system/lib/libc.so
I/BootReceiver(  164): Copying /data/tombstones/tombstone_08 to DropBox (SYSTEM_TOMBSTONE)
D/Zygote  (   88): Process 1635 terminated by signal (11)
I/ActivityManager(  164): Process com.gnufabio.ndk.test (pid 1635) has died.

Any idea? It seems that it's a buffer overflow, but of what? Thanks in advance.


You must include the generated .h file in your .c file, otherwise your native method won't have the correct calling sequence modifiers.


This probably isn't causing the crash but testNDK should be TestNDK in the line Java_com_gnufabio_ndk_test_testNDK_buildString to match your class name.


What you are looking for is:

jstring str_ret = env->NewStringUTF(STRING);
0

精彩评论

暂无评论...
验证码 换一张
取 消