classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Loading native libraries


From: Archie Cobbs
Subject: Loading native libraries
Date: Thu, 3 Jun 2004 09:38:53 -0500 (CDT)

I'm working on the right way to fix the bug described in bug #7084
"Runtime.nativeLoad() needs a ClassLoader parameter":

    http://savannah.gnu.org/bugs/?func=detailitem&item_id=7084

In a nutshell, all native libraries are supposed to be associated
with a specific class loader (and they are supposed to be unloaded
when/if that loader is unloaded). Right now they are not associated
with any specific class loader. Therefore, there are two things to fix:

#1. When loading the library by calling whatever native method,
    a ClassLoader parameter must be specified somehow.

#2. We must determine the correct class loader to specify.

Solving #1 is easy, we just add a ClassLoader parameter to the
VMRuntime.nativeLoad() method; this parameter can be null to indicate
the boot loader.

For #2, it's a little less clear. In the current version of
Runtime.loadLibrary(String) we acquire the loader to use like this:

  ClassLoader cl = VMSecurityManager.currentClassLoader();

but this is wrong. Imagine this call sequence:

  - MyClass is loaded by the system (not bootstrap) loader per usual
  - MyClass.main() invokes ObjectInputStream.<init>
  - This triggers loading of ObjectInputStream, which will be loaded
    by the bootstrap loader, and then its class initialization, which
    causes ObjectInputStream to invoke System.loadLibrary("javaio");

Then VMSecurityManager.currentClassLoader() will return the system class
loader rather than the bootstrap class loader, which is the wrong one
(it finds the first non-bootstrap loader on the stack).

That is, the "javaio" library should be loaded in association with the
bootstrap loader (ObjectInputStream's loader), rather than the system
loader (MyClass's loader).

So instead, VMRuntime.nativeLoad() must do this:

  - Invoke VMSecurityManager.getClassContext() to get the Class[] array
    of the current call stack.
  - Skip over any instances of java.lang.Runtime and java.lang.System
    at the beginning of the returned array to get to the "real" class
    that invoked ??.load() or ??.loadLibrary().
  - Use the class loader associated with that class as the one to
    associate with the native library.

Before I go and implement this, does this sound correct to everyone?

-Archie

__________________________________________________________________________
Archie Cobbs      *        CTO, Awarix        *      http://www.awarix.com




reply via email to

[Prev in Thread] Current Thread [Next in Thread]