Skip to content

Commit

Permalink
Manually set thread stack sizes.
Browse files Browse the repository at this point in the history
8KB was empirically determined to work on musl. We can't go lower (musl
supports up to 2KB) because posix_spawn allocates a temporary stack for
vfork (via clone(2)) on its own stack.

This improves our resource consumption and increases the likeliness for
pthread_create to work even when there isn't a lot of free memory.

Using pthread_setattr_default_np isn't possible, because on musl it
doesn't allow us to go below the default thread stack size. This way is
also more portable.

We don't remove the guard pages entirely, because a shell prompt might
be active in attacker controlled directories, and it's a reasonable
protection against any stack overflow exploits that might appear.
  • Loading branch information
ericonr committed Jul 17, 2021
1 parent 9dc6a30 commit bd30334
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
25 changes: 23 additions & 2 deletions ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#define PROMPT " ➜ "
#define JOBS " ✦"

pthread_attr_t *thread_a = 0;

int main(int argc, char **argv)
{
setlocale(LC_ALL, "");
Expand Down Expand Up @@ -54,18 +56,37 @@ int main(int argc, char **argv)
}
}

pthread_attr_t attr;

do {
if (pthread_attr_init(&attr)) break;
/* stack buffer in popen/posix_spawn can get big, so we can't go much below 8KB;
* we subtract 1024 so libc TLS can still fit into a 8KB region instead of
* requiring a 12KB region. This fiddling has no effect if PAGE_SIZE>4KB */
if (pthread_attr_setstacksize(&attr, (1 << 13) - 1024)) {
pthread_attr_destroy(&attr);
break;
}
/* guarantee at least one memory page as guard */
if (pthread_attr_setguardsize(&attr, 1)) {
pthread_attr_destroy(&attr);
break;
}
thread_a = &attr;
} while(0);

/* start threads for long(er) running steps */
struct threaded_task root_lang_task = { .task = task_launch_root_lang };
int git_launched = 1;
pthread_t git_handle;
if (pthread_create(&git_handle, NULL, git_thread, &root_lang_task)) {
if (pthread_create(&git_handle, thread_a, git_thread, &root_lang_task)) {
e(INFO, "couldn't create git thread", errno);
git_launched = 0;
}

int pwd_lang_launched = 1;
pthread_t pwd_lang_handle;
if (pthread_create(&pwd_lang_handle, NULL, lang_thread, NULL)) {
if (pthread_create(&pwd_lang_handle, thread_a, lang_thread, NULL)) {
e(INFO, "couldn't create lang thread", errno);
pwd_lang_launched = 0;
}
Expand Down
3 changes: 3 additions & 0 deletions ep.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct threaded_task {
enum task_identity task;
};

/* from ep.c */
extern pthread_attr_t *thread_a;

/* from out.c */
extern FILE *out, *outerr;
enum log_level_value { DEBUG, INFO, WARN, ERROR };
Expand Down
6 changes: 3 additions & 3 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ void *git_thread(void *arg)
* if we add more stuff to do in repos, launch more threads */

pthread_t status_handle, root_handle;
if (pthread_create(&status_handle, NULL, get_git_status, git_info))
if (pthread_create(&status_handle, thread_a, get_git_status, git_info))
goto status_create_error;
if (pthread_create(&root_handle, NULL, get_git_root, git_info))
if (pthread_create(&root_handle, thread_a, get_git_root, git_info))
goto root_create_error;

pthread_join(root_handle, NULL);
if (root_lang_task && git_info->git_root) {
root_lang_task->launched = !pthread_create(&root_lang_task->handle, NULL, lang_thread, git_info->git_root);
root_lang_task->launched = !pthread_create(&root_lang_task->handle, thread_a, lang_thread, git_info->git_root);
}
root_create_error:
pthread_join(status_handle, NULL);
Expand Down

0 comments on commit bd30334

Please sign in to comment.