From 71b0ff2b9fc2fb45dfe78716b779a4b28f6a256c Mon Sep 17 00:00:00 2001 From: Eric Salo Date: Fri, 1 Dec 2023 10:01:00 -0800 Subject: [PATCH] upb: tag upb_MiniTableField:offset as UPB_ONLYBITS() PiperOrigin-RevId: 587040868 --- upb/message/accessors.h | 5 ++- upb/message/internal/accessors.h | 4 +- upb/mini_descriptor/decode.c | 41 +++++++++++---------- upb/mini_descriptor/internal/encode_test.cc | 19 +++++----- upb/mini_table/compat.c | 3 +- upb/mini_table/internal/field.h | 7 +++- upb/wire/decode.c | 10 ++--- upb/wire/encode.c | 9 +++-- upb_generator/common.cc | 3 +- upb_generator/protoc-gen-upb_minitable.cc | 3 +- 10 files changed, 58 insertions(+), 46 deletions(-) diff --git a/upb/message/accessors.h b/upb/message/accessors.h index 4d4ee3651685d..55ce69889a6fc 100644 --- a/upb/message/accessors.h +++ b/upb/message/accessors.h @@ -364,13 +364,14 @@ UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage( const upb_MiniTableField* field, upb_Arena* arena) { UPB_ASSERT(arena); UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message); - upb_Message* sub_message = *UPB_PTR_AT(msg, field->offset, upb_Message*); + upb_Message* sub_message = + *UPB_PTR_AT(msg, field->UPB_ONLYBITS(offset), upb_Message*); if (!sub_message) { const upb_MiniTable* sub_mini_table = upb_MiniTableSub_Message( mini_table->UPB_PRIVATE(subs)[field->UPB_PRIVATE(submsg_index)]); UPB_ASSERT(sub_mini_table); sub_message = _upb_Message_New(sub_mini_table, arena); - *UPB_PTR_AT(msg, field->offset, upb_Message*) = sub_message; + *UPB_PTR_AT(msg, field->UPB_ONLYBITS(offset), upb_Message*) = sub_message; UPB_PRIVATE(_upb_Message_SetPresence)(msg, field); } return sub_message; diff --git a/upb/message/internal/accessors.h b/upb/message/internal/accessors.h index 1dccea1881f07..fd6bd2f72478c 100644 --- a/upb/message/internal/accessors.h +++ b/upb/message/internal/accessors.h @@ -100,12 +100,12 @@ UPB_INLINE void UPB_PRIVATE(_upb_Message_SetOneofCase)( UPB_INLINE void* _upb_MiniTableField_GetPtr(upb_Message* msg, const upb_MiniTableField* field) { - return (char*)msg + field->offset; + return (char*)msg + field->UPB_ONLYBITS(offset); } UPB_INLINE const void* _upb_MiniTableField_GetConstPtr( const upb_Message* msg, const upb_MiniTableField* field) { - return (char*)msg + field->offset; + return (char*)msg + field->UPB_ONLYBITS(offset); } UPB_INLINE void UPB_PRIVATE(_upb_Message_SetPresence)( diff --git a/upb/mini_descriptor/decode.c b/upb/mini_descriptor/decode.c index 1dafe0de1e8b1..dfd0413b291b7 100644 --- a/upb/mini_descriptor/decode.c +++ b/upb/mini_descriptor/decode.c @@ -174,10 +174,10 @@ static void upb_MiniTable_SetField(upb_MtDecoder* d, uint8_t ch, type -= kUpb_EncodedType_RepeatedBase; field->UPB_PRIVATE(mode) = kUpb_FieldMode_Array; field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift; - field->offset = kNoPresence; + field->UPB_PRIVATE(offset) = kNoPresence; } else { field->UPB_PRIVATE(mode) = kUpb_FieldMode_Scalar; - field->offset = kHasbitPresence; + field->UPB_PRIVATE(offset) = kHasbitPresence; if (type == kUpb_EncodedType_Group || type == kUpb_EncodedType_Message) { field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift; } else if ((unsigned long)type >= sizeof(kUpb_EncodedToFieldRep)) { @@ -225,7 +225,7 @@ static void upb_MtDecoder_ModifyField(upb_MtDecoder* d, bool required = field_modifiers & kUpb_EncodedFieldModifier_IsRequired; // Validate. - if ((singular || required) && field->offset != kHasbitPresence) { + if ((singular || required) && field->UPB_PRIVATE(offset) != kHasbitPresence) { upb_MdDecoder_ErrorJmp(&d->base, "Invalid modifier(s) for repeated field %" PRIu32, upb_MiniTableField_Number(field)); @@ -236,9 +236,9 @@ static void upb_MtDecoder_ModifyField(upb_MtDecoder* d, upb_MiniTableField_Number(field)); } - if (singular) field->offset = kNoPresence; + if (singular) field->UPB_PRIVATE(offset) = kNoPresence; if (required) { - field->offset = kRequiredPresence; + field->UPB_PRIVATE(offset) = kRequiredPresence; } } @@ -325,7 +325,7 @@ static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d, " to oneof, no such field number.", field_num); } - if (f->offset != kHasbitPresence) { + if (f->UPB_PRIVATE(offset) != kHasbitPresence) { upb_MdDecoder_ErrorJmp( &d->base, "Cannot add repeated, required, or singular field %" PRIu32 @@ -340,7 +340,7 @@ static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d, item->rep = rep; } // Prepend this field to the linked list. - f->offset = item->field_index; + f->UPB_PRIVATE(offset) = item->field_index; item->field_index = (f - d->fields) + kOneofBase; return ptr; } @@ -517,7 +517,7 @@ static bool upb_MtDecoder_SortLayoutItems(upb_MtDecoder* d) { int n = d->table->UPB_PRIVATE(field_count); for (int i = 0; i < n; i++) { upb_MiniTableField* f = &d->fields[i]; - if (f->offset >= kOneofBase) continue; + if (f->UPB_PRIVATE(offset) >= kOneofBase) continue; upb_LayoutItem item = {.field_index = i, .rep = f->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift, .type = kUpb_LayoutItemType_Field}; @@ -545,9 +545,9 @@ static void upb_MtDecoder_AssignHasbits(upb_MtDecoder* d) { for (int i = 0; i < n; i++) { upb_MiniTableField* field = (upb_MiniTableField*)&ret->UPB_PRIVATE(fields)[i]; - if (field->offset == kRequiredPresence) { + if (field->UPB_PRIVATE(offset) == kRequiredPresence) { field->presence = ++last_hasbit; - } else if (field->offset == kNoPresence) { + } else if (field->UPB_PRIVATE(offset) == kNoPresence) { field->presence = 0; } } @@ -561,7 +561,7 @@ static void upb_MtDecoder_AssignHasbits(upb_MtDecoder* d) { for (int i = 0; i < n; i++) { upb_MiniTableField* field = (upb_MiniTableField*)&ret->UPB_PRIVATE(fields)[i]; - if (field->offset == kHasbitPresence) { + if (field->UPB_PRIVATE(offset) == kHasbitPresence) { field->presence = ++last_hasbit; } } @@ -599,9 +599,10 @@ static void upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) { upb_MiniTableField* f = &d->fields[item->field_index]; while (true) { f->presence = ~item->offset; - if (f->offset == kUpb_LayoutItem_IndexSentinel) break; - UPB_ASSERT(f->offset - kOneofBase < d->table->UPB_PRIVATE(field_count)); - f = &d->fields[f->offset - kOneofBase]; + if (f->UPB_PRIVATE(offset) == kUpb_LayoutItem_IndexSentinel) break; + UPB_ASSERT(f->UPB_PRIVATE(offset) - kOneofBase < + d->table->UPB_PRIVATE(field_count)); + f = &d->fields[f->UPB_PRIVATE(offset) - kOneofBase]; } } @@ -611,14 +612,14 @@ static void upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) { switch (item->type) { case kUpb_LayoutItemType_OneofField: while (true) { - uint16_t next_offset = f->offset; - f->offset = item->offset; + uint16_t next_offset = f->UPB_PRIVATE(offset); + f->UPB_PRIVATE(offset) = item->offset; if (next_offset == kUpb_LayoutItem_IndexSentinel) break; f = &d->fields[next_offset - kOneofBase]; } break; case kUpb_LayoutItemType_Field: - f->offset = item->offset; + f->UPB_PRIVATE(offset) = item->offset; break; default: break; @@ -689,8 +690,8 @@ static void upb_MtDecoder_ParseMap(upb_MtDecoder* d, const char* data, // NOTE: sync with mini_table/message_internal.h. const size_t kv_size = d->platform == kUpb_MiniTablePlatform_32Bit ? 8 : 16; const size_t hasbit_size = 8; - d->fields[0].offset = hasbit_size; - d->fields[1].offset = hasbit_size + kv_size; + d->fields[0].UPB_PRIVATE(offset) = hasbit_size; + d->fields[1].UPB_PRIVATE(offset) = hasbit_size + kv_size; d->table->UPB_PRIVATE(size) = UPB_ALIGN_UP(hasbit_size + kv_size + kv_size, 8); @@ -815,7 +816,7 @@ static const char* upb_MtDecoder_DoBuildMiniTableExtension( upb_MiniTableField* f = &ext->UPB_PRIVATE(field); f->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsExtension; - f->offset = 0; + f->UPB_PRIVATE(offset) = 0; f->presence = 0; if (extendee->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMessageSet) { diff --git a/upb/mini_descriptor/internal/encode_test.cc b/upb/mini_descriptor/internal/encode_test.cc index 498464106073d..aa9979db3c270 100644 --- a/upb/mini_descriptor/internal/encode_test.cc +++ b/upb/mini_descriptor/internal/encode_test.cc @@ -70,8 +70,8 @@ TEST_P(MiniTableTest, AllScalarTypes) { const upb_MiniTableField* f = &table->UPB_PRIVATE(fields)[i]; EXPECT_EQ(i + 1, upb_MiniTableField_Number(f)); EXPECT_TRUE(upb_MiniTableField_IsScalar(f)); - EXPECT_TRUE(offsets.insert(f->offset).second); - EXPECT_TRUE(f->offset < table->UPB_PRIVATE(size)); + EXPECT_TRUE(offsets.insert(f->UPB_PRIVATE(offset)).second); + EXPECT_TRUE(f->UPB_PRIVATE(offset) < table->UPB_PRIVATE(size)); } EXPECT_EQ(0, table->UPB_PRIVATE(required_count)); } @@ -96,8 +96,8 @@ TEST_P(MiniTableTest, AllRepeatedTypes) { const upb_MiniTableField* f = &table->UPB_PRIVATE(fields)[i]; EXPECT_EQ(i + 1, upb_MiniTableField_Number(f)); EXPECT_TRUE(upb_MiniTableField_IsArray(f)); - EXPECT_TRUE(offsets.insert(f->offset).second); - EXPECT_TRUE(f->offset < table->UPB_PRIVATE(size)); + EXPECT_TRUE(offsets.insert(f->UPB_PRIVATE(offset)).second); + EXPECT_TRUE(f->UPB_PRIVATE(offset) < table->UPB_PRIVATE(size)); } EXPECT_EQ(0, table->UPB_PRIVATE(required_count)); } @@ -125,8 +125,8 @@ TEST_P(MiniTableTest, Skips) { EXPECT_EQ(field_numbers[i], upb_MiniTableField_Number(f)); EXPECT_EQ(kUpb_FieldType_Float, upb_MiniTableField_Type(f)); EXPECT_TRUE(upb_MiniTableField_IsScalar(f)); - EXPECT_TRUE(offsets.insert(f->offset).second); - EXPECT_TRUE(f->offset < table->UPB_PRIVATE(size)); + EXPECT_TRUE(offsets.insert(f->UPB_PRIVATE(offset)).second); + EXPECT_TRUE(f->UPB_PRIVATE(offset) < table->UPB_PRIVATE(size)); } EXPECT_EQ(0, table->UPB_PRIVATE(required_count)); } @@ -155,13 +155,14 @@ TEST_P(MiniTableTest, AllScalarTypesOneof) { EXPECT_EQ(i + 1, upb_MiniTableField_Number(f)); EXPECT_TRUE(upb_MiniTableField_IsScalar(f)); // For a oneof all fields have the same offset. - EXPECT_EQ(table->UPB_PRIVATE(fields)[0].offset, f->offset); + EXPECT_EQ(table->UPB_PRIVATE(fields)[0].UPB_PRIVATE(offset), + f->UPB_PRIVATE(offset)); // All presence fields should point to the same oneof case offset. size_t case_ofs = _upb_MiniTableField_OneofOffset(f); EXPECT_EQ(table->UPB_PRIVATE(fields)[0].presence, f->presence); - EXPECT_TRUE(f->offset < table->UPB_PRIVATE(size)); + EXPECT_TRUE(f->UPB_PRIVATE(offset) < table->UPB_PRIVATE(size)); EXPECT_TRUE(case_ofs < table->UPB_PRIVATE(size)); - EXPECT_TRUE(case_ofs != f->offset); + EXPECT_TRUE(case_ofs != f->UPB_PRIVATE(offset)); } EXPECT_EQ(0, table->UPB_PRIVATE(required_count)); } diff --git a/upb/mini_table/compat.c b/upb/mini_table/compat.c index 23e13cef346c7..e834c4f09f62a 100644 --- a/upb/mini_table/compat.c +++ b/upb/mini_table/compat.c @@ -45,7 +45,8 @@ static upb_MiniTableEquals_Status upb_deep_check(const upb_MiniTable* src, return false; if (src_field->UPB_PRIVATE(mode) != dst_field->UPB_PRIVATE(mode)) return false; - if (src_field->offset != dst_field->offset) return false; + if (src_field->UPB_PRIVATE(offset) != dst_field->UPB_PRIVATE(offset)) + return false; if (src_field->presence != dst_field->presence) return false; if (src_field->UPB_PRIVATE(submsg_index) != dst_field->UPB_PRIVATE(submsg_index)) diff --git a/upb/mini_table/internal/field.h b/upb/mini_table/internal/field.h index cc0974c737b4b..8d7c5e7308908 100644 --- a/upb/mini_table/internal/field.h +++ b/upb/mini_table/internal/field.h @@ -20,7 +20,7 @@ // LINT.IfChange(struct_definition) struct upb_MiniTableField { uint32_t UPB_ONLYBITS(number); - uint16_t offset; + uint16_t UPB_ONLYBITS(offset); int16_t presence; // If >0, hasbit_index. If <0, ~oneof_index // Indexes into `upb_MiniTable.subs` @@ -174,6 +174,11 @@ UPB_PRIVATE(_upb_MiniTableField_Number)(const struct upb_MiniTableField* f) { return f->UPB_ONLYBITS(number); } +UPB_INLINE uint16_t +UPB_PRIVATE(_upb_MiniTableField_Offset)(const struct upb_MiniTableField* f) { + return f->UPB_ONLYBITS(offset); +} + UPB_INLINE size_t _upb_MiniTableField_OneofOffset(const struct upb_MiniTableField* f) { UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_IsInOneof)(f)); diff --git a/upb/wire/decode.c b/upb/wire/decode.c index f298290334975..f5ad1cfa46cdf 100644 --- a/upb/wire/decode.c +++ b/upb/wire/decode.c @@ -517,7 +517,7 @@ static const char* _upb_Decoder_DecodeToArray(upb_Decoder* d, const char* ptr, const upb_MiniTableSub* subs, const upb_MiniTableField* field, wireval* val, int op) { - upb_Array** arrp = UPB_PTR_AT(msg, field->offset, void); + upb_Array** arrp = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void); upb_Array* arr = *arrp; void* mem; @@ -608,8 +608,8 @@ upb_Map* _upb_Decoder_CreateMap(upb_Decoder* d, const upb_MiniTable* entry) { const upb_MiniTableField* val_field = &entry->UPB_PRIVATE(fields)[1]; char key_size = kSizeInMap[key_field->UPB_PRIVATE(descriptortype)]; char val_size = kSizeInMap[val_field->UPB_PRIVATE(descriptortype)]; - UPB_ASSERT(key_field->offset == offsetof(upb_MapEntryData, k)); - UPB_ASSERT(val_field->offset == offsetof(upb_MapEntryData, v)); + UPB_ASSERT(key_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntryData, k)); + UPB_ASSERT(val_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntryData, v)); upb_Map* ret = _upb_Map_New(&d->arena, key_size, val_size); if (!ret) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory); return ret; @@ -620,7 +620,7 @@ static const char* _upb_Decoder_DecodeToMap(upb_Decoder* d, const char* ptr, const upb_MiniTableSub* subs, const upb_MiniTableField* field, wireval* val) { - upb_Map** map_p = UPB_PTR_AT(msg, field->offset, upb_Map*); + upb_Map** map_p = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), upb_Map*); upb_Map* map = *map_p; upb_MapEntry ent; UPB_ASSERT(upb_MiniTableField_Type(field) == kUpb_FieldType_Message); @@ -683,7 +683,7 @@ static const char* _upb_Decoder_DecodeToSubMessage( upb_Decoder* d, const char* ptr, upb_Message* msg, const upb_MiniTableSub* subs, const upb_MiniTableField* field, wireval* val, int op) { - void* mem = UPB_PTR_AT(msg, field->offset, void); + void* mem = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void); int type = field->UPB_PRIVATE(descriptortype); if (UPB_UNLIKELY(op == kUpb_DecodeOp_Enum) && diff --git a/upb/wire/encode.c b/upb/wire/encode.c index 5ec9edc8e1809..4f923e9c3994c 100644 --- a/upb/wire/encode.c +++ b/upb/wire/encode.c @@ -309,7 +309,7 @@ static void encode_scalar(upb_encstate* e, const void* _field_mem, static void encode_array(upb_encstate* e, const upb_Message* msg, const upb_MiniTableSub* subs, const upb_MiniTableField* f) { - const upb_Array* arr = *UPB_PTR_AT(msg, f->offset, upb_Array*); + const upb_Array* arr = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), upb_Array*); bool packed = upb_MiniTableField_IsPacked(f); size_t pre_len = e->limit - e->ptr; @@ -432,7 +432,7 @@ static void encode_mapentry(upb_encstate* e, uint32_t number, static void encode_map(upb_encstate* e, const upb_Message* msg, const upb_MiniTableSub* subs, const upb_MiniTableField* f) { - const upb_Map* map = *UPB_PTR_AT(msg, f->offset, const upb_Map*); + const upb_Map* map = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), const upb_Map*); const upb_MiniTable* layout = upb_MiniTableSub_Message(subs[f->UPB_PRIVATE(submsg_index)]); UPB_ASSERT(layout->UPB_PRIVATE(field_count) == 2); @@ -467,7 +467,7 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg, const upb_MiniTableField* f) { if (f->presence == 0) { // Proto3 presence or map/array. - const void* mem = UPB_PTR_AT(msg, f->offset, void); + const void* mem = UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), void); switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) { case kUpb_FieldRep_1Byte: { char ch; @@ -512,7 +512,8 @@ static void encode_field(upb_encstate* e, const upb_Message* msg, encode_map(e, msg, subs, field); break; case kUpb_FieldMode_Scalar: - encode_scalar(e, UPB_PTR_AT(msg, field->offset, void), subs, field); + encode_scalar(e, UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void), subs, + field); break; default: UPB_UNREACHABLE(); diff --git a/upb_generator/common.cc b/upb_generator/common.cc index 554c02e6cc64b..9c60a7edf0c18 100644 --- a/upb_generator/common.cc +++ b/upb_generator/common.cc @@ -85,7 +85,8 @@ std::string FieldInitializer(upb::FieldDefPtr field, const upb_MiniTableField* field32) { return absl::Substitute( "{$0, $1, $2, $3, $4, $5}", upb_MiniTableField_Number(field64), - ArchDependentSize(field32->offset, field64->offset), + ArchDependentSize(field32->UPB_PRIVATE(offset), + field64->UPB_PRIVATE(offset)), ArchDependentSize(field32->presence, field64->presence), field64->UPB_PRIVATE(submsg_index) == kUpb_NoSub ? "kUpb_NoSub" diff --git a/upb_generator/protoc-gen-upb_minitable.cc b/upb_generator/protoc-gen-upb_minitable.cc index 1c5d636f2d869..d58af3a89bd1d 100644 --- a/upb_generator/protoc-gen-upb_minitable.cc +++ b/upb_generator/protoc-gen-upb_minitable.cc @@ -286,7 +286,8 @@ bool TryFillTableEntry(const DefPoolPair& pools, upb::FieldDefPtr field, // // - |presence| is either hasbit index or field number for oneofs. - uint64_t data = static_cast(mt_f->offset) << 48 | expected_tag; + uint64_t data = + static_cast(mt_f->UPB_PRIVATE(offset)) << 48 | expected_tag; if (field.IsSequence()) { // No hasbit/oneof-related fields.