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

[clang-cl] access violation with an extern reference at runtime with clang-cl 20.1.0-rc2 using /std:c++latest #127572

Closed
Mishura4 opened this issue Feb 18, 2025 · 5 comments
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" miscompilation

Comments

@Mishura4
Copy link

Mishura4 commented Feb 18, 2025

// test.hpp

#include <vector>
#include <string>

extern const std::vector<std::string>& vec_ref;

void init();
// test.cpp
#include "test.hpp"

namespace
{

std::vector<std::string> impl;

}

const std::vector<std::string>& vec_ref = impl;

void init() {
    std::vector<std::string> vec;

    vec.push_back("hello");
    vec.push_back("world!");

    impl = std::move(vec);
}
// main.cpp
#include "test.hpp"

#include <iostream>

int main()
{
    init();
    for (auto& str : vec_ref)
    {
        std::cout << str << std::endl;
    }
}

Compile with clang-cl test.cpp main.cpp /std:c++latest. For some reason either vec_ref or impl seems to become corrupted and the for loop seems to read garbage data. In the project I initially encountered this, the program crashed with an access violation in std::vector::begin().

Observations:

  • The issue seems to be with /std:c++latest, the same code works without an issue on 20.1.0-rc2 with /std:c++20
  • The same code works without an issue on 19.1.7 even with /std:c++latest.

clang-cl was compiled with:

set CXX=clang-cl
set CC=clang-cl
set VECTORIZE="-march=native -mtune=native"

cmake llvm/ -B build ^
  -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;lldb" ^
  -DLLVM_ENABLE_RUNTIMES="compiler-rt;libcxx" ^
  -DLLVM_TARGETS_TO_BUILD=Native ^
  -DLLVM_PARALLEL_COMPILE_JOBS=30 ^
  -DLLVM_PARALLEL_LINK_JOBS=3 ^
  -DLLVM_ENABLE_LLD=on ^
  -DCLANG_DEFAULT_LINKER=lld ^
  -DCMAKE_BUILD_TYPE=Release ^
  -DCLANG_ENABLE_BOOTSTRAP=On ^
  -DLLVM_HOST_TRIPLE=x86_64-pc-windows-msvc ^
  -DLLVM_ENABLE_LTO=Thin ^
  -DLLVM_ENABLE_EH=ON ^
  -DLLVM_ENABLE_RTTI=ON ^
  -DLLVM_ENABLE_LIBXML2=ON ^
  -DCMAKE_CXX_STANDARD=20 ^
  -DBOOTSTRAP_CMAKE_CXX_FLAGS=%VECTORIZE% ^
  -DBOOTSTRAP_CMAKE_C_FLAGS=%VECTORIZE% ^
  -DBOOTSTRAP_CMAKE_EXE_LINKER_FLAGS=%VECTORIZE% ^
  -DRUNTIMES_x86_64-pc-windows-msvc_LLVM_ENABLE_EH=ON ^
  -DRUNTIMES_x86_64-pc-windows-msvc_LLVM_ENABLE_RTTI=ON ^
  -DRUNTIMES_x86_64-pc-windows-msvc_LLVM_ENABLE_LIBXML2=ON ^
  -DCMAKE_MT=mt.exe ^
  -G Ninja

ninja -C build

(Issue also happens with -fuse-ld=link)

@dtcxzyw
Copy link
Member

dtcxzyw commented Feb 18, 2025

It may be duplicate of #127475

@dtcxzyw dtcxzyw added clang:codegen IR generation bugs: mangling, exceptions, etc. miscompilation and removed new issue labels Feb 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/issue-subscribers-clang-codegen

Author: Miuna (Mishura4)

```c++ // test.hpp

#include <vector>
#include <string>

extern const std::vector<std::string>& vec_ref;

void init();


```c++
// test.cpp
#include "test.hpp"

namespace
{

std::vector&lt;std::string&gt; impl;

}

const std::vector&lt;std::string&gt;&amp; vec_ref = impl;

void init() {
    std::vector&lt;std::string&gt; vec;

    vec.push_back("hello");
    vec.push_back("world!");

    impl = std::move(vec);
}
// main.cpp
#include "test.hpp"

#include &lt;iostream&gt;

int main()
{
    init();
    for (auto&amp; str : vec_ref)
    {
        std::cout &lt;&lt; str &lt;&lt; std::endl;
    }
}

Compile with clang-cl test.cpp main.cpp /std:c++latest. For some reason either vec_ref or impl seems to become corrupted and the for loop seems to read garbage data. In the project I initially encountered this, the program crashed with an access violation in std::vector::begin().

Observations:

  • The issue seems to be with /std:c++latest, the same code works without an issue on 20.1.0-rc2 with /std:c++20
  • The same code works without an issue on 19.1.7 even with /std:c++latest.

clang-cl was compiled with:

set CXX=clang-cl
set CC=clang-cl
set VECTORIZE="-march=native -mtune=native"

cmake llvm/ -B build ^
  -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;lldb" ^
  -DLLVM_ENABLE_RUNTIMES="compiler-rt;libcxx" ^
  -DLLVM_TARGETS_TO_BUILD=Native ^
  -DLLVM_PARALLEL_COMPILE_JOBS=30 ^
  -DLLVM_PARALLEL_LINK_JOBS=3 ^
  -DLLVM_ENABLE_LLD=on ^
  -DCLANG_DEFAULT_LINKER=lld ^
  -DCMAKE_BUILD_TYPE=Release ^
  -DCLANG_ENABLE_BOOTSTRAP=On ^
  -DLLVM_HOST_TRIPLE=x86_64-pc-windows-msvc ^
  -DLLVM_ENABLE_LTO=Thin ^
  -DLLVM_ENABLE_EH=ON ^
  -DLLVM_ENABLE_RTTI=ON ^
  -DLLVM_ENABLE_LIBXML2=ON ^
  -DCMAKE_CXX_STANDARD=20 ^
  -DBOOTSTRAP_CMAKE_CXX_FLAGS=%VECTORIZE% ^
  -DBOOTSTRAP_CMAKE_C_FLAGS=%VECTORIZE% ^
  -DBOOTSTRAP_CMAKE_EXE_LINKER_FLAGS=%VECTORIZE% ^
  -DRUNTIMES_x86_64-pc-windows-msvc_LLVM_ENABLE_EH=ON ^
  -DRUNTIMES_x86_64-pc-windows-msvc_LLVM_ENABLE_RTTI=ON ^
  -DRUNTIMES_x86_64-pc-windows-msvc_LLVM_ENABLE_LIBXML2=ON ^
  -DCMAKE_MT=mt.exe ^
  -G Ninja

ninja -C build

(Issue also happens with -fuse-ld=link)

@Mishura4
Copy link
Author

Yep, looks very similar

@Mishura4 Mishura4 changed the title [clang-cl] access violation at runtime with clang-cl 20.1.0-rc2 using /std:c++latest [clang-cl] access violation with a global reference at runtime with clang-cl 20.1.0-rc2 using /std:c++latest Feb 18, 2025
@Mishura4 Mishura4 changed the title [clang-cl] access violation with a global reference at runtime with clang-cl 20.1.0-rc2 using /std:c++latest [clang-cl] access violation with an extern reference at runtime with clang-cl 20.1.0-rc2 using /std:c++latest Feb 18, 2025
@Mishura4
Copy link
Author

Mishura4 commented Mar 23, 2025

Fixed in 9010db1

@EugeneZelenko EugeneZelenko added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Mar 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 23, 2025

@llvm/issue-subscribers-clang-frontend

Author: Miuna (Mishura4)

```c++ // test.hpp

#include <vector>
#include <string>

extern const std::vector<std::string>& vec_ref;

void init();


```c++
// test.cpp
#include "test.hpp"

namespace
{

std::vector&lt;std::string&gt; impl;

}

const std::vector&lt;std::string&gt;&amp; vec_ref = impl;

void init() {
    std::vector&lt;std::string&gt; vec;

    vec.push_back("hello");
    vec.push_back("world!");

    impl = std::move(vec);
}
// main.cpp
#include "test.hpp"

#include &lt;iostream&gt;

int main()
{
    init();
    for (auto&amp; str : vec_ref)
    {
        std::cout &lt;&lt; str &lt;&lt; std::endl;
    }
}

Compile with clang-cl test.cpp main.cpp /std:c++latest. For some reason either vec_ref or impl seems to become corrupted and the for loop seems to read garbage data. In the project I initially encountered this, the program crashed with an access violation in std::vector::begin().

Observations:

  • The issue seems to be with /std:c++latest, the same code works without an issue on 20.1.0-rc2 with /std:c++20
  • The same code works without an issue on 19.1.7 even with /std:c++latest.

clang-cl was compiled with:

set CXX=clang-cl
set CC=clang-cl
set VECTORIZE="-march=native -mtune=native"

cmake llvm/ -B build ^
  -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;lldb" ^
  -DLLVM_ENABLE_RUNTIMES="compiler-rt;libcxx" ^
  -DLLVM_TARGETS_TO_BUILD=Native ^
  -DLLVM_PARALLEL_COMPILE_JOBS=30 ^
  -DLLVM_PARALLEL_LINK_JOBS=3 ^
  -DLLVM_ENABLE_LLD=on ^
  -DCLANG_DEFAULT_LINKER=lld ^
  -DCMAKE_BUILD_TYPE=Release ^
  -DCLANG_ENABLE_BOOTSTRAP=On ^
  -DLLVM_HOST_TRIPLE=x86_64-pc-windows-msvc ^
  -DLLVM_ENABLE_LTO=Thin ^
  -DLLVM_ENABLE_EH=ON ^
  -DLLVM_ENABLE_RTTI=ON ^
  -DLLVM_ENABLE_LIBXML2=ON ^
  -DCMAKE_CXX_STANDARD=20 ^
  -DBOOTSTRAP_CMAKE_CXX_FLAGS=%VECTORIZE% ^
  -DBOOTSTRAP_CMAKE_C_FLAGS=%VECTORIZE% ^
  -DBOOTSTRAP_CMAKE_EXE_LINKER_FLAGS=%VECTORIZE% ^
  -DRUNTIMES_x86_64-pc-windows-msvc_LLVM_ENABLE_EH=ON ^
  -DRUNTIMES_x86_64-pc-windows-msvc_LLVM_ENABLE_RTTI=ON ^
  -DRUNTIMES_x86_64-pc-windows-msvc_LLVM_ENABLE_LIBXML2=ON ^
  -DCMAKE_MT=mt.exe ^
  -G Ninja

ninja -C build

(Issue also happens with -fuse-ld=link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" miscompilation
Projects
None yet
Development

No branches or pull requests

4 participants