From 4aa44b36a005967ba3bfff09d740eb2edd93260f Mon Sep 17 00:00:00 2001 From: darkfi Date: Sat, 21 Sep 2024 15:28:03 +0200 Subject: [PATCH] wallet: tor-dirmgr tries to pull sqlite3, which on android has a missing symbol __extenddftf2 due to missing compiler builtins. We therefore link against clang_rt.builtins for each target_arch on android using a build.rs. See the code comment for more details. --- bin/darkwallet/Cargo.toml | 5 ++- bin/darkwallet/Makefile | 4 ++ bin/darkwallet/README.md | 20 +++++++--- bin/darkwallet/build.rs | 68 ++++++++++++++++++++++++++++++++++ bin/darkwallet/src/darkirc2.rs | 7 ++-- bin/darkwallet/src/main.rs | 4 ++ script/evgrd/Cargo.toml | 36 +----------------- 7 files changed, 99 insertions(+), 45 deletions(-) create mode 100644 bin/darkwallet/build.rs diff --git a/bin/darkwallet/Cargo.toml b/bin/darkwallet/Cargo.toml index 479817da9..312cc60af 100644 --- a/bin/darkwallet/Cargo.toml +++ b/bin/darkwallet/Cargo.toml @@ -54,8 +54,9 @@ halo2_gadgets = {git="https://github.com/parazyd/halo2", branch="v4"} [target.'cfg(target_os = "android")'.dependencies] android_logger = "0.13.3" -#openssl = { version = "*", features = ["vendored"] } -rusqlite = {version = "0.31.0", features = ["bundled"]} +# Required by Arti: tor-dirmgr +#rusqlite = {version = "0.31.0", features = ["bundled"]} +tor-dirmgr = {version="0.21.0", features=["static"]} [target.'cfg(target_os = "linux")'.dependencies] simplelog = "0.12.1" diff --git a/bin/darkwallet/Makefile b/bin/darkwallet/Makefile index 6415c0170..266eb1a45 100644 --- a/bin/darkwallet/Makefile +++ b/bin/darkwallet/Makefile @@ -17,6 +17,10 @@ android: fonts adb logcat -c adb logcat -s darkfi +# Useful for dev +cli: + podman run -v $(shell pwd)/../../:/root/darkfi -w /root/darkfi/bin/darkwallet/ -it apk bash + clean: podman run -v $(shell pwd):/root/dw -w /root/dw -t apk rm -fr target/ diff --git a/bin/darkwallet/README.md b/bin/darkwallet/README.md index 61d2711b7..30c25575a 100644 --- a/bin/darkwallet/README.md +++ b/bin/darkwallet/README.md @@ -6,15 +6,19 @@ make # Android -Make sure you have podman installed. Then run `make android`. - -To debug any issues, you can enter an interactive terminal using: +Make sure you have podman installed. Then run: ``` -podman run -v $(pwd):/root/dw -it apk bash +make android ``` -# Debugging Missing Symbols (note to self) +To debug any issues, you can enter an interactive terminal using `make cli`. + +# Useful Dev Commands + +This is just devs. + +## Debugging Missing Symbols ``` "hb_ft_font_create_referenced" @@ -22,3 +26,9 @@ podman run -v $(pwd):/root/dw -it apk bash nm libharfbuzz_rs-5d6b743170eb0207.rlib | grep hb_ | less ``` +## Resolve Dependency Issues + +``` +cargo tree --target aarch64-linux-android --invert openssl-sys +``` + diff --git a/bin/darkwallet/build.rs b/bin/darkwallet/build.rs new file mode 100644 index 000000000..75d35c0fb --- /dev/null +++ b/bin/darkwallet/build.rs @@ -0,0 +1,68 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2024 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use std::{fs::File, io::Write}; + +/// Adds a temporary workaround for [an issue] with the Rust compiler and Android when +/// compiling sqlite3 bundled C code. +/// +/// The Android NDK used to include `libgcc` for unwind support (which is required by Rust +/// among others). From NDK r23, `libgcc` is removed, replaced by LLVM's `libunwind`. +/// However, `libgcc` was ambiently providing other compiler builtins, one of which we +/// require: `__extenddftf2` for software floating-point emulation. This is used by SQLite +/// (via the `rusqlite` crate), which defines a `LONGDOUBLE_TYPE` type as `long double`. +/// +/// Rust uses a `compiler-builtins` crate that does not provide `__extenddftf2` because +/// it involves floating-point types that are not supported by Rust. +/// +/// The workaround comes from [this Mozilla PR]: we tell Cargo to statically link the +/// builtins from the Clang runtime provided inside the NDK, to provide this symbol. +/// +/// See also this [zcash issue] and [their workaround]. +/// +/// [an issue]: https://github.com/rust-lang/rust/issues/109717 +/// [this Mozilla PR]: https://github.com/mozilla/application-services/pull/5442 +/// [unsupported]: https://github.com/rust-lang/compiler-builtins#unimplemented-functions +/// [zcash issue]: https://github.com/zcash/librustzcash/issues/800 +/// [their workaround]: https://github.com/Electric-Coin-Company/zcash-android-wallet-sdk/blob/88058c63461f2808efc953af70db726b9f36f9b9/backend-lib/build.rs +fn main() { + let target_os = std::env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set"); + let target_arch = + std::env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set"); + //println!("cargo:warning={target_arch}"); + + // Add some useful debug info directly into the app itself + let mut f = File::create("src/build_info.rs").unwrap(); + writeln!(f, "pub const TARGET_OS: &'static str = \"{target_os}\";").unwrap(); + writeln!(f, "pub const TARGET_ARCH: &'static str = \"{target_arch}\";").unwrap(); + + if target_os == "android" { + // Since we run this inside a container, we can just hardcore the paths directly + println!("cargo:rustc-link-search=/usr/local/android-ndk-r25/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/14.0.6/lib/linux/"); + match target_arch.as_str() { + "aarch64" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-aarch64-android"), + "arm" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-arm-android"), + "i686" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-i686-android"), + "x86_64" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android"), + // Maybe this should panic instead + _ => println!( + "cargo:warning='leaving linker args for {target_os}:{target_arch} unchanged" + ), + } + } +} diff --git a/bin/darkwallet/src/darkirc2.rs b/bin/darkwallet/src/darkirc2.rs index 19d3526ca..55dd23780 100644 --- a/bin/darkwallet/src/darkirc2.rs +++ b/bin/darkwallet/src/darkirc2.rs @@ -51,13 +51,14 @@ pub async fn receive_msgs(sg_root: SceneNodePtr, ex: ExecutorPtr) -> Result<()> let chatview_node = sg_root.lookup_node("/window/view/chatty").ok_or(Error::ConnectFailed)?; info!(target: "darkirc", "Instantiating DarkIRC event DAG"); - let datastore = expand_path(EVGRDB_PATH)?; - fs::create_dir_all(&datastore).await?; - let sled_db = sled::open(datastore)?; + //let datastore = expand_path(EVGRDB_PATH)?; + //fs::create_dir_all(&datastore).await?; + let sled_db = sled::open(EVGRDB_PATH)?; let evgr = LocalEventGraph::new(sled_db.clone(), "darkirc_dag", 1, ex.clone()).await?; let endpoint = "tcp://127.0.0.1:5588"; + let endpoint = "tcp://192.168.1.38:5588"; let endpoint = Url::parse(endpoint)?; let dialer = Dialer::new(endpoint.clone(), None).await?; diff --git a/bin/darkwallet/src/main.rs b/bin/darkwallet/src/main.rs index ea9893e90..286d30d47 100644 --- a/bin/darkwallet/src/main.rs +++ b/bin/darkwallet/src/main.rs @@ -46,6 +46,7 @@ extern crate log; use log::LevelFilter; mod app; +mod build_info; //mod darkirc; mod darkirc2; mod error; @@ -104,6 +105,9 @@ fn main() { simplelog::CombinedLogger::init(vec![term_logger]).expect("logger"); } + info!("Target OS: {}", build_info::TARGET_OS); + info!("Target arch: {}", build_info::TARGET_ARCH); + let ex = Arc::new(smol::Executor::new()); let sg_root = SceneNode3::root(); diff --git a/script/evgrd/Cargo.toml b/script/evgrd/Cargo.toml index be1f902db..5cd6ed30d 100644 --- a/script/evgrd/Cargo.toml +++ b/script/evgrd/Cargo.toml @@ -17,59 +17,25 @@ name = "test" path = "bin/test.rs" [dependencies] -darkfi = {path = "../../", features = ["async-daemonize", "event-graph", "rpc", "zk"]} -darkfi-sdk = {path = "../../src/sdk", features = ["async"]} +darkfi = {path = "../../", features = ["event-graph"]} darkfi-serial = {path = "../../src/serial", features = ["async"]} -futures-lite = "2.3.0" -libc = "0.2.158" # Event Graph DB sled-overlay = "0.1.3" -# TLS -async-trait = "0.1.81" -futures = "0.3.30" -futures-rustls = {version = "0.26.0", default-features = false, features = ["logging", "tls12", "ring"]} -rustls-pemfile = "2.1.3" - # Crypto blake3 = "1.5.4" -bcrypt = "0.15.1" -crypto_box = {version = "0.9.1", features = ["std", "chacha20"]} -rand = "0.8.5" # Misc log = "0.4.22" url = "2.5.2" -# Encoding and parsing -bs58 = "0.5.1" -toml = "0.8.19" -semver = "1.0.23" - # Daemon -easy-parallel = "3.3.1" -signal-hook-async-std = "0.2.2" -signal-hook = "0.3.17" -simplelog = "0.12.2" smol = "2.0.1" -# Argument parsing -serde = {version = "1.0.209", features = ["derive"]} -structopt = "0.3.26" -structopt-toml = "0.5.1" - -# See https://github.com/rust-mobile/android-rs-glue/issues/193 -[target.aarch64-linux-android.dependencies] -openssl = { version = "*", features = ["vendored"] } -rusqlite = {version = "0.31.0", features = ["bundled"]} - #[lints] #workspace = true # Temp stuff [workspace] -[patch.crates-io] -halo2_proofs = {git="https://github.com/parazyd/halo2", branch="v4"} -halo2_gadgets = {git="https://github.com/parazyd/halo2", branch="v4"}