Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upb: lock down the internal headers for upb:wire_reader #15239

Merged
1 commit merged into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions upb/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cc_library(
cc_library(
name = "internal",
hdrs = [
"internal/endian.h",
"internal/log2.h",
],
copts = UPB_DEFAULT_COPTS,
Expand Down
21 changes: 11 additions & 10 deletions upb/wire/internal/endian.h → upb/base/internal/endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

#ifndef UPB_WIRE_INTERNAL_ENDIAN_H_
#define UPB_WIRE_INTERNAL_ENDIAN_H_
#ifndef UPB_BASE_INTERNAL_ENDIAN_H_
#define UPB_BASE_INTERNAL_ENDIAN_H_

#include <stdint.h>

Expand All @@ -17,23 +17,24 @@
extern "C" {
#endif

UPB_INLINE bool UPB_PRIVATE(_upb_IsLittleEndian)(void) {
UPB_INLINE bool upb_IsLittleEndian(void) {
const int x = 1;
return *(char*)&x == 1;
}

UPB_INLINE uint32_t UPB_PRIVATE(_upb_BigEndian32)(uint32_t val) {
if (UPB_PRIVATE(_upb_IsLittleEndian)()) return val;
UPB_INLINE uint32_t upb_BigEndian32(uint32_t val) {
if (upb_IsLittleEndian()) return val;

return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
}

UPB_INLINE uint64_t UPB_PRIVATE(_upb_BigEndian64)(uint64_t val) {
if (UPB_PRIVATE(_upb_IsLittleEndian)()) return val;
UPB_INLINE uint64_t upb_BigEndian64(uint64_t val) {
if (upb_IsLittleEndian()) return val;

return ((uint64_t)UPB_PRIVATE(_upb_BigEndian32)((uint32_t)val) << 32) |
UPB_PRIVATE(_upb_BigEndian32)((uint32_t)(val >> 32));
const uint64_t hi = ((uint64_t)upb_BigEndian32((uint32_t)val)) << 32;
const uint64_t lo = upb_BigEndian32((uint32_t)(val >> 32));
return hi | lo;
}

#ifdef __cplusplus
Expand All @@ -42,4 +43,4 @@ UPB_INLINE uint64_t UPB_PRIVATE(_upb_BigEndian64)(uint64_t val) {

#include "upb/port/undef.inc"

#endif /* UPB_WIRE_INTERNAL_ENDIAN_H_ */
#endif /* UPB_BASE_INTERNAL_ENDIAN_H_ */
1 change: 1 addition & 0 deletions upb/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ cc_test(
":compare",
"//upb:port",
"//upb:wire_reader",
"//upb/base:internal",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
Expand Down
6 changes: 3 additions & 3 deletions upb/util/compare_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <vector>

#include <gtest/gtest.h>
#include "upb/wire/internal/endian.h"
#include "upb/base/internal/endian.h"
#include "upb/wire/types.h"

// Must be last.
Expand Down Expand Up @@ -84,11 +84,11 @@ std::string ToBinaryPayload(const UnknownFields& fields) {
ret.append(val->val);
} else if (const auto* val = std::get_if<Fixed64>(&field.value)) {
EncodeVarint(field.field_number << 3 | kUpb_WireType_64Bit, &ret);
uint64_t swapped = UPB_PRIVATE(_upb_BigEndian64)(val->val);
uint64_t swapped = upb_BigEndian64(val->val);
ret.append(reinterpret_cast<const char*>(&swapped), sizeof(swapped));
} else if (const auto* val = std::get_if<Fixed32>(&field.value)) {
EncodeVarint(field.field_number << 3 | kUpb_WireType_32Bit, &ret);
uint32_t swapped = UPB_PRIVATE(_upb_BigEndian32)(val->val);
uint32_t swapped = upb_BigEndian32(val->val);
ret.append(reinterpret_cast<const char*>(&swapped), sizeof(swapped));
} else if (const auto* val = std::get_if<Group>(&field.value)) {
EncodeVarint(field.field_number << 3 | kUpb_WireType_StartGroup, &ret);
Expand Down
5 changes: 3 additions & 2 deletions upb/wire/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cc_library(
"//upb:message_accessors",
"//upb:mini_table",
"//upb:port",
"//upb/base:internal",
"//upb/hash",
"//upb/mem:internal",
"//upb/message:internal",
Expand All @@ -43,18 +44,18 @@ cc_library(
cc_library(
name = "reader",
srcs = [
"internal/reader.h",
"reader.c",
],
hdrs = [
"internal/endian.h",
"internal/reader.h",
"reader.h",
"types.h",
],
visibility = ["//visibility:public"],
deps = [
":eps_copy_input_stream",
"//upb:port",
"//upb/base:internal",
],
)

Expand Down
8 changes: 4 additions & 4 deletions upb/wire/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <string.h>

#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/mem/arena.h"
Expand Down Expand Up @@ -42,7 +43,6 @@
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/internal/constants.h"
#include "upb/wire/internal/decoder.h"
#include "upb/wire/internal/endian.h"
#include "upb/wire/reader.h"

// Must be last.
Expand Down Expand Up @@ -212,7 +212,7 @@ static const char* upb_Decoder_DecodeSize(upb_Decoder* d, const char* ptr,
}

static void _upb_Decoder_MungeInt32(wireval* val) {
if (!UPB_PRIVATE(_upb_IsLittleEndian)()) {
if (!upb_IsLittleEndian()) {
/* The next stage will memcpy(dst, &val, 4) */
val->uint32_val = val->uint64_val;
}
Expand Down Expand Up @@ -434,7 +434,7 @@ static const char* _upb_Decoder_DecodeFixedPacked(
arr->UPB_PRIVATE(size) += count;
// Note: if/when the decoder supports multi-buffer input, we will need to
// handle buffer seams here.
if (UPB_PRIVATE(_upb_IsLittleEndian)()) {
if (upb_IsLittleEndian()) {
ptr = upb_EpsCopyInputStream_Copy(&d->input, ptr, mem, val->size);
} else {
int delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size);
Expand Down Expand Up @@ -758,7 +758,7 @@ const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
}
uint64_t msg_head;
memcpy(&msg_head, msg, 8);
msg_head = UPB_PRIVATE(_upb_BigEndian64)(msg_head);
msg_head = upb_BigEndian64(msg_head);
if (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~msg_head) {
d->missing_required = true;
}
Expand Down
14 changes: 7 additions & 7 deletions upb/wire/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string.h>

#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/hash/str_table.h"
Expand All @@ -37,7 +38,6 @@
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/wire/internal/constants.h"
#include "upb/wire/internal/endian.h"
#include "upb/wire/types.h"

// Must be last.
Expand Down Expand Up @@ -130,12 +130,12 @@ static void encode_bytes(upb_encstate* e, const void* data, size_t len) {
}

static void encode_fixed64(upb_encstate* e, uint64_t val) {
val = UPB_PRIVATE(_upb_BigEndian64)(val);
val = upb_BigEndian64(val);
encode_bytes(e, &val, sizeof(uint64_t));
}

static void encode_fixed32(upb_encstate* e, uint32_t val) {
val = UPB_PRIVATE(_upb_BigEndian32)(val);
val = upb_BigEndian32(val);
encode_bytes(e, &val, sizeof(uint32_t));
}

Expand Down Expand Up @@ -186,18 +186,18 @@ static void encode_fixedarray(upb_encstate* e, const upb_Array* arr,
const char* data = _upb_array_constptr(arr);
const char* ptr = data + bytes - elem_size;

if (tag || !UPB_PRIVATE(_upb_IsLittleEndian)()) {
if (tag || !upb_IsLittleEndian()) {
while (true) {
if (elem_size == 4) {
uint32_t val;
memcpy(&val, ptr, sizeof(val));
val = UPB_PRIVATE(_upb_BigEndian32)(val);
val = upb_BigEndian32(val);
encode_bytes(e, &val, elem_size);
} else {
UPB_ASSERT(elem_size == 8);
uint64_t val;
memcpy(&val, ptr, sizeof(val));
val = UPB_PRIVATE(_upb_BigEndian64)(val);
val = upb_BigEndian64(val);
encode_bytes(e, &val, elem_size);
}

Expand Down Expand Up @@ -552,7 +552,7 @@ static void encode_message(upb_encstate* e, const upb_Message* msg,
m->UPB_PRIVATE(required_count)) {
uint64_t msg_head;
memcpy(&msg_head, msg, 8);
msg_head = UPB_PRIVATE(_upb_BigEndian64)(msg_head);
msg_head = upb_BigEndian64(msg_head);
if (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~msg_head) {
encode_err(e, kUpb_EncodeStatus_MissingRequired);
}
Expand Down
11 changes: 6 additions & 5 deletions upb/wire/internal/decode_fast.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
// - '1' for one-byte tags (field numbers 1-15)
// - '2' for two-byte tags (field numbers 16-2048)

#ifndef UPB_WIRE_DECODE_INTERNAL_FAST_H_
#define UPB_WIRE_DECODE_INTERNAL_FAST_H_
#ifndef UPB_WIRE_INTERNAL_DECODE_FAST_H_
#define UPB_WIRE_INTERNAL_DECODE_FAST_H_

#include "upb/message/message.h"

Expand Down Expand Up @@ -110,6 +110,7 @@ TAGBYTES(o)
TAGBYTES(r)

#undef F
#undef UTF8
#undef TAGBYTES

/* sub-message fields *********************************************************/
Expand All @@ -132,9 +133,9 @@ TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)

#undef TAGBYTES
#undef SIZES
#undef F
#undef SIZES
#undef TAGBYTES

#undef UPB_PARSE_PARAMS

Expand All @@ -144,4 +145,4 @@ TAGBYTES(r)

#include "upb/port/undef.inc"

#endif /* UPB_WIRE_DECODE_INTERNAL_FAST_H_ */
#endif /* UPB_WIRE_INTERNAL_DECODE_FAST_H_ */
6 changes: 3 additions & 3 deletions upb/wire/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#ifndef UPB_WIRE_READER_H_
#define UPB_WIRE_READER_H_

#include "upb/base/internal/endian.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/internal/endian.h"
#include "upb/wire/internal/reader.h"
#include "upb/wire/types.h" // IWYU pragma: export

Expand Down Expand Up @@ -89,7 +89,7 @@ UPB_INLINE const char* upb_WireReader_ReadSize(const char* ptr, int* size) {
UPB_INLINE const char* upb_WireReader_ReadFixed32(const char* ptr, void* val) {
uint32_t uval;
memcpy(&uval, ptr, 4);
uval = UPB_PRIVATE(_upb_BigEndian32)(uval);
uval = upb_BigEndian32(uval);
memcpy(val, &uval, 4);
return ptr + 4;
}
Expand All @@ -102,7 +102,7 @@ UPB_INLINE const char* upb_WireReader_ReadFixed32(const char* ptr, void* val) {
UPB_INLINE const char* upb_WireReader_ReadFixed64(const char* ptr, void* val) {
uint64_t uval;
memcpy(&uval, ptr, 8);
uval = UPB_PRIVATE(_upb_BigEndian64)(uval);
uval = upb_BigEndian64(uval);
memcpy(val, &uval, 8);
return ptr + 8;
}
Expand Down
Loading