Rust on iOS

Porting process

Finally got working Rust cross-compiler for iOS so now it is possible to create static libraries which could be used from Objective C. For now only armv7 architecture is supported (although there are upcoming commits in “try” branch which will update LLVM version with ARM64 support right from Apple).

These were main pain points:

  • Slow full rebuild… Yep, I know, I know, 3 stage bootstrapping to be extremely optimized but still… it was especially painful on initial stage while experimenting with built options and LLVM patching.

  • TLS isn’t supported on armv7 so it should be emulated. Initially I’ve started adding it to LLVM and with help from Tim Northover and Dan Olson even got it working. (BTW, Dan knows a lot about this topic as he got a working D compiler for iOS, so if you’re interested in modern compiled language for iOS - you can try it too). Just to discover that actually Rust has built-in TLS “emulation” support (actually using the same technique as in LLVM patches)… Grr, hate myself for not checking this before messing with LLVM.

  • There were absolutely crazy crashes hard to debug because some kind linker errors/misconfiguration when functions were resolved correctly but all the data was relocated incorrectly and contained garbage.

  • There were mystical crashes in hash function which automagically disappeared once I’ve increased optimization level.

  • The final show-stopper was happening in runtime finalization routine - it always crashed on misaligned data during mutex locking. The reason was that mutex opaque data was copied as a plain data. It wasn’t the case on desktop OSes but seemed to be a big difference on iOS. There is a simple fix for native runtime and should be similar fix for green/rustuv.

What works

  • armv7 only (ARM64 port should be quite easy to introduce)

  • it is possible to compile standard libraries and use them

  • “native”-based runtime

  • creating static libraries which could be used from iOS applications

Known issues

  • green/rustuv runtime crashes because of incorrect mutex implementation

  • segmented stacks aren’t working

  • it might require additional manual linking

  • no support for simulator yet

  • it only works on common compiler flags. It is critical to have exactly –opt-level 2 (or -O) as both decreasing and increasing opt level may cause a lot of unexplainable issues

Shut up and take my money Build instructions

clone and create build dir

git clone git@github.com/vhbit/rust.git
cd rust
git checkout ios
mkdir build_ios
cd build_ios

configure

../configure --target=arm-apple-ios

build

make

or (for 4 core CPU)

make -j4

There is a sample project with instructions on how to integrate.

Thanks

Special thanks to Tim Northover (ARM TLS), Dan Olson (ARM TLS), Alex Crichton (Rust internals) and my wife for being patient.

Comments