Skip to content

RuntimeException due to Java SecurityManager enabled #405

@isaacrivriv

Description

@isaacrivriv

Hello there!! While using Netty as an HTTP Server, we ran into a java.lang.RuntimeException when the adaptive buffer allocator attempts to load pointing to shaded JCTools classes. Specifically the error seems to be attempting to access field/constructor from Unsafe.class as seen below, which if the SecurityManager is enabled will throw a java.security.AccessControlException

try
{
final Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
instance = (Unsafe) field.get(null);
}
catch (Exception ignored)
{
// Some platforms, notably Android, might not have a sun.misc.Unsafe implementation with a private
// `theUnsafe` static instance. In this case we can try to call the default constructor, which is sufficient
// for Android usage.
try
{
Constructor<Unsafe> c = Unsafe.class.getDeclaredConstructor();
c.setAccessible(true);
instance = c.newInstance();
}
catch (Exception e)
{
throw new RuntimeException(e);
}

This could be addressed by verifying if the SecurityManager is enabled and if so, use AccessController.doPrivileged for the logic. I searched on the repo but couldn't find any usage of the AccessController that would imply that something like this has been reported/addressed before. I wanted to bring it up since enabling Java 2 Security doesn't allow the UnsafeAccess class to load due to the RuntimeException caused by the AccessControlException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions