google / mundane

Mundane is a Rust cryptography library backed by BoringSSL that is difficult to misuse, ergonomic, and performant (in that order).
MIT License
1.07k stars 46 forks source link

Automatically generate link_name attributes in boringssl.rs #7

Closed joshlf closed 6 years ago

joshlf commented 6 years ago

Currently, our bindgen.sh script does not automatically generate the link_name attributes that we need in order to perform symbol renaming as described in boringssl/README.md. Instead, we do this manually, which is annoying.

We should do this automatically. One good option would be to implement the bindgen feature described in this issue: https://github.com/rust-lang-nursery/rust-bindgen/issues/1375. It could also be good to have a simpler workaround until that feature is implemented.

inejge commented 6 years ago

Can a workaround be a postprocessing script called from bindgen.sh? I tested the following on a copy of boringssl.rs with #[link name...] lines removed and got the original file back. I can open a PR if the script is good enough.

diff --git a/boringssl/bindgen.sh b/boringssl/bindgen.sh
index 564423c..e52c289 100755
--- a/boringssl/bindgen.sh
+++ b/boringssl/bindgen.sh
@@ -143,3 +143,16 @@ cat boringssl.rs >> "$TMP"

 mv "$TMP" boringssl.rs
 rustfmt boringssl.rs
+
+awk -v "vers=${MAJOR}_${MINOR}_${PATCH}_" '
+/extern "C" {/ {
+    print
+    getline
+    if ($0 ~ "pub fn") {
+        fn = $3
+        sub("[(].*", "", fn)
+        print "    #[link_name = \"__RUST_MUNDANE_" vers fn "\"]"
+    }
+}
+{ print }' boringssl.rs >> "$TMP"
+mv "$TMP" boringssl.rs
joshlf commented 6 years ago

Yeah, that looks awesome! Check out CONTRIBUTING.md to see how to submit it. Would it be possible to also count the number of link_name attributes emitted by the script? We could make sure it matches the number of functions we expect in the script as a sanity check. I'm always a little wary of using simple syntax analysis on code because it can be so error-prone.

joshlf commented 6 years ago

Btw if you want to work on this, it'll probably be easier for you if you work on top of this PR (which is in turn on top of this PR).

inejge commented 6 years ago

Uploaded, let's hope I haven't messed up anything in the procedure.

joshlf commented 6 years ago

Looks like you got all of the Gerrit magic right; well done! Faster than it took me to figure out for sure.