Search code examples
javamacosdynamic-librarydylddarwin

DYLD_PRINT_LIBRARIES not working


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?


Solution

  • 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*