Skip to content

NFC: Fix gmock integration in unit tests#8879

Merged
stevenfontanella merged 1 commit into
mainfrom
gmock
Jul 1, 2026
Merged

NFC: Fix gmock integration in unit tests#8879
stevenfontanella merged 1 commit into
mainfrom
gmock

Conversation

@stevenfontanella

@stevenfontanella stevenfontanella commented Jun 30, 2026

Copy link
Copy Markdown
Member

Tested with the following code which passes (in arena.cpp, not in source-map.cpp which would have already worked previously due to how it was handled in the build file):

#include "gmock/gmock-matchers.h"
...
EXPECT_THAT(1, ::testing::Eq(1));

This was written by Gemini. I didn't quite understand the fix but I figured it's worth doing to make tests easier to write. Below is Gemini's description of the fix.


Previously, when BUILD_FUZZTEST=ON, the fuzztest dependency would download its own newer version of googletest (v1.14.0) via CMake FetchContent. The gmock headers bundled in Binaryen's third_party/googletest/googlemock were incompatible with this newer googletest. This caused compilation failures for tests using gmock. As a workaround, source-map.cpp was completely excluded from the test suite during fuzztest builds.

This fix works because fuzztest's FetchContent actually makes the gmock target available as well. By linking binaryen-unittests against the gmock target when BUILD_FUZZTEST=ON, CMake correctly configures the include paths to use the newly fetched gmock headers instead of the bundled ones, seamlessly resolving the incompatibility.

  • Unconditionally include source-map.cpp in unit tests, rather than conditionally disabling it when BUILD_FUZZTEST is ON.
  • Link against the gmock target for binaryen-unittests when BUILD_FUZZTEST is ON.

Previously, when `BUILD_FUZZTEST=ON`, the `fuzztest` dependency would download its own newer version of `googletest` (v1.14.0) via CMake `FetchContent`. The `gmock` headers bundled in Binaryen's `third_party/googletest/googlemock` were incompatible with this newer `googletest`. This caused compilation failures for tests using `gmock`. As a workaround, `source-map.cpp` was completely excluded from the test suite during fuzztest builds.

This fix works because `fuzztest`'s `FetchContent` actually makes the `gmock` target available as well. By linking `binaryen-unittests` against the `gmock` target when `BUILD_FUZZTEST=ON`, CMake correctly configures the include paths to use the newly fetched `gmock` headers instead of the bundled ones, seamlessly resolving the incompatibility.

- Unconditionally include `source-map.cpp` in unit tests, rather than conditionally disabling it when `BUILD_FUZZTEST` is ON.
- Link against the `gmock` target for `binaryen-unittests` when `BUILD_FUZZTEST` is ON.
- Add a simple gmock test in `arena.cpp` to demonstrate that it correctly compiles and links.
@stevenfontanella stevenfontanella changed the title Fix gmock integration when BUILD_FUZZTEST is ON NFC: Fix gmock integration in unit tests Jun 30, 2026
@stevenfontanella
stevenfontanella marked this pull request as ready for review June 30, 2026 21:28
@stevenfontanella
stevenfontanella requested a review from a team as a code owner June 30, 2026 21:28
@stevenfontanella
stevenfontanella requested review from aheejin and removed request for a team June 30, 2026 21:29
@stevenfontanella
stevenfontanella merged commit 8c3dfa2 into main Jul 1, 2026
16 checks passed
@stevenfontanella
stevenfontanella deleted the gmock branch July 1, 2026 04:32
stevenfontanella added a commit that referenced this pull request Jul 22, 2026
Part of #8615. Fixes #8833 which was wrong. The earlier fix never
touched `newTypeEffects` at all. It passed tests and fixed the breakage
only incidentally by being maximally conservative and losing all
indirect call effects in the case of a type update. Fix this logic and
add unit tests to vet this code better.

Drive-by fixes:
* Clear indirectCallEffects during --discard-global-effects, before
recomputing global effects, and if a pass's `addsEffects()` is true.
Clearing it when recomputing global effects is necessary to remove stale
entries for types that no longer exist after type rewriting (although it
should make no difference to optimizations).
* Fix use-after-free in tests due to static storage of HeapTypes / Types
+ destroyAllTypesForTestingPurposesOnly. The static HeapType / Type in
`getMutI8Array` and similar functions referred to an index in
`globalHeapTypeStore` / `globalTupleStore`, which gets cleared in unit
tests with `destroyAllTypesForTestingPurposesOnly()`, causing the index
to refer to garbage. Change these to not use static storage so that
they're always in sync with `globalHeapTypeStore` / `globalTupleStore`.
* More changes to support gmock. #8879 partially addressed this but I
still hit some linker errors when adding new test code using gmock. The
issue is that the earlier PR didn't change anything when BUILD_FUZZTEST
is false.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants