Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/fbsd_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -57,6 +58,9 @@
#define WEAK_REDEF3(type, fname, arg1, arg2, arg3) \
MESH_EXPORT type fname(arg1, arg2, arg3) \
__THROW WEAK(mesh_##fname)
#define WEAK_REDEF6(type, fname, arg1, arg2, arg3, arg4, arg5, arg6) \
MESH_EXPORT type fname(arg1, arg2, arg3, arg4, arg5, arg6) \
__THROW WEAK(mesh_##fname)

extern "C" {
WEAK_REDEF1(void *, malloc, size_t);
Expand All @@ -69,6 +73,10 @@ WEAK_REDEF2(void *, memalign, size_t, size_t);
WEAK_REDEF3(int, posix_memalign, void **, size_t, size_t);
WEAK_REDEF2(void *, aligned_alloc, size_t, size_t);
WEAK_REDEF1(size_t, malloc_usable_size, const void *);
WEAK_REDEF6(int, posix_spawn, pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
char *const[], char *const[]);
WEAK_REDEF6(int, posix_spawnp, pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
char *const[], char *const[]);
}

#include "wrapper.cc"
8 changes: 8 additions & 0 deletions src/gnu_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -61,6 +62,9 @@
#define WEAK_REDEF3(type, fname, arg1, arg2, arg3) \
MESH_EXPORT type fname(arg1, arg2, arg3) \
__THROW WEAK(mesh_##fname)
#define WEAK_REDEF6(type, fname, arg1, arg2, arg3, arg4, arg5, arg6) \
MESH_EXPORT type fname(arg1, arg2, arg3, arg4, arg5, arg6) \
__THROW WEAK(mesh_##fname)

extern "C" {
WEAK_REDEF1(void *, malloc, size_t);
Expand All @@ -72,6 +76,10 @@ WEAK_REDEF2(void *, memalign, size_t, size_t);
WEAK_REDEF3(int, posix_memalign, void **, size_t, size_t);
WEAK_REDEF2(void *, aligned_alloc, size_t, size_t);
WEAK_REDEF1(size_t, malloc_usable_size, void *);
WEAK_REDEF6(int, posix_spawn, pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
char *const[], char *const[]);
WEAK_REDEF6(int, posix_spawnp, pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
char *const[], char *const[]);
}

#include "wrapper.cc"
47 changes: 47 additions & 0 deletions src/libmesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,54 @@ ssize_t MESH_EXPORT recvmsg(int sockfd, struct msghdr *msg, int flags) {
return mesh::dispatchByPageSize([=](auto &rt) { return rt.recvmsg(sockfd, msg, flags); });
}
#endif
} // extern "C"

namespace {
// RAII guard for the three locks needed during spawn operations.
// Acquires locks in the same order as prepareForFork to prevent races.
template <size_t PageSize>
class SpawnLockGuard {
public:
SpawnLockGuard() {
mesh::runtime<PageSize>().heap().lock();
mesh::runtime<PageSize>().lock();
mesh::internal::Heap().lock();
}

~SpawnLockGuard() {
mesh::internal::Heap().unlock();
mesh::runtime<PageSize>().unlock();
mesh::runtime<PageSize>().heap().unlock();
}

SpawnLockGuard(const SpawnLockGuard &) = delete;
SpawnLockGuard &operator=(const SpawnLockGuard &) = delete;
};
} // namespace

extern "C" {
int MESH_EXPORT mesh_posix_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *file_actions,
const posix_spawnattr_t *attrp, char *const argv[], char *const envp[]) {
if (likely(getPageSize() == kPageSize4K)) {
SpawnLockGuard<kPageSize4K> guard;
return mesh::real::posix_spawn(pid, path, file_actions, attrp, argv, envp);
} else {
SpawnLockGuard<kPageSize16K> guard;
return mesh::real::posix_spawn(pid, path, file_actions, attrp, argv, envp);
}
}

int MESH_EXPORT mesh_posix_spawnp(pid_t *pid, const char *file, const posix_spawn_file_actions_t *file_actions,
const posix_spawnattr_t *attrp, char *const argv[], char *const envp[]) {
if (likely(getPageSize() == kPageSize4K)) {
SpawnLockGuard<kPageSize4K> guard;
return mesh::real::posix_spawnp(pid, file, file_actions, attrp, argv, envp);
} else {
SpawnLockGuard<kPageSize16K> guard;
return mesh::real::posix_spawnp(pid, file, file_actions, attrp, argv, envp);
}
}
} // extern "C"

#if defined(__linux__)
#include "gnu_wrapper.cc"
Expand Down
9 changes: 0 additions & 9 deletions src/meshable_arena.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ void MeshableArena<PageSize>::prepareForFork() {
runtime<PageSize>().lock();
internal::Heap().lock();

int r = mprotect(_arenaBegin, kArenaSize, PROT_READ);
hard_assert(r == 0);

int err = pipe(_forkPipe);
if (err == -1) {
abort();
Expand Down Expand Up @@ -49,9 +46,6 @@ void MeshableArena<PageSize>::afterForkParent() {
_forkPipe[0] = -1;
_forkPipe[1] = -1;

int r = mprotect(_arenaBegin, kArenaSize, PROT_READ | PROT_WRITE);
hard_assert(r == 0);

runtime<PageSize>().unlock();
runtime<PageSize>().heap().unlock();
}
Expand Down Expand Up @@ -96,9 +90,6 @@ void MeshableArena<PageSize>::afterForkChild() {
d_assert(result == CPUInfo::PageSize);
}

int r = mprotect(_arenaBegin, kArenaSize, PROT_READ | PROT_WRITE);
hard_assert(r == 0);

void *ptr = mmap(_arenaBegin, kArenaSize, HL_MMAP_PROTECTION_MASK, kMapShared | MAP_FIXED, newFd, 0);
hard_assert_msg(ptr != MAP_FAILED, "map failed: %d", errno);

Expand Down
2 changes: 0 additions & 2 deletions src/meshable_arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,6 @@ MeshableArena<PageSize>::MeshableArena() : SuperHeap(), _fastPrng(internal::seed
madvise(_arenaBegin, kArenaSize, MADV_DONTDUMP);
}

debug("MeshableArena(%p): fd:%4d\t%p-%p\n", this, _fd, _arenaBegin, arenaEnd());

atexit(staticAtExit);
pthread_atfork(staticPrepareForFork, staticAfterForkParent, staticAfterForkChild);
}
Expand Down
6 changes: 6 additions & 0 deletions src/real.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ DEFINE_REAL(pthread_exit);
DEFINE_REAL(sigaction);
DEFINE_REAL(sigprocmask);

DEFINE_REAL(posix_spawn);
DEFINE_REAL(posix_spawnp);

void init() {
static mutex initLock;
static bool initialized;
Expand All @@ -51,6 +54,9 @@ void init() {

INIT_REAL(sigaction, RTLD_NEXT);
INIT_REAL(sigprocmask, RTLD_NEXT);

INIT_REAL(posix_spawn, RTLD_NEXT);
INIT_REAL(posix_spawnp, RTLD_NEXT);
}
} // namespace real
} // namespace mesh
4 changes: 4 additions & 0 deletions src/real.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <pthread.h>
#include <signal.h>
#include <spawn.h>

#ifdef __linux__
#include <sys/epoll.h>
Expand Down Expand Up @@ -34,6 +35,9 @@ DECLARE_REAL(pthread_exit);

DECLARE_REAL(sigaction);
DECLARE_REAL(sigprocmask);

DECLARE_REAL(posix_spawn);
DECLARE_REAL(posix_spawnp);
} // namespace real
} // namespace mesh

Expand Down
Loading