|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * The Universal Permissive License (UPL), Version 1.0
|
|
163 | 163 | import com.oracle.truffle.api.dsl.UnsupportedSpecializationException;
|
164 | 164 | import com.oracle.truffle.api.frame.VirtualFrame;
|
165 | 165 | import com.oracle.truffle.api.interop.ArityException;
|
| 166 | +import com.oracle.truffle.api.interop.InteropException; |
166 | 167 | import com.oracle.truffle.api.interop.InteropLibrary;
|
167 | 168 | import com.oracle.truffle.api.interop.InvalidArrayIndexException;
|
168 | 169 | import com.oracle.truffle.api.interop.TruffleObject;
|
@@ -799,6 +800,70 @@ Object py_dl_sym(VirtualFrame frame, Object obj, Object name,
|
799 | 800 | }
|
800 | 801 | }
|
801 | 802 |
|
| 803 | + @Builtin(name = "_dyld_shared_cache_contains_path", minNumOfPositionalArgs = 1) |
| 804 | + @GenerateNodeFactory |
| 805 | + protected abstract static class DyldSharedCacheConstainsPath extends PythonBinaryBuiltinNode { |
| 806 | + private static boolean hasDynamicLoaderCacheValue = false; |
| 807 | + private static boolean hasDynamicLoaderCacheInit = false; |
| 808 | + |
| 809 | + private static boolean hasDynamicLoaderCache() { |
| 810 | + if (hasDynamicLoaderCacheInit) { |
| 811 | + return hasDynamicLoaderCacheValue; |
| 812 | + } |
| 813 | + |
| 814 | + CompilerDirectives.transferToInterpreter(); |
| 815 | + if (System.getProperty("os.name").contains("Mac")) { |
| 816 | + String osVersion = System.getProperty("os.version"); |
| 817 | + // dynamic linker cache support on os.version >= 11.x |
| 818 | + int major = 11; |
| 819 | + int i = osVersion.indexOf('.'); |
| 820 | + try { |
| 821 | + major = Integer.parseInt(i < 0 ? osVersion : osVersion.substring(0, i)); |
| 822 | + } catch (NumberFormatException e) { |
| 823 | + } |
| 824 | + hasDynamicLoaderCacheValue = major >= 11; |
| 825 | + } else { |
| 826 | + hasDynamicLoaderCacheValue = false; |
| 827 | + } |
| 828 | + hasDynamicLoaderCacheInit = true; |
| 829 | + return hasDynamicLoaderCacheValue; |
| 830 | + } |
| 831 | + |
| 832 | + @Specialization |
| 833 | + Object py_dyld_shared_pstring(VirtualFrame frame, PString ppath, |
| 834 | + @CachedLibrary(limit = "1") InteropLibrary ilib) { |
| 835 | + return py_dyld_shared_cache_contains_path(frame, ppath.getValue(), ilib); |
| 836 | + } |
| 837 | + |
| 838 | + @CompilationFinal Object cachedFunction = null; |
| 839 | + |
| 840 | + // TODO: 'path' might need to be processed using FSConverter. |
| 841 | + @Specialization |
| 842 | + Object py_dyld_shared_cache_contains_path(VirtualFrame frame, String path, |
| 843 | + @CachedLibrary(limit = "1") InteropLibrary ilib) { |
| 844 | + if (!hasDynamicLoaderCache()) { |
| 845 | + throw raise(NotImplementedError, "_dyld_shared_cache_contains_path symbol is missing"); |
| 846 | + } |
| 847 | + |
| 848 | + try { |
| 849 | + if (cachedFunction == null) { |
| 850 | + String name = "_dyld_shared_cache_contains_path"; |
| 851 | + CompilerDirectives.transferToInterpreter(); |
| 852 | + DLHandler handle = DlOpenNode.loadNFILibrary(getContext(), NFIBackend.NATIVE, "", RTLD_LOCAL.getValueIfDefined()); |
| 853 | + Object sym = ilib.readMember(handle.getLibrary(), name); |
| 854 | + // bool _dyld_shared_cache_contains_path(const char* path) |
| 855 | + Source source = Source.newBuilder(NFI_LANGUAGE, "(string):sint32", name).build(); |
| 856 | + Object nfiSignature = getContext().getEnv().parseInternal(source).call(); |
| 857 | + cachedFunction = SignatureLibrary.getUncached().bind(nfiSignature, sym); |
| 858 | + assert ilib.isExecutable(cachedFunction); |
| 859 | + } |
| 860 | + return (int) ilib.execute(cachedFunction, path) != 0; |
| 861 | + } catch (InteropException e) { |
| 862 | + return false; |
| 863 | + } |
| 864 | + } |
| 865 | + } |
| 866 | + |
802 | 867 | @Builtin(name = "alignment", minNumOfPositionalArgs = 1)
|
803 | 868 | @GenerateNodeFactory
|
804 | 869 | protected abstract static class AlignmentNode extends PythonUnaryBuiltinNode {
|
|
0 commit comments