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

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

Issue 1486573003: Mojo C++ bindings: associated interface pointers/requests validation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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 {%- set class_name = struct.name ~ "_Data" %} 1 {%- set class_name = struct.name ~ "_Data" %}
2 2
3 {#- TODO(yzshen): Consider eliminating _validate_object() and
4 _validate_handle(). #}
5
6 {#- Validates the specified struct field, which is supposed to be an object 3 {#- Validates the specified struct field, which is supposed to be an object
7 (struct/array/string/map/union). 4 (struct/array/string/map/union).
8 This macro is expanded by the Validate() method. #} 5 This macro is expanded by the Validate() method. #}
9 {%- macro _validate_object(struct, packed_field) %} 6 {%- macro _validate_object(struct, packed_field) %}
10 {%- set name = packed_field.field.name %} 7 {%- set name = packed_field.field.name %}
11 {%- set kind = packed_field.field.kind %} 8 {%- set kind = packed_field.field.kind %}
12 {%- set wrapper_type = kind|cpp_wrapper_type %}
13 {%- if not kind|is_nullable_kind %} 9 {%- if not kind|is_nullable_kind %}
14 {%- if kind|is_union_kind %} 10 {%- if kind|is_union_kind %}
15 if (object->{{name}}.is_null()) { 11 if (!mojo::internal::ValidateInlinedUnionNonNullable(
16 {%- else %} 12 object->{{name}}, "null {{name}} field in {{struct.name}} struct")) {
17 if (!object->{{name}}.offset) {
18 {%- endif %}
19 ReportValidationError(
20 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
21 "null {{name}} field in {{struct.name}} struct");
22 return false; 13 return false;
23 } 14 }
24 {%- endif %} 15 {%- else %}
25 {%- if not kind|is_union_kind %} 16 if (!mojo::internal::ValidatePointerNonNullable(
26 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) { 17 object->{{name}}, "null {{name}} field in {{struct.name}} struct")) {
27 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
28 return false; 18 return false;
29 } 19 }
20 {%- endif %}
30 {%- endif %} 21 {%- endif %}
31 {%- if kind|is_array_kind or kind|is_string_kind %} 22 {%- if kind|is_array_kind or kind|is_string_kind %}
32 const mojo::internal::ArrayValidateParams {{name}}_validate_params( 23 const mojo::internal::ArrayValidateParams {{name}}_validate_params(
33 {{kind|get_array_validate_params_ctor_args|indent(6)}}); 24 {{kind|get_array_validate_params_ctor_args|indent(6)}});
34 if (!{{wrapper_type}}::Data_::Validate( 25 if (!mojo::internal::ValidateArray(object->{{name}}, bounds_checker,
35 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 26 &{{name}}_validate_params)) {
36 bounds_checker, &{{name}}_validate_params)) { 27 return false;
28 }
37 {%- elif kind|is_map_kind %} 29 {%- elif kind|is_map_kind %}
38 const mojo::internal::ArrayValidateParams {{name}}_validate_params( 30 const mojo::internal::ArrayValidateParams {{name}}_validate_params(
39 {{kind.value_kind|get_map_validate_params_ctor_args|indent(6)}}); 31 {{kind.value_kind|get_map_validate_params_ctor_args|indent(6)}});
40 if (!{{wrapper_type}}::Data_::Validate( 32 if (!mojo::internal::ValidateMap(object->{{name}}, bounds_checker,
41 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), 33 &{{name}}_validate_params)) {
42 bounds_checker, &{{name}}_validate_params)) {
43 {%- elif kind|is_struct_kind %}
44 if (!{{kind|get_name_for_kind}}::Data_::Validate(
45 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
46 bounds_checker)) {
47 {%- elif kind|is_union_kind %}
48 if (!{{kind|get_name_for_kind}}::Data_::Validate(
49 &object->{{name}}, bounds_checker, true)) {
50 {%- else %}
51 if (!{{wrapper_type}}::Data_::Validate(
52 mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
53 bounds_checker)) {
54 {%- endif %}
55 return false; 34 return false;
56 } 35 }
36 {%- elif kind|is_struct_kind %}
37 if (!mojo::internal::ValidateStruct(object->{{name}}, bounds_checker))
38 return false;
39 {%- elif kind|is_union_kind %}
40 if (!mojo::internal::ValidateInlinedUnion(object->{{name}}, bounds_checker)) {
41 return false;
42 }
43 {%- else %}
44 #error Not reached!
45 {%- endif %}
57 {%- endmacro %} 46 {%- endmacro %}
58 47
59 {#- Validates the specified struct field, which is supposed to be a handle or 48 {#- Validates the specified struct field, which is supposed to be a handle or
60 contain a handle (in the case of interfaces). 49 contain a handle (in the case of interfaces).
61 This macro is expanded by the Validate() method. #} 50 This macro is expanded by the Validate() method. #}
62 {%- macro _validate_handle(struct, packed_field) %} 51 {%- macro _validate_handle(struct, packed_field) %}
63 {%- set name = packed_field.field.name %} 52 {%- set name = packed_field.field.name %}
64 {%- set kind = packed_field.field.kind %} 53 {%- set kind = packed_field.field.kind %}
65 {%- if kind|is_interface_kind %} 54 {%- if kind|is_interface_kind %}
66 const mojo::Handle {{name}}_handle = object->{{name}}.handle; 55 const mojo::Handle {{name}}_handle = object->{{name}}.handle;
67 {%- else %} 56 {%- else %}
68 const mojo::Handle {{name}}_handle = object->{{name}}; 57 const mojo::Handle {{name}}_handle = object->{{name}};
69 {%- endif %} 58 {%- endif %}
70 {%- if not kind|is_nullable_kind %} 59 {%- if not kind|is_nullable_kind %}
71 if ({{name}}_handle.value() == mojo::internal::kEncodedInvalidHandleValue) { 60 if (!mojo::internal::ValidateHandleNonNullable(
72 ReportValidationError( 61 {{name}}_handle,
73 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, 62 "invalid {{name}} field in {{struct.name}} struct")) {
74 "invalid {{name}} field in {{struct.name}} struct");
75 return false; 63 return false;
76 } 64 }
77 {%- endif %} 65 {%- endif %}
78 if (!bounds_checker->ClaimHandle({{name}}_handle)) { 66 if (!mojo::internal::ValidateHandle({{name}}_handle, bounds_checker))
79 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE);
80 return false; 67 return false;
81 }
82 {%- endmacro %} 68 {%- endmacro %}
83 69
84 {#- Validates the specified struct field, which is supposed to be an associated 70 {#- Validates the specified struct field, which is supposed to be an associated
85 interface or an associated interface request. 71 interface or an associated interface request.
86 This macro is expanded by the Validate() method. #} 72 This macro is expanded by the Validate() method. #}
87 {%- macro _validate_associated(struct, packed_field) %} 73 {%- macro _validate_associated(struct, packed_field) %}
88 // TODO(yzshen): add validation for associated kinds. 74 {%- set name = packed_field.field.name %}
75 {%- set kind = packed_field.field.kind %}
76 {%- if kind|is_associated_interface_kind %}
77 const mojo::internal::InterfaceId {{name}}_interface_id =
78 object->{{name}}.interface_id;
79 {%- else %}
80 const mojo::internal::InterfaceId {{name}}_interface_id = object->{{name}};
81 {%- endif %}
82 {%- if not kind|is_nullable_kind %}
83 if (!mojo::internal::ValidateInterfaceIdNonNullable(
84 {{name}}_interface_id,
85 "invalid {{name}} field in {{struct.name}} struct")) {
86 return false;
87 }
88 {%- endif %}
89 if (!mojo::internal::ValidateAssociatedInterfaceId({{name}}_interface_id))
90 return false;
89 {%- endmacro %} 91 {%- endmacro %}
90 92
91 // static 93 // static
92 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) { 94 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) {
93 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); 95 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}();
94 } 96 }
95 97
96 // static 98 // static
97 bool {{class_name}}::Validate(const void* data, 99 bool {{class_name}}::Validate(const void* data,
98 mojo::internal::BoundsChecker* bounds_checker) { 100 mojo::internal::BoundsChecker* bounds_checker) {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 mojo::internal::DecodeHandle(&{{name}}, handles); 204 mojo::internal::DecodeHandle(&{{name}}, handles);
203 {%- endif %} 205 {%- endif %}
204 {%- endif %} 206 {%- endif %}
205 {%- endfor %} 207 {%- endfor %}
206 } 208 }
207 209
208 {{class_name}}::{{class_name}}() { 210 {{class_name}}::{{class_name}}() {
209 header_.num_bytes = sizeof(*this); 211 header_.num_bytes = sizeof(*this);
210 header_.version = {{struct.versions[-1].version}}; 212 header_.version = {{struct.versions[-1].version}};
211 } 213 }
OLDNEW
« no previous file with comments | « mojo/public/interfaces/bindings/tests/validation_test_associated_interfaces.mojom ('k') | third_party/mojo/mojo_public.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698