Skip to content

Commit

Permalink
fixup! Move allocators to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
carenas committed Dec 30, 2022
1 parent 3e99f2a commit 9a758ae
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 160 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ SLJIT_LIR_FILES = $(SRCDIR)/sljitLir.c $(SRCDIR)/sljitUtils.c \
$(SRCDIR)/allocator_src/sljitExecAllocatorCore.c $(SRCDIR)/allocator_src/sljitExecAllocatorApple.c \
$(SRCDIR)/allocator_src/sljitExecAllocatorPosix.c $(SRCDIR)/allocator_src/sljitExecAllocatorWindows.c \
$(SRCDIR)/allocator_src/sljitProtExecAllocatorNetBSD.c $(SRCDIR)/allocator_src/sljitProtExecAllocatorPosix.c \
$(SRCDIR)/allocator_src/sljitWXExecAllocatorLinux.c $(SRCDIR)/allocator_src/sljitWXExecAllocatorNetBSD.c \
$(SRCDIR)/allocator_src/sljitWXExecAllocatorWindows.c \
$(SRCDIR)/allocator_src/sljitWXExecAllocatorPosix.c $(SRCDIR)/allocator_src/sljitWXExecAllocatorWindows.c \
$(SRCDIR)/sljitNativeARM_32.c $(SRCDIR)/sljitNativeARM_T2_32.c $(SRCDIR)/sljitNativeARM_64.c \
$(SRCDIR)/sljitNativeMIPS_common.c $(SRCDIR)/sljitNativeMIPS_32.c $(SRCDIR)/sljitNativeMIPS_64.c \
$(SRCDIR)/sljitNativePPC_common.c $(SRCDIR)/sljitNativePPC_32.c $(SRCDIR)/sljitNativePPC_64.c \
Expand Down
3 changes: 3 additions & 0 deletions sljit_src/allocator_src/sljitExecAllocatorApple.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <sys/types.h>
#include <sys/mman.h>
/*
On macOS systems, returns MAP_JIT if it is defined _and_ we're running on a
version where it's OK to have more than one JIT block or where MAP_JIT is
Expand All @@ -37,6 +39,7 @@
#include <stdlib.h>

#define SLJIT_MAP_JIT (get_map_jit_flag())
#define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec)

static SLJIT_INLINE int get_map_jit_flag()
{
Expand Down
10 changes: 6 additions & 4 deletions sljit_src/allocator_src/sljitExecAllocatorCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@
SLJIT_UPDATE_WX_FLAGS - (optional) update WX flags
*/

#include <sys/types.h>
#include <sys/mman.h>

#ifdef SLJIT_HAS_CHUNK_HEADER
#define CHUNK_HEADER_SIZE ((sizeof(struct sljit_chunk_header) + 7u) & ~(sljit_uw)7)
#define CHUNK_HEADER_SIZE (sizeof(struct sljit_chunk_header))
#else /* !SLJIT_HAS_CHUNK_HEADER */
#define CHUNK_HEADER_SIZE 0
#endif /* SLJIT_HAS_CHUNK_HEADER */
Expand Down Expand Up @@ -161,11 +164,11 @@ SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
sljit_sw executable_offset;
#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */

SLJIT_ALLOCATOR_LOCK();
if (size < (64 - sizeof(struct block_header)))
size = (64 - sizeof(struct block_header));
size = ALIGN_SIZE(size);

SLJIT_ALLOCATOR_LOCK();
free_block = free_blocks;
while (free_block) {
if (free_block->size >= size) {
Expand Down Expand Up @@ -206,7 +209,6 @@ SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
executable_offset = (sljit_sw)((sljit_u8*)chunk_header->executable - (sljit_u8*)chunk_header);
#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */

chunk_size -= CHUNK_EXTRA_SIZE;
total_size += chunk_size;

header = (struct block_header*)(((sljit_u8*)chunk_header) + CHUNK_HEADER_SIZE);
Expand Down Expand Up @@ -236,12 +238,12 @@ SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
header->size = chunk_size;
next_header = AS_BLOCK_HEADER(header, chunk_size);
}
SLJIT_ALLOCATOR_UNLOCK();
next_header->size = 1;
next_header->prev_size = chunk_size;
#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
next_header->executable_offset = executable_offset;
#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
SLJIT_ALLOCATOR_UNLOCK();
return MEM_START(header);
}

Expand Down
10 changes: 5 additions & 5 deletions sljit_src/allocator_src/sljitProtExecAllocatorPosix.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ static SLJIT_INLINE int create_tempfile(void)
dir = secure_getenv("TMPDIR");

if (dir) {
tmp_name_len = strlen(dir);
if (tmp_name_len > 0 && tmp_name_len < sizeof(tmp_name)) {
if ((stat(dir, &st) == 0) && S_ISDIR(st.st_mode))
size_t len = strlen(dir);
if (len > 0 && len < sizeof(tmp_name)) {
if ((stat(dir, &st) == 0) && S_ISDIR(st.st_mode)) {
strcpy(tmp_name, dir);
tmp_name_len = len;
}
}
}

Expand All @@ -97,7 +99,6 @@ static SLJIT_INLINE int create_tempfile(void)
if (tmp_name[tmp_name_len - 1] == '/')
tmp_name[--tmp_name_len] = '\0';

#ifdef __linux__
/*
* the previous trimming might had left an empty string if TMPDIR="/"
* so work around the problem below
Expand All @@ -106,7 +107,6 @@ static SLJIT_INLINE int create_tempfile(void)
O_TMPFILE | O_EXCL | O_RDWR | O_NOATIME | O_CLOEXEC, 0);
if (fd != -1)
return fd;
#endif

if (tmp_name_len + 7 >= sizeof(tmp_name))
return -1;
Expand Down
123 changes: 0 additions & 123 deletions sljit_src/allocator_src/sljitWXExecAllocatorNetBSD.c

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,45 +50,41 @@
not possible.
*/

#include <sys/types.h>
#include <sys/mman.h>

#define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec) \
sljit_update_wx_flags((from), (to), (enable_exec))

#if !(defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED)
#include <pthread.h>
#define SLJIT_SE_LOCK() pthread_mutex_lock(&se_lock)
#define SLJIT_SE_UNLOCK() pthread_mutex_unlock(&se_lock)
#else
#define SLJIT_SE_LOCK()
#define SLJIT_SE_UNLOCK()
#endif /* !SLJIT_SINGLE_THREADED */

#define check_se_protected(ptr, size) generic_se_protected(ptr, size)
#define SLJIT_WX_IS_BLOCK(ptr, size) generic_check_is_wx_block(ptr, size)

static SLJIT_INLINE int generic_se_protected(void *ptr, sljit_uw size)
static SLJIT_INLINE int generic_check_is_wx_block(void *ptr, sljit_uw size)
{
if (SLJIT_LIKELY(!mprotect(ptr, size, PROT_EXEC)))
return mprotect(ptr, size, PROT_READ | PROT_WRITE);
return !!mprotect(ptr, size, PROT_READ | PROT_WRITE);

return -1;
return 1;
}

#ifndef SLJIT_SE_LOCK
#define SLJIT_SE_LOCK()
#endif
#ifndef SLJIT_SE_UNLOCK
#define SLJIT_SE_UNLOCK()
#endif
#ifndef SLJIT_PROT_WX
#define SLJIT_PROT_WX 0
#endif

SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
{
#if !(defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED)
static pthread_mutex_t se_lock = PTHREAD_MUTEX_INITIALIZER;
#endif
static int se_protected = !SLJIT_PROT_WX;
int prot = PROT_READ | PROT_WRITE | SLJIT_PROT_WX;
static int wx_block = -1;
int prot = PROT_READ | PROT_WRITE;
sljit_uw* ptr;

if (SLJIT_UNLIKELY(se_protected < 0))
if (SLJIT_UNLIKELY(wx_block > 0))
return NULL;

#ifdef PROT_MAX
Expand All @@ -101,11 +97,11 @@ SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
if (ptr == MAP_FAILED)
return NULL;

if (SLJIT_UNLIKELY(se_protected > 0)) {
if (SLJIT_UNLIKELY(wx_block < 0)) {
SLJIT_SE_LOCK();
se_protected = check_se_protected(ptr, size);
wx_block = SLJIT_WX_IS_BLOCK(ptr, size);
SLJIT_SE_UNLOCK();
if (SLJIT_UNLIKELY(se_protected < 0)) {
if (SLJIT_UNLIKELY(wx_block)) {
munmap((void *)ptr, size);
return NULL;
}
Expand All @@ -115,7 +111,6 @@ SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
return ptr;
}

#undef SLJIT_PROT_WX
#undef SLJIT_SE_UNLOCK
#undef SLJIT_SE_LOCK

Expand All @@ -125,7 +120,7 @@ SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr)
munmap((void*)start_ptr, *start_ptr);
}

static void sljit_update_wx_flags(void *from, void *to, sljit_s32 enable_exec)
static void sljit_update_wx_flags(void *from, void *to, int enable_exec)
{
sljit_uw page_mask = (sljit_uw)get_page_alignment();
sljit_uw start = (sljit_uw)from;
Expand Down
6 changes: 2 additions & 4 deletions sljit_src/sljitLir.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,15 @@

#if defined(_WIN32)
#include "allocator_src/sljitWXExecAllocatorWindows.c"
#elif defined(__NetBSD__)
#include "allocator_src/sljitWXExecAllocatorNetBSD.c"
#else
#include "allocator_src/sljitWXExecAllocatorLinux.c"
#include "allocator_src/sljitWXExecAllocatorPosix.c"
#endif

#else

#if defined(_WIN32)
#include "allocator_src/sljitExecAllocatorWindows.c"
#elif defined(__APPLE__) && defined(MAP_JIT)
#elif defined(__APPLE__)
#include "allocator_src/sljitExecAllocatorApple.c"
#else
#include "allocator_src/sljitExecAllocatorPosix.c"
Expand Down

0 comments on commit 9a758ae

Please sign in to comment.