Skip to content

Conversation

ndrewh
Copy link
Contributor

@ndrewh ndrewh commented Sep 24, 2025

Updates the tests in compiler-rt/lib/fuzzer/tests/ to use darwin_filter_host_archs to check which architectures we can run tests for on Darwin, and adds a target for each of them.

This mirrors the code in compiler-rtlib/asan/tests/

set(ASAN_TEST_ARCH ${ASAN_SUPPORTED_ARCH})
if(APPLE)
darwin_filter_host_archs(ASAN_SUPPORTED_ARCH ASAN_TEST_ARCH)
endif()
list(REMOVE_ITEM ASAN_TEST_ARCH sparcv9)
if(OS_NAME MATCHES "SunOS")
list(REMOVE_ITEM ASAN_TEST_ARCH x86_64)
endif()
foreach(arch ${ASAN_TEST_ARCH})

The current behavior to run tests on COMPILER_RT_DEFAULT_TARGET_ARCH is preserved on non-Apple platforms.

@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Andrew Haberlandt (ndrewh)

Changes

Updates the tests in compiler-rt/lib/fuzzer/tests/ to use darwin_filter_host_archs to check which architectures we can run tests for on Darwin, and adds a target for each of them.

This mirrors the code in compiler-rtlib/asan/tests/

set(ASAN_TEST_ARCH ${ASAN_SUPPORTED_ARCH})
if(APPLE)
darwin_filter_host_archs(ASAN_SUPPORTED_ARCH ASAN_TEST_ARCH)
endif()
list(REMOVE_ITEM ASAN_TEST_ARCH sparcv9)
if(OS_NAME MATCHES "SunOS")
list(REMOVE_ITEM ASAN_TEST_ARCH x86_64)
endif()
foreach(arch ${ASAN_TEST_ARCH})

The current behavior to run tests on COMPILER_RT_DEFAULT_TARGET_ARCH is preserved on non-Apple platforms.


Full diff: https://github.com/llvm/llvm-project/pull/160578.diff

1 Files Affected:

  • (modified) compiler-rt/lib/fuzzer/tests/CMakeLists.txt (+49-42)
diff --git a/compiler-rt/lib/fuzzer/tests/CMakeLists.txt b/compiler-rt/lib/fuzzer/tests/CMakeLists.txt
index c5885ccccd207..39b8648db3182 100644
--- a/compiler-rt/lib/fuzzer/tests/CMakeLists.txt
+++ b/compiler-rt/lib/fuzzer/tests/CMakeLists.txt
@@ -63,51 +63,58 @@ if ("-fvisibility=hidden" IN_LIST LIBFUZZER_CFLAGS)
   list(APPEND LIBFUZZER_UNITTEST_CFLAGS "-fvisibility=hidden")
 endif()
 
-if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
-  # libFuzzer unit tests are only run on the host machine.
-  set(arch ${COMPILER_RT_DEFAULT_TARGET_ARCH})
-
-  set(LIBFUZZER_TEST_RUNTIME RTFuzzerTest.${arch})
-  if(APPLE)
-    set(LIBFUZZER_TEST_RUNTIME_OBJECTS
-      $<TARGET_OBJECTS:RTfuzzer.osx>)
+if(COMPILER_RT_CAN_EXECUTE_TESTS)
+  set(FUZZER_TEST_ARCH ${FUZZER_SUPPORTED_ARCH})
+  if (APPLE)
+    darwin_filter_host_archs(FUZZER_SUPPORTED_ARCH FUZZER_TEST_ARCH)
   else()
-    set(LIBFUZZER_TEST_RUNTIME_OBJECTS
-      $<TARGET_OBJECTS:RTfuzzer.${arch}>)
+    # libFuzzer unit tests are only run on the host machine.
+    set(FUZZER_TEST_ARCH ${COMPILER_RT_DEFAULT_TARGET_ARCH})
   endif()
-  add_library(${LIBFUZZER_TEST_RUNTIME} STATIC
-    ${LIBFUZZER_TEST_RUNTIME_OBJECTS})
-  set_target_properties(${LIBFUZZER_TEST_RUNTIME} PROPERTIES
-    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
-    FOLDER "Compiler-RT/Tests/Runtime")
 
-  if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
-     COMPILER_RT_LIBCXX_PATH AND
-     COMPILER_RT_LIBCXXABI_PATH)
-    file(GLOB libfuzzer_headers ../*.h)
-    set(LIBFUZZER_TEST_RUNTIME_DEPS libcxx_fuzzer_${arch}-install-cmake326-workaround ${libfuzzer_headers})
-    set(LIBFUZZER_TEST_RUNTIME_CFLAGS -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
-    set(LIBFUZZER_TEST_RUNTIME_LINK_FLAGS ${LIBCXX_${arch}_PREFIX}/lib/libc++.a)
-  endif()
+  foreach(arch ${FUZZER_TEST_ARCH})
+    set(LIBFUZZER_TEST_RUNTIME RTFuzzerTest.${arch})
+    if(APPLE)
+      set(LIBFUZZER_TEST_RUNTIME_OBJECTS
+        $<TARGET_OBJECTS:RTfuzzer.osx>)
+    else()
+      set(LIBFUZZER_TEST_RUNTIME_OBJECTS
+        $<TARGET_OBJECTS:RTfuzzer.${arch}>)
+    endif()
+    add_library(${LIBFUZZER_TEST_RUNTIME} STATIC
+      ${LIBFUZZER_TEST_RUNTIME_OBJECTS})
+    set_target_properties(${LIBFUZZER_TEST_RUNTIME} PROPERTIES
+      ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+      FOLDER "Compiler-RT/Tests/Runtime")
+
+    if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
+      COMPILER_RT_LIBCXX_PATH AND
+      COMPILER_RT_LIBCXXABI_PATH)
+      file(GLOB libfuzzer_headers ../*.h)
+      set(LIBFUZZER_TEST_RUNTIME_DEPS libcxx_fuzzer_${arch}-install-cmake326-workaround ${libfuzzer_headers})
+      set(LIBFUZZER_TEST_RUNTIME_CFLAGS -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
+      set(LIBFUZZER_TEST_RUNTIME_LINK_FLAGS ${LIBCXX_${arch}_PREFIX}/lib/libc++.a)
+    endif()
 
-  set(FuzzerTestObjects)
-  generate_compiler_rt_tests(FuzzerTestObjects
-    FuzzerUnitTests "Fuzzer-${arch}-Test" ${arch}
-    SOURCES FuzzerUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
-    RUNTIME ${LIBFUZZER_TEST_RUNTIME}
-    DEPS ${LIBFUZZER_TEST_RUNTIME_DEPS}
-    CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
-    LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
-  set_target_properties(FuzzerUnitTests PROPERTIES
-    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
+    set(FuzzerTestObjects)
+    generate_compiler_rt_tests(FuzzerTestObjects
+      FuzzerUnitTests "Fuzzer-${arch}-Test" ${arch}
+      SOURCES FuzzerUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
+      RUNTIME ${LIBFUZZER_TEST_RUNTIME}
+      DEPS ${LIBFUZZER_TEST_RUNTIME_DEPS}
+      CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
+      LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
+    set_target_properties(FuzzerUnitTests PROPERTIES
+      RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
 
-  set(FuzzedDataProviderTestObjects)
-  generate_compiler_rt_tests(FuzzedDataProviderTestObjects
-    FuzzedDataProviderUnitTests "FuzzerUtils-${arch}-Test" ${arch}
-    SOURCES FuzzedDataProviderUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
-    DEPS ${LIBFUZZER_TEST_RUNTIME_DEPS} ${COMPILER_RT_SOURCE_DIR}/include/fuzzer/FuzzedDataProvider.h
-    CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
-    LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
-  set_target_properties(FuzzedDataProviderUnitTests PROPERTIES
-    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
+    set(FuzzedDataProviderTestObjects)
+    generate_compiler_rt_tests(FuzzedDataProviderTestObjects
+      FuzzedDataProviderUnitTests "FuzzerUtils-${arch}-Test" ${arch}
+      SOURCES FuzzedDataProviderUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
+      DEPS ${LIBFUZZER_TEST_RUNTIME_DEPS} ${COMPILER_RT_SOURCE_DIR}/include/fuzzer/FuzzedDataProvider.h
+      CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
+      LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
+    set_target_properties(FuzzedDataProviderUnitTests PROPERTIES
+      RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
+  endforeach()
 endif()

@padriff
Copy link
Contributor

padriff commented Sep 24, 2025

You may want to split this into two commits - the first with just the semantic change, the second with the clang-format changes to make it easier to understand the exact diffs

@ndrewh ndrewh force-pushed the libfuzzer-fix-apple-tests branch from 0f36f25 to 76ba012 Compare September 24, 2025 23:15
Copy link
Contributor

@wrotki wrotki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo the comment nit

Copy link
Contributor

@DanBlackwell DanBlackwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the endforeach() indentation is correct, otherwise LGTM

@Meinersbur Meinersbur removed their request for review September 25, 2025 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants