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

Abierto
#8,999 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
C++
Estrellas
3.7k
Forks
926
Merge medio
3 d 22 h
PR fusionados (30 d)
45

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de microsoft/DirectXShaderCompiler

Todos los issues de microsoft/DirectXShaderCompiler

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.