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
22 changes: 13 additions & 9 deletions src/vice_core/adapters/python_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from collections.abc import Callable, Sequence

from vice_core.adapters.base import CompilerDescriptor
from vice_core.adapters.python_dis import MAX_PYTHON_SOURCE_BYTES, _truncate_utf8_lines
from vice_core.parsers.diagnostics import parse_diagnostics as parse_stderr_diagnostics
from vice_core.parsers.gas import ParsedAssembly
from vice_core.parsers.python_ast import format_python_ast_to_text, parse_python_ast_text
Expand Down Expand Up @@ -85,6 +86,14 @@ def execute(
)

filename = req.source_path or "<stdin>"
if len(req.source_text.encode("utf-8")) > MAX_PYTHON_SOURCE_BYTES:
return RunResult(
exit_code=1,
stdout="",
stderr=f"{filename}:1:1: error: Python source exceeds safety limit ({MAX_PYTHON_SOURCE_BYTES} bytes)\n",
compile_ms=int((time.perf_counter() - start) * 1000),
raw_bytes=0,
)
try:
text = format_python_ast_to_text(req.source_text, filename=filename)
except SyntaxError as exc:
Expand All @@ -99,27 +108,22 @@ def execute(
compile_ms=int((time.perf_counter() - start) * 1000),
raw_bytes=0,
)
except ValueError as exc:
except (ValueError, RecursionError, MemoryError) as exc:
return RunResult(
exit_code=1,
stdout="",
stderr=f"{filename}:1:1: error: {exc}\n",
stderr=f"{filename}:1:1: error: {type(exc).__name__}: {exc}\n",
compile_ms=int((time.perf_counter() - start) * 1000),
raw_bytes=0,
)

encoded = text.encode("utf-8")
truncated = False
if len(encoded) > req.max_output_bytes:
encoded = encoded[: req.max_output_bytes]
text = encoded.decode("utf-8", errors="replace")
truncated = True
text, raw_bytes, truncated = _truncate_utf8_lines(text, req.max_output_bytes)
return RunResult(
exit_code=0,
stdout=text,
stderr="",
compile_ms=int((time.perf_counter() - start) * 1000),
raw_bytes=len(encoded),
raw_bytes=raw_bytes,
truncated=truncated,
)

Expand Down
36 changes: 26 additions & 10 deletions src/vice_core/adapters/python_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
from vice_core.runner import RunResult
from vice_core.schema import CommandSpec, CompileRequest, CompilerInfo, Diagnostic

MAX_PYTHON_SOURCE_BYTES: int = 2 * 1024 * 1024


def _truncate_utf8_lines(text: str, max_bytes: int) -> tuple[str, int, bool]:
encoded = text.encode("utf-8")
if len(encoded) <= max_bytes:
return text, len(encoded), False
cut = encoded[:max_bytes]
nl = cut.rfind(b"\n")
if nl > 0:
cut = cut[: nl + 1]
return cut.decode("utf-8", errors="ignore"), len(cut), True


class PythonDisAdapter:
"""Statically compiles Python source into CPython bytecode without executing it."""
Expand Down Expand Up @@ -105,6 +118,15 @@ def execute(
elif "-O" in spec.argv:
opt = 1
filename = req.source_path or "<stdin>"
if len(req.source_text.encode("utf-8")) > MAX_PYTHON_SOURCE_BYTES:
elapsed_ms = int((time.perf_counter() - start) * 1000)
return RunResult(
exit_code=1,
stdout="",
stderr=f"{filename}:1:1: error: Python source exceeds safety limit ({MAX_PYTHON_SOURCE_BYTES} bytes)\n",
compile_ms=elapsed_ms,
raw_bytes=0,
)

try:
text = disassemble_python_to_text(
Expand All @@ -126,10 +148,9 @@ def execute(
compile_ms=elapsed_ms,
raw_bytes=0,
)
except ValueError as exc:
# E.g. source code string cannot contain null bytes
except (ValueError, RecursionError, MemoryError) as exc:
elapsed_ms = int((time.perf_counter() - start) * 1000)
stderr = f"{filename}:1:1: error: {exc}\n"
stderr = f"{filename}:1:1: error: {type(exc).__name__}: {exc}\n"
return RunResult(
exit_code=1,
stdout="",
Expand All @@ -138,19 +159,14 @@ def execute(
raw_bytes=0,
)

encoded = text.encode("utf-8")
truncated = False
if len(encoded) > req.max_output_bytes:
encoded = encoded[: req.max_output_bytes]
text = encoded.decode("utf-8", errors="replace")
truncated = True
text, raw_bytes, truncated = _truncate_utf8_lines(text, req.max_output_bytes)
elapsed_ms = int((time.perf_counter() - start) * 1000)
return RunResult(
exit_code=0,
stdout=text,
stderr="",
compile_ms=elapsed_ms,
raw_bytes=len(encoded),
raw_bytes=raw_bytes,
truncated=truncated,
)

Expand Down
Loading