1

The protobuf version is 3.0.0. In some device (say Android 6), this issue happens always; While in some devices (say Android 5.x), this issue happens when the app is put into the background mode.

I am calling following method:

bool ParseFromString(const string& data);

Which probably internally calls bool ParseFromArray(const void* data, int size). Stacktrace:

Build fingerprint: 'lge/b2lds_global_com/b2lds:5.0.2/LRX22G/152532131cf4c:user/release-keys'
 Revision: '1.0'
 ABI: 'arm'
 pid: 9946, tid: 9970, name: ahlaad.saarathy  >>> com.org.aahlaad.saarathy <<< 
 signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xdeadbaad
 Abort message: 'invalid address or address of corrupt block 0xa19fca5c passed to dlfree'
     r0 ffffffff  r1 deadbaad  r2 73797300  r3 2e6d6f63
     r4 b6f2bfd8  r5 a19fca5c  r6 a19469e4  r7 b6f310fc
     r8 a19fca64  r9 a1c417b0  sl 00000000  fp a1946a64
     ip 00000000  sp a19469a8  lr b6efe031  pc b6efe034  cpsr 800f0030
 backtrace:
     #00 pc 00027034  /system/lib/libc.so (__bionic_heap_usage_error+35)
     #01 pc 00029853  /system/lib/libc.so (dlfree+1354)
     #02 pc 0000f0e7  /system/lib/libc.so (free+10)
     #03 pc 001bc24d  /data/app/com.org.aahlaad.saarathy-1/lib/arm/libprotobuf.so
     #04 pc 001bc4d7  /data/app/com.org.aahlaad.saarathy-1/lib/arm/libprotobuf.so
===> #05 pc 000dde37  /data/app/com.org.aahlaad.saarathy-1/lib/arm/libprotobuf.so (google::protobuf::internal::WireFormatLite::ReadBytes(google::protobuf::io::CodedInputStream*, std::string*)+50)  
     #06 pc 00060944  /data/app/com.org.aahlaad.saarathy-1/lib/arm/libsaarathyclient.so (Saarathy::Messaging::Convey::MergePartialFromCodedStream(google::protobuf::io::CodedInputStream*)+480)
     #07 pc 000d7bdd  /data/app/com.org.aahlaad.saarathy-1/lib/arm/libprotobuf.so (google::protobuf::MessageLite::ParseFromString(std::string const&)+48)
     #08 pc 00022d84  /data/app/com.org.aahlaad.saarathy-1/lib/arm/libsaarathyclient.so (Connection::CloudSSL::DecodeProtoBuffer(unsigned int, std::string const&)+580)
     #09 pc 00024f0c  /data/app/com.org.aahlaad.saarathy-1/lib/arm/libsaarathyclient.so (Connection::CloudSSL::Receive()+308)
     #10 pc 0002af68  /data/app/com.org.aahlaad.saarathy-1/lib/arm/libsaarathyclient.so (Util::Synchronized_<Connection::CloudSSL>::Start()::{lambda()#1}::operator()() const+72)
     #11 pc 00091207  /data/app/com.org.aahlaad.saarathy-1/lib/arm/libgnustl_shared.so (execute_native_thread_routine+14)
     #12 pc 00012f57  /system/lib/libc.so (__pthread_start(void*)+30)
     #13 pc 00011017  /system/lib/libc.so (__start_thread+6)

Apparently as shown with arrow, it crashes at google::protobuf::internal::WireFormatLite::ReadBytes(...). This problem has been discussed in this thread, where the user fixed it by upgrading VS2005 to VS2008. However we are running this in Android.

I have also referred below posts to no avail:

Any detailed help is appreciated as I am novice in building protobuf library with Android.

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336

1 Answers1

1

I resolved above problem by following next steps

Step 1: Building both JNI source code and Google Protobuf C++ source code using same NDK runtime stlport_shared or gnustl_shared,we can define it in Application.mk file as

APP_STL := gnustl_shared

I was using gnustl-static to build my JNI source code which was causing issue. Issue may come if you are using different runtime while building Google Protobuf source code. You may get more information about android NDK Runtime at Android CPP support guide

Step 2: Be sure that your JNI code and Shared library (libprotobuf.so) referring to same version of the Google Protobuf library. It may cause undefined behavior in different android versions.

You can download latest source code from Google Protobuf Git Repo

You can follow the building steps at Android Platform external protobuf link or for simplicity can use bellow "Andoid.mk" file

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
c_includes := $(LOCAL_PATH)
LOCAL_MODULE := libprotobuf       
LOCAL_SRC_FILES := \
        google/protobuf/arena.cc \
        google/protobuf/arenastring.cc \
        google/protobuf/extension_set.cc \    
        google/protobuf/generated_message_util.cc \
        google/protobuf/io/coded_stream.cc \
        google/protobuf/io/zero_copy_stream.cc \
        google/protobuf/io/zero_copy_stream_impl_lite.cc \
        google/protobuf/message_lite.cc \
        google/protobuf/repeated_field.cc \
        google/protobuf/stubs/atomicops_internals_x86_gcc.cc \
        google/protobuf/stubs/atomicops_internals_x86_msvc.cc \
        google/protobuf/stubs/bytestream.cc \
        google/protobuf/stubs/common.cc \
        google/protobuf/stubs/int128.cc \
        google/protobuf/stubs/once.cc \
        google/protobuf/stubs/status.cc \
        google/protobuf/stubs/statusor.cc \
        google/protobuf/stubs/stringpiece.cc \
        google/protobuf/stubs/stringprintf.cc \
        google/protobuf/stubs/structurally_valid.cc \
        google/protobuf/stubs/strutil.cc \
        google/protobuf/stubs/time.cc \
        google/protobuf/wire_format_lite.cc \
        google/protobuf/any.cc \
        google/protobuf/any.pb.cc \
        google/protobuf/api.pb.cc \
        google/protobuf/compiler/importer.cc \
        google/protobuf/compiler/parser.cc \
        google/protobuf/descriptor.cc \
        google/protobuf/descriptor.pb.cc \
        google/protobuf/descriptor_database.cc \
        google/protobuf/duration.pb.cc \
        google/protobuf/dynamic_message.cc \
        google/protobuf/empty.pb.cc \
        google/protobuf/extension_set_heavy.cc \
        google/protobuf/field_mask.pb.cc \
        google/protobuf/generated_message_reflection.cc \
        google/protobuf/io/gzip_stream.cc \
        google/protobuf/io/printer.cc \
        google/protobuf/io/strtod.cc \
        google/protobuf/io/tokenizer.cc \
        google/protobuf/io/zero_copy_stream_impl.cc \
        google/protobuf/map_field.cc \
        google/protobuf/message.cc \
        google/protobuf/reflection_ops.cc \
        google/protobuf/service.cc \
        google/protobuf/source_context.pb.cc \
        google/protobuf/struct.pb.cc \
        google/protobuf/stubs/mathlimits.cc \
        google/protobuf/stubs/substitute.cc \
        google/protobuf/text_format.cc \
        google/protobuf/timestamp.pb.cc \
        google/protobuf/type.pb.cc \
        google/protobuf/unknown_field_set.cc \
        google/protobuf/util/field_comparator.cc \
        google/protobuf/util/field_mask_util.cc \
        google/protobuf/util/internal/datapiece.cc \
        google/protobuf/util/internal/default_value_objectwriter.cc \
        google/protobuf/util/internal/error_listener.cc \
        google/protobuf/util/internal/field_mask_utility.cc \
        google/protobuf/util/internal/json_escaping.cc \
        google/protobuf/util/internal/json_objectwriter.cc \
        google/protobuf/util/internal/json_stream_parser.cc \
        google/protobuf/util/internal/object_writer.cc \
        google/protobuf/util/internal/proto_writer.cc \
        google/protobuf/util/internal/protostream_objectsource.cc \
        google/protobuf/util/internal/protostream_objectwriter.cc \
        google/protobuf/util/internal/type_info.cc \
        google/protobuf/util/internal/type_info_test_helper.cc \
        google/protobuf/util/internal/utility.cc \
        google/protobuf/util/json_util.cc \
        google/protobuf/util/message_differencer.cc \
        google/protobuf/util/time_util.cc \
        google/protobuf/util/type_resolver_util.cc \
        google/protobuf/wire_format.cc \
        google/protobuf/wrappers.pb.cc \
        google/protobuf/compiler/code_generator.cc      \
        google/protobuf/compiler/command_line_interface.cc      \
        google/protobuf/compiler/cpp/cpp_enum.cc      \
        google/protobuf/compiler/cpp/cpp_enum_field.cc      \
        google/protobuf/compiler/cpp/cpp_extension.cc      \
        google/protobuf/compiler/cpp/cpp_field.cc      \
        google/protobuf/compiler/cpp/cpp_file.cc      \
        google/protobuf/compiler/cpp/cpp_generator.cc      \
        google/protobuf/compiler/cpp/cpp_helpers.cc      \
        google/protobuf/compiler/cpp/cpp_map_field.cc      \
        google/protobuf/compiler/cpp/cpp_message.cc      \
        google/protobuf/compiler/cpp/cpp_message_field.cc      \
        google/protobuf/compiler/cpp/cpp_primitive_field.cc      \
        google/protobuf/compiler/cpp/cpp_service.cc      \
        google/protobuf/compiler/cpp/cpp_string_field.cc      \
        google/protobuf/compiler/csharp/csharp_doc_comment.cc      \
        google/protobuf/compiler/csharp/csharp_enum.cc      \
        google/protobuf/compiler/csharp/csharp_enum_field.cc      \
        google/protobuf/compiler/csharp/csharp_field_base.cc      \
        google/protobuf/compiler/csharp/csharp_generator.cc      \
        google/protobuf/compiler/csharp/csharp_helpers.cc      \
        google/protobuf/compiler/csharp/csharp_map_field.cc      \
        google/protobuf/compiler/csharp/csharp_message.cc      \
        google/protobuf/compiler/csharp/csharp_message_field.cc      \
        google/protobuf/compiler/csharp/csharp_primitive_field.cc      \
        google/protobuf/compiler/csharp/csharp_reflection_class.cc      \
        google/protobuf/compiler/csharp    /csharp_repeated_enum_field.cc      \
        google/protobuf/compiler/csharp/csharp_repeated_message_field.cc      \
        google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc      \
        google/protobuf/compiler/csharp/csharp_source_generator_base.cc      \
        google/protobuf/compiler/csharp/csharp_wrapper_field.cc      \
        google/protobuf/compiler/java/java_context.cc      \
        google/protobuf/compiler/java/java_doc_comment.cc      \
        google/protobuf/compiler/java/java_enum.cc      \
        google/protobuf/compiler/java/java_enum_field.cc      \
        google/protobuf/compiler/java/java_enum_field_lite.cc      \
        google/protobuf/compiler/java/java_enum_lite.cc      \
        google/protobuf/compiler/java/java_extension.cc      \
        google/protobuf/compiler/java/java_field.cc      \
        google/protobuf/compiler/java/java_file.cc      \
        google/protobuf/compiler/java/java_generator.cc      \
        google/protobuf/compiler/java/java_generator_factory.cc      \
        google/protobuf/compiler/java/java_helpers.cc      \
        google/protobuf/compiler/java/java_lazy_message_field.cc      \
        google/protobuf/compiler/java/java_lazy_message_field_lite.cc      \
        google/protobuf/compiler/java/java_map_field.cc      \
        google/protobuf/compiler/java/java_map_field_lite.cc      \
        google/protobuf/compiler/java/java_message.cc      \
        google/protobuf/compiler/java/java_message_builder.cc      \
        google/protobuf/compiler/java/java_message_builder_lite.cc      \
        google/protobuf/compiler/java/java_message_field.cc      \
        google/protobuf/compiler/java/java_message_field_lite.cc      \
        google/protobuf/compiler/java/java_message_lite.cc      \
        google/protobuf/compiler/java/java_name_resolver.cc      \
        google/protobuf/compiler/java/java_primitive_field.cc      \
        google/protobuf/compiler/java/java_primitive_field_lite.cc      \
        google/protobuf/compiler/java/java_service.cc      \
        google/protobuf/compiler/java/java_shared_code_generator.cc      \
        google/protobuf/compiler/java/java_string_field.cc      \
        google/protobuf/compiler/java/java_string_field_lite.cc      \
        google/protobuf/compiler/javanano/javanano_enum.cc      \
        google/protobuf/compiler/javanano/javanano_enum_field.cc      \
        google/protobuf/compiler/javanano/javanano_extension.cc      \
        google/protobuf/compiler/javanano/javanano_field.cc      \
        google/protobuf/compiler/javanano/javanano_file.cc      \
        google/protobuf/compiler/javanano/javanano_generator.cc      \
        google/protobuf/compiler/javanano/javanano_helpers.cc      \
        google/protobuf/compiler/javanano/javanano_map_field.cc      \
        google/protobuf/compiler/javanano/javanano_message.cc      \
        google/protobuf/compiler/javanano/javanano_message_field.cc      \
        google/protobuf/compiler/javanano/javanano_primitive_field.cc      \
        google/protobuf/compiler/js/js_generator.cc      \
        google/protobuf/compiler/objectivec/objectivec_enum.cc      \
        google/protobuf/compiler/objectivec/objectivec_enum_field.cc      \
        google/protobuf/compiler/objectivec/objectivec_extension.cc      \
        google/protobuf/compiler/objectivec/objectivec_field.cc      \
        google/protobuf/compiler/objectivec/objectivec_file.cc      \
        google/protobuf/compiler/objectivec/objectivec_generator.cc      \
        google/protobuf/compiler/objectivec/objectivec_helpers.cc      \
        google/protobuf/compiler/objectivec/objectivec_map_field.cc      \
        google/protobuf/compiler/objectivec/objectivec_message.cc      \
        google/protobuf/compiler/objectivec/objectivec_message_field.cc      \
        google/protobuf/compiler/objectivec/objectivec_oneof.cc      \
        google/protobuf/compiler/objectivec/objectivec_primitive_field.cc      \
        google/protobuf/compiler/plugin.cc      \
        google/protobuf/compiler/plugin.pb.cc      \
        google/protobuf/compiler/python/python_generator.cc      \
        google/protobuf/compiler/ruby/ruby_generator.cc      \
        google/protobuf/compiler/subprocess.cc      \
        google/protobuf/compiler/zip_writer.cc      \

LOCAL_CFLAGS := -D GOOGLE_PROTOBUF_NO_RTTI=1
LOCAL_CPPFLAGS := -std=c++11
LOCAL_C_INCLUDES = $(LOCAL_PATH)/android 
LOCAL_C_INCLUDES += ${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.8/include
LOCAL_LDLIBS += -lz
LOCAL_EXPORT_LDLIBS += -lz
LOCAL_EXPORT_CFLAGS := $(LOCAL_CFLAGS)
LOCAL_EXPORT_CPPFLAGS := $(LOCAL_CPPFLAGS)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
include $(BUILD_SHARED_LIBRARY)

Step 3 : You need to add bellow flag to Android.mk

LOCAL_CFLAGS += $(cf_includes) -DGOOGLE_PROTOBUF_NO_RTTI

refering to link Android NDK Standalone toolchain

Using above steps may resolve this problem.

Community
  • 1
  • 1
Swapnil
  • 2,409
  • 4
  • 26
  • 48