storage.c 34 KB

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