From afffa9eaeb58efedbe695b6f985ea7c810871af2 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 10 Mar 2022 15:21:55 -0800 Subject: [PATCH] Two Ruby changes to unblock the release --- bazel/amalgamate.py | 3 +++ upb/def.c | 7 ++++--- upb/port_def.inc | 8 ++++++++ upb/port_undef.inc | 1 + upb/table.c | 10 +++++----- upb/table_internal.h | 2 ++ 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/bazel/amalgamate.py b/bazel/amalgamate.py index 103b11c351..70399085ca 100755 --- a/bazel/amalgamate.py +++ b/bazel/amalgamate.py @@ -44,6 +44,9 @@ class Amalgamator: self.output_c.write("/* Amalgamated source file */\n") self.output_c.write('#include "%supb.h"\n' % (prefix)) + if prefix == "ruby-": + self.output_h.write("// Ruby is still using proto3 enum semantics for proto2\n") + self.output_h.write("#define UPB_DISABLE_PROTO2_ENUM_CHECKING\n") self.output_c.write(open("upb/port_def.inc").read()) self.output_h.write("/* Amalgamated source file */\n") diff --git a/upb/def.c b/upb/def.c index 451aecdaf3..140465aab5 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1414,6 +1414,7 @@ static uint8_t map_descriptortype(const upb_FieldDef* f) { return kUpb_FieldType_Bytes; } else if (type == kUpb_FieldType_Enum && (f->sub.enumdef->file->syntax == kUpb_Syntax_Proto3 || + UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 || // TODO(https://github.com/protocolbuffers/upb/issues/541): // fix map enum values to check for unknown enum values and put // them in the unknown field set. @@ -1584,11 +1585,11 @@ static void make_layout(symtab_addctx* ctx, const upb_MessageDef* m) { fill_fieldlayout(field, f); - if (upb_FieldDef_IsSubMessage(f)) { + if (field->descriptortype == kUpb_FieldType_Message || + field->descriptortype == kUpb_FieldType_Group) { field->submsg_index = sublayout_count++; subs[field->submsg_index].submsg = upb_FieldDef_MessageSubDef(f)->layout; - } else if (upb_FieldDef_CType(f) == kUpb_CType_Enum && - f->sub.enumdef->file->syntax == kUpb_Syntax_Proto2) { + } else if (field->descriptortype == kUpb_FieldType_Enum) { field->submsg_index = sublayout_count++; subs[field->submsg_index].subenum = upb_FieldDef_EnumSubDef(f)->layout; UPB_ASSERT(subs[field->submsg_index].subenum); diff --git a/upb/port_def.inc b/upb/port_def.inc index 75d416d1ce..2b240ff6e8 100644 --- a/upb/port_def.inc +++ b/upb/port_def.inc @@ -251,3 +251,11 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size); #define UPB_UNPOISON_MEMORY_REGION(addr, size) \ ((void)(addr), (void)(size)) #endif + +/* Disable proto2 arena behavior (TEMPORARY) **********************************/ + +#ifdef UPB_DISABLE_PROTO2_ENUM_CHECKING +#define UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 1 +#else +#define UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 0 +#endif diff --git a/upb/port_undef.inc b/upb/port_undef.inc index 70956df25d..3ef2d0dbcd 100644 --- a/upb/port_undef.inc +++ b/upb/port_undef.inc @@ -59,3 +59,4 @@ #undef UPB_POISON_MEMORY_REGION #undef UPB_UNPOISON_MEMORY_REGION #undef UPB_ASAN +#undef UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 diff --git a/upb/table.c b/upb/table.c index 33b3a9dc6e..63fe3cc960 100644 --- a/upb/table.c +++ b/upb/table.c @@ -433,14 +433,14 @@ const uint64_t kWyhashSalt[5] = { 0x082EFA98EC4E6C89ULL, 0x452821E638D01377ULL, }; -static uint32_t table_hash(const char* p, size_t n) { +uint32_t _upb_Hash(const char* p, size_t n) { return Wyhash(p, n, 0, kWyhashSalt); } static uint32_t strhash(upb_tabkey key) { uint32_t len; char* str = upb_tabstr(key, &len); - return table_hash(str, len); + return _upb_Hash(str, len); } static bool streql(upb_tabkey k1, lookupkey_t k2) { @@ -496,20 +496,20 @@ bool upb_strtable_insert(upb_strtable* t, const char* k, size_t len, tabkey = strcopy(key, a); if (tabkey == 0) return false; - hash = table_hash(key.str.str, key.str.len); + hash = _upb_Hash(key.str.str, key.str.len); insert(&t->t, key, tabkey, v, hash, &strhash, &streql); return true; } bool upb_strtable_lookup2(const upb_strtable* t, const char* key, size_t len, upb_value* v) { - uint32_t hash = table_hash(key, len); + uint32_t hash = _upb_Hash(key, len); return lookup(&t->t, strkey2(key, len), v, hash, &streql); } bool upb_strtable_remove2(upb_strtable* t, const char* key, size_t len, upb_value* val) { - uint32_t hash = table_hash(key, len); + uint32_t hash = _upb_Hash(key, len); upb_tabkey tabkey; return rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql); } diff --git a/upb/table_internal.h b/upb/table_internal.h index 9fb2d238cd..74076551de 100644 --- a/upb/table_internal.h +++ b/upb/table_internal.h @@ -374,6 +374,8 @@ void upb_inttable_iter_setdone(upb_inttable_iter* i); bool upb_inttable_iter_isequal(const upb_inttable_iter* i1, const upb_inttable_iter* i2); +uint32_t _upb_Hash(const char* p, size_t n); + #ifdef __cplusplus } /* extern "C" */ #endif