Skip to content

Add allocator check to pcre2_jit_compile #433

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

Merged
merged 1 commit into from
Jul 23, 2024
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
1 change: 1 addition & 0 deletions src/pcre2.h.generic
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ D is inspected during pcre2_dfa_match() execution
#define PCRE2_JIT_PARTIAL_SOFT 0x00000002u
#define PCRE2_JIT_PARTIAL_HARD 0x00000004u
#define PCRE2_JIT_INVALID_UTF 0x00000100u
#define PCRE2_JIT_TEST_ALLOC 0x00000200u

/* These are for pcre2_match(), pcre2_dfa_match(), pcre2_jit_match(), and
pcre2_substitute(). Some are allowed only for one of the functions, and in
Expand Down
1 change: 1 addition & 0 deletions src/pcre2.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ D is inspected during pcre2_dfa_match() execution
#define PCRE2_JIT_PARTIAL_SOFT 0x00000002u
#define PCRE2_JIT_PARTIAL_HARD 0x00000004u
#define PCRE2_JIT_INVALID_UTF 0x00000100u
#define PCRE2_JIT_TEST_ALLOC 0x00000200u

/* These are for pcre2_match(), pcre2_dfa_match(), pcre2_jit_match(), and
pcre2_substitute(). Some are allowed only for one of the functions, and in
Expand Down
37 changes: 23 additions & 14 deletions src/pcre2_jit_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -14848,8 +14848,31 @@ pcre2_jit_compile(pcre2_code *code, uint32_t options)
{
pcre2_real_code *re = (pcre2_real_code *)code;
#ifdef SUPPORT_JIT
void *exec_memory;
executable_functions *functions;
static int executable_allocator_is_working = -1;

if (executable_allocator_is_working == -1)
{
/* Checks whether the executable allocator is working. This check
might run multiple times in multi-threaded environments, but the
result should not be affected by it. */
exec_memory = SLJIT_MALLOC_EXEC(32, NULL);
if (exec_memory != NULL)
{
SLJIT_FREE_EXEC(((sljit_u8*)(exec_memory)) + SLJIT_EXEC_OFFSET(exec_memory), NULL);
executable_allocator_is_working = 1;
}
else executable_allocator_is_working = 0;
}

if (options & PCRE2_JIT_TEST_ALLOC)
{
if (options != PCRE2_JIT_TEST_ALLOC)
return PCRE2_ERROR_JIT_BADOPTION;

return executable_allocator_is_working ? 0 : PCRE2_ERROR_NOMEMORY;
}
#endif

if (code == NULL)
Expand Down Expand Up @@ -14912,20 +14935,6 @@ return PCRE2_ERROR_JIT_BADOPTION;

if ((re->flags & PCRE2_NOJIT) != 0) return 0;

if (executable_allocator_is_working == -1)
{
/* Checks whether the executable allocator is working. This check
might run multiple times in multi-threaded environments, but the
result should not be affected by it. */
void *ptr = SLJIT_MALLOC_EXEC(32, NULL);
if (ptr != NULL)
{
SLJIT_FREE_EXEC(((sljit_u8*)(ptr)) + SLJIT_EXEC_OFFSET(ptr), NULL);
executable_allocator_is_working = 1;
}
else executable_allocator_is_working = 0;
}

if (!executable_allocator_is_working)
return PCRE2_ERROR_NOMEMORY;

Expand Down
Loading