Skip to content
New issue

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

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

Already on GitHub? # to your account

std::cout isn't initialized before any user-defined static initialization #60

Open
moshimo2019 opened this issue Jan 8, 2024 · 2 comments

Comments

@moshimo2019
Copy link

moshimo2019 commented Jan 8, 2024

clang version 14.0.7
bazel 5.3.0

The minimal source code source.cc is

#include <iostream>

struct S {
  S() { 
    // std::ios_base::Init init;
    std::cout << "static cout" << std::endl; 
  }
};

static S var_;

int main() {
  return 0;
}

BUILD.bazel

cc_binary(
    name = "source",
    srcs = ["source.cc"],
)

build command:

build --repo_env="ANDROID_NDK_HOME=/**/android-ndk-r25c" --cpu=arm64-v8a --crosstool_top=@androidndk//:toolchain --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --compilation_mode=opt --copt='-O3' --cxxopt='-std=c++17'

Running the binary in development board got Segmentation fault. Uncomment the line // std::ios_base::Init init;, running is successful.
It seems that the global object std::cost didn't initialized at that time. However, if I directly invoke clang by android-ndk-r25c/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android23-clang --std=c++17 src/static-test.cc -o bazel-bin/static-clang.i -lstdc++, the Segmentation fault didn't happen.
related issues:
llvm/llvm-project#29324
WebAssembly/wasi-sdk#153

@DanAlbert
Copy link

This isn't a bazel issue, nor even a bug. Static initialization order is not defined. You need explicit initialization if you care about order. That's not our choice, that's how C++ works.

@DanAlbert
Copy link

Reading through to the LLVM bug you linked, I guess LLVM has some sort of attribute to hack around that? idk how that's implemented so it may not be portable. If you want your code to be portable, don't rely on static initialization order.

In any case, that fix isn't needed in bazel, you just need a new enough NDK.

tomrenn pushed a commit to tomrenn/rules_android_ndk that referenced this issue Dec 4, 2024
PiperOrigin-RevId: 518334951
Change-Id: I82c56f59772959be2c9750b5c4adc236dbf2ac4d
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants