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

Help Clang find bottom of stacks for safety, use desired stack size #873

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "test.hh"
#include "working_files.hh"

#include <clang/Basic/Stack.h>
#include <clang/Basic/Version.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/CrashRecoveryContext.h>
Expand Down Expand Up @@ -78,8 +79,12 @@ int main(int argc, char **argv) {

pipeline::init();
const char *env = getenv("CCLS_CRASH_RECOVERY");
if (!env || strcmp(env, "0") != 0)
if (!env || strcmp(env, "0") != 0) {
CrashRecoveryContext::Enable();
#if LLVM_VERSION_MAJOR >= 10 // rL369940
clang::noteBottomOfStack(); // per-thread, needed to avoid stack exhaustion
#endif
}

bool language_server = true;

Expand Down
20 changes: 18 additions & 2 deletions src/platform_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <malloc.h>
#endif

#include <clang/Basic/Stack.h>

#include <llvm/ADT/SmallString.h>
#include <llvm/Support/Path.h>

Expand Down Expand Up @@ -61,18 +63,32 @@ void traceMe() {
raise(traceme[0] == 's' ? SIGSTOP : SIGTSTP);
}

struct ThreadInfo {
void *(*fn)(void *);
void *arg;
};

void *threadWrapper(void *arg) {
ThreadInfo ti = *(ThreadInfo *)arg;
delete (ThreadInfo *)arg;
#if LLVM_VERSION_MAJOR >= 10 // rL369940
clang::noteBottomOfStack(); // per-thread, needed to avoid stack exhaustion
#endif
return ti.fn(ti.arg);
}

void spawnThread(void *(*fn)(void *), void *arg) {
pthread_t thd;
pthread_attr_t attr;
struct rlimit rlim;
size_t stack_size = 4 * 1024 * 1024;
size_t stack_size = clang::DesiredStackSize;
if (getrlimit(RLIMIT_STACK, &rlim) == 0 && rlim.rlim_cur != RLIM_INFINITY)
stack_size = rlim.rlim_cur;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize(&attr, stack_size);
pipeline::threadEnter();
pthread_create(&thd, &attr, fn, arg);
pthread_create(&thd, &attr, threadWrapper, new ThreadInfo{fn, arg});
pthread_attr_destroy(&attr);
}
} // namespace ccls
Expand Down