storage.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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 <math.h>
  32. #include <ruby/encoding.h>
  33. // -----------------------------------------------------------------------------
  34. // Ruby <-> native slot management.
  35. // -----------------------------------------------------------------------------
  36. #define DEREF(memory, type) *(type*)(memory)
  37. size_t native_slot_size(upb_fieldtype_t type) {
  38. switch (type) {
  39. case UPB_TYPE_FLOAT: return 4;
  40. case UPB_TYPE_DOUBLE: return 8;
  41. case UPB_TYPE_BOOL: return 1;
  42. case UPB_TYPE_STRING: return sizeof(VALUE);
  43. case UPB_TYPE_BYTES: return sizeof(VALUE);
  44. case UPB_TYPE_MESSAGE: return sizeof(VALUE);
  45. case UPB_TYPE_ENUM: return 4;
  46. case UPB_TYPE_INT32: return 4;
  47. case UPB_TYPE_INT64: return 8;
  48. case UPB_TYPE_UINT32: return 4;
  49. case UPB_TYPE_UINT64: return 8;
  50. default: return 0;
  51. }
  52. }
  53. static VALUE value_from_default(const upb_fielddef *field) {
  54. switch (upb_fielddef_type(field)) {
  55. case UPB_TYPE_FLOAT: return DBL2NUM(upb_fielddef_defaultfloat(field));
  56. case UPB_TYPE_DOUBLE: return DBL2NUM(upb_fielddef_defaultdouble(field));
  57. case UPB_TYPE_BOOL:
  58. return upb_fielddef_defaultbool(field) ? Qtrue : Qfalse;
  59. case UPB_TYPE_MESSAGE: return Qnil;
  60. case UPB_TYPE_ENUM: {
  61. const upb_enumdef *enumdef = upb_fielddef_enumsubdef(field);
  62. int32_t num = upb_fielddef_defaultint32(field);
  63. const char *label = upb_enumdef_iton(enumdef, num);
  64. if (label) {
  65. return ID2SYM(rb_intern(label));
  66. } else {
  67. return INT2NUM(num);
  68. }
  69. }
  70. case UPB_TYPE_INT32: return INT2NUM(upb_fielddef_defaultint32(field));
  71. case UPB_TYPE_INT64: return LL2NUM(upb_fielddef_defaultint64(field));;
  72. case UPB_TYPE_UINT32: return UINT2NUM(upb_fielddef_defaultuint32(field));
  73. case UPB_TYPE_UINT64: return ULL2NUM(upb_fielddef_defaultuint64(field));
  74. case UPB_TYPE_STRING:
  75. case UPB_TYPE_BYTES: {
  76. size_t size;
  77. const char *str = upb_fielddef_defaultstr(field, &size);
  78. return rb_str_new(str, size);
  79. }
  80. default: return Qnil;
  81. }
  82. }
  83. static bool is_ruby_num(VALUE value) {
  84. return (TYPE(value) == T_FLOAT ||
  85. TYPE(value) == T_FIXNUM ||
  86. TYPE(value) == T_BIGNUM);
  87. }
  88. void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE val) {
  89. if (!is_ruby_num(val)) {
  90. rb_raise(rb_eTypeError, "Expected number type for integral field.");
  91. }
  92. // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
  93. // bound; we just need to do precision checks (i.e., disallow rounding) and
  94. // check for < 0 on unsigned types.
  95. if (TYPE(val) == T_FLOAT) {
  96. double dbl_val = NUM2DBL(val);
  97. if (floor(dbl_val) != dbl_val) {
  98. rb_raise(rb_eRangeError,
  99. "Non-integral floating point value assigned to integer field.");
  100. }
  101. }
  102. if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
  103. if (NUM2DBL(val) < 0) {
  104. rb_raise(rb_eRangeError,
  105. "Assigning negative value to unsigned integer field.");
  106. }
  107. }
  108. }
  109. VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value) {
  110. rb_encoding* desired_encoding = (type == UPB_TYPE_STRING) ?
  111. kRubyStringUtf8Encoding : kRubyString8bitEncoding;
  112. VALUE desired_encoding_value = rb_enc_from_encoding(desired_encoding);
  113. // Note: this will not duplicate underlying string data unless necessary.
  114. value = rb_str_encode(value, desired_encoding_value, 0, Qnil);
  115. if (type == UPB_TYPE_STRING &&
  116. rb_enc_str_coderange(value) == ENC_CODERANGE_BROKEN) {
  117. rb_raise(rb_eEncodingError, "String is invalid UTF-8");
  118. }
  119. // Ensure the data remains valid. Since we called #encode a moment ago,
  120. // this does not freeze the string the user assigned.
  121. rb_obj_freeze(value);
  122. return value;
  123. }
  124. void native_slot_set(upb_fieldtype_t type, VALUE type_class,
  125. void* memory, VALUE value) {
  126. native_slot_set_value_and_case(type, type_class, memory, value, NULL, 0);
  127. }
  128. void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class,
  129. void* memory, VALUE value,
  130. uint32_t* case_memory,
  131. uint32_t case_number) {
  132. // Note that in order to atomically change the value in memory and the case
  133. // value (w.r.t. Ruby VM calls), we must set the value at |memory| only after
  134. // all Ruby VM calls are complete. The case is then set at the bottom of this
  135. // function.
  136. switch (type) {
  137. case UPB_TYPE_FLOAT:
  138. if (!is_ruby_num(value)) {
  139. rb_raise(rb_eTypeError, "Expected number type for float field.");
  140. }
  141. DEREF(memory, float) = NUM2DBL(value);
  142. break;
  143. case UPB_TYPE_DOUBLE:
  144. if (!is_ruby_num(value)) {
  145. rb_raise(rb_eTypeError, "Expected number type for double field.");
  146. }
  147. DEREF(memory, double) = NUM2DBL(value);
  148. break;
  149. case UPB_TYPE_BOOL: {
  150. int8_t val = -1;
  151. if (value == Qtrue) {
  152. val = 1;
  153. } else if (value == Qfalse) {
  154. val = 0;
  155. } else {
  156. rb_raise(rb_eTypeError, "Invalid argument for boolean field.");
  157. }
  158. DEREF(memory, int8_t) = val;
  159. break;
  160. }
  161. case UPB_TYPE_STRING:
  162. if (CLASS_OF(value) == rb_cSymbol) {
  163. value = rb_funcall(value, rb_intern("to_s"), 0, NULL);
  164. } else if (CLASS_OF(value) != rb_cString) {
  165. rb_raise(rb_eTypeError, "Invalid argument for string field.");
  166. }
  167. DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
  168. break;
  169. case UPB_TYPE_BYTES: {
  170. if (CLASS_OF(value) != rb_cString) {
  171. rb_raise(rb_eTypeError, "Invalid argument for string field.");
  172. }
  173. DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
  174. break;
  175. }
  176. case UPB_TYPE_MESSAGE: {
  177. if (CLASS_OF(value) == CLASS_OF(Qnil)) {
  178. value = Qnil;
  179. } else if (CLASS_OF(value) != type_class) {
  180. rb_raise(rb_eTypeError,
  181. "Invalid type %s to assign to submessage field.",
  182. rb_class2name(CLASS_OF(value)));
  183. }
  184. DEREF(memory, VALUE) = value;
  185. break;
  186. }
  187. case UPB_TYPE_ENUM: {
  188. int32_t int_val = 0;
  189. if (TYPE(value) == T_STRING) {
  190. value = rb_funcall(value, rb_intern("to_sym"), 0, NULL);
  191. } else if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
  192. rb_raise(rb_eTypeError,
  193. "Expected number or symbol type for enum field.");
  194. }
  195. if (TYPE(value) == T_SYMBOL) {
  196. // Ensure that the given symbol exists in the enum module.
  197. VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value);
  198. if (lookup == Qnil) {
  199. rb_raise(rb_eRangeError, "Unknown symbol value for enum field.");
  200. } else {
  201. int_val = NUM2INT(lookup);
  202. }
  203. } else {
  204. native_slot_check_int_range_precision(UPB_TYPE_INT32, value);
  205. int_val = NUM2INT(value);
  206. }
  207. DEREF(memory, int32_t) = int_val;
  208. break;
  209. }
  210. case UPB_TYPE_INT32:
  211. case UPB_TYPE_INT64:
  212. case UPB_TYPE_UINT32:
  213. case UPB_TYPE_UINT64:
  214. native_slot_check_int_range_precision(type, value);
  215. switch (type) {
  216. case UPB_TYPE_INT32:
  217. DEREF(memory, int32_t) = NUM2INT(value);
  218. break;
  219. case UPB_TYPE_INT64:
  220. DEREF(memory, int64_t) = NUM2LL(value);
  221. break;
  222. case UPB_TYPE_UINT32:
  223. DEREF(memory, uint32_t) = NUM2UINT(value);
  224. break;
  225. case UPB_TYPE_UINT64:
  226. DEREF(memory, uint64_t) = NUM2ULL(value);
  227. break;
  228. default:
  229. break;
  230. }
  231. break;
  232. default:
  233. break;
  234. }
  235. if (case_memory != NULL) {
  236. *case_memory = case_number;
  237. }
  238. }
  239. VALUE native_slot_get(upb_fieldtype_t type,
  240. VALUE type_class,
  241. const void* memory) {
  242. switch (type) {
  243. case UPB_TYPE_FLOAT:
  244. return DBL2NUM(DEREF(memory, float));
  245. case UPB_TYPE_DOUBLE:
  246. return DBL2NUM(DEREF(memory, double));
  247. case UPB_TYPE_BOOL:
  248. return DEREF(memory, int8_t) ? Qtrue : Qfalse;
  249. case UPB_TYPE_STRING:
  250. case UPB_TYPE_BYTES:
  251. case UPB_TYPE_MESSAGE:
  252. return DEREF(memory, VALUE);
  253. case UPB_TYPE_ENUM: {
  254. int32_t val = DEREF(memory, int32_t);
  255. VALUE symbol = enum_lookup(type_class, INT2NUM(val));
  256. if (symbol == Qnil) {
  257. return INT2NUM(val);
  258. } else {
  259. return symbol;
  260. }
  261. }
  262. case UPB_TYPE_INT32:
  263. return INT2NUM(DEREF(memory, int32_t));
  264. case UPB_TYPE_INT64:
  265. return LL2NUM(DEREF(memory, int64_t));
  266. case UPB_TYPE_UINT32:
  267. return UINT2NUM(DEREF(memory, uint32_t));
  268. case UPB_TYPE_UINT64:
  269. return ULL2NUM(DEREF(memory, uint64_t));
  270. default:
  271. return Qnil;
  272. }
  273. }
  274. void native_slot_init(upb_fieldtype_t type, void* memory) {
  275. switch (type) {
  276. case UPB_TYPE_FLOAT:
  277. DEREF(memory, float) = 0.0;
  278. break;
  279. case UPB_TYPE_DOUBLE:
  280. DEREF(memory, double) = 0.0;
  281. break;
  282. case UPB_TYPE_BOOL:
  283. DEREF(memory, int8_t) = 0;
  284. break;
  285. case UPB_TYPE_STRING:
  286. case UPB_TYPE_BYTES:
  287. DEREF(memory, VALUE) = rb_str_new2("");
  288. rb_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) ?
  289. kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  290. break;
  291. case UPB_TYPE_MESSAGE:
  292. DEREF(memory, VALUE) = Qnil;
  293. break;
  294. case UPB_TYPE_ENUM:
  295. case UPB_TYPE_INT32:
  296. DEREF(memory, int32_t) = 0;
  297. break;
  298. case UPB_TYPE_INT64:
  299. DEREF(memory, int64_t) = 0;
  300. break;
  301. case UPB_TYPE_UINT32:
  302. DEREF(memory, uint32_t) = 0;
  303. break;
  304. case UPB_TYPE_UINT64:
  305. DEREF(memory, uint64_t) = 0;
  306. break;
  307. default:
  308. break;
  309. }
  310. }
  311. void native_slot_mark(upb_fieldtype_t type, void* memory) {
  312. switch (type) {
  313. case UPB_TYPE_STRING:
  314. case UPB_TYPE_BYTES:
  315. case UPB_TYPE_MESSAGE:
  316. rb_gc_mark(DEREF(memory, VALUE));
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
  323. memcpy(to, from, native_slot_size(type));
  324. }
  325. void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from) {
  326. switch (type) {
  327. case UPB_TYPE_STRING:
  328. case UPB_TYPE_BYTES: {
  329. VALUE from_val = DEREF(from, VALUE);
  330. DEREF(to, VALUE) = (from_val != Qnil) ?
  331. rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
  332. break;
  333. }
  334. case UPB_TYPE_MESSAGE: {
  335. VALUE from_val = DEREF(from, VALUE);
  336. DEREF(to, VALUE) = (from_val != Qnil) ?
  337. Message_deep_copy(from_val) : Qnil;
  338. break;
  339. }
  340. default:
  341. memcpy(to, from, native_slot_size(type));
  342. }
  343. }
  344. bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2) {
  345. switch (type) {
  346. case UPB_TYPE_STRING:
  347. case UPB_TYPE_BYTES:
  348. case UPB_TYPE_MESSAGE: {
  349. VALUE val1 = DEREF(mem1, VALUE);
  350. VALUE val2 = DEREF(mem2, VALUE);
  351. VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
  352. return ret == Qtrue;
  353. }
  354. default:
  355. return !memcmp(mem1, mem2, native_slot_size(type));
  356. }
  357. }
  358. // -----------------------------------------------------------------------------
  359. // Map field utilities.
  360. // -----------------------------------------------------------------------------
  361. const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) {
  362. const upb_msgdef* subdef;
  363. if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
  364. upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
  365. return NULL;
  366. }
  367. subdef = upb_fielddef_msgsubdef(field);
  368. return upb_msgdef_mapentry(subdef) ? subdef : NULL;
  369. }
  370. const upb_msgdef *map_entry_msgdef(const upb_fielddef* field) {
  371. const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
  372. assert(subdef);
  373. return subdef;
  374. }
  375. bool is_map_field(const upb_fielddef *field) {
  376. return tryget_map_entry_msgdef(field) != NULL;
  377. }
  378. const upb_fielddef* map_field_key(const upb_fielddef* field) {
  379. const upb_msgdef* subdef = map_entry_msgdef(field);
  380. return map_entry_key(subdef);
  381. }
  382. const upb_fielddef* map_field_value(const upb_fielddef* field) {
  383. const upb_msgdef* subdef = map_entry_msgdef(field);
  384. return map_entry_value(subdef);
  385. }
  386. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
  387. const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
  388. assert(key_field != NULL);
  389. return key_field;
  390. }
  391. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
  392. const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
  393. assert(value_field != NULL);
  394. return value_field;
  395. }
  396. // -----------------------------------------------------------------------------
  397. // Memory layout management.
  398. // -----------------------------------------------------------------------------
  399. static size_t align_up_to(size_t offset, size_t granularity) {
  400. // Granularity must be a power of two.
  401. return (offset + granularity - 1) & ~(granularity - 1);
  402. }
  403. MessageLayout* create_layout(const upb_msgdef* msgdef) {
  404. MessageLayout* layout = ALLOC(MessageLayout);
  405. int nfields = upb_msgdef_numfields(msgdef);
  406. upb_msg_field_iter it;
  407. upb_msg_oneof_iter oit;
  408. size_t off = 0;
  409. layout->fields = ALLOC_N(MessageField, nfields);
  410. for (upb_msg_field_begin(&it, msgdef);
  411. !upb_msg_field_done(&it);
  412. upb_msg_field_next(&it)) {
  413. const upb_fielddef* field = upb_msg_iter_field(&it);
  414. size_t field_size;
  415. if (upb_fielddef_containingoneof(field)) {
  416. // Oneofs are handled separately below.
  417. continue;
  418. }
  419. // Allocate |field_size| bytes for this field in the layout.
  420. field_size = 0;
  421. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  422. field_size = sizeof(VALUE);
  423. } else {
  424. field_size = native_slot_size(upb_fielddef_type(field));
  425. }
  426. // Align current offset up to |size| granularity.
  427. off = align_up_to(off, field_size);
  428. layout->fields[upb_fielddef_index(field)].offset = off;
  429. layout->fields[upb_fielddef_index(field)].case_offset =
  430. MESSAGE_FIELD_NO_CASE;
  431. off += field_size;
  432. }
  433. // Handle oneofs now -- we iterate over oneofs specifically and allocate only
  434. // one slot per oneof.
  435. //
  436. // We assign all value slots first, then pack the 'case' fields at the end,
  437. // since in the common case (modern 64-bit platform) these are 8 bytes and 4
  438. // bytes respectively and we want to avoid alignment overhead.
  439. //
  440. // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
  441. // space for oneof cases is conceptually as wide as field tag numbers. In
  442. // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
  443. // members (8 or 16 bits respectively), so conceivably we could assign
  444. // consecutive case numbers and then pick a smaller oneof case slot size, but
  445. // the complexity to implement this indirection is probably not worthwhile.
  446. for (upb_msg_oneof_begin(&oit, msgdef);
  447. !upb_msg_oneof_done(&oit);
  448. upb_msg_oneof_next(&oit)) {
  449. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  450. upb_oneof_iter fit;
  451. // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
  452. // all fields.
  453. size_t field_size = NATIVE_SLOT_MAX_SIZE;
  454. // Align the offset.
  455. off = align_up_to(off, field_size);
  456. // Assign all fields in the oneof this same offset.
  457. for (upb_oneof_begin(&fit, oneof);
  458. !upb_oneof_done(&fit);
  459. upb_oneof_next(&fit)) {
  460. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  461. layout->fields[upb_fielddef_index(field)].offset = off;
  462. }
  463. off += field_size;
  464. }
  465. // Now the case fields.
  466. for (upb_msg_oneof_begin(&oit, msgdef);
  467. !upb_msg_oneof_done(&oit);
  468. upb_msg_oneof_next(&oit)) {
  469. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  470. upb_oneof_iter fit;
  471. size_t field_size = sizeof(uint32_t);
  472. // Align the offset.
  473. off = (off + field_size - 1) & ~(field_size - 1);
  474. // Assign all fields in the oneof this same offset.
  475. for (upb_oneof_begin(&fit, oneof);
  476. !upb_oneof_done(&fit);
  477. upb_oneof_next(&fit)) {
  478. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  479. layout->fields[upb_fielddef_index(field)].case_offset = off;
  480. }
  481. off += field_size;
  482. }
  483. layout->size = off;
  484. layout->msgdef = msgdef;
  485. upb_msgdef_ref(layout->msgdef, &layout->msgdef);
  486. return layout;
  487. }
  488. void free_layout(MessageLayout* layout) {
  489. xfree(layout->fields);
  490. upb_msgdef_unref(layout->msgdef, &layout->msgdef);
  491. xfree(layout);
  492. }
  493. VALUE field_type_class(const upb_fielddef* field) {
  494. VALUE type_class = Qnil;
  495. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  496. VALUE submsgdesc =
  497. get_def_obj(upb_fielddef_subdef(field));
  498. type_class = Descriptor_msgclass(submsgdesc);
  499. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  500. VALUE subenumdesc =
  501. get_def_obj(upb_fielddef_subdef(field));
  502. type_class = EnumDescriptor_enummodule(subenumdesc);
  503. }
  504. return type_class;
  505. }
  506. static void* slot_memory(MessageLayout* layout,
  507. const void* storage,
  508. const upb_fielddef* field) {
  509. return ((uint8_t *)storage) +
  510. layout->fields[upb_fielddef_index(field)].offset;
  511. }
  512. static uint32_t* slot_oneof_case(MessageLayout* layout,
  513. const void* storage,
  514. const upb_fielddef* field) {
  515. return (uint32_t *)(((uint8_t *)storage) +
  516. layout->fields[upb_fielddef_index(field)].case_offset);
  517. }
  518. VALUE layout_get(MessageLayout* layout,
  519. const void* storage,
  520. const upb_fielddef* field) {
  521. void* memory = slot_memory(layout, storage, field);
  522. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  523. if (upb_fielddef_containingoneof(field)) {
  524. if (*oneof_case != upb_fielddef_number(field)) {
  525. return value_from_default(field);
  526. }
  527. return native_slot_get(upb_fielddef_type(field),
  528. field_type_class(field),
  529. memory);
  530. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  531. return *((VALUE *)memory);
  532. } else {
  533. return native_slot_get(upb_fielddef_type(field),
  534. field_type_class(field),
  535. memory);
  536. }
  537. }
  538. static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
  539. RepeatedField* self;
  540. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  541. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  542. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  543. rb_raise(rb_eTypeError, "Expected repeated field array");
  544. }
  545. self = ruby_to_RepeatedField(val);
  546. if (self->field_type != upb_fielddef_type(field)) {
  547. rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
  548. }
  549. if (self->field_type == UPB_TYPE_MESSAGE) {
  550. if (self->field_type_class !=
  551. Descriptor_msgclass(get_def_obj(upb_fielddef_subdef(field)))) {
  552. rb_raise(rb_eTypeError,
  553. "Repeated field array has wrong message class");
  554. }
  555. }
  556. if (self->field_type == UPB_TYPE_ENUM) {
  557. if (self->field_type_class !=
  558. EnumDescriptor_enummodule(get_def_obj(upb_fielddef_subdef(field)))) {
  559. rb_raise(rb_eTypeError,
  560. "Repeated field array has wrong enum class");
  561. }
  562. }
  563. }
  564. static void check_map_field_type(VALUE val, const upb_fielddef* field) {
  565. const upb_fielddef* key_field = map_field_key(field);
  566. const upb_fielddef* value_field = map_field_value(field);
  567. Map* self;
  568. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  569. RTYPEDDATA_TYPE(val) != &Map_type) {
  570. rb_raise(rb_eTypeError, "Expected Map instance");
  571. }
  572. self = ruby_to_Map(val);
  573. if (self->key_type != upb_fielddef_type(key_field)) {
  574. rb_raise(rb_eTypeError, "Map key type does not match field's key type");
  575. }
  576. if (self->value_type != upb_fielddef_type(value_field)) {
  577. rb_raise(rb_eTypeError, "Map value type does not match field's value type");
  578. }
  579. if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE ||
  580. upb_fielddef_type(value_field) == UPB_TYPE_ENUM) {
  581. if (self->value_type_class !=
  582. get_def_obj(upb_fielddef_subdef(value_field))) {
  583. rb_raise(rb_eTypeError,
  584. "Map value type has wrong message/enum class");
  585. }
  586. }
  587. }
  588. void layout_set(MessageLayout* layout,
  589. void* storage,
  590. const upb_fielddef* field,
  591. VALUE val) {
  592. void* memory = slot_memory(layout, storage, field);
  593. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  594. if (upb_fielddef_containingoneof(field)) {
  595. if (val == Qnil) {
  596. // Assigning nil to a oneof field clears the oneof completely.
  597. *oneof_case = ONEOF_CASE_NONE;
  598. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  599. } else {
  600. // The transition between field types for a single oneof (union) slot is
  601. // somewhat complex because we need to ensure that a GC triggered at any
  602. // point by a call into the Ruby VM sees a valid state for this field and
  603. // does not either go off into the weeds (following what it thinks is a
  604. // VALUE but is actually a different field type) or miss an object (seeing
  605. // what it thinks is a primitive field but is actually a VALUE for the new
  606. // field type).
  607. //
  608. // In order for the transition to be safe, the oneof case slot must be in
  609. // sync with the value slot whenever the Ruby VM has been called. Thus, we
  610. // use native_slot_set_value_and_case(), which ensures that both the value
  611. // and case number are altered atomically (w.r.t. the Ruby VM).
  612. native_slot_set_value_and_case(
  613. upb_fielddef_type(field), field_type_class(field),
  614. memory, val,
  615. oneof_case, upb_fielddef_number(field));
  616. }
  617. } else if (is_map_field(field)) {
  618. check_map_field_type(val, field);
  619. DEREF(memory, VALUE) = val;
  620. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  621. check_repeated_field_type(val, field);
  622. DEREF(memory, VALUE) = val;
  623. } else {
  624. native_slot_set(upb_fielddef_type(field), field_type_class(field),
  625. memory, val);
  626. }
  627. }
  628. void layout_init(MessageLayout* layout,
  629. void* storage) {
  630. upb_msg_field_iter it;
  631. for (upb_msg_field_begin(&it, layout->msgdef);
  632. !upb_msg_field_done(&it);
  633. upb_msg_field_next(&it)) {
  634. const upb_fielddef* field = upb_msg_iter_field(&it);
  635. void* memory = slot_memory(layout, storage, field);
  636. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  637. if (upb_fielddef_containingoneof(field)) {
  638. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  639. *oneof_case = ONEOF_CASE_NONE;
  640. } else if (is_map_field(field)) {
  641. VALUE map = Qnil;
  642. const upb_fielddef* key_field = map_field_key(field);
  643. const upb_fielddef* value_field = map_field_value(field);
  644. VALUE type_class = field_type_class(value_field);
  645. if (type_class != Qnil) {
  646. VALUE args[3] = {
  647. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  648. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  649. type_class,
  650. };
  651. map = rb_class_new_instance(3, args, cMap);
  652. } else {
  653. VALUE args[2] = {
  654. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  655. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  656. };
  657. map = rb_class_new_instance(2, args, cMap);
  658. }
  659. DEREF(memory, VALUE) = map;
  660. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  661. VALUE ary = Qnil;
  662. VALUE type_class = field_type_class(field);
  663. if (type_class != Qnil) {
  664. VALUE args[2] = {
  665. fieldtype_to_ruby(upb_fielddef_type(field)),
  666. type_class,
  667. };
  668. ary = rb_class_new_instance(2, args, cRepeatedField);
  669. } else {
  670. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  671. ary = rb_class_new_instance(1, args, cRepeatedField);
  672. }
  673. DEREF(memory, VALUE) = ary;
  674. } else {
  675. native_slot_init(upb_fielddef_type(field), memory);
  676. }
  677. }
  678. }
  679. void layout_mark(MessageLayout* layout, void* storage) {
  680. upb_msg_field_iter it;
  681. for (upb_msg_field_begin(&it, layout->msgdef);
  682. !upb_msg_field_done(&it);
  683. upb_msg_field_next(&it)) {
  684. const upb_fielddef* field = upb_msg_iter_field(&it);
  685. void* memory = slot_memory(layout, storage, field);
  686. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  687. if (upb_fielddef_containingoneof(field)) {
  688. if (*oneof_case == upb_fielddef_number(field)) {
  689. native_slot_mark(upb_fielddef_type(field), memory);
  690. }
  691. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  692. rb_gc_mark(DEREF(memory, VALUE));
  693. } else {
  694. native_slot_mark(upb_fielddef_type(field), memory);
  695. }
  696. }
  697. }
  698. void layout_dup(MessageLayout* layout, void* to, void* from) {
  699. upb_msg_field_iter it;
  700. for (upb_msg_field_begin(&it, layout->msgdef);
  701. !upb_msg_field_done(&it);
  702. upb_msg_field_next(&it)) {
  703. const upb_fielddef* field = upb_msg_iter_field(&it);
  704. void* to_memory = slot_memory(layout, to, field);
  705. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  706. void* from_memory = slot_memory(layout, from, field);
  707. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  708. if (upb_fielddef_containingoneof(field)) {
  709. if (*from_oneof_case == upb_fielddef_number(field)) {
  710. *to_oneof_case = *from_oneof_case;
  711. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  712. }
  713. } else if (is_map_field(field)) {
  714. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  715. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  716. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  717. } else {
  718. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  719. }
  720. }
  721. }
  722. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  723. upb_msg_field_iter it;
  724. for (upb_msg_field_begin(&it, layout->msgdef);
  725. !upb_msg_field_done(&it);
  726. upb_msg_field_next(&it)) {
  727. const upb_fielddef* field = upb_msg_iter_field(&it);
  728. void* to_memory = slot_memory(layout, to, field);
  729. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  730. void* from_memory = slot_memory(layout, from, field);
  731. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  732. if (upb_fielddef_containingoneof(field)) {
  733. if (*from_oneof_case == upb_fielddef_number(field)) {
  734. *to_oneof_case = *from_oneof_case;
  735. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  736. }
  737. } else if (is_map_field(field)) {
  738. DEREF(to_memory, VALUE) =
  739. Map_deep_copy(DEREF(from_memory, VALUE));
  740. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  741. DEREF(to_memory, VALUE) =
  742. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  743. } else {
  744. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  745. }
  746. }
  747. }
  748. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  749. upb_msg_field_iter it;
  750. for (upb_msg_field_begin(&it, layout->msgdef);
  751. !upb_msg_field_done(&it);
  752. upb_msg_field_next(&it)) {
  753. const upb_fielddef* field = upb_msg_iter_field(&it);
  754. void* msg1_memory = slot_memory(layout, msg1, field);
  755. uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, field);
  756. void* msg2_memory = slot_memory(layout, msg2, field);
  757. uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, field);
  758. if (upb_fielddef_containingoneof(field)) {
  759. if (*msg1_oneof_case != *msg2_oneof_case ||
  760. (*msg1_oneof_case == upb_fielddef_number(field) &&
  761. !native_slot_eq(upb_fielddef_type(field),
  762. msg1_memory,
  763. msg2_memory))) {
  764. return Qfalse;
  765. }
  766. } else if (is_map_field(field)) {
  767. if (!Map_eq(DEREF(msg1_memory, VALUE),
  768. DEREF(msg2_memory, VALUE))) {
  769. return Qfalse;
  770. }
  771. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  772. if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
  773. DEREF(msg2_memory, VALUE))) {
  774. return Qfalse;
  775. }
  776. } else {
  777. if (!native_slot_eq(upb_fielddef_type(field),
  778. msg1_memory, msg2_memory)) {
  779. return Qfalse;
  780. }
  781. }
  782. }
  783. return Qtrue;
  784. }
  785. VALUE layout_hash(MessageLayout* layout, void* storage) {
  786. upb_msg_field_iter it;
  787. st_index_t h = rb_hash_start(0);
  788. VALUE hash_sym = rb_intern("hash");
  789. for (upb_msg_field_begin(&it, layout->msgdef);
  790. !upb_msg_field_done(&it);
  791. upb_msg_field_next(&it)) {
  792. const upb_fielddef* field = upb_msg_iter_field(&it);
  793. VALUE field_val = layout_get(layout, storage, field);
  794. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  795. }
  796. h = rb_hash_end(h);
  797. return INT2FIX(h);
  798. }
  799. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  800. VALUE str = rb_str_new2("");
  801. upb_msg_field_iter it;
  802. bool first = true;
  803. for (upb_msg_field_begin(&it, layout->msgdef);
  804. !upb_msg_field_done(&it);
  805. upb_msg_field_next(&it)) {
  806. const upb_fielddef* field = upb_msg_iter_field(&it);
  807. VALUE field_val = layout_get(layout, storage, field);
  808. if (!first) {
  809. str = rb_str_cat2(str, ", ");
  810. } else {
  811. first = false;
  812. }
  813. str = rb_str_cat2(str, upb_fielddef_name(field));
  814. str = rb_str_cat2(str, ": ");
  815. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  816. }
  817. return str;
  818. }