google-protobuf 3.25.5 → 4.28.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/convert.c +46 -18
  3. data/ext/google/protobuf_c/defs.c +368 -29
  4. data/ext/google/protobuf_c/extconf.rb +1 -1
  5. data/ext/google/protobuf_c/glue.c +16 -0
  6. data/ext/google/protobuf_c/map.c +74 -29
  7. data/ext/google/protobuf_c/map.h +7 -3
  8. data/ext/google/protobuf_c/message.c +106 -114
  9. data/ext/google/protobuf_c/message.h +2 -6
  10. data/ext/google/protobuf_c/protobuf.c +30 -15
  11. data/ext/google/protobuf_c/protobuf.h +3 -7
  12. data/ext/google/protobuf_c/repeated_field.c +58 -23
  13. data/ext/google/protobuf_c/repeated_field.h +6 -2
  14. data/ext/google/protobuf_c/ruby-upb.c +13047 -10836
  15. data/ext/google/protobuf_c/ruby-upb.h +11114 -9028
  16. data/ext/google/protobuf_c/shared_convert.c +10 -5
  17. data/ext/google/protobuf_c/shared_convert.h +2 -2
  18. data/ext/google/protobuf_c/shared_message.c +3 -31
  19. data/ext/google/protobuf_c/shared_message.h +0 -4
  20. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +467 -0
  21. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -8
  22. data/lib/google/protobuf/any_pb.rb +1 -22
  23. data/lib/google/protobuf/api_pb.rb +1 -24
  24. data/lib/google/protobuf/descriptor_pb.rb +3 -23
  25. data/lib/google/protobuf/duration_pb.rb +1 -22
  26. data/lib/google/protobuf/empty_pb.rb +1 -22
  27. data/lib/google/protobuf/ffi/descriptor.rb +4 -4
  28. data/lib/google/protobuf/ffi/descriptor_pool.rb +3 -1
  29. data/lib/google/protobuf/ffi/enum_descriptor.rb +3 -1
  30. data/lib/google/protobuf/ffi/ffi.rb +3 -6
  31. data/lib/google/protobuf/ffi/field_descriptor.rb +13 -2
  32. data/lib/google/protobuf/ffi/file_descriptor.rb +3 -13
  33. data/lib/google/protobuf/ffi/internal/arena.rb +0 -6
  34. data/lib/google/protobuf/ffi/internal/convert.rb +21 -30
  35. data/lib/google/protobuf/ffi/map.rb +50 -24
  36. data/lib/google/protobuf/ffi/message.rb +183 -64
  37. data/lib/google/protobuf/ffi/method_descriptor.rb +114 -0
  38. data/lib/google/protobuf/ffi/object_cache.rb +3 -3
  39. data/lib/google/protobuf/ffi/oneof_descriptor.rb +3 -1
  40. data/lib/google/protobuf/ffi/repeated_field.rb +47 -19
  41. data/lib/google/protobuf/ffi/service_descriptor.rb +107 -0
  42. data/lib/google/protobuf/field_mask_pb.rb +1 -22
  43. data/lib/google/protobuf/internal/object_cache.rb +99 -0
  44. data/lib/google/protobuf/plugin_pb.rb +2 -24
  45. data/lib/google/protobuf/repeated_field.rb +4 -5
  46. data/lib/google/protobuf/source_context_pb.rb +1 -22
  47. data/lib/google/protobuf/struct_pb.rb +1 -22
  48. data/lib/google/protobuf/timestamp_pb.rb +1 -22
  49. data/lib/google/protobuf/type_pb.rb +1 -24
  50. data/lib/google/protobuf/wrappers_pb.rb +1 -22
  51. data/lib/google/protobuf.rb +1 -1
  52. data/lib/google/protobuf_ffi.rb +3 -2
  53. data/lib/google/protobuf_native.rb +0 -1
  54. data/lib/google/tasks/ffi.rake +1 -3
  55. metadata +25 -12
  56. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +0 -92
  57. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +0 -157
  58. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +0 -170
  59. data/lib/google/protobuf/descriptor_dsl.rb +0 -465
  60. data/lib/google/protobuf/object_cache.rb +0 -97
@@ -164,11 +164,23 @@ static void Arena_free(void *data) {
164
164
  xfree(arena);
165
165
  }
166
166
 
167
+ static size_t Arena_memsize(const void *data) {
168
+ const Arena *arena = data;
169
+ size_t fused_count;
170
+ size_t memsize = upb_Arena_SpaceAllocated(arena->arena, &fused_count);
171
+ if (fused_count > 1) {
172
+ // If other arena were fused we attribute an equal
173
+ // share of memory usage to each one.
174
+ memsize /= fused_count;
175
+ }
176
+ return memsize + sizeof(Arena);
177
+ }
178
+
167
179
  static VALUE cArena;
168
180
 
169
181
  const rb_data_type_t Arena_type = {
170
182
  "Google::Protobuf::Internal::Arena",
171
- {Arena_mark, Arena_free, NULL},
183
+ {Arena_mark, Arena_free, Arena_memsize},
172
184
  .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
173
185
  };
174
186
 
@@ -209,15 +221,6 @@ void Arena_fuse(VALUE _arena, upb_Arena *other) {
209
221
 
210
222
  VALUE Arena_new() { return Arena_alloc(cArena); }
211
223
 
212
- void Arena_Pin(VALUE _arena, VALUE obj) {
213
- Arena *arena;
214
- TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
215
- if (arena->pinned_objs == Qnil) {
216
- RB_OBJ_WRITE(_arena, &arena->pinned_objs, rb_ary_new());
217
- }
218
- rb_ary_push(arena->pinned_objs, obj);
219
- }
220
-
221
224
  void Arena_register(VALUE module) {
222
225
  VALUE internal = rb_define_module_under(module, "Internal");
223
226
  VALUE klass = rb_define_class_under(internal, "Arena", rb_cObject);
@@ -241,16 +244,17 @@ static void ObjectCache_Init(VALUE protobuf) {
241
244
  item_try_add = rb_intern("try_add");
242
245
 
243
246
  rb_gc_register_address(&weak_obj_cache);
247
+ VALUE internal = rb_const_get(protobuf, rb_intern("Internal"));
244
248
  #if SIZEOF_LONG >= SIZEOF_VALUE
245
- VALUE cache_class = rb_const_get(protobuf, rb_intern("ObjectCache"));
249
+ VALUE cache_class = rb_const_get(internal, rb_intern("ObjectCache"));
246
250
  #else
247
- VALUE cache_class = rb_const_get(protobuf, rb_intern("LegacyObjectCache"));
251
+ VALUE cache_class = rb_const_get(internal, rb_intern("LegacyObjectCache"));
248
252
  #endif
249
253
 
250
254
  weak_obj_cache = rb_class_new_instance(0, NULL, cache_class);
251
- rb_const_set(protobuf, rb_intern("OBJECT_CACHE"), weak_obj_cache);
252
- rb_const_set(protobuf, rb_intern("SIZEOF_LONG"), INT2NUM(SIZEOF_LONG));
253
- rb_const_set(protobuf, rb_intern("SIZEOF_VALUE"), INT2NUM(SIZEOF_VALUE));
255
+ rb_const_set(internal, rb_intern("OBJECT_CACHE"), weak_obj_cache);
256
+ rb_const_set(internal, rb_intern("SIZEOF_LONG"), INT2NUM(SIZEOF_LONG));
257
+ rb_const_set(internal, rb_intern("SIZEOF_VALUE"), INT2NUM(SIZEOF_VALUE));
254
258
  }
255
259
 
256
260
  static VALUE ObjectCache_GetKey(const void *key) {
@@ -341,3 +345,14 @@ __attribute__((visibility("default"))) void Init_protobuf_c() {
341
345
  rb_define_singleton_method(protobuf, "deep_copy", Google_Protobuf_deep_copy,
342
346
  1);
343
347
  }
348
+
349
+ // -----------------------------------------------------------------------------
350
+ // Utilities
351
+ // -----------------------------------------------------------------------------
352
+
353
+ // Raises a Ruby error if val is frozen in Ruby or UPB.
354
+ void Protobuf_CheckNotFrozen(VALUE val, bool upb_frozen) {
355
+ if (RB_UNLIKELY(rb_obj_frozen_p(val)||upb_frozen)) {
356
+ rb_error_frozen_object(val);
357
+ }
358
+ }
@@ -50,13 +50,6 @@ upb_Arena* Arena_get(VALUE arena);
50
50
  // possible.
51
51
  void Arena_fuse(VALUE arena, upb_Arena* other);
52
52
 
53
- // Pins this Ruby object to the lifetime of this arena, so that as long as the
54
- // arena is alive this object will not be collected.
55
- //
56
- // We use this to guarantee that the "frozen" bit on the object will be
57
- // remembered, even if the user drops their reference to this precise object.
58
- void Arena_Pin(VALUE arena, VALUE obj);
59
-
60
53
  // -----------------------------------------------------------------------------
61
54
  // ObjectCache
62
55
  // -----------------------------------------------------------------------------
@@ -105,6 +98,9 @@ extern VALUE cTypeError;
105
98
  rb_bug("Assertion failed at %s:%d, expr: %s", __FILE__, __LINE__, #expr)
106
99
  #endif
107
100
 
101
+ // Raises a Ruby error if val is frozen in Ruby or upb_frozen is true.
102
+ void Protobuf_CheckNotFrozen(VALUE val, bool upb_frozen);
103
+
108
104
  #define PBRUBY_MAX(x, y) (((x) > (y)) ? (x) : (y))
109
105
 
110
106
  #define UPB_UNUSED(var) (void)var
@@ -44,8 +44,9 @@ static RepeatedField* ruby_to_RepeatedField(VALUE _self) {
44
44
  }
45
45
 
46
46
  static upb_Array* RepeatedField_GetMutable(VALUE _self) {
47
- rb_check_frozen(_self);
48
- return (upb_Array*)ruby_to_RepeatedField(_self)->array;
47
+ const upb_Array* array = ruby_to_RepeatedField(_self)->array;
48
+ Protobuf_CheckNotFrozen(_self, upb_Array_IsFrozen(array));
49
+ return (upb_Array*)array;
49
50
  }
50
51
 
51
52
  VALUE RepeatedField_alloc(VALUE klass) {
@@ -56,9 +57,32 @@ VALUE RepeatedField_alloc(VALUE klass) {
56
57
  return TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
57
58
  }
58
59
 
59
- VALUE RepeatedField_GetRubyWrapper(upb_Array* array, TypeInfo type_info,
60
+ VALUE RepeatedField_EmptyFrozen(const upb_FieldDef* f) {
61
+ PBRUBY_ASSERT(upb_FieldDef_IsRepeated(f));
62
+ VALUE val = ObjectCache_Get(f);
63
+
64
+ if (val == Qnil) {
65
+ val = RepeatedField_alloc(cRepeatedField);
66
+ RepeatedField* self;
67
+ TypedData_Get_Struct(val, RepeatedField, &RepeatedField_type, self);
68
+ self->arena = Arena_new();
69
+ TypeInfo type_info = TypeInfo_get(f);
70
+ self->array = upb_Array_New(Arena_get(self->arena), type_info.type);
71
+ self->type_info = type_info;
72
+ if (self->type_info.type == kUpb_CType_Message) {
73
+ self->type_class = Descriptor_DefToClass(type_info.def.msgdef);
74
+ }
75
+ val = ObjectCache_TryAdd(f, RepeatedField_freeze(val));
76
+ }
77
+ PBRUBY_ASSERT(RB_OBJ_FROZEN(val));
78
+ PBRUBY_ASSERT(upb_Array_IsFrozen(ruby_to_RepeatedField(val)->array));
79
+ return val;
80
+ }
81
+
82
+ VALUE RepeatedField_GetRubyWrapper(const upb_Array* array, TypeInfo type_info,
60
83
  VALUE arena) {
61
84
  PBRUBY_ASSERT(array);
85
+ PBRUBY_ASSERT(arena != Qnil);
62
86
  VALUE val = ObjectCache_Get(array);
63
87
 
64
88
  if (val == Qnil) {
@@ -78,7 +102,6 @@ VALUE RepeatedField_GetRubyWrapper(upb_Array* array, TypeInfo type_info,
78
102
  PBRUBY_ASSERT(ruby_to_RepeatedField(val)->type_info.def.msgdef ==
79
103
  type_info.def.msgdef);
80
104
  PBRUBY_ASSERT(ruby_to_RepeatedField(val)->array == array);
81
-
82
105
  return val;
83
106
  }
84
107
 
@@ -473,36 +496,47 @@ VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
473
496
 
474
497
  /*
475
498
  * call-seq:
476
- * RepeatedField.freeze => self
499
+ * RepeatedField.frozen? => bool
477
500
  *
478
- * Freezes the repeated field. We have to intercept this so we can pin the Ruby
479
- * object into memory so we don't forget it's frozen.
501
+ * Returns true if the repeated field is frozen in either Ruby or the underlying
502
+ * representation. Freezes the Ruby repeated field object if it is not already
503
+ * frozen in Ruby but it is frozen in the underlying representation.
480
504
  */
481
- static VALUE RepeatedField_freeze(VALUE _self) {
505
+ VALUE RepeatedField_frozen(VALUE _self) {
482
506
  RepeatedField* self = ruby_to_RepeatedField(_self);
483
- if (!RB_OBJ_FROZEN(_self)) {
484
- Arena_Pin(self->arena, _self);
485
- RB_OBJ_FREEZE(_self);
507
+ if (!upb_Array_IsFrozen(self->array)) {
508
+ PBRUBY_ASSERT(!RB_OBJ_FROZEN(_self));
509
+ return Qfalse;
486
510
  }
487
- return _self;
511
+
512
+ // Lazily freeze the Ruby wrapper.
513
+ if (!RB_OBJ_FROZEN(_self)) RB_OBJ_FREEZE(_self);
514
+ return Qtrue;
488
515
  }
489
516
 
490
517
  /*
491
- * Deep freezes the repeated field and values recursively.
492
- * Internal use only.
518
+ * call-seq:
519
+ * RepeatedField.freeze => self
520
+ *
521
+ * Freezes the repeated field object. We have to intercept this so we can freeze
522
+ * the underlying representation, not just the Ruby wrapper.
493
523
  */
494
- VALUE RepeatedField_internal_deep_freeze(VALUE _self) {
524
+ VALUE RepeatedField_freeze(VALUE _self) {
495
525
  RepeatedField* self = ruby_to_RepeatedField(_self);
496
- RepeatedField_freeze(_self);
497
- if (self->type_info.type == kUpb_CType_Message) {
498
- int size = upb_Array_Size(self->array);
499
- int i;
500
- for (i = 0; i < size; i++) {
501
- upb_MessageValue msgval = upb_Array_Get(self->array, i);
502
- VALUE val = Convert_UpbToRuby(msgval, self->type_info, self->arena);
503
- Message_internal_deep_freeze(val);
526
+ if (RB_OBJ_FROZEN(_self)) {
527
+ PBRUBY_ASSERT(upb_Array_IsFrozen(self->array));
528
+ return _self;
529
+ }
530
+
531
+ if (!upb_Array_IsFrozen(self->array)) {
532
+ if (self->type_info.type == kUpb_CType_Message) {
533
+ upb_Array_Freeze(RepeatedField_GetMutable(_self),
534
+ upb_MessageDef_MiniTable(self->type_info.def.msgdef));
535
+ } else {
536
+ upb_Array_Freeze(RepeatedField_GetMutable(_self), NULL);
504
537
  }
505
538
  }
539
+ RB_OBJ_FREEZE(_self);
506
540
  return _self;
507
541
  }
508
542
 
@@ -649,6 +683,7 @@ void RepeatedField_register(VALUE module) {
649
683
  rb_define_method(klass, "==", RepeatedField_eq, 1);
650
684
  rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0);
651
685
  rb_define_method(klass, "freeze", RepeatedField_freeze, 0);
686
+ rb_define_method(klass, "frozen?", RepeatedField_frozen, 0);
652
687
  rb_define_method(klass, "hash", RepeatedField_hash, 0);
653
688
  rb_define_method(klass, "+", RepeatedField_plus, 1);
654
689
  rb_define_method(klass, "concat", RepeatedField_concat, 1);
@@ -11,9 +11,13 @@
11
11
  #include "protobuf.h"
12
12
  #include "ruby-upb.h"
13
13
 
14
+ // Returns a frozen sentinel Ruby wrapper object for an empty upb_Array of the
15
+ // type specified by the field. Creates one if it doesn't exist.
16
+ VALUE RepeatedField_EmptyFrozen(const upb_FieldDef* f);
17
+
14
18
  // Returns a Ruby wrapper object for the given upb_Array, which will be created
15
19
  // if one does not exist already.
16
- VALUE RepeatedField_GetRubyWrapper(upb_Array* msg, TypeInfo type_info,
20
+ VALUE RepeatedField_GetRubyWrapper(const upb_Array* msg, TypeInfo type_info,
17
21
  VALUE arena);
18
22
 
19
23
  // Gets the underlying upb_Array for this Ruby RepeatedField object, which must
@@ -36,6 +40,6 @@ extern VALUE cRepeatedField;
36
40
  void RepeatedField_register(VALUE module);
37
41
 
38
42
  // Recursively freeze RepeatedField.
39
- VALUE RepeatedField_internal_deep_freeze(VALUE _self);
43
+ VALUE RepeatedField_freeze(VALUE _self);
40
44
 
41
45
  #endif // RUBY_PROTOBUF_REPEATED_FIELD_H_