upb: fix the leaking port defs

PiperOrigin-RevId: 559225386
diff --git a/BUILD b/BUILD
index bbbba8b..751ec5a 100644
--- a/BUILD
+++ b/BUILD
@@ -411,6 +411,15 @@
     visibility = ["//:friends"],
 )
 
+# begin:google_only
+# alias(
+#     name = "mini_table_compat",
+#     actual = "//upb/mini_table:compat",
+#     compatible_with = ["//buildenv/target:non_prod"],
+#     visibility = ["//:friends"],
+# )
+# end:google_only
+
 alias(
     name = "mini_table_internal",
     actual = "//upb/mini_table:internal",
@@ -535,8 +544,9 @@
         ":mini_table",
         ":mini_table_internal",
         ":port",
-        ":reflection_internal",
         ":reflection",
+        ":reflection_internal",
+        ":wire",
         ":wire_internal",
         ":wire_reader",
         ":wire_types",
@@ -582,8 +592,9 @@
         ":mini_table",
         ":mini_table_internal",
         ":port",
-        ":reflection_internal",
         ":reflection",
+        ":reflection_internal",
+        ":wire",
         ":wire_internal",
         ":wire_reader",
         ":wire_types",
@@ -630,8 +641,9 @@
         ":mini_table",
         ":mini_table_internal",
         ":port",
-        ":reflection_internal",
         ":reflection",
+        ":reflection_internal",
+        ":wire",
         ":wire_internal",
         ":wire_reader",
         ":wire_types",
diff --git a/python/BUILD b/python/BUILD
index 03b79ab..1adb2ba 100644
--- a/python/BUILD
+++ b/python/BUILD
@@ -243,7 +243,6 @@
         "//:port",
         "//:reflection",
         "//:text",
-        "//:wire",
         "//:wire_reader",
         "//:wire_types",
         "//upb/util:compare",
diff --git a/upb/message/promote_test.cc b/upb/message/promote_test.cc
index 8a60283..225e1ce 100644
--- a/upb/message/promote_test.cc
+++ b/upb/message/promote_test.cc
@@ -56,26 +56,6 @@
 
 namespace {
 
-// Proto2 test messages field numbers used for reflective access.
-const uint32_t kFieldOptionalInt32 = 1;
-const uint32_t kFieldOptionalUInt32 = 3;
-const uint32_t kFieldOptionalBool = 13;
-const uint32_t kFieldOptionalString = 14;
-const uint32_t kFieldOptionalNestedMessage = 18;
-const uint32_t kFieldOptionalRepeatedInt32 = 31;
-const uint32_t kFieldOptionalNestedMessageA = 1;
-const uint32_t kFieldOptionalOneOfUInt32 = 111;
-const uint32_t kFieldOptionalOneOfString = 113;
-
-const uint32_t kFieldProto3OptionalInt64 = 2;
-const uint32_t kFieldProto3OptionalUInt64 = 4;
-
-const char kTestStr1[] = "Hello1";
-const char kTestStr2[] = "Hello2";
-const int32_t kTestInt32 = 567;
-const int32_t kTestUInt32 = 0xF1234567;
-const uint64_t kTestUInt64 = 0xFEDCBAFF87654321;
-
 TEST(GeneratedCode, FindUnknown) {
   upb_Arena* arena = upb_Arena_New();
   upb_test_ModelWithExtensions* msg = upb_test_ModelWithExtensions_new(arena);
diff --git a/upb/mini_table/BUILD b/upb/mini_table/BUILD
index 0f44350..36feedc 100644
--- a/upb/mini_table/BUILD
+++ b/upb/mini_table/BUILD
@@ -28,6 +28,26 @@
     "UPB_DEFAULT_COPTS",
 )
 
+# begin:google_only
+# cc_library(
+#     name = "compat",
+#     srcs = [
+#         "compat.c",
+#     ],
+#     hdrs = [
+#         "compat.h",
+#     ],
+#     compatible_with = ["//buildenv/target:non_prod"],
+#     copts = UPB_DEFAULT_COPTS,
+#     visibility = ["//:__pkg__"],
+#     deps = [
+#         ":mini_table",
+#         "//:base",
+#         "//:port",
+#     ],
+# )
+# end:google_only
+
 cc_library(
     name = "mini_table",
     srcs = [
diff --git a/upb/mini_table/compat.c b/upb/mini_table/compat.c
new file mode 100644
index 0000000..26c69dd
--- /dev/null
+++ b/upb/mini_table/compat.c
@@ -0,0 +1,81 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2023 Google LLC.  All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//     * Neither the name of Google LLC nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "upb/mini_table/compat.h"
+
+#include "upb/base/descriptor_constants.h"
+#include "upb/mini_table/field.h"
+#include "upb/mini_table/message.h"
+
+// Must be last.
+#include "upb/port/def.inc"
+
+static bool upb_deep_check(const upb_MiniTable* src, const upb_MiniTable* dst,
+                           bool eq) {
+  if (src->field_count != dst->field_count) return false;
+
+  for (int i = 0; i < src->field_count; i++) {
+    const upb_MiniTableField* src_field = &src->fields[i];
+    const upb_MiniTableField* dst_field =
+        upb_MiniTable_FindFieldByNumber(dst, src_field->number);
+
+    if (upb_MiniTableField_CType(src_field) !=
+        upb_MiniTableField_CType(dst_field)) return false;
+    if (src_field->mode != dst_field->mode) return false;
+    if (src_field->offset != dst_field->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)) return false;
+
+    // Go no further if we are only checking for compatibility.
+    if (!eq) continue;
+
+    if (upb_MiniTableField_CType(src_field) == kUpb_CType_Message) {
+      const upb_MiniTable* sub_src =
+          upb_MiniTable_GetSubMessageTable(src, src_field);
+      const upb_MiniTable* sub_dst =
+          upb_MiniTable_GetSubMessageTable(dst, dst_field);
+      if (sub_src != NULL && !upb_MiniTable_Equals(sub_src, sub_dst)) {
+        return false;
+      }
+    }
+  }
+
+  return true;
+}
+
+bool upb_MiniTable_Compatible(const upb_MiniTable* src,
+                              const upb_MiniTable* dst) {
+  return upb_deep_check(src, dst, false);
+}
+
+bool upb_MiniTable_Equals(const upb_MiniTable* src, const upb_MiniTable* dst) {
+  return upb_deep_check(src, dst, true);
+}
diff --git a/upb/wire/common.h b/upb/mini_table/compat.h
similarity index 69%
rename from upb/wire/common.h
rename to upb/mini_table/compat.h
index e9750fc..d79f99a 100644
--- a/upb/wire/common.h
+++ b/upb/mini_table/compat.h
@@ -28,20 +28,34 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-#ifndef UPB_WIRE_COMMON_H_
-#define UPB_WIRE_COMMON_H_
+#ifndef UPB_MINI_TABLE_COMPAT_H_
+#define UPB_MINI_TABLE_COMPAT_H_
+
+#include "upb/mini_table/message.h"
 
 // Must be last.
 #include "upb/port/def.inc"
 
+// upb does not support mixing minitables from different sources but these
+// functions are still used by some existing users so for now we make them
+// available here. This may or may not change in the future so do not add
+// them to new code.
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#define kUpb_WireFormat_DefaultDepthLimit 100
+// Checks if memory layout of src is compatible with dst.
+bool upb_MiniTable_Compatible(const upb_MiniTable* src,
+                              const upb_MiniTable* dst);
+
+// Checks equality of mini tables originating from different language runtimes.
+bool upb_MiniTable_Equals(const upb_MiniTable* src, const upb_MiniTable* dst);
 
 #ifdef __cplusplus
-}
+} /* extern "C" */
 #endif
 
-#endif  // UPB_WIRE_COMMON_H_
+#include "upb/port/undef.inc"
+
+#endif /* UPB_MINI_TABLE_COMPAT_H_ */
diff --git a/upb/wire/BUILD b/upb/wire/BUILD
index 5e916e5..008777f 100644
--- a/upb/wire/BUILD
+++ b/upb/wire/BUILD
@@ -30,7 +30,6 @@
     srcs = [
     ],
     hdrs = [
-        "common.h",
         "decode.h",
         "encode.h",
     ],
@@ -38,6 +37,7 @@
     visibility = ["//visibility:public"],
     deps = [
         ":internal",
+        ":types",
         "//:mem",
         "//:message",
         "//:mini_table",
@@ -48,7 +48,6 @@
 cc_library(
     name = "internal",
     srcs = [
-        "common.h",
         "decode.c",
         "decode.h",
         "decode_fast.c",
diff --git a/upb/wire/decode.c b/upb/wire/decode.c
index f8b0746..1f6e601 100644
--- a/upb/wire/decode.c
+++ b/upb/wire/decode.c
@@ -40,14 +40,12 @@
 #include "upb/message/internal/map_entry.h"
 #include "upb/mini_table/sub.h"
 #include "upb/port/atomic.h"
-#include "upb/wire/common.h"
 #include "upb/wire/encode.h"
 #include "upb/wire/eps_copy_input_stream.h"
 #include "upb/wire/internal/common.h"
 #include "upb/wire/internal/decode.h"
 #include "upb/wire/internal/swap.h"
 #include "upb/wire/reader.h"
-#include "upb/wire/types.h"
 
 // Must be last.
 #include "upb/port/def.inc"
diff --git a/upb/wire/decode.h b/upb/wire/decode.h
index 2bd32e7..4bd6eb8 100644
--- a/upb/wire/decode.h
+++ b/upb/wire/decode.h
@@ -36,7 +36,7 @@
 #include "upb/mem/arena.h"
 #include "upb/message/message.h"
 #include "upb/mini_table/extension_registry.h"
-#include "upb/wire/common.h"
+#include "upb/wire/types.h"
 
 // Must be last.
 #include "upb/port/def.inc"
diff --git a/upb/wire/encode.c b/upb/wire/encode.c
index b87d9cc..82429dd 100644
--- a/upb/wire/encode.c
+++ b/upb/wire/encode.c
@@ -39,10 +39,8 @@
 #include "upb/message/internal/accessors.h"
 #include "upb/message/internal/extension.h"
 #include "upb/mini_table/sub.h"
-#include "upb/wire/common.h"
 #include "upb/wire/internal/common.h"
 #include "upb/wire/internal/swap.h"
-#include "upb/wire/types.h"
 
 // Must be last.
 #include "upb/port/def.inc"
diff --git a/upb/wire/encode.h b/upb/wire/encode.h
index 1b27004..8212b87 100644
--- a/upb/wire/encode.h
+++ b/upb/wire/encode.h
@@ -34,7 +34,7 @@
 #define UPB_WIRE_ENCODE_H_
 
 #include "upb/message/message.h"
-#include "upb/wire/common.h"
+#include "upb/wire/types.h"
 
 // Must be last.
 #include "upb/port/def.inc"
diff --git a/upb/wire/types.h b/upb/wire/types.h
index ad9620b..b66bcfe 100644
--- a/upb/wire/types.h
+++ b/upb/wire/types.h
@@ -31,6 +31,8 @@
 #ifndef UPB_WIRE_TYPES_H_
 #define UPB_WIRE_TYPES_H_
 
+#define kUpb_WireFormat_DefaultDepthLimit 100
+
 // A list of types as they are encoded on the wire.
 typedef enum {
   kUpb_WireType_Varint = 0,
diff --git a/upbc/upbdev.h b/upbc/upbdev.h
index b33390d..047a1be 100644
--- a/upbc/upbdev.h
+++ b/upbc/upbdev.h
@@ -59,7 +59,7 @@
 
 // The following wrappers allow the protoc plugins to call the above functions
 // without pulling in the entire pb_runtime library.
-UPB_API upb_Arena* upbdev_Arena_New();
+UPB_API upb_Arena* upbdev_Arena_New(void);
 UPB_API void upbdev_Status_Clear(upb_Status* status);
 
 #ifdef __cplusplus