Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2227)

Unified Diff: third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl

Issue 954643002: Update mojo sdk to rev 3d23dae011859a2aae49f1d1adde705c8e85d819 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use run_renderer_in_process() Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl
diff --git a/third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl b/third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl
index 461f158602b72f3243d722da7f0b574a1c127f44..79a0bc4bb9cf77be61b02ca38bd2b6f98cdef642 100644
--- a/third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl
+++ b/third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl
@@ -1,6 +1,69 @@
-{%- import "struct_macros.tmpl" as struct_macros %}
{%- set class_name = struct.name ~ "_Data" %}
+{#- TODO(yzshen): Consider eliminating _validate_object() and
+ _validate_handle(). #}
+
+{#- Validates the specified struct field, which is supposed to be an object
+ (struct/array/string/map/union).
+ This macro is expanded by the Validate() method. #}
+{%- macro _validate_object(struct, packed_field) %}
+{%- set name = packed_field.field.name %}
+{%- set kind = packed_field.field.kind %}
+{%- set wrapper_type = kind|cpp_wrapper_type %}
+{%- if not kind|is_nullable_kind %}
+ if (!object->{{name}}.offset) {
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
+ "null {{name}} field in {{struct.name}} struct");
+ return false;
+ }
+{%- endif %}
+ if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) {
+ ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
+ return false;
+ }
+{%- if kind|is_array_kind or kind|is_string_kind %}
+ if (!{{wrapper_type}}::Data_::Validate<
+ {{kind|get_array_validate_params|indent(10)}}>(
+ mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
+ bounds_checker)) {
+{%- elif kind|is_map_kind %}
+ if (!{{wrapper_type}}::Data_::Validate<
+ {{kind.value_kind|get_map_validate_params|indent(10)}}>(
+ mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
+ bounds_checker)) {
+{%- elif kind|is_struct_kind %}
+ if (!{{kind|get_name_for_kind}}::Data_::Validate(
+ mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
+ bounds_checker)) {
+{%- else %}
+ if (!{{wrapper_type}}::Data_::Validate(
+ mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
+ bounds_checker)) {
+{%- endif %}
+ return false;
+ }
+{%- endmacro %}
+
+{#- Validates the specified struct field, which is supposed to be a handle.
+ This macro is expanded by the Validate() method. #}
+{%- macro _validate_handle(struct, packed_field) %}
+{%- set name = packed_field.field.name %}
+{%- set kind = packed_field.field.kind %}
+{%- if not kind|is_nullable_kind %}
+ if (object->{{name}}.value() == mojo::internal::kEncodedInvalidHandleValue) {
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE,
+ "invalid {{name}} field in {{struct.name}} struct");
+ return false;
+ }
+{%- endif %}
+ if (!bounds_checker->ClaimHandle(object->{{name}})) {
+ ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE);
+ return false;
+ }
+{%- endmacro %}
+
// static
{{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) {
return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}();
@@ -9,20 +72,109 @@
// static
bool {{class_name}}::Validate(const void* data,
mojo::internal::BoundsChecker* bounds_checker) {
- {{ struct_macros.validate(struct, class_name)|indent(2) }}
-}
+ if (!data)
+ return true;
-{{class_name}}::{{class_name}}() {
- header_.num_bytes = sizeof(*this);
- header_.num_fields = {{struct.packed.packed_fields|length}};
+ if (!ValidateStructHeaderAndClaimMemory(data, bounds_checker))
+ return false;
+
+ // NOTE: The memory backing |object| may be smaller than |sizeof(*object)| if
+ // the message comes from an older version.
+ const {{class_name}}* object = static_cast<const {{class_name}}*>(data);
+
+ static const struct {
+ uint32_t version;
+ uint32_t num_bytes;
+ } kVersionSizes[] = {
+{%- for version in struct.versions -%}
+ { {{version.version}}, {{version.num_bytes}} }{% if not loop.last %}, {% endif -%}
+{%- endfor -%}
+ };
+
+ if (object->header_.version <=
+ kVersionSizes[MOJO_ARRAYSIZE(kVersionSizes) - 1].version) {
+ // Scan in reverse order to optimize for more recent versions.
+ for (int i = MOJO_ARRAYSIZE(kVersionSizes) - 1; i >= 0; --i) {
+ if (object->header_.version >= kVersionSizes[i].version) {
+ if (object->header_.num_bytes == kVersionSizes[i].num_bytes)
+ break;
+
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
+ return false;
+ }
+ }
+ } else if (object->header_.num_bytes <
+ kVersionSizes[MOJO_ARRAYSIZE(kVersionSizes) - 1].num_bytes) {
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
+ return false;
+ }
+
+{#- Before validating fields introduced at a certain version, we need to add
+ a version check, which makes sure we skip further validation if |object|
+ is from an earlier version. |last_checked_version| records the last
+ version that we have added such version check. #}
+{%- set last_checked_version = 0 %}
+{%- for packed_field in struct.packed.packed_fields_in_ordinal_order %}
+{%- set kind = packed_field.field.kind %}
+{%- if kind|is_object_kind or kind|is_any_handle_kind %}
+{%- if packed_field.min_version > last_checked_version %}
+{%- set last_checked_version = packed_field.min_version %}
+ if (object->header_.version < {{packed_field.min_version}})
+ return true;
+{%- endif %}
+{%- if kind|is_object_kind %}
+{{_validate_object(struct, packed_field)}}
+{%- elif kind|is_any_handle_kind %}
+{{_validate_handle(struct, packed_field)}}
+{%- endif %}
+{%- endif %}
+{%- endfor %}
+
+ return true;
}
void {{class_name}}::EncodePointersAndHandles(
std::vector<mojo::Handle>* handles) {
- {{ struct_macros.encodes(struct)|indent(2) }}
+ MOJO_CHECK(header_.version == {{struct.versions[-1].version}});
+{%- for pf in struct.packed.packed_fields_in_ordinal_order %}
+{%- if pf.field.kind|is_object_kind %}
+ mojo::internal::Encode(&{{pf.field.name}}, handles);
+{%- elif pf.field.kind|is_any_handle_kind %}
+ mojo::internal::EncodeHandle(&{{pf.field.name}}, handles);
+{%- endif %}
+{%- endfor %}
}
void {{class_name}}::DecodePointersAndHandles(
std::vector<mojo::Handle>* handles) {
- {{ struct_macros.decodes(struct)|indent(2) }}
+ // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if
+ // the message comes from an older version.
+{#- Before decoding fields introduced at a certain version, we need to add
+ a version check, which makes sure we skip further decoding if |this|
+ is from an earlier version. |last_checked_version| records the last
+ version that we have added such version check. #}
+{%- set last_checked_version = 0 %}
+{%- for pf in struct.packed.packed_fields_in_ordinal_order %}
+{%- set name = pf.field.name %}
+{%- set kind = pf.field.kind %}
+{%- if kind|is_object_kind or kind|is_any_handle_kind %}
+{%- if pf.min_version > last_checked_version %}
+{%- set last_checked_version = pf.min_version %}
+ if (header_.version < {{pf.min_version}})
+ return;
+{%- endif %}
+{%- if kind|is_object_kind %}
+ mojo::internal::Decode(&{{name}}, handles);
+{%- else %}
+ mojo::internal::DecodeHandle(&{{name}}, handles);
+{%- endif %}
+{%- endif %}
+{%- endfor %}
+}
+
+{{class_name}}::{{class_name}}() {
+ header_.num_bytes = sizeof(*this);
+ header_.version = {{struct.versions[-1].version}};
}

Powered by Google App Engine
This is Rietveld 408576698