android - Google protobuf 在解析字符串数据时在 Android 中崩溃

标签 android c++ crash protocol-buffers

protobuf 版本为 3.0.0。在某些设备(比如 Android 6)中,这个问题总是会发生;在某些设备(例如 Android 5.x)中,当应用程序进入后台模式时会发生此问题。

我正在调用以下方法:

bool ParseFromString(const string& data);

这可能在内部调用 bool ParseFromArray(const void* data, int size)。堆栈跟踪:

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)

显然如箭头所示,它在 google::protobuf::internal::WireFormatLite::ReadBytes(...) 处崩溃。此问题已在 this thread 中讨论过,用户通过将 VS2005 升级到 VS2008 来修复它。然而,我们在 Android 中运行它。

我也引用了以下帖子无济于事:

感谢任何详细的帮助,因为我是使用 Android 构建 protobuf 库的新手。

最佳答案

我按照后续步骤解决了上述问题

第 1 步:使用相同的 NDK 运行时 STLport_sharedgnuSTL_shared 构建 JNI 源代码和 Google Protobuf C++ 源代码,我们可以定义它在 Application.mk 文件中作为

APP_STL := gnustl_shared

我正在使用 gnuSTL-static 构建导致问题的 JNI 源代码。如果您在构建 Google Protobuf 源代码时使用不同的运行时,则可能会出现问题。您可以在 Android CPP support guide 获得更多关于 android NDK 运行时的信息。

第 2 步:确保您的 JNI 代码和共享库 (libprotobuf.so) 引用相同版本的 Google Protobuf 库。它可能会在不同的 android 版本中导致未定义的行为。

您可以从Google Protobuf Git Repo 下载最新的源代码。

您可以按照 Android Platform external protobuf link 中的构建步骤进行操作或者为简单起见,可以使用下面的“Andoid.mk”文件

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)

第 3 步:您需要将波纹管标志添加到 Android.mk

LOCAL_CFLAGS += $(cf_includes) -DGOOGLE_PROTOBUF_NO_RTTI

引用链接Android NDK Standalone toolchain

使用上述步骤可能会解决此问题。

关于android - Google protobuf 在解析字符串数据时在 Android 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37163821/

相关文章:

java - Android-将对象的 Intent 从一个 Activity 传递到另一个 Activity (崩溃)

c - 当功能返回空时,程序崩溃

android - 下载管理器不适用于 LG 设备

android - "WARN: error in opening zip file:"使用 AndroidStudio Kotlin REPL 时

c++ - 我可以检查初始化列表中设置的构造函数主体中的变量吗?

c++ - 具有继承的静态变量的行为

java - 为什么我会收到未经检查的作业警告?

Java Android 将数据发送到服务器应用程序永远不要使用 onPostExecute()

c++ - 冒泡排序忽略数组的第一个元素

java - Bump API Android 崩溃