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