cannam@147: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@147: // Licensed under the MIT License: cannam@147: // cannam@147: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@147: // of this software and associated documentation files (the "Software"), to deal cannam@147: // in the Software without restriction, including without limitation the rights cannam@147: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@147: // copies of the Software, and to permit persons to whom the Software is cannam@147: // furnished to do so, subject to the following conditions: cannam@147: // cannam@147: // The above copyright notice and this permission notice shall be included in cannam@147: // all copies or substantial portions of the Software. cannam@147: // cannam@147: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@147: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@147: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@147: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@147: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@147: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@147: // THE SOFTWARE. cannam@147: cannam@147: #ifndef CAPNP_SCHEMA_LOADER_H_ cannam@147: #define CAPNP_SCHEMA_LOADER_H_ cannam@147: cannam@147: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) cannam@147: #pragma GCC system_header cannam@147: #endif cannam@147: cannam@147: #include "schema.h" cannam@147: #include cannam@147: #include cannam@147: cannam@147: namespace capnp { cannam@147: cannam@147: class SchemaLoader { cannam@147: // Class which can be used to construct Schema objects from schema::Nodes as defined in cannam@147: // schema.capnp. cannam@147: // cannam@147: // It is a bad idea to use this class on untrusted input with exceptions disabled -- you may cannam@147: // be exposing yourself to denial-of-service attacks, as attackers can easily construct schemas cannam@147: // that are subtly inconsistent in a way that causes exceptions to be thrown either by cannam@147: // SchemaLoader or by the dynamic API when the schemas are subsequently used. If you enable and cannam@147: // properly catch exceptions, you should be OK -- assuming no bugs in the Cap'n Proto cannam@147: // implementation, of course. cannam@147: cannam@147: public: cannam@147: class LazyLoadCallback { cannam@147: public: cannam@147: virtual void load(const SchemaLoader& loader, uint64_t id) const = 0; cannam@147: // Request that the schema node with the given ID be loaded into the given SchemaLoader. If cannam@147: // the callback is able to find a schema for this ID, it should invoke `loadOnce()` on cannam@147: // `loader` to load it. If no such node exists, it should simply do nothing and return. cannam@147: // cannam@147: // The callback is allowed to load schema nodes other than the one requested, e.g. because it cannam@147: // expects they will be needed soon. cannam@147: // cannam@147: // If the `SchemaLoader` is used from multiple threads, the callback must be thread-safe. cannam@147: // In particular, it's possible for multiple threads to invoke `load()` with the same ID. cannam@147: // If the callback performs a large amount of work to look up IDs, it should be sure to cannam@147: // de-dup these requests. cannam@147: }; cannam@147: cannam@147: SchemaLoader(); cannam@147: cannam@147: SchemaLoader(const LazyLoadCallback& callback); cannam@147: // Construct a SchemaLoader which will invoke the given callback when a schema node is requested cannam@147: // that isn't already loaded. cannam@147: cannam@147: ~SchemaLoader() noexcept(false); cannam@147: KJ_DISALLOW_COPY(SchemaLoader); cannam@147: cannam@147: Schema get(uint64_t id, schema::Brand::Reader brand = schema::Brand::Reader(), cannam@147: Schema scope = Schema()) const; cannam@147: // Gets the schema for the given ID, throwing an exception if it isn't present. cannam@147: // cannam@147: // The returned schema may be invalidated if load() is called with a new schema for the same ID. cannam@147: // In general, you should not call load() while a schema from this loader is in-use. cannam@147: // cannam@147: // `brand` and `scope` are used to determine brand bindings where relevant. `brand` gives cannam@147: // parameter bindings for the target type's brand parameters that were specified at the reference cannam@147: // site. `scope` specifies the scope in which the type ID appeared -- if `brand` itself contains cannam@147: // parameter references or indicates that some parameters will be inherited, these will be cannam@147: // interpreted within / inherited from `scope`. cannam@147: cannam@147: kj::Maybe tryGet(uint64_t id, schema::Brand::Reader bindings = schema::Brand::Reader(), cannam@147: Schema scope = Schema()) const; cannam@147: // Like get() but doesn't throw. cannam@147: cannam@147: Schema getUnbound(uint64_t id) const; cannam@147: // Gets a special version of the schema in which all brand parameters are "unbound". This means cannam@147: // that if you look up a type via the Schema API, and it resolves to a brand parameter, the cannam@147: // returned Type's getBrandParameter() method will return info about that parameter. Otherwise, cannam@147: // normally, all brand parameters that aren't otherwise bound are assumed to simply be cannam@147: // "AnyPointer". cannam@147: cannam@147: Type getType(schema::Type::Reader type, Schema scope = Schema()) const; cannam@147: // Convenience method which interprets a schema::Type to produce a Type object. Implemented in cannam@147: // terms of get(). cannam@147: cannam@147: Schema load(const schema::Node::Reader& reader); cannam@147: // Loads the given schema node. Validates the node and throws an exception if invalid. This cannam@147: // makes a copy of the schema, so the object passed in can be destroyed after this returns. cannam@147: // cannam@147: // If the node has any dependencies which are not already loaded, they will be initialized as cannam@147: // stubs -- empty schemas of whichever kind is expected. cannam@147: // cannam@147: // If another schema for the given reader has already been seen, the loader will inspect both cannam@147: // schemas to determine which one is newer, and use that that one. If the two versions are cannam@147: // found to be incompatible, an exception is thrown. If the two versions differ but are cannam@147: // compatible and the loader cannot determine which is newer (e.g., the only changes are renames), cannam@147: // the existing schema will be preferred. Note that in any case, the loader will end up keeping cannam@147: // around copies of both schemas, so you shouldn't repeatedly reload schemas into the same loader. cannam@147: // cannam@147: // The following properties of the schema node are validated: cannam@147: // - Struct size and preferred list encoding are valid and consistent. cannam@147: // - Struct members are fields or unions. cannam@147: // - Union members are fields. cannam@147: // - Field offsets are in-bounds. cannam@147: // - Ordinals and codeOrders are sequential starting from zero. cannam@147: // - Values are of the right union case to match their types. cannam@147: // cannam@147: // You should assume anything not listed above is NOT validated. In particular, things that are cannam@147: // not validated now, but could be in the future, include but are not limited to: cannam@147: // - Names. cannam@147: // - Annotation values. (This is hard because the annotation declaration is not always cannam@147: // available.) cannam@147: // - Content of default/constant values of pointer type. (Validating these would require knowing cannam@147: // their schema, but even if the schemas are available at validation time, they could be cannam@147: // updated by a subsequent load(), invalidating existing values. Instead, these values are cannam@147: // validated at the time they are used, as usual for Cap'n Proto objects.) cannam@147: // cannam@147: // Also note that unknown types are not considered invalid. Instead, the dynamic API returns cannam@147: // a DynamicValue with type UNKNOWN for these. cannam@147: cannam@147: Schema loadOnce(const schema::Node::Reader& reader) const; cannam@147: // Like `load()` but does nothing if a schema with the same ID is already loaded. In contrast, cannam@147: // `load()` would attempt to compare the schemas and take the newer one. `loadOnce()` is safe cannam@147: // to call even while concurrently using schemas from this loader. It should be considered an cannam@147: // error to call `loadOnce()` with two non-identical schemas that share the same ID, although cannam@147: // this error may or may not actually be detected by the implementation. cannam@147: cannam@147: template cannam@147: void loadCompiledTypeAndDependencies(); cannam@147: // Load the schema for the given compiled-in type and all of its dependencies. cannam@147: // cannam@147: // If you want to be able to cast a DynamicValue built from this SchemaLoader to the compiled-in cannam@147: // type using as(), you must call this method before constructing the DynamicValue. Otherwise, cannam@147: // as() will throw an exception complaining about type mismatch. cannam@147: cannam@147: kj::Array getAllLoaded() const; cannam@147: // Get a complete list of all loaded schema nodes. It is particularly useful to call this after cannam@147: // loadCompiledTypeAndDependencies() in order to get a flat list of all of T's transitive cannam@147: // dependencies. cannam@147: cannam@147: private: cannam@147: class Validator; cannam@147: class CompatibilityChecker; cannam@147: class Impl; cannam@147: class InitializerImpl; cannam@147: class BrandedInitializerImpl; cannam@147: kj::MutexGuarded> impl; cannam@147: cannam@147: void loadNative(const _::RawSchema* nativeSchema); cannam@147: }; cannam@147: cannam@147: template cannam@147: inline void SchemaLoader::loadCompiledTypeAndDependencies() { cannam@147: loadNative(&_::rawSchema()); cannam@147: } cannam@147: cannam@147: } // namespace capnp cannam@147: cannam@147: #endif // CAPNP_SCHEMA_LOADER_H_