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

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl

Issue 2064903002: Mojo: Report bindings validation errors via MojoNotifyBadMessage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 6 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 unified diff | Download patch
OLDNEW
1 {%- import "validation_macros.tmpl" as validation_macros %} 1 {%- import "validation_macros.tmpl" as validation_macros %}
2 {%- set class_name = struct.name ~ "_Data" %} 2 {%- set class_name = struct.name ~ "_Data" %}
3 3
4 // static 4 // static
5 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) { 5 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) {
6 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); 6 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}();
7 } 7 }
8 8
9 // static 9 // static
10 bool {{class_name}}::Validate(const void* data, 10 bool {{class_name}}::Validate(
11 mojo::internal::BoundsChecker* bounds_checker) { 11 const void* data,
12 mojo::internal::ValidationContext* validation_context) {
12 if (!data) 13 if (!data)
13 return true; 14 return true;
14 15
15 if (!ValidateStructHeaderAndClaimMemory(data, bounds_checker)) 16 if (!ValidateStructHeaderAndClaimMemory(data, validation_context))
16 return false; 17 return false;
17 18
18 // NOTE: The memory backing |object| may be smaller than |sizeof(*object)| if 19 // NOTE: The memory backing |object| may be smaller than |sizeof(*object)| if
19 // the message comes from an older version. 20 // the message comes from an older version.
20 const {{class_name}}* object = static_cast<const {{class_name}}*>(data); 21 const {{class_name}}* object = static_cast<const {{class_name}}*>(data);
21 22
22 static const struct { 23 static const struct {
23 uint32_t version; 24 uint32_t version;
24 uint32_t num_bytes; 25 uint32_t num_bytes;
25 } kVersionSizes[] = { 26 } kVersionSizes[] = {
26 {%- for version in struct.versions -%} 27 {%- for version in struct.versions -%}
27 { {{version.version}}, {{version.num_bytes}} }{% if not loop.last %}, {% end if -%} 28 { {{version.version}}, {{version.num_bytes}} }{% if not loop.last %}, {% end if -%}
28 {%- endfor -%} 29 {%- endfor -%}
29 }; 30 };
30 31
31 if (object->header_.version <= 32 if (object->header_.version <=
32 kVersionSizes[arraysize(kVersionSizes) - 1].version) { 33 kVersionSizes[arraysize(kVersionSizes) - 1].version) {
33 // Scan in reverse order to optimize for more recent versions. 34 // Scan in reverse order to optimize for more recent versions.
34 for (int i = arraysize(kVersionSizes) - 1; i >= 0; --i) { 35 for (int i = arraysize(kVersionSizes) - 1; i >= 0; --i) {
35 if (object->header_.version >= kVersionSizes[i].version) { 36 if (object->header_.version >= kVersionSizes[i].version) {
36 if (object->header_.num_bytes == kVersionSizes[i].num_bytes) 37 if (object->header_.num_bytes == kVersionSizes[i].num_bytes)
37 break; 38 break;
38 39
39 ReportValidationError( 40 ReportValidationError(
41 validation_context,
40 mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER); 42 mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
41 return false; 43 return false;
42 } 44 }
43 } 45 }
44 } else if (object->header_.num_bytes < 46 } else if (object->header_.num_bytes <
45 kVersionSizes[arraysize(kVersionSizes) - 1].num_bytes) { 47 kVersionSizes[arraysize(kVersionSizes) - 1].num_bytes) {
46 ReportValidationError( 48 ReportValidationError(
49 validation_context,
47 mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER); 50 mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
48 return false; 51 return false;
49 } 52 }
50 53
51 {#- Before validating fields introduced at a certain version, we need to add 54 {#- Before validating fields introduced at a certain version, we need to add
52 a version check, which makes sure we skip further validation if |object| 55 a version check, which makes sure we skip further validation if |object|
53 is from an earlier version. |last_checked_version| records the last 56 is from an earlier version. |last_checked_version| records the last
54 version that we have added such version check. #} 57 version that we have added such version check. #}
55 {%- set last_checked_version = 0 %} 58 {%- set last_checked_version = 0 %}
56 {%- for packed_field in struct.packed.packed_fields_in_ordinal_order %} 59 {%- for packed_field in struct.packed.packed_fields_in_ordinal_order %}
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 mojo::internal::Decode(&this->{{name}}); 108 mojo::internal::Decode(&this->{{name}});
106 {%- endif %} 109 {%- endif %}
107 {%- endif %} 110 {%- endif %}
108 {%- endfor %} 111 {%- endfor %}
109 } 112 }
110 113
111 {{class_name}}::{{class_name}}() { 114 {{class_name}}::{{class_name}}() {
112 header_.num_bytes = sizeof(*this); 115 header_.num_bytes = sizeof(*this);
113 header_.version = {{struct.versions[-1].version}}; 116 header_.version = {{struct.versions[-1].version}};
114 } 117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698