From 739c80c474446d904be05eb953c7afa15a04809b Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 20 Nov 2024 17:56:14 -0500 Subject: [PATCH] replace symbolizer with kj built-in --- build/kj_test.bzl | 5 +- justfile | 5 +- src/workerd/server/BUILD.bazel | 5 +- src/workerd/util/BUILD.bazel | 15 --- src/workerd/util/symbolizer.c++ | 174 -------------------------------- 5 files changed, 4 insertions(+), 200 deletions(-) delete mode 100644 src/workerd/util/symbolizer.c++ diff --git a/build/kj_test.bzl b/build/kj_test.bzl index d4cabcfc33a..5f4aa823ee1 100644 --- a/build/kj_test.bzl +++ b/build/kj_test.bzl @@ -10,10 +10,7 @@ def kj_test( srcs = [src], deps = [ "@capnp-cpp//src/kj:kj-test", - ] + select({ - "@platforms//os:windows": [], - "//conditions:default": ["@workerd//src/workerd/util:symbolizer"], - }) + deps, + ] + deps, linkopts = select({ "@//:use_dead_strip": ["-Wl,-dead_strip", "-Wl,-no_exported_symbols"], "//conditions:default": [""], diff --git a/justfile b/justfile index 649cea51023..8db8dd77ea4 100644 --- a/justfile +++ b/justfile @@ -7,7 +7,6 @@ default: @just --list pwd := `pwd` -clang_version := "18" prepare: cargo install gen-compile-commands @@ -25,14 +24,14 @@ build-asan *args="//...": just build {{args}} --config=asan --sandbox_debug test *args="//...": - bazel test {{args}} --test_env=LLVM_SYMBOLIZER=llvm-symbolizer-{{clang_version}} + bazel test {{args}} test-asan *args="//...": just test {{args}} --config=asan # e.g. just stream-test //src/cloudflare:cloudflare.capnp@eslint stream-test args: - bazel test {{args}} --test_output=streamed --test_env=LLVM_SYMBOLIZER=llvm-symbolizer-{{clang_version}} + bazel test {{args}} --test_output=streamed # e.g. just node-test zlib node-test test_name: diff --git a/src/workerd/server/BUILD.bazel b/src/workerd/server/BUILD.bazel index 498493f9934..61df87553d9 100644 --- a/src/workerd/server/BUILD.bazel +++ b/src/workerd/server/BUILD.bazel @@ -57,10 +57,7 @@ wd_cc_binary( "//src/workerd/util:autogate", "//src/workerd/util:perfetto", "@capnp-cpp//src/capnp:capnpc", - ] + select({ - "@platforms//os:windows": [], - "//conditions:default": ["@workerd//src/workerd/util:symbolizer"], - }), + ], ) wd_cc_library( diff --git a/src/workerd/util/BUILD.bazel b/src/workerd/util/BUILD.bazel index 4f0ecdb9aae..6e5b4bc5725 100644 --- a/src/workerd/util/BUILD.bazel +++ b/src/workerd/util/BUILD.bazel @@ -143,21 +143,6 @@ wd_cc_library( ], ) -wd_cc_library( - name = "symbolizer", - srcs = ["symbolizer.c++"], - target_compatible_with = select({ - "@platforms//os:windows": ["@platforms//:incompatible"], - "//conditions:default": [], - }), - visibility = ["//visibility:public"], - deps = [ - ":sentry", - "@capnp-cpp//src/kj", - ], - alwayslink = 1, -) - wd_cc_library( name = "own-util", hdrs = ["own-util.h"], diff --git a/src/workerd/util/symbolizer.c++ b/src/workerd/util/symbolizer.c++ deleted file mode 100644 index 9f1e7b8c8ec..00000000000 --- a/src/workerd/util/symbolizer.c++ +++ /dev/null @@ -1,174 +0,0 @@ -// Copyright (c) 2023 Cloudflare, Inc. -// Licensed under the Apache 2.0 license found in the LICENSE file or at: -// https://opensource.org/licenses/Apache-2.0 - -#include "dlfcn.h" - -#include - -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -// Stack trace symbolizer. To use link this source to the binary. -// Current implementation shells out to $LLVM_SYMBOLIZER if it is defined. - -namespace kj { - -namespace { - -// A simple subprocess wrapper with in/out pipes. -struct Subprocess { - // Execute command with a shell. - static kj::Maybe exec(const char* argv[]) noexcept { - // Since this is used during error handling we do not to try to free resources in - // case of errors. - - int in[2]{}; // process stdin pipe - int out[2]{}; // process stdout pipe - - if (pipe(in)) { - KJ_LOG(ERROR, "can't allocate in pipe", strerror(errno)); - return kj::none; - } - if (pipe(out)) { - KJ_LOG(ERROR, "can't allocate out pipe", strerror(errno)); - return kj::none; - } - - auto pid = fork(); - if (pid > 0) { - // parent - close(in[0]); - close(out[1]); - return Subprocess({.pid = pid, .in = in[1], .out = out[0]}); - } else { - // child - close(in[1]); - close(out[0]); - - // redirect stdin - close(0); - if (dup(in[0]) < 0) { - _exit(2 * errno); - } - - // redirect stdout - close(1); - if (dup(out[1]) < 0) { - _exit(3 * errno); - } - - KJ_SYSCALL_HANDLE_ERRORS(execvp(argv[0], const_cast(argv))) { - case ENOENT: - _exit(2); - default: - KJ_FAIL_SYSCALL("execvp", error); - } - _exit(1); - } - } - - int closeAndWait() { - close(in); - close(out); - int status; - waitpid(pid, &status, 0); - return status; - } - - int pid; - int in; - int out; -}; - -} // namespace - -String stringifyStackTrace(ArrayPtr trace) { - const char* llvmSymbolizer = getenv("LLVM_SYMBOLIZER"); - if (llvmSymbolizer == nullptr) { - LOG_WARNING_ONCE( - "Not symbolizing stack traces because $LLVM_SYMBOLIZER is not set. " - "To symbolize stack traces, set $LLVM_SYMBOLIZER to the location of the llvm-symbolizer " - "binary. When running tests under bazel, use `--test_env=LLVM_SYMBOLIZER=`."); - return nullptr; - } - - const char* argv[] = {llvmSymbolizer, "--pretty-print", "--relativenames", nullptr}; - - bool disableSigpipeOnce KJ_UNUSED = []() { - // Just in case for some reason SIGPIPE is not already disabled in this process, disable it - // now, otherwise the below will fail in the case that llvm-symbolizer is not found. Note - // that if the process creates a kj::UnixEventPort, then SIGPIPE will already be disabled. - signal(SIGPIPE, SIG_IGN); - return false; - }(); - - KJ_IF_SOME(subprocess, Subprocess::exec(argv)) { - // write addresses as "CODE " lines. - auto addrs = strArray(KJ_MAP(addr, trace) { - Dl_info info; - if (dladdr(addr, &info)) { - uintptr_t offset = - reinterpret_cast(addr) - reinterpret_cast(info.dli_fbase); - return kj::str("CODE ", info.dli_fname, " 0x", reinterpret_cast(offset)); - } else { - return kj::str("CODE 0x", reinterpret_cast(addr)); - } - }, "\n"); - if (write(subprocess.in, addrs.cStr(), addrs.size()) != addrs.size()) { - // Ignore EPIPE, which means the process exited early. We'll deal with it below, presumably. - if (errno != EPIPE) { - KJ_LOG(ERROR, "write error", strerror(errno)); - return nullptr; - } - } - close(subprocess.in); - - // read result - auto out = fdopen(subprocess.out, "r"); - if (!out) { - KJ_LOG(ERROR, "fdopen error", strerror(errno)); - return nullptr; - } - - kj::String lines[256]; - size_t i = 0; - for (char line[512]{}; fgets(line, sizeof(line), out) != nullptr;) { - if (i < kj::size(lines)) { - lines[i++] = kj::str(line); - } - } - int status = subprocess.closeAndWait(); - if (WIFEXITED(status)) { - if (WEXITSTATUS(status) != 0) { - if (WEXITSTATUS(status) == 2) { - LOG_WARNING_ONCE(kj::str(llvmSymbolizer, - " was not found. " - "To symbolize stack traces, install it in your $PATH or set $LLVM_SYMBOLIZER to the " - "location of the binary. When running tests under bazel, use " - "`--test_env=LLVM_SYMBOLIZER=`.")); - } else { - KJ_LOG(ERROR, "bad exit code", WEXITSTATUS(status)); - } - return nullptr; - } - } else { - KJ_LOG(ERROR, "bad exit status", status); - return nullptr; - } - return kj::str("\n", kj::strArray(kj::arrayPtr(lines, i), "")); - } else { - return kj::str("\nerror starting llvm-symbolizer"); - } -} - -} // namespace kj