Coder Social home page Coder Social logo

Overhead of SecurityManager about nailgun HOT 17 OPEN

facebook avatar facebook commented on July 27, 2024 3
Overhead of SecurityManager

from nailgun.

Comments (17)

unkarjedy avatar unkarjedy commented on July 27, 2024 1

Note that SecurityManager is deprecated since Java 17 and will be removed in future releases:
https://openjdk.java.net/jeps/411

To move the Java Platform forward, we will deprecate the legacy Security Manager technology for removal from the JDK. We plan to deprecate and attenuate the capabilities of the Security Manager over a number of releases, simultaneously creating alternative APIs for such tasks as blocking System::exit and other use cases considered important enough to have replacements.

Currently, NailgunServer produces a warning in stderr:

WARNING: A terminally deprecated method in java.lang.System has been called
WARNING: System::setSecurityManager has been called by %s
WARNING: Please consider reporting this to the maintainers of %s
WARNING: System::setSecurityManager will be removed in a future release

from nailgun.

sbalabanov-zz avatar sbalabanov-zz commented on July 27, 2024

That's interesting. Is it only Nailgun related and if such then why? Permission checks are done for all file operations under Oracle's HotSpot implementation of a Path/File provider, it has to do nothing with nailgun.

Also, if you use HotSpot then this approach probably violates Oracle's Java license as system code is essentially altered.

To come along with this problems on projects we use with Nailgun, we are considering following options:

  • Implementing a custom FileSystem/FileSystemProvider
  • Running on forked OpenJDK which has those checks off

from nailgun.

justinas-dabravolskas avatar justinas-dabravolskas commented on July 27, 2024

Security checks are triggered just in case securityManager!=null http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/sun/misc/URLClassPath.java#URLClassPath.checkURL%28java.net.URL%29
Overhead is added even with NOOP security manager. Usually you don't run javac under security manager, but Nailgun sets one for exit trap, so this overhead is triggered specifically by Nailgun https://github.com/facebook/nailgun/blob/master/nailgun-server/src/main/java/com/martiansoftware/nailgun/NGServer.java

from nailgun.

sbalabanov-zz avatar sbalabanov-zz commented on July 27, 2024

SecurityManager is the only way to intercept termination that I know of. Without altering bytecode, the only way I see is to pass a parameter to Nailgun whether or not to intercept termination. But, with interception switched off, you can't use System.exit in nail's code as it would do what supposed - terminate jvm instead of terminating a nail. Thoughts?

from nailgun.

justinas-dabravolskas avatar justinas-dabravolskas commented on July 27, 2024

The only option 'without side effects' I know is byte code manipulation. I've tried just disabling exit interception and it broke Nailgun protocol, I guess it can be done in more elegant way, but terminating jvm after nail's System.exit doesn't sound like an option.

from nailgun.

sbalabanov-zz avatar sbalabanov-zz commented on July 27, 2024

What I mean is that instead of calling System.exit() the nail should call some other function explicitly (like NGSession.exit() which yet to be implemented) which will make sure the protocol is not broken and terminate a nail but not jvm. A linter should validate what no System.exit() code sneaks in for nail's codebase.
We can consider doing that for Buck.
Are you saying the win is mostly for in-proc javac? How about regular class loader?

from nailgun.

jvican avatar jvican commented on July 27, 2024

the nail should call some other function explicitly (like NGSession.exit())

Are nails not supposed to run ngSession.exit() instead of System.exit() directly? If that's the case and the security manager is disabled completely, would it break the invariants of the Nailgun protocol?

from nailgun.

sbalabanov-zz avatar sbalabanov-zz commented on July 27, 2024

ngSession.exit() does not really interrupt running nail and in fact does not work correctly; the problem is client terminates the socket and the server still listens to that in NGCommunicator which cause listening thread to throw and log an error. This needs to be processed gracefully.

@justinas-dabravolskas do you mind to share numbers about the optimization win, like how many classes were sent to javac to achieve above mentioned improvement?

from nailgun.

justinas-dabravolskas avatar justinas-dabravolskas commented on July 27, 2024

javac for ~50k files takes ~75 minutes without agent and 10-15minutes with one (Xmx set for minimal gc overhead, tested on macOS). Anyways overhead of security manager should be linear and not limited to javac. Offtopic suggestion for running javac 1.8 (should not be an issue with java 9) inside nailgun: use -XDuseUnsharedTable. In our case it saves ~2G of heap without noticeable time overhead.

from nailgun.

retronym avatar retronym commented on July 27, 2024

An alternative to simply avoiding use of the security manager would change NGSecurityManager to override more methods from SecurityManager to route around the slow parts.

For instance, the default checkWrite implementation:

    public void checkWrite(String file) {
        checkPermission(new FilePermission(file,
            SecurityConstants.FILE_WRITE_ACTION));
    }

Calls new File(file).canonicalize during construction of FilePermission.

(I just came across this performance problem in the context of IntelliJ IDEA's use of Nailgun in its compile server for Scala)


I see now that #11 already implemented this idea for checkRead. I'll submit a PR to make this more comprehensive.

from nailgun.

RobertDeRose avatar RobertDeRose commented on July 27, 2024

This might be unrelated, as my knowledge of the SecurityManager protocol is about ZERO. However, while trying to embed Nailgun into a project, the fact that Nailgun overtakes the SecurityManager makes it so the parent application is unable to exit via System.exit as it throws an NGExitException.

My only workaround thus far has been to reset the SecurityManager back to the base after starting the NailgunServer. Unknown to me at this time what side-effects that has.

There is another issue about a lack of documentation, if this issue could result in some more documentation for the purpose of the NGSecurityManager and how to properly embed it, that would be awesome.

from nailgun.

kolotyluk avatar kolotyluk commented on July 27, 2024

So, this is a blocking issue for me because I am trying to run Scala 3 code under JDK18 with Project Loom.

I look forward to a fix so that I can use Scala 3...

https://users.scala-lang.org/t/scala-compile-server-exception/7972

from nailgun.

sbalabanov avatar sbalabanov commented on July 27, 2024

@kolotyluk you are welcome to come up with the fix :)
so far no one in this thread suggested a working solution to intercept System.exit() calls without using SecurityManager.

from nailgun.

pjfanning avatar pjfanning commented on July 27, 2024

I haven't seen a great deal of discussion online about how to replace the SecurityManager. This is one the best to read - https://inside.java/2021/04/23/security-and-sandboxing-post-securitymanager/ .

It seems like ASM or byte-buddy might be the best bets for changing the System.exit behaviour.

from nailgun.

unkarjedy avatar unkarjedy commented on July 27, 2024

Note that in Scala Plugin 2021.3 we added a cosmetic workaround:
We ignore this warning output and do not show this output as a notification to the user.

(IJ Scala Plugin Compile Server uses Nailgun under the hood)

from nailgun.

unkarjedy avatar unkarjedy commented on July 27, 2024

@kolotyluk

So, this is a blocking issue for me because I am trying to run Scala 3 code under JDK18 with Project Loom.
I look forward to a fix so that I can use Scala 3...

Could you please clarify in which way it "blocks" you?
Scala Compile Server can be used as before.
The only difference is that during startup users saw an error notification, which might frighten them.
The notification shouldn't affect Scala 3 (or Scala 2) experience in any way.

from nailgun.

stewert72 avatar stewert72 commented on July 27, 2024

With JDK 18 I get following Exception while starting the nailgun server. Switching back to JDK17 and it works again. Maybe that's related to this topic.

[root@xxx]# java -Dlog4j.properties=/opt/xxx/cfg/log4j.properties -cp /opt/xxx/lib/nailgun-server.jar:some.more.jar com.facebook.nailgun.NGServer
Exception in thread "NGServer(all addresses, port 2113)" java.lang.UnsupportedOperationException: The Security Manager is deprecated and will be removed in a future release
at java.base/java.lang.System.setSecurityManager(System.java:416)
at com.facebook.nailgun.NGServer.run(NGServer.java:318)
at java.base/java.lang.Thread.run(Thread.java:833)
NGServer shut down.

[root@xxx]# java -version
openjdk version "18.0.1" 2022-04-19
OpenJDK Runtime Environment 22.3 (build 18.0.1+10)
OpenJDK 64-Bit Server VM 22.3 (build 18.0.1+10, mixed mode, sharing)

from nailgun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.