storage.c 30 KB

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