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
strviewdup(upb_DefBuilder * ctx,upb_StringView view)173 static char* strviewdup(upb_DefBuilder* ctx, upb_StringView view) {
174 char* ret = upb_strdup2(view.data, view.size, _upb_DefBuilder_Arena(ctx));
175 if (!ret) _upb_DefBuilder_OomErr(ctx);
176 return ret;
177 }
178
streql_view(upb_StringView view,const char * b)179 static bool streql_view(upb_StringView view, const char* b) {
180 return view.size == strlen(b) && memcmp(view.data, b, view.size) == 0;
181 }
182
count_exts_in_msg(const UPB_DESC (DescriptorProto)* msg_proto)183 static int count_exts_in_msg(const UPB_DESC(DescriptorProto) * msg_proto) {
184 size_t n;
185 UPB_DESC(DescriptorProto_extension)(msg_proto, &n);
186 int ext_count = n;
187
188 const UPB_DESC(DescriptorProto)* const* nested_msgs =
189 UPB_DESC(DescriptorProto_nested_type)(msg_proto, &n);
190 for (size_t i = 0; i < n; i++) {
191 ext_count += count_exts_in_msg(nested_msgs[i]);
192 }
193
194 return ext_count;
195 }
196
UPB_DESC(FeatureSet *)197 const UPB_DESC(FeatureSet*)
198 _upb_FileDef_FindEdition(upb_DefBuilder* ctx, int edition) {
199 const UPB_DESC(FeatureSetDefaults)* defaults =
200 upb_DefPool_FeatureSetDefaults(ctx->symtab);
201
202 int min = UPB_DESC(FeatureSetDefaults_minimum_edition)(defaults);
203 int max = UPB_DESC(FeatureSetDefaults_maximum_edition)(defaults);
204 if (edition < min) {
205 _upb_DefBuilder_Errf(ctx,
206 "Edition %s is earlier than the minimum edition %s "
207 "given in the defaults",
208 upb_FileDef_EditionName(edition),
209 upb_FileDef_EditionName(min));
210 return NULL;
211 }
212 if (edition > max) {
213 _upb_DefBuilder_Errf(ctx,
214 "Edition %s is later than the maximum edition %s "
215 "given in the defaults",
216 upb_FileDef_EditionName(edition),
217 upb_FileDef_EditionName(max));
218 return NULL;
219 }
220
221 size_t n;
222 const UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault)* const* d =
223 UPB_DESC(FeatureSetDefaults_defaults)(defaults, &n);
224 const UPB_DESC(FeatureSet)* ret = NULL;
225 for (size_t i = 0; i < n; i++) {
226 if (UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault_edition)(d[i]) >
227 edition) {
228 break;
229 }
230 ret = UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault_features)(d[i]);
231 }
232 if (ret == NULL) {
233 _upb_DefBuilder_Errf(ctx, "No valid default found for edition %s",
234 upb_FileDef_EditionName(edition));
235 return NULL;
236 }
237 return ret;
238 }
239
240 // 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)241 void _upb_FileDef_Create(upb_DefBuilder* ctx,
242 const UPB_DESC(FileDescriptorProto) * file_proto) {
243 upb_FileDef* file = _upb_DefBuilder_Alloc(ctx, sizeof(upb_FileDef));
244 ctx->file = file;
245
246 const UPB_DESC(DescriptorProto)* const* msgs;
247 const UPB_DESC(EnumDescriptorProto)* const* enums;
248 const UPB_DESC(FieldDescriptorProto)* const* exts;
249 const UPB_DESC(ServiceDescriptorProto)* const* services;
250 const upb_StringView* strs;
251 const int32_t* public_deps;
252 const int32_t* weak_deps;
253 size_t n;
254
255 file->symtab = ctx->symtab;
256
257 // Count all extensions in the file, to build a flat array of layouts.
258 UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
259 int ext_count = n;
260 msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);
261 for (size_t i = 0; i < n; i++) {
262 ext_count += count_exts_in_msg(msgs[i]);
263 }
264 file->ext_count = ext_count;
265
266 if (ctx->layout) {
267 // We are using the ext layouts that were passed in.
268 file->ext_layouts = ctx->layout->UPB_PRIVATE(exts);
269 const int mt_ext_count = upb_MiniTableFile_ExtensionCount(ctx->layout);
270 if (mt_ext_count != file->ext_count) {
271 _upb_DefBuilder_Errf(ctx,
272 "Extension count did not match layout (%d vs %d)",
273 mt_ext_count, file->ext_count);
274 }
275 } else {
276 // We are building ext layouts from scratch.
277 file->ext_layouts = _upb_DefBuilder_Alloc(
278 ctx, sizeof(*file->ext_layouts) * file->ext_count);
279 upb_MiniTableExtension* ext =
280 _upb_DefBuilder_Alloc(ctx, sizeof(*ext) * file->ext_count);
281 for (int i = 0; i < file->ext_count; i++) {
282 file->ext_layouts[i] = &ext[i];
283 }
284 }
285
286 upb_StringView name = UPB_DESC(FileDescriptorProto_name)(file_proto);
287 file->name = strviewdup(ctx, name);
288 if (strlen(file->name) != name.size) {
289 _upb_DefBuilder_Errf(ctx, "File name contained embedded NULL");
290 }
291
292 upb_StringView package = UPB_DESC(FileDescriptorProto_package)(file_proto);
293
294 if (package.size) {
295 _upb_DefBuilder_CheckIdentFull(ctx, package);
296 file->package = strviewdup(ctx, package);
297 } else {
298 file->package = NULL;
299 }
300
301 // TODO: How should we validate this?
302 file->edition = UPB_DESC(FileDescriptorProto_edition)(file_proto);
303
304 if (UPB_DESC(FileDescriptorProto_has_syntax)(file_proto)) {
305 upb_StringView syntax = UPB_DESC(FileDescriptorProto_syntax)(file_proto);
306
307 if (streql_view(syntax, "proto2")) {
308 file->syntax = kUpb_Syntax_Proto2;
309 file->edition = UPB_DESC(EDITION_PROTO2);
310 } else if (streql_view(syntax, "proto3")) {
311 file->syntax = kUpb_Syntax_Proto3;
312 file->edition = UPB_DESC(EDITION_PROTO3);
313 } else if (streql_view(syntax, "editions")) {
314 file->syntax = kUpb_Syntax_Editions;
315 file->edition = UPB_DESC(FileDescriptorProto_edition)(file_proto);
316 } else {
317 _upb_DefBuilder_Errf(ctx, "Invalid syntax '" UPB_STRINGVIEW_FORMAT "'",
318 UPB_STRINGVIEW_ARGS(syntax));
319 }
320 } else {
321 file->syntax = kUpb_Syntax_Proto2;
322 file->edition = UPB_DESC(EDITION_PROTO2);
323 }
324
325 // Read options.
326 UPB_DEF_SET_OPTIONS(file->opts, FileDescriptorProto, FileOptions, file_proto);
327
328 // Resolve features.
329 const UPB_DESC(FeatureSet*) edition_defaults =
330 _upb_FileDef_FindEdition(ctx, file->edition);
331 const UPB_DESC(FeatureSet*) unresolved =
332 UPB_DESC(FileOptions_features)(file->opts);
333 file->resolved_features =
334 _upb_DefBuilder_ResolveFeatures(ctx, edition_defaults, unresolved);
335
336 // Verify dependencies.
337 strs = UPB_DESC(FileDescriptorProto_dependency)(file_proto, &n);
338 file->dep_count = n;
339 file->deps = _upb_DefBuilder_Alloc(ctx, sizeof(*file->deps) * n);
340
341 for (size_t i = 0; i < n; i++) {
342 upb_StringView str = strs[i];
343 file->deps[i] =
344 upb_DefPool_FindFileByNameWithSize(ctx->symtab, str.data, str.size);
345 if (!file->deps[i]) {
346 _upb_DefBuilder_Errf(ctx,
347 "Depends on file '" UPB_STRINGVIEW_FORMAT
348 "', but it has not been loaded",
349 UPB_STRINGVIEW_ARGS(str));
350 }
351 }
352
353 public_deps = UPB_DESC(FileDescriptorProto_public_dependency)(file_proto, &n);
354 file->public_dep_count = n;
355 file->public_deps =
356 _upb_DefBuilder_Alloc(ctx, sizeof(*file->public_deps) * n);
357 int32_t* mutable_public_deps = (int32_t*)file->public_deps;
358 for (size_t i = 0; i < n; i++) {
359 if (public_deps[i] >= file->dep_count) {
360 _upb_DefBuilder_Errf(ctx, "public_dep %d is out of range",
361 (int)public_deps[i]);
362 }
363 mutable_public_deps[i] = public_deps[i];
364 }
365
366 weak_deps = UPB_DESC(FileDescriptorProto_weak_dependency)(file_proto, &n);
367 file->weak_dep_count = n;
368 file->weak_deps = _upb_DefBuilder_Alloc(ctx, sizeof(*file->weak_deps) * n);
369 int32_t* mutable_weak_deps = (int32_t*)file->weak_deps;
370 for (size_t i = 0; i < n; i++) {
371 if (weak_deps[i] >= file->dep_count) {
372 _upb_DefBuilder_Errf(ctx, "weak_dep %d is out of range",
373 (int)weak_deps[i]);
374 }
375 mutable_weak_deps[i] = weak_deps[i];
376 }
377
378 // Create enums.
379 enums = UPB_DESC(FileDescriptorProto_enum_type)(file_proto, &n);
380 file->top_lvl_enum_count = n;
381 file->top_lvl_enums =
382 _upb_EnumDefs_New(ctx, n, enums, file->resolved_features, NULL);
383
384 // Create extensions.
385 exts = UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
386 file->top_lvl_ext_count = n;
387 file->top_lvl_exts = _upb_Extensions_New(
388 ctx, n, exts, file->resolved_features, file->package, NULL);
389
390 // Create messages.
391 msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);
392 file->top_lvl_msg_count = n;
393 file->top_lvl_msgs =
394 _upb_MessageDefs_New(ctx, n, msgs, file->resolved_features, NULL);
395
396 // Create services.
397 services = UPB_DESC(FileDescriptorProto_service)(file_proto, &n);
398 file->service_count = n;
399 file->services =
400 _upb_ServiceDefs_New(ctx, n, services, file->resolved_features);
401
402 // Now that all names are in the table, build layouts and resolve refs.
403
404 for (int i = 0; i < file->top_lvl_msg_count; i++) {
405 upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
406 _upb_MessageDef_Resolve(ctx, m);
407 }
408
409 for (int i = 0; i < file->top_lvl_ext_count; i++) {
410 upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i);
411 _upb_FieldDef_Resolve(ctx, file->package, f);
412 }
413
414 for (int i = 0; i < file->top_lvl_msg_count; i++) {
415 upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
416 _upb_MessageDef_CreateMiniTable(ctx, (upb_MessageDef*)m);
417 }
418
419 for (int i = 0; i < file->top_lvl_ext_count; i++) {
420 upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i);
421 _upb_FieldDef_BuildMiniTableExtension(ctx, f);
422 }
423
424 for (int i = 0; i < file->top_lvl_msg_count; i++) {
425 upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
426 _upb_MessageDef_LinkMiniTable(ctx, m);
427 }
428
429 if (file->ext_count) {
430 bool ok = upb_ExtensionRegistry_AddArray(
431 _upb_DefPool_ExtReg(ctx->symtab), file->ext_layouts, file->ext_count);
432 if (!ok) _upb_DefBuilder_OomErr(ctx);
433 }
434 }
435