Skip to content

Failed consistency check when trying to relocate compiled app (MacOS) #943

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

Closed
rallen10 opened this issue May 14, 2024 · 2 comments
Closed

Comments

@rallen10
Copy link

I am running into an error when trying to relocate a very simple compiled app to a different part of my machine (not even a different machine).

System and Library Info

  • MacOS 14.4.1 on M1 Max
  • PackageCompiler v2.1.17
  • julia install via juliaup: curl -fsSL https://install.julialang.org | sh
  • julia versioninfo()
Julia Version 1.10.3
Commit 0b4590a5507 (2024-04-30 10:59 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

Minimum working example

I'm just trying to create a very simple app I'm calling AppSandbox

# AppSandbox.jl/src/AppSandbox.jl
module AppSandbox

function julia_main()::Cint
    print("Hello World from my compiled app!\n")
    return 0 # if things finished successfully
end

greet() = print("Greetings from AppSandbox!")

export greet

end # module AppSandbox

Then to build, I run the following using a local fix for issue #738 (see comment with fix here)

# pwd AppSandbox.jl. Started Julia with `julia --project=.`
using PackageCompiler

# re-define the create_sysimg_from_object_file function
using PackageCompiler: get_extra_linker_flags, julia_libdir, julia_private_libdir, ldlibs, bitflag, march, run_compiler
function PackageCompiler.create_sysimg_from_object_file(object_files::Vector{String},
                                        sysimage_path::String;
                                        version,
                                        compat_level::String,
                                        soname::Union{Nothing, String})

    if soname === nothing && (Sys.isunix() && !Sys.isapple())
        soname = basename(sysimage_path)
    end
    mkpath(dirname(sysimage_path))
    # Prevent compiler from stripping all symbols from the shared lib.
    if Sys.isapple()
        cltools_version_cmd = `pkgutil --pkg-info=com.apple.pkg.CLTools_Executables`
        cltools_version = match(r"version: (.*)\n", readchomp(cltools_version_cmd))[1]
        if startswith(cltools_version, "15")
            o_file_flags = `-Wl,-all_load $object_files -Wl,-ld_classic`
        else
            o_file_flags = `-Wl,-all_load $object_files`
        end
    else
        o_file_flags = `-Wl,--whole-archive $object_files -Wl,--no-whole-archive`
    end
    extra = get_extra_linker_flags(version, compat_level, soname)
    cmd = `$(bitflag()) $(march()) -shared -L$(julia_libdir()) -L$(julia_private_libdir()) -o $sysimage_path $o_file_flags $(Base.shell_split(ldlibs())) $extra`
    run_compiler(cmd; cplusplus=true)
    return nothing
end

# build the compiled app, overwritting compiled direction if it exists (force=true)
create_app(".", "build", force=true)

The app builds and I can successfully run it in the location it was built

# pwd is AppSandbox.jl
./build/bin/AppSandbox
# output:
# Hello World from my compiled app!

But if I try to relocate the app locally, I get the following error

cp -r build/ ~/Desktop/AppSandboxBuild
~/Desktop/AppSandboxBuild/bin/AppSandbox

# output
ERROR: System image file failed consistency check: maybe opened the wrong version?

[65750] signal (11.2): Segmentation fault: 11
in expression starting at none:0

[65750] signal (6): Abort trap: 6
in expression starting at none:0
Abort trap: 6
@hbe72
Copy link

hbe72 commented May 15, 2024

Maybe macOS security mechanism kicks in. macOS caches information about the code’s signature in the kernel.
It seems that Apple M processors require all code to be validly signed (if only ad hoc) or the operating system will not execute it, instead killing it on launch.

See https://developer.apple.com/documentation/security/updating_mac_software
and some related discussion in https://stackoverflow.com/questions/67378106/mac-m1-cping-binary-over-another-results-in-crash

@rallen10
Copy link
Author

@hbe72 This seems to be the issue! Thank you.

The fix/workaround is identified in Mac's documentation. In short, use ditto to move instead of cp

cp -r build/ ~/Desktop/AppSandboxBuild
~/Desktop/AppSandboxBuild/bin/AppSandbox
# This will fail with the above error

ditto build/ ~/Desktop/AppSandboxBuild2
~/Desktop/AppSandboxBuild2/bin/AppSandbox
# This works

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

No branches or pull requests

2 participants