4

Trying to experiment with other DYLD_ properties I've found that jvm is manipulating with properties and they are ignored during execution.
My Java test:

class Env {
    public static void main(String... args) {
        System.getenv().entrySet().stream().forEach(e -> System.out.println(e.getKey() + " = " + e.getValue()));
    }
}

Invocation:

$ export DYLD_PRINT_LIBRARIES=1
$ export MY_PRINT_LIBRARIES=2
$ javac Env.java && java Env|grep PRINT
MY_PRINT_LIBRARIES = 2
$ 

On the other side, my C test:

#include <stdio.h>

int main(int argc, char **argv, char **envp) {
    while (*envp) {
        printf("%s\n", *envp);
        envp++;
    }
    return 0;
}

Invocation:

$ gcc env.c && ./a.out|grep PRINT
dyld: loaded: /Users/okutane/test/java/./a.out
dyld: loaded: /usr/lib/libSystem.B.dylib
dyld: loaded: /usr/lib/system/libcache.dylib
...
dyld: loaded: /usr/lib/libc++.1.dylib
dyld: loaded: /usr/lib/libDiagnosticMessagesClient.dylib
MY_PRINT_LIBRARIES=2
DYLD_PRINT_LIBRARIES=1
$

I expected jvm test to work too, is there are any workaround?

okutane
  • 13,754
  • 10
  • 59
  • 67

1 Answers1

8

With the introduction of SIP, all environment variables matching DYLD_* will be stripped before executing a restricted binary. That includes the /usr/bin/java binary you would be using:

$ ls -lOL /usr/bin/java
-rwxr-xr-x  1 root  wheel  restricted,compressed 58560 Sep  7 06:41 /usr/bin/java*
raimue
  • 4,322
  • 1
  • 21
  • 31
  • Any way around this? – Max Coplan Oct 27 '20 at 19:53
  • 1
    It depends on what you actually want to achieve. Maybe you are actually looking for `java -Djava.library.path=...`? However, to get `DYLD_*` variables working with `/usr/bin/java`, you would need to completely [disable SIP](https://developer.apple.com/documentation/security/disabling_and_enabling_system_integrity_protection). – raimue Oct 28 '20 at 15:39