protobuf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include "protobuf.h"
  31. #include <ruby/version.h>
  32. #include "defs.h"
  33. #include "map.h"
  34. #include "message.h"
  35. #include "repeated_field.h"
  36. VALUE cParseError;
  37. VALUE cTypeError;
  38. const upb_fielddef* map_field_key(const upb_fielddef* field) {
  39. const upb_msgdef *entry = upb_fielddef_msgsubdef(field);
  40. return upb_msgdef_itof(entry, 1);
  41. }
  42. const upb_fielddef* map_field_value(const upb_fielddef* field) {
  43. const upb_msgdef *entry = upb_fielddef_msgsubdef(field);
  44. return upb_msgdef_itof(entry, 2);
  45. }
  46. // -----------------------------------------------------------------------------
  47. // StringBuilder, for inspect
  48. // -----------------------------------------------------------------------------
  49. struct StringBuilder {
  50. size_t size;
  51. size_t cap;
  52. char *data;
  53. };
  54. typedef struct StringBuilder StringBuilder;
  55. static size_t StringBuilder_SizeOf(size_t cap) {
  56. return sizeof(StringBuilder) + cap;
  57. }
  58. StringBuilder* StringBuilder_New() {
  59. const size_t cap = 128;
  60. StringBuilder* builder = malloc(sizeof(*builder));
  61. builder->size = 0;
  62. builder->cap = cap;
  63. builder->data = malloc(builder->cap);
  64. return builder;
  65. }
  66. void StringBuilder_Free(StringBuilder* b) {
  67. free(b->data);
  68. free(b);
  69. }
  70. void StringBuilder_Printf(StringBuilder* b, const char *fmt, ...) {
  71. size_t have = b->cap - b->size;
  72. size_t n;
  73. va_list args;
  74. va_start(args, fmt);
  75. n = vsnprintf(&b->data[b->size], have, fmt, args);
  76. va_end(args);
  77. if (have <= n) {
  78. while (have <= n) {
  79. b->cap *= 2;
  80. have = b->cap - b->size;
  81. }
  82. b->data = realloc(b->data, StringBuilder_SizeOf(b->cap));
  83. va_start(args, fmt);
  84. n = vsnprintf(&b->data[b->size], have, fmt, args);
  85. va_end(args);
  86. PBRUBY_ASSERT(n < have);
  87. }
  88. b->size += n;
  89. }
  90. VALUE StringBuilder_ToRubyString(StringBuilder* b) {
  91. VALUE ret = rb_str_new(b->data, b->size);
  92. rb_enc_associate(ret, rb_utf8_encoding());
  93. return ret;
  94. }
  95. static void StringBuilder_PrintEnum(StringBuilder* b, int32_t val,
  96. const upb_enumdef* e) {
  97. const char *name = upb_enumdef_iton(e, val);
  98. if (name) {
  99. StringBuilder_Printf(b, ":%s", name);
  100. } else {
  101. StringBuilder_Printf(b, "%" PRId32, val);
  102. }
  103. }
  104. void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val,
  105. TypeInfo info) {
  106. switch (info.type) {
  107. case UPB_TYPE_BOOL:
  108. StringBuilder_Printf(b, "%s", val.bool_val ? "true" : "false");
  109. break;
  110. case UPB_TYPE_FLOAT: {
  111. VALUE str = rb_inspect(DBL2NUM(val.float_val));
  112. StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
  113. break;
  114. }
  115. case UPB_TYPE_DOUBLE: {
  116. VALUE str = rb_inspect(DBL2NUM(val.double_val));
  117. StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
  118. break;
  119. }
  120. case UPB_TYPE_INT32:
  121. StringBuilder_Printf(b, "%" PRId32, val.int32_val);
  122. break;
  123. case UPB_TYPE_UINT32:
  124. StringBuilder_Printf(b, "%" PRIu32, val.uint32_val);
  125. break;
  126. case UPB_TYPE_INT64:
  127. StringBuilder_Printf(b, "%" PRId64, val.int64_val);
  128. break;
  129. case UPB_TYPE_UINT64:
  130. StringBuilder_Printf(b, "%" PRIu64, val.uint64_val);
  131. break;
  132. case UPB_TYPE_STRING:
  133. StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size, val.str_val.data);
  134. break;
  135. case UPB_TYPE_BYTES:
  136. StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size, val.str_val.data);
  137. break;
  138. case UPB_TYPE_ENUM:
  139. StringBuilder_PrintEnum(b, val.int32_val, info.def.enumdef);
  140. break;
  141. case UPB_TYPE_MESSAGE:
  142. Message_PrintMessage(b, val.msg_val, info.def.msgdef);
  143. break;
  144. }
  145. }
  146. // -----------------------------------------------------------------------------
  147. // Arena
  148. // -----------------------------------------------------------------------------
  149. typedef struct {
  150. upb_arena *arena;
  151. VALUE pinned_objs;
  152. } Arena;
  153. static void Arena_mark(void *data) {
  154. Arena *arena = data;
  155. rb_gc_mark(arena->pinned_objs);
  156. }
  157. static void Arena_free(void *data) {
  158. Arena *arena = data;
  159. upb_arena_free(arena->arena);
  160. }
  161. static VALUE cArena;
  162. const rb_data_type_t Arena_type = {
  163. "Google::Protobuf::Internal::Arena",
  164. { Arena_mark, Arena_free, NULL },
  165. .flags = RUBY_TYPED_FREE_IMMEDIATELY,
  166. };
  167. static VALUE Arena_alloc(VALUE klass) {
  168. Arena *arena = ALLOC(Arena);
  169. arena->arena = upb_arena_new();
  170. arena->pinned_objs = Qnil;
  171. return TypedData_Wrap_Struct(klass, &Arena_type, arena);
  172. }
  173. upb_arena *Arena_get(VALUE _arena) {
  174. Arena *arena;
  175. TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
  176. return arena->arena;
  177. }
  178. VALUE Arena_new() {
  179. return Arena_alloc(cArena);
  180. }
  181. void Arena_Pin(VALUE _arena, VALUE obj) {
  182. Arena *arena;
  183. TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
  184. if (arena->pinned_objs == Qnil) {
  185. arena->pinned_objs = rb_ary_new();
  186. }
  187. rb_ary_push(arena->pinned_objs, obj);
  188. }
  189. void Arena_register(VALUE module) {
  190. VALUE internal = rb_define_module_under(module, "Internal");
  191. VALUE klass = rb_define_class_under(internal, "Arena", rb_cObject);
  192. rb_define_alloc_func(klass, Arena_alloc);
  193. rb_gc_register_address(&cArena);
  194. cArena = klass;
  195. }
  196. // -----------------------------------------------------------------------------
  197. // Object Cache
  198. // -----------------------------------------------------------------------------
  199. // A pointer -> Ruby Object cache that keeps references to Ruby wrapper
  200. // objects. This allows us to look up any Ruby wrapper object by the address
  201. // of the object it is wrapping. That way we can avoid ever creating two
  202. // different wrapper objects for the same C object, which saves memory and
  203. // preserves object identity.
  204. //
  205. // We use WeakMap for the cache. For Ruby <2.7 we also need a secondary Hash
  206. // to store WeakMap keys because Ruby <2.7 WeakMap doesn't allow non-finalizable
  207. // keys.
  208. #if RUBY_API_VERSION_CODE >= 20700
  209. #define USE_SECONDARY_MAP 0
  210. #else
  211. #define USE_SECONDARY_MAP 1
  212. #endif
  213. #if USE_SECONDARY_MAP
  214. // Maps Numeric -> Object. The object is then used as a key into the WeakMap.
  215. // This is needed for Ruby <2.7 where a number cannot be a key to WeakMap.
  216. // The object is used only for its identity; it does not contain any data.
  217. VALUE secondary_map = Qnil;
  218. static void SecondaryMap_Init() {
  219. rb_gc_register_address(&secondary_map);
  220. secondary_map = rb_hash_new();
  221. }
  222. static VALUE SecondaryMap_Get(VALUE key) {
  223. VALUE ret = rb_hash_lookup(secondary_map, key);
  224. if (ret == Qnil) {
  225. ret = rb_eval_string("Object.new");
  226. rb_hash_aset(secondary_map, key, ret);
  227. }
  228. return ret;
  229. }
  230. #endif
  231. static VALUE ObjectCache_GetKey(const void* key) {
  232. char buf[sizeof(key)];
  233. memcpy(&buf, &key, sizeof(key));
  234. intptr_t key_int = (intptr_t)key;
  235. PBRUBY_ASSERT((key_int & 3) == 0);
  236. VALUE ret = LL2NUM(key_int >> 2);
  237. #if USE_SECONDARY_MAP
  238. ret = SecondaryMap_Get(ret);
  239. #endif
  240. return ret;
  241. }
  242. // Public ObjectCache API.
  243. VALUE weak_obj_cache = Qnil;
  244. ID item_get;
  245. ID item_set;
  246. static void ObjectCache_Init() {
  247. rb_gc_register_address(&weak_obj_cache);
  248. VALUE klass = rb_eval_string("ObjectSpace::WeakMap");
  249. weak_obj_cache = rb_class_new_instance(0, NULL, klass);
  250. item_get = rb_intern("[]");
  251. item_set = rb_intern("[]=");
  252. #if USE_SECONDARY_MAP
  253. SecondaryMap_Init();
  254. #endif
  255. }
  256. void ObjectCache_Add(const void* key, VALUE val) {
  257. PBRUBY_ASSERT(ObjectCache_Get(key) == Qnil);
  258. VALUE key_rb = ObjectCache_GetKey(key);
  259. rb_funcall(weak_obj_cache, item_set, 2, key_rb, val);
  260. PBRUBY_ASSERT(ObjectCache_Get(key) == val);
  261. }
  262. // Returns the cached object for this key, if any. Otherwise returns Qnil.
  263. VALUE ObjectCache_Get(const void* key) {
  264. VALUE key_rb = ObjectCache_GetKey(key);
  265. return rb_funcall(weak_obj_cache, item_get, 1, key_rb);
  266. }
  267. /*
  268. * call-seq:
  269. * Google::Protobuf.discard_unknown(msg)
  270. *
  271. * Discard unknown fields in the given message object and recursively discard
  272. * unknown fields in submessages.
  273. */
  274. static VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb) {
  275. const upb_msgdef *m;
  276. upb_msg *msg = Message_GetMutable(msg_rb, &m);
  277. if (!upb_msg_discardunknown(msg, m, 128)) {
  278. rb_raise(rb_eRuntimeError, "Messages nested too deeply.");
  279. }
  280. return Qnil;
  281. }
  282. /*
  283. * call-seq:
  284. * Google::Protobuf.deep_copy(obj) => copy_of_obj
  285. *
  286. * Performs a deep copy of a RepeatedField instance, a Map instance, or a
  287. * message object, recursively copying its members.
  288. */
  289. VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
  290. VALUE klass = CLASS_OF(obj);
  291. if (klass == cRepeatedField) {
  292. return RepeatedField_deep_copy(obj);
  293. } else if (klass == cMap) {
  294. return Map_deep_copy(obj);
  295. } else {
  296. VALUE new_arena_rb = Arena_new();
  297. upb_arena *new_arena = Arena_get(new_arena_rb);
  298. const upb_msgdef *m;
  299. const upb_msg *msg = Message_Get(obj, &m);
  300. upb_msg* new_msg = Message_deep_copy(msg, m, new_arena);
  301. return Message_GetRubyWrapper(new_msg, m, new_arena_rb);
  302. }
  303. }
  304. // -----------------------------------------------------------------------------
  305. // Initialization/entry point.
  306. // -----------------------------------------------------------------------------
  307. // This must be named "Init_protobuf_c" because the Ruby module is named
  308. // "protobuf_c" -- the VM looks for this symbol in our .so.
  309. __attribute__ ((visibility ("default")))
  310. void Init_protobuf_c() {
  311. ObjectCache_Init();
  312. VALUE google = rb_define_module("Google");
  313. VALUE protobuf = rb_define_module_under(google, "Protobuf");
  314. Arena_register(protobuf);
  315. Defs_register(protobuf);
  316. RepeatedField_register(protobuf);
  317. Map_register(protobuf);
  318. Message_register(protobuf);
  319. cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
  320. rb_gc_register_mark_object(cParseError);
  321. cTypeError = rb_const_get(protobuf, rb_intern("TypeError"));
  322. rb_gc_register_mark_object(cTypeError);
  323. rb_define_singleton_method(protobuf, "discard_unknown",
  324. Google_Protobuf_discard_unknown, 1);
  325. rb_define_singleton_method(protobuf, "deep_copy",
  326. Google_Protobuf_deep_copy, 1);
  327. }