Skip to content

Commit 68b0258

Browse files
zherczegZoltan Herczeg
and
Zoltan Herczeg
authored
Add allocator check to pcre2_jit_compile (#433)
Co-authored-by: Zoltan Herczeg <[email protected]>
1 parent 6d82f0c commit 68b0258

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/pcre2.h.generic

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ D is inspected during pcre2_dfa_match() execution
166166
#define PCRE2_JIT_PARTIAL_SOFT 0x00000002u
167167
#define PCRE2_JIT_PARTIAL_HARD 0x00000004u
168168
#define PCRE2_JIT_INVALID_UTF 0x00000100u
169+
#define PCRE2_JIT_TEST_ALLOC 0x00000200u
169170

170171
/* These are for pcre2_match(), pcre2_dfa_match(), pcre2_jit_match(), and
171172
pcre2_substitute(). Some are allowed only for one of the functions, and in

src/pcre2.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ D is inspected during pcre2_dfa_match() execution
166166
#define PCRE2_JIT_PARTIAL_SOFT 0x00000002u
167167
#define PCRE2_JIT_PARTIAL_HARD 0x00000004u
168168
#define PCRE2_JIT_INVALID_UTF 0x00000100u
169+
#define PCRE2_JIT_TEST_ALLOC 0x00000200u
169170

170171
/* These are for pcre2_match(), pcre2_dfa_match(), pcre2_jit_match(), and
171172
pcre2_substitute(). Some are allowed only for one of the functions, and in

src/pcre2_jit_compile.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14848,8 +14848,31 @@ pcre2_jit_compile(pcre2_code *code, uint32_t options)
1484814848
{
1484914849
pcre2_real_code *re = (pcre2_real_code *)code;
1485014850
#ifdef SUPPORT_JIT
14851+
void *exec_memory;
1485114852
executable_functions *functions;
1485214853
static int executable_allocator_is_working = -1;
14854+
14855+
if (executable_allocator_is_working == -1)
14856+
{
14857+
/* Checks whether the executable allocator is working. This check
14858+
might run multiple times in multi-threaded environments, but the
14859+
result should not be affected by it. */
14860+
exec_memory = SLJIT_MALLOC_EXEC(32, NULL);
14861+
if (exec_memory != NULL)
14862+
{
14863+
SLJIT_FREE_EXEC(((sljit_u8*)(exec_memory)) + SLJIT_EXEC_OFFSET(exec_memory), NULL);
14864+
executable_allocator_is_working = 1;
14865+
}
14866+
else executable_allocator_is_working = 0;
14867+
}
14868+
14869+
if (options & PCRE2_JIT_TEST_ALLOC)
14870+
{
14871+
if (options != PCRE2_JIT_TEST_ALLOC)
14872+
return PCRE2_ERROR_JIT_BADOPTION;
14873+
14874+
return executable_allocator_is_working ? 0 : PCRE2_ERROR_NOMEMORY;
14875+
}
1485314876
#endif
1485414877

1485514878
if (code == NULL)
@@ -14912,20 +14935,6 @@ return PCRE2_ERROR_JIT_BADOPTION;
1491214935

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

14915-
if (executable_allocator_is_working == -1)
14916-
{
14917-
/* Checks whether the executable allocator is working. This check
14918-
might run multiple times in multi-threaded environments, but the
14919-
result should not be affected by it. */
14920-
void *ptr = SLJIT_MALLOC_EXEC(32, NULL);
14921-
if (ptr != NULL)
14922-
{
14923-
SLJIT_FREE_EXEC(((sljit_u8*)(ptr)) + SLJIT_EXEC_OFFSET(ptr), NULL);
14924-
executable_allocator_is_working = 1;
14925-
}
14926-
else executable_allocator_is_working = 0;
14927-
}
14928-
1492914938
if (!executable_allocator_is_working)
1493014939
return PCRE2_ERROR_NOMEMORY;
1493114940

0 commit comments

Comments
 (0)