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/hash/int_table.h"
29 #include "upb/hash/str_table.h"
30 #include "upb/mini_table/decode.h"
31 #include "upb/reflection/def.h"
32 #include "upb/reflection/def_builder_internal.h"
33 #include "upb/reflection/def_type.h"
34 #include "upb/reflection/desc_state_internal.h"
35 #include "upb/reflection/enum_def_internal.h"
36 #include "upb/reflection/extension_range_internal.h"
37 #include "upb/reflection/field_def_internal.h"
38 #include "upb/reflection/file_def_internal.h"
39 #include "upb/reflection/message_def_internal.h"
40 #include "upb/reflection/message_reserved_range_internal.h"
41 #include "upb/reflection/oneof_def_internal.h"
42 
43 // Must be last.
44 #include "upb/port/def.inc"
45 
46 struct upb_MessageDef {
47   const UPB_DESC(MessageOptions) * opts;
48   const upb_MiniTable* layout;
49   const upb_FileDef* file;
50   const upb_MessageDef* containing_type;
51   const char* full_name;
52 
53   // Tables for looking up fields by number and name.
54   upb_inttable itof;
55   upb_strtable ntof;
56 
57   /* All nested defs.
58    * MEM: We could save some space here by putting nested defs in a contiguous
59    * region and calculating counts from offsets or vice-versa. */
60   const upb_FieldDef* fields;
61   const upb_OneofDef* oneofs;
62   const upb_ExtensionRange* ext_ranges;
63   const upb_StringView* res_names;
64   const upb_MessageDef* nested_msgs;
65   const upb_MessageReservedRange* res_ranges;
66   const upb_EnumDef* nested_enums;
67   const upb_FieldDef* nested_exts;
68 
69   // TODO(salo): These counters don't need anywhere near 32 bits.
70   int field_count;
71   int real_oneof_count;
72   int oneof_count;
73   int ext_range_count;
74   int res_range_count;
75   int res_name_count;
76   int nested_msg_count;
77   int nested_enum_count;
78   int nested_ext_count;
79   bool in_message_set;
80   bool is_sorted;
81   upb_WellKnown well_known_type;
82 #if UINTPTR_MAX == 0xffffffff
83   uint32_t padding;  // Increase size to a multiple of 8.
84 #endif
85 };
86 
assign_msg_wellknowntype(upb_MessageDef * m)87 static void assign_msg_wellknowntype(upb_MessageDef* m) {
88   const char* name = m->full_name;
89   if (name == NULL) {
90     m->well_known_type = kUpb_WellKnown_Unspecified;
91     return;
92   }
93   if (!strcmp(name, "google.protobuf.Any")) {
94     m->well_known_type = kUpb_WellKnown_Any;
95   } else if (!strcmp(name, "google.protobuf.FieldMask")) {
96     m->well_known_type = kUpb_WellKnown_FieldMask;
97   } else if (!strcmp(name, "google.protobuf.Duration")) {
98     m->well_known_type = kUpb_WellKnown_Duration;
99   } else if (!strcmp(name, "google.protobuf.Timestamp")) {
100     m->well_known_type = kUpb_WellKnown_Timestamp;
101   } else if (!strcmp(name, "google.protobuf.DoubleValue")) {
102     m->well_known_type = kUpb_WellKnown_DoubleValue;
103   } else if (!strcmp(name, "google.protobuf.FloatValue")) {
104     m->well_known_type = kUpb_WellKnown_FloatValue;
105   } else if (!strcmp(name, "google.protobuf.Int64Value")) {
106     m->well_known_type = kUpb_WellKnown_Int64Value;
107   } else if (!strcmp(name, "google.protobuf.UInt64Value")) {
108     m->well_known_type = kUpb_WellKnown_UInt64Value;
109   } else if (!strcmp(name, "google.protobuf.Int32Value")) {
110     m->well_known_type = kUpb_WellKnown_Int32Value;
111   } else if (!strcmp(name, "google.protobuf.UInt32Value")) {
112     m->well_known_type = kUpb_WellKnown_UInt32Value;
113   } else if (!strcmp(name, "google.protobuf.BoolValue")) {
114     m->well_known_type = kUpb_WellKnown_BoolValue;
115   } else if (!strcmp(name, "google.protobuf.StringValue")) {
116     m->well_known_type = kUpb_WellKnown_StringValue;
117   } else if (!strcmp(name, "google.protobuf.BytesValue")) {
118     m->well_known_type = kUpb_WellKnown_BytesValue;
119   } else if (!strcmp(name, "google.protobuf.Value")) {
120     m->well_known_type = kUpb_WellKnown_Value;
121   } else if (!strcmp(name, "google.protobuf.ListValue")) {
122     m->well_known_type = kUpb_WellKnown_ListValue;
123   } else if (!strcmp(name, "google.protobuf.Struct")) {
124     m->well_known_type = kUpb_WellKnown_Struct;
125   } else {
126     m->well_known_type = kUpb_WellKnown_Unspecified;
127   }
128 }
129 
_upb_MessageDef_At(const upb_MessageDef * m,int i)130 upb_MessageDef* _upb_MessageDef_At(const upb_MessageDef* m, int i) {
131   return (upb_MessageDef*)&m[i];
132 }
133 
_upb_MessageDef_IsValidExtensionNumber(const upb_MessageDef * m,int n)134 bool _upb_MessageDef_IsValidExtensionNumber(const upb_MessageDef* m, int n) {
135   for (int i = 0; i < m->ext_range_count; i++) {
136     const upb_ExtensionRange* r = upb_MessageDef_ExtensionRange(m, i);
137     if (upb_ExtensionRange_Start(r) <= n && n < upb_ExtensionRange_End(r)) {
138       return true;
139     }
140   }
141   return false;
142 }
143 
UPB_DESC(MessageOptions)144 const UPB_DESC(MessageOptions) *
145     upb_MessageDef_Options(const upb_MessageDef* m) {
146   return m->opts;
147 }
148 
upb_MessageDef_HasOptions(const upb_MessageDef * m)149 bool upb_MessageDef_HasOptions(const upb_MessageDef* m) {
150   return m->opts != (void*)kUpbDefOptDefault;
151 }
152 
upb_MessageDef_FullName(const upb_MessageDef * m)153 const char* upb_MessageDef_FullName(const upb_MessageDef* m) {
154   return m->full_name;
155 }
156 
upb_MessageDef_File(const upb_MessageDef * m)157 const upb_FileDef* upb_MessageDef_File(const upb_MessageDef* m) {
158   return m->file;
159 }
160 
upb_MessageDef_ContainingType(const upb_MessageDef * m)161 const upb_MessageDef* upb_MessageDef_ContainingType(const upb_MessageDef* m) {
162   return m->containing_type;
163 }
164 
upb_MessageDef_Name(const upb_MessageDef * m)165 const char* upb_MessageDef_Name(const upb_MessageDef* m) {
166   return _upb_DefBuilder_FullToShort(m->full_name);
167 }
168 
upb_MessageDef_Syntax(const upb_MessageDef * m)169 upb_Syntax upb_MessageDef_Syntax(const upb_MessageDef* m) {
170   return upb_FileDef_Syntax(m->file);
171 }
172 
upb_MessageDef_FindFieldByNumber(const upb_MessageDef * m,uint32_t i)173 const upb_FieldDef* upb_MessageDef_FindFieldByNumber(const upb_MessageDef* m,
174                                                      uint32_t i) {
175   upb_value val;
176   return upb_inttable_lookup(&m->itof, i, &val) ? upb_value_getconstptr(val)
177                                                 : NULL;
178 }
179 
upb_MessageDef_FindFieldByNameWithSize(const upb_MessageDef * m,const char * name,size_t size)180 const upb_FieldDef* upb_MessageDef_FindFieldByNameWithSize(
181     const upb_MessageDef* m, const char* name, size_t size) {
182   upb_value val;
183 
184   if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) {
185     return NULL;
186   }
187 
188   return _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD);
189 }
190 
upb_MessageDef_FindOneofByNameWithSize(const upb_MessageDef * m,const char * name,size_t size)191 const upb_OneofDef* upb_MessageDef_FindOneofByNameWithSize(
192     const upb_MessageDef* m, const char* name, size_t size) {
193   upb_value val;
194 
195   if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) {
196     return NULL;
197   }
198 
199   return _upb_DefType_Unpack(val, UPB_DEFTYPE_ONEOF);
200 }
201 
_upb_MessageDef_Insert(upb_MessageDef * m,const char * name,size_t len,upb_value v,upb_Arena * a)202 bool _upb_MessageDef_Insert(upb_MessageDef* m, const char* name, size_t len,
203                             upb_value v, upb_Arena* a) {
204   return upb_strtable_insert(&m->ntof, name, len, v, a);
205 }
206 
upb_MessageDef_FindByNameWithSize(const upb_MessageDef * m,const char * name,size_t len,const upb_FieldDef ** out_f,const upb_OneofDef ** out_o)207 bool upb_MessageDef_FindByNameWithSize(const upb_MessageDef* m,
208                                        const char* name, size_t len,
209                                        const upb_FieldDef** out_f,
210                                        const upb_OneofDef** out_o) {
211   upb_value val;
212 
213   if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
214     return false;
215   }
216 
217   const upb_FieldDef* f = _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD);
218   const upb_OneofDef* o = _upb_DefType_Unpack(val, UPB_DEFTYPE_ONEOF);
219   if (out_f) *out_f = f;
220   if (out_o) *out_o = o;
221   return f || o; /* False if this was a JSON name. */
222 }
223 
upb_MessageDef_FindByJsonNameWithSize(const upb_MessageDef * m,const char * name,size_t size)224 const upb_FieldDef* upb_MessageDef_FindByJsonNameWithSize(
225     const upb_MessageDef* m, const char* name, size_t size) {
226   upb_value val;
227   const upb_FieldDef* f;
228 
229   if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) {
230     return NULL;
231   }
232 
233   f = _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD);
234   if (!f) f = _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD_JSONNAME);
235 
236   return f;
237 }
238 
upb_MessageDef_ExtensionRangeCount(const upb_MessageDef * m)239 int upb_MessageDef_ExtensionRangeCount(const upb_MessageDef* m) {
240   return m->ext_range_count;
241 }
242 
upb_MessageDef_ReservedRangeCount(const upb_MessageDef * m)243 int upb_MessageDef_ReservedRangeCount(const upb_MessageDef* m) {
244   return m->res_range_count;
245 }
246 
upb_MessageDef_ReservedNameCount(const upb_MessageDef * m)247 int upb_MessageDef_ReservedNameCount(const upb_MessageDef* m) {
248   return m->res_name_count;
249 }
250 
upb_MessageDef_FieldCount(const upb_MessageDef * m)251 int upb_MessageDef_FieldCount(const upb_MessageDef* m) {
252   return m->field_count;
253 }
254 
upb_MessageDef_OneofCount(const upb_MessageDef * m)255 int upb_MessageDef_OneofCount(const upb_MessageDef* m) {
256   return m->oneof_count;
257 }
258 
upb_MessageDef_RealOneofCount(const upb_MessageDef * m)259 int upb_MessageDef_RealOneofCount(const upb_MessageDef* m) {
260   return m->real_oneof_count;
261 }
262 
upb_MessageDef_NestedMessageCount(const upb_MessageDef * m)263 int upb_MessageDef_NestedMessageCount(const upb_MessageDef* m) {
264   return m->nested_msg_count;
265 }
266 
upb_MessageDef_NestedEnumCount(const upb_MessageDef * m)267 int upb_MessageDef_NestedEnumCount(const upb_MessageDef* m) {
268   return m->nested_enum_count;
269 }
270 
upb_MessageDef_NestedExtensionCount(const upb_MessageDef * m)271 int upb_MessageDef_NestedExtensionCount(const upb_MessageDef* m) {
272   return m->nested_ext_count;
273 }
274 
upb_MessageDef_MiniTable(const upb_MessageDef * m)275 const upb_MiniTable* upb_MessageDef_MiniTable(const upb_MessageDef* m) {
276   return m->layout;
277 }
278 
upb_MessageDef_ExtensionRange(const upb_MessageDef * m,int i)279 const upb_ExtensionRange* upb_MessageDef_ExtensionRange(const upb_MessageDef* m,
280                                                         int i) {
281   UPB_ASSERT(0 <= i && i < m->ext_range_count);
282   return _upb_ExtensionRange_At(m->ext_ranges, i);
283 }
284 
upb_MessageDef_ReservedRange(const upb_MessageDef * m,int i)285 const upb_MessageReservedRange* upb_MessageDef_ReservedRange(
286     const upb_MessageDef* m, int i) {
287   UPB_ASSERT(0 <= i && i < m->res_range_count);
288   return _upb_MessageReservedRange_At(m->res_ranges, i);
289 }
290 
upb_MessageDef_ReservedName(const upb_MessageDef * m,int i)291 upb_StringView upb_MessageDef_ReservedName(const upb_MessageDef* m, int i) {
292   UPB_ASSERT(0 <= i && i < m->res_name_count);
293   return m->res_names[i];
294 }
295 
upb_MessageDef_Field(const upb_MessageDef * m,int i)296 const upb_FieldDef* upb_MessageDef_Field(const upb_MessageDef* m, int i) {
297   UPB_ASSERT(0 <= i && i < m->field_count);
298   return _upb_FieldDef_At(m->fields, i);
299 }
300 
upb_MessageDef_Oneof(const upb_MessageDef * m,int i)301 const upb_OneofDef* upb_MessageDef_Oneof(const upb_MessageDef* m, int i) {
302   UPB_ASSERT(0 <= i && i < m->oneof_count);
303   return _upb_OneofDef_At(m->oneofs, i);
304 }
305 
upb_MessageDef_NestedMessage(const upb_MessageDef * m,int i)306 const upb_MessageDef* upb_MessageDef_NestedMessage(const upb_MessageDef* m,
307                                                    int i) {
308   UPB_ASSERT(0 <= i && i < m->nested_msg_count);
309   return &m->nested_msgs[i];
310 }
311 
upb_MessageDef_NestedEnum(const upb_MessageDef * m,int i)312 const upb_EnumDef* upb_MessageDef_NestedEnum(const upb_MessageDef* m, int i) {
313   UPB_ASSERT(0 <= i && i < m->nested_enum_count);
314   return _upb_EnumDef_At(m->nested_enums, i);
315 }
316 
upb_MessageDef_NestedExtension(const upb_MessageDef * m,int i)317 const upb_FieldDef* upb_MessageDef_NestedExtension(const upb_MessageDef* m,
318                                                    int i) {
319   UPB_ASSERT(0 <= i && i < m->nested_ext_count);
320   return _upb_FieldDef_At(m->nested_exts, i);
321 }
322 
upb_MessageDef_WellKnownType(const upb_MessageDef * m)323 upb_WellKnown upb_MessageDef_WellKnownType(const upb_MessageDef* m) {
324   return m->well_known_type;
325 }
326 
_upb_MessageDef_InMessageSet(const upb_MessageDef * m)327 bool _upb_MessageDef_InMessageSet(const upb_MessageDef* m) {
328   return m->in_message_set;
329 }
330 
upb_MessageDef_FindFieldByName(const upb_MessageDef * m,const char * name)331 const upb_FieldDef* upb_MessageDef_FindFieldByName(const upb_MessageDef* m,
332                                                    const char* name) {
333   return upb_MessageDef_FindFieldByNameWithSize(m, name, strlen(name));
334 }
335 
upb_MessageDef_FindOneofByName(const upb_MessageDef * m,const char * name)336 const upb_OneofDef* upb_MessageDef_FindOneofByName(const upb_MessageDef* m,
337                                                    const char* name) {
338   return upb_MessageDef_FindOneofByNameWithSize(m, name, strlen(name));
339 }
340 
upb_MessageDef_IsMapEntry(const upb_MessageDef * m)341 bool upb_MessageDef_IsMapEntry(const upb_MessageDef* m) {
342   return UPB_DESC(MessageOptions_map_entry)(m->opts);
343 }
344 
upb_MessageDef_IsMessageSet(const upb_MessageDef * m)345 bool upb_MessageDef_IsMessageSet(const upb_MessageDef* m) {
346   return UPB_DESC(MessageOptions_message_set_wire_format)(m->opts);
347 }
348 
_upb_MessageDef_MakeMiniTable(upb_DefBuilder * ctx,const upb_MessageDef * m)349 static upb_MiniTable* _upb_MessageDef_MakeMiniTable(upb_DefBuilder* ctx,
350                                                     const upb_MessageDef* m) {
351   upb_StringView desc;
352   // Note: this will assign layout_index for fields, so upb_FieldDef_MiniTable()
353   // is safe to call only after this call.
354   bool ok = upb_MessageDef_MiniDescriptorEncode(m, ctx->tmp_arena, &desc);
355   if (!ok) _upb_DefBuilder_OomErr(ctx);
356 
357   void** scratch_data = _upb_DefPool_ScratchData(ctx->symtab);
358   size_t* scratch_size = _upb_DefPool_ScratchSize(ctx->symtab);
359   upb_MiniTable* ret = upb_MiniTable_BuildWithBuf(
360       desc.data, desc.size, ctx->platform, ctx->arena, scratch_data,
361       scratch_size, ctx->status);
362   if (!ret) _upb_DefBuilder_FailJmp(ctx);
363 
364   return ret;
365 }
366 
_upb_MessageDef_Resolve(upb_DefBuilder * ctx,upb_MessageDef * m)367 void _upb_MessageDef_Resolve(upb_DefBuilder* ctx, upb_MessageDef* m) {
368   for (int i = 0; i < m->field_count; i++) {
369     upb_FieldDef* f = (upb_FieldDef*)upb_MessageDef_Field(m, i);
370     _upb_FieldDef_Resolve(ctx, m->full_name, f);
371   }
372 
373   m->in_message_set = false;
374   for (int i = 0; i < upb_MessageDef_NestedExtensionCount(m); i++) {
375     upb_FieldDef* ext = (upb_FieldDef*)upb_MessageDef_NestedExtension(m, i);
376     _upb_FieldDef_Resolve(ctx, m->full_name, ext);
377     if (upb_FieldDef_Type(ext) == kUpb_FieldType_Message &&
378         upb_FieldDef_Label(ext) == kUpb_Label_Optional &&
379         upb_FieldDef_MessageSubDef(ext) == m &&
380         UPB_DESC(MessageOptions_message_set_wire_format)(
381             upb_MessageDef_Options(upb_FieldDef_ContainingType(ext)))) {
382       m->in_message_set = true;
383     }
384   }
385 
386   for (int i = 0; i < upb_MessageDef_NestedMessageCount(m); i++) {
387     upb_MessageDef* n = (upb_MessageDef*)upb_MessageDef_NestedMessage(m, i);
388     _upb_MessageDef_Resolve(ctx, n);
389   }
390 }
391 
_upb_MessageDef_InsertField(upb_DefBuilder * ctx,upb_MessageDef * m,const upb_FieldDef * f)392 void _upb_MessageDef_InsertField(upb_DefBuilder* ctx, upb_MessageDef* m,
393                                  const upb_FieldDef* f) {
394   const int32_t field_number = upb_FieldDef_Number(f);
395 
396   if (field_number <= 0 || field_number > kUpb_MaxFieldNumber) {
397     _upb_DefBuilder_Errf(ctx, "invalid field number (%u)", field_number);
398   }
399 
400   const char* json_name = upb_FieldDef_JsonName(f);
401   const char* shortname = upb_FieldDef_Name(f);
402   const size_t shortnamelen = strlen(shortname);
403 
404   upb_value v = upb_value_constptr(f);
405 
406   upb_value existing_v;
407   if (upb_strtable_lookup(&m->ntof, shortname, &existing_v)) {
408     _upb_DefBuilder_Errf(ctx, "duplicate field name (%s)", shortname);
409   }
410 
411   const upb_value field_v = _upb_DefType_Pack(f, UPB_DEFTYPE_FIELD);
412   bool ok =
413       _upb_MessageDef_Insert(m, shortname, shortnamelen, field_v, ctx->arena);
414   if (!ok) _upb_DefBuilder_OomErr(ctx);
415 
416   if (strcmp(shortname, json_name) != 0) {
417     if (upb_strtable_lookup(&m->ntof, json_name, &v)) {
418       _upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name);
419     }
420 
421     const size_t json_size = strlen(json_name);
422     const upb_value json_v = _upb_DefType_Pack(f, UPB_DEFTYPE_FIELD_JSONNAME);
423     ok = _upb_MessageDef_Insert(m, json_name, json_size, json_v, ctx->arena);
424     if (!ok) _upb_DefBuilder_OomErr(ctx);
425   }
426 
427   if (upb_inttable_lookup(&m->itof, field_number, NULL)) {
428     _upb_DefBuilder_Errf(ctx, "duplicate field number (%u)", field_number);
429   }
430 
431   ok = upb_inttable_insert(&m->itof, field_number, v, ctx->arena);
432   if (!ok) _upb_DefBuilder_OomErr(ctx);
433 }
434 
_upb_MessageDef_CreateMiniTable(upb_DefBuilder * ctx,upb_MessageDef * m)435 void _upb_MessageDef_CreateMiniTable(upb_DefBuilder* ctx, upb_MessageDef* m) {
436   if (ctx->layout == NULL) {
437     m->layout = _upb_MessageDef_MakeMiniTable(ctx, m);
438   } else {
439     UPB_ASSERT(ctx->msg_count < ctx->layout->msg_count);
440     m->layout = ctx->layout->msgs[ctx->msg_count++];
441     UPB_ASSERT(m->field_count == m->layout->field_count);
442 
443     // We don't need the result of this call, but it will assign layout_index
444     // for all the fields in O(n lg n) time.
445     _upb_FieldDefs_Sorted(m->fields, m->field_count, ctx->tmp_arena);
446   }
447 
448   for (int i = 0; i < m->nested_msg_count; i++) {
449     upb_MessageDef* nested =
450         (upb_MessageDef*)upb_MessageDef_NestedMessage(m, i);
451     _upb_MessageDef_CreateMiniTable(ctx, nested);
452   }
453 }
454 
_upb_MessageDef_LinkMiniTable(upb_DefBuilder * ctx,const upb_MessageDef * m)455 void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
456                                    const upb_MessageDef* m) {
457   for (int i = 0; i < upb_MessageDef_NestedExtensionCount(m); i++) {
458     const upb_FieldDef* ext = upb_MessageDef_NestedExtension(m, i);
459     _upb_FieldDef_BuildMiniTableExtension(ctx, ext);
460   }
461 
462   for (int i = 0; i < m->nested_msg_count; i++) {
463     _upb_MessageDef_LinkMiniTable(ctx, upb_MessageDef_NestedMessage(m, i));
464   }
465 
466   if (ctx->layout) return;
467 
468   for (int i = 0; i < m->field_count; i++) {
469     const upb_FieldDef* f = upb_MessageDef_Field(m, i);
470     const upb_MessageDef* sub_m = upb_FieldDef_MessageSubDef(f);
471     const upb_EnumDef* sub_e = upb_FieldDef_EnumSubDef(f);
472     const int layout_index = _upb_FieldDef_LayoutIndex(f);
473     upb_MiniTable* mt = (upb_MiniTable*)upb_MessageDef_MiniTable(m);
474 
475     UPB_ASSERT(layout_index < m->field_count);
476     upb_MiniTableField* mt_f =
477         (upb_MiniTableField*)&m->layout->fields[layout_index];
478     if (sub_m) {
479       if (!mt->subs) {
480         _upb_DefBuilder_Errf(ctx, "unexpected submsg for (%s)", m->full_name);
481       }
482       UPB_ASSERT(mt_f);
483       UPB_ASSERT(sub_m->layout);
484       if (UPB_UNLIKELY(!upb_MiniTable_SetSubMessage(mt, mt_f, sub_m->layout))) {
485         _upb_DefBuilder_Errf(ctx, "invalid submsg for (%s)", m->full_name);
486       }
487     } else if (_upb_FieldDef_IsClosedEnum(f)) {
488       const upb_MiniTableEnum* mt_e = _upb_EnumDef_MiniTable(sub_e);
489       if (UPB_UNLIKELY(!upb_MiniTable_SetSubEnum(mt, mt_f, mt_e))) {
490         _upb_DefBuilder_Errf(ctx, "invalid subenum for (%s)", m->full_name);
491       }
492     }
493   }
494 
495 #ifndef NDEBUG
496   for (int i = 0; i < m->field_count; i++) {
497     const upb_FieldDef* f = upb_MessageDef_Field(m, i);
498     const int layout_index = _upb_FieldDef_LayoutIndex(f);
499     UPB_ASSERT(layout_index < m->layout->field_count);
500     const upb_MiniTableField* mt_f = &m->layout->fields[layout_index];
501     UPB_ASSERT(upb_FieldDef_Type(f) == upb_MiniTableField_Type(mt_f));
502     UPB_ASSERT(upb_FieldDef_HasPresence(f) ==
503                upb_MiniTableField_HasPresence(mt_f));
504   }
505 #endif
506 }
507 
_upb_MessageDef_Modifiers(const upb_MessageDef * m)508 static uint64_t _upb_MessageDef_Modifiers(const upb_MessageDef* m) {
509   uint64_t out = 0;
510   if (upb_FileDef_Syntax(m->file) == kUpb_Syntax_Proto3) {
511     out |= kUpb_MessageModifier_ValidateUtf8;
512     out |= kUpb_MessageModifier_DefaultIsPacked;
513   }
514   if (m->ext_range_count) {
515     out |= kUpb_MessageModifier_IsExtendable;
516   }
517   return out;
518 }
519 
_upb_MessageDef_EncodeMap(upb_DescState * s,const upb_MessageDef * m,upb_Arena * a)520 static bool _upb_MessageDef_EncodeMap(upb_DescState* s, const upb_MessageDef* m,
521                                       upb_Arena* a) {
522   if (m->field_count != 2) return false;
523 
524   const upb_FieldDef* key_field = upb_MessageDef_Field(m, 0);
525   const upb_FieldDef* val_field = upb_MessageDef_Field(m, 1);
526   if (key_field == NULL || val_field == NULL) return false;
527 
528   UPB_ASSERT(_upb_FieldDef_LayoutIndex(key_field) == 0);
529   UPB_ASSERT(_upb_FieldDef_LayoutIndex(val_field) == 1);
530 
531   s->ptr = upb_MtDataEncoder_EncodeMap(
532       &s->e, s->ptr, upb_FieldDef_Type(key_field), upb_FieldDef_Type(val_field),
533       _upb_FieldDef_Modifiers(key_field), _upb_FieldDef_Modifiers(val_field));
534   return true;
535 }
536 
_upb_MessageDef_EncodeMessage(upb_DescState * s,const upb_MessageDef * m,upb_Arena * a)537 static bool _upb_MessageDef_EncodeMessage(upb_DescState* s,
538                                           const upb_MessageDef* m,
539                                           upb_Arena* a) {
540   const upb_FieldDef** sorted = NULL;
541   if (!m->is_sorted) {
542     sorted = _upb_FieldDefs_Sorted(m->fields, m->field_count, a);
543     if (!sorted) return false;
544   }
545 
546   s->ptr = upb_MtDataEncoder_StartMessage(&s->e, s->ptr,
547                                           _upb_MessageDef_Modifiers(m));
548 
549   for (int i = 0; i < m->field_count; i++) {
550     const upb_FieldDef* f = sorted ? sorted[i] : upb_MessageDef_Field(m, i);
551     const upb_FieldType type = upb_FieldDef_Type(f);
552     const int number = upb_FieldDef_Number(f);
553     const uint64_t modifiers = _upb_FieldDef_Modifiers(f);
554 
555     if (!_upb_DescState_Grow(s, a)) return false;
556     s->ptr = upb_MtDataEncoder_PutField(&s->e, s->ptr, type, number, modifiers);
557   }
558 
559   for (int i = 0; i < m->real_oneof_count; i++) {
560     if (!_upb_DescState_Grow(s, a)) return false;
561     s->ptr = upb_MtDataEncoder_StartOneof(&s->e, s->ptr);
562 
563     const upb_OneofDef* o = upb_MessageDef_Oneof(m, i);
564     const int field_count = upb_OneofDef_FieldCount(o);
565     for (int j = 0; j < field_count; j++) {
566       const int number = upb_FieldDef_Number(upb_OneofDef_Field(o, j));
567 
568       if (!_upb_DescState_Grow(s, a)) return false;
569       s->ptr = upb_MtDataEncoder_PutOneofField(&s->e, s->ptr, number);
570     }
571   }
572 
573   return true;
574 }
575 
_upb_MessageDef_EncodeMessageSet(upb_DescState * s,const upb_MessageDef * m,upb_Arena * a)576 static bool _upb_MessageDef_EncodeMessageSet(upb_DescState* s,
577                                              const upb_MessageDef* m,
578                                              upb_Arena* a) {
579   s->ptr = upb_MtDataEncoder_EncodeMessageSet(&s->e, s->ptr);
580 
581   return true;
582 }
583 
upb_MessageDef_MiniDescriptorEncode(const upb_MessageDef * m,upb_Arena * a,upb_StringView * out)584 bool upb_MessageDef_MiniDescriptorEncode(const upb_MessageDef* m, upb_Arena* a,
585                                          upb_StringView* out) {
586   upb_DescState s;
587   _upb_DescState_Init(&s);
588 
589   if (!_upb_DescState_Grow(&s, a)) return false;
590 
591   if (upb_MessageDef_IsMapEntry(m)) {
592     if (!_upb_MessageDef_EncodeMap(&s, m, a)) return false;
593   } else if (UPB_DESC(MessageOptions_message_set_wire_format)(m->opts)) {
594     if (!_upb_MessageDef_EncodeMessageSet(&s, m, a)) return false;
595   } else {
596     if (!_upb_MessageDef_EncodeMessage(&s, m, a)) return false;
597   }
598 
599   if (!_upb_DescState_Grow(&s, a)) return false;
600   *s.ptr = '\0';
601 
602   out->data = s.buf;
603   out->size = s.ptr - s.buf;
604   return true;
605 }
606 
_upb_ReservedNames_New(upb_DefBuilder * ctx,int n,const upb_StringView * protos)607 static upb_StringView* _upb_ReservedNames_New(upb_DefBuilder* ctx, int n,
608                                               const upb_StringView* protos) {
609   upb_StringView* sv = _upb_DefBuilder_Alloc(ctx, sizeof(upb_StringView) * n);
610   for (size_t i = 0; i < n; i++) {
611     sv[i].data =
612         upb_strdup2(protos[i].data, protos[i].size, _upb_DefBuilder_Arena(ctx));
613     sv[i].size = protos[i].size;
614   }
615   return sv;
616 }
617 
create_msgdef(upb_DefBuilder * ctx,const char * prefix,const UPB_DESC (DescriptorProto)* msg_proto,const upb_MessageDef * containing_type,upb_MessageDef * m)618 static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
619                           const UPB_DESC(DescriptorProto) * msg_proto,
620                           const upb_MessageDef* containing_type,
621                           upb_MessageDef* m) {
622   const UPB_DESC(OneofDescriptorProto)* const* oneofs;
623   const UPB_DESC(FieldDescriptorProto)* const* fields;
624   const UPB_DESC(DescriptorProto_ExtensionRange)* const* ext_ranges;
625   const UPB_DESC(DescriptorProto_ReservedRange)* const* res_ranges;
626   const upb_StringView* res_names;
627   size_t n_oneof, n_field, n_enum, n_ext, n_msg;
628   size_t n_ext_range, n_res_range, n_res_name;
629   upb_StringView name;
630 
631   // Must happen before _upb_DefBuilder_Add()
632   m->file = _upb_DefBuilder_File(ctx);
633 
634   m->containing_type = containing_type;
635   m->is_sorted = true;
636 
637   name = UPB_DESC(DescriptorProto_name)(msg_proto);
638 
639   m->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
640   _upb_DefBuilder_Add(ctx, m->full_name, _upb_DefType_Pack(m, UPB_DEFTYPE_MSG));
641 
642   oneofs = UPB_DESC(DescriptorProto_oneof_decl)(msg_proto, &n_oneof);
643   fields = UPB_DESC(DescriptorProto_field)(msg_proto, &n_field);
644   ext_ranges =
645       UPB_DESC(DescriptorProto_extension_range)(msg_proto, &n_ext_range);
646   res_ranges =
647       UPB_DESC(DescriptorProto_reserved_range)(msg_proto, &n_res_range);
648   res_names = UPB_DESC(DescriptorProto_reserved_name)(msg_proto, &n_res_name);
649 
650   bool ok = upb_inttable_init(&m->itof, ctx->arena);
651   if (!ok) _upb_DefBuilder_OomErr(ctx);
652 
653   ok = upb_strtable_init(&m->ntof, n_oneof + n_field, ctx->arena);
654   if (!ok) _upb_DefBuilder_OomErr(ctx);
655 
656   UPB_DEF_SET_OPTIONS(m->opts, DescriptorProto, MessageOptions, msg_proto);
657 
658   m->oneof_count = n_oneof;
659   m->oneofs = _upb_OneofDefs_New(ctx, n_oneof, oneofs, m);
660 
661   m->field_count = n_field;
662   m->fields =
663       _upb_FieldDefs_New(ctx, n_field, fields, m->full_name, m, &m->is_sorted);
664 
665   // Message Sets may not contain fields.
666   if (UPB_UNLIKELY(UPB_DESC(MessageOptions_message_set_wire_format)(m->opts))) {
667     if (UPB_UNLIKELY(n_field > 0)) {
668       _upb_DefBuilder_Errf(ctx, "invalid message set (%s)", m->full_name);
669     }
670   }
671 
672   m->ext_range_count = n_ext_range;
673   m->ext_ranges = _upb_ExtensionRanges_New(ctx, n_ext_range, ext_ranges, m);
674 
675   m->res_range_count = n_res_range;
676   m->res_ranges =
677       _upb_MessageReservedRanges_New(ctx, n_res_range, res_ranges, m);
678 
679   m->res_name_count = n_res_name;
680   m->res_names = _upb_ReservedNames_New(ctx, n_res_name, res_names);
681 
682   const size_t synthetic_count = _upb_OneofDefs_Finalize(ctx, m);
683   m->real_oneof_count = m->oneof_count - synthetic_count;
684 
685   assign_msg_wellknowntype(m);
686   upb_inttable_compact(&m->itof, ctx->arena);
687 
688   const UPB_DESC(EnumDescriptorProto)* const* enums =
689       UPB_DESC(DescriptorProto_enum_type)(msg_proto, &n_enum);
690   m->nested_enum_count = n_enum;
691   m->nested_enums = _upb_EnumDefs_New(ctx, n_enum, enums, m);
692 
693   const UPB_DESC(FieldDescriptorProto)* const* exts =
694       UPB_DESC(DescriptorProto_extension)(msg_proto, &n_ext);
695   m->nested_ext_count = n_ext;
696   m->nested_exts = _upb_Extensions_New(ctx, n_ext, exts, m->full_name, m);
697 
698   const UPB_DESC(DescriptorProto)* const* msgs =
699       UPB_DESC(DescriptorProto_nested_type)(msg_proto, &n_msg);
700   m->nested_msg_count = n_msg;
701   m->nested_msgs = _upb_MessageDefs_New(ctx, n_msg, msgs, m);
702 }
703 
704 // Allocate and initialize an array of |n| message defs.
_upb_MessageDefs_New(upb_DefBuilder * ctx,int n,const UPB_DESC (DescriptorProto)* const * protos,const upb_MessageDef * containing_type)705 upb_MessageDef* _upb_MessageDefs_New(
706     upb_DefBuilder* ctx, int n, const UPB_DESC(DescriptorProto) * const* protos,
707     const upb_MessageDef* containing_type) {
708   _upb_DefType_CheckPadding(sizeof(upb_MessageDef));
709 
710   const char* name = containing_type ? containing_type->full_name
711                                      : _upb_FileDef_RawPackage(ctx->file);
712 
713   upb_MessageDef* m = _upb_DefBuilder_Alloc(ctx, sizeof(upb_MessageDef) * n);
714   for (int i = 0; i < n; i++) {
715     create_msgdef(ctx, name, protos[i], containing_type, &m[i]);
716   }
717   return m;
718 }
719