use of com.oracle.truffle.espresso.impl.PackageTable.PackageEntry in project graal by oracle.
the class Klass method doModuleAccessChecks.
public static boolean doModuleAccessChecks(Klass klass, Klass accessingKlass, EspressoContext context) {
ModuleEntry moduleFrom = accessingKlass.module();
ModuleEntry moduleTo = klass.module();
if (moduleFrom == moduleTo) {
return true;
}
/*
* Acceptable access to a type in an unnamed module. Note that since unnamed modules can
* read all unnamed modules, this also handles the case where module_from is also unnamed
* but in a different class loader.
*/
if (!moduleTo.isNamed() && (moduleFrom.canReadAllUnnamed() || moduleFrom.canRead(moduleTo, context))) {
return true;
}
// Establish readability, check if moduleFrom is allowed to read moduleTo.
if (!moduleFrom.canRead(moduleTo, context)) {
return false;
}
// exported
if (moduleTo.isOpen()) {
return true;
}
PackageEntry packageTo = klass.packageEntry();
/*
* Once readability is established, if module_to exports T unqualifiedly, (to all modules),
* then whether module_from is in the unnamed module or not does not matter, access is
* allowed.
*/
if (packageTo.isUnqualifiedExported()) {
return true;
}
/*-
* Access is allowed if both 1 & 2 hold:
* 1. Readability, module_from can read module_to (established above).
* 2. Either module_to exports T to module_from qualifiedly.
* or
* module_to exports T to all unnamed modules and module_from is unnamed.
* or
* module_to exports T unqualifiedly to all modules (checked above).
*/
return packageTo.isQualifiedExportTo(moduleFrom);
}
use of com.oracle.truffle.espresso.impl.PackageTable.PackageEntry in project graal by oracle.
the class VM method defineModule.
@SuppressWarnings("try")
public void defineModule(StaticObject module, String moduleName, boolean is_open, String[] packages, SubstitutionProfiler profiler) {
Meta meta = getMeta();
StaticObject loader = meta.java_lang_Module_loader.getObject(module);
if (loader != nonReflectionClassLoader(loader)) {
profiler.profile(15);
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Class loader is an invalid delegating class loader");
}
// Prepare variables
ClassRegistry registry = getRegistries().getClassRegistry(loader);
assert registry != null;
PackageTable packageTable = registry.packages();
ModuleTable moduleTable = registry.modules();
assert moduleTable != null && packageTable != null;
boolean loaderIsBootOrPlatform = ClassRegistry.loaderIsBootOrPlatform(loader, meta);
ArrayList<Symbol<Name>> pkgSymbols = new ArrayList<>();
try (EntryTable.BlockLock block = packageTable.write()) {
for (String str : packages) {
// Extract the package symbols. Also checks for duplicates.
if (!loaderIsBootOrPlatform && (str.equals("java") || str.startsWith("java/"))) {
// Only modules defined to either the boot or platform class loader, can define
// a "java/" package.
profiler.profile(14);
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Class loader (", loader.getKlass().getType(), ") tried to define prohibited package name: ", str));
}
Symbol<Name> symbol = getNames().getOrCreate(str);
if (packageTable.lookup(symbol) != null) {
profiler.profile(13);
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Package ", str, " is already defined."));
}
pkgSymbols.add(symbol);
}
Symbol<Name> moduleSymbol = getNames().getOrCreate(moduleName);
// Try define module
ModuleEntry moduleEntry = moduleTable.createAndAddEntry(moduleSymbol, registry, is_open, module);
if (moduleEntry == null) {
// Module already defined
profiler.profile(12);
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, cat("Module ", moduleName, " is already defined"));
}
// Register packages
for (Symbol<Name> pkgSymbol : pkgSymbols) {
PackageEntry pkgEntry = packageTable.createAndAddEntry(pkgSymbol, moduleEntry);
// should have been checked before
assert pkgEntry != null;
}
// Link guest module to its host representation
meta.HIDDEN_MODULE_ENTRY.setHiddenObject(module, moduleEntry);
}
if (StaticObject.isNull(loader) && getContext().getVmProperties().bootClassPathType().isExplodedModule()) {
profiler.profile(11);
// If we have an exploded build, and the module is defined to the bootloader, prepend a
// class path entry for this module.
prependModuleClasspath(moduleName);
}
}
use of com.oracle.truffle.espresso.impl.PackageTable.PackageEntry in project graal by oracle.
the class JImageLibrary method findLocation.
private long findLocation(TruffleObject jimage, TruffleObject sizePtr, String name) {
try (RawBuffer nameBuffer = RawBuffer.getNativeString(name)) {
TruffleObject namePtr = nameBuffer.pointer();
long location = (long) execute(findResource, jimage, emptyStringBuffer.pointer(), versionBuffer.pointer(), namePtr, sizePtr);
if (location != 0) {
// found.
return location;
}
String pkg = packageFromName(name);
if (pkg == null) {
return 0;
}
if (!getContext().modulesInitialized()) {
location = (long) execute(findResource, jimage, javaBaseBuffer.pointer(), versionBuffer.pointer(), namePtr, sizePtr);
if (location != 0 || !getContext().metaInitialized()) {
// finding certain classes in java.base (/ex: sun/misc/Unsafe).
return location;
}
TruffleObject moduleName;
try (RawBuffer pkgBuffer = RawBuffer.getNativeString(pkg)) {
moduleName = (TruffleObject) execute(packageToModule, jimage, pkgBuffer.pointer());
}
if (uncached.isNull(moduleName)) {
return 0;
}
return (long) execute(findResource, jimage, moduleName, versionBuffer.pointer(), namePtr, sizePtr);
} else {
Symbol<Name> pkgSymbol = getNames().lookup(pkg);
if (pkgSymbol == null) {
return 0;
}
PackageEntry pkgEntry = getRegistries().getBootClassRegistry().packages().lookup(pkgSymbol);
if (pkgEntry == null) {
return 0;
}
Symbol<Name> moduleName = pkgEntry.module().getName();
if (moduleName == Name.java_base) {
return (long) execute(findResource, jimage, javaBaseBuffer.pointer(), versionBuffer.pointer(), namePtr, sizePtr);
} else {
String nameAsString = moduleName == null ? "" : moduleName.toString();
try (RawBuffer moduleNameBuffer = RawBuffer.getNativeString(nameAsString)) {
return (long) execute(findResource, jimage, moduleNameBuffer.pointer(), versionBuffer.pointer(), namePtr, sizePtr);
}
}
}
}
}
Aggregations