Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems running recompiled extension on M1/Monterey combo #328

Closed
JamesGuthrie opened this issue Dec 13, 2021 · 3 comments · Fixed by #333
Closed

Problems running recompiled extension on M1/Monterey combo #328

JamesGuthrie opened this issue Dec 13, 2021 · 3 comments · Fixed by #333

Comments

@JamesGuthrie
Copy link
Contributor

JamesGuthrie commented Dec 13, 2021

I seem to be running into an issue in the M1/Monterey combo which appears to be a bug in macOS.

The short version is: When I change the contents of my extension and recompile, dylib refuses to load it and fails with Code Signature Invalid. This appears to have something to do with the file being "copied over" from /target... to the .pgx directory. I have opened an issue on the apple developer forums which goes into a fair amount of detail: https://developer.apple.com/forums/thread/696460

With the following (awfully hacky) patch (which just removes the .so before copying it in), I am able to work around the issue that I'm seeing:

diff --git a/cargo-pgx/src/commands/install.rs b/cargo-pgx/src/commands/install.rs
index c4d0356..fd56b98 100644
--- a/cargo-pgx/src/commands/install.rs
+++ b/cargo-pgx/src/commands/install.rs
@@ -45,6 +45,12 @@ pub(crate) fn install_extension(
         let mut dest = base_directory.clone();
         dest.push(&pkgdir);
         dest.push(format!("{}.so", extname));
+        if dest.exists() {
+            handle_result!(
+                std::fs::remove_file(&dest),
+                format!("unable to remove existing file {}", dest.display())
+            )
+        }
         copy_file(&shlibpath, &dest, "shared library", false);
     }
 

@Hoverbear I'm curious to find out if you run into the same issue when you upgrade to Monterey.

@JamesGuthrie
Copy link
Contributor Author

JamesGuthrie commented Dec 14, 2021

I got feedback on the issue, and it turns out that as far as Apple is concerned, this is a "works as designed": https://developer.apple.com/documentation/security/updating_mac_software.

@eeeebbbbrrrr
Copy link
Contributor

So deleting the .so before copying in the new one fixes it? If that's the case, send us a PR and I'll merge and release later today.

I read the linked developer.apple.com article but I can't say I understand. We're not signing anything, so I'm a little confused about it.

@JamesGuthrie
Copy link
Contributor Author

That's correct, as far as I see it there are two workarounds:

  1. Delete the so before copying in the new one
  2. Write the new so to temporary file and then rename that new file over the old file

I was also confused by the whole thing. It's a new thing which is being enforced on M1 macs (and possibly also new in Monterey). The compiler automatically signs the shared library.

Hoverbear pushed a commit that referenced this issue Dec 16, 2021
This is a workaround for an issue on new M1 Macs, as outlined in [1].

The basic rundown is: Apple has recently added mandatory signing of
binaries and libraries, with associated automatic signing of
locally-compiled artifacts.

The signature verification uses a static in-kernel inode-based signature
cache. Once cached, the signature associated with an inode is not
updated.

This means that by "copying over" the shared library, the kernel
compares the outdated signature (from its static cache) to the new
shared library contents.

This workaround removes the existing .so, ensuring that a new inode is
used for the newly-compiled extension .so.

Fixes #328

[1]: https://developer.apple.com/documentation/security/updating_mac_software
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants