From 58877b55e0796bcca743e9bd4d2be42092562f30 Mon Sep 17 00:00:00 2001
From: Mike Kruskal <mkruskal@google.com>
Date: Fri, 4 Aug 2023 16:44:48 -0700
Subject: [PATCH] Fixes a bug on 32-bit architectures with map decoding.

This also enables 32-bit tests and adds ASAN coverage for better diagnostics.

PiperOrigin-RevId: 553946734
---
 .github/workflows/bazel_tests.yml |  3 ++-
 upb/message/internal.h            |  2 ++
 upb/message/internal/map_entry.h  | 10 +++++++++-
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/bazel_tests.yml b/.github/workflows/bazel_tests.yml
index caa5289645..7694132c2d 100644
--- a/.github/workflows/bazel_tests.yml
+++ b/.github/workflows/bazel_tests.yml
@@ -26,7 +26,8 @@ jobs:
           - { NAME: "FastTable", BAZEL: bazel, CC: clang, os: ubuntu-20-large, flags: "--//:fasttable_enabled=true -- -cmake:test_generated_files" }
           - { NAME: "ASAN", BAZEL: bazel, CC: clang, os: ubuntu-20-large, flags: "--config=asan -c dbg -- -benchmarks:benchmark -python/..." }
           - { NAME: "UBSAN", BAZEL: bazel, CC: clang, os: ubuntu-20-large, flags: "--config=ubsan -c dbg -- -benchmarks:benchmark -python/... -lua/...", install: "libunwind-dev" }
-          - { NAME: "32-bit", BAZEL: bazel, CC: clang, os: ubuntu-20-large, flags: "--copt=-m32 --linkopt=-m32 -- -... benchmarks:benchmark ", install: "g++-multilib" }
+          - { NAME: "32-bit", BAZEL: bazel, CC: clang, os: ubuntu-20-large, flags: "--copt=-m32 --linkopt=-m32 -- benchmarks:benchmark -python/...", install: "g++-multilib" }
+          - { NAME: "32-bit ASAN", BAZEL: bazel, CC: clang, os: ubuntu-20-large, flags: "--config=asan -c dbg --copt=-m32 --linkopt=-m32 -- -//benchmarks/... -//python/... -//:message_copy_test -//:message_promote_test -//:message_test -//upb/test:test_generated_code", install: "g++-multilib" }
           - { NAME: "Windows", BAZEL: bazel, os: windows-2019, startup-flags: "--output_user_root=C:/tmp", flags: "--config=cpp17_msvc", targets: "upb/... upbc/... python/... protos/... protos_generator/..." }
           - { NAME: "macOS", BAZEL: bazel, CC: clang, os: macos-11 }
           # Current github runners are all Intel based, so just build/compile for Apple Silicon to detect issues there.
diff --git a/upb/message/internal.h b/upb/message/internal.h
index 95423c63f8..d142789511 100644
--- a/upb/message/internal.h
+++ b/upb/message/internal.h
@@ -86,6 +86,7 @@ typedef struct {
 } upb_Message_InternalData;
 
 typedef struct {
+  // LINT.IfChange(internal_layout)
   union {
     upb_Message_InternalData* internal;
 
@@ -93,6 +94,7 @@ typedef struct {
     // require 8-byte alignment.
     double d;
   };
+  // LINT.ThenChange(//depot/google3/third_party/upb/upb/message/internal/map_entry.h:internal_layout)
   /* Message data follows. */
 } upb_Message_Internal;
 
diff --git a/upb/message/internal/map_entry.h b/upb/message/internal/map_entry.h
index 95c7181f68..90161dced9 100644
--- a/upb/message/internal/map_entry.h
+++ b/upb/message/internal/map_entry.h
@@ -57,7 +57,15 @@ typedef struct {
 } upb_MapEntryData;
 
 typedef struct {
-  void* internal_data;
+  // LINT.IfChange(internal_layout)
+  union {
+    void* internal_data;
+
+    // Force 8-byte alignment, since the data members may contain members that
+    // require 8-byte alignment.
+    double d;
+  };
+  // LINT.ThenChange(//depot/google3/third_party/upb/upb/message/internal.h:internal_layout)
   upb_MapEntryData data;
 } upb_MapEntry;