Unable to add JNI code to ZygoteInit

33 views
Skip to first unread message

Omkar Bolla

unread,
Jun 19, 2018, 4:03:47 PM6/19/18
to android-platform
Hi,

I want to add small C code as JNI in android, for learning purpose.
Here I am using Android Marshmallow and using 'aosp_arm-eng' in ubuntu 14.04. It got compiled properly and works properly. But when I add JNI code (Basic c code with print statement) I am getting below SIGSEGV error.

Below is procedure i tried to run JNI code:
-------------------------------------------------------------
Created a .so library which is a basic C JNI code:

Created this code at external/libctest/

file: Android.mk:
-----------------------<< Start of File >>------------------------------

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := libctest
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := \
ctest.c
LOCAL_SHARED_LIBRARIES := \
        libutils    \
        libcutils \
        liblog

LOCAL_LDFLAGS := -Wl,--hash-style=sysv
LOCAL_CFLAGS := -DSTDC_HEADERS

LOCAL_ARM_MODE := arm
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
include $(BUILD_SHARED_LIBRARY)
-----------------------<< End of File >>------------------------------

file: ctest,c
-----------------------<< Start of File >>-----------------------------

#include <android/log.h>
#include <jni.h>
#include <JNIHelp.h>

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>


#if 1
#include <utils/Log.h>
#define printI(fmt, ...) \
ALOGI("[ctest]: "fmt, ##__VA_ARGS__)
#else
#if 0
#define printI(fmt, ...) \
printf("[ctest]: "fmt, ##__VA_ARGS__)
#else
#define printI(fmt, ...) ;
#endif
#endif

JNIEXPORT void JNICALL Java_com_android_internal_os_ZygoteInit_ZygoteFromC (JNIEnv * env __unused, jobject obj __unused)
{
        printI("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n");
        printI("MAX_PATH: %d\n", MAX_PATH);
        printI("%s():%d: Hello from C!\n", __func__, getpid());
        printI("%s():%d: Hello from C!\n", __func__, getpid());
}
-----------------------<< End of File >>------------------------------


file: frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
-----------------------<< Start of File >>-----------------------------
Before 'public static void main(String argv[]) {' line


public native void ZygoteFromC(); /* (1) */
static {
        Log.i(TAG, "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL");
try {
        System.loadLibrary("ctest"); /* (2) */
} catch (UnsatisfiedLinkError e) {
Log.i(TAG, "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
e.printStackTrace();
}
        Log.i(TAG, "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL");
}

In main() function after preload() execution

            preload();

        Log.i(TAG, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
//      ZygoteInit zygote_init = new ZygoteInit();
//      zygote_init.ZygoteFromC();
        new ZygoteInit().ZygoteFromC();
//      ZygoteFromC();
        Log.i(TAG, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
            EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
                SystemClock.uptimeMillis());


-----------------------<< End of File >>------------------------------

After adding this code it is being compiled properly, but i am having below error while running AOSP using emulator:

Error:
-------

06-18 10:28:07.541  1473  1473 W Resources: Preloaded drawable resource #0x10806f0 (android:drawable/text_select_handle_middle_material) that varies with configuration!!
06-18 10:28:07.545  1473  1473 W Resources: Preloaded drawable resource #0x10806f3 (android:drawable/text_select_handle_right_material) that varies with configuration!!
06-18 10:28:07.570  1473  1473 I Zygote  : ...preloaded 342 resources in 5004ms.
06-18 10:28:07.700  1473  1473 I Zygote  : ...preloaded 41 resources in 129ms.
06-18 10:28:07.701  1473  1473 D libEGL  : Emulator without GPU support detected. Fallback to software renderer.
06-18 10:28:07.737  1473  1473 D libEGL  : loaded /system/lib/egl/libGLES_android.so
06-18 10:28:08.216  1473  1473 I Zygote  : Preloading shared libraries...
06-18 10:28:08.301  1473  1473 D Zygote  : end preload
06-18 10:28:08.301  1473  1473 I Zygote  : AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
--------- beginning of crash
06-18 10:28:08.307  1473  1473 F libc    : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x200c in tid 1473 (main)
06-18 10:28:08.349    65    65 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-18 10:28:08.360    65    65 F DEBUG   : Build fingerprint: 'Android/aosp_arm/generic:6.0.1/MOI10E/omkar.bolla04121156:eng/test-keys'
06-18 10:28:08.360    65    65 F DEBUG   : Revision: '0'
06-18 10:28:08.360    65    65 F DEBUG   : ABI: 'arm'
06-18 10:28:08.361    65    65 F DEBUG   : pid: 1473, tid: 1473, name: main  >>> zygote <<<
06-18 10:28:08.361    65    65 F DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x200c
06-18 10:28:08.447    65    65 F DEBUG   :     r0 b4dd6a80  r1 bea3d56c  r2 00430000  r3 00000001
06-18 10:28:08.447    65    65 F DEBUG   :     r4 000072a6  r5 710ea020  r6 12e6fb30  r7 00000003
06-18 10:28:08.447    65    65 F DEBUG   :     r8 7137d410  r9 b4df6500  sl 12c36200  fp 713726d8
06-18 10:28:08.447    65    65 F DEBUG   :     ip b4dbc000  sp bea3d560  lr 7383b381  pc 0000200c  cpsr 60000010
06-18 10:28:08.463    65    65 F DEBUG   :
06-18 10:28:08.463    65    65 F DEBUG   : backtrace:
06-18 10:28:08.464    65    65 F DEBUG   :     #00 pc 0000200c  <unknown>
06-18 10:28:08.464    65    65 F DEBUG   :     #01 pc 7383b37f  /data/dalvik-cache/arm/system@framework@boot.oat (offset 0x1ed6000)
06-18 10:28:11.544    65    65 F DEBUG   :
06-18 10:28:11.544    65    65 F DEBUG   : Tombstone written to: /data/tombstones/tombstone_02
06-18 10:28:11.628    57    57 I ServiceManager: service 'media.audio_flinger' died
06-18 10:28:11.641    57    57 I ServiceManager: service 'media.player' died
06-18 10:28:11.641    57    57 I ServiceManager: service 'media.resource_manager' died
06-18 10:28:11.641    57    57 I ServiceManager: service 'media.camera' died
06-18 10:28:11.641    57    57 I ServiceManager: service 'media.audio_policy' died
06-18 10:28:11.641    57    57 I ServiceManager: service 'media.sound_trigger_hw' died
06-18 10:28:11.641    57    57 I ServiceManager: service 'media.radio' died
06-18 10:28:12.072  1625  1625 I Netd    : Netd 1.0 starting
06-18 10:28:12.074  1625  1625 D TetherController: Setting IP forward enable = 0
06-18 10:28:14.601  1626  1626 V MediaUtils: physMem: 510377984
06-18 10:28:14.601  1626  1626 V MediaUtils: requested limit: 331745635
06-18 10:28:14.602  1626  1626 V MediaUtils: actual limit: 331745635
06-18 10:28:14.602  1626  1626 V MediaUtils: original limits: 4294967295/4294967295
06-18 10:28:14.602  1626  1626 V MediaUtils: new limits: 331745635/4294967295
06-18 10:28:14.634  1626  1626 I mediaserver: ServiceManager: 0xb608e120
06-18 10:28:14.635  1626  1626 I AudioFlinger: Using default 3000 mSec as standby time.

--------------------------------------------------------------------------


Please help me to start my C code using JNI in emulator.


Thanks,
Omkar B
Reply all
Reply to author
Forward
0 new messages