Skip to content

Commit

Permalink
Put a cap on the code object size before it is compiled. Default 10,000
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybaloney committed Feb 15, 2021
1 parent 8d0a5ed commit 1cfad51
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 36 deletions.
15 changes: 9 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_definitions(-DDEFAULT_RECURSION_LIMIT=1000)
add_definitions(-DDEFAULT_CODEOBJECT_SIZE_LIMIT=10000)

option(BUILD_TESTS "Build the unit tests" OFF)

include(CMakeOptimizations.txt)
Expand Down Expand Up @@ -242,9 +245,9 @@ if (SKBUILD)
python_extension_module(_pyjion)
endif (SKBUILD)

install(TARGETS _pyjion LIBRARY DESTINATION src/pyjion)
#set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})
#install(TARGETS _pyjion
# CONFIGURATIONS Debug
# LIBRARY DESTINATION src/pyjion
# RUNTIME DESTINATION Debug/bin)
#install(TARGETS _pyjion LIBRARY DESTINATION src/pyjion)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})
install(TARGETS _pyjion
CONFIGURATIONS Debug
LIBRARY DESTINATION src/pyjion
RUNTIME DESTINATION Debug/bin)
24 changes: 0 additions & 24 deletions Tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,6 @@ def test_add_inplace(self):
self.assertEqual(sys.getrefcount(c), before_ref_c - 1 )
self.assertEqual(c, "...Hello world!")

def test_many_fstring_expressions(self):
# Create a string with many expressions in it. Note that
# because we have a space in here as a literal, we're actually
# going to use twice as many ast nodes: one for each literal
# plus one for each expression.
def build_fstr(n, extra=''):
return "f'" + ('{x} ' * n) + extra + "'"

x = 'X'
width = 1

# Test around 256.
for i in range(250, 260):
self.assertEqual(eval(build_fstr(i)), (x+' ')*i)

# Test concatenating 2 largs fstrings.
self.assertEqual(eval(build_fstr(255)*256), (x+' ')*(255*256))

s = build_fstr(253, '{x:{width}} ')
self.assertEqual(eval(s), (x+' ')*254)

# Test lots of expressions and constants, concatenated.
s = "f'{1}' 'x' 'y'" * 1024
self.assertEqual(eval(s), '1xy' * 1024)

if __name__ == "__main__":
unittest.main()
10 changes: 8 additions & 2 deletions src/pyjion/absint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ bool AbstractInterpreter::preprocess() {
// all parameters are initially definitely assigned
m_assignmentState[i] = true;
}
if (mSize >= g_pyjionSettings.codeObjectSizeLimit){
#ifdef DEBUG
printf("Skipping function because it is too big.");
#endif
return false;
}

int oparg;
vector<bool> ehKind;
Expand Down Expand Up @@ -1618,7 +1624,7 @@ JittedCode* AbstractInterpreter::compileWorker() {
auto byte = GET_OPCODE(curByte);

// Get an additional oparg, see dis help for information on what each means
auto oparg = GET_OPARG(curByte);
size_t oparg = GET_OPARG(curByte);

processOpCode:
markOffsetLabel(curByte);
Expand Down Expand Up @@ -2224,7 +2230,7 @@ JittedCode* AbstractInterpreter::compileWorker() {
// Array
m_comp->emit_load_local(stackArray);
// Count
m_comp->emit_int(oparg);
m_comp->emit_long_long(oparg);

m_comp->emit_unicode_joinarray();

Expand Down
2 changes: 1 addition & 1 deletion src/pyjion/intrins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2552,7 +2552,7 @@ int PyJit_PeriodicWork() {
return 0;
}

PyObject* PyJit_UnicodeJoinArray(PyObject** items, int count) {
PyObject* PyJit_UnicodeJoinArray(PyObject** items, ssize_t count) {
auto empty = PyUnicode_New(0, 0);
auto res = _PyUnicode_JoinArray(empty, items, count);
for (auto i = 0; i < count; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/pyjion/intrins.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void PyJit_DecRef(PyObject* value);

int PyJit_PeriodicWork();

PyObject* PyJit_UnicodeJoinArray(PyObject** items, int count);
PyObject* PyJit_UnicodeJoinArray(PyObject** items, ssize_t count);
PyObject* PyJit_FormatObject(PyObject* item, PyObject*fmtSpec);
PyObject* PyJit_FormatValue(PyObject* item);

Expand Down
2 changes: 1 addition & 1 deletion src/pyjion/pycomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,7 @@ GLOBAL_METHOD(METHOD_PYERR_SETSTRING, PyErr_SetString, CORINFO_TYPE_VOID, Parame

GLOBAL_METHOD(METHOD_PERIODIC_WORK, PyJit_PeriodicWork, CORINFO_TYPE_INT)

GLOBAL_METHOD(METHOD_PYUNICODE_JOINARRAY, &PyJit_UnicodeJoinArray, CORINFO_TYPE_NATIVEINT, Parameter(CORINFO_TYPE_NATIVEINT), Parameter(CORINFO_TYPE_INT));
GLOBAL_METHOD(METHOD_PYUNICODE_JOINARRAY, &PyJit_UnicodeJoinArray, CORINFO_TYPE_NATIVEINT, Parameter(CORINFO_TYPE_NATIVEINT), Parameter(CORINFO_TYPE_NATIVEINT));
GLOBAL_METHOD(METHOD_FORMAT_VALUE, &PyJit_FormatValue, CORINFO_TYPE_NATIVEINT, Parameter(CORINFO_TYPE_NATIVEINT));
GLOBAL_METHOD(METHOD_FORMAT_OBJECT, &PyJit_FormatObject, CORINFO_TYPE_NATIVEINT, Parameter(CORINFO_TYPE_NATIVEINT), Parameter(CORINFO_TYPE_NATIVEINT));

Expand Down
3 changes: 2 additions & 1 deletion src/pyjion/pyjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ typedef struct PyjionSettings {
bool tracing = false;
bool profiling = false;
unsigned short optimizationLevel = 1;
int recursionLimit = 1000;
int recursionLimit = DEFAULT_RECURSION_LIMIT;
int codeObjectSizeLimit = DEFAULT_CODEOBJECT_SIZE_LIMIT;

// Optimizations
bool opt_inlineIs = OPTIMIZE_IS; // OPT-1
Expand Down

0 comments on commit 1cfad51

Please sign in to comment.