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

[HLSL] Enable protection code for destruction of local statics #106130

Open
pow2clk opened this issue Aug 26, 2024 · 1 comment
Open

[HLSL] Enable protection code for destruction of local statics #106130

pow2clk opened this issue Aug 26, 2024 · 1 comment
Labels
clang:codegen HLSL HLSL Language Support

Comments

@pow2clk
Copy link
Contributor

pow2clk commented Aug 26, 2024

The usual way to set up a destructor call for a local static is to use atexit, but HLSL doesn't have one of those. Instead, we currently add the destructor to the global destructor vector and generate it at the bottom of the entry function:

// HLSL doesn't support atexit.
if (CGM.getLangOpts().HLSL)
return CGM.AddCXXDtorEntry(Dtor, Addr);

The flaw in this is that it will be called regardless of whether the construtor was encountered at all. This is inconsistent with how C++ operates and potentially damaging. Instead, we need to reuse the guard variable and bit offset that is used to ensure the constructor only gets called once:

// Pseudo code for the test:
// if (!(GuardVar & MyGuardBit)) {
// GuardVar |= MyGuardBit;
// ... initialize the object ...;
// }

To do this, we'll have to keep some map of the variable and offset to the associated destructor call. That could either be added to the CXXGLobalDtorsOrStermFinalizers vector or stored in a map within CGHLSLRuntime for use when generating the global destructor calls.

This is specific to the version of HLSL that adds destructors, which looks to be 202x.

@pow2clk pow2clk added HLSL HLSL Language Support clang:codegen labels Aug 26, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Aug 26, 2024

@llvm/issue-subscribers-clang-codegen

Author: Greg Roth (pow2clk)

The usual way to set up a destructor call for a local static is to use atexit, but HLSL doesn't have one of those. Instead, we currently add the destructor to the global destructor vector and generate it at the bottom of the entry function: https://github.com/llvm/llvm-project/blob/f05145cd31e92c73301e308a6e13c581af3076ce/clang/lib/CodeGen/MicrosoftCXXABI.cpp#L2385-L2387

The flaw in this is that it will be called regardless of whether the construtor was encountered at all. This is inconsistent with how C++ operates and potentially damaging. Instead, we need to reuse the guard variable and bit offset that is used to ensure the constructor only gets called once:

// Pseudo code for the test:
// if (!(GuardVar & MyGuardBit)) {
// GuardVar |= MyGuardBit;
// ... initialize the object ...;
// }

To do this, we'll have to keep some map of the variable and offset to the associated destructor call. That could either be added to the CXXGLobalDtorsOrStermFinalizers vector or stored in a map within CGHLSLRuntime for use when generating the global destructor calls.

This is specific to the version of HLSL that adds destructors, which looks to be 202x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen HLSL HLSL Language Support
Projects
Status: Ready
Development

No branches or pull requests

3 participants
@pow2clk @llvmbot and others