try_run() crashes with segmentation fault when cross compiling
Executing the CMakeLists.txt
cmake_minimum_required(VERSION 3.25)
project(bug)
include(CheckCSourceRuns)
check_c_source_runs("int main() { return 0; }" SEGFAULT_HERE)
with cmake -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=whatever .
will cause a segmentation fault on Debian unstable amd64. I tested the latest CMake 3.25.1.
Relevant excerpt of GDB backtrace:
#0 __memcpy_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:326
#1 0x00005597d02cb748 in std::char_traits<char>::copy (__n=2, __s2=0x14 <error: Cannot access memory at address 0x14>, __s1=<optimized out>) at /usr/include/c++/12/bits/char_traits.h:431
#2 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy (__n=2, __s=0x14 <error: Cannot access memory at address 0x14>, __d=<optimized out>)
at /usr/include/c++/12/bits/basic_string.h:423
#3 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy (__n=2, __s=0x14 <error: Cannot access memory at address 0x14>, __d=<optimized out>)
at /usr/include/c++/12/bits/basic_string.h:418
#4 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars (__k2=0x16 <error: Cannot access memory at address 0x16>,
__k1=0x14 <error: Cannot access memory at address 0x14>, __p=<optimized out>) at /usr/include/c++/12/bits/basic_string.h:471
#5 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*> (__end=0x16 <error: Cannot access memory at address 0x16>,
__beg=0x14 <error: Cannot access memory at address 0x14>, this=0x7ffda48d69e0) at /usr/include/c++/12/bits/basic_string.tcc:243
#6 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (__str=<error: Cannot access memory at address 0x14>, this=0x7ffda48d69e0)
at /usr/include/c++/12/bits/basic_string.h:544
#7 std::operator+<char, std::char_traits<char>, std::allocator<char> > (__rhs=0x5597d06bad3f "\n", __lhs=<error: Cannot access memory at address 0x14>)
at /usr/include/c++/12/bits/basic_string.h:3475
#8 (anonymous namespace)::TryRunCommandImpl::DoNotRunExecutable (this=0x7ffda48d6df0, runArgs="", srcFile=<error: Cannot access memory at address 0x14>,
compileResultVariable="SEGFAULT_HERE_COMPILED", out=0x7ffda48d6d70, stdOut=0x0, stdErr=0x0) at ./Source/cmTryRunCommand.cxx:426
#9 0x00005597d02cd272 in (anonymous namespace)::TryRunCommandImpl::TryRunCode (argv=std::vector of length -1456776407141, capacity -1456775049074 = {...}, this=0x7ffda48d6df0)
at /usr/include/c++/12/optional:306
#10 cmTryRunCommand (args=std::vector of length 15, capacity 18 = {...}, status=...) at ./Source/cmTryRunCommand.cxx:494
#11 0x00005597d010676e in InvokeBuiltinCommand (status=..., args=...,
command=0x5597d02cbf70 <cmTryRunCommand(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, cmExecutionStatus&)>) at ./Source/cmState.cxx:417
#12 operator() (status=..., args=..., __closure=<optimized out>) at ./Source/cmState.cxx:427
#13 std::__invoke_impl<bool, cmState::AddBuiltinCommand(const std::string&, BuiltinCommand)::<lambda(const std::vector<cmListFileArgument>&, cmExecutionStatus&)>&, const std::vector<cmListFileArgument, std::allocator<cmListFileArgument> >&, cmExecutionStatus&> (__f=...) at /usr/include/c++/12/bits/invoke.h:61
#14 std::__invoke_r<bool, cmState::AddBuiltinCommand(const std::string&, BuiltinCommand)::<lambda(const std::vector<cmListFileArgument>&, cmExecutionStatus&)>&, const std::vector<cmListFileArgument, std::allocator<cmListFileArgument> >&, cmExecutionStatus&> (__fn=...) at /usr/include/c++/12/bits/invoke.h:114
#15 std::_Function_handler<bool(const std::vector<cmListFileArgument, std::allocator<cmListFileArgument> >&, cmExecutionStatus&), cmState::AddBuiltinCommand(const std::string&, BuiltinCommand)::<lambda(const std::vector<cmListFileArgument, std::allocator<cmListFileArgument> >&, cmExecutionStatus&)> >::_M_invoke(const std::_Any_data &, const std::vector<cmListFileArgument, std::allocator<cmListFileArgument> > &, cmExecutionStatus &) (__functor=..., __args#0=..., __args#1=...) at /usr/include/c++/12/bits/std_function.h:290
#16 0x00005597d00b2b98 in std::function<bool (std::vector<cmListFileArgument, std::allocator<cmListFileArgument> > const&, cmExecutionStatus&)>::operator()(std::vector<cmListFileArgument, std::allocator<cmListFileArgument> > const&, cmExecutionStatus&) const (__args#1=..., __args#0=..., this=0x7ffda48d74b0) at /usr/include/c++/12/bits/std_function.h:591
#17 cmMakefile::ExecuteCommand (this=this@entry=0x5597d28333f0, lff=..., status=..., deferId=std::optional<std::string> [no contained value]) at ./Source/cmMakefile.cxx:457
#18 0x00005597d023845d in cmIfFunctionBlocker::Replay (this=0x5597d2ad5100, functions=..., inStatus=...) at ./Source/cmIfCommand.cxx:151
#19 0x00005597d022f62f in cmFunctionBlocker::IsFunctionBlocked (this=0x5597d2ad5100, lff=..., status=...) at ./Source/cmFunctionBlocker.cxx:52
#20 0x00005597d00b2159 in cmMakefile::IsFunctionBlocked (status=..., lff=..., this=<optimized out>) at ./Source/cmMakefile.cxx:3222
#21 cmMakefile::ExecuteCommand (this=this@entry=0x5597d28333f0, lff=..., status=..., deferId=std::optional<std::string> [no contained value]) at ./Source/cmMakefile.cxx:416
#22 0x00005597d02310f6 in (anonymous namespace)::cmFunctionHelperCommand::operator() (this=0x5597d2ae9180, args=..., inStatus=...) at ./Source/cmFunctionCommand.cxx:115
#23 0x00005597d00b2b98 in std::function<bool (std::vector<cmListFileArgument, std::allocator<cmListFileArgument> > const&, cmExecutionStatus&)>::operator()(std::vector<cmListFileArgument, std::allocator<cmListFileArgument> > const&, cmExecutionStatus&) const (__args#1=..., __args#0=..., this=0x7ffda48d8230) at /usr/include/c++/12/bits/std_function.h:591
#24 cmMakefile::ExecuteCommand (this=this@entry=0x5597d28333f0, lff=..., status=..., deferId=std::optional<std::string> [no contained value]) at ./Source/cmMakefile.cxx:457
#25 0x00005597d0280dad in (anonymous namespace)::cmMacroHelperCommand::operator() (this=<optimized out>, args=..., inStatus=...) at ./Source/cmMacroCommand.cxx:121
#26 0x00005597d00b2b98 in std::function<bool (std::vector<cmListFileArgument, std::allocator<cmListFileArgument> > const&, cmExecutionStatus&)>::operator()(std::vector<cmListFileArgument, std::allocator<cmListFileArgument> > const&, cmExecutionStatus&) const (__args#1=..., __args#0=..., this=0x7ffda48d8930) at /usr/include/c++/12/bits/std_function.h:591
#27 cmMakefile::ExecuteCommand (this=0x5597d28333f0, lff=..., status=..., deferId=...) at ./Source/cmMakefile.cxx:457
[...snipped...]
Relevant excerpt from --trace-expand
:
[...snipped...]
/usr/share/cmake-3.25/Modules/Internal/CheckSourceRuns.cmake(90): message(CHECK_START Performing Test SEGFAULT_HERE )
-- Performing Test SEGFAULT_HERE
/usr/share/cmake-3.25/Modules/Internal/CheckSourceRuns.cmake(92): string(APPEND _source
)
/usr/share/cmake-3.25/Modules/Internal/CheckSourceRuns.cmake(93): try_run(SEGFAULT_HERE_EXITCODE SEGFAULT_HERE_COMPILED SOURCE_FROM_VAR src.c _source COMPILE_DEFINITIONS -DSEGFAULT_HERE CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING= -DCMAKE_SKIP_RPATH:BOOL=NO COMPILE_OUTPUT_VARIABLE OUTPUT RUN_OUTPUT_VARIABLE RUN_OUTPUT )
CMake Warning (dev) at /usr/share/cmake-3.25/Modules/Internal/CheckSourceRuns.cmake:93 (try_run):
Policy CMP0066 is not set: Honor per-config flags in try_compile()
source-file signature. Run "cmake --help-policy CMP0066" for policy
details. Use the cmake_policy command to set the policy and suppress this
warning.
For compatibility with older versions of CMake, try_compile is not honoring
caller config-specific compiler flags (e.g. CMAKE_C_FLAGS_DEBUG) in the
test project.
Call Stack (most recent call first):
/usr/share/cmake-3.25/Modules/CheckCSourceRuns.cmake:76 (cmake_check_source_runs)
CMakeLists.txt:4 (check_c_source_runs)
This warning is for project developers. Use -Wno-dev to suppress it.
CMake Warning (dev) at /usr/share/cmake-3.25/Modules/Internal/CheckSourceRuns.cmake:93 (try_run):
Policy CMP0056 is not set: Honor link flags in try_compile() source-file
signature. Run "cmake --help-policy CMP0056" for policy details. Use the
cmake_policy command to set the policy and suppress this warning.
For compatibility with older versions of CMake, try_compile is not honoring
caller link flags (e.g. CMAKE_EXE_LINKER_FLAGS) in the test project.
Call Stack (most recent call first):
/usr/share/cmake-3.25/Modules/CheckCSourceRuns.cmake:76 (cmake_check_source_runs)
CMakeLists.txt:4 (check_c_source_runs)
This warning is for project developers. Use -Wno-dev to suppress it.
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_M_create
Edited by Timo Röhling