• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "upb/reflection/internal/file_def.h"
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 #include <string.h>
13 
14 #include "upb/base/string_view.h"
15 #include "upb/mini_table/extension.h"
16 #include "upb/mini_table/extension_registry.h"
17 #include "upb/mini_table/file.h"
18 #include "upb/reflection/def.h"
19 #include "upb/reflection/internal/def_builder.h"
20 #include "upb/reflection/internal/def_pool.h"
21 #include "upb/reflection/internal/enum_def.h"
22 #include "upb/reflection/internal/field_def.h"
23 #include "upb/reflection/internal/message_def.h"
24 #include "upb/reflection/internal/service_def.h"
25 #include "upb/reflection/internal/strdup2.h"
26 
27 // Must be last.
28 #include "upb/port/def.inc"
29 
30 struct upb_FileDef {
31   const UPB_DESC(FileOptions*) opts;
32   const UPB_DESC(FeatureSet*) resolved_features;
33   const char* name;
34   const char* package;
35   UPB_DESC(Edition) edition;
36 
37   const upb_FileDef** deps;
38   const int32_t* public_deps;
39   const int32_t* weak_deps;
40   const upb_MessageDef* top_lvl_msgs;
41   const upb_EnumDef* top_lvl_enums;
42   const upb_FieldDef* top_lvl_exts;
43   const upb_ServiceDef* services;
44   const upb_MiniTableExtension** ext_layouts;
45   const upb_DefPool* symtab;
46 
47   int dep_count;
48   int public_dep_count;
49   int weak_dep_count;
50   int top_lvl_msg_count;
51   int top_lvl_enum_count;
52   int top_lvl_ext_count;
53   int service_count;
54   int ext_count;  // All exts in the file.
55   upb_Syntax syntax;
56 };
57 
upb_FileDef_EditionName(int edition)58 UPB_API const char* upb_FileDef_EditionName(int edition) {
59   // TODO Synchronize this with descriptor.proto better.
60   switch (edition) {
61     case UPB_DESC(EDITION_PROTO2):
62       return "PROTO2";
63     case UPB_DESC(EDITION_PROTO3):
64       return "PROTO3";
65     case UPB_DESC(EDITION_2023):
66       return "2023";
67     default:
68       return "UNKNOWN";
69   }
70 }
71 
UPB_DESC(FileOptions)72 const UPB_DESC(FileOptions) * upb_FileDef_Options(const upb_FileDef* f) {
73   return f->opts;
74 }
75 
UPB_DESC(FeatureSet)76 const UPB_DESC(FeatureSet) *
77     upb_FileDef_ResolvedFeatures(const upb_FileDef* f) {
78   return f->resolved_features;
79 }
80 
upb_FileDef_HasOptions(const upb_FileDef * f)81 bool upb_FileDef_HasOptions(const upb_FileDef* f) {
82   return f->opts != (void*)kUpbDefOptDefault;
83 }
84 
upb_FileDef_Name(const upb_FileDef * f)85 const char* upb_FileDef_Name(const upb_FileDef* f) { return f->name; }
86 
upb_FileDef_Package(const upb_FileDef * f)87 const char* upb_FileDef_Package(const upb_FileDef* f) {
88   return f->package ? f->package : "";
89 }
90 
upb_FileDef_Edition(const upb_FileDef * f)91 UPB_DESC(Edition) upb_FileDef_Edition(const upb_FileDef* f) {
92   return f->edition;
93 }
94 
_upb_FileDef_RawPackage(const upb_FileDef * f)95 const char* _upb_FileDef_RawPackage(const upb_FileDef* f) { return f->package; }
96 
upb_FileDef_Syntax(const upb_FileDef * f)97 upb_Syntax upb_FileDef_Syntax(const upb_FileDef* f) { return f->syntax; }
98 
upb_FileDef_TopLevelMessageCount(const upb_FileDef * f)99 int upb_FileDef_TopLevelMessageCount(const upb_FileDef* f) {
100   return f->top_lvl_msg_count;
101 }
102 
upb_FileDef_DependencyCount(const upb_FileDef * f)103 int upb_FileDef_DependencyCount(const upb_FileDef* f) { return f->dep_count; }
104 
upb_FileDef_PublicDependencyCount(const upb_FileDef * f)105 int upb_FileDef_PublicDependencyCount(const upb_FileDef* f) {
106   return f->public_dep_count;
107 }
108 
upb_FileDef_WeakDependencyCount(const upb_FileDef * f)109 int upb_FileDef_WeakDependencyCount(const upb_FileDef* f) {
110   return f->weak_dep_count;
111 }
112 
_upb_FileDef_PublicDependencyIndexes(const upb_FileDef * f)113 const int32_t* _upb_FileDef_PublicDependencyIndexes(const upb_FileDef* f) {
114   return f->public_deps;
115 }
116 
_upb_FileDef_WeakDependencyIndexes(const upb_FileDef * f)117 const int32_t* _upb_FileDef_WeakDependencyIndexes(const upb_FileDef* f) {
118   return f->weak_deps;
119 }
120 
upb_FileDef_TopLevelEnumCount(const upb_FileDef * f)121 int upb_FileDef_TopLevelEnumCount(const upb_FileDef* f) {
122   return f->top_lvl_enum_count;
123 }
124 
upb_FileDef_TopLevelExtensionCount(const upb_FileDef * f)125 int upb_FileDef_TopLevelExtensionCount(const upb_FileDef* f) {
126   return f->top_lvl_ext_count;
127 }
128 
upb_FileDef_ServiceCount(const upb_FileDef * f)129 int upb_FileDef_ServiceCount(const upb_FileDef* f) { return f->service_count; }
130 
upb_FileDef_Dependency(const upb_FileDef * f,int i)131 const upb_FileDef* upb_FileDef_Dependency(const upb_FileDef* f, int i) {
132   UPB_ASSERT(0 <= i && i < f->dep_count);
133   return f->deps[i];
134 }
135 
upb_FileDef_PublicDependency(const upb_FileDef * f,int i)136 const upb_FileDef* upb_FileDef_PublicDependency(const upb_FileDef* f, int i) {
137   UPB_ASSERT(0 <= i && i < f->public_dep_count);
138   return f->deps[f->public_deps[i]];
139 }
140 
upb_FileDef_WeakDependency(const upb_FileDef * f,int i)141 const upb_FileDef* upb_FileDef_WeakDependency(const upb_FileDef* f, int i) {
142   UPB_ASSERT(0 <= i && i < f->public_dep_count);
143   return f->deps[f->weak_deps[i]];
144 }
145 
upb_FileDef_TopLevelMessage(const upb_FileDef * f,int i)146 const upb_MessageDef* upb_FileDef_TopLevelMessage(const upb_FileDef* f, int i) {
147   UPB_ASSERT(0 <= i && i < f->top_lvl_msg_count);
148   return _upb_MessageDef_At(f->top_lvl_msgs, i);
149 }
150 
upb_FileDef_TopLevelEnum(const upb_FileDef * f,int i)151 const upb_EnumDef* upb_FileDef_TopLevelEnum(const upb_FileDef* f, int i) {
152   UPB_ASSERT(0 <= i && i < f->top_lvl_enum_count);
153   return _upb_EnumDef_At(f->top_lvl_enums, i);
154 }
155 
upb_FileDef_TopLevelExtension(const upb_FileDef * f,int i)156 const upb_FieldDef* upb_FileDef_TopLevelExtension(const upb_FileDef* f, int i) {
157   UPB_ASSERT(0 <= i && i < f->top_lvl_ext_count);
158   return _upb_FieldDef_At(f->top_lvl_exts, i);
159 }
160 
upb_FileDef_Service(const upb_FileDef * f,int i)161 const upb_ServiceDef* upb_FileDef_Service(const upb_FileDef* f, int i) {
162   UPB_ASSERT(0 <= i && i < f->service_count);
163   return _upb_ServiceDef_At(f->services, i);
164 }
165 
upb_FileDef_Pool(const upb_FileDef * f)166 const upb_DefPool* upb_FileDef_Pool(const upb_FileDef* f) { return f->symtab; }
167 
_upb_FileDef_ExtensionMiniTable(const upb_FileDef * f,int i)168 const upb_MiniTableExtension* _upb_FileDef_ExtensionMiniTable(
169     const upb_FileDef* f, int i) {
170   return f->ext_layouts[i];
171 }
172 
173 // Note: Import cycles are not allowed so this will terminate.
upb_FileDef_Resolves(const upb_FileDef * f,const char * path)174 bool upb_FileDef_Resolves(const upb_FileDef* f, const char* path) {
175   if (!strcmp(f->name, path)) return true;
176 
177   for (int i = 0; i < upb_FileDef_PublicDependencyCount(f); i++) {
178     const upb_FileDef* dep = upb_FileDef_PublicDependency(f, i);
179     if (upb_FileDef_Resolves(dep, path)) return true;
180   }
181   return false;
182 }
183 
strviewdup(upb_DefBuilder * ctx,upb_StringView view)184 static char* strviewdup(upb_DefBuilder* ctx, upb_StringView view) {
185   char* ret = upb_strdup2(view.data, view.size, _upb_DefBuilder_Arena(ctx));
186   if (!ret) _upb_DefBuilder_OomErr(ctx);
187   return ret;
188 }
189 
streql_view(upb_StringView view,const char * b)190 static bool streql_view(upb_StringView view, const char* b) {
191   return view.size == strlen(b) && memcmp(view.data, b, view.size) == 0;
192 }
193 
count_exts_in_msg(const UPB_DESC (DescriptorProto)* msg_proto)194 static int count_exts_in_msg(const UPB_DESC(DescriptorProto) * msg_proto) {
195   size_t n;
196   UPB_DESC(DescriptorProto_extension)(msg_proto, &n);
197   int ext_count = n;
198 
199   const UPB_DESC(DescriptorProto)* const* nested_msgs =
200       UPB_DESC(DescriptorProto_nested_type)(msg_proto, &n);
201   for (size_t i = 0; i < n; i++) {
202     ext_count += count_exts_in_msg(nested_msgs[i]);
203   }
204 
205   return ext_count;
206 }
207 
UPB_DESC(FeatureSet *)208 const UPB_DESC(FeatureSet*)
209     _upb_FileDef_FindEdition(upb_DefBuilder* ctx, int edition) {
210   const UPB_DESC(FeatureSetDefaults)* defaults =
211       upb_DefPool_FeatureSetDefaults(ctx->symtab);
212 
213   int min = UPB_DESC(FeatureSetDefaults_minimum_edition)(defaults);
214   int max = UPB_DESC(FeatureSetDefaults_maximum_edition)(defaults);
215   if (edition < min) {
216     _upb_DefBuilder_Errf(ctx,
217                          "Edition %s is earlier than the minimum edition %s "
218                          "given in the defaults",
219                          upb_FileDef_EditionName(edition),
220                          upb_FileDef_EditionName(min));
221     return NULL;
222   }
223   if (edition > max) {
224     _upb_DefBuilder_Errf(ctx,
225                          "Edition %s is later than the maximum edition %s "
226                          "given in the defaults",
227                          upb_FileDef_EditionName(edition),
228                          upb_FileDef_EditionName(max));
229     return NULL;
230   }
231 
232   size_t n;
233   const UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault)* const* d =
234       UPB_DESC(FeatureSetDefaults_defaults)(defaults, &n);
235   const UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault)* result = NULL;
236   for (size_t i = 0; i < n; i++) {
237     if (UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault_edition)(d[i]) >
238         edition) {
239       break;
240     }
241     result = d[i];
242   }
243   if (result == NULL) {
244     _upb_DefBuilder_Errf(ctx, "No valid default found for edition %s",
245                          upb_FileDef_EditionName(edition));
246     return NULL;
247   }
248 
249   // Merge the fixed and overridable features to get the edition's default
250   // feature set.
251   const UPB_DESC(FeatureSet)* fixed = UPB_DESC(
252       FeatureSetDefaults_FeatureSetEditionDefault_fixed_features)(result);
253   const UPB_DESC(FeatureSet)* overridable = UPB_DESC(
254       FeatureSetDefaults_FeatureSetEditionDefault_overridable_features)(result);
255   if (!fixed && !overridable) {
256     _upb_DefBuilder_Errf(ctx, "No valid default found for edition %s",
257                          upb_FileDef_EditionName(edition));
258     return NULL;
259   } else if (!fixed) {
260     return overridable;
261   }
262   return _upb_DefBuilder_DoResolveFeatures(ctx, fixed, overridable,
263                                            /*is_implicit=*/true);
264 }
265 
266 // 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)267 void _upb_FileDef_Create(upb_DefBuilder* ctx,
268                          const UPB_DESC(FileDescriptorProto) * file_proto) {
269   upb_FileDef* file = _upb_DefBuilder_Alloc(ctx, sizeof(upb_FileDef));
270   ctx->file = file;
271 
272   const UPB_DESC(DescriptorProto)* const* msgs;
273   const UPB_DESC(EnumDescriptorProto)* const* enums;
274   const UPB_DESC(FieldDescriptorProto)* const* exts;
275   const UPB_DESC(ServiceDescriptorProto)* const* services;
276   const upb_StringView* strs;
277   const int32_t* public_deps;
278   const int32_t* weak_deps;
279   size_t n;
280 
281   file->symtab = ctx->symtab;
282 
283   // Count all extensions in the file, to build a flat array of layouts.
284   UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
285   int ext_count = n;
286   msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);
287   for (size_t i = 0; i < n; i++) {
288     ext_count += count_exts_in_msg(msgs[i]);
289   }
290   file->ext_count = ext_count;
291 
292   if (ctx->layout) {
293     // We are using the ext layouts that were passed in.
294     file->ext_layouts = ctx->layout->UPB_PRIVATE(exts);
295     const int mt_ext_count = upb_MiniTableFile_ExtensionCount(ctx->layout);
296     if (mt_ext_count != file->ext_count) {
297       _upb_DefBuilder_Errf(ctx,
298                            "Extension count did not match layout (%d vs %d)",
299                            mt_ext_count, file->ext_count);
300     }
301   } else {
302     // We are building ext layouts from scratch.
303     file->ext_layouts = _upb_DefBuilder_Alloc(
304         ctx, sizeof(*file->ext_layouts) * file->ext_count);
305     upb_MiniTableExtension* ext =
306         _upb_DefBuilder_Alloc(ctx, sizeof(*ext) * file->ext_count);
307     for (int i = 0; i < file->ext_count; i++) {
308       file->ext_layouts[i] = &ext[i];
309     }
310   }
311 
312   upb_StringView name = UPB_DESC(FileDescriptorProto_name)(file_proto);
313   file->name = strviewdup(ctx, name);
314   if (strlen(file->name) != name.size) {
315     _upb_DefBuilder_Errf(ctx, "File name contained embedded NULL");
316   }
317 
318   upb_StringView package = UPB_DESC(FileDescriptorProto_package)(file_proto);
319 
320   if (package.size) {
321     _upb_DefBuilder_CheckIdentFull(ctx, package);
322     file->package = strviewdup(ctx, package);
323   } else {
324     file->package = NULL;
325   }
326 
327   // TODO: How should we validate this?
328   file->edition = UPB_DESC(FileDescriptorProto_edition)(file_proto);
329 
330   if (UPB_DESC(FileDescriptorProto_has_syntax)(file_proto)) {
331     upb_StringView syntax = UPB_DESC(FileDescriptorProto_syntax)(file_proto);
332 
333     if (streql_view(syntax, "proto2")) {
334       file->syntax = kUpb_Syntax_Proto2;
335       file->edition = UPB_DESC(EDITION_PROTO2);
336     } else if (streql_view(syntax, "proto3")) {
337       file->syntax = kUpb_Syntax_Proto3;
338       file->edition = UPB_DESC(EDITION_PROTO3);
339     } else if (streql_view(syntax, "editions")) {
340       file->syntax = kUpb_Syntax_Editions;
341       file->edition = UPB_DESC(FileDescriptorProto_edition)(file_proto);
342     } else {
343       _upb_DefBuilder_Errf(ctx, "Invalid syntax '" UPB_STRINGVIEW_FORMAT "'",
344                            UPB_STRINGVIEW_ARGS(syntax));
345     }
346   } else {
347     file->syntax = kUpb_Syntax_Proto2;
348     file->edition = UPB_DESC(EDITION_PROTO2);
349   }
350 
351   // Read options.
352   UPB_DEF_SET_OPTIONS(file->opts, FileDescriptorProto, FileOptions, file_proto);
353 
354   // Resolve features.
355   const UPB_DESC(FeatureSet*) edition_defaults =
356       _upb_FileDef_FindEdition(ctx, file->edition);
357   const UPB_DESC(FeatureSet*) unresolved =
358       UPB_DESC(FileOptions_features)(file->opts);
359   file->resolved_features =
360       _upb_DefBuilder_ResolveFeatures(ctx, edition_defaults, unresolved);
361 
362   // Verify dependencies.
363   strs = UPB_DESC(FileDescriptorProto_dependency)(file_proto, &n);
364   file->dep_count = n;
365   file->deps = _upb_DefBuilder_Alloc(ctx, sizeof(*file->deps) * n);
366 
367   for (size_t i = 0; i < n; i++) {
368     upb_StringView str = strs[i];
369     file->deps[i] =
370         upb_DefPool_FindFileByNameWithSize(ctx->symtab, str.data, str.size);
371     if (!file->deps[i]) {
372       _upb_DefBuilder_Errf(ctx,
373                            "Depends on file '" UPB_STRINGVIEW_FORMAT
374                            "', but it has not been loaded",
375                            UPB_STRINGVIEW_ARGS(str));
376     }
377   }
378 
379   public_deps = UPB_DESC(FileDescriptorProto_public_dependency)(file_proto, &n);
380   file->public_dep_count = n;
381   file->public_deps =
382       _upb_DefBuilder_Alloc(ctx, sizeof(*file->public_deps) * n);
383   int32_t* mutable_public_deps = (int32_t*)file->public_deps;
384   for (size_t i = 0; i < n; i++) {
385     if (public_deps[i] >= file->dep_count) {
386       _upb_DefBuilder_Errf(ctx, "public_dep %d is out of range",
387                            (int)public_deps[i]);
388     }
389     mutable_public_deps[i] = public_deps[i];
390   }
391 
392   weak_deps = UPB_DESC(FileDescriptorProto_weak_dependency)(file_proto, &n);
393   file->weak_dep_count = n;
394   file->weak_deps = _upb_DefBuilder_Alloc(ctx, sizeof(*file->weak_deps) * n);
395   int32_t* mutable_weak_deps = (int32_t*)file->weak_deps;
396   for (size_t i = 0; i < n; i++) {
397     if (weak_deps[i] >= file->dep_count) {
398       _upb_DefBuilder_Errf(ctx, "weak_dep %d is out of range",
399                            (int)weak_deps[i]);
400     }
401     mutable_weak_deps[i] = weak_deps[i];
402   }
403 
404   // Create enums.
405   enums = UPB_DESC(FileDescriptorProto_enum_type)(file_proto, &n);
406   file->top_lvl_enum_count = n;
407   file->top_lvl_enums =
408       _upb_EnumDefs_New(ctx, n, enums, file->resolved_features, NULL);
409 
410   // Create extensions.
411   exts = UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
412   file->top_lvl_ext_count = n;
413   file->top_lvl_exts = _upb_Extensions_New(
414       ctx, n, exts, file->resolved_features, file->package, NULL);
415 
416   // Create messages.
417   msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);
418   file->top_lvl_msg_count = n;
419   file->top_lvl_msgs =
420       _upb_MessageDefs_New(ctx, n, msgs, file->resolved_features, NULL);
421 
422   // Create services.
423   services = UPB_DESC(FileDescriptorProto_service)(file_proto, &n);
424   file->service_count = n;
425   file->services =
426       _upb_ServiceDefs_New(ctx, n, services, file->resolved_features);
427 
428   // Now that all names are in the table, build layouts and resolve refs.
429 
430   for (int i = 0; i < file->top_lvl_msg_count; i++) {
431     upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
432     _upb_MessageDef_Resolve(ctx, m);
433   }
434 
435   for (int i = 0; i < file->top_lvl_ext_count; i++) {
436     upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i);
437     _upb_FieldDef_Resolve(ctx, file->package, f);
438   }
439 
440   for (int i = 0; i < file->top_lvl_msg_count; i++) {
441     upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
442     _upb_MessageDef_CreateMiniTable(ctx, (upb_MessageDef*)m);
443   }
444 
445   for (int i = 0; i < file->top_lvl_ext_count; i++) {
446     upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i);
447     _upb_FieldDef_BuildMiniTableExtension(ctx, f);
448   }
449 
450   for (int i = 0; i < file->top_lvl_msg_count; i++) {
451     upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
452     _upb_MessageDef_LinkMiniTable(ctx, m);
453   }
454 
455   if (file->ext_count) {
456     bool ok = upb_ExtensionRegistry_AddArray(
457         _upb_DefPool_ExtReg(ctx->symtab), file->ext_layouts, file->ext_count);
458     if (!ok) _upb_DefBuilder_OomErr(ctx);
459   }
460 }
461