Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect string-merge results when code is compiled with clang #180

Open
davidlattimore opened this issue Oct 23, 2024 · 5 comments
Open

Comments

@davidlattimore
Copy link
Owner

When program_name_29___cpp_integration_cc__ is compiled with clang rather than gcc, it fails. Apparently all the merged strings end up as the string ELF. See #140. We should probably first add an option to allow overriding the compiler - i.e. #179.

@marxin
Copy link
Collaborator

marxin commented Oct 24, 2024

I cannot see the wrong output, what happens to be is /home/marxin/Programming/wild/wild/tests/build/cpp-integration.cc-model-large.wild crashes with:

❯ valgrind wild/tests/build/cpp-integration.cc-model-large.wild
==42077== Memcheck, a memory error detector
==42077== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==42077== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==42077== Command: /home/marxin/Programming/wild/wild/tests/build/cpp-integration.cc-model-large.wild
==42077== 
==42077== Invalid read of size 1
==42077==    at 0x48506A2: strlen (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==42077==    by 0x49DEF57: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*) (in /usr/lib64/libstdc++.so.6.0.33)
==42077==    by 0x4028F6: main (in /home/marxin/Programming/wild/wild/tests/build/cpp-integration.cc-model-large.wild)
==42077==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

Do we speak about the same issue?

@davidlattimore
Copy link
Owner Author

Interesting. I still get output even under valgrind:

valgrind wild/tests/build/cpp-integration.cc-model-large.wild
==13114== Memcheck, a memory error detector
==13114== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13114== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==13114== Command: wild/tests/build/cpp-integration.cc-model-large.wild
==13114== 
ELFELFELFELFELFELFELF
==13114== 
==13114== HEAP SUMMARY:
==13114==     in use at exit: 0 bytes in 0 blocks
==13114==   total heap usage: 4 allocs, 4 frees, 73,820 bytes allocated
==13114== 
==13114== All heap blocks were freed -- no leaks are possible
==13114== 
==13114== For lists of detected and suppressed errors, rerun with: -s
==13114== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

I just tried running in the OpenSUSE docker image and the test there didn't pass linker-diff validation:

wild: /wild/wild/tests/build/cpp-integration.cc-model-large.wild
ld: /wild/wild/tests/build/cpp-integration.cc-model-large.ld
asm._start
  ORIG            `/usr/lib64/gcc/x86_64-suse-linux/14/../../../../lib64/crt1.o`
                  endbr64
                  xor %ebp,%ebp
                  mov %rdx,%r9
                  pop %rsi
                  mov %rsp,%rdx
                  and $0xFFFFFFFFFFFFFFF0,%rsp
                  push %rax
                  push %rsp
                  xor %r8d,%r8d
                  xor %ecx,%ecx
  
  wild 0x00402778 48 c7 c7 90 28 40 00 mov $0x402890,%rdi  // Mov_rm64_imm32(0x402890) main
  ld   0x00401038 48 8b 3d 49 2f 00 00 mov 0x2F68,%rdi  // Mov_r64_rm64(0x403f88) GOT(main)
  ORIG            48 8b 3d 00 00 00 00 mov 0x1F,%rdi  // R_X86_64_REX_GOTPCRELX -> `main` -4
  TRACE           relaxation.kind=RexMovIndirectToAbsolute value_flags=ADDRESS | CAN_BYPASS_GOT resolution_flags=DIRECT
  
                  callq *0  // 0xAAA=DYNAMIC(__libc_start_main@GLIBC_2.34)
                  hlt

section.ltext._ZNSt11char_traitsIcE7compareEPKcS2_m.flags
  wild AXG
  ld AX

section.ltext._ZStneIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_.flags
  wild AXG
  ld AX

section.ltext._ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_.flags
  wild AXG
  ld AX

section.ltext._ZNSt11char_traitsIcE6lengthEPKc.flags
  wild AXG
  ld AX

We don't do anything special for .ltext sections and probably we should. Our support for large models is incomplete in other ways too.

@andrewdavidmackenzie
Copy link
Contributor

If I edit the source file test arguments to be:

//#Config:model-large:default
//#CompArgs:-mcmodel=large
//#LinkArgs:--cc=clang -Wl,-z,now

putting clang instead of gcc, the test fails thus:

thread 'main' panicked at /home/andrew/workspace/wild/wild/tests/sources/rust-integration.rs:21:5:
Make sure unwinding works
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at /home/andrew/workspace/wild/wild/tests/sources/rust-integration.rs:21:5:
Make sure unwinding works
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test integration_test::program_name_28___rust_integration_dynamic_rs__ ... ok
/usr/bin/ld: /home/andrew/workspace/wild/wild/tests/build/cpp-integration.model-large-8ead9704af940f25.o: in function `main':
cpp-integration.cc:(.text+0x40): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
/usr/bin/ld: cpp-integration.cc:(.text+0x6a): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)'
/usr/bin/ld: cpp-integration.cc:(.text+0x9b): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)'
/usr/bin/ld: cpp-integration.cc:(.text+0xc5): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)'
/usr/bin/ld: cpp-integration.cc:(.text+0xef): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)'
/usr/bin/ld: cpp-integration.cc:(.text+0x120): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)'
/usr/bin/ld: /home/andrew/workspace/wild/wild/tests/build/cpp-integration.model-large-8ead9704af940f25.o:cpp-integration.cc:(.text+0x14a): more undefined references to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)' follow
/usr/bin/ld: /home/andrew/workspace/wild/wild/tests/build/cpp-integration.model-large-8ead9704af940f25.o: in function `main':
cpp-integration.cc:(.text+0x1b6): undefined reference to `std::cout'
/usr/bin/ld: cpp-integration.cc:(.text+0x1ca): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: cpp-integration.cc:(.text+0x1d9): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: cpp-integration.cc:(.text+0x1f0): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: cpp-integration.cc:(.text+0x217): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/usr/bin/ld: cpp-integration.cc:(.text+0x24b): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/usr/bin/ld: /home/andrew/workspace/wild/wild/tests/build/cpp-integration.model-large-8ead9704af940f25.o: in function `bool std::operator==<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*)':
cpp-integration.cc:(.text._ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_[_ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_]+0x39): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
/usr/bin/ld: cpp-integration.cc:(.text._ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_[_ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_]+0x71): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
/usr/bin/ld: cpp-integration.cc:(.text._ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_[_ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_]+0x8d): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::data() const'
/usr/bin/ld: /home/andrew/workspace/wild/wild/tests/build/cpp-integration.model-large-8ead9704af940f25.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
test integration_test::program_name_29___cpp_integration_cc__ ... FAILED
thread 'main' panicked at /home/andrew/workspace/wild/wild/tests/sources/rust-integration.rs:21:5:
Make sure unwinding works
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at /home/andrew/workspace/wild/wild/tests/sources/rust-integration.rs:21:5:
Make sure unwinding works
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test integration_test::program_name_27___rust_integration_rs__ ... ok
test integration_test::program_name_26___libc_integration_c__ ... ok
test integration_test::program_name_30___rust_tls_rs__ ... ok

failures:

---- integration_test::program_name_29___cpp_integration_cc__ stdout ----
Error: Failed to build program `cpp-integration.cc` with linker `ld` config `model-large`

Caused by:
    Linker failed. Relink with:
    clang --ld-path=/usr/bin/ld -Wl,-z,now -o /home/andrew/workspace/wild/wild/tests/build/cpp-integration.cc-model-large.ld /home/andrew/workspace/wild/wild/tests/build/cpp-integration.model-large-8ead9704af940f25.o /home/andrew/workspace/wild/wild/tests/build/cpp-integration-2.model-large-d2a94c574f4beb01.o


failures:
    integration_test::program_name_29___cpp_integration_cc__

test result: FAILED. 29 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.53s

error: test failed, to rerun pass `-p wild --test integration_tests`

Would they be a reproducer for this issue, to allow me to tinker with it and see what the cause it?

@marxin
Copy link
Collaborator

marxin commented Dec 2, 2024

//#LinkArgs:--cc=clang -Wl,-z,now

Note this is about changing the linker wrapper that is used and does not influence what compiler is used for .o files creation. Plus, the error you face is caused by the fact you changed C++ wrapper with a C wrapper (instead of --cc=clang++).

Please try this:

diff --git a/wild/tests/sources/cpp-integration.cc b/wild/tests/sources/cpp-integration.cc
index ffcbf3c..385790b 100644
--- a/wild/tests/sources/cpp-integration.cc
+++ b/wild/tests/sources/cpp-integration.cc
@@ -16,6 +16,7 @@
 //#Config:model-large:default
 //#CompArgs:-mcmodel=large
 //#LinkArgs:--cc=g++ -Wl,-z,now
+//#Compiler: clang
 
 #include <iostream>
 #include <string>

@andrewdavidmackenzie
Copy link
Contributor

OK, got it reproducing same output now. Thanks.

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

No branches or pull requests

3 participants