rust-mobile / android-rs-glue

Glue between Rust and Android
Apache License 2.0
904 stars 109 forks source link

problems with link time optimizations? #158

Open tiby312 opened 7 years ago

tiby312 commented 7 years ago

I get the following runtime error when I run an apk generated with the cargo flag lto=true. ` 08-26 18:58:31.364 15668 15668 D AndroidRuntime: Shutting down VM 08-26 18:58:31.365 15668 15668 E AndroidRuntime: FATAL EXCEPTION: main 08-26 18:58:31.365 15668 15668 E AndroidRuntime: Process: rust.kdtree, PID: 15668 08-26 18:58:31.365 15668 15668 E AndroidRuntime: java.lang.UnsatisfiedLinkError: Unable to load native library "/data/app/rust.kdtree-1/lib/arm/libmain.so": dlopen failed: cannot locate symbol "ZN38$LT$core..option..Option$LT$T$GT$$GT$6unwrap18_MSG_FILE_LINE_COL17h711e8ccdb50917a3E" referenced by "/data/app/rust.kdtree-1/lib/arm/libmain.so"... 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.app.NativeActivity.onCreate(NativeActivity.java:182) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6687) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.app.ActivityThread.-wrap12(ActivityThread.java) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6165) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 08-26 18:58:31.365 15668 15668 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

`

tiby312 commented 7 years ago

I noticed that the mangled symbol is similar to one I found here: https://github.com/alexcrichton/rustc-demangle/blob/master/src/lib.rs

#[test]
fn demangle_osx() {
    t!("__ZN5alloc9allocator6Layout9for_value17h02a996811f781011E", "alloc::allocator::Layout::for_value::h02a996811f781011");
    t!("__ZN38_$LT$core..option..Option$LT$T$GT$$GT$6unwrap18_MSG_FILE_LINE_COL17haf7cb8d5824ee659E", "<core::option::Option<T>>::unwrap::_MSG_FILE_LINE_COL::haf7cb8d5824ee659");
    t!("__ZN4core5slice89_$LT$impl$u20$core..iter..traits..IntoIterator$u20$for$u20$$RF$$u27$a$u20$$u5b$T$u5d$$GT$9into_iter17h450e234d27262170E", "core::slice::<impl core::iter::traits::IntoIterator for &'a [T]>::into_iter::h450e234d27262170");
}

So seems like it demangles to something like <core::option::Option<T>>::unwrap::_MSG_FILE_LINE_COL::haf7cb8d5824ee659

This is cargo profile I'm using. If I set lto = false. The error goes away.

[profile.release]
opt-level = 3
lto = true 
debug = false
debug-assertions = false
codegen-units = 1 
panic = 'unwind'

Just throwing more info out there.

tiby312 commented 6 years ago

I think this might be related to this code change: https://fossies.org/diffs/rustc/1.21.0-src_vs_1.22.1-src/src/libcore/macros.rs-diff.html

tomaka commented 6 years ago

In any case, this is probably a problem with Rust and not with this crate. However this issue can stay open to keep track of this.