Linux: libdxcompiler replaces the host's signal handlers (SA_RESETHAND) on in-process DXIL compile and -P

Aperta
#8,999 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp

Direzione di ricerca

Start with CompilerInstance.cpp around createDefaultOutputFile and compare its RemoveFileOnSignal path with the library-exposed handling in FrontendActions.cpp. Trace the call into lib/Support/Unix/Signals.inc, then run the provided repro for DXIL and -spirv, including -P. Done means in-process DXIL compilation and preprocessing leave the host's signal dispositions unchanged while existing output behavior remains intact.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

This issue was written and submitted by an AI assistant (Claude) on behalf of the repository owner (@kaltinril), after the owner reviewed and agreed to its creation. The owner is accountable for it and will answer questions.

Description

On Linux, an in-process DXIL compile (or -P preprocess) through IDxcCompiler3::Compile installs LLVM's signal handler over 16 process-wide signals (SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2, SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT, SIGSYS, SIGXCPU, SIGXFSZ) with SA_NODEFER|SA_RESETHAND, replacing the host application's handlers. A -spirv compile of the same shader does not.

Path: CompilerInstance::createDefaultOutputFile passes RemoveFileOnSignal=true (CompilerInstance.cpp#L603-L609), reached from EmitBCAction and PrintPreprocessedAction, then llvm::sys::RemoveFileOnSignal -> RegisterHandlers() (Signals.inc#L67-L122). In dxcompiler the output goes to an in-memory stream, so there seems to be no on-disk file to protect. FrontendActions.cpp already avoids RemoveFileOnSignal for library-exposed paths (#L120-L125).

Impact: with SA_RESETHAND, a host that delivers one of these signals to several threads can be killed by the default action. Related: #5271 / #5272 made the registration thread-safe; this report is about the registration happening at all in library use.

Steps to Reproduce

From the root of the extracted linux_dxc_2026_09_28.x86_x64.tar.gz:

g++ -std=c++17 -I include repro.cpp -o repro -L lib -ldxcompiler -lpthread -ldl
LD_LIBRARY_PATH=lib ./repro          # DXIL
LD_LIBRARY_PATH=lib ./repro -spirv   # same shader, SPIR-V
#include <dlfcn.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <atomic>
#include <cstdio>
#include <cstring>
#include <thread>
#include <vector>
#include "dxc/dxcapi.h"

static std::atomic<int> g_handled{0};
static std::atomic<bool> g_stop{false};
static void hostHandler(int) { g_handled++; }

int main(int argc, char **argv) {
  struct sigaction sa;  // the host owns SIGUSR1 before DXC is called
  memset(&sa, 0, sizeof sa);
  sa.sa_handler = hostHandler;
  sigaction(SIGUSR1, &sa, nullptr);

  const int kThreads = 8;
  std::vector<std::thread> threads;
  std::vector<pthread_t> ids(kThreads);
  std::atomic<int> ready{0};
  for (int i = 0; i < kThreads; i++)
    threads.emplace_back([&, i] { ids[i] = pthread_self(); ready++; while (!g_stop) {} });
  while (ready != kThreads) usleep(1000);

  static const char src[] = "float4 main() : SV_Target { return 0; }";
  std::vector<LPCWSTR> args = {L"-T", L"ps_6_0", L"-E", L"main"};
  if (argc > 1 && !strcmp(argv[1], "-spirv")) args.push_back(L"-spirv");
  IDxcCompiler3 *compiler = nullptr;
  DxcCreateInstance(CLSID_DxcCompiler, __uuidof(IDxcCompiler3), (void **)&compiler);
  DxcBuffer buf = {src, sizeof(src) - 1, 65001};
  IDxcResult *result = nullptr;
  HRESULT hr = compiler->Compile(&buf, args.data(), (UINT32)args.size(), nullptr,
                                 __uuidof(IDxcResult), (void **)&result);
  HRESULT status = -1;
  if (result) result->GetStatus(&status);
  printf("Compile hr=0x%x status=0x%x\n", (unsigned)hr, (unsigned)status);

  struct sigaction now;
  sigaction(SIGUSR1, nullptr, &now);
  Dl_info info;
  const char *image = "?";
  if (dladdr((void *)now.sa_handler, &info) && info.dli_fname) image = info.dli_fname;
  printf("SIGUSR1 handler after Compile: %s, in %s%s\n",
         now.sa_handler == hostHandler ? "still the host's" : "REPLACED", image,
         (now.sa_flags & SA_RESETHAND) ? ", SA_RESETHAND" : "");
  fflush(stdout);

  for (int i = 0; i < kThreads; i++) pthread_kill(ids[i], SIGUSR1);
  usleep(200 * 1000);
  g_stop = true;
  for (auto &t : threads) t.join();
  printf("survived; host handler ran %d times\n", g_handled.load());
  return 0;
}

Actual Behavior

DXIL (killed in 50 of 50 runs):

Compile hr=0x0 status=0x0
SIGUSR1 handler after Compile: REPLACED, in lib/libdxcompiler.so, SA_RESETHAND
User defined signal 1      (exit 138)

-spirv (survives 50 of 50 runs):

Compile hr=0x0 status=0x0
SIGUSR1 handler after Compile: still the host's, in ./repro
survived; host handler ran 8 times

Expected: an in-process compile leaves the host's signal dispositions alone.

Environment

  • DXC version: libdxcompiler.so: 1.9(5512-01b62ad4)(1.9.2609.5) (official v1.9.2609 release). Also present in v1.7.2212.1.
  • Host Operating System: Ubuntu 24.04 (WSL2), glibc 2.39, g++ 13.3.0, x86_64
Lingua principale
C++
Stelle
3.7k
Fork
926
Merge medio
3g 22h
PR unite (30g)
45

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/DirectXShaderCompiler

Tutte le issue di microsoft/DirectXShaderCompiler

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.