1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "upb/reflection/def_builder_internal.h"
29 #include "upb/reflection/def_pool.h"
30 #include "upb/reflection/enum_def_internal.h"
31 #include "upb/reflection/field_def_internal.h"
32 #include "upb/reflection/file_def_internal.h"
33 #include "upb/reflection/message_def_internal.h"
34 #include "upb/reflection/service_def_internal.h"
35 
36 // Must be last.
37 #include "upb/port/def.inc"
38 
39 struct upb_FileDef {
40   const UPB_DESC(FileOptions) * opts;
41   const char* name;
42   const char* package;
43   const char* edition;
44 
45   const upb_FileDef** deps;
46   const int32_t* public_deps;
47   const int32_t* weak_deps;
48   const upb_MessageDef* top_lvl_msgs;
49   const upb_EnumDef* top_lvl_enums;
50   const upb_FieldDef* top_lvl_exts;
51   const upb_ServiceDef* services;
52   const upb_MiniTableExtension** ext_layouts;
53   const upb_DefPool* symtab;
54 
55   int dep_count;
56   int public_dep_count;
57   int weak_dep_count;
58   int top_lvl_msg_count;
59   int top_lvl_enum_count;
60   int top_lvl_ext_count;
61   int service_count;
62   int ext_count;  // All exts in the file.
63   upb_Syntax syntax;
64 };
65 
UPB_DESC(FileOptions)66 const UPB_DESC(FileOptions) * upb_FileDef_Options(const upb_FileDef* f) {
67   return f->opts;
68 }
69 
upb_FileDef_HasOptions(const upb_FileDef * f)70 bool upb_FileDef_HasOptions(const upb_FileDef* f) {
71   return f->opts != (void*)kUpbDefOptDefault;
72 }
73 
upb_FileDef_Name(const upb_FileDef * f)74 const char* upb_FileDef_Name(const upb_FileDef* f) { return f->name; }
75 
upb_FileDef_Package(const upb_FileDef * f)76 const char* upb_FileDef_Package(const upb_FileDef* f) {
77   return f->package ? f->package : "";
78 }
79 
upb_FileDef_Edition(const upb_FileDef * f)80 const char* upb_FileDef_Edition(const upb_FileDef* f) {
81   return f->edition ? f->edition : "";
82 }
83 
_upb_FileDef_RawPackage(const upb_FileDef * f)84 const char* _upb_FileDef_RawPackage(const upb_FileDef* f) { return f->package; }
85 
upb_FileDef_Syntax(const upb_FileDef * f)86 upb_Syntax upb_FileDef_Syntax(const upb_FileDef* f) { return f->syntax; }
87 
upb_FileDef_TopLevelMessageCount(const upb_FileDef * f)88 int upb_FileDef_TopLevelMessageCount(const upb_FileDef* f) {
89   return f->top_lvl_msg_count;
90 }
91 
upb_FileDef_DependencyCount(const upb_FileDef * f)92 int upb_FileDef_DependencyCount(const upb_FileDef* f) { return f->dep_count; }
93 
upb_FileDef_PublicDependencyCount(const upb_FileDef * f)94 int upb_FileDef_PublicDependencyCount(const upb_FileDef* f) {
95   return f->public_dep_count;
96 }
97 
upb_FileDef_WeakDependencyCount(const upb_FileDef * f)98 int upb_FileDef_WeakDependencyCount(const upb_FileDef* f) {
99   return f->weak_dep_count;
100 }
101 
_upb_FileDef_PublicDependencyIndexes(const upb_FileDef * f)102 const int32_t* _upb_FileDef_PublicDependencyIndexes(const upb_FileDef* f) {
103   return f->public_deps;
104 }
105 
_upb_FileDef_WeakDependencyIndexes(const upb_FileDef * f)106 const int32_t* _upb_FileDef_WeakDependencyIndexes(const upb_FileDef* f) {
107   return f->weak_deps;
108 }
109 
upb_FileDef_TopLevelEnumCount(const upb_FileDef * f)110 int upb_FileDef_TopLevelEnumCount(const upb_FileDef* f) {
111   return f->top_lvl_enum_count;
112 }
113 
upb_FileDef_TopLevelExtensionCount(const upb_FileDef * f)114 int upb_FileDef_TopLevelExtensionCount(const upb_FileDef* f) {
115   return f->top_lvl_ext_count;
116 }
117 
upb_FileDef_ServiceCount(const upb_FileDef * f)118 int upb_FileDef_ServiceCount(const upb_FileDef* f) { return f->service_count; }
119 
upb_FileDef_Dependency(const upb_FileDef * f,int i)120 const upb_FileDef* upb_FileDef_Dependency(const upb_FileDef* f, int i) {
121   UPB_ASSERT(0 <= i && i < f->dep_count);
122   return f->deps[i];
123 }
124 
upb_FileDef_PublicDependency(const upb_FileDef * f,int i)125 const upb_FileDef* upb_FileDef_PublicDependency(const upb_FileDef* f, int i) {
126   UPB_ASSERT(0 <= i && i < f->public_dep_count);
127   return f->deps[f->public_deps[i]];
128 }
129 
upb_FileDef_WeakDependency(const upb_FileDef * f,int i)130 const upb_FileDef* upb_FileDef_WeakDependency(const upb_FileDef* f, int i) {
131   UPB_ASSERT(0 <= i && i < f->public_dep_count);
132   return f->deps[f->weak_deps[i]];
133 }
134 
upb_FileDef_TopLevelMessage(const upb_FileDef * f,int i)135 const upb_MessageDef* upb_FileDef_TopLevelMessage(const upb_FileDef* f, int i) {
136   UPB_ASSERT(0 <= i && i < f->top_lvl_msg_count);
137   return _upb_MessageDef_At(f->top_lvl_msgs, i);
138 }
139 
upb_FileDef_TopLevelEnum(const upb_FileDef * f,int i)140 const upb_EnumDef* upb_FileDef_TopLevelEnum(const upb_FileDef* f, int i) {
141   UPB_ASSERT(0 <= i && i < f->top_lvl_enum_count);
142   return _upb_EnumDef_At(f->top_lvl_enums, i);
143 }
144 
upb_FileDef_TopLevelExtension(const upb_FileDef * f,int i)145 const upb_FieldDef* upb_FileDef_TopLevelExtension(const upb_FileDef* f, int i) {
146   UPB_ASSERT(0 <= i && i < f->top_lvl_ext_count);
147   return _upb_FieldDef_At(f->top_lvl_exts, i);
148 }
149 
upb_FileDef_Service(const upb_FileDef * f,int i)150 const upb_ServiceDef* upb_FileDef_Service(const upb_FileDef* f, int i) {
151   UPB_ASSERT(0 <= i && i < f->service_count);
152   return _upb_ServiceDef_At(f->services, i);
153 }
154 
upb_FileDef_Pool(const upb_FileDef * f)155 const upb_DefPool* upb_FileDef_Pool(const upb_FileDef* f) { return f->symtab; }
156 
_upb_FileDef_ExtensionMiniTable(const upb_FileDef * f,int i)157 const upb_MiniTableExtension* _upb_FileDef_ExtensionMiniTable(
158     const upb_FileDef* f, int i) {
159   return f->ext_layouts[i];
160 }
161 
strviewdup(upb_DefBuilder * ctx,upb_StringView view)162 static char* strviewdup(upb_DefBuilder* ctx, upb_StringView view) {
163   char* ret = upb_strdup2(view.data, view.size, _upb_DefBuilder_Arena(ctx));
164   if (!ret) _upb_DefBuilder_OomErr(ctx);
165   return ret;
166 }
167 
streql_view(upb_StringView view,const char * b)168 static bool streql_view(upb_StringView view, const char* b) {
169   return view.size == strlen(b) && memcmp(view.data, b, view.size) == 0;
170 }
171 
count_exts_in_msg(const UPB_DESC (DescriptorProto)* msg_proto)172 static int count_exts_in_msg(const UPB_DESC(DescriptorProto) * msg_proto) {
173   size_t n;
174   UPB_DESC(DescriptorProto_extension)(msg_proto, &n);
175   int ext_count = n;
176 
177   const UPB_DESC(DescriptorProto)* const* nested_msgs =
178       UPB_DESC(DescriptorProto_nested_type)(msg_proto, &n);
179   for (size_t i = 0; i < n; i++) {
180     ext_count += count_exts_in_msg(nested_msgs[i]);
181   }
182 
183   return ext_count;
184 }
185 
186 // Allocate and initialize one file def, and add it to the context object.
_upb_FileDef_Create(upb_DefBuilder * ctx,const UPB_DESC (FileDescriptorProto)* file_proto)187 void _upb_FileDef_Create(upb_DefBuilder* ctx,
188                          const UPB_DESC(FileDescriptorProto) * file_proto) {
189   upb_FileDef* file = _upb_DefBuilder_Alloc(ctx, sizeof(upb_FileDef));
190   ctx->file = file;
191 
192   const UPB_DESC(DescriptorProto)* const* msgs;
193   const UPB_DESC(EnumDescriptorProto)* const* enums;
194   const UPB_DESC(FieldDescriptorProto)* const* exts;
195   const UPB_DESC(ServiceDescriptorProto)* const* services;
196   const upb_StringView* strs;
197   const int32_t* public_deps;
198   const int32_t* weak_deps;
199   size_t n;
200 
201   file->symtab = ctx->symtab;
202 
203   // Count all extensions in the file, to build a flat array of layouts.
204   UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
205   int ext_count = n;
206   msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);
207   for (int i = 0; i < n; i++) {
208     ext_count += count_exts_in_msg(msgs[i]);
209   }
210   file->ext_count = ext_count;
211 
212   if (ctx->layout) {
213     // We are using the ext layouts that were passed in.
214     file->ext_layouts = ctx->layout->exts;
215     if (ctx->layout->ext_count != file->ext_count) {
216       _upb_DefBuilder_Errf(ctx,
217                            "Extension count did not match layout (%d vs %d)",
218                            ctx->layout->ext_count, file->ext_count);
219     }
220   } else {
221     // We are building ext layouts from scratch.
222     file->ext_layouts = _upb_DefBuilder_Alloc(
223         ctx, sizeof(*file->ext_layouts) * file->ext_count);
224     upb_MiniTableExtension* ext =
225         _upb_DefBuilder_Alloc(ctx, sizeof(*ext) * file->ext_count);
226     for (int i = 0; i < file->ext_count; i++) {
227       file->ext_layouts[i] = &ext[i];
228     }
229   }
230 
231   upb_StringView name = UPB_DESC(FileDescriptorProto_name)(file_proto);
232   file->name = strviewdup(ctx, name);
233   if (strlen(file->name) != name.size) {
234     _upb_DefBuilder_Errf(ctx, "File name contained embedded NULL");
235   }
236 
237   upb_StringView package = UPB_DESC(FileDescriptorProto_package)(file_proto);
238 
239   if (package.size) {
240     _upb_DefBuilder_CheckIdentFull(ctx, package);
241     file->package = strviewdup(ctx, package);
242   } else {
243     file->package = NULL;
244   }
245 
246   upb_StringView edition = UPB_DESC(FileDescriptorProto_edition)(file_proto);
247 
248   if (edition.size == 0) {
249     file->edition = NULL;
250   } else {
251     // TODO(b/267770604): How should we validate this?
252     file->edition = strviewdup(ctx, edition);
253     if (strlen(file->edition) != edition.size) {
254       _upb_DefBuilder_Errf(ctx, "Edition name contained embedded NULL");
255     }
256   }
257 
258   if (UPB_DESC(FileDescriptorProto_has_syntax)(file_proto)) {
259     upb_StringView syntax = UPB_DESC(FileDescriptorProto_syntax)(file_proto);
260 
261     if (streql_view(syntax, "proto2")) {
262       file->syntax = kUpb_Syntax_Proto2;
263     } else if (streql_view(syntax, "proto3")) {
264       file->syntax = kUpb_Syntax_Proto3;
265     } else {
266       _upb_DefBuilder_Errf(ctx, "Invalid syntax '" UPB_STRINGVIEW_FORMAT "'",
267                            UPB_STRINGVIEW_ARGS(syntax));
268     }
269   } else {
270     file->syntax = kUpb_Syntax_Proto2;
271   }
272 
273   // Read options.
274   UPB_DEF_SET_OPTIONS(file->opts, FileDescriptorProto, FileOptions, file_proto);
275 
276   // Verify dependencies.
277   strs = UPB_DESC(FileDescriptorProto_dependency)(file_proto, &n);
278   file->dep_count = n;
279   file->deps = _upb_DefBuilder_Alloc(ctx, sizeof(*file->deps) * n);
280 
281   for (size_t i = 0; i < n; i++) {
282     upb_StringView str = strs[i];
283     file->deps[i] =
284         upb_DefPool_FindFileByNameWithSize(ctx->symtab, str.data, str.size);
285     if (!file->deps[i]) {
286       _upb_DefBuilder_Errf(ctx,
287                            "Depends on file '" UPB_STRINGVIEW_FORMAT
288                            "', but it has not been loaded",
289                            UPB_STRINGVIEW_ARGS(str));
290     }
291   }
292 
293   public_deps = UPB_DESC(FileDescriptorProto_public_dependency)(file_proto, &n);
294   file->public_dep_count = n;
295   file->public_deps =
296       _upb_DefBuilder_Alloc(ctx, sizeof(*file->public_deps) * n);
297   int32_t* mutable_public_deps = (int32_t*)file->public_deps;
298   for (size_t i = 0; i < n; i++) {
299     if (public_deps[i] >= file->dep_count) {
300       _upb_DefBuilder_Errf(ctx, "public_dep %d is out of range",
301                            (int)public_deps[i]);
302     }
303     mutable_public_deps[i] = public_deps[i];
304   }
305 
306   weak_deps = UPB_DESC(FileDescriptorProto_weak_dependency)(file_proto, &n);
307   file->weak_dep_count = n;
308   file->weak_deps = _upb_DefBuilder_Alloc(ctx, sizeof(*file->weak_deps) * n);
309   int32_t* mutable_weak_deps = (int32_t*)file->weak_deps;
310   for (size_t i = 0; i < n; i++) {
311     if (weak_deps[i] >= file->dep_count) {
312       _upb_DefBuilder_Errf(ctx, "weak_dep %d is out of range",
313                            (int)weak_deps[i]);
314     }
315     mutable_weak_deps[i] = weak_deps[i];
316   }
317 
318   // Create enums.
319   enums = UPB_DESC(FileDescriptorProto_enum_type)(file_proto, &n);
320   file->top_lvl_enum_count = n;
321   file->top_lvl_enums = _upb_EnumDefs_New(ctx, n, enums, NULL);
322 
323   // Create extensions.
324   exts = UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
325   file->top_lvl_ext_count = n;
326   file->top_lvl_exts = _upb_Extensions_New(ctx, n, exts, file->package, NULL);
327 
328   // Create messages.
329   msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);
330   file->top_lvl_msg_count = n;
331   file->top_lvl_msgs = _upb_MessageDefs_New(ctx, n, msgs, NULL);
332 
333   // Create services.
334   services = UPB_DESC(FileDescriptorProto_service)(file_proto, &n);
335   file->service_count = n;
336   file->services = _upb_ServiceDefs_New(ctx, n, services);
337 
338   // Now that all names are in the table, build layouts and resolve refs.
339 
340   for (int i = 0; i < file->top_lvl_msg_count; i++) {
341     upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
342     _upb_MessageDef_Resolve(ctx, m);
343   }
344 
345   for (int i = 0; i < file->top_lvl_ext_count; i++) {
346     upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i);
347     _upb_FieldDef_Resolve(ctx, file->package, f);
348   }
349 
350   for (int i = 0; i < file->top_lvl_msg_count; i++) {
351     upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
352     _upb_MessageDef_CreateMiniTable(ctx, (upb_MessageDef*)m);
353   }
354 
355   for (int i = 0; i < file->top_lvl_ext_count; i++) {
356     upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i);
357     _upb_FieldDef_BuildMiniTableExtension(ctx, f);
358   }
359 
360   for (int i = 0; i < file->top_lvl_msg_count; i++) {
361     upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
362     _upb_MessageDef_LinkMiniTable(ctx, m);
363   }
364 
365   if (file->ext_count) {
366     bool ok = upb_ExtensionRegistry_AddArray(
367         _upb_DefPool_ExtReg(ctx->symtab), file->ext_layouts, file->ext_count);
368     if (!ok) _upb_DefBuilder_OomErr(ctx);
369   }
370 }
371