|
1 | 1 | # Copyright 2025-2026 Arm Limited and/or its affiliates. |
2 | | -# Copyright 2026 Arm Limited and/or its affiliates. |
3 | 2 | # |
4 | 3 | # This source code is licensed under the BSD-style license found in the |
5 | 4 | # LICENSE file in the root directory of this source tree. |
6 | 5 |
|
7 | 6 | import os |
| 7 | +from pathlib import Path |
8 | 8 | from types import SimpleNamespace |
9 | 9 | from typing import cast |
10 | 10 | from unittest import mock |
|
23 | 23 | _copy_failure_artifacts, |
24 | 24 | _format_repro_command, |
25 | 25 | _replace_converter_input_path, |
| 26 | + _safe_output_path, |
| 27 | + _validate_tag_name, |
26 | 28 | vgf_compile, |
27 | 29 | ) |
28 | 30 | from executorch.exir.backend.backend_details import PreprocessResult |
@@ -177,6 +179,83 @@ def fake_vgf_compile(tosa_flatbuffer, compile_flags, artifact_path, tag_name): |
177 | 179 | assert captured_flags == ["--emit-debug-info"] |
178 | 180 |
|
179 | 181 |
|
| 182 | +@pytest.mark.parametrize( |
| 183 | + "tag_name", |
| 184 | + [ |
| 185 | + "", |
| 186 | + "delegate_0", |
| 187 | + "delegate-1", |
| 188 | + "delegate.2", |
| 189 | + "Partition_A-1.2", |
| 190 | + ], |
| 191 | +) |
| 192 | +def test_validate_tag_name_accepts_safe_values(tag_name): |
| 193 | + assert _validate_tag_name(tag_name) == tag_name |
| 194 | + |
| 195 | + |
| 196 | +@pytest.mark.parametrize( |
| 197 | + "tag_name", |
| 198 | + [ |
| 199 | + "..", |
| 200 | + "../escape", |
| 201 | + "../../escape", |
| 202 | + "delegate/../../../escape", |
| 203 | + "/absolute/escape", |
| 204 | + r"..\escape", |
| 205 | + r"delegate\..\..\escape", |
| 206 | + r"C:\temp\escape", |
| 207 | + "delegate name", |
| 208 | + "delegate:name", |
| 209 | + "delegate$evil", |
| 210 | + ], |
| 211 | +) |
| 212 | +def test_validate_tag_name_rejects_unsafe_path_values(tag_name): |
| 213 | + with pytest.raises(ValueError, match="Invalid VGF delegation tag"): |
| 214 | + _validate_tag_name(tag_name) |
| 215 | + |
| 216 | + |
| 217 | +def test_safe_output_path_accepts_child_path(tmp_path): |
| 218 | + output_path = _safe_output_path(str(tmp_path), "output.tosa") |
| 219 | + |
| 220 | + assert output_path == os.path.realpath(tmp_path / "output.tosa") |
| 221 | + |
| 222 | + |
| 223 | +def test_safe_output_path_rejects_parent_traversal(tmp_path): |
| 224 | + with pytest.raises( |
| 225 | + ValueError, |
| 226 | + match="escapes the intended directory", |
| 227 | + ): |
| 228 | + _safe_output_path(str(tmp_path), "../outside.tosa") |
| 229 | + |
| 230 | + |
| 231 | +def test_copy_failure_artifacts_rejects_unsafe_tag_name(tmp_path): |
| 232 | + tosa_path = tmp_path / "input.tosa" |
| 233 | + artifact_path = tmp_path / "artifacts" |
| 234 | + tosa_path.write_bytes(b"tosa bytes") |
| 235 | + |
| 236 | + with pytest.raises(ValueError, match="Invalid VGF delegation tag"): |
| 237 | + _copy_failure_artifacts( |
| 238 | + str(tosa_path), |
| 239 | + str(artifact_path), |
| 240 | + tag_name="../../outside", |
| 241 | + ) |
| 242 | + |
| 243 | + assert not list(tmp_path.glob("outside*")) |
| 244 | + |
| 245 | + |
| 246 | +@mock.patch("executorch.backends.arm.vgf.backend.subprocess.run") |
| 247 | +def test_vgf_compile_rejects_unsafe_tag_before_converter_is_invoked(mock_run): |
| 248 | + with pytest.raises(ValueError, match="Invalid VGF delegation tag"): |
| 249 | + vgf_compile( |
| 250 | + b"serialized tosa", |
| 251 | + [], |
| 252 | + artifact_path=None, |
| 253 | + tag_name="../../outside", |
| 254 | + ) |
| 255 | + |
| 256 | + mock_run.assert_not_called() |
| 257 | + |
| 258 | + |
180 | 259 | def test_format_repro_command_quotes_shell_metacharacters(): |
181 | 260 | command = [ |
182 | 261 | "model-converter", |
@@ -348,7 +427,8 @@ def test_vgf_compile_failure_includes_temp_repro_command_without_artifact_path( |
348 | 427 | assert "Vgf compiler failed." in error |
349 | 428 | assert "Repro command:" in error |
350 | 429 | assert "model-converter --some-flag -i" in error |
351 | | - assert "output_delegate_0.tosa.vgf" in error |
| 430 | + assert "output.tosa.vgf" in error |
| 431 | + assert "output_delegate_0.tosa" not in error |
352 | 432 | assert "failed_model_converter_input_delegate_0.tosa" not in error |
353 | 433 | assert "Stderr:\nconverter stderr" in error |
354 | 434 | assert "Stdout:\nconverter stdout" in error |
@@ -390,3 +470,64 @@ def test_vgf_compile_failure_preserves_converter_error_when_artifact_copy_fails( |
390 | 470 | assert "cannot copy artifact" in error |
391 | 471 | assert "Stderr:\nconverter stderr" in error |
392 | 472 | assert "Stdout:\nconverter stdout" in error |
| 473 | + |
| 474 | + |
| 475 | +@mock.patch("executorch.backends.arm.vgf.backend.model_converter_env") |
| 476 | +@mock.patch("executorch.backends.arm.vgf.backend.require_model_converter_executable") |
| 477 | +@mock.patch("executorch.backends.arm.vgf.backend.subprocess.run") |
| 478 | +def test_vgf_compile_success_artifacts_are_distinct_per_tag( |
| 479 | + mock_run, |
| 480 | + mock_require_model_converter_executable, |
| 481 | + mock_model_converter_env, |
| 482 | + tmp_path, |
| 483 | +): |
| 484 | + artifact_path = tmp_path / "artifacts" |
| 485 | + |
| 486 | + mock_require_model_converter_executable.return_value = "model-converter" |
| 487 | + mock_model_converter_env.return_value = {"PATH": "/test/bin"} |
| 488 | + |
| 489 | + def fake_converter_run(command, **kwargs): |
| 490 | + input_path = command[command.index("-i") + 1] |
| 491 | + output_path = command[command.index("-o") + 1] |
| 492 | + |
| 493 | + # Make the generated VGF content depend on the TOSA input so the test |
| 494 | + # verifies both compilation results independently. |
| 495 | + input_bytes = Path(input_path).read_bytes() |
| 496 | + Path(output_path).write_bytes(b"vgf:" + input_bytes) |
| 497 | + |
| 498 | + return backend.subprocess.CompletedProcess( |
| 499 | + command, |
| 500 | + 0, |
| 501 | + stdout=b"", |
| 502 | + stderr=b"", |
| 503 | + ) |
| 504 | + |
| 505 | + mock_run.side_effect = fake_converter_run |
| 506 | + |
| 507 | + tag0_result = vgf_compile( |
| 508 | + b"partition zero", |
| 509 | + [], |
| 510 | + artifact_path=str(artifact_path), |
| 511 | + tag_name="tag0", |
| 512 | + ) |
| 513 | + tag1_result = vgf_compile( |
| 514 | + b"partition one", |
| 515 | + [], |
| 516 | + artifact_path=str(artifact_path), |
| 517 | + tag_name="tag1", |
| 518 | + ) |
| 519 | + |
| 520 | + tag0_artifact = artifact_path / "output_tag0.tosa.vgf" |
| 521 | + tag1_artifact = artifact_path / "output_tag1.tosa.vgf" |
| 522 | + |
| 523 | + assert tag0_result == b"vgf:partition zero" |
| 524 | + assert tag1_result == b"vgf:partition one" |
| 525 | + |
| 526 | + assert tag0_artifact.read_bytes() == b"vgf:partition zero" |
| 527 | + assert tag1_artifact.read_bytes() == b"vgf:partition one" |
| 528 | + |
| 529 | + # Regression check: successful compilations must not collapse back onto |
| 530 | + # the basename of the fixed temporary converter output. |
| 531 | + assert not (artifact_path / "output.tosa.vgf").exists() |
| 532 | + |
| 533 | + assert mock_run.call_count == 2 |
0 commit comments