The Class Loader architecture (Architecture Analysis of the Class Loader)

Source: Internet
Author: User

This articleArticleIs from 《Inside Java Virtual Machine (Deep into Java Virtual Machine) This book excerpt, in the face of this chapter mainly talks about the Java class loader architecture, speak quite well.

In Java's sandbox, the Class Loader architecture is the first line of defense. it is the class loader, after all, that brings code into the Java Virtual Machine -- code that cocould be hostile or buggy. the Class Loader architecture contributes to Java's sandbox in three ways:

    1. It prevents malicious code from interfering with benevolent code,
    2. It guards the borders of the trusted class libraries, and
    3. It places code into categories (calledProtection Domains) That will determine which actions the code will be allowed to take.

The Class Loader architecture prevents malicious code from interfering with benevolent code by providing separate name-spaces for Classes loaded by different class loaders. Name-space Is a set of unique names -- one name for each loaded class -- that the Java Virtual Machine maintains for each class loader. Once a Java Virtual Machine has loaded a class named Volcano Into a participant name-space, for example, it is impossible to load a different class named Volcano Into that same name-space. You can load multiple Volcano Classes into a Java virtual machine, however, because you can create multiple name-spaces inside a Java application by creating multiple class loaders. if you create three separate name-spaces (one for each of three class loaders) in a running Java application, then, by loading one Volcano Class into each name-space, your program cocould load three different Volcano Classes into your application.

Name-spaces contribute to security because you can in effect place a shield between classes loaded into different name-spaces. inside the Java Virtual Machine, classes in the same name-space can interact with one another directly. classes in different name-spaces, however, can't even detect each other's presence unless you explicitly provide a mechanic that allows them to interact. if a malicious class, Once loaded, had guaranteed access to every other class currently loaded by the virtual machine, that class cocould potentially learn things it shouldn't know or interfere with the proper execution of your program.

Figure 3-1 shows the name-spaces associated with two class loaders, both of which have loaded a type named Volcano . Each name in a name space is associated with the type data in the method area that defines the type with that name. figure 3-1 shows arrows from the names in the name-spaces to the types in the method area that define the type. the class loader on the left, which is shown dark gray, has loaded the two dark gray types named Climber And Volcano . They class loader on the right, which is shown light gray, has loaded the two light gray types named Bakingsoda And Volcano . Because of the nature of name-spaces, when Climber Class mentions Volcano Class, it refers to the dark gray Volcano , Volcano Loaded in the Same Name Space. It has no way to know that the other Volcano , Which is sitting in the same virtual machine, even exists. For the details on how the Class Loader architecture achieves its separation of namespaces, see Chapter 8, "the linking model ."

 

The Class Loader architecture guards the borders of the trusted class libraries by making it possible for trusted packages to be loaded with different class loaders than untrusted packages. although you can grant special access privileges between types belonging to the same package by giving members protected or package access, this special access is granted to members of the same package at runtime only if they were loaded by the same class loader.

Often, a user-defined class loader relies on other class loaders -- at the very least, upon the class loaders created at Virtual Machine startup -- to help it fulfill some of the class load requests that come its way. prior to 1.2, class loaders had to explicitly ask for the help of other class loaders. A Class Loader cocould ask another user-defined class loader to load a class by invoking Loadclass () On a reference to that user-defined class loader. Or, a Class Loader cocould ask the bootstrap class loader to attempt to load a class by invoking Findsystemclass () , A static method defined in class Classloader . In version 1.2, the process by which one class loader asks another class loader to try and load a type was formalized into a parent-delegation model. starting with 1.2, each class loader doesn't the bootstrap class loader has a "parent" class loader. before a participating Class Loader attempts to load a type in its custom way, it by default "delegates" the job to its parent -- it asks its parent to try and load the type. the parent, in turn, asks its parent to try and load the type. the delegation process continues all the way up to the bootstrap class loader, which is in general the last class loader in the delegation chain. if a class loader's parent class loader is able to load a type, the Class Loader returns that type. else, the Class Loader attempts to load the type itself.

in most Java Virtual Machine implementations prior to 1.2, the built-in Class Loader (which was then called the primordial Class Loader) was responsible for loading locally available class files. such class files usually encoded the class files that made up the Java application being executed plus any libraries needed by the application, including the class files of the Java API. although the manner in which the class files for requested types were located was implementation specific, many implementations searched directories and jar files in an order specified by a Class path.

in 1.2, the job of loading locally available class files was parceled out to multiple class loaders. the built-in class loader, previusly called the primordial class loader, was renamed the "Bootstrap" class loader to indicate that it was now responsible for loading only the class files of the core Java API. the name Bootstrap Class Loader comes from the idea that the class files of the core Java API are the class files required to "Bootstrap" the Java Virtual Machine.

Responsibility for loading other class files, such as the class files for the application being executed, class files for installed or downloaded standard extensions, class files for libraries discovered in the class path, and so on, was given in 1.2 to user-defined class loaders. when a 1.2 Java virtual machine starts execution, therefore, it creates at least one and probably more user-defined class loaders before the application even starts. all of these class loaders are connected in one chain of parent-child relationships. at the top of the chain is the bootstrap class loader. at the bottom of the chain is what cname in 1.2 to be called the "system class loader. "prior to 1.2, the name" system class loader "was sometimes used to refer to the built-in class loader, which was also called the primordial class loader. in 1.2, the name system class loader was more formally defined to mean the default delegation parent for new user-defined class loaders created by a Java application. this default delegation parent is usually going to be the user-defined class loader that loaded the initial class of the application, but may be any user-defined class loader decided upon by the designers of the Java platform implementation.

For example, imagine you write a Java application that installa Class Loader whose particle manner of loading class files is by downloading them implements ss a network. imagine you run this application on a virtual machine that instantiates two user-defined class loaders on startup: An "installed extensions" class loader and a "class path" class loader. these class loaders are connected in a parent-child relationship chain along with the bootstrap class loader as shown in Figure 3-2. the class path's class loader's parent is the installed extensions class loader, whose parent is the bootstrap class loader. as shown in Figure 3-2, the class path class loader is designated as the system class loader, the default delegation parent for new user-defined class loaders instantiated by the application. assume that when you application instantiates its Network Class Loader, it specifies the system class loader as its parent.

 

Imagine that during the course of running the Java application, a request is made of your class loader to load a class named Volcano . Your class loader wocould first ask its parent, the class path class loader, to find and load the class. the class path class loader, in turn, wocould make the same request of its parent, the installed extensions class loader. this class loader, wocould also first delegate the request to its parent, the bootstrap class loader. assuming that class Volcano Is not a part of the Java API, an installed extension, or on the class path, all of these class loaders wocould return without supplying a loaded class named Volcano . When the class path Class Loader responds that neither it nor any of its parents can load the class, your class loader cocould then attempt to load Volcano Class in its custom manner, by downloading it implements ss the network. Assuming your class loader was able to download class Volcano , That Volcano Class cocould then play a role in the application's future course of execution.

To continue with the same example, assume that at some time later a method of class Volcano Is invoked for the first time, and that method references class Java. util. hashmap From the Java API. because it is the first time the reference was used by the running program, the virtual machine asks your class loader (the one that loaded Volcano ) To load Java. util. hashmap . As before, your class loader first passes the request to its parent class loader, And the request gets delegated all the way up to the bootstrap class loader. but in this case, the bootstrap class loader is able to return Java. util. hashmap Class back to your class loader. since the bootstrap class loader was able to find the class, the installed extensions Class Loader doesn' t attempt to look for the type in the installed extensions. the class path Class Loader doesn' t attempt to look for the type on the class path. and your class loader doesn' t attempt to download it into ss the network. all of these class loaders merely return Java. util. hashmap Class returned by the bootstrap Class Loader. From that point forward, the virtual machine uses that Java. util. hashmap Class whenever class Volcano References a class named Java. util. hashmap .

Given this background into how class loaders work, you are ready to look at how class loaders can be used to protect trusted libraries. the Class Loader architecture guards the borders of the trusted class libraries by preventing untrusted classes from pretending to be trusted. if a malicious class cocould successfully trick the Java Virtual Machine into believing it was a trusted class from the Java API, that malicious class cocould potentially break through the sandbox barrier. by preventing untrusted classes from impersonating trusted classes, the Class Loader architecture blocks one potential approach to compromising the security of the Java runtime.

Given the parent-delegation model, the bootstrap class loader is able to attempt to load types before the standard extensions class loader, which is able to attempt to load types before the class path class loader, which is able to attempt to load types before your network class loader. thus, given the manner in which the parent-child delegation chain is built, the most trusted library, the core Java API, is checked first for each type. after that, the standard extensions are checked. after that, local class files that are sitting on the class path are checked. so if some mobile code loaded by your Network Class Loader wants to download a type pair ss the network with the same name as something in the Java API, such Java. Lang. Integer , It won't be able to do it. If a class file Java. Lang. Integer Exists in the Java API, it will be loaded by the bootstrap Class Loader. The Network Class Loader will not attempt to download and define a class named Java. Lang. Integer . It will just use the type returned by its parent, the one loaded by the bootstrap class loader. in this way, the Class Loader architecture prevents Untrusted code from replacing trusted classes with their own versions.

But what if the Mobile Code, rather than trying to replace a trusted type, wants to insert a brand new type into a trusted package? Imagine what wowould happen if your Network Class Loader from the previous example was requested to load a class namedJava. Lang. Virus. As before, this request wocould first be delegated all the way up the Parent-Child chain to the bootstrap class loader. although the bootstrap class loader is responsible for loading the class files of the core Java API, which has des a package named,Java. Lang, It is unable to find a member ofJava. LangPackage with the name,Virus. Assuming this class was also not found among the installed extensions or on the local class path, your class loader wowould proceed to attempt to download the type setting ss the network.

Assume your class loader is successful in the Download Attempt and defines the type named Java. Lang. Virus . Java allows classes in the same package to grant each other special access privileges that aren't granted to classes outside the package. so, since your class loader has loaded a class that by its name ( Java. Lang. Virus ) Brazenly declares itself to be part of the Java API, you might should CT it cocould gain special access to the trusted classes Java. Lang And cocould possibly use that special access for devious purposes. The class loader mechanic thwarts this code from gaining special access to the trusted types in Java. Lang Package, because the Java Virtual Machine only grants that special package access between types loaded into the same package by the same class loader. Since the trusted class files of the Java API's Java. Lang Package were loaded by the bootstrap class loader, and the maliciousJava. Lang. Virus Class was loaded by your network class loader, these types do not belong to the same Runtime package . The term runtime package, which first appeared in the second edition of the Java Virtual Machine specification, refers to a set of types that belong to the same package and that were all loaded by the same class loader. before allowing access to package-visible members (members declared with protected or package access) between two types, the virtual machine makes sure not only that the two types belong to the same package, but that they belong to the same runtime package -- that they were loaded by the same class loader. thus, because Java. Lang. Virus And the members Java. Lang From the core Java API don't belong to the same runtime package, Java. Lang. Virus Can't access the package-visible members and types of the Java API's Java. Lang Package.

This concept of a runtime package is one motivation for using different class loaders to load different kinds of classes. the bootstrap Class Loader loads the class files of the core Java API. these class files are the most trusted. an installed extensions Class Loader loads class files from any installed extensions. installed extensions are quite trusted, but need not be trusted to the extent that they are allowed to gain access to package-visible members of the Java API by simply inserting new types into those packages. because installed extensions are loaded with a different class loader than the core API, they can't. likewise, code found on the class path by the class path class loader can't gain access to package-visible members of the installed extensions or the Java API.

Another way class loaders can be used to protect the borders of trusted class libraries is by simply disallowing the loading of certain forbidden types. for example, you may have installed some packages that contain classes you want your application to be able to load through your network class loader's parent, the class path class loader, but not through your own network class loader. assume you have created a package named Absolutepower And installed it somewhere on the local class path, where it is accessible by the class path class loader. assume also that you don't want classes loaded by your class loader to be able to load any class from Absolutepower Package. In this case, you wocould write your class loader such that the very first thing it does is make sure the requested class doesn't declare itself as a member of Absolutepower Package. If such a class is requested, your class loader, rather than passing the class name to its parent class loader, wocould throw a security exception.

The only way a class loader can know whether or not a class is from a forbidden package, suchAbsolutepower, Is by the class's name. Thus a Class Loader must have a list of the names of forbidden packages. Because the name of classAbsolutepower. fancyclassloaderIndicates it is part ofAbsolutepowerPackage, andAbsolutepowerPackage is on the list of forbidden packages, your class loader shocould throw a security exception absolutely.

Besides shielding classes in different namespaces and protecting the borders of trusted class libraries, class loaders play one other security role. class loaders must place each loaded class into a protection domain, which defines what permissions the code is going to be given as it runs. more information about this vitally important security job of class loaders will be given later in this chapter.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.