diff --git a/.github/workflows/macos-appkit-provider.yml b/.github/workflows/macos-appkit-provider.yml index 83c1d85b..c320fef2 100644 --- a/.github/workflows/macos-appkit-provider.yml +++ b/.github/workflows/macos-appkit-provider.yml @@ -13,6 +13,11 @@ name: macOS AppKit provider checks type: choice options: [provider, display] default: provider + llvm: + description: Enable native application routes in the display campaign + type: choice + options: ["OFF", "ON"] + default: "OFF" permissions: contents: read @@ -21,9 +26,8 @@ concurrency: group: macos-appkit-provider-${{ github.ref }}-${{ inputs.architecture }} cancel-in-progress: false -# Build and exercise the AppKit desktop provider only. LLVM is off and no SDK -# is configured, qualified or published, so a cycle here costs minutes rather -# than a full source campaign. +# Provider-only checks keep LLVM off. The display campaign can enable native +# application routes. Neither scope qualifies or publishes an SDK. jobs: provider: if: ${{ inputs.scope == 'provider' }} @@ -80,7 +84,7 @@ jobs: display: if: ${{ inputs.scope == 'display' }} - name: macOS ${{ matrix.host.arch }} AppKit display contract + name: macOS ${{ matrix.host.arch }} AppKit display / LLVM ${{ inputs.llvm }} runs-on: ${{ matrix.host.runner }} timeout-minutes: 60 strategy: @@ -91,6 +95,9 @@ jobs: defaults: run: shell: bash + env: + NIL_CI_LLVM: ${{ inputs.llvm }} + HOMEBREW_NO_AUTO_UPDATE: '1' steps: - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 with: @@ -105,18 +112,50 @@ jobs: print('CGMainDisplayID:', core.CGMainDisplayID()) " # The language-level contract needs the compiler, interpreter and test - # runner as well as the provider, so this is a whole build; LLVM stays - # off to keep it to one route rather than a full source campaign. - - name: Configure without LLVM + # runner as well as the provider. LLVM ON adds actual native cases. + - name: Configure the selected application routes timeout-minutes: 8 run: | + nil_llvm_args=() + if [ "$NIL_CI_LLVM" = ON ]; then + nil_llvm_prefix="$(brew --prefix llvm@18)" + test -x "$nil_llvm_prefix/bin/llvm-config" + nil_llvm_args+=("-DLLVM_DIR=$nil_llvm_prefix/lib/cmake/llvm") + fi cmake -S . -B build/ci \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_TESTING=ON \ - -DNIL_ENABLE_LLVM=OFF + -DNIL_ENABLE_LLVM="$NIL_CI_LLVM" "${nil_llvm_args[@]}" - name: Build timeout-minutes: 40 - run: cmake --build build/ci --parallel 3 + run: >- + cmake --build build/ci --parallel 2 --target nilcompiler nilinterpreter + niltest nil_ui_runtime_appkit_tests nil_ui_lifecycle_tests nil_ui_appkit_input_driver + nil_ui_appkit_compile_driver nil_ui_layout_tests - name: Language-level desktop contract timeout-minutes: 15 - run: ctest --test-dir build/ci -R 'nil.ui' --output-on-failure + run: | + # Anchor the product namespace: 'nil.ui' also matches nilbuild.*. + ctest --test-dir build/ci -R '^nil[.]ui([.-]|$)' \ + --no-tests=error --output-on-failure --output-junit appkit-display-results.xml + python3 - <<'PY' + import xml.etree.ElementTree as ET + cases = list(ET.parse('build/ci/appkit-display-results.xml').getroot().iter('testcase')) + expected = {'nil.ui-runtime', 'nil.ui-layout', 'nil.ui-windows-display-harness', 'nil.ui-appkit-display-harness', 'nil.ui-display', + 'nil.ui-intrinsic-contract', 'nil.ui-models'} + assert len(cases) == len(expected) and {case.get('name') for case in cases} == expected + assert all(case.get('status') == 'run' and case.find('skipped') is None and + case.find('failure') is None and case.find('error') is None for case in cases) + PY + - name: Preserve complete or partial application evidence + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: appkit-display-${{ matrix.host.arch }}-llvm-${{ inputs.llvm }}-${{ github.sha }}-${{ github.run_attempt }} + path: | + build/ci/appkit-display-results.xml + build/ci/Testing/Temporary/LastTest.log + build/ci/CMakeCache.txt + build/ci/appkit-input-evidence/ + if-no-files-found: error + retention-days: 14 diff --git a/.github/workflows/macos-application-parity.yml b/.github/workflows/macos-application-parity.yml new file mode 100644 index 00000000..5fae6c5e --- /dev/null +++ b/.github/workflows/macos-application-parity.yml @@ -0,0 +1,437 @@ +name: macOS application platform checks + +"on": + pull_request: + paths: + - '.github/workflows/macos-application-parity.yml' + - 'CMakeLists.txt' + - 'nilcompiler/**' + - 'nilruntime-freestanding/**' + - 'nilinterpreter/**' + - 'nilstd/**' + - 'nilbuild/**' + - 'niltest/**' + - 'nildebugger/**' + - 'nillsp/**' + - 'aperture/**' + - 'common/**' + - 'nillinker/**' + - 'validation/ny-3d/scripts/debugger_process.py' + - 'scripts/ci/macos-toolchain-census.py' + - 'tests/darwin*' + - 'tests/compiler_module_darwin_tests.cpp' + - 'tests/tools/darwin_native_heap_budget.py' + - 'tests/tools/native_managed_lifetime.py' + - 'tests/tools/native_owned_lifetime.py' + - 'tests/tools/native_symbol_surface.py' + - 'tests/tools/file_modified_at_contract.py' + - 'tests/tools/formatted_text_contract.py' + - 'tests/tools/markup_contract.py' + - 'tests/language/formatted-text*' + - 'tests/language/markup*' + - 'tests/routes/ny_route_matrix.py' + - 'tests/tools/native_toolchain_pipeline.py' + - 'tests/tools/project_build_contract.py' + - 'tests/tools/tool_discovery_contract.py' + - 'tests/tools/context_manifest.py' + - 'nilassembler/tests/nilassembler_cli_tests.py' + - 'scripts/sdk_freestanding_imports.py' + - 'scripts/package-sdk.py' + - 'scripts/validate-sdk.py' + - 'tests/sdk_freestanding_imports_tests.py' + - 'tests/sdk_matrix_sources_tests.py' + - 'tests/freestanding_hook_contract.cpp' + - 'vscode/**' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: macos-application-parity-${{ github.ref }} + cancel-in-progress: true + +# Real LLVM native consumers and an isolated VS Code development-extension host. +# No installed SDK, signed release, personal profile or extension is replaced. +jobs: + applications: + name: macOS ${{ matrix.host.arch }} native applications and editor + runs-on: ${{ matrix.host.runner }} + timeout-minutes: 100 + strategy: + fail-fast: false + matrix: + host: + - runner: macos-15-intel + arch: x86_64 + archive: VSCode-darwin.zip + sha256: 74b1deef1e33cc144d913d60c0e98cef6b1e29c1f2993d905c3b58576fa12202 + - runner: macos-15 + arch: arm64 + archive: VSCode-darwin-arm64.zip + sha256: 6e8d27ee0c5a8f2aa3c43e373407eecd11c270c3fe7f46100ef682aac14bb648 + defaults: + run: + shell: bash + env: + HOMEBREW_NO_AUTO_UPDATE: '1' + PYTHONDONTWRITEBYTECODE: '1' + NIL_EDITOR_ARCHIVE: ${{ matrix.host.archive }} + NIL_EDITOR_SHA256: ${{ matrix.host.sha256 }} + steps: + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + with: + node-version: '22' + cache: npm + cache-dependency-path: vscode/package-lock.json + - name: Configure LLVM applications + id: configure + timeout-minutes: 8 + run: | + mkdir -p build/application-evidence + git rev-parse HEAD > build/application-evidence/revision.txt + uname -a > build/application-evidence/host.txt + sw_vers >> build/application-evidence/host.txt + set -o pipefail + nil_llvm_prefix="$(brew --prefix llvm@18)" + test -x "$nil_llvm_prefix/bin/llvm-config" + cmake -S . -B build/application -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_TESTING=ON -DNIL_ENABLE_LLVM=ON \ + "-DLLVM_DIR=$nil_llvm_prefix/lib/cmake/llvm" \ + -DNILCOMPILER_SQLITE_INCLUDE_DIR=OFF -DNILCOMPILER_POSTGRESQL_INCLUDE_DIR=OFF \ + 2>&1 | tee build/application-evidence/configure.log + - name: Build independent allocation controls + id: heap + timeout-minutes: 3 + run: | + set -o pipefail + cmake --build build/application --parallel 2 --target \ + nil_darwin_native_heap_budget nil_darwin_native_heap_budget_control \ + 2>&1 | tee build/application-evidence/heap-build.log + - name: Qualify allocator injection before building applications + if: ${{ !cancelled() && steps.heap.outcome == 'success' }} + timeout-minutes: 4 + run: | + set -o pipefail + python3 -B tests/tools/darwin_native_heap_budget.py --controls-only \ + --heap-budget-library build/application/lib/libnil_darwin_native_heap_budget.dylib \ + --heap-budget-control build/application/bin/nil_darwin_native_heap_budget_control \ + --output-parent build/application/test-evidence \ + 2>&1 | tee build/application-evidence/heap-controls.log + - name: Preserve early allocation evidence + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: macos-heap-controls-${{ matrix.host.arch }}-${{ github.sha }}-${{ github.run_attempt }} + path: | + build/application/test-evidence/darwin-heap-controls-*/ + build/application-evidence/heap-*.log + if-no-files-found: warn + retention-days: 14 + - name: Build early native dependency probe + id: native_inputs + if: ${{ !cancelled() && steps.configure.outcome == 'success' }} + timeout-minutes: 8 + run: | + set -o pipefail + cmake --build build/application --parallel 2 --target nil_darwin_native_inputs_tests \ + 2>&1 | tee build/application-evidence/native-inputs-build.log + - name: Check the selected native dependency profile + if: ${{ !cancelled() && steps.native_inputs.outcome == 'success' }} + timeout-minutes: 4 + run: | + set -o pipefail + ctest --test-dir build/application -R '^nil[.]darwin-native-inputs$' \ + --no-tests=error --output-on-failure --output-junit native-inputs-results.xml \ + 2>&1 | tee build/application-evidence/native-inputs.log + - name: Preserve early native dependency evidence + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: macos-native-inputs-${{ matrix.host.arch }}-${{ github.sha }}-${{ github.run_attempt }} + path: | + build/application-evidence/native-inputs*.log + build/application/native-inputs-results.xml + if-no-files-found: warn + retention-days: 14 + - name: Build application tools + id: tools + if: ${{ !cancelled() && steps.configure.outcome == 'success' }} + timeout-minutes: 40 + run: | + set -o pipefail + cmake --build build/application --parallel 2 --target \ + nilcompiler nilinterpreter nilassembler nillinker nillsp nildebugger nilbuild niltest aperture nilruntime_shared \ + nil_vscode_host_runner nil_file_time_support_tests nil_compiler_module_darwin_tests \ + nil_darwin_toolchain_inputs_tests nil_freestanding_hook_default nil_freestanding_hook_override \ + nildebugger_core_tests nildebugger_source_metadata_tests nildebugger_dap_owner_tests \ + 2>&1 | tee build/application-evidence/build.log + - name: Verify formatted text and markup through interpreter and native routes + if: ${{ !cancelled() && steps.tools.outcome == 'success' }} + timeout-minutes: 8 + run: | + python3 - <<'PYTHON' + import json, subprocess, xml.etree.ElementTree as ET + from pathlib import Path + expected = {'nil.formatted-text-contract', 'nil.markup-contract'} + expected.update('ny.'+route+'.'+fixture for route in ['routes', 'tools'] + for fixture in ['formatted-text', 'formatted-text-matrix', 'markup', 'markup-matrix']) + pattern = r'^(nil[.](formatted-text-contract|markup-contract)|ny[.](routes|tools)[.](formatted-text|markup)(-matrix)?)$' + base = ['ctest', '--test-dir', 'build/application', '-R', pattern] + root = Path('build/application-evidence').resolve() + inventory = subprocess.check_output(base+['--show-only=json-v1'], timeout=30) + (root/'markup-inventory.json').write_bytes(inventory) + assert {case['name'] for case in json.loads(inventory)['tests']} == expected + with (root/'markup.log').open('wb') as output: + run = subprocess.run(base+['--no-tests=error', '--output-on-failure', + '--test-output-size-passed', '65536', + '--output-junit', str(root/'markup-results.xml')], stdout=output, + stderr=subprocess.STDOUT, timeout=450) + print((root/'markup.log').read_text(errors='replace')) + run.check_returncode() + cases = list(ET.parse(root/'markup-results.xml').getroot().iter('testcase')) + assert len(cases) == len(expected) and {case.get('name') for case in cases} == expected + assert all(case.get('status') == 'run' and all(case.find(kind) is None + for kind in ['failure', 'error', 'skipped']) for case in cases) + PYTHON + - name: Verify debugger protocols and owned desktop sessions + if: ${{ !cancelled() && steps.tools.outcome == 'success' }} + timeout-minutes: 12 + run: | + set -o pipefail + python3 - <<'PYTHON' + import json, subprocess, xml.etree.ElementTree as ET + from pathlib import Path + expected = {'nildebugger.'+name for name in + ('core', 'json-protocol', 'dap-protocol', 'source-metadata', 'dap-lifecycle', 'dap-owner')} + base = ['ctest', '--test-dir', 'build/application', '-R', + r'^nildebugger[.](core|json-protocol|dap-protocol|source-metadata|dap-lifecycle|dap-owner)$'] + root = Path('build/application-evidence').resolve() + inventory = subprocess.check_output(base+['--show-only=json-v1'], timeout=30) + (root/'debugger-inventory.json').write_bytes(inventory) + assert {case['name'] for case in json.loads(inventory)['tests']} == expected + with (root/'debugger.log').open('wb') as output: + run = subprocess.run(base+['--no-tests=error', '--output-on-failure', + '--output-junit', str(root/'debugger-results.xml')], stdout=output, + stderr=subprocess.STDOUT, timeout=480) + print((root/'debugger.log').read_text(errors='replace')) + run.check_returncode() + cases = list(ET.parse(root/'debugger-results.xml').getroot().iter('testcase')) + assert len(cases) == len(expected) and {case.get('name') for case in cases} == expected + assert all(case.get('status') == 'run' and all(case.find(kind) is None + for kind in ['failure','error','skipped']) for case in cases) + PYTHON + python3 -B nildebugger/tests/dap_desktop.py \ + --debugger build/application/bin/nildebugger --compiler build/application/bin/nilcompiler \ + --nilstd nilstd/src --output-parent build/application/test-evidence \ + 2>&1 | tee build/application-evidence/debugger-desktop.log + - name: Record selected native SDK and linker inputs + if: ${{ !cancelled() && steps.tools.outcome == 'success' }} + timeout-minutes: 2 + run: >- + python3 -B scripts/ci/macos-toolchain-census.py --build build/application + --output build/application-evidence/toolchain-census + - name: Verify actual native lifetime and timestamp contracts + if: ${{ !cancelled() && steps.tools.outcome == 'success' && steps.heap.outcome == 'success' }} + timeout-minutes: 16 + run: | + python3 - <<'PYTHON' + import json, subprocess, xml.etree.ElementTree as ET + from pathlib import Path + expected = {'compiler.darwin-module-fingerprint', 'ny.native-managed-lifetime', 'ny.native-owned-lifetime', + 'nil.file-time-support', 'nil.file-modified-at-contract', + 'nil.freestanding-hook-default', 'nil.freestanding-hook-override', 'nil.native-symbol-surface'} + pattern = r'^(compiler[.]darwin-module-fingerprint|ny[.]native-(managed|owned)-lifetime|nil[.]file-(time-support|modified-at-contract)|nil[.]freestanding-hook-(default|override)|nil[.]native-symbol-surface)$' + base = ['ctest', '--test-dir', 'build/application', '-R', pattern] + root = Path('build/application-evidence').resolve() + inventory = subprocess.check_output(base+['--show-only=json-v1'], timeout=30) + (root/'inventory.json').write_bytes(inventory) + assert {case['name'] for case in json.loads(inventory)['tests']} == expected + with (root/'ctest.log').open('wb') as output: + run = subprocess.run(base+['--no-tests=error', '--output-on-failure', + '--output-junit', str(root/'results.xml')], stdout=output, + stderr=subprocess.STDOUT, timeout=910) + print((root/'ctest.log').read_text(errors='replace')) + run.check_returncode() + cases = list(ET.parse(root/'results.xml').getroot().iter('testcase')) + assert len(cases) == len(expected) and {case.get('name') for case in cases} == expected + assert all(case.get('status') == 'run' and all(case.find(kind) is None + for kind in ['failure','error','skipped']) for case in cases) + PYTHON + - name: Verify project, discovery, assembler, context, archive and packaging contracts + if: ${{ !cancelled() && steps.tools.outcome == 'success' }} + timeout-minutes: 30 + run: | + python3 - <<'PYTHON' + import json, subprocess, xml.etree.ElementTree as ET + from pathlib import Path + expected = {'nilbuild.project-contract', 'nilbuild.tool-discovery-contract', 'nilassembler.cli', + 'ny.tools.context-manifest', 'nil.freestanding-archive-imports', 'ny.sdk-matrix-sources'} + pattern = r'^(nilbuild[.](project-contract|tool-discovery-contract)|nilassembler[.]cli|ny[.]tools[.]context-manifest|nil[.]freestanding-archive-imports|ny[.]sdk-matrix-sources)$' + base = ['ctest', '--test-dir', 'build/application', '-R', pattern] + root = Path('build/application-evidence').resolve() + inventory = subprocess.check_output(base+['--show-only=json-v1'], timeout=30) + (root/'sdk-contract-inventory.json').write_bytes(inventory) + assert {case['name'] for case in json.loads(inventory)['tests']} == expected + with (root/'sdk-contracts.log').open('wb') as output: + run = subprocess.run(base+['--no-tests=error', '--output-on-failure', + '--test-output-size-passed', '65536', + '--output-junit', str(root/'sdk-contract-results.xml')], stdout=output, + stderr=subprocess.STDOUT, timeout=1750) + print((root/'sdk-contracts.log').read_text(errors='replace')) + run.check_returncode() + cases = list(ET.parse(root/'sdk-contract-results.xml').getroot().iter('testcase')) + assert len(cases) == len(expected) and {case.get('name') for case in cases} == expected + assert all(case.get('status') == 'run' and all(case.find(kind) is None + for kind in ['failure','error','skipped']) for case in cases) + PYTHON + - name: Verify default native toolchain and real incremental reuse + if: ${{ !cancelled() && steps.tools.outcome == 'success' }} + timeout-minutes: 20 + run: | + python3 - <<'PYTHON' + import json, subprocess, xml.etree.ElementTree as ET + from pathlib import Path + expected = {'nil.darwin-toolchain-inputs', 'nilbuild.incremental-contract'} + pattern = r'^(nil[.]darwin-toolchain-inputs|nilbuild[.]incremental-contract)$' + base = ['ctest', '--test-dir', 'build/application', '-R', pattern] + root = Path('build/application-evidence').resolve() + inventory = subprocess.check_output(base+['--show-only=json-v1'], timeout=30) + (root/'incremental-inventory.json').write_bytes(inventory) + assert {case['name'] for case in json.loads(inventory)['tests']} == expected + with (root/'incremental.log').open('wb') as output: + run = subprocess.run(base+['--no-tests=error', '--output-on-failure', + '--output-junit', str(root/'incremental-results.xml')], stdout=output, + stderr=subprocess.STDOUT, timeout=1100) + print((root/'incremental.log').read_text(errors='replace')) + run.check_returncode() + cases = list(ET.parse(root/'incremental-results.xml').getroot().iter('testcase')) + assert len(cases) == len(expected) and {case.get('name') for case in cases} == expected + assert all(case.get('status') == 'run' and all(case.find(kind) is None + for kind in ['failure','error','skipped']) for case in cases) + PYTHON + - name: Prepare pinned VS Code in this runner's private build directory + id: editor + if: ${{ !cancelled() && steps.tools.outcome == 'success' }} + timeout-minutes: 6 + run: | + # Version 1.146.0; architecture-specific SHA256 values from Microsoft's + # update API on 2026-11-11. No auto-update or system installation. + mkdir -p build/editor-download + curl --fail --location --max-time 120 --max-filesize 800000000 \ + --retry 2 --retry-all-errors --retry-delay 2 --retry-max-time 180 \ + "https://vscode.download.prss.microsoft.com/dbazure/download/stable/69047191fc427f2f92828279aae1d90d8eeda7ab/$NIL_EDITOR_ARCHIVE" \ + -o build/editor-download/editor.zip + python3 - <<'PYTHON' + import hashlib, json, os + from pathlib import Path + path = Path('build/editor-download/editor.zip') + digest_state = hashlib.sha256() + with path.open('rb') as stream: + for chunk in iter(lambda: stream.read(1024*1024), b''): digest_state.update(chunk) + digest = digest_state.hexdigest() + assert digest == os.environ['NIL_EDITOR_SHA256'] + Path('build/application-evidence/editor.json').write_text(json.dumps(dict( + version='1.146.0', revision='69047191fc427f2f92828279aae1d90d8eeda7ab', + archive=os.environ['NIL_EDITOR_ARCHIVE'], sha256=digest, bytes=path.stat().st_size), indent=2)+'\n') + PYTHON + ditto -x -k build/editor-download/editor.zip build/editor-download + - name: Build and check the extension + id: extension + if: ${{ !cancelled() && steps.editor.outcome == 'success' }} + timeout-minutes: 5 + working-directory: vscode + run: | + npm ci + npm test + npm run build + - name: Exercise the real editor host + if: ${{ !cancelled() && steps.extension.outcome == 'success' }} + timeout-minutes: 17 + env: + NIL_VSCODE_HOST_TIMEOUT_MS: '900000' + run: | + set -o pipefail + VSCODE_EXECUTABLE="$(python3 - <<'PYTHON' + import plistlib + from pathlib import Path + app = Path('build/editor-download/Visual Studio Code.app/Contents').resolve(strict=True) + with (app/'Info.plist').open('rb') as stream: name = plistlib.load(stream)['CFBundleExecutable'] + assert name and Path(name).name == name + executable = app/'MacOS'/name + assert executable.is_file() + print(executable) + PYTHON + )" + export VSCODE_EXECUTABLE + export NIL_VSCODE_TOOL_DIRECTORY="$GITHUB_WORKSPACE/build/application/bin" + export NIL_VSCODE_HOST_RUNNER="$NIL_VSCODE_TOOL_DIRECTORY/nil_vscode_host_runner" + export NIL_VSCODE_SDK_PATH="$GITHUB_WORKSPACE" + for nil_subset in NIL_VSCODE_NILDATA_ONLY NIL_VSCODE_LANGUAGE_VALUES_ONLY \ + NIL_VSCODE_MEMBER_SIGNATURE_ONLY NIL_VSCODE_COMPLETION_ONLY NIL_VSCODE_SEMANTIC_ONLY \ + NIL_VSCODE_WORKSPACE_ONLY NIL_VSCODE_ANALYSIS_ONLY NIL_VSCODE_ANALYSIS_PROBE_ONLY; do + unset "$nil_subset" + done + node vscode/test/run-host.cjs 2>&1 | tee build/application-evidence/editor-host.log + python3 - <<'PYTHON' + import json + from pathlib import Path + report = json.loads(Path('build/vscode-host-smoke/latest.json').read_text()) + assert report['platform'] == 'darwin' and report['status'] == 'PASS' + assert report['vscode'] == '1.146.0' and not report['skippedChecks'] + expected_modules = ['analyzer-checks', 'project-checks', 'workspace-checks', 'semantic-checks', + 'completion-checks', 'member-signature-checks', 'language-values-checks', + 'memory-profile-checks', 'niltest-checks'] + assert report['fullSuiteCompleted'] is True and report['completedModules'] == expected_modules + checks = report['checks'] + assert checks and all(row['status'] == 'PASS' and row['name'] for row in checks) + assert len({row['name'] for row in checks}) == len(checks) + PYTHON + - name: Preserve complete or partial evidence + if: always() + timeout-minutes: 2 + run: | + python3 - <<'PYTHON' + import hashlib, json, os, shutil, subprocess + from pathlib import Path + root = Path('build/application-evidence'); root.mkdir(parents=True, exist_ok=True) + for source, name in [('build/application/test-evidence','contracts'), + ('build/vscode-host-smoke','editor-reports')]: + if Path(source).is_dir(): shutil.copytree(source, root/name) + for launch in Path('build/vscode-host-smoke').glob('*-launch.json'): + record = json.loads(launch.read_text()) + temporary = Path(record['temporary']) + assert record['platform'] == 'darwin' and temporary.parent == Path('/tmp') + assert temporary.name.startswith('nil vscode host café 中文-') + profile = temporary/'profile' + assert str(profile) == record['profile'] and not profile.is_symlink() + logs = profile/'logs' + if logs.is_dir(): shutil.copytree(logs, root/'editor-logs'/record['runId']) + dirty = subprocess.check_output(['git','status','--porcelain','--untracked-files=no'], text=True, timeout=15) + revision = subprocess.check_output(['git','rev-parse','HEAD'], text=True, timeout=15).strip() + files = {} + for path in sorted(root.rglob('*')): + if path.is_file() and not path.is_symlink(): + digest_state = hashlib.sha256() + with path.open('rb') as stream: + for chunk in iter(lambda: stream.read(1024*1024), b''): digest_state.update(chunk) + digest = digest_state.hexdigest() + files[path.relative_to(root).as_posix()] = dict(bytes=path.stat().st_size,sha256=digest) + (root/'evidence.json').write_text(json.dumps(dict(revision=revision, + source_unchanged=not dirty,files=files),indent=2)+'\n') + assert not dirty and revision == os.environ['GITHUB_SHA'] + PYTHON + - name: Upload application results + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: macos-applications-${{ matrix.host.arch }}-${{ github.sha }}-${{ github.run_attempt }} + path: | + build/application-evidence/ + build/application/Testing/Temporary/LastTest.log + build/application/CMakeCache.txt + if-no-files-found: error + retention-days: 14 diff --git a/.github/workflows/macos-bringup.yml b/.github/workflows/macos-bringup.yml index ccff96ea..a11af12f 100644 --- a/.github/workflows/macos-bringup.yml +++ b/.github/workflows/macos-bringup.yml @@ -53,6 +53,7 @@ jobs: run: | set -o pipefail python3 -B scripts/ci/test_macos_evidence.py -v 2>&1 | tee build/macos-evidence/collector-controls.log + python3 -B tests/test_runtime_provider_environment.py -v 2>&1 | tee build/macos-evidence/provider-environment-controls.log - name: Observe image toolchain and optional providers timeout-minutes: 3 run: | @@ -66,9 +67,12 @@ jobs: test -f "$nil_macos_sdk/usr/include/sqlite3.h" { echo "NIL_CI_SQLITE_INCLUDE=$nil_macos_sdk/usr/include" - echo "DYLD_FALLBACK_LIBRARY_PATH=/usr/local/lib:/usr/lib" + # Inert declaration: provider harnesses apply it only to runtime + # children. Never export a DYLD override to compiler/build steps. + echo "NIL_TEST_DARWIN_RUNTIME_LIBRARY_PATH=/usr/local/lib:/usr/lib" } >> "$GITHUB_ENV" printf 'SQLite: Apple SDK headers at %s; system dylib provider\n' "$nil_macos_sdk/usr/include" > build/macos-evidence/providers.txt + echo 'SQL loader paths are runtime-only declarations; JIT combines compilation/execution in one process' >> build/macos-evidence/providers.txt # Use one installed PostgreSQL distribution for headers, client # library, binaries and shared data; never mix independent versions. if nil_pg_prefix="$(brew --prefix postgresql@16 2>/dev/null)" && @@ -84,7 +88,7 @@ jobs: echo "NIL_TEST_POSTGRES_BIN=$nil_pg_prefix/bin" echo "NIL_TEST_POSTGRES_SHARE=$nil_pg_share" echo "NIL_CI_PQ_INCLUDE=$nil_pg_include" - echo "DYLD_FALLBACK_LIBRARY_PATH=$nil_pg_library:/usr/local/lib:/usr/lib" + echo "NIL_TEST_DARWIN_RUNTIME_LIBRARY_PATH=$nil_pg_library:/usr/local/lib:/usr/lib" } >> "$GITHUB_ENV" "$nil_pg_prefix/bin/pg_config" --version --includedir --libdir --sharedir >> build/macos-evidence/providers.txt else @@ -94,7 +98,7 @@ jobs: echo "NIL_CI_PQ_INCLUDE=OFF" >> "$GITHUB_ENV" echo "PostgreSQL: coherent postgresql@16 distribution absent; provider unavailable in this image profile" >> build/macos-evidence/providers.txt fi - echo "GTK: macOS backend not implemented; complete CTest inventory retains unsupported results" >> build/macos-evidence/providers.txt + echo "Desktop: native AppKit provider; display tests require a window server session" >> build/macos-evidence/providers.txt if command -v pandoc > /dev/null; then pandoc --version >> build/macos-evidence/providers.txt else @@ -167,7 +171,7 @@ jobs: # Even a partial build supplies useful NOT_RUN/failed test inventory. # An earlier build failure continues to fail the job. if: ${{ !cancelled() && steps.configure.outcome == 'success' }} - timeout-minutes: 20 + timeout-minutes: 35 run: | set -o pipefail ctest --test-dir build/ci --output-on-failure --parallel 2 \ diff --git a/.github/workflows/macos-sdk-replay.yml b/.github/workflows/macos-sdk-replay.yml index bf46252f..6be167fc 100644 --- a/.github/workflows/macos-sdk-replay.yml +++ b/.github/workflows/macos-sdk-replay.yml @@ -1,4 +1,4 @@ -name: macOS installed debugger replay +name: macOS installed SDK replay "on": workflow_dispatch: @@ -16,6 +16,10 @@ name: macOS installed debugger replay type: choice options: [both, x86_64, arm64] default: both + desktop: + description: Also accept installed nilstd/nillib UI, desktop DAP and the complete pinned editor + type: boolean + default: false permissions: contents: read @@ -29,9 +33,9 @@ concurrency: # This never rebuilds, activates, publishes or release-qualifies an SDK. jobs: replay: - name: macOS ${{ matrix.host.arch }} installed debugger + name: macOS ${{ matrix.host.arch }} installed SDK acceptance runs-on: ${{ matrix.host.runner }} - timeout-minutes: 60 + timeout-minutes: ${{ inputs.desktop && 115 || 60 }} strategy: fail-fast: false max-parallel: 2 @@ -58,6 +62,60 @@ jobs: set -o pipefail python3 -B scripts/ci/test_macos_sdk_replay.py -v 2>&1 | tee build/macos-replay-evidence/replay-controls.log python3 -B scripts/ci/test_macos_debugger.py -v 2>&1 | tee build/macos-replay-evidence/debugger-controls.log + python3 -B scripts/ci/test_macos_desktop.py -v 2>&1 | tee build/macos-replay-evidence/desktop-controls.log + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + if: inputs.desktop + with: + node-version: '22' + - name: Build only the three test-only desktop helpers + if: inputs.desktop + timeout-minutes: 5 + run: | + set -o pipefail + cmake -S . -B build/desktop-helpers -DCMAKE_BUILD_TYPE=Release -DNIL_ENABLE_LLVM=OFF -DBUILD_TESTING=ON \ + 2>&1 | tee build/macos-replay-evidence/helpers-configure.log + cmake --build build/desktop-helpers --parallel 2 --target \ + nil_ui_appkit_input_driver nil_ui_appkit_compile_driver nil_vscode_host_runner \ + 2>&1 | tee build/macos-replay-evidence/helpers-build.log + cp build/desktop-helpers/CMakeCache.txt build/macos-replay-evidence/helpers-CMakeCache.txt + - name: Prepare pinned VS Code for installed SDK acceptance + if: inputs.desktop + timeout-minutes: 5 + run: | + mkdir -p build/editor-download + if [[ "$NIL_CI_ARCH" == arm64 ]]; then + export NIL_EDITOR_ARCHIVE=VSCode-darwin-arm64.zip + export NIL_EDITOR_SHA256=6e8d27ee0c5a8f2aa3c43e373407eecd11c270c3fe7f46100ef682aac14bb648 + else + export NIL_EDITOR_ARCHIVE=VSCode-darwin.zip + export NIL_EDITOR_SHA256=74b1deef1e33cc144d913d60c0e98cef6b1e29c1f2993d905c3b58576fa12202 + fi + curl --fail --location --max-time 120 --max-filesize 800000000 \ + --retry 2 --retry-all-errors --retry-delay 2 --retry-max-time 180 \ + "https://vscode.download.prss.microsoft.com/dbazure/download/stable/69047191fc427f2f92828279aae1d90d8eeda7ab/$NIL_EDITOR_ARCHIVE" \ + -o build/editor-download/editor.zip + python3 -B - <<'PY' + import hashlib, json, os + from pathlib import Path + archive = Path('build/editor-download/editor.zip') + digest = hashlib.sha256() + with archive.open('rb') as stream: + while block := stream.read(1024**2): digest.update(block) + assert digest.hexdigest() == os.environ['NIL_EDITOR_SHA256'] + Path('build/macos-replay-evidence/editor.json').write_text(json.dumps(dict( + version='1.146.0', revision='69047191fc427f2f92828279aae1d90d8eeda7ab', + archive=os.environ['NIL_EDITOR_ARCHIVE'], sha256=digest.hexdigest(), bytes=archive.stat().st_size), indent=2)+'\n') + PY + ditto -x -k build/editor-download/editor.zip build/editor-download + - name: Build and check current fixture extension + if: inputs.desktop + timeout-minutes: 5 + working-directory: vscode + run: | + set -o pipefail + npm ci 2>&1 | tee ../build/macos-replay-evidence/extension-install.log + npm test 2>&1 | tee ../build/macos-replay-evidence/extension-controls.log + npm run build 2>&1 | tee ../build/macos-replay-evidence/extension-build.log - name: Retrieve exact SDK artifact and authenticated metadata timeout-minutes: 7 env: @@ -121,15 +179,33 @@ jobs: (evidence / 'transport.json').write_text(json.dumps({'bytes': archive.stat().st_size, 'sha256': digest.hexdigest(), 'artifact_id': artifact['id'], 'source_run': int(source_run)}, indent=2) + '\n') PY - - name: Replay sealed installed debugger and execution checkpoints - timeout-minutes: 37 + - name: Replay sealed installed SDK acceptance + timeout-minutes: ${{ inputs.desktop && 79 || 37 }} + env: + NIL_REPLAY_DESKTOP: ${{ inputs.desktop }} run: | set -o pipefail + desktop_args=() + if [[ "$NIL_REPLAY_DESKTOP" == true ]]; then + editor="$(python3 -B - <<'PY' + import plistlib + from pathlib import Path + app = Path('build/editor-download/Visual Studio Code.app/Contents').resolve(strict=True) + with (app / 'Info.plist').open('rb') as stream: name = plistlib.load(stream)['CFBundleExecutable'] + assert name and Path(name).name == name + executable = app / 'MacOS' / name + assert executable.is_file() + print(executable) + PY + )" + desktop_args=(--desktop --desktop-helpers "$GITHUB_WORKSPACE/build/desktop-helpers" + --editor "$editor" --node "$(command -v node)") + fi python3 -B scripts/ci/macos-sdk-replay.py \ --artifact-zip "$GITHUB_WORKSPACE/build/macos-replay-evidence/source-artifact.zip" \ --artifact-metadata "$GITHUB_WORKSPACE/build/macos-replay-evidence/artifact-metadata.json" \ --sdk-revision "$NIL_SDK_REVISION" --helper-revision "$GITHUB_SHA" --architecture "$NIL_CI_ARCH" \ - --work-dir "$RUNNER_TEMP/nil-sdk-replay" \ + --work-dir "$RUNNER_TEMP/nil-sdk-replay" "${desktop_args[@]}" \ --output "$GITHUB_WORKSPACE/build/macos-replay-evidence/replay" \ 2>&1 | tee build/macos-replay-evidence/replay.log - name: Seal complete or partial replay evidence diff --git a/.github/workflows/macos-sdk.yml b/.github/workflows/macos-sdk.yml index cf90600e..65365ada 100644 --- a/.github/workflows/macos-sdk.yml +++ b/.github/workflows/macos-sdk.yml @@ -63,6 +63,7 @@ jobs: python3 -B tests/sdk_consumer_routes_tests.py -v 2>&1 | tee build/macos-sdk-evidence/consumer-controls.log python3 -B scripts/ci/test_macos_postgresql_provider.py -v 2>&1 | tee build/macos-sdk-evidence/provider-controls.log python3 -B scripts/ci/test_macos_debugger.py -v 2>&1 | tee build/macos-sdk-evidence/debugger-controls.log + python3 -B tests/test_runtime_provider_environment.py -v 2>&1 | tee build/macos-sdk-evidence/provider-environment-controls.log - name: Fetch hash-pinned official Pandoc id: pandoc timeout-minutes: 5 @@ -91,7 +92,9 @@ jobs: nil_macos_sdk="$(xcrun --sdk macosx --show-sdk-path)" test -f "$nil_macos_sdk/usr/include/sqlite3.h" echo "NIL_CI_SQLITE_INCLUDE=$nil_macos_sdk/usr/include" >> "$GITHUB_ENV" - echo "DYLD_FALLBACK_LIBRARY_PATH=/usr/local/lib:/usr/lib" >> "$GITHUB_ENV" + # Keep loader paths inert during compiler and SDK build processes. + # The SQL/route harnesses apply this declaration only to execution. + echo "NIL_TEST_DARWIN_RUNTIME_LIBRARY_PATH=/usr/local/lib:/usr/lib" >> "$GITHUB_ENV" printf 'SQLite: Apple SDK headers at %s; system dylib\n' "$nil_macos_sdk/usr/include" > build/macos-sdk-evidence/providers.txt if [ "$NIL_CI_POSTGRES_PROFILE" = private16 ]; then python3 -B - <<'PY' @@ -103,7 +106,7 @@ jobs: provider.validate_provider(report) values = {'NIL_TEST_POSTGRES_BIN': report['bin_dir'], 'NIL_TEST_POSTGRES_SHARE': report['share_dir'], 'NIL_CI_PQ_INCLUDE': report['include_dir'], - 'DYLD_FALLBACK_LIBRARY_PATH': report['library_dir'] + ':/usr/lib'} + 'NIL_TEST_DARWIN_RUNTIME_LIBRARY_PATH': report['library_dir'] + ':/usr/lib'} with open(os.environ['GITHUB_ENV'], 'a') as stream: for key, value in values.items(): assert isinstance(value, str) and '\n' not in value and '\r' not in value @@ -123,7 +126,7 @@ jobs: echo "NIL_TEST_POSTGRES_BIN=$nil_pg_prefix/bin" echo "NIL_TEST_POSTGRES_SHARE=$nil_pg_share" echo "NIL_CI_PQ_INCLUDE=$nil_pg_include" - echo "DYLD_FALLBACK_LIBRARY_PATH=$nil_pg_library:/usr/local/lib:/usr/lib" + echo "NIL_TEST_DARWIN_RUNTIME_LIBRARY_PATH=$nil_pg_library:/usr/local/lib:/usr/lib" } >> "$GITHUB_ENV" "$nil_pg_prefix/bin/pg_config" --version --includedir --libdir --sharedir >> build/macos-sdk-evidence/providers.txt else @@ -131,9 +134,10 @@ jobs: echo 'PostgreSQL: coherent installed provider absent; actual CTest skips/failures retained' >> build/macos-sdk-evidence/providers.txt fi { - echo 'GTK: explicit macOS provider available in source; this SDK campaign does not provision or qualify desktop use' + echo 'AppKit: native macOS provider; this SDK campaign does not provision or qualify desktop use' echo 'Installed candidate checks: inherited DYLD/source/tool overrides removed; Apple toolchain PATH; system SQLite only' echo 'Private PostgreSQL installed checks use a separate explicitly declared provider environment' + echo 'SQL loader paths apply only to runtime children; JIT is a combined compilation/execution process' echo 'Homebrew LLVM dependencies remain external development prerequisites, not bundled portable libraries' } >> build/macos-sdk-evidence/providers.txt { @@ -211,6 +215,20 @@ jobs: ctest --test-dir build/ci --output-on-failure --parallel 2 --no-tests=error --timeout 360 \ --test-output-size-passed 16384 --test-output-size-failed 65536 \ --output-junit ctest-results.xml 2>&1 | tee build/macos-sdk-evidence/ctest.log + - name: Upload full CTest evidence immediately + if: always() + timeout-minutes: 3 + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: macos-sdk-ctest-${{ matrix.host.arch }}-${{ github.sha }}-${{ github.run_attempt }} + path: | + build/macos-sdk-evidence/registration.json + build/macos-sdk-evidence/capability-registration.json + build/macos-sdk-evidence/sdk-build-evidence.json + build/macos-sdk-evidence/ctest.log + build/ci/ctest-results.xml + if-no-files-found: warn + retention-days: 14 - name: Install isolated development stage id: install # A complete failed suite is still recorded, and still fails this job. diff --git a/.github/workflows/web-runtime-parity.yml b/.github/workflows/web-runtime-parity.yml new file mode 100644 index 00000000..fd2b4f6e --- /dev/null +++ b/.github/workflows/web-runtime-parity.yml @@ -0,0 +1,174 @@ +name: Web runtime platform checks + +"on": + pull_request: + paths: + - '.github/workflows/web-runtime-parity.yml' + - 'CMakeLists.txt' + - 'nilcompiler/**' + - 'nilstd/**' + - 'tests/net_*' + - 'tests/file_time_support_tests.cpp' + - 'tests/tools/net_socket_contract.py' + - 'tests/tools/file_modified_at_contract.py' + - 'tests/tools/formatted_text_contract.py' + - 'tests/tools/markup_contract.py' + - 'tests/language/formatted-text*' + - 'tests/language/markup*' + - 'tests/routes/ny_route_matrix.py' + - 'tests/matrix/**' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: web-runtime-parity-${{ github.ref }} + cancel-in-progress: true + +# These are real host/interpreter checks for the new web-runtime APIs. LLVM is +# disabled; success does not qualify native socket support or an installed SDK. +jobs: + runtime: + name: ${{ matrix.host.name }} interpreter runtime + runs-on: ${{ matrix.host.runner }} + timeout-minutes: 60 + strategy: + fail-fast: false + max-parallel: 4 + matrix: + host: + - {name: Linux x86_64, runner: ubuntu-24.04, python: python3} + - {name: Windows x86_64, runner: windows-2022, python: python} + - {name: macOS x86_64, runner: macos-15-intel, python: python3} + - {name: macOS arm64, runner: macos-15, python: python3} + defaults: + run: + shell: bash + env: + PYTHONDONTWRITEBYTECODE: '1' + PYTHONUTF8: '1' + HOMEBREW_NO_AUTO_UPDATE: '1' + NIL_CI_PYTHON: ${{ matrix.host.python }} + steps: + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + with: + persist-credentials: false + - name: Record source and host + timeout-minutes: 2 + run: | + "$NIL_CI_PYTHON" - <<'PY' + import json, os, platform, subprocess, sys + from pathlib import Path + root = Path('build/web-parity-evidence') + root.mkdir(parents=True) + revision = subprocess.check_output(['git', 'rev-parse', 'HEAD'], text=True, timeout=15).strip() + assert revision == os.environ['GITHUB_SHA'] + report = dict(revision=revision, system=platform.system(), machine=platform.machine(), + python=sys.executable, python_version=sys.version, + llvm=False, scope='host interpreter runtime; no SDK or native socket qualification') + (root/'identity.json').write_text(json.dumps(report, indent=2)+'\n') + print(json.dumps(report)) + PY + - name: Configure focused Release build + timeout-minutes: 8 + run: | + set -o pipefail + nil_python_executable="$("$NIL_CI_PYTHON" -c 'import sys; print(sys.executable)')" + cmake -S . -B build/web-parity \ + -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DNIL_ENABLE_LLVM=OFF \ + "-DPython3_EXECUTABLE=$nil_python_executable" \ + -DNILCOMPILER_SQLITE_INCLUDE_DIR=OFF -DNILCOMPILER_POSTGRESQL_INCLUDE_DIR=OFF \ + 2>&1 | tee build/web-parity-evidence/configure.log + - name: Build compiler and focused fixtures + timeout-minutes: 40 + run: | + set -o pipefail + cmake --build build/web-parity --config Release --parallel 2 \ + --target nilcompiler nilinterpreter nil_net_support_tests \ + nil_net_lifecycle_tests nil_file_time_support_tests \ + 2>&1 | tee build/web-parity-evidence/build.log + - name: Run real peer and language contracts + timeout-minutes: 8 + run: | + "$NIL_CI_PYTHON" - <<'PY' + import json, subprocess + from pathlib import Path + pattern = r'^(net[.](support-contract|lifecycle-contract|socket-contract)|nil[.](file-time-support|file-modified-at-contract|formatted-text-contract|markup-contract)|matrix[.]execution)$' + base = ['ctest', '--test-dir', 'build/web-parity', '-C', 'Release', '-R', pattern] + root = Path('build/web-parity-evidence').resolve() + inventory = subprocess.check_output(base+['--show-only=json-v1'], timeout=30) + (root/'inventory.json').write_bytes(inventory) + expected = {'net.support-contract', 'net.lifecycle-contract', 'net.socket-contract', + 'nil.file-time-support', 'nil.file-modified-at-contract', 'matrix.execution', + 'nil.formatted-text-contract', 'nil.markup-contract'} + assert {test['name'] for test in json.loads(inventory)['tests']} == expected + with (root/'ctest.log').open('wb') as output: + result = subprocess.run(base+['--no-tests=error', '--output-on-failure', + '--test-output-size-passed', '16384', '--test-output-size-failed', '65536', + '--output-junit', str(root/'results.xml')], stdout=output, stderr=subprocess.STDOUT, timeout=450) + print((root/'ctest.log').read_text(errors='replace')) + result.check_returncode() + import xml.etree.ElementTree as ET + cases = list(ET.parse(root/'results.xml').getroot().iter('testcase')) + assert len(cases) == len(expected) and {case.get('name') for case in cases} == expected + assert all(case.get('status') == 'run' and case.find('skipped') is None and case.find('failure') is None and + case.find('error') is None for case in cases), 'Unexpected failure or skipped contract' + PY + - name: Run formatted text and markup on every interpreted artifact route + timeout-minutes: 6 + run: | + "$NIL_CI_PYTHON" - <<'PYTHON' + import json, subprocess, sys + from pathlib import Path + root = Path('build/web-parity-evidence').resolve() + tests = json.loads((root/'inventory.json').read_text())['tests'] + command = next(test['command'] for test in tests if test['name'] == 'nil.markup-contract') + compiler = command[command.index('--compiler')+1] + interpreter = command[command.index('--interpreter')+1] + routes = ['text-interpreter', 'binary-interpreter', 'bundle-text', 'bundle-binary'] + for fixture in ['formatted-text', 'formatted-text-matrix', 'markup', 'markup-matrix']: + language = 'matrix' if fixture.endswith('-matrix') else 'ny' + report = root/(fixture+'-routes.json') + subprocess.run([sys.executable, 'tests/routes/ny_route_matrix.py', + '--compiler', compiler, '--interpreter', interpreter, + '--source', f'tests/language/{fixture}.{language}', + '--expected-stdout', f'tests/language/{fixture}.stdout', '--expected-exit', '0', + '--routes', ','.join(routes), '--strict', '--compiler-arg=--sdk-path', + '--compiler-arg='+str(Path('nilstd/src').resolve()), + '--work-dir', 'build/web-parity/language-routes', '--report-json', str(report)], + check=True, timeout=75) + data = json.loads(report.read_text()) + assert len(data['results']) == 4 and all(item['status'] == 'PASS' for item in data['results']) + PYTHON + - name: Preserve source identity and evidence + if: always() + timeout-minutes: 2 + run: | + "$NIL_CI_PYTHON" - <<'PY' + import hashlib, json, os, shutil, subprocess + from pathlib import Path + root = Path('build/web-parity-evidence') + root.mkdir(parents=True, exist_ok=True) + contracts = Path('build/web-parity/test-evidence') + if contracts.is_dir(): + shutil.copytree(contracts, root/'contracts') + dirty = subprocess.check_output(['git', 'status', '--porcelain', '--untracked-files=no'], text=True, timeout=15) + revision = subprocess.check_output(['git', 'rev-parse', 'HEAD'], text=True, timeout=15).strip() + files = {} + for path in sorted(root.rglob('*')): + if path.is_file() and not path.is_symlink(): + data = path.read_bytes() + files[path.relative_to(root).as_posix()] = dict(bytes=len(data), sha256=hashlib.sha256(data).hexdigest()) + (root/'evidence.json').write_text(json.dumps(dict(revision=revision, + source_unchanged=not dirty, files=files), indent=2)+'\n') + assert not dirty and revision == os.environ['GITHUB_SHA'] + PY + - name: Upload complete or partial results + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: web-runtime-${{ matrix.host.runner }}-${{ github.sha }}-${{ github.run_attempt }} + path: build/web-parity-evidence/ + if-no-files-found: error + retention-days: 14 diff --git a/CMakeLists.txt b/CMakeLists.txt index e69001db..78f23a6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,6 +101,33 @@ install( install(FILES LICENSE DESTINATION ".") if(BUILD_TESTING) + foreach(nil_hook_profile IN ITEMS default override) + add_executable(nil_freestanding_hook_${nil_hook_profile} tests/freestanding_hook_contract.cpp) + target_compile_features(nil_freestanding_hook_${nil_hook_profile} PRIVATE cxx_std_17) + target_link_libraries(nil_freestanding_hook_${nil_hook_profile} PRIVATE nilruntime_freestanding) + if(nil_hook_profile STREQUAL "override") + target_compile_definitions(nil_freestanding_hook_${nil_hook_profile} PRIVATE NIL_TEST_HOST_OVERRIDE=1) + endif() + if(UNIX AND NOT APPLE) + target_link_options(nil_freestanding_hook_${nil_hook_profile} PRIVATE -no-pie) + endif() + add_test(NAME nil.freestanding-hook-${nil_hook_profile} COMMAND $) + set_tests_properties(nil.freestanding-hook-${nil_hook_profile} PROPERTIES LABELS "runtime;rc" TIMEOUT 15) + endforeach() + if(WIN32) + add_executable(nil_atomic_file_replace_tests tests/atomic_file_replace_tests.cpp) + target_compile_features(nil_atomic_file_replace_tests PRIVATE cxx_std_17) + target_link_libraries(nil_atomic_file_replace_tests PRIVATE advapi32) + add_test(NAME nil.atomic-file-replace COMMAND $) + set_tests_properties(nil.atomic-file-replace PROPERTIES LABELS "tools;windows;publication;rc" TIMEOUT 15) + # Keep the public backend declarations usable after real Windows RPC + # headers, even in an LLVM-OFF build that normally hides this interface. + add_library(nil_native_windows_header_contract OBJECT tests/native_windows_header_contract.cpp) + target_compile_features(nil_native_windows_header_contract PRIVATE cxx_std_17) + target_compile_definitions(nil_native_windows_header_contract PRIVATE NY_HAS_LLVM=1) + target_include_directories(nil_native_windows_header_contract PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/nilcompiler/src") + add_dependencies(nilcompiler nil_native_windows_header_contract) + endif() add_executable(nil_compiler_module_fingerprint_tests tests/compiler_module_fingerprint_tests.cpp) target_compile_features(nil_compiler_module_fingerprint_tests PRIVATE cxx_std_17) if(WIN32) @@ -108,6 +135,32 @@ if(BUILD_TESTING) endif() add_test(NAME nilbuild.compiler-module-fingerprints COMMAND $) set_tests_properties(nilbuild.compiler-module-fingerprints PROPERTIES LABELS "tools;project;rc" TIMEOUT 30) + add_executable(nil_compiler_module_darwin_tests tests/compiler_module_darwin_tests.cpp) + target_compile_features(nil_compiler_module_darwin_tests PRIVATE cxx_std_17) + if(APPLE) + target_sources(nil_compiler_module_darwin_tests PRIVATE nilcompiler/src/compiler_module_fingerprint_darwin.cpp) + target_compile_options(nil_compiler_module_darwin_tests PRIVATE -fblocks) + endif() + add_test(NAME compiler.darwin-module-fingerprint COMMAND $) + set_tests_properties(compiler.darwin-module-fingerprint PROPERTIES LABELS "tools;project;rc" TIMEOUT 60) + if(WIN32) + target_link_libraries(nil_compiler_module_darwin_tests PRIVATE advapi32) + endif() + add_executable(nil_darwin_toolchain_inputs_tests tests/darwin_toolchain_inputs_tests.cpp) + target_compile_features(nil_darwin_toolchain_inputs_tests PRIVATE cxx_std_17) + if(WIN32) + target_link_libraries(nil_darwin_toolchain_inputs_tests PRIVATE advapi32) + endif() + add_test(NAME nil.darwin-toolchain-inputs COMMAND $) + set_tests_properties(nil.darwin-toolchain-inputs PROPERTIES LABELS "tools;project;rc" TIMEOUT 60) + if(APPLE) + add_executable(nil_darwin_native_inputs_tests tests/darwin_native_inputs_tests.cpp) + target_compile_features(nil_darwin_native_inputs_tests PRIVATE cxx_std_17) + add_dependencies(nil_darwin_native_inputs_tests nilruntime) + add_test(NAME nil.darwin-native-inputs COMMAND $ + "${NIL_NATIVE_LINKER_DRIVER}" "$") + set_tests_properties(nil.darwin-native-inputs PROPERTIES LABELS "tools;project;rc" TIMEOUT 180) + endif() # Test-only launcher for isolated editor integration runs. Reuse the # toolchain's bounded process/Job cleanup; never ship it in the SDK. @@ -237,6 +290,29 @@ if(BUILD_TESTING) set_tests_properties(nil.owned-contract PROPERTIES LABELS "nil;ownership;rc" TIMEOUT 30) find_package(Threads REQUIRED) + add_executable(nil_net_support_tests tests/net_support_tests.cpp) + target_compile_features(nil_net_support_tests PRIVATE cxx_std_17) + target_include_directories(nil_net_support_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/nilcompiler/src") + target_link_libraries(nil_net_support_tests PRIVATE Threads::Threads) + if(WIN32) + target_link_libraries(nil_net_support_tests PRIVATE ws2_32) + endif() + add_test(NAME net.support-contract COMMAND $) + set_tests_properties(net.support-contract PROPERTIES LABELS "net;runtime;rc" TIMEOUT 60) + + add_executable(nil_net_lifecycle_tests tests/net_lifecycle_tests.cpp) + target_compile_features(nil_net_lifecycle_tests PRIVATE cxx_std_17) + target_include_directories(nil_net_lifecycle_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/nilcompiler/src") + target_link_libraries(nil_net_lifecycle_tests PRIVATE nilcompiler_core) + add_test(NAME net.lifecycle-contract COMMAND $) + set_tests_properties(net.lifecycle-contract PROPERTIES LABELS "net;runtime;vm;rc" TIMEOUT 60) + + add_executable(nil_file_time_support_tests tests/file_time_support_tests.cpp) + target_compile_features(nil_file_time_support_tests PRIVATE cxx_std_17) + target_include_directories(nil_file_time_support_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/nilcompiler/src") + add_test(NAME nil.file-time-support COMMAND $) + set_tests_properties(nil.file-time-support PROPERTIES LABELS "io;runtime;rc" TIMEOUT 30) + add_executable(ny_database_runtime_tests tests/database_runtime_tests.cpp) target_link_libraries(ny_database_runtime_tests PRIVATE nilruntime) target_include_directories(ny_database_runtime_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/nilcompiler/src") @@ -250,6 +326,12 @@ if(BUILD_TESTING) target_compile_features(nil_database_postgresql_tests PRIVATE cxx_std_17) add_test(NAME nil.database-postgresql-client COMMAND $) set_tests_properties(nil.database-postgresql-client PROPERTIES LABELS "database;runtime" SKIP_RETURN_CODE 77 TIMEOUT 30) + if(APPLE AND NOT "$ENV{NIL_TEST_DARWIN_RUNTIME_LIBRARY_PATH}" STREQUAL "") + # The optional provider path belongs to this executing runtime only. + # Keep compiler/build processes free of test-only dyld selection inputs. + set_tests_properties(nil.database-postgresql-client PROPERTIES + ENVIRONMENT "DYLD_FALLBACK_LIBRARY_PATH=$ENV{NIL_TEST_DARWIN_RUNTIME_LIBRARY_PATH}") + endif() add_executable(nilagent_ipc_tests tests/nilagent_ipc_tests.cpp) target_link_libraries(nilagent_ipc_tests PRIVATE Threads::Threads) @@ -290,6 +372,17 @@ if(BUILD_TESTING) add_test(NAME nil.etp-codec COMMAND $) set_tests_properties(nil.etp-codec PROPERTIES LABELS "tools;ipc;etp" TIMEOUT 30) + add_executable(nil_ui_layout_tests tests/ui_layout_tests.cpp) + target_compile_features(nil_ui_layout_tests PRIVATE cxx_std_17) + add_test(NAME nil.ui-layout COMMAND $) + set_tests_properties(nil.ui-layout PROPERTIES LABELS "ui;layout;parity" TIMEOUT 30) + if(UNIX) + # Also built on Linux for portable executable-adapter controls. + # This fixture has no install/export rule. + add_executable(nil_ui_appkit_compile_driver tests/ui_appkit_compile_driver.cpp) + target_compile_features(nil_ui_appkit_compile_driver PRIVATE cxx_std_17) + endif() + # The provider fixture follows the provider: tests/ui_runtime_tests.cpp # inspects loaded GTK modules and symbol origins, so it describes the GTK # provider and cannot speak for Win32. @@ -305,6 +398,12 @@ if(BUILD_TESTING) target_include_directories(nil_ui_runtime_appkit_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/nilcompiler/src") target_compile_features(nil_ui_runtime_appkit_tests PRIVATE cxx_std_17) set(NIL_UI_PROVIDER_TEST nil_ui_runtime_appkit_tests) + # Test-only input into the owning process's AppKit responder chain. + # No install/export rule: SDKs never carry or load this fixture. + add_library(nil_ui_appkit_input_driver SHARED tests/ui_appkit_input_driver.mm) + target_compile_features(nil_ui_appkit_input_driver PRIVATE cxx_std_17) + target_compile_options(nil_ui_appkit_input_driver PRIVATE -fobjc-arc) + target_link_libraries(nil_ui_appkit_input_driver PRIVATE "-framework Cocoa") else() add_executable(nil_ui_runtime_tests tests/ui_runtime_tests.cpp) target_link_libraries(nil_ui_runtime_tests PRIVATE nilruntime Threads::Threads) @@ -626,6 +725,12 @@ if(BUILD_TESTING) # test suite when testing was explicitly enabled. find_package(Python3 3.8 REQUIRED COMPONENTS Interpreter) if(Python3_Interpreter_FOUND) + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + add_test(NAME nil.darwin-heap-accounting + COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/darwin_heap_accounting_contract.py" + --cc "${CMAKE_C_COMPILER}" --output-parent "${CMAKE_BINARY_DIR}/test-evidence") + set_tests_properties(nil.darwin-heap-accounting PROPERTIES LABELS "runtime;native;rc" TIMEOUT 120) + endif() add_test(NAME nilbuild.llvm-dia-configuration COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/llvm_dia_cmake_contract.py" --helper "${CMAKE_CURRENT_SOURCE_DIR}/cmake/NilLlvmDia.cmake" @@ -691,8 +796,26 @@ if(BUILD_TESTING) add_test(NAME nil.ui-windows-display-harness COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/test_windows_ui_display_contract.py") set_tests_properties(nil.ui-windows-display-harness PROPERTIES LABELS "ui;tools;windows" TIMEOUT 45) + set(NIL_UI_APPKIT_COMPILE_TEST_ARGS) + if(TARGET nil_ui_appkit_compile_driver) + set(NIL_UI_APPKIT_COMPILE_TEST_ARGS --compile-driver $) + if(TARGET nil_ui_appkit_input_driver) + list(APPEND NIL_UI_APPKIT_COMPILE_TEST_ARGS --input-driver $) + endif() + endif() + add_test(NAME nil.ui-appkit-display-harness + COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/test_ui_appkit_display_contract.py" + ${NIL_UI_APPKIT_COMPILE_TEST_ARGS}) + set_tests_properties(nil.ui-appkit-display-harness PROPERTIES LABELS "ui;tools;apple" TIMEOUT 30) set(NIL_UI_DISPLAY_CONTRACT "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/ui_display_contract.py") set(NIL_UI_DISPLAY_TIMEOUT 240) + set(NIL_UI_DISPLAY_APPKIT_ARGS) + if(APPLE) + set(NIL_UI_DISPLAY_TIMEOUT 420) + set(NIL_UI_DISPLAY_APPKIT_ARGS --appkit-input-driver $ + --appkit-compile-driver $ + --appkit-evidence-directory "${CMAKE_CURRENT_BINARY_DIR}/appkit-input-evidence") + endif() if(WIN32) set(NIL_UI_DISPLAY_CONTRACT "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/windows_ui_display_contract.py") set(NIL_UI_DISPLAY_TIMEOUT 660) @@ -707,7 +830,7 @@ if(BUILD_TESTING) --interaction-source "${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/gtk_interaction.matrix" --provider-test $ --lifecycle-test $ - --llvm "${NIL_ENABLE_LLVM}") + --llvm "${NIL_ENABLE_LLVM}" ${NIL_UI_DISPLAY_APPKIT_ARGS}) set_tests_properties(nil.ui-display PROPERTIES LABELS "ui;routes" SKIP_RETURN_CODE 77 TIMEOUT "${NIL_UI_DISPLAY_TIMEOUT}") add_test(NAME nil.ui-intrinsic-contract COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/ui_intrinsic_contract.py" @@ -727,9 +850,16 @@ if(BUILD_TESTING) # consumer has to supply every undefined symbol deliberately, so this # boundary widens only on purpose. if(NIL_ENABLE_LLVM) + find_program(NIL_TEST_LLVM_NM NAMES llvm-nm llvm-nm-18 + HINTS "${LLVM_DIR}/../../../bin" "${LLVM_DIR}/../bin") + set(NIL_SYMBOL_TOOL_ARGUMENTS) + if(NIL_TEST_LLVM_NM) + list(APPEND NIL_SYMBOL_TOOL_ARGUMENTS --llvm-nm "${NIL_TEST_LLVM_NM}") + endif() add_test(NAME nil.native-symbol-surface COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/native_symbol_surface.py" - --compiler $ --assembler $) + --compiler $ --assembler $ + ${NIL_SYMBOL_TOOL_ARGUMENTS}) set_tests_properties(nil.native-symbol-surface PROPERTIES LABELS "native;abi;rc" SKIP_RETURN_CODE 77 TIMEOUT 180) endif() @@ -777,8 +907,14 @@ if(BUILD_TESTING) --llvm "${NIL_ENABLE_LLVM}" --cxx "${CMAKE_C_COMPILER}" ) + set(NIL_PROJECT_CONTRACT_TIMEOUT 180) + if(APPLE) + # The mixed-language contract performs multiple independently + # fingerprinted VM/native builds; each retains its own deadline. + set(NIL_PROJECT_CONTRACT_TIMEOUT 900) + endif() set_tests_properties(nilbuild.project-contract PROPERTIES - LABELS "tools;project;analysis;rc" TIMEOUT 180) + LABELS "tools;project;analysis;rc" TIMEOUT "${NIL_PROJECT_CONTRACT_TIMEOUT}") if(UNIX) # exec identity, POSIX controlling terminals and FIFOs do not # exist on Windows. The common contract tests Windows' resident @@ -800,6 +936,11 @@ if(BUILD_TESTING) --cxx "${CMAKE_C_COMPILER}" --llvm "${NIL_ENABLE_LLVM}") set_tests_properties(nilbuild.incremental-contract PROPERTIES LABELS "tools;project;rc" TIMEOUT 240) + if(APPLE) + # Three real builds read complete dyld cache contents. The measured + # first Intel hash took 104 s; fake/mutation cases keep their limits. + set_tests_properties(nilbuild.incremental-contract PROPERTIES TIMEOUT 900) + endif() add_test(NAME nilbuild.compiler-identity-contract COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/incremental_build_contract.py" --builder $ --compiler $ @@ -816,6 +957,9 @@ if(BUILD_TESTING) --compiler $ --interpreter $ --stdlib "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src") set_tests_properties(nilbuild.tool-discovery-contract PROPERTIES LABELS "tools;project;rc" TIMEOUT 180) + if(APPLE) + set_tests_properties(nilbuild.tool-discovery-contract PROPERTIES TIMEOUT 600) + endif() add_test(NAME nil.platform.raw-byte-routes COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/raw_byte_routes_contract.py" --compiler $ --interpreter $ @@ -881,6 +1025,12 @@ if(BUILD_TESTING) COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/sdk_qualification_tests.py" ) set_tests_properties(ny.sdk-qualification PROPERTIES LABELS "sdk;rc" TIMEOUT 30) + # Inspect the actual runtime archive on every host, independently of + # LLVM availability. Unknown/malformed object members fail closed. + add_test(NAME nil.freestanding-archive-imports + COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/sdk_freestanding_imports_tests.py" + --archive $) + set_tests_properties(nil.freestanding-archive-imports PROPERTIES LABELS "sdk;native;abi;rc" TIMEOUT 120) add_test( NAME ny.release-artifacts COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/release_artifact_tests.py" @@ -943,6 +1093,20 @@ if(BUILD_TESTING) set(NIL_MEMORY_LIMIT_ARGUMENTS --memory-launcher $) add_test(NAME nil.native-memory-limit-contract COMMAND nil_native_memory_limiter --self-test) set_tests_properties(nil.native-memory-limit-contract PROPERTIES LABELS "native;memory;rc" TIMEOUT 60) + elseif(APPLE) + # A requested-allocation retention oracle for these exact native + # fixtures. This is distinct from Linux AS and Windows commit caps. + add_library(nil_darwin_native_heap_budget SHARED tests/darwin_native_heap_budget.c) + add_executable(nil_darwin_native_heap_budget_control tests/darwin_native_heap_budget_control.c) + foreach(nil_heap_target IN ITEMS nil_darwin_native_heap_budget nil_darwin_native_heap_budget_control) + target_compile_features(${nil_heap_target} PRIVATE c_std_11) + target_compile_options(${nil_heap_target} PRIVATE -fno-builtin -Wall -Wextra -Werror -mmacosx-version-min=14.0) + target_link_options(${nil_heap_target} PRIVATE -mmacosx-version-min=14.0) + endforeach() + set(NIL_MEMORY_LIMIT_ARGUMENTS + --heap-budget-library $ + --heap-budget-control $ + --output-parent "${CMAKE_BINARY_DIR}/test-evidence") endif() add_test( NAME ny.native-managed-lifetime @@ -962,6 +1126,12 @@ if(BUILD_TESTING) --compiler $ --llvm "${NIL_ENABLE_LLVM}" ${NIL_MEMORY_LIMIT_ARGUMENTS}) set_tests_properties(ny.native-owned-lifetime PROPERTIES LABELS "native;ownership;rc" SKIP_RETURN_CODE 77 TIMEOUT 240) + if(APPLE) + # Includes independent allocator controls and a second execution of + # each unchanged native fixture with real frees deliberately withheld. + set_tests_properties(ny.native-managed-lifetime PROPERTIES TIMEOUT 240) + set_tests_properties(ny.native-owned-lifetime PROPERTIES TIMEOUT 360) + endif() # Matrix lexer conformance: the sample corpus must tokenise clean, and # each negative fixture must be rejected for the stated reason. add_executable(matrix_lex_dump tests/matrix/lex_dump.cpp) @@ -1023,6 +1193,29 @@ if(BUILD_TESTING) --interpreter $ --root "${CMAKE_CURRENT_SOURCE_DIR}") set_tests_properties(matrix.samples PROPERTIES LABELS "matrix;routes" TIMEOUT 180) + # The network intrinsics, against a real peer. A socket cannot be + # checked by comparing stdout, because the interesting behaviour is + # what the other end of the connection sees: the test runs a Matrix + # page as a process and plays the other side from Python. It covers + # the cases a simpler fixture passes without exercising -- an opaque + # handle refusing a number it never issued, a leaked listener whose + # port the runtime still has to release, a read deadline expiring, and + # a peer that vanishes mid-write being reported rather than killing + # the process. + add_test(NAME net.socket-contract + COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/net_socket_contract.py" + --compiler $ + --interpreter $ + --root "${CMAKE_CURRENT_SOURCE_DIR}") + set_tests_properties(net.socket-contract PROPERTIES LABELS "net;routes;rc" TIMEOUT 300) + add_test(NAME nil.file-modified-at-contract + COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/file_modified_at_contract.py" + --compiler $ + --interpreter $ + --nilstd "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src" + --llvm "${NIL_ENABLE_LLVM}" + --output-parent "${CMAKE_BINARY_DIR}/test-evidence") + set_tests_properties(nil.file-modified-at-contract PROPERTIES LABELS "io;routes;rc" TIMEOUT 180) add_test(NAME nillsp.semantic-protocol COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/lsp_semantic_protocol.py" --lsp $) @@ -1587,6 +1780,9 @@ if(BUILD_TESTING) --compiler $ --interpreter $ --stdlib "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src" --llvm "${NIL_ENABLE_LLVM}") set_tests_properties(nil.reflection-consumer PROPERTIES LABELS "analysis;reflection;routes" TIMEOUT 120) + add_test(NAME nil.runtime-provider-environment + COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_runtime_provider_environment.py") + set_tests_properties(nil.runtime-provider-environment PROPERTIES LABELS "database;tools;routes" TIMEOUT 30) add_test(NAME nil.database-postgresql-fixture COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_database_postgresql_isolated.py") set_tests_properties(nil.database-postgresql-fixture PROPERTIES LABELS "database;tools" TIMEOUT 30) @@ -2954,6 +3150,7 @@ if(BUILD_TESTING) bugpass-constructor-context bugpass-call-context collected-queries collected-queries-matrix + markup markup-matrix signed-numbers-matrix integer-literal-widths integer-literal-widths-matrix checked-text-spans checked-text-spans-matrix @@ -3050,6 +3247,12 @@ if(BUILD_TESTING) --compiler $ --llvm "${NIL_ENABLE_LLVM}") set_tests_properties(nil.formatted-text-contract PROPERTIES LABELS "ny;matrix;analysis;interpolation" TIMEOUT 120) + add_test(NAME nil.markup-contract + COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/markup_contract.py" + --compiler $ --interpreter $ + --stdlib "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src" --llvm "${NIL_ENABLE_LLVM}") + set_tests_properties(nil.markup-contract PROPERTIES + LABELS "matrix;markup;analysis;native;interpreter;rc" TIMEOUT 300) if(TARGET nilassembler AND TARGET nillinker) add_test(NAME nil.formatted-nil-contract COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/formatted_nil_contract.py" diff --git a/README.md b/README.md index a0cf6f16..7ee02c9e 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,10 @@ The framework includes: typed faults, and bounded parallel execution. - **Shared language features.** Structural tuples provide typed multiple results and local destructuring. Explicit string interpolation supports - expressions with consistent scalar formatting across execution routes. + expressions with consistent scalar formatting across execution routes, including + multiline blocks. [Structured markup](docs/standard-library/markup.md) keeps + generated HTML and its conditionals in one checked document, with context-specific + escaping and document composition in both Ny and Matrix. [Collected queries](docs/standard-library/queries.md) compose filtering, stable ordering and projection. [Text patterns](docs/standard-library/text-patterns.md) offer readable declarations, bounded regex matching with named captures, @@ -108,10 +111,11 @@ The framework includes: [testing guide](docs/tools/testing.md). - **Desktop applications.** The Matrix-authored [`nil.std.ui`](docs/standard-library/ui.md) provides windows, layout containers, - standard controls, typed events, and explicit model synchronization. Its GTK 4 - provider supports VM and native execution on Linux and Windows development - builds. GTK is an external runtime dependency; Windows requires an interactive, - visible desktop. This does not extend Windows release qualification. + standard controls, typed events, and explicit model synchronization. VM and + native desktop checks pass with GTK4 on Linux, Win32 on Windows and AppKit on + Intel and Apple Silicon macOS. Linux requires an external GTK4 runtime; + desktop tests require an interactive session. Installed SDK and release + qualification remain separate from these development checks. - **Aperture runtime diagnostics.** Memory profiles record runtime allocations, frees, retained storage, peaks and source locations for VM and native programs. Saved JSON reports support inspection and automated analysis. See @@ -245,23 +249,30 @@ such as `say formatted "value={5}"`; it works on interpreted and native routes. ## Project status -The current version is **1.0.0-rc.1**. Linux x86_64 is the reference development -platform. The Windows x86_64 development SDK has -[paired Linux acceptance and installed tooling coverage](notebook/2026-11-02-windows-port.md). -Intel and Apple Silicon macOS have passing native build baselines with LLVM -enabled and disabled, real interpreter/checkpoint execution, and representative -LLVM JIT/AOT execution. Both macOS architectures also pass bounded native inline -assembly and executable-context embedding, inspection and dead-strip retention -checks. Both macOS development SDKs also pass the full installed -standard-library campaign, isolated PostgreSQL application routes, installed -debugger checks and execution checkpoints. Both hosts now pass the underlying -AppKit window/event checks, including directed keyboard input and native closure -of a verified child window. The GTK port and desktop acceptance remain in progress. -Incremental reuse and native memory-limit tests also remain open, so full platform -parity is not yet established. -The [platform support matrix](docs/platform-support.md) separates verified -development behavior from remaining SDK, provider and release work. -Windows and macOS release qualification remains pending. The +The current version is **1.0.0-rc.1**. Development coverage spans Linux x86_64, +Windows x86_64, Intel macOS and Apple Silicon macOS. Relocated development SDKs +on all four hosts pass 1,184 installed standard-library routes, twelve PostgreSQL +source/binary routes, 146 debugger checks and same-process execution checkpoints. +Linux's current SDK also passes all 590 source tests, twenty installed GTK +desktop/input cases and all 88 real VS Code host groups without exclusions. + +Windows uses Win32 and macOS uses AppKit. Windows and both Mac architectures +also pass twenty installed desktop/input cases, four desktop debugger sessions +and all 88 editor groups. Both Macs pass the expanded application campaign and +native incremental reuse. An earlier Intel installed-editor timeout passed a +diagnostic rerun within the unchanged deadline; its cause remains unconfirmed +and the failed report is retained. A separate Windows account exposed a +packaged-library permission defect; the correction passes real cross-account +access tests. + +The newer socket, entropy and text-byte APIs pass the same interpreter contracts +on every host; these APIs currently require the interpreter on all platforms. +File timestamps also pass textual/binary NIL, LLVM JIT and AOT checks everywhere. +The [platform support matrix](docs/platform-support.md) records exact SDK/source +revisions, retained failures and remaining deployment boundaries. + +These are development results. Windows and macOS release qualification remains +pending. The [release profile](docs/release/v1-profile.md) defines supported configurations and release requirements. Known limitations and planned improvements are tracked in the [development findings](matrix/docs/next-version-findings.md). diff --git a/common/atomic_file_replace.hpp b/common/atomic_file_replace.hpp new file mode 100644 index 00000000..e65db249 --- /dev/null +++ b/common/atomic_file_replace.hpp @@ -0,0 +1,48 @@ +#ifndef NIL_COMMON_ATOMIC_FILE_REPLACE_HPP +#define NIL_COMMON_ATOMIC_FILE_REPLACE_HPP + +#include +#include + +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif + +namespace nil::detail { + +inline bool replace_output_file(const std::filesystem::path &staging, + const std::filesystem::path &output, + std::error_code &error) { +#ifdef _WIN32 + // A recently executed image or a reader without delete sharing can briefly + // prevent replacement. Retain the compiler's bounded retry policy for every + // executable publisher. Persistent locks and access failures still fail; + // never unlink the old output or relax its permissions to make progress. + for (int attempt = 0;; ++attempt) { + if (MoveFileExW(staging.c_str(), output.c_str(), + MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) { + error.clear(); + return true; + } + const DWORD code = GetLastError(); + const bool transient = code == ERROR_ACCESS_DENIED || + code == ERROR_SHARING_VIOLATION || + code == ERROR_LOCK_VIOLATION; + if (!transient || attempt >= 50) { + error = std::error_code(static_cast(code), std::system_category()); + return false; + } + Sleep(20); + } +#else + std::filesystem::rename(staging, output, error); + return !error; +#endif +} + +} // namespace nil::detail + +#endif diff --git a/common/content_digest.hpp b/common/content_digest.hpp index 7bcb6a5b..03cc87ec 100644 --- a/common/content_digest.hpp +++ b/common/content_digest.hpp @@ -2,6 +2,7 @@ // SHA-256 content identities for build inputs. Timestamps are not cache keys. #include +#include #include #include #include @@ -9,9 +10,13 @@ #include #include #include +#include +#if defined(__APPLE__) +#include +#endif namespace nil::detail { -class sha256 { +class portable_sha256 { std::array state{0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a, 0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19}; std::array block{}; @@ -58,14 +63,49 @@ class sha256 { return out.str(); } }; +#if defined(__APPLE__) +// The identity is still ordinary SHA-256 over every byte. Apple's implementation +// avoids spending minutes in the scalar fallback on the dyld backing files. +class sha256 { + CC_SHA256_CTX state{}; +public: + sha256() { + if (CC_SHA256_Init(&state) != 1) throw std::runtime_error("cannot initialize SHA-256"); + } + void update(const char *data, std::size_t length) { + while (length) { + const auto count = static_cast(std::min(length, std::numeric_limits::max())); + if (CC_SHA256_Update(&state, data, count) != 1) throw std::runtime_error("cannot update SHA-256"); + data += count; + length -= count; + } + } + std::string finish() { + unsigned char bytes[CC_SHA256_DIGEST_LENGTH]; + if (CC_SHA256_Final(bytes, &state) != 1) throw std::runtime_error("cannot finish SHA-256"); + constexpr char digits[] = "0123456789abcdef"; + std::string result; + result.reserve(sizeof(bytes) * 2); + for (auto byte : bytes) { result += digits[byte >> 4]; result += digits[byte & 15]; } + return result; + } +}; +#else +using sha256 = portable_sha256; +#endif inline std::string content_digest(const std::string &text) { sha256 hash; hash.update(text.data(),text.size());return hash.finish(); } -inline std::string file_digest(const std::filesystem::path &path) { +template inline std::string file_digest(const std::filesystem::path &path, Check check) { + check(); if(!std::filesystem::is_regular_file(path)) throw std::runtime_error("expected a regular build input: "+path.string()); std::ifstream input(path,std::ios::binary); if(!input) throw std::runtime_error("cannot read build input: "+path.string()); sha256 hash; char buffer[65536]; - while(input.read(buffer,sizeof(buffer))||input.gcount()) hash.update(buffer,static_cast(input.gcount())); + while(input.read(buffer,sizeof(buffer))||input.gcount()) { + check(); + hash.update(buffer,static_cast(input.gcount())); + } if(input.bad()) throw std::runtime_error("cannot read build input: "+path.string()); return hash.finish(); } +inline std::string file_digest(const std::filesystem::path &path) { return file_digest(path, [] {}); } } // namespace nil::detail diff --git a/docs/analysis-api.md b/docs/analysis-api.md index a9ad40e3..ee6e048f 100644 --- a/docs/analysis-api.md +++ b/docs/analysis-api.md @@ -177,8 +177,8 @@ Analysis reports also expose metadata for incremental build clients: | --- | --- | | `dependencies` | Sorted, unique `{path, digest}` entries for consumed native files. `path` is canonical and absolute; `digest` is lowercase hexadecimal SHA-256 of the exact consumed bytes. | | `dependenciesComplete` | True only after successful disk-based analysis with a complete, consistent dependency list containing the primary source. | -| `compilerFiles` | Sorted compiler toolchain file paths on Linux and Windows, including loaded shared libraries and known optional code inputs. Paths require content hashing; a partial list does not certify completeness. | -| `compilerFilesComplete` | Explicit boolean certification of the compiler file inventory, independent of source dependency completeness. Missing or false disables build reuse. Darwin currently reports false because loaded-image/shared-cache identity is incomplete. | +| `compilerFiles` | Sorted compiler toolchain file paths on Linux, Windows and macOS, including loaded shared libraries and known optional code inputs. Darwin includes the complete backing files for admitted dyld shared-cache images. Paths require content hashing; a partial list does not certify completeness. | +| `compilerFilesComplete` | Explicit boolean certification of the compiler file inventory, independent of source dependency completeness. Missing or false disables build reuse. Darwin certifies supported loaded-image/shared-cache profiles; unknown or unstable observations remain incomplete. | | `nativeRuntime` | Resolved native runtime archive path, or null when unavailable. | | `nativeLinker` | Resolved configured native linker driver path, or null when unavailable. | diff --git a/docs/design/NIL-0022-incremental-project-builds.md b/docs/design/NIL-0022-incremental-project-builds.md index 10d741f5..5c74ccc8 100644 --- a/docs/design/NIL-0022-incremental-project-builds.md +++ b/docs/design/NIL-0022-incremental-project-builds.md @@ -38,9 +38,11 @@ driver are identified separately for native toolchain fingerprints. `compilerFilesComplete` separately certifies the observed compiler loaded-file inventory. Missing or false certification disables reuse without discarding valid source dependency hashes or their output-collision and change protection. -Darwin currently leaves this flag false until loaded-image/shared-cache identity -is implemented. Its native link-input discovery is also incomplete. Successful -uncached builds do not establish incremental parity. +Darwin certifies supported loaded-image/shared-cache profiles, including full +backing-file content hashes. Its native link-input collector also covers the +admitted host toolchain and SDK dependencies. Real VM/native reuse and +invalidation pass on both Mac architectures; unsupported or unstable profiles +remain uncached. See [platform support](../platform-support.md) for evidence. The driver uses those inputs, effective project settings, tool identity and artifact content to decide whether compilation and linking can be skipped. diff --git a/docs/design/NIL-0025-matrix-desktop-ui.md b/docs/design/NIL-0025-matrix-desktop-ui.md index d84ae3b2..749bc294 100644 --- a/docs/design/NIL-0025-matrix-desktop-ui.md +++ b/docs/design/NIL-0025-matrix-desktop-ui.md @@ -1,9 +1,10 @@ # NIL-0025: Matrix desktop UI -- **Status:** Implemented Linux and Windows development GTK 4 profiles; macOS provider remains open +- **Status:** Implemented GTK4 on Linux, Win32 on Windows and AppKit on Intel/Apple Silicon macOS; release qualification remains separate -The original Linux design below is retained, followed by its Windows development -addendum. Current platform evidence and release limits are summarized in +The original Linux design below and dated Windows GTK addendum are retained as +design history. The current provider addendum supersedes that Windows deployment. +Current platform evidence and release limits are summarized in [platform support](../platform-support.md). ## Purpose @@ -94,3 +95,21 @@ evidence, not Windows release, bare-host or broader accessibility qualification. See the [current UI deployment and test contract](../standard-library/ui.md) and [Windows validation notebook](../../notebook/2026-11-02-windows-port.md) for the supported environment, evidence and remaining boundaries. + +## Current native providers — 2026-11-11 + +The checked `nil.std.ui` API now uses GTK4 only on Linux. Windows uses Win32 +directly, with no external GTK runtime or DLL-directory setting. macOS uses +AppKit/Cocoa and CoreGraphics, with creation and event processing on the process +main thread. The debugger keeps VM execution and UI cleanup on that same thread. +Win32 and AppKit implement row/column/grid layout behind the common boundary, +including hidden-child reflow, grid spans, expansion and fill. + +Both installed library roots pass ten interpreter/native desktop and input +cases on Windows and each Mac architecture. Tests use verified owned windows +on Windows and in-process AppKit responders on macOS. These checks supplement +the provider/lifecycle tests; they do not establish arbitrary desktop automation, +accessibility support, bare-host installation or release qualification. +See the [UI contract](../standard-library/ui.md) and +[platform matrix](../platform-support.md) for the current deployment requirements +and separately recorded editor acceptance. diff --git a/docs/design/NIL-0027-structured-markup.md b/docs/design/NIL-0027-structured-markup.md new file mode 100644 index 00000000..4f00abc5 --- /dev/null +++ b/docs/design/NIL-0027-structured-markup.md @@ -0,0 +1,407 @@ +# NIL-0027: Multiline formatted text and structured markup literals + +- **Status:** The initial §2 and §3 profile is implemented for both surfaces + on 2026-11-13. The current contract below and §3.6 supersede the original + runtime-tree, optional-hole and dialect-renderer proposals. +- **Scope:** Ny and Matrix syntax, shared hole lexing and AST, checking, + lowering, `nil.std.markup`, text builder +- **Date:** 2026-11-13 + +## Context + +A program that generates a document today concatenates one. The shape recurs +in every generator written against this framework: + +```text +value nav is when signed-in + give "Dashboard | Projects | Logout" + otherwise give "Login | Register" +end when +value html is "" plus title plus "" +give html plus "" +``` + +Three separate defects, and only the first one is about typing: + +1. **The document is not editable as a document.** Its structure is spread + across bindings and `plus` chains, so a change to the markup is a change to + control flow. This is the failure mode of a generation of server-rendered + business applications, where one function is a ladder of conditionals each + writing fragments of tags. +2. **Nothing checks the structure.** `"
" plus x` cannot be known to be + balanced, so an unclosed element is discovered by a browser. +3. **Escaping is manual and context-blind.** The correct escape for a value + differs between a text node, an attribute value and a URL attribute. A + concatenated document applies none of them, and an interpolated string + cannot apply the right one either, because it does not know where the hole + landed. + +NIL-0014 introduced explicit holes but confined a formatted literal to one +physical source line. Raw `begin text` blocks are multiline but deliberately +inert. Neither carries structure. + +NIL-0016 is the precedent for the answer. A text pattern is not a string that +happens to be a regular expression, and a query is not a loop the author wrote +out; both are declared in the language's own words and lower to a checked +value. **A document deserves the same treatment,** and this record is the third +member of that family. + +## 1. One hole grammar, three levels of structure + +| Surface | Holes | Structure | Status | +| --- | --- | --- | --- | +| `formatted "..."` | yes | none, one line | NIL-0014, implemented | +| `formatted begin text ... end text` / `$"""` | yes | none, multiline | §2, implemented | +| `begin markup ... end markup` / `$markup` | yes | elements, attributes, control flow | §3, implemented | + +**Current implementation contract.** A markup literal is checked as a document +at compile time, then evaluates to `nil.std.markup.document`, a record of +serialized text. Static markup stays literal; holes and directives assemble +the result at run time. `render` returns that text. There is no runtime DOM, +XML mode, compact renderer or automatic attribute omission. Ordinary holes +accept text and the formatted-text scalar types; a document splices only into +element content. Arrays and optional values need an explicit loop or +conversion. The original tree and renderer proposals are retained in §3.4–3.5 +as design history, not available APIs. The library reference records the +[current escaping policy](../standard-library/markup.md). + +All three use the hole lexer NIL-0014 already built, which emits an expression's +tokens with their original byte offsets rather than re-lexing decoded text. +Everything in this record inherits that contract, and no construct here +introduces an expression language of its own: a hole holds an ordinary +expression in its frontend's grammar, and nothing else. + +## 2. Multiline formatted text + +```text +value page is formatted begin text +

Signed in as {who}

+

{count plus 1} projects

+end text +``` + +Ny spells the same construct `$"""` ... `"""`. One difference is forced by the +surface: Matrix's `end text` owns its line, while Ny closes on the first line +whose content *begins* with `"""` and resumes lexing immediately after it, +because Ny is expression oriented and the closing `""")` must be allowed in +a call. The `$"""` opener must end its source line, apart from spaces or tabs; +content starts on the following line. + +The block follows the existing raw-block rules exactly — line-anchored +terminator, common leading indentation removed, deeper indentation kept, +leading and trailing blank lines dropped, no escape sequences — and adds +`{expression}` holes with `{{` and `}}` for literal braces. A single hole +stays on one physical line, which keeps the expression lexer unchanged. + +The implementation constraint, named as follow-up work in NIL-0014: **the +dedent must not move the holes.** The block scanner removes common indentation +and retains a per-line mapping back to the source. Hole tokens therefore keep +their original coordinates, so definition, completion, signature help and +local rename behave as they do inside a single-line formatted literal. + +This is the smallest useful step and it is worth taking on its own: it is what +a C# raw interpolated string gives, and it removes most `plus` ladders from +ordinary code. It does not solve §3's problem, because the control flow still +lives outside the document. + +**Implemented for Matrix on 2026-11-13.** `scan_formatted_block` in +`nilcompiler/src/formatted_text.hpp` scans the dedented block content, and +`matrix::lexer::scan_formatted_text_block` maps every piece back through a +per-line offset table before emitting the existing `formatted_string_begin` / +`formatted_text` / `interpolation_*` tokens. The parser, checker, lowering and +both execution routes are untouched, because the token stream and therefore the +AST are the ones the quoted form already produced. Coverage: the +`formatted-text-matrix` parity fixture across the VM, binary-NIL and native +routes; malformed blocks rejected on every route in +`tests/tools/formatted_text_contract.py`; and a CRLF block in +`tests/tools/semantic_contract.py` whose hole is renamed while an identical +word inside doubled braces in the dedented literal is left alone, which is the +proof that indentation removal did not move the expression. + +**Ny followed on 2026-11-13.** `lexer::formatted_block` mirrors the Matrix +scanner and shares `scan_formatted_block`, so both surfaces produce the same +token stream and the same AST. A block is raw in both: Ny's backslash escapes +apply to a quoted `$"..."` and not inside `$"""`. + +## 3. Markup literals + +```text +read nil.std.markup + +public page-shell receives title as text, signed-in as boolean, who as text + returns nil.std.markup.document begin paragraph + give begin markup + + {title} + + + + + end markup +end paragraph +``` + +The conditional is *inside* the document, where the variation actually is. The +page remains one unit a person can read, edit and paste. + +### 3.1 What the block contains + +Elements `children`, HTML void elements such as `
`, +text, comments, holes, and directives. Self-closing syntax is accepted only for +HTML void elements; a non-void element needs its matching closing tag. This +profile uses HTML tables and does not model SVG or MathML namespace semantics. Directives are spelled with the language's own +control words, so the block introduces no second vocabulary: + +```text +
    + {repeat for each p in projects} +
  • {p.name}
  • + {end repeat} +
+``` + +`{when ...}` / `{otherwise when ...}` / `{otherwise}` / `{end when}` and +`{repeat for each ... in ...}` / `{end repeat}` are the implemented Matrix +directives. Ny uses `{if ...}`, `{elseif ...}`, `{else}`, `{end if}`, +`{for ... in ...}` and `{end for}`. Numeric-range directives from the original +proposal are not implemented. + +A directive may stand between elements, between attributes, or inside text or +an ordinary attribute value. Each arm must restore both the element stack and +the surrounding tag/attribute context: it cannot leave an element or quote +open for another arm to close. Directives inside URL attribute values are +rejected in this profile; calculate the URL beforehand or conditionally emit +the complete element. A bare boolean attribute cannot be an arm's entire +content: use a complete quoted attribute such as `disabled=""` inside that arm, +or conditionally emit the complete element. + +**A hole cannot name an element or an attribute.** Both are fixed at compile +time, which is what makes the tree checkable. Dynamic attribute sets are +future work (§6). + +### 3.2 Hole typing, by position + +The initial proposal included arrays of documents and `nothing` holes. Those +are not implemented; this table describes the current checker. + +| Position | Accepted | Behavior | +| --- | --- | --- | +| Element content | `text`, `bool`, `i32`, `i64`, `u8`, `f64`, `document` | Text/scalars escape; a document splices its serialized content | +| Ordinary attribute value | `text` and the same scalar types | Escape within the source's existing quotes; no implicit omission | +| URL attribute | `text` and the same scalar types | Whole-value scheme filter or fixed-prefix component encoding; see §3.4 | +| Code-bearing attribute or raw-text element body | Explicit `{raw ...}` only | Trusted bypass, not an escaping or sanitizing operation | + +Use a loop for an array and explicit control flow for an optional value. +`{raw expression}` is the explicit trusted bypass. It accepts text or a +document in content or an already quoted attribute value, and does not +validate or escape the inserted bytes. Even `raw` cannot name an element or +attribute or appear in a comment. A manually constructed document and `nil.std.markup::raw` likewise require trusted input; +the public record is not a security boundary. + +### 3.3 Decision: raw-text elements are literal + +CSS and JavaScript are full of braces, and an author must be able to paste +them. Inside ` +``` + +The rule is a single lookahead over the element body and needs no escaping +convention, which keeps the no-escapes property of Matrix text intact in the +place where escaping would hurt most. + +### 3.4 Original proposal: a dialect-free renderer (superseded) + +**Design history.** The following renderer/table proposal preceded §3.6. +The implementation instead uses shared compiler-side HTML tables and library +escaping. Source quotes and self-closing spelling are preserved, and only HTML +is supported. The proposed scheme allowlist and runtime rejection below were +not implemented; see the current policy after the list. + +The block parses generic elements, attributes, text, comments and holes, and +checks balance. Everything specific to HTML lives in one table the **renderer** +owns: void elements, raw-text elements, boolean attributes, URL-bearing +attributes, and the escape sets. The parser accepts `
`; an HTML render +emits `
`, an XML render emits `
`. + +This is the difference between adding a construct and welding a document format +into the compiler. `begin markup as xml` then costs a table, not a grammar. + +Escaping is per context and belongs to the same table: + +- text node: `&`, `<`, `>` +- attribute value: `&`, `<`, `>`, `"`; output is always double-quoted +- URL attribute (`href`, `src`, `action`, `formaction`, `cite`, `poster`, + `data`, `srcset`): percent-encoded per component, with a scheme allowlist. + A hole producing a `javascript:` or `data:` URL is refused at run time unless + it came through `{raw}`. + +**Current URL policy.** A whole-value hole is checked for `javascript:`, +`vbscript:` and `data:` after browser-relevant ASCII control/space normalization +and ASCII case folding. A match encodes the colons as `%3a`, then applies +attribute escaping; it does not throw or omit the attribute. Other schemes +are not allowlisted. A component hole requires a fixed safe prefix such as +`/search?q=`, `https://example.com/`, `?q=` or `#section-`; variable scheme +assembly is rejected. Ordinary holes in `on*`, `style`, `srcdoc`, `srcset`, +`imagesrcset` and `ping` attributes require an explicit trusted `raw` bypass. +This policy is not an HTML, JavaScript, CSS or URL-list sanitizer. + +### 3.5 Original proposal: a runtime tree (superseded) + +**Design history.** This argument motivated the first design. §3.6 replaced +its runtime representation and rendering API; `render ... as xml` and +`render ... compact` below are proposals, not implemented syntax. + +`begin markup` evaluates to `markup`. `nil.std.markup::render with document +being page` produces `text`; `render ... as xml` and a `compact` option select +serialization. + +A string result would be simpler and would give up all four of the reasons for +doing this at all: + +1. Context-correct escaping requires knowing where a value sits. +2. Balance is checkable only while the structure still exists. +3. **Composition stays structural.** A paragraph returning `markup` + interpolates into another block as nodes — `
{page-header with title + being title}
` — so factoring a page into components does not route + back through text, and nothing is escaped twice. +4. A tree can be rendered to HTML, rendered to XML, or diffed. The last is + what a future webview host wants, and it is unavailable from a string + forever after. + +### 3.6 Representation: revised — compiled serialization with runtime holes + +**This section replaced its original proposal on 2026-11-13, during +implementation.** It first specified a runtime arena: parallel scalar arrays +holding a node tree, because §3.5 argued for a tree over text. Building it +turned up two things. + +The first is that the obvious implementation does not execute. A `node` record +holding `array of node` children fails: native arrays of records, choices and +nested arrays are unsupported (MATRIX-GUIDE §17) and an array of class handles +compiles and then fails at run time (§16). An arena of parallel scalar and text +arrays does work on both routes, so the plan was sound. + +The second is that it buys almost nothing here. Of §3.5's four reasons for a +tree, three do not need one at run time: + +- **Context selection** happens while the block is parsed. The generated + code still calls the escaping functions at run time, but does not traverse + a runtime tree to decide which escaper to use. +- **Balance checking** happens at parse time, whatever the value becomes. +- **Composition without seams** needs only that a document be a distinct + *type*, not a distinct *shape*: an already-escaped fragment spliced into + another document is correct whether it is a node list or serialized bytes. + +Only the fourth — rendering one document to several dialects, or diffing two — +needs a runtime tree, and that was already deferred to §5. + +The implemented value type is `nil.std.markup.document`, a record holding +serialized text whose ordinary holes have already been escaped. Static +segments are compile-time constants; holes, control flow and result assembly +execute at run time. The type distinguishes +composition from ordinary text interpolation. Its public field and `raw` +constructor still allow trusted callers to supply arbitrary markup; the type +does not certify that manually constructed content is safe. A future arena +would need to preserve the public API or explicitly migrate its callers. + +What this costs, stated plainly: the dialect is fixed when the block is +compiled rather than when it is rendered, so `begin markup as xml` becomes a +second lowering rather than a second renderer, and diffing two documents will +need the arena after all. Both were out of scope for this version, and neither +is reachable from a program written against this one. + +### 3.7 Decision: the text builder — not yet needed + +The original plan made an amortized text builder a prerequisite, because the +2026-10-15 audit (item 7) records that the experimental builder concatenates on +every append. With §3.6's revision the renderer is not a tree walk at all: a +page is a sequence of `STR_CONCAT` appends the compiler emits directly, and the +builder never enters the path. The audit item stands on its own merits and is +still worth fixing; it is no longer blocking. + +What rendering does depend on is `nil.std.text`, and one gap surfaced there: +`to_lower` reaches the `_char_code` interop, which has no native ABI, so a page +using it would compile for the VM and fail natively. The URL scheme check folds +case with `replace` instead. Making `to_lower` native is separate follow-up +work. + +## 4. What this is not + +Not a template file format: there is no second language in a second file, and +holes are checked against the enclosing paragraph's parameters like any other +expression. Not a component system, a virtual DOM or a binding framework. No +precision or presentation grammar inside holes — those remain library calls, as +NIL-0014 decided. No automatic minification. No implicit database, network or +file access from a document. + +## 5. Staging + +1. §2 multiline formatted text. **Done 2026-11-13.** +2. ~~Amortized text builder~~ — not a prerequisite after §3.6; see §3.7. +3. `begin markup` with the HTML table: parse, check balance, holes, `{when}`, + `{repeat}`, `{raw}`, render to text. **Done 2026-11-13.** The literal is + lexed by the block scanner from §2, parsed as markup by + `parser::parse_markup_expression`, checked in `type_checker::visit` + (escape versus splice) and emitted by `emit_markup` as ordinary NIL, so both + execution routes are covered by one emitter. Coverage: the `markup-matrix` + parity fixtures across the VM, binary, native and native-toolchain routes; + `tests/tools/markup_contract.py` checks malformed-document rejection and + context-specific escaping on the supported routes. +4. `nil.std.markup` constructors beyond `raw` and `empty`, so a document can be + assembled from values rather than written literally, and the XML table. + **Deferred.** +5. Diffing two documents, which is what would bring §3.6's arena back. + **Deferred.** + +Ny reached the initial profile on 2026-11-13: `markup_structure` holds document +parsing, structural checks and escape decisions. Each frontend supplies its +own directive words and expression lexing; parity fixtures exercise the shared +semantics. + +## 6. Open questions + +- **Dynamic attribute sets.** `{spread attrs}` in attribute position, taking a + list of name/value pairs, would cover `data-*` generation. Deferred: it + weakens compile-time checking and is not needed by the first consumers. +- **Whitespace.** The initial rule preserves text after the block's common + indentation and surrounding blank lines are removed. A composed document + keeps its own remaining indentation. `render ... compact` is a deferred + proposal; there is no whitespace-collapsing renderer in this version. +- **Editor support.** Holes keep original spans by construction (§2), but the + VS Code grammar needs markup-inside-Matrix highlighting or the construct will + look worse in an editor than the string it replaces. +- **Performance claims.** §3.6 asserts an append count, not a measurement. + Benchmark the rendered page against the concatenated equivalent before the + claim appears in any guide. +- ~~**Ny spelling.**~~ Settled while implementing. `markup { ... }`, this + record's original candidate, does not survive contact with the content: a + markup block is full of braces — holes, and CSS or JavaScript inside a + raw-text element — so a brace-delimited block would end at the first `}` in a + stylesheet. The terminator has to be a line-anchored keyword, which is what + lets the block hold any bytes without escapes. Ny therefore opens with + `$markup` at the end of its opening line and closes with line-leading + `end markup`. Ny resumes the surrounding expression after that terminator; + Matrix requires the terminator to occupy its line. The `$` is what keeps + it unambiguous: Ny has no `begin`, so a bare word before a newline could be + an ordinary name, while `$` already means "a literal with holes in it" in + that surface. Matrix needs no sigil because `begin` supplies the same + disambiguation. + +## 7. Compatibility + +Every construct here is additive under NIL-0026 §1: no existing program +observes a difference, because `formatted` before `begin text` is currently a +parse error, `markup` is a new contextual word before `begin`, and no existing +text literal changes meaning. Ships on by default, no edition gate. diff --git a/docs/design/README.md b/docs/design/README.md index 2f49752a..939f1e83 100644 --- a/docs/design/README.md +++ b/docs/design/README.md @@ -51,8 +51,9 @@ not assigned architecture identifiers. | NIL-0022 | [Incremental project builds and NILData configuration](NIL-0022-incremental-project-builds.md) | Implemented initial local project graph and target scheduling profile | | NIL-0023 | [Dynamic values and Boolean vocabulary](NIL-0023-dynamic-values-and-booleans.md) | Implemented | | NIL-0024 | [Tests alongside source](NIL-0024-colocated-testing.md) | Implemented initial unit/integration profile | -| NIL-0025 | [Matrix desktop UI](NIL-0025-matrix-desktop-ui.md) | Implemented Linux and Windows development GTK 4 profiles; macOS provider remains open | +| NIL-0025 | [Matrix desktop UI](NIL-0025-matrix-desktop-ui.md) | Implemented GTK4 on Linux, Win32 on Windows and AppKit on Intel/Apple Silicon macOS; release qualification remains separate | | NIL-0026 | [Evolving the language without breaking what is written](NIL-0026-language-evolution.md) | Editions implemented in the project manifest | +| NIL-0027 | [Multiline formatted text and structured markup literals](NIL-0027-structured-markup.md) | Proposed; staged, nothing shipped | | NIL-0093 | [Executable context is artifact self-description](NIL-0093-executable-context.md) | Implemented within the documented inspection profile | ## Retained Ny feature references diff --git a/docs/developer-tooling.md b/docs/developer-tooling.md index fb28d8c6..573f70db 100644 --- a/docs/developer-tooling.md +++ b/docs/developer-tooling.md @@ -28,18 +28,20 @@ dependency ordering, and bounded parallel jobs build independent projects and VM/native targets. SHA-256 input and artifact identities avoid redundant compilation only when the compiler certifies its source and loaded-code input graphs; native reuse also requires a complete linker/toolchain graph. Every -invocation still performs fresh analysis. Darwin currently rebuilds both VM -and native outputs while loaded-image/shared-cache identity is incomplete, -preserving source protection checks. See the +invocation still performs fresh analysis. Darwin now hashes admitted loaded +images, shared-cache backing files and native linker inputs; actual VM/native +reuse and invalidation pass on both Mac architectures. Unsupported profiles +rebuild, preserving source protection checks. See the [incremental build contract](tools/projects.md#incremental-and-parallel-builds). A single executable remains one compiler job over its complete import graph. Source debugging is implemented through `nildebugger --dap` and VS Code launch configurations for Ny, Matrix and debug-enabled NIL. Source breakpoints, stepping, frames, locals and streamed output use the interpreter's actual -execution state. The paired Windows/Linux `5630388` development baseline -includes installed debugger acceptance, with VS Code acceptance recorded for -Windows. See [platform support](platform-support.md) and the +execution state. Development SDKs on Linux, Windows and both Mac architectures +pass installed debugger and same-process execution-checkpoint acceptance. +See [platform support](platform-support.md) for separate installed editor +results and remaining checks, and the [source-debugging guide](tools/index.md#source-debugging-and-vs-code). Workspace source requests now let the compiler analyze open unsaved dependencies diff --git a/docs/getting-started/index.md b/docs/getting-started/index.md index dc420558..33ebb599 100644 --- a/docs/getting-started/index.md +++ b/docs/getting-started/index.md @@ -203,16 +203,22 @@ developer VM is not proof of installation on a bare Windows machine. ## macOS development builds Native Intel and Apple Silicon builds are established with LLVM enabled and -disabled. The recorded `c123849` baseline passed compiler/interpreter/Matrix and -checkpoint execution on all four profiles, with representative JIT/AOT routes -also passing when LLVM is enabled. The full CTest inventory still has failures; -these are development builds, not qualified macOS SDK releases. +disabled. LLVM-enabled development SDKs pass the full installed standard-library +campaign, isolated PostgreSQL routes, debugger/checkpoints and AppKit +interpreter/native desktop/input checks. Original full CTest failures are +retained alongside passing current-source follow-ups; these are development +results, not qualified macOS SDK releases. The manually dispatched [macOS workflow](../../.github/workflows/macos-bringup.yml) contains the tested configure/build commands and records the runner, Apple SDK, LLVM and optional-provider inputs. It builds tools directly and retains the -complete test inventory. Its tool archives have not undergone installed SDK -extraction, relocation, provider, debugger/editor or release acceptance. +complete test inventory. The separate +[SDK workflow](../../.github/workflows/macos-sdk.yml) packages development SDKs +and checks extraction, relocation and installed consumers. The +[installed replay workflow](../../.github/workflows/macos-sdk-replay.yml) +tests authenticated, unchanged SDK artifacts with current fixtures, including +optional desktop/editor checks. This distinguishes passing source/build-tree +tests from installed-package acceptance. Start with the [platform support matrix](../platform-support.md) and the [macOS notebook](../../notebook/2026-11-04-macos-parity-plan.md) for the exact source baselines and remaining work. Disabling LLVM does not make native routes diff --git a/docs/language-reference/expressions.md b/docs/language-reference/expressions.md index be581a84..c3942c88 100644 --- a/docs/language-reference/expressions.md +++ b/docs/language-reference/expressions.md @@ -205,14 +205,35 @@ and the same conversion is used by the interpreter and native compiler. Native text retains its existing NUL-free restriction; formatted text does not change the text storage ABI or permit embedded NUL constants natively. +Ny's multiline form opens with `$"""` at the end of its source line, apart +from spaces or tabs; content starts on the following line. It closes with the +first line whose non-indentation content begins with `"""`. Lexing resumes +after that delimiter, so a block can sit inside a call: + +```ny +println($""" +

Signed in as {who}

+

{count + 1} projects

+""") +``` + +A block is **raw**: unlike the quoted form, its literal segments carry no +backslash escapes, which is what makes a path, a regular expression or embedded +JSON writable without doubling anything. The block's common leading indentation +is removed and deeper indentation is kept, leading and trailing blank lines are +dropped, and indentation removal touches literal segments only, so every hole +keeps its original file coordinates. + `{{` and `}}` insert literal braces. Ny's normal backslash escapes still apply -to the literal portions, so `\n` inserts a newline in the result. Embedded +to the literal portions of a **quoted** formatted string, so `\n` inserts a +newline in the result. Embedded expressions keep their own string delimiters; no extra escaping is needed for `$"Result: {choose("first", "second")}"`. -The entire explicit string, including its expressions, must fit on one source -line. Empty holes, trailing tokens, unmatched braces and missing quotes are -errors. Formatting flags and precision specifiers are not part of this syntax; +A quoted explicit string, including its expressions, must fit on one source +line. Both surfaces have a block form that spans lines, and a single hole still +stays on one of them. Empty holes, trailing tokens, unmatched braces and missing quotes +are errors. Formatting flags and precision specifiers are not part of this syntax; use an explicit formatting function inside a hole when needed. Legacy ordinary strings such as `"Hello {name}"` retain identifier-only @@ -234,6 +255,92 @@ formatted Matrix text, backslashes remain ordinary characters; use the named `formatted "First{newline}Second"`. Braces delimit ordinary Matrix expressions, including named calls such as `{choose with first being "one"}`. +`formatted` in front of `begin text` opts a raw block into interpolation: + +```text +value page is formatted begin text +

Signed in as {who}

+

{count plus 1} projects

+end text +``` + +The block keeps every raw-block rule — a line-anchored `end text` terminator, +removal of the common leading indentation, retained deeper indentation, dropped +leading and trailing blank lines, and no escape sequences — and adds the holes. +Indentation removal applies to literal segments only. Every hole retains its +original file coordinates, so definition, completion, signature help and local +rename behave inside a block exactly as they do inside a quoted formatted +literal. A block and a quoted literal produce the same interpolation AST, the +same shared conversion and the same lowering on both execution routes. + +## Markup literals + +A markup literal holds a document — elements, attributes, text and holes — and +its own control flow, so a generated page is written as one unit instead of +assembled from concatenated fragments. Matrix opens one with `begin markup`, +Ny with `$markup`, and both close with the block's own terminator: + +```ny +import nil.std.markup.escaping + +fn page(signed_in: bool, who: text, names: array) -> document + return $markup + +
    + {for name in names} +
  • {name}
  • + {end for} +
+ end markup +end fn +``` + +The directive words are each surface's own: Ny writes `{if}`, `{elseif}`, +`{else}`, `{end if}`, `{for x in xs}` and `{end for}`, while Matrix writes +`{when}`, `{otherwise when}`, `{otherwise}`, `{end when}`, +`{repeat for each x in xs}` and `{end repeat}`. Everything else is shared, +including the parsing of the document, the checks, the escaping and the value. + +The literal segments are parsed as markup at compile time. Unbalanced or +unclosed elements, an unterminated directive, an arm that opens an element it +does not close, and a hole that would name an element or an attribute are +compile errors. Element and attribute names are fixed; that is what keeps the +document checkable. Self-closing syntax is limited to HTML void elements such +as `
`; other elements need a closing tag. The initial profile uses HTML +tables, without SVG or MathML namespace semantics. + +Ordinary holes accept `text`, `bool`, `i32`, `i64`, `u8` and `f64`, escaping +them for their position. A `nil.std.markup.document` also composes into element +content without being escaped again. Arrays and optional values need explicit +loops or conversions; they do not splice or omit attributes automatically. + +A whole URL hole uses a limited executable-scheme filter, while a component +hole needs a fixed safe URL prefix. Directives inside URL attribute values and +ordinary holes in code-bearing or URL-list attributes are rejected. See the +[`nil.std.markup` escaping contract](../standard-library/markup.md) for the +exact policy. `{raw ...}` explicitly trusts its input and bypasses escaping; +it is not a sanitizer. + +The block evaluates to a document record containing serialized text. `render` +returns that text; it does not walk a runtime tree. XML serialization and a +compact renderer are not implemented. + +Inside ` +``` + +Elsewhere, `{{` and `}}` are literal braces, as in formatted text. + +**URL and code contexts need distinct handling.** A whole URL hole filters +`javascript:`, `vbscript:` and `data:` after browser-relevant ASCII control/space +normalization and case folding, then applies attribute escaping. It encodes +the colons of a matched value rather than throwing or dropping the attribute; +other schemes are allowed. Component holes require a fixed safe URL prefix, +such as `/search?q=`, `https://example.com/`, `?q=` or `#section-`. A variable +scheme such as `href="{scheme}:example"` is rejected. Ordinary holes in `on*`, +`style`, `srcdoc`, `srcset`, `imagesrcset` and `ping` require explicit trusted +`raw`; HTML escaping is not a sanitizer for those languages. See the +[exact escaping contract](../docs/standard-library/markup.md). + +**Known limits.** Self-closing syntax is limited to HTML void elements such as +`
`; other elements need matching closing tags. The parser uses HTML tables +and does not model SVG or MathML namespace semantics. Element and attribute +names are fixed: a hole cannot name either, because that is what keeps the +document checkable. An attribute value +must be quoted, and the source's quote style is preserved. Text is emitted +after the block's common indentation and surrounding blank lines are removed; +a spliced document keeps its own remaining indentation. The dialect is HTML; +XML serialization and a compact renderer are not available yet. diff --git a/matrix/docs/grammar.ebnf b/matrix/docs/grammar.ebnf index dc40947c..0f0f0881 100644 --- a/matrix/docs/grammar.ebnf +++ b/matrix/docs/grammar.ebnf @@ -400,6 +400,7 @@ variadic_tail = 'more' expression { ',' expression } ; primary = literal | query_expr + | markup_block | identifier | 'my' identifier | nominal_type (* also carries explicit type arguments: @@ -454,11 +455,52 @@ text_literal = '"' { character_except_quote_or_newline } '"' | text_block ; named_character = 'quote' | 'newline' | 'tab' ; formatted_text = 'formatted' '"' { formatted_segment | '{{' | '}}' - | '{' expression '}' } '"' ; + | '{' expression '}' } '"' + | 'formatted' formatted_block ; (* formatted_segment is raw text excluding quotes, braces and newlines. * Expressions use the complete Matrix grammar, including their own quoted * literals. Doubled braces are literal text; backslashes remain ordinary. - * 'formatted' is contextual only before a quote, and remains a name elsewhere. *) + * 'formatted' is contextual before a quote and before 'begin text', and + * remains an ordinary name elsewhere. *) + +(* The block form is text_block with holes: identical terminator, dedent and + * blank-line rules, no escapes, and literal text that may span lines. A single + * hole still stays on one physical line. Indentation removal applies to the + * literal segments only, so a hole keeps its original source coordinates. *) +formatted_block = 'begin' 'text' newline + { any_line_with { '{{' | '}}' | '{' expression '}' } } + 'end' 'text' ; + +(* NIL-0027. A markup literal is the same block machinery reading markup: the + * literal segments are parsed as elements, attributes, comments and text so + * that balance is checked and each hole learns its context, then emitted + * verbatim. Holes carry the context's escaping, which the author never spells. + * Directives reuse the language's own control words and each arm must restore + * both the element stack and its tag/attribute context. Directives inside URL + * attribute values are rejected; numeric-range directives are not implemented. + * Element and attribute names are fixed, even for raw holes; comments cannot + * contain holes. Attribute values must be quoted; source quotes are preserved. + * Conditional bare boolean attributes are not supported; emit a complete + * quoted attribute inside the arm or conditionally emit the whole element. + * Self-closing syntax is limited to HTML void elements. This profile does not + * model SVG or MathML namespace semantics. + * Ordinary holes take text or formatted-text scalars. A document splices only + * into element content. Arrays/optional values need explicit control flow. + * Inside a raw-text element ('script', 'style') a brace is ordinary unless the + * body is exactly one hole, which must be explicit raw. Code-bearing and + * URL-list attribute holes likewise require trusted raw. A whole URL hole uses + * a limited scheme filter; component holes require a fixed safe URL prefix. + * Raw inserts trusted text/document bytes without validating or escaping them. + * The value is a nil.std.markup.document record of serialized text, with no + * runtime DOM, XML dialect or compact renderer in this profile. *) +markup_block = 'begin' 'markup' newline + { markup_content } 'end' 'markup' ; +markup_content = markup_literal | '{{' | '}}' + | '{' [ 'raw' ] expression '}' + | '{' 'when' expression '}' | '{' 'otherwise' [ 'when' expression ] '}' + | '{' 'end' 'when' '}' + | '{' 'repeat' 'for' 'each' identifier 'in' expression '}' + | '{' 'end' 'repeat' '}' ; (* A raw block, following the universal 'begin ' / 'end ' shape. * It is terminated by a line whose only content is 'end text' -- a diff --git a/matrix/docs/next-version-findings.md b/matrix/docs/next-version-findings.md index 4a8b399f..93c2fd21 100644 --- a/matrix/docs/next-version-findings.md +++ b/matrix/docs/next-version-findings.md @@ -1637,10 +1637,12 @@ record, array or tuple arguments at the callsite before code generation. Doubled braces insert literal braces in explicit formatted text. Ordinary Matrix quoted strings and `begin text` blocks remain raw; legacy Ny identifier interpolation stays available for compatibility. `formatted` is contextual -before a quote and remains an ordinary Matrix name elsewhere. Explicit strings -and their expressions currently occupy one physical source line. Empty holes, -trailing tokens, unmatched braces and unterminated strings are errors; the -parser does not silently convert malformed expressions back into text. +before a quote or `begin text` and remains an ordinary Matrix name elsewhere. +Quoted formatted strings occupy one physical source line. Multiline blocks are +implemented for both surfaces (F88); each hole still stays on one physical +line. Empty holes, trailing tokens, unmatched braces and unterminated strings +are errors; the parser does not silently convert malformed expressions back +into text. **Coverage:** Source-span tests include decoded Ny prefixes, raw Matrix backslashes, nested formatted strings, quoted expression arguments, UTF-8, @@ -1650,8 +1652,8 @@ contract checks local references and applies an actual rename, proving that matching words inside doubled-brace literal text remain unchanged. Execution contracts cover the shared scalar conversion and evaluation order. -**Follow-up:** Add multiline formatted text only with an explicit original -source map through indentation removal. Custom record formatting should use +**Follow-up:** Multiline formatted text now preserves an explicit original +source map through indentation removal (F88). Custom record formatting should use the existing `Display` contract once dispatch is qualified on all routes; currently callers must explicitly produce a supported scalar or text value. Precision and presentation options remain library calls rather than a second @@ -2480,3 +2482,87 @@ RHS lowering receives the destination context. Paired Ny/Matrix fixtures cover local and record-field assignments of `Option`, with side effects proving each payload is evaluated once. They run through textual/binary NIL, bundles, LLVM JIT and AOT. This does not extend native global storage to enums. + +### F88 — A generated document needs multiline interpolation before it needs a document type + +**Observed:** Every generator written against the framework builds its output +by concatenation, because a `formatted` literal was confined to one physical +source line (NIL-0014) and a raw `begin text` block carried no holes. The +result is the shape this framework exists to avoid: the document's structure +spread across bindings and `plus` chains, its control flow outside the markup, +and no check that the output is even balanced. Writing a page shell by hand +produced four bindings and six concatenations for eleven tags. + +A second, quieter cost: `MATRIX-GUIDE.md` documented none of `formatted`, +`collect begin query` or `begin pattern`, all of which had shipped. An agent +writing Matrix from the guide therefore could not have used them even where +they applied. + +**Fixed:** `formatted` in front of `begin text` opts a raw block into +interpolation, keeping every raw-block rule — line-anchored terminator, common +indentation removed, deeper indentation kept, no escapes — and adding holes. A +single hole still stays on one physical line. Indentation removal applies to +literal segments only: the lexer records the source offset each dedented line +starts at and maps every piece back through it, so hole tokens keep their +original coordinates and NIL-0014's editing contract holds unchanged. The +block emits the token stream the quoted form already emitted, so the parser, +checker, lowering, VM and native routes needed no change at all. The three +shipped constructs are now documented in the guide and asserted by +`tests/matrix/execution/guide-constructs.matrix` and +`guide-queries-patterns.matrix`. + +**Ny parity:** `$"""` now opens the same raw formatted block. The opener +must end its line; the first later line beginning with `"""` closes the block +and permits an expression continuation such as a call's closing `)`. Both +surfaces share the interpolation scanner and preserve original hole spans. +Structured document support followed in F89 and +[NIL-0027](../../docs/design/NIL-0027-structured-markup.md) §3. + +### F89 — A document assembled from fragments cannot be checked or escaped + +**Observed:** F88 gave a generator readable interpolation, and left the real +problem untouched: the control flow still lived outside the document. A page +shell was still four bindings and a conditional that produced two different +strings of tags, nothing checked that the result was balanced, and every +interpolated value carried whatever escaping its author remembered. The +escaping is the part that cannot be fixed by discipline — the correct escape +differs between element content, an attribute value and a URL attribute, and a +string does not know which one it landed in. + +**Fixed:** `begin markup` (NIL-0027 §3). The literal segments are parsed as +markup at compile time, so unbalanced elements and unterminated directives are +compile errors, and each hole is escaped for the position it occupies. The +block's value is a `nil.std.markup.document`, a distinct type whose whole job +is that an already-escaped fragment composed into another document is spliced +rather than escaped twice. `{raw ...}` is the single, greppable way to bypass +escaping. Control flow is written with the language's own words inside the +document, and every arm must leave the element stack as it found it. + +Three things fell out of building it, each recorded where it belongs: + +- The node tree the design record specified cannot execute natively, and would + have bought only the capability that was already deferred. The value is + serialized at compile time instead; NIL-0027 §3.6 records the reversal. +- `nil.std.text::to_lower` reaches `_char_code`, which has no native ABI, so a + page that used it compiled for the VM and failed natively. The URL scheme + check folds case with `replace`. Making `to_lower` native is open work. +- The checker spells `text` as `String` internally while the diagnostic + renderer prints `text`, so a comparison against `"text"` alone fails while + reporting that it got `text`. Any new check over text types must accept both + spellings. + +**Ny reached parity the same day.** `markup_structure` holds the document +parsing, the checks and the escape decisions; each frontend supplies only its +own directive words (`{if}`/`{for}` in Ny, `{when}`/`{repeat}` in Matrix) and +its own expression lexing. Ny opens a block with `$markup` rather than the +`markup { ... }` the design record first guessed: a markup block is full of +braces, so a brace-delimited block would end at the first `}` in a stylesheet. +Two frictions showed up only on the Ny side — the sigil skips six characters +after the already-consumed `$`, not seven, and Ny names an imported type by its +short spelling, so the document type has to be recognised by declaration rather +than by name. + +**Remaining:** whitespace is emitted as written, so +a composed document keeps its own indentation; a compact renderer and the XML +dialect are future work. Element and attribute names are fixed, so dynamic +attribute sets (`data-*` generation) still need a design. diff --git a/matrix/docs/types-and-text.md b/matrix/docs/types-and-text.md index a092dd44..4df58a9c 100644 --- a/matrix/docs/types-and-text.md +++ b/matrix/docs/types-and-text.md @@ -199,10 +199,28 @@ not make embedded NUL bytes representable by the native text ABI. Backslashes remain ordinary characters. `{{` and `}}` represent literal braces only in this explicitly formatted form. Empty holes, extra tokens, unmatched -braces and missing quotes are errors. The string and its expressions must stay -on one physical source line; interpolate `newline` to create a multiline -result. A raw `begin text` block inside a hole therefore is not supported. -Ordinary quoted text and `begin text` keep their previous raw behavior. +braces and missing quotes are errors. A quoted formatted literal and its +expressions stay on one physical source line. A raw `begin text` block inside a +hole therefore is not supported. Ordinary quoted text and `begin text` keep +their previous raw behavior. + +### Multiline formatted blocks + +`formatted` in front of `begin text` opts a raw block into interpolation: + +```text +value page as text is formatted begin text +

Signed in as {who}

+

{count plus 1} projects

+end text +``` + +Every raw-block rule still holds: the terminator is a line whose only content +is `end text`, the common leading indentation is removed and deeper indentation +kept, leading and trailing blank lines are dropped, and nothing inside is +escaped. A single hole stays on one physical line; the literal text around it +does not. Indentation is removed from literal segments only, so a hole keeps +its original source position and remains editable. `formatted` remains available as a name when it is not followed by a quote. Expression identifiers keep their original source ranges for editor checking, diff --git a/nilassembler/main.cpp b/nilassembler/main.cpp index efb7b0fd..9cbfc922 100644 --- a/nilassembler/main.cpp +++ b/nilassembler/main.cpp @@ -473,9 +473,9 @@ int run_assembler(const command_line &arguments) { native.setFreestandingEntry(arguments.freestanding); native.setDisableRedZone(arguments.no_red_zone); if (arguments.code_model == "small") { - native.setCodeModel(ny::NativeCodegen::CodeModel::small); + native.setCodeModel(ny::NativeCodegen::CodeModel::Small); } else if (arguments.code_model == "large") { - native.setCodeModel(ny::NativeCodegen::CodeModel::large); + native.setCodeModel(ny::NativeCodegen::CodeModel::Large); } native.setEmbedContext(arguments.embed_context); if (!native.initialize()) { diff --git a/nilassembler/tests/nilassembler_cli_tests.py b/nilassembler/tests/nilassembler_cli_tests.py index d16c5ba8..4003c77e 100644 --- a/nilassembler/tests/nilassembler_cli_tests.py +++ b/nilassembler/tests/nilassembler_cli_tests.py @@ -122,6 +122,8 @@ def link_and_run( if sys.platform.startswith("linux"): command.append("-no-pie") command.extend(["-o", str(executable), str(object_path), str(runtime)]) + if sys.platform == "darwin": + command.extend(["-framework", "Cocoa", "-framework", "CoreGraphics"]) linked = run(command, cwd) if linked.returncode != 0: raise AssertionError( diff --git a/nilbuild/darwin_toolchain_inputs.hpp b/nilbuild/darwin_toolchain_inputs.hpp new file mode 100644 index 00000000..6ea439c5 --- /dev/null +++ b/nilbuild/darwin_toolchain_inputs.hpp @@ -0,0 +1,763 @@ +#pragma once + +// Conservative closure for nilbuild's ordinary Apple-clang native invocation. +// This deliberately inventories a superset of SDK linkable inputs; unsupported +// drivers, search profiles, stub formats or escaping dependencies disable reuse. +// Every returned file is content-hashed by nilbuild, and the complete namespace +// is collected again before publication. No timestamps certify cache identity. +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace nilbuild::darwin_toolchain_detail { +namespace fs = std::filesystem; + +inline void explain(std::string *reason, std::string message) { + if (!reason || !reason->empty()) return; + if (message.size() > 1024) message.resize(1024); + for (char &byte : message) + if (static_cast(byte) < 32 || byte == 127) byte = '?'; + *reason = std::move(message); +} + +inline std::string trim(const std::string &text) { + const auto first = text.find_first_not_of(" \r\n\t"); + return first == std::string::npos ? "" : text.substr(first, text.find_last_not_of(" \r\n\t") - first + 1); +} +inline bool regular(const fs::path &path) { + std::error_code error; + return fs::is_regular_file(path, error) && !error; +} +inline bool inside(const fs::path &path, const fs::path &root) { + const auto value = path.lexically_normal(), base = root.lexically_normal(); + auto at = value.begin(); + for (auto part = base.begin(); part != base.end(); ++part, ++at) + if (at == value.end() || *at != *part) return false; + return true; +} +inline bool clean_path(const fs::path &path) { + const auto text = path.string(); + return path.is_absolute() && text.find_first_of("\r\n\0", 0, 3) == std::string::npos && + path == path.lexically_normal(); +} + +// clang -### quotes every command argument. Interpret only its double-quoted +// command syntax; never execute text through a shell or accept response files. +inline std::optional> quoted_arguments(const std::string &line) { + std::vector result; + std::size_t at = 0; + while (at < line.size()) { + while (at < line.size() && std::isspace(static_cast(line[at]))) ++at; + if (at == line.size()) break; + if (line[at++] != '"') return {}; + std::string value; + bool closed = false; + while (at < line.size()) { + char byte = line[at++]; + if (byte == '"') { closed = true; break; } + if (byte == '\\') { + if (at == line.size() || (line[at] != '\\' && line[at] != '"')) return {}; + byte = line[at++]; + } + if (byte == '\0' || byte == '\n' || byte == '\r') return {}; + value += byte; + } + if (!closed || value.empty()) return {}; + result.push_back(std::move(value)); + if (at < line.size() && !std::isspace(static_cast(line[at]))) return {}; + } + return result.empty() ? std::nullopt : std::optional>(std::move(result)); +} + +struct link_plan { + fs::path installed, linker, sdk, lto, compiler_rt; + std::string architecture; + bool local_libraries = false; +}; +inline std::optional parse_plan(const std::string &text, const fs::path &runtime) { + if (text.size() > 1024 * 1024 || text.find('\0') != std::string::npos) return {}; + link_plan plan; + std::vector command; + std::istringstream lines(text); + std::string line; + bool apple = false; + while (std::getline(lines, line)) { + line = trim(line); + if (line.empty()) continue; + if (line.rfind("Apple clang version ", 0) == 0 && !apple) { apple = true; continue; } + if (line.rfind("Target: ", 0) == 0 || line == "Thread model: posix") continue; + if (line.rfind("InstalledDir: ", 0) == 0 && plan.installed.empty()) { + plan.installed = line.substr(14); continue; + } + if (line.front() == '"' && command.empty()) { + const auto parsed = quoted_arguments(line); + if (!parsed) return {}; + command = *parsed; continue; + } + // Includes default configuration-file diagnostics and extra compiler jobs. + return {}; + } + if (!apple || !clean_path(plan.installed) || command.empty() || + plan.installed.filename() != "bin" || plan.installed.parent_path().filename() != "usr") return {}; + const auto installation = plan.installed.parent_path().parent_path().filename(); + if (installation != "XcodeDefault.xctoolchain" && installation != "CommandLineTools") return {}; + plan.linker = command.front(); + if (plan.linker != plan.installed / "ld") return {}; + bool dynamic = false, platform = false, output = false, input = false, cxx = false, system = false; + std::set frameworks; + for (std::size_t index = 1; index < command.size(); ++index) { + const auto &option = command[index]; + const auto next = [&]() -> std::optional { + if (index >= command.size() - 1) { index = command.size(); return {}; } + return command[++index]; + }; + if (option == "-demangle" || option == "-no_deduplicate" || option == "-adhoc_codesign") continue; + if (option == "-dynamic" && !dynamic) { dynamic = true; continue; } + if (option == "-lc++" && !cxx) { cxx = true; continue; } + if (option == "-lSystem" && !system) { system = true; continue; } + // Apple's selected driver emits this explicit search path on ordinary + // hosted installations. Unlike ld's implicit default it can fall back to + // the host when the selected SDK has no usr/local/lib directory. + if (option == "-L/usr/local/lib") { plan.local_libraries = true; continue; } + if (option == "-L") { + const auto value = next(); + if (!value || *value != "/usr/local/lib") return {}; + plan.local_libraries = true; continue; + } + if (option == "-arch" && plan.architecture.empty()) { + const auto value = next(); if (!value || (*value != "arm64" && *value != "x86_64")) return {}; + plan.architecture = *value; continue; + } + if (option == "-platform_version" && !platform) { + const auto name = next(), minimum = next(), sdk_version = next(); + if (!name || *name != "macos" || !minimum || !sdk_version || minimum->empty() || sdk_version->empty() || + minimum->find_first_not_of("0123456789.") != std::string::npos || + sdk_version->find_first_not_of("0123456789.") != std::string::npos) return {}; + platform = true; continue; + } + if (option == "-syslibroot" && plan.sdk.empty()) { + const auto value = next(); if (!value || !clean_path(*value)) return {}; + plan.sdk = *value; continue; + } + if (option == "-lto_library" && plan.lto.empty()) { + const auto value = next(); if (!value || !clean_path(*value)) return {}; + plan.lto = *value; continue; + } + if (option == "-mllvm") { + const auto value = next(); if (!value || *value != "-enable-linkonceodr-outlining") return {}; + continue; + } + if (option == "-o" && !output) { + const auto value = next(); if (!value || *value != "/dev/null") return {}; + output = true; continue; + } + if (option == "-framework") { + const auto value = next(); + if (!value || (*value != "Cocoa" && *value != "CoreGraphics") || !frameworks.insert(*value).second) return {}; + continue; + } + if (option == runtime.string() && !input) { input = true; continue; } + const fs::path path(option); + if (plan.compiler_rt.empty() && clean_path(path) && path.filename() == "libclang_rt.osx.a" && + inside(path, plan.installed.parent_path() / "lib" / "clang")) { + plan.compiler_rt = path; continue; + } + return {}; // Includes custom -L/-F, -force_load, plugins, and response files. + } + if (!dynamic || !platform || !output || !input || !cxx || !system || frameworks.size() != 2 || + plan.architecture.empty() || plan.sdk.empty() || plan.lto.empty() || plan.compiler_rt.empty() || + plan.lto != plan.installed.parent_path() / "lib" / "libLTO.dylib" || + plan.sdk.filename().string().rfind("MacOSX", 0) != 0 || plan.sdk.extension() != ".sdk") return {}; + return plan; +} + +// The two explicit archives are the only link inputs that can carry object +// autolinks in this profile. An unmodeled LC_LINKER_OPTION can request libraries +// outside the command's fixed c++/System/framework roots. otool must successfully +// inspect the selected architecture before the caller invokes the collector. +inline bool supported_archive_commands(const std::string &text) { + return !text.empty() && text.size() <= 4 * 1024 * 1024 && text.find('\0') == std::string::npos && + text.find("LC_LINKER_OPTION") == std::string::npos; +} + +inline bool macho_magic(const std::string &bytes) { + if (bytes.size() < 4) return false; + const auto prefix = bytes.substr(0, 4); + return prefix == std::string("\xfe\xed\xfa\xce", 4) || prefix == std::string("\xce\xfa\xed\xfe", 4) || + prefix == std::string("\xfe\xed\xfa\xcf", 4) || prefix == std::string("\xcf\xfa\xed\xfe", 4) || + prefix == std::string("\xca\xfe\xba\xbe", 4) || prefix == std::string("\xbe\xba\xfe\xca", 4) || + prefix == std::string("\xca\xfe\xba\xbf", 4) || prefix == std::string("\xbf\xba\xfe\xca", 4); +} + +// An SDK may ship runnable plug-ins alongside its linkable interfaces. Apple +// ld's ordinary dylib parser rejects MH_EXECUTE and MH_BUNDLE; this profile +// never passes -bundle_loader. Ignore only positively identified such images, +// including every universal slice. A dylib/object, malformed header or unknown +// file remains unmodeled. Sources: cctools mach-o/{loader,fat}.h and ld64's +// src/ld/parsers/macho_dylib_file.cpp, Parser::validFile. +inline std::optional> macho_file_types(const fs::path &path) { + std::vector kinds; + std::error_code error; + const auto size = fs::file_size(path, error); + if (error || size > static_cast(std::numeric_limits::max())) return {}; + std::ifstream file(path, std::ios::binary); + const auto read = [&](std::uint64_t offset, std::size_t count) -> std::optional { + if (offset > size || count > size - offset || count > 4 * 1024 * 1024) return {}; + std::string bytes(count, '\0'); file.clear(); file.seekg(static_cast(offset)); + if (!file.read(bytes.data(), static_cast(count))) return {}; + return bytes; + }; + const auto number = [](const std::string &bytes, std::size_t at, unsigned count, bool big) { + std::uint64_t value = 0; + for (unsigned index = 0; index < count; ++index) + value |= std::uint64_t(static_cast(bytes[at + index])) << + (8 * (big ? count - index - 1 : index)); + return value; + }; + const auto slice = [&](std::uint64_t offset, std::uint64_t extent, + std::optional> cpu) { + const auto header = read(offset, 28); + if (!header || extent < 28) return false; + const auto magic = header->substr(0, 4); + const bool big = magic == std::string("\xfe\xed\xfa\xce", 4) || magic == std::string("\xfe\xed\xfa\xcf", 4); + const bool wide = magic == std::string("\xfe\xed\xfa\xcf", 4) || magic == std::string("\xcf\xfa\xed\xfe", 4); + if (!big && magic != std::string("\xce\xfa\xed\xfe", 4) && magic != std::string("\xcf\xfa\xed\xfe", 4)) return false; + const auto header_size = wide ? 32U : 28U; + if (extent < header_size || !read(offset, header_size)) return false; + if (cpu && (number(*header, 4, 4, big) != cpu->first || (number(*header, 8, 4, big) & 0x00ffffffU) != (cpu->second & 0x00ffffffU))) return false; + const auto kind = number(*header, 12, 4, big); + const auto count = number(*header, 16, 4, big), bytes = number(*header, 20, 4, big); + if (count > 65536 || bytes > 4 * 1024 * 1024 || bytes > extent - header_size) return false; + const auto commands = read(offset + header_size, static_cast(bytes)); + if (!commands) return false; + std::size_t at = 0; + for (std::uint64_t index = 0; index < count; ++index) { + if (at > commands->size() || commands->size() - at < 8) return false; + const auto length = number(*commands, at + 4, 4, big); + if (length < 8 || length % 4 || length > commands->size() - at) return false; + at += static_cast(length); + } + if (at != commands->size()) return false; + kinds.push_back(static_cast(kind)); + return true; + }; + const auto header = read(0, 8); + if (!header || !macho_magic(*header)) return {}; + const auto magic = header->substr(0, 4); + const bool fat_big = magic == std::string("\xca\xfe\xba\xbe", 4) || magic == std::string("\xca\xfe\xba\xbf", 4); + const bool fat_little = magic == std::string("\xbe\xba\xfe\xca", 4) || magic == std::string("\xbf\xba\xfe\xca", 4); + if (!fat_big && !fat_little) { + if (!slice(0, size, {})) return {}; + return kinds; + } + const bool wide = magic == std::string("\xca\xfe\xba\xbf", 4) || magic == std::string("\xbf\xba\xfe\xca", 4); + const auto count = number(*header, 4, 4, fat_big); + const auto stride = wide ? 32U : 20U; + if (!count || count > 64 || size < 8 + count * stride) return {}; + const auto table = read(8, static_cast(count * stride)); + if (!table) return {}; + // ld64 also recognizes one legacy ARM64 descriptor beyond nfat_arch in + // FAT_MAGIC files. Do not silently omit a hidden potentially-linkable slice. + if (!wide && size >= 8 + (count + 1) * stride) { + const auto extra = read(8 + count * stride, stride); + if (!extra) return {}; + if (number(*extra, 0, 4, fat_big) == 0x0100000c && number(*extra, 4, 4, fat_big) <= 1) return {}; + } + std::vector> extents; + for (std::uint64_t index = 0; index < count; ++index) { + const auto at = static_cast(index * stride); + const auto offset = number(*table, at + 8, wide ? 8 : 4, fat_big); + const auto extent = number(*table, at + (wide ? 16 : 12), wide ? 8 : 4, fat_big); + const auto alignment = number(*table, at + (wide ? 24 : 16), 4, fat_big); + if (alignment > 63 || offset < 8 + count * stride || offset > size || extent > size - offset || + offset % (std::uint64_t(1) << alignment) || (wide && number(*table, at + 28, 4, fat_big))) return {}; + for (const auto &previous : extents) + if (offset < previous.second && previous.first < offset + extent) return {}; + extents.emplace_back(offset, offset + extent); + if (!slice(offset, extent, std::make_pair(number(*table, at, 4, fat_big), number(*table, at + 4, 4, fat_big)))) return {}; + } + return kinds; +} + +inline bool non_linkable_macho(const fs::path &path) { + const auto kinds = macho_file_types(path); + return kinds && !kinds->empty() && std::all_of(kinds->begin(), kinds->end(), + [](std::uint32_t kind) { return kind == 2 || kind == 8; }); // MH_EXECUTE / MH_BUNDLE +} + +inline bool system_install_name(const std::string &name) { + const fs::path path(name); + // Install names are always POSIX paths, including in portable parser tests. + return !name.empty() && name.front() == '/' && name.find_first_of("\\\r\n\0 \t", 0, 6) == std::string::npos && + path == path.lexically_normal() && (inside(path, "/usr/lib") || inside(path, "/System/Library/Frameworks") || + inside(path, "/System/Library/PrivateFrameworks")); +} + +// A conservative lexical admission check for Apple's generated YAML TBD v4. +// Re-export paths must be explicit system install names. Escapes, aliases, custom tags, +// relative re-exports and rpath names require a richer parser and disable reuse. +// Inventorying every SDK stub covers all architecture/OS re-export choices. +inline bool supported_stub(const std::string &text, std::set *references = nullptr) { + if (text.size() > 32 * 1024 * 1024 || text.find('\0') != std::string::npos || + text.find('\\') != std::string::npos || text.rfind("--- !tapi-tbd", 0) != 0) return false; + std::istringstream lines(text); std::string line; + bool version = false, install = false, in_libraries = false; + std::string libraries; + auto scalar = [](std::string value) { + value = trim(value); + if (value.size() >= 2 && (value.front() == '\'' || value.front() == '"') && value.back() == value.front()) + value = value.substr(1, value.size() - 2); + return value; + }; + const auto admit_name = [&](const std::string &name) { + if (!system_install_name(name)) return false; + const fs::path path(name); + if (!path.extension().empty() && path.extension() != ".dylib" && path.extension() != ".tbd") return false; + for (const auto &part : path) + if (part == "Headers" || part == "PrivateHeaders" || part == "Modules") return false; + if (references) { references->insert(name); if (references->size() > 65536) return false; } + return true; + }; + while (std::getline(lines, line)) { + line = trim(line); + if (line.empty() || line.front() == '#') continue; + if (line.rfind("---", 0) == 0) { if (line != "--- !tapi-tbd") return false; continue; } + if (line.rfind("tbd-version:", 0) == 0) { + if (trim(line.substr(12)) != "4") return false; + version = true; continue; + } + if (line.rfind("install-name:", 0) == 0) { + // LC_ID_DYLIB and $ld$install_name export strings describe output/interface + // identity; they do not name additional files read by the linker. + // Generated SDKs contain @rpath and historical AppleInternal IDs here. + if (scalar(line.substr(13)).empty()) return false; + install = true; + } + const auto split = line.find(':'); + const auto key = split == std::string::npos ? std::string{} : trim(line.substr(0, split)); + if (key == "reexported-libraries" && !trim(line.substr(split + 1)).empty()) return false; + if (!line.empty() && (line.front() == '"' || line.front() == '\'')) { + const auto end_quote = line.find(line.front(), 1); + if (end_quote != std::string::npos && trim(line.substr(end_quote + 1)).rfind(':', 0) == 0) return false; + } + if (key == "libraries" || key == "library") { + if (in_libraries) return false; + libraries = trim(line.substr(split + 1)); + if (libraries.empty() || libraries.front() != '[') return false; + in_libraries = true; + } else if (in_libraries) libraries += ' ' + line; + if (in_libraries && libraries.find(']') != std::string::npos) { + if (libraries.back() != ']') return false; + std::istringstream values(libraries.substr(1, libraries.size() - 2)); std::string value; + while (std::getline(values, value, ',')) if (!admit_name(scalar(value))) return false; + in_libraries = false; + } + // Reject YAML indirection outside quoted strings, so it cannot manufacture + // an unobserved re-export value from an anchor or tagged scalar. + bool quoted = false; char quote = 0; + for (std::size_t at = 0; at < line.size(); ++at) { + const char byte = line[at]; + if ((byte == '\'' || byte == '"') && (!quoted || quote == byte)) { + quoted = !quoted; quote = byte; continue; + } + if (!quoted && (byte == '&' || byte == '*' || byte == '!' || byte == '|' || byte == '>' || + byte == '{' || byte == '}')) return false; + } + } + return version && install && !in_libraries; +} + +struct image_description { + std::vector libraries, rpaths; + bool system_loader = false; +}; +inline std::optional parse_image(const fs::path &path, + const std::string &listing, + const std::string &commands) { + if (listing.size() > 1024 * 1024 || commands.size() > 4 * 1024 * 1024 || + listing.find('\0') != std::string::npos || commands.find('\0') != std::string::npos || + commands.find("LC_DYLD_ENVIRONMENT") != std::string::npos) return {}; + image_description result; + std::istringstream lines(listing); std::string line; bool heading = false; + while (std::getline(lines, line)) { + if (line.empty()) continue; + if (line.front() != '\t' && line.front() != ' ') { + if (line.rfind(path.string(), 0) != 0 || line.back() != ':') return {}; + heading = true; continue; + } + line = trim(line); const auto marker = line.find(" (compatibility version "); + if (marker == std::string::npos) return {}; + result.libraries.push_back(line.substr(0, marker)); + } + if (!heading) return {}; + std::istringstream load_commands(commands); bool rpath = false, loader = false; + while (std::getline(load_commands, line)) { + line = trim(line); + if (line == "cmd LC_RPATH") { if (rpath || loader) return {}; rpath = true; } + else if (line == "cmd LC_LOAD_DYLINKER") { if (rpath || loader) return {}; loader = true; } + else if (rpath && line.rfind("path ", 0) == 0) { + const auto marker = line.rfind(" (offset "); + if (marker == std::string::npos || marker <= 5) return {}; + result.rpaths.push_back(line.substr(5, marker - 5)); rpath = false; + } else if (loader && line.rfind("name ", 0) == 0) { + const auto marker = line.rfind(" (offset "); + if (marker == std::string::npos || line.substr(5, marker - 5) != "/usr/lib/dyld") return {}; + result.system_loader = true; loader = false; + } else if ((rpath || loader) && line.rfind("cmdsize ", 0) != 0) return {}; + } + return rpath || loader ? std::nullopt : std::optional(std::move(result)); +} + +class collector { +public: + using probe = std::function(const fs::path &)>; +private: + fs::path driver, cwd, runtime, host; + link_plan plan; + probe dependencies; + std::set files_seen, active_directories; + std::set stub_references; + std::set inspected_contexts; + std::map image_descriptions; + std::size_t entries = 0; + std::string *reason; + + bool fail(const char *message, const fs::path &path = {}) { + if (reason) explain(reason, std::string(message) + (path.empty() ? "" : ": " + path.string())); + return false; + } + std::optional> refuse(const char *message, const fs::path &path = {}) { + fail(message, path); return {}; + } + + fs::path host_path(const fs::path &path) const { return host / path.relative_path(); } + bool add(const fs::path &path) { + if (!clean_path(path) || !regular(path)) return fail("required input is not a normalized regular file", path); + if (files_seen.size() >= 65536) return fail("input file inventory exceeded 65536 entries", path); + if (path.extension() == ".a") { + std::ifstream archive(path, std::ios::binary); char magic[8]{}; + archive.read(magic, sizeof(magic)); + if (archive.gcount() == 8 && std::string(magic, sizeof(magic)) == "!\n") return fail("thin archive has unmodeled external members", path); + } + files_seen.insert(path); return true; + } + bool read_stub(const fs::path &path) { + std::ifstream stream(path, std::ios::binary); std::string text; char block[8192]; + while (stream.read(block, sizeof(block)) || stream.gcount()) { + text.append(block, static_cast(stream.gcount())); + if (text.size() > 32 * 1024 * 1024) return fail("TBD input exceeds 32 MiB", path); + } + if (stream.bad()) return fail("could not read TBD input", path); + return supported_stub(text, &stub_references) || fail("unsupported TBD dependency profile", path); + } + bool tree(const fs::path &path, bool sdk, unsigned depth = 0) { + std::error_code error; + if (depth > 32 || ++entries > 1000000) return fail("SDK traversal exceeded depth or entry bound", path); + const auto canonical = fs::canonical(path, error); + if (error || !active_directories.insert(canonical).second) return fail("unreadable or cyclic SDK directory", path); + fs::directory_iterator iterator(path, error), end; + if (error) return fail("could not enumerate SDK directory", path); + for (; iterator != end; iterator.increment(error)) { + if (error || ++entries > 1000000) return fail("SDK enumeration failed or exceeded entry bound", path); + const auto entry = iterator->path(); + const auto status = fs::status(entry, error); + if (error) return fail("could not stat SDK entry", entry); + if (fs::is_directory(status)) { + // Header/module trees cannot be selected as linked library files. + const auto name = entry.filename(); + if (name == "Headers" || name == "PrivateHeaders" || name == "Modules") continue; + if (!tree(entry, sdk, depth + 1)) return false; + } else if (fs::is_regular_file(status)) { + const auto extension = entry.extension(); + if (extension == ".tbd" || extension == ".dylib" || extension == ".so" || extension == ".a" || extension == ".o" || extension.empty()) { + if (sdk && extension.empty()) { + std::ifstream stream(entry, std::ios::binary); char prefix[16]{}; + stream.read(prefix, sizeof(prefix)); + const auto size = static_cast(stream.gcount()); + if (stream.bad()) return fail("could not inspect extensionless SDK entry", entry); + const std::string bytes(prefix, size); + if (bytes.rfind("--- !tapi-tbd", 0) == 0) { + if (!add(entry) || !read_stub(entry)) return false; + } else if (bytes.rfind("!\n", 0) == 0 || bytes.rfind("!\n", 0) == 0) { + return fail("unmodeled extensionless archive SDK input", entry); + } else if (macho_magic(bytes) && !non_linkable_macho(entry)) { + return fail("unmodeled or malformed extensionless Mach-O SDK input", entry); + } + continue; // Non-linkable metadata such as framework PkgInfo. + } + if (!add(entry) || (extension == ".tbd" && !read_stub(entry))) return false; + if (sdk && (extension == ".dylib" || extension == ".so")) return fail("unmodeled binary SDK input", entry); + } + } else return fail("SDK entry is not a regular file or directory", entry); + } + active_directories.erase(canonical); + return !error || fail("SDK enumeration failed", path); + } + bool image(const fs::path &path, const fs::path &executable_directory, unsigned depth = 0, + const std::vector &inherited_rpaths = {}, std::vector *effective_rpaths = nullptr) { + if (depth > 32) return fail("tool image dependency depth exceeded 32", path); + if (!add(path)) return false; + std::error_code error; + const auto physical = fs::canonical(path, error); + if (error) return fail("could not resolve tool image", path); + if (!add(physical)) return false; + auto cached = image_descriptions.find(path); + if (cached == image_descriptions.end()) { + const auto description = dependencies(path); + if (!description) return fail("tool image inspection failed", path); + cached = image_descriptions.emplace(path, *description).first; + } + const auto &description = cached->second; + if (physical.parent_path() != path.parent_path()) { + const auto loader_relative = [](const std::string &name) { return name.rfind("@loader_path", 0) == 0; }; + if (std::any_of(description.rpaths.begin(), description.rpaths.end(), loader_relative) || + std::any_of(description.libraries.begin(), description.libraries.end(), loader_relative)) return fail("cross-directory image alias uses loader-relative paths", path); + } + // Compiler identity separately proves the active system dyld image and all + // shared-cache backing bytes. Include an ordinary loader path as well when + // the host exposes one; an alternative LC_LOAD_DYLINKER is never admitted. + if (description.system_loader && regular(host_path("/usr/lib/dyld")) && !add(host_path("/usr/lib/dyld"))) return false; + std::vector rpaths; + for (const auto &name : description.rpaths) { + fs::path selected; + if (name.rfind("@loader_path/", 0) == 0) selected = (physical.parent_path() / name.substr(13)).lexically_normal(); + else if (name.rfind("@executable_path/", 0) == 0) selected = (executable_directory / name.substr(17)).lexically_normal(); + else selected = name; + if (!clean_path(selected) || (!inside(selected, plan.installed.parent_path()) && + !system_install_name(selected.string()))) return fail("tool rpath escapes modeled roots", selected); + rpaths.push_back(selected); + } + rpaths.insert(rpaths.end(), inherited_rpaths.begin(), inherited_rpaths.end()); + std::vector unique_rpaths; + for (const auto &root : rpaths) + if (std::find(unique_rpaths.begin(), unique_rpaths.end(), root) == unique_rpaths.end()) unique_rpaths.push_back(root); + rpaths = std::move(unique_rpaths); + if (effective_rpaths) *effective_rpaths = rpaths; + std::string context = path.string() + '\0' + executable_directory.string(); + for (const auto &root : rpaths) context += '\0' + root.string(); + if (!inspected_contexts.insert(context).second) return true; + if (inspected_contexts.size() > 1024) return fail("tool image contexts exceeded 1024", path); + for (const auto &name : description.libraries) { + std::vector selected; + if (name.rfind("@rpath/", 0) == 0) { + const auto tail = fs::path(name.substr(7)); + if (tail.has_parent_path()) return fail("tool rpath dependency has an unmodeled suffix", name); + // Tool inspection inventories every universal slice (the OS shim may + // be arm64e while the emitted link target is arm64). Its merged rpaths + // cannot safely pick only one winner: include every existing candidate + // so a different architecture/context's first match cannot be hidden. + for (const auto &root : rpaths) { + const auto candidate = root / tail; + if (regular(candidate)) selected.push_back(candidate); + } + // A dylib's install ID is self metadata, not an extra file dependency. + if (selected.empty() && path.filename() == tail && path.extension() == ".dylib") selected.push_back(path); + if (selected.empty()) return fail("tool rpath dependency could not be resolved", name); + } else if (name.rfind("@loader_path/", 0) == 0) { + const auto candidate = (physical.parent_path() / name.substr(13)).lexically_normal(); + if (!inside(candidate, plan.installed.parent_path())) return fail("loader-relative dependency escapes toolchain", candidate); + selected.push_back(candidate); + } else if (system_install_name(name)) { + const auto candidate = host_path(name); + // Actual system images without ordinary files are covered by the + // compiler's complete active shared-cache backing-file superset. + if (regular(candidate)) selected.push_back(candidate); + } else { + const fs::path candidate = name; + if (!clean_path(candidate) || !inside(candidate, plan.installed.parent_path())) return fail("tool dependency escapes modeled roots", candidate); + selected.push_back(candidate); + } + for (const auto &candidate : selected) + if (candidate != path && !image(candidate, executable_directory, depth + 1, rpaths)) return false; + } + return true; + } + bool adjacent_helpers() { + const auto directory = plan.installed.parent_path() / "lib"; + std::error_code error; + fs::directory_iterator iterator(directory, error), end; + if (error) return fail("could not enumerate adjacent toolchain helpers", directory); + for (; iterator != end; iterator.increment(error)) { + if (error || ++entries > 1000000) return fail("adjacent helper enumeration failed or exceeded entry bound", directory); + if (iterator->path().extension() == ".dylib" && !add(iterator->path())) return false; + } + return !error || fail("adjacent helper enumeration failed", directory); + } + bool local_library_inputs() { + if (!plan.local_libraries) return true; + std::error_code error; + // Options.cpp:4380-4429: an explicit absolute -L uses the SDK directory + // when it exists; unlike a standard search directory it otherwise falls + // back to the host. Inspect this choice anew on every collection. + const auto sdk_directory = plan.sdk / "usr/local/lib"; + const bool sdk_directory_exists = fs::is_directory(sdk_directory, error); + if (error && error != std::errc::no_such_file_or_directory) return fail("could not inspect SDK local library directory", sdk_directory); + if (sdk_directory_exists) return true; // Complete SDK tree was collected. + error.clear(); + const auto directory = host_path("/usr/local/lib"); + const bool host_directory_exists = fs::is_directory(directory, error); + if (error && error != std::errc::no_such_file_or_directory) return fail("could not inspect host local library directory", directory); + if (!host_directory_exists) return true; + + // The default command's two -l roots and every architecture/OS SDK + // re-export form a conservative set of names the linker can look up here. + // This is a fresh namespace lookup, not a list cached from the last build: + // additions, removals, aliases and new transitive TBD names are observed. + // Unrelated Homebrew libraries cannot be chosen by these roots; inspecting + // their dyld dependencies would incorrectly model them as executed tools. + std::set candidates = {"libc++.tbd", "libc++.dylib", "libc++.so", "libc++.a", + "libSystem.tbd", "libSystem.dylib", "libSystem.so", "libSystem.a"}; + std::set checked; + for (;;) { + for (const auto &reference : stub_references) { + const auto path = fs::path(reference), leaf = path.filename(); + // Options.cpp:1153-1199 searches frameworks via -F, and never searches + // -L for a .dylib embedded inside a framework. Other indirect library + // lookups use the literal leaf and its .tbd replacement (no lib prefix). + if (reference.find('/' + leaf.string() + ".framework/") != std::string::npos || + (path.extension() == ".dylib" && reference.find(".framework/") != std::string::npos)) continue; + candidates.insert(leaf.string()); + auto stub = leaf; stub.replace_extension(".tbd"); candidates.insert(stub.string()); + } + if (candidates.size() > 131072) return fail("local-library candidate bound exceeded", directory); + bool changed = false; + for (const auto &name : candidates) { + if (!checked.insert(name).second) continue; + changed = true; + const auto path = directory / name; + error.clear(); const auto status = fs::status(path, error); + if (error == std::errc::no_such_file_or_directory || status.type() == fs::file_type::not_found) continue; + if (error || !fs::is_regular_file(status)) return fail("local-library candidate is unreadable or not regular", path); + // A selected ordinary binary/archive requires its own link-input + // dependency closure; don't confuse that with the tools' dyld graph. + // Admit only supported text stubs, including a stub behind an alias + // with a .dylib/extensionless name, and collect its re-exports as well. + if (!add(path) || !read_stub(path)) return false; + } + if (!changed) break; + } + return true; + } + bool direct_library_inputs() { + // Options.cpp:861-873 searches each directory for dylib/TBD, .so, then + // archive before advancing. Only the two command-line -l roots may select + // an archive: supported TBD re-exports name dylibs/frameworks explicitly. + // A selected archive could carry new LC_LINKER_OPTION roots, so require + // captured text interfaces for these two lookups. Unrelated SDK archives + // and archives shadowed by an earlier interface remain harmless inputs. + // Selection precedes architecture parsing: InputFiles.cpp's constructor / + // parseWorkerThread ignores or errors on incompatible inputs, never resumes + // findLibrary. textstub_dylib_file.cpp's architecture fallback reparses the + // same selected interface, so it cannot expose a shadowed archive either. + std::vector directories; + const auto directory = [&](const fs::path &path) -> std::optional { + std::error_code error; + const bool present = fs::is_directory(path, error); + if (error && error != std::errc::no_such_file_or_directory) { + fail("could not inspect direct library search directory", path); return {}; + } + return present; + }; + const auto sdk_local = plan.sdk / "usr/local/lib"; + const auto has_sdk_local = directory(sdk_local); + if (!has_sdk_local) return false; + if (plan.local_libraries) { + if (*has_sdk_local) directories.push_back(sdk_local); + else { + const auto local = host_path("/usr/local/lib"); + const auto present = directory(local); + if (!present) return false; + if (*present) directories.push_back(local); + } + } + // The one-SDK profile omits absent implicit host directories. Explicit -L + // comes first; the implicit SDK order is /usr/lib, then /usr/local/lib. + directories.push_back(plan.sdk / "usr/lib"); + if (*has_sdk_local) directories.push_back(sdk_local); + for (const auto *name : {"c++", "System"}) { + bool selected = false; + for (const auto &root : directories) { + for (const auto *extension : {".tbd", ".dylib", ".so", ".a"}) { + const auto path = root / (std::string("lib") + name + extension); + std::error_code error; + const auto status = fs::status(path, error); + if (error == std::errc::no_such_file_or_directory || status.type() == fs::file_type::not_found) continue; + if (error || !fs::is_regular_file(status)) return fail("direct library candidate is unreadable or not regular", path); + if (path.extension() == ".a") return fail("selected SDK archive has unmodeled autolink dependencies", path); + if (!files_seen.count(path) || !read_stub(path)) return fail("direct library root is not a captured text interface", path); + selected = true; break; + } + if (selected) break; + } + if (!selected) return fail("required direct library text interface was not found", name); + } + return true; + } +public: + collector(fs::path selected_driver, fs::path directory, fs::path selected_runtime, + link_plan selected_plan, probe inspect_image, fs::path host_root = "/", std::string *failure_reason = nullptr) + : driver(std::move(selected_driver)), cwd(std::move(directory)), runtime(std::move(selected_runtime)), + host(std::move(host_root)), plan(std::move(selected_plan)), dependencies(std::move(inspect_image)), reason(failure_reason) {} + + std::optional> files() { + std::error_code error; + const auto actual_driver = fs::canonical(driver, error); + if (error) return refuse("could not resolve selected driver", driver); + const auto actual_compiler = fs::canonical(plan.installed / "clang++", error); + if (error) return refuse("could not resolve selected compiler", plan.installed / "clang++"); + const auto actual_linker = fs::canonical(plan.linker, error); + if (error) return refuse("could not resolve selected linker", plan.linker); + std::vector linker_rpaths; + if (!clean_path(driver) || !clean_path(cwd)) return refuse("driver or working directory is not normalized"); + if (!add(runtime) || !image(driver, actual_driver.parent_path()) || + !image(plan.installed / "clang++", actual_compiler.parent_path()) || + !image(plan.linker, actual_linker.parent_path(), 0, {}, &linker_rpaths) || + !image(plan.lto, actual_linker.parent_path(), 0, linker_rpaths) || !add(plan.compiler_rt)) return {}; + if (!add(plan.sdk / "SDKSettings.json") || !add(host_path("/System/Library/CoreServices/SystemVersion.plist"))) return {}; + if (fs::exists(plan.sdk / "AppleInternal", error) || error) return refuse("AppleInternal SDK profile or lookup failure", plan.sdk); + for (const auto &directory : {plan.sdk / "usr/lib", plan.sdk / "System/Library/Frameworks"}) + if (!tree(directory, true)) return {}; + if (!adjacent_helpers()) return {}; + // With exactly one syslibroot, Apple's linker omits absent standard search + // directories instead of falling back to host /usr/local or /Library. + // https://github.com/apple-oss-distributions/ld64/blob/main/src/ld/Options.cpp + // Keep optional SDK search namespaces in the fresh inventory when present. + for (const auto &relative : {"usr/local/lib", "Library/Frameworks", "System/Library/PrivateFrameworks"}) { + const auto directory = plan.sdk / relative; + const bool present = fs::exists(directory, error); + if (error) return refuse("could not inspect optional SDK directory", directory); + if (present && !tree(directory, true)) return {}; + } + if (!local_library_inputs() || !direct_library_inputs()) return {}; + // Absolute re-export lookup differs from standard -L/-F lookup: when the + // SDK/inlined stub cannot satisfy it, ld can try the raw host pathname. + // Until ordinary host link-input closure is modeled, refuse that profile. + // Re-checking these absences catches a newly installed fallback as well. + for (const auto &name : stub_references) { + const auto relative = fs::path(name).relative_path(); + auto sdk_stub = plan.sdk / relative; sdk_stub.replace_extension(".tbd"); + if (regular(plan.sdk / relative)) { + if (!files_seen.count(plan.sdk / relative)) return refuse("SDK re-export missing from inventory", plan.sdk / relative); + continue; + } + if (regular(sdk_stub)) { + if (!files_seen.count(sdk_stub)) return refuse("SDK re-export stub missing from inventory", sdk_stub); + continue; + } + auto host_stub = host_path(name); host_stub.replace_extension(".tbd"); + if (regular(host_path(name)) || regular(host_stub)) return refuse("unmodeled ordinary host re-export fallback", name); + } + return std::vector(files_seen.begin(), files_seen.end()); + } +}; +} // namespace nilbuild::darwin_toolchain_detail diff --git a/nilbuild/digest_phase.hpp b/nilbuild/digest_phase.hpp new file mode 100644 index 00000000..e2e41fa1 --- /dev/null +++ b/nilbuild/digest_phase.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include "../common/content_digest.hpp" +#include +#include +#include + +namespace nilbuild { +// A single preparation OR verification phase owns this map. Never retain it +// across compilation, analysis refresh, dependency layers, or build commands. +// Only shared compiler/toolchain inputs use it; source dependency checks stay +// independent. Selected path spellings remain identities, including symlinks. +class digest_phase { + std::map values; + std::set excluded; + bool excluded_path(const std::filesystem::path &path) const { + const auto spelling = std::filesystem::absolute(path).lexically_normal().string(); + if (excluded.count(spelling)) return true; + std::error_code error; + const auto canonical = std::filesystem::canonical(path, error); + if (error || excluded.count(canonical.string())) return true; + // Canonicalization resolves symlinks, not hard links. Most shared inputs + // have one link, so avoid a per-source identity scan in that common case. + const auto links = std::filesystem::hard_link_count(path, error); + if (error) return true; + if (links > 1) for (const auto &source : excluded) { + const bool same = std::filesystem::equivalent(path, source, error); + if (same) return true; + // Missing/changed sources are handled by the independent source check; + // don't memoize while their physical identity cannot be compared. + if (error) return true; + } + return false; + } +public: + void exclude(const std::filesystem::path &path) { + excluded.insert(std::filesystem::absolute(path).lexically_normal().string()); + std::error_code error; + const auto canonical = std::filesystem::canonical(path, error); + if (!error) excluded.insert(canonical.string()); + } + void inherit_exclusions(const digest_phase &preparation) { excluded = preparation.excluded; } + template std::string read(const std::filesystem::path &path, Read digest) { + if (excluded_path(path)) return digest(path); + const auto key = std::filesystem::absolute(path).lexically_normal().string(); + const auto found = values.find(key); + if (found != values.end()) return found->second; + return values.emplace(key, digest(path)).first->second; + } + bool contains(const std::string &path) const { return !excluded_path(path) && values.count(path) != 0; } +}; +} // namespace nilbuild diff --git a/nilbuild/main.cpp b/nilbuild/main.cpp index 36ebed6b..2aca39e0 100644 --- a/nilbuild/main.cpp +++ b/nilbuild/main.cpp @@ -5,6 +5,7 @@ #include "../common/tool_discovery.hpp" #include "process.hpp" #include "toolchain_inputs.hpp" +#include "digest_phase.hpp" #include "nil_version.hpp" #include @@ -658,14 +659,19 @@ void check_cancelled() { if (nilbuild::pending_signal) throw build_cancelled{128 + nilbuild::pending_signal}; } using input_map = std::map; -void add_input(input_map &inputs, const fs::path &path) { - inputs[fs::absolute(path).lexically_normal().string()] = nil::detail::file_digest(path); +std::string checked_digest(const fs::path &path) { + return nil::detail::file_digest(path, check_cancelled); +} +void add_input(input_map &inputs, const fs::path &path, nilbuild::digest_phase *phase = nullptr) { + inputs[fs::absolute(path).lexically_normal().string()] = phase ? phase->read(path, checked_digest) : checked_digest(path); check_cancelled(); } -void verify_inputs(const input_map &inputs) { +void verify_inputs(const input_map &inputs, nilbuild::digest_phase *phase = nullptr, + const nilbuild::digest_phase *shared = nullptr) { for (const auto &[path, digest] : inputs) { check_cancelled(); - if (nil::detail::file_digest(path) != digest) + const auto current = phase && shared && shared->contains(path) ? phase->read(path, checked_digest) : checked_digest(path); + if (current != digest) throw std::runtime_error("build input changed during the build; retry: " + path); } } @@ -694,10 +700,11 @@ bool compiler_files_complete(const json &report) { // The compiler tells us which runtime and linker it would select. Query that // driver for startup objects, libraries and its linker rather than guessing a // host installation layout. Missing identities disable reuse, not compilation. -bool native_inputs(input_map &inputs, const json &report, const project &value) { +bool native_inputs(input_map &inputs, const json &report, const project &value, + nilbuild::digest_phase *phase = nullptr, std::string *reason = nullptr) { const auto runtime = report.object.find("nativeRuntime"), linker = report.object.find("nativeLinker"); if (runtime == report.object.end() || runtime->second.type != json::kind::string) return false; - add_input(inputs, runtime->second.text); + add_input(inputs, runtime->second.text, phase); if (linker == report.object.end() || linker->second.type != json::kind::string) return false; std::vector library_paths; const auto reported_paths = report.object.find("nativeLibraryPaths"); @@ -709,9 +716,9 @@ bool native_inputs(input_map &inputs, const json &report, const project &value) } } const auto files = nilbuild::native_toolchain_files(linker->second.text, value.directory, - runtime->second.text, library_paths); + runtime->second.text, library_paths, reason); if (!files) return false; - for (const auto &file : *files) add_input(inputs, file); + for (const auto &file : *files) add_input(inputs, file, phase); return true; } struct build_task { @@ -729,6 +736,13 @@ std::string build_key(const project &value, const build_task &task, const std::v auto description = describe(value); description.object["schema"] = json::str("nil.build.inputs.v1"); description.object["target"] = json::str(task.native ? "native" : "vm"); +#if defined(__APPLE__) +#if defined(__aarch64__) || defined(__arm64__) + description.object["hostArchitecture"] = json::str("arm64"); +#else + description.object["hostArchitecture"] = json::str("x86_64"); +#endif +#endif description.object["inputs"] = json::map(); for (const auto &[path, digest] : *task.inputs) description.object["inputs"].object[path] = json::str(digest); auto environment = json::map(); @@ -737,6 +751,12 @@ std::string build_key(const project &value, const build_task &task, const std::v environment.object[name] = value ? json::str(value) : json{}; } description.object["environment"] = std::move(environment); +#if defined(__APPLE__) + for (const auto *name : {"DEVELOPER_DIR", "TOOLCHAINS"}) { + const char *value = std::getenv(name); + description.object["environment"].object[name] = value ? json::str(value) : json{}; + } +#endif if (task.native && report.object.count("nativeLibraryPaths")) description.object["nativeLibraryPaths"] = report.at("nativeLibraryPaths"); description.object["references"] = json::list(); @@ -798,6 +818,27 @@ int build(const project &root, const options &opts, const fs::path &compiler, co if (!status) status = analyzed; } if (status) return status; + // Darwin's shared cache is several GiB. Hash its complete contents once + // for this preparation phase, shared across ready projects and targets. + // Other hosts retain their existing read behavior. Sources/manifests never + // use this memo, and verification below always starts with an empty one. + nilbuild::digest_phase prepared_shared; + nilbuild::digest_phase *preparation = nullptr; +#if defined(__APPLE__) + // Exclude every source/manifest before any shared input is memoized. A + // compiler report can legitimately overlap inventories; even a symlinked + // source advertised as a module must keep independent fresh checks. + for (const auto &value : graph.projects) { + prepared_shared.exclude(value.file); prepared_shared.exclude(value.entry); + } + for (const auto &report : reports) { + const auto dependencies = report.object.find("dependencies"); + if (dependencies == report.object.end() || dependencies->second.type != json::kind::array) continue; + for (const auto &dependency : dependencies->second.array) + prepared_shared.exclude(string_value(dependency.at("path"), "dependency path")); + } + preparation = &prepared_shared; +#endif std::vector tasks; std::vector commands; for (std::size_t i = 0; i < ready.size(); ++i) { @@ -823,14 +864,19 @@ int build(const project &root, const options &opts, const fs::path &compiler, co for (const bool native : {false, true}) { if ((native && opts.target == "vm") || (!native && opts.target == "native")) continue; build_task task{index, native, native ? value.native_output() : value.vm_output(), sources.value_or(input_map{}), {}, {}, - sources.has_value() && compiler_files_complete(reports[i])}; + sources.has_value() && compiler_files_complete(reports[i]), {}}; if (task.inputs) { - add_input(*task.inputs, value.file); add_input(*task.inputs, value.entry); add_input(*task.inputs, compiler); add_input(*task.inputs, self); + add_input(*task.inputs, value.file); add_input(*task.inputs, value.entry); + add_input(*task.inputs, compiler, preparation); add_input(*task.inputs, self, preparation); if (reports[i].object.count("compilerFiles")) { - for (const auto &file : string_list(reports[i].at("compilerFiles"), "compilerFiles")) add_input(*task.inputs, file); + for (const auto &file : string_list(reports[i].at("compilerFiles"), "compilerFiles")) add_input(*task.inputs, file, preparation); } if (native) { - if (!native_inputs(task.native_files, reports[i], value)) task.cacheable = false; + std::string reason; + if (!native_inputs(task.native_files, reports[i], value, preparation, &reason)) { + task.cacheable = false; + if (!reason.empty()) std::cerr << "nilbuild: native reuse unavailable: " << reason << '\n'; + } task.inputs->insert(task.native_files.begin(), task.native_files.end()); } } @@ -864,6 +910,12 @@ int build(const project &root, const options &opts, const fs::path &compiler, co const auto compiled = nilbuild::execute_many(commands, opts.jobs); for (const auto &result : compiled) std::cerr << result.output << result.error; if (const auto failed = failed_batch(compiled)) return failed; + nilbuild::digest_phase verified_shared; + nilbuild::digest_phase *verification = nullptr; +#if defined(__APPLE__) + verified_shared.inherit_exclusions(prepared_shared); + verification = &verified_shared; +#endif // Re-resolve the graph after a compilation before publishing cacheable // artifacts. This catches new shadowing files as well as modified inputs. for (std::size_t i = 0; i < ready.size(); ++i) { @@ -885,7 +937,7 @@ int build(const project &root, const options &opts, const fs::path &compiler, co compiler_module_difference(reports[i], current)); for (const auto &task : tasks) if (task.project_index == index && task.native && task.cacheable) { input_map current_native; - if (!native_inputs(current_native, current, value) || current_native != task.native_files || + if (!native_inputs(current_native, current, value, verification) || current_native != task.native_files || (current.object.count("nativeLibraryPaths") && (!reports[i].object.count("nativeLibraryPaths") || nilbuild::dump(current.at("nativeLibraryPaths")) != @@ -897,7 +949,7 @@ int build(const project &root, const options &opts, const fs::path &compiler, co const auto temporary = task.staging->path / task.output.filename(); if (!fs::is_regular_file(fs::symlink_status(temporary))) throw std::runtime_error("compiler succeeded without creating a regular output artifact"); if (task.native && !executable(temporary)) throw std::runtime_error("compiler native artifact is not executable"); - if (task.inputs) verify_inputs(*task.inputs); + if (task.inputs) verify_inputs(*task.inputs, verification, preparation); protect_inputs(graph.projects[task.project_index]); } check_cancelled(); diff --git a/nilbuild/toolchain_inputs.hpp b/nilbuild/toolchain_inputs.hpp index 8524cdcf..2132f87c 100644 --- a/nilbuild/toolchain_inputs.hpp +++ b/nilbuild/toolchain_inputs.hpp @@ -2,6 +2,7 @@ #include "process.hpp" #include "windows_toolchain_inputs.hpp" +#include "darwin_toolchain_inputs.hpp" #include #include @@ -11,6 +12,9 @@ #include #include #include +#if defined(__APPLE__) +#include +#endif namespace nilbuild { struct process_cancelled : std::exception { @@ -237,7 +241,8 @@ class collector { inline std::optional> native_toolchain_files( const std::filesystem::path &driver, const std::filesystem::path &directory, const std::filesystem::path &runtime = {}, - const std::vector &library_paths = {}) { + const std::vector &library_paths = {}, std::string *reason = nullptr) { + if (reason) reason->clear(); #if defined(__linux__) (void)runtime; (void)library_paths; return toolchain_detail::collector(driver, directory).files(); @@ -258,6 +263,93 @@ inline std::optional> native_toolchain_files( }; return windows_toolchain_detail::collector(driver, directory, runtime, library_paths, lib ? lib : "", inspect).files(); +#elif defined(__APPLE__) + const auto unavailable = [&](std::string message) -> std::optional> { + darwin_toolchain_detail::explain(reason, std::move(message)); return {}; + }; + // nilbuild currently forwards no -l/--link options. Expanding that command + // surface requires expanding this closure before admitting those projects. + if (!library_paths.empty() || runtime.empty()) return unavailable("custom library paths or missing native runtime"); + bool local_library_environment = false; + for (char **entry = *_NSGetEnviron(); entry && *entry; ++entry) { + const std::string item(*entry); + const auto equal = item.find('='); + if (equal == std::string::npos || equal + 1 == item.size()) continue; + const auto name = item.substr(0, equal); + if (name == "LIBRARY_PATH") { + // This exact search profile is modeled below and already appears in the + // build key. Empty components/other directories are intentionally not + // inferred or sanitized; they require a broader search closure. + if (item.substr(equal + 1) != "/usr/local/lib") return unavailable("unmodeled LIBRARY_PATH search profile"); + local_library_environment = true; continue; + } + if (name.rfind("DYLD_", 0) == 0 || name.rfind("LD_", 0) == 0 || name.rfind("CCC_", 0) == 0 || + name.rfind("CLANG_CONFIG_", 0) == 0 || name == "COMPILER_PATH" || + name == "GCC_EXEC_PREFIX" || name == "CPATH" || name == "CPLUS_INCLUDE_PATH" || + name == "C_INCLUDE_PATH" || name == "OBJC_INCLUDE_PATH" || name == "ARCHFLAGS" || + name == "IPHONEOS_DEPLOYMENT_TARGET" || name == "TVOS_DEPLOYMENT_TARGET" || + name == "WATCHOS_DEPLOYMENT_TARGET" || name == "OTOOL_PATH") return unavailable("unmodeled tool-selection environment variable: " + name); + } + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(120); + unsigned queries = 0; + const auto query = [&](const std::vector &command, bool driver_plan = false) + -> std::optional { + const auto now = std::chrono::steady_clock::now(); + if (++queries > 128 || now >= deadline) { + darwin_toolchain_detail::explain(reason, "toolchain inspection exceeded query or time budget"); return {}; + } + process_options options; + options.close_stdin = true; options.cleanup_descendants = true; + options.time_limit = std::min(std::chrono::milliseconds(15000), + std::chrono::duration_cast(deadline - now)); + options.output_limit = 4 * 1024 * 1024; options.total_output_limit = 4 * 1024 * 1024; + const auto result = execute_many({{command, directory}}, 1, options).front(); + if (result.cancelled) throw process_cancelled(result.status); + if (result.status || result.timed_out || result.output_limited || (!driver_plan && !result.error.empty())) { + darwin_toolchain_detail::explain(reason, "toolchain query failed (status=" + std::to_string(result.status) + + ", timeout=" + std::to_string(result.timed_out) + ", output_limit=" + std::to_string(result.output_limited) + + "): " + command.front() + (driver_plan ? " [driver link plan]" : " " + command.back()) + + (result.error.empty() ? "" : "; stderr=" + result.error.substr(0, 512))); + return {}; + } + return driver_plan ? result.output + result.error : result.output; + }; + const auto output = query({driver.string(), "-###", "-pthread", "-o", "/dev/null", runtime.string(), + "-framework", "Cocoa", "-framework", "CoreGraphics"}, true); + if (!output) return {}; + const auto plan = darwin_toolchain_detail::parse_plan(*output, runtime); + if (!plan) return unavailable("selected Apple driver link plan is outside the supported profile"); + if (local_library_environment && !plan->local_libraries) return unavailable("LIBRARY_PATH was not represented by the modeled link plan"); +#if defined(__aarch64__) || defined(__arm64__) + if (plan->architecture != "arm64") return unavailable("link plan architecture differs from running ARM64 host"); +#elif defined(__x86_64__) + if (plan->architecture != "x86_64") return unavailable("link plan architecture differs from running x86-64 host"); +#else + return unavailable("unsupported running host architecture"); +#endif + // Archives can carry hidden -l/-framework directives in object load commands. + // Keep that unsupported until their names join the modeled search roots. + for (const auto &archive : {runtime, plan->compiler_rt}) { + const auto commands = query({"/usr/bin/otool", "-arch", plan->architecture, "-l", archive.string()}); + if (!commands) return {}; + if (!darwin_toolchain_detail::supported_archive_commands(*commands)) + return unavailable("native archive has unmodeled autolinks or empty inspection: " + archive.string()); + } + const auto inspect = [&](const std::filesystem::path &image) + -> std::optional { + const auto kinds = darwin_toolchain_detail::macho_file_types(image); + if (!kinds || !std::all_of(kinds->begin(), kinds->end(), [](std::uint32_t kind) { return kind == 2 || kind == 6; })) { + darwin_toolchain_detail::explain(reason, "unsupported or ambiguous Mach-O tool slices: " + image.string()); + return {}; + } + const auto libraries = query({"/usr/bin/otool", "-arch", "all", "-L", image.string()}); + const auto commands = query({"/usr/bin/otool", "-arch", "all", "-l", image.string()}); + if (!libraries || !commands) return {}; + const auto description = darwin_toolchain_detail::parse_image(image, *libraries, *commands); + if (!description) darwin_toolchain_detail::explain(reason, "unsupported otool image/load-command output: " + image.string()); + return description; + }; + return darwin_toolchain_detail::collector(driver, directory, runtime, *plan, inspect, "/", reason).files(); #else (void)driver; (void)directory; (void)runtime; (void)library_paths; return std::nullopt; diff --git a/nilcompiler/CMakeLists.txt b/nilcompiler/CMakeLists.txt index 73733a5e..b29a9cda 100644 --- a/nilcompiler/CMakeLists.txt +++ b/nilcompiler/CMakeLists.txt @@ -45,8 +45,8 @@ set(NILCOMPILER_CORE_SOURCES src/executable_context.cpp ) -# One desktop provider per platform: GTK is the Linux (and macOS) provider, -# Win32 is the Windows provider. Both implement the same ui.v1 contract and +# One desktop provider per platform: GTK on Linux, Win32 on Windows, and +# AppKit on macOS. All implement the same ui.v1 contract and # share its toolkit-independent half through src/ui_runtime_shared.hpp, so # exactly one of them is ever compiled into the runtime. if(WIN32) @@ -210,6 +210,11 @@ if(NILCOMPILER_POSTGRESQL_INCLUDE_DIR) endif() add_executable(nilcompiler src/main.cpp) +if(APPLE) + target_sources(nilcompiler PRIVATE src/compiler_module_fingerprint_darwin.cpp) + set_source_files_properties(src/compiler_module_fingerprint_darwin.cpp PROPERTIES + COMPILE_OPTIONS "-fblocks") +endif() if(WIN32) target_sources(nilcompiler PRIVATE ../nillinker/process_runner.cpp) target_link_libraries(nilcompiler PRIVATE advapi32) diff --git a/nilcompiler/src/ast.hpp b/nilcompiler/src/ast.hpp index b6f96621..c0102783 100644 --- a/nilcompiler/src/ast.hpp +++ b/nilcompiler/src/ast.hpp @@ -198,6 +198,7 @@ enum class ast_node_kind { hotreload_expr_node, // hotreload.method(args) - intrinsic hot reload if_expr_node, // if cond then expr else expr end if - conditional expression query_expr_node, // eager collect query over a checked array + markup_expr_node, // markup literal: a checked document with holes // Ny testing nodes test_block_node, @@ -336,6 +337,7 @@ struct ast_node::ast_visitor { virtual void visit(struct ast_hotreload_expr &e) {} virtual void visit(struct ast_if_expr &e) {} virtual void visit(struct ast_query_expr &e) {} + virtual void visit(struct ast_markup_expr &e) {} }; enum class visibility_kind { @@ -1205,6 +1207,36 @@ struct ast_query_expr : public ast_expr { void accept(ast_node::ast_visitor &v) override { v.visit(*this); } }; +// A markup literal (NIL-0027). The block is parsed as markup at compile time, +// which is what makes its structure checkable and lets each hole learn the +// context it landed in; what survives into the AST is a flat program of +// literal output, escaped holes and control flow, because that is all the +// serialized result needs. Escaping is chosen here, never by the author, and +// a hole that is already a document is spliced rather than escaped again. +struct ast_markup_item { + enum class item_kind { + literal, hole, when_begin, else_when, else_arm, end_when, repeat_begin, + end_repeat + }; + item_kind kind = item_kind::literal; + std::string text; // literal output, already serialized + ast_expr_ptr expr; // hole value, condition, or repeat source + std::string escape; // text | attribute | url | url_component | raw + std::string binding; // repeat row name + std::string element_type; // checked repeat element type + bool splice = false; // checked: the hole is already a document + int line = 0; +}; + +struct ast_markup_expr : public ast_expr { + std::vector items; + bool checked_markup = false; + + explicit ast_markup_expr(int line = 0) + : ast_expr(ast_node_kind::markup_expr_node, line) {} + void accept(ast_node::ast_visitor &v) override { v.visit(*this); } +}; + // Index expression: array[index] struct ast_index_expr : public ast_expr { ast_expr_ptr object; diff --git a/nilcompiler/src/codegen.cpp b/nilcompiler/src/codegen.cpp index 219ef77b..bde58c9e 100644 --- a/nilcompiler/src/codegen.cpp +++ b/nilcompiler/src/codegen.cpp @@ -3214,6 +3214,148 @@ class emitter { // Queries lower to ordinary typed NIL arrays and loops. Row expressions are // evaluated here in the enclosing frame, so captures require no closures. + // A markup literal builds its serialized document in one text local. The + // literal segments are constants decided at compile time; only the holes and + // the control flow cost anything at run time, and each hole carries the + // escape its position earned during parsing. + void emit_markup(const ast_markup_expr &markup) { + if (!markup.checked_markup) { + cg_.reject_codegen_construct(markup.line, + "a markup block requires checked holes and nil.std.markup", + "enable type checking and read nil.std.markup"); + return; + } + using item_kind = ast_markup_item::item_kind; + const int line = markup.line; + const auto push = [&](const std::string &op, std::vector args = {}, + int at = 0) { push_instr(op, args, at ? at : line); }; + const auto result = new_temp("markup.result"); + push("PUSH_CONST_STR", {get_or_add_const_string_name("")}); + push("STORE_LOCAL", {result}); + + struct when_frame { std::string next, end; }; + struct repeat_frame { std::string rows, index, count, top, done; }; + std::vector whens; + std::vector repeats; + std::vector> scopes; + + for (const auto &item : markup.items) { + switch (item.kind) { + case item_kind::literal: { + push("LOAD_LOCAL", {result}, item.line); + push("PUSH_CONST_STR", {get_or_add_const_string_name(item.text)}, item.line); + push("STR_CONCAT", {}, item.line); + push("STORE_LOCAL", {result}, item.line); + break; + } + case item_kind::hole: { + if (!item.expr) break; + push("LOAD_LOCAL", {result}, item.line); + emit_expr(*item.expr); + if (item.splice) { + push("GET_FIELD", {"content"}, item.line); + } else { + const auto expr_type = scalar_text_type(substitute_nil_type_name( + item.expr->inferred_type.value_or(""), substitutions)); + if (!supports_scalar_text(expr_type)) { + cg_.reject_codegen_construct(item.expr->line, + "a markup hole requires a checked scalar, text or document", + "enable type checking and call an explicit formatter for other types"); + break; + } + if (expr_type != "text") + push("TO_STRING", {expr_type}, item.line); + if (item.escape != "raw") + push("CALL", {"nil__std__markup__escape_" + item.escape, "1"}, item.line); + } + push("STR_CONCAT", {}, item.line); + push("STORE_LOCAL", {result}, item.line); + break; + } + case item_kind::when_begin: { + when_frame frame{new_label("L_markup_next"), new_label("L_markup_when_end")}; + if (item.expr) emit_expr(*item.expr); + push("JMP_IF_NOT", {frame.next}, item.line); + whens.push_back(frame); + break; + } + case item_kind::else_when: { + if (whens.empty()) break; + push("JMP", {whens.back().end}, item.line); + push("LABEL", {whens.back().next}, item.line); + whens.back().next = new_label("L_markup_next"); + if (item.expr) emit_expr(*item.expr); + push("JMP_IF_NOT", {whens.back().next}, item.line); + break; + } + case item_kind::else_arm: { + if (whens.empty()) break; + push("JMP", {whens.back().end}, item.line); + push("LABEL", {whens.back().next}, item.line); + whens.back().next.clear(); + break; + } + case item_kind::end_when: { + if (whens.empty()) break; + if (!whens.back().next.empty()) + push("LABEL", {whens.back().next}, item.line); + push("LABEL", {whens.back().end}, item.line); + whens.pop_back(); + break; + } + case item_kind::repeat_begin: { + repeat_frame frame{new_temp("markup.rows"), new_temp("markup.index"), + new_temp("markup.count"), new_label("L_markup_row"), + new_label("L_markup_rows_done")}; + if (item.expr) emit_expr(*item.expr); + push("STORE_LOCAL", {frame.rows}, item.line); + push("LOAD_LOCAL", {frame.rows}, item.line); + push("LEN", {}, item.line); + push("CAST", {"i64"}, item.line); + push("STORE_LOCAL", {frame.count}, item.line); + push("PUSH_CONST", {"0"}, item.line); + push("CAST", {"i64"}, item.line); + push("STORE_LOCAL", {frame.index}, item.line); + push("LABEL", {frame.top}, item.line); + push("LOAD_LOCAL", {frame.index}, item.line); + push("LOAD_LOCAL", {frame.count}, item.line); + push("CMP_LT", {}, item.line); + push("JMP_IF_NOT", {frame.done}, item.line); + scopes.push_back(std::make_unique(*this)); + declare_local(item.binding, item.element_type); + push("LOAD_LOCAL", {frame.rows}, item.line); + push("LOAD_LOCAL", {frame.index}, item.line); + push("GET_INDEX", {}, item.line); + push("STORE_LOCAL", {item.binding}, item.line); + repeats.push_back(frame); + break; + } + case item_kind::end_repeat: { + if (repeats.empty()) break; + const auto frame = repeats.back(); + repeats.pop_back(); + if (!scopes.empty()) scopes.pop_back(); + push("LOAD_LOCAL", {frame.index}, item.line); + push("PUSH_CONST", {"1"}, item.line); + push("CAST", {"i64"}, item.line); + push("ADD", {"i64"}, item.line); + push("STORE_LOCAL", {frame.index}, item.line); + push("JMP", {frame.top}, item.line); + push("LABEL", {frame.done}, item.line); + break; + } + } + } + // The result is a document, not text: the type is what keeps an already + // escaped fragment from being escaped a second time when it is composed. + const auto content = new_temp("markup.document"); + push("LOAD_LOCAL", {result}); + push("STORE_LOCAL", {content}); + push("NEW_STRUCT", {"nil::std::markup::document"}); + push("LOAD_LOCAL", {content}); + push("SET_FIELD", {"content"}); + } + void emit_query(const ast_query_expr &query) { if (!query.checked_query || !query.source || !query.projection) { cg_.reject_codegen_construct(query.line, @@ -3445,6 +3587,9 @@ class emitter { case ast_node_kind::query_expr_node: emit_query(static_cast(e)); break; + case ast_node_kind::markup_expr_node: + emit_markup(static_cast(e)); + break; case ast_node_kind::array_literal_expr_node: { const auto &arr = static_cast(e); for (const auto &elem : arr.elements) { diff --git a/nilcompiler/src/compiler_module_fingerprint.hpp b/nilcompiler/src/compiler_module_fingerprint.hpp index d951ce0f..44113c60 100644 --- a/nilcompiler/src/compiler_module_fingerprint.hpp +++ b/nilcompiler/src/compiler_module_fingerprint.hpp @@ -20,6 +20,10 @@ namespace nil::detail::compiler_modules { namespace fs = std::filesystem; using files = std::set; +#if defined(__APPLE__) +// Defined in the Darwin-only translation unit (-fblocks). +bool collect_darwin(files &observed); +#endif // Linux file-backed mappings are a separate identity graph from source inputs. // Keep valid observations on failure; callers may still protect/hash them, but diff --git a/nilcompiler/src/compiler_module_fingerprint_darwin.cpp b/nilcompiler/src/compiler_module_fingerprint_darwin.cpp new file mode 100644 index 00000000..c8d6fbe3 --- /dev/null +++ b/nilcompiler/src/compiler_module_fingerprint_darwin.cpp @@ -0,0 +1,282 @@ +#include "compiler_module_fingerprint.hpp" +#include "compiler_module_fingerprint_darwin.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace nil::detail::compiler_modules { +namespace { +using namespace darwin; +constexpr std::size_t maximum_commands = 65536; +bool read_self(std::uint64_t address, void *destination, std::size_t size) { + if (!address || size > maximum_commands || address > UINT64_MAX - size) return false; + mach_vm_size_t copied = 0; + return mach_vm_read_overwrite(mach_task_self(), address, size, + reinterpret_cast(destination), &copied) == KERN_SUCCESS && copied == size; +} +loaded_image inspect(const mach_header *pointer, std::int64_t slide, bool derive_slide = false) { + loaded_image row; + row.address = reinterpret_cast(pointer); + row.slide = slide; + mach_header_64 header{}; + require(read_self(row.address, &header, sizeof(header)) && header.magic == MH_MAGIC_64 && + header.ncmds && header.ncmds <= 1024 && header.sizeofcmds <= maximum_commands && + header.ncmds <= header.sizeofcmds / sizeof(load_command)); + row.cpu = static_cast(header.cputype); row.flags = header.flags; + std::array commands{}; + require(read_self(end(row.address, sizeof(header)), commands.data(), header.sizeofcmds)); + std::size_t offset = 0; + bool text = false, identifier = false; + for (std::uint32_t i = 0; i < header.ncmds; ++i) { + require(offset <= header.sizeofcmds && sizeof(load_command) <= header.sizeofcmds - offset); + load_command command{}; + std::memcpy(&command, commands.data() + offset, sizeof(command)); + require(command.cmdsize >= sizeof(command) && command.cmdsize % 8 == 0 && command.cmdsize <= header.sizeofcmds - offset); + if (command.cmd == LC_UUID) { + require(!identifier && command.cmdsize == sizeof(uuid_command)); + uuid_command item{}; std::memcpy(&item, commands.data() + offset, sizeof(item)); + std::copy(std::begin(item.uuid), std::end(item.uuid), row.id.begin()); + identifier = true; + } + if (command.cmd == LC_SEGMENT_64) { + require(command.cmdsize >= sizeof(segment_command_64)); + segment_command_64 item{}; std::memcpy(&item, commands.data() + offset, sizeof(item)); + require(item.nsects <= (command.cmdsize - sizeof(item)) / sizeof(section_64) && + sizeof(item) + item.nsects * sizeof(section_64) == command.cmdsize); + end(item.vmaddr, item.vmsize); + if (std::strncmp(item.segname, "__TEXT", sizeof(item.segname)) == 0) { + require(!text && item.vmsize); + const auto actual_slide = image_slide(row.address, item.vmaddr); + if (derive_slide) row.slide = actual_slide; + else require(actual_slide == row.slide); + row.text_size = item.vmsize; text = true; + } + } + offset += command.cmdsize; + } + require(text && offset == header.sizeofcmds); + // An ordinary executable built without LC_UUID is still covered by its full + // file digest. Cached images must carry a matching UUID in attribute(). + return row; +} +struct image_state { + std::mutex mutex; + std::vector rows; + std::uint64_t generation = 0; + bool failed = false; +}; +// dyld callbacks cannot be unregistered and may run during process teardown. +image_state &state() { static auto *value = new image_state; return *value; } +void added(const mach_header *header, intptr_t slide) { + auto &owner = state(); + try { + auto row = inspect(header, slide); + std::lock_guard lock(owner.mutex); + ++owner.generation; + if (owner.rows.size() >= 4096) owner.failed = true; + else owner.rows.push_back(std::move(row)); + } catch (...) { + std::lock_guard lock(owner.mutex); + ++owner.generation; owner.failed = true; + } +} +void removed(const mach_header *header, intptr_t) { + auto &owner = state(); + std::lock_guard lock(owner.mutex); + ++owner.generation; + const auto address = reinterpret_cast(header); + owner.rows.erase(std::remove_if(owner.rows.begin(), owner.rows.end(), + [&](const auto &row) { return row.address == address; }), owner.rows.end()); +} +template Function symbol(const char *name) { + dlerror(); void *pointer = dlsym(RTLD_DEFAULT, name); + require(pointer && !dlerror()); + return reinterpret_cast(pointer); +} +using opaque = void *; +struct api { + opaque (*create)() = symbol("dyld_process_create_for_current_task"); + void (*dispose)(opaque) = symbol("dyld_process_dispose"); + opaque (*snapshot)(opaque, int *) = symbol("dyld_process_snapshot_create_for_process"); + void (*free_snapshot)(opaque) = symbol("dyld_process_snapshot_dispose"); + opaque (*cache)(opaque) = symbol("dyld_process_snapshot_get_shared_cache"); + void (*cache_uuid)(opaque, unsigned char(*)[16]) = symbol("dyld_shared_cache_copy_uuid"); + std::uint64_t (*base)(opaque) = symbol("dyld_shared_cache_get_base_address"); + std::uint64_t (*size)(opaque) = symbol("dyld_shared_cache_get_mapped_size"); + bool (*private_mapping)(opaque) = symbol("dyld_shared_cache_is_mapped_private"); + void (*files)(opaque, void(^)(const char *)) = symbol("dyld_shared_cache_for_each_file"); +}; +struct snapshot { + observation active; + std::vector paths; +}; +snapshot capture(const api &functions, opaque process) { + int error = -1; + std::unique_ptr owner(functions.snapshot(process, &error), functions.free_snapshot); + require(owner && !error); + const auto cache = functions.cache(owner.get()); + require(cache && !functions.private_mapping(cache)); + snapshot result; + unsigned char id[16]{}; + functions.cache_uuid(cache, &id); + std::copy(std::begin(id), std::end(id), result.active.cache_id.begin()); + result.active.base = functions.base(cache); result.active.size = functions.size(cache); + __block bool complete = true; + // A pointer capture avoids copying the mutable C++ owner into the block. + auto *paths = &result.paths; + functions.files(cache, ^(const char *path) { + if (!path || path[0] != '/' || strnlen(path, 4096) == 4096 || paths->size() >= maximum_files) { complete = false; return; } + try { paths->emplace_back(path); } catch (...) { complete = false; } + }); + require(complete && !result.paths.empty()); + std::sort(result.paths.begin(), result.paths.end()); + require(std::adjacent_find(result.paths.begin(), result.paths.end()) == result.paths.end()); + return result; +} +dyld_all_image_infos loader() { + task_dyld_info_data_t task{}; + mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; + require(task_info(mach_task_self(), TASK_DYLD_INFO, reinterpret_cast(&task), &count) == KERN_SUCCESS && + count == TASK_DYLD_INFO_COUNT && task.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_64 && + task.all_image_info_size >= offsetof(dyld_all_image_infos, dyldPath) + sizeof(const char *)); + dyld_all_image_infos info{}; + require(read_self(task.all_image_info_addr, &info, offsetof(dyld_all_image_infos, dyldPath) + sizeof(const char *)) && + info.version >= 15 && info.dyldImageLoadAddress && info.sharedCacheBaseAddress && + info.infoArray && info.infoArrayCount && info.infoArrayCount <= 4096); + return info; +} +bool same_file(const struct stat &a, const struct stat &b) { + return a.st_dev == b.st_dev && a.st_ino == b.st_ino && a.st_size == b.st_size && + a.st_mtimespec.tv_sec == b.st_mtimespec.tv_sec && a.st_mtimespec.tv_nsec == b.st_mtimespec.tv_nsec && + a.st_ctimespec.tv_sec == b.st_ctimespec.tv_sec && a.st_ctimespec.tv_nsec == b.st_ctimespec.tv_nsec; +} +void retain_readable(files &observed, const std::string &path) { + if (path.empty() || path.front() != '/' || path.find('\0') != std::string::npos) return; + std::error_code error; + if (fs::is_regular_file(fs::u8path(path), error) && !error && + static_cast(std::ifstream(fs::u8path(path), std::ios::binary))) observed.insert(path); +} +} // namespace + +bool collect_darwin(files &observed) { + try { + // Preserve independently known ordinary inputs even on an older host + // without the optional shared-cache APIs. This inventory alone never + // certifies completeness; cached virtual install names are not files. + const auto ordinary_count = _dyld_image_count(); + require(ordinary_count <= 4096); + for (std::uint32_t i = 0; i < ordinary_count; ++i) { + const auto *path = _dyld_get_image_name(i); + if (path && strnlen(path, 4096) < 4096) retain_readable(observed, path); + } + api functions; + std::unique_ptr process(functions.create(), functions.dispose); + require(static_cast(process)); + // Resolve/prewarm optional API and digest machinery before observing loader + // generations. Their libraries are themselves part of the resulting set. + CC_SHA256_CTX digest{}; require(CC_SHA256_Init(&digest) == 1); + (void)capture(functions, process.get()); + static std::once_flag registration; + std::call_once(registration, [] { + (void)state(); + _dyld_register_func_for_add_image(added); + _dyld_register_func_for_remove_image(removed); + }); + auto before = capture(functions, process.get()); + const auto loaded = loader(); + require(loaded.sharedCacheBaseAddress == before.active.base && + std::equal(std::begin(loaded.sharedCacheUUID), std::end(loaded.sharedCacheUUID), before.active.cache_id.begin())); + before.active.slide = loaded.sharedCacheSlide; + before.active.loader = reinterpret_cast(loaded.dyldImageLoadAddress); + std::uint64_t generation = 0; + { + auto &owner = state(); + std::lock_guard lock(owner.mutex); + require(!owner.failed && owner.rows.size() == loaded.infoArrayCount); + before.active.images = owner.rows; generation = owner.generation; + } + if (std::none_of(before.active.images.begin(), before.active.images.end(), + [&](const auto &row) { return row.address == before.active.loader; })) + before.active.images.push_back(inspect(loaded.dyldImageLoadAddress, 0, true)); + for (auto &row : before.active.images) { + Dl_info named{}; + if (dladdr(reinterpret_cast(row.address), &named) && + reinterpret_cast(named.dli_fbase) == row.address && named.dli_fname && + strnlen(named.dli_fname, 4096) < 4096) row.ordinary_path = named.dli_fname; + if (row.ordinary_path.empty() && row.address == before.active.loader && loaded.dyldPath && + strnlen(loaded.dyldPath, 4096) < 4096) row.ordinary_path = loaded.dyldPath; + retain_readable(observed, row.ordinary_path); + } + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(15); + std::size_t remaining = 8U * 1024 * 1024; + std::vector caches; + std::set> backing_ids; + for (const auto &path : before.paths) { + const int descriptor = open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NONBLOCK); + require(descriptor >= 0); + struct close_file { int value; ~close_file() { close(value); } } cleanup{descriptor}; + struct stat first{}, last{}, named{}; + require(!fstat(descriptor, &first) && S_ISREG(first.st_mode) && first.st_size > 0 && + static_cast(first.st_size) <= maximum_bytes && backing_ids.emplace(first.st_dev, first.st_ino).second); + // A readable API-reported backing file remains protected even if a + // later metadata check is unsupported or another backing file is absent. + observed.insert(path); + const reader read = [&](std::uint64_t offset, std::size_t count) { + require(std::chrono::steady_clock::now() < deadline && count <= remaining); + end(offset, count, static_cast(first.st_size)); + remaining -= count; + bytes data(count); + std::size_t copied = 0; + while (copied < count) { + require(std::chrono::steady_clock::now() < deadline); + const auto size = pread(descriptor, data.data() + copied, count - copied, static_cast(offset + copied)); + if (size < 0 && errno == EINTR) continue; + require(size > 0); copied += static_cast(size); + } + return data; + }; + const auto prefix = read(0, 104); + caches.push_back(parse(path, static_cast(first.st_size), read, + identity(prefix, 88) == before.active.cache_id)); + require(!fstat(descriptor, &last) && !stat(path.c_str(), &named) && same_file(first, last) && same_file(first, named)); + } +#if defined(__aarch64__) || defined(__arm64__) + constexpr std::uint32_t cpu = 0x0100000cU; +#else + constexpr std::uint32_t cpu = 0x01000007U; +#endif + const auto paths = attribute(before.active, caches, cpu); + for (const auto &path : paths) { + std::error_code error; + require(fs::is_regular_file(fs::u8path(path), error) && !error && static_cast(std::ifstream(fs::u8path(path), std::ios::binary))); + } + const auto after = capture(functions, process.get()); + const auto loaded_after = loader(); + require(after.paths == before.paths && after.active.cache_id == before.active.cache_id && + after.active.base == before.active.base && after.active.size == before.active.size && + loaded_after.infoArrayChangeTimestamp == loaded.infoArrayChangeTimestamp && + loaded_after.sharedCacheBaseAddress == loaded.sharedCacheBaseAddress && + loaded_after.sharedCacheSlide == loaded.sharedCacheSlide); + { + auto &owner = state(); std::lock_guard lock(owner.mutex); + require(!owner.failed && owner.generation == generation); + } + observed.insert(paths.begin(), paths.end()); + return true; + } catch (...) { + return false; + } +} +} // namespace nil::detail::compiler_modules diff --git a/nilcompiler/src/compiler_module_fingerprint_darwin.hpp b/nilcompiler/src/compiler_module_fingerprint_darwin.hpp new file mode 100644 index 00000000..b9bb6051 --- /dev/null +++ b/nilcompiler/src/compiler_module_fingerprint_darwin.hpp @@ -0,0 +1,241 @@ +#pragma once + +// Bounded, portable parsing and attribution for the Darwin module collector. +// UUIDs select backing files; the builder hashes their entire contents. Neither +// UUIDs nor timestamps are persistent cache identities. +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace nil::detail::compiler_modules::darwin { +using uuid = std::array; +using bytes = std::vector; +constexpr std::size_t maximum_images = 16384, maximum_files = 128; +constexpr std::uint64_t maximum_bytes = 8ULL * 1024 * 1024 * 1024; +inline void require(bool condition) { if (!condition) throw std::runtime_error("incomplete Darwin module identity"); } +inline std::uint64_t end(std::uint64_t begin, std::uint64_t size, + std::uint64_t limit = UINT64_MAX) { + require(begin <= limit && size <= limit - begin); + return begin + size; +} +inline std::uint64_t number(const bytes &data, std::size_t offset, std::size_t width) { + require(width <= 8 && offset <= data.size() && width <= data.size() - offset); + std::uint64_t value = 0; + for (std::size_t i = 0; i < width; ++i) value |= std::uint64_t(data[offset + i]) << (8 * i); + return value; +} +inline uuid identity(const bytes &data, std::size_t offset) { + require(offset <= data.size() && 16 <= data.size() - offset); + uuid value{}; + std::copy_n(data.begin() + offset, value.size(), value.begin()); + require(value != uuid{}); + return value; +} +struct mapping { + std::uint64_t address = 0, size = 0, offset = 0; + std::uint32_t maximum = 0, initial = 0; +}; +struct subcache { uuid id{}; std::uint64_t offset = 0; std::string suffix; }; +struct cache_image { uuid id{}; std::uint64_t address = 0, size = 0; }; +struct cache_file { + std::string path, magic; + uuid id{}; + std::uint64_t size = 0, base = 0, region_size = 0; + std::vector mappings; + std::vector children; + std::vector images; +}; +using reader = std::function; +inline void disjoint(std::vector> ranges) { + std::sort(ranges.begin(), ranges.end()); + for (std::size_t i = 0; i < ranges.size(); ++i) { + const auto finish = end(ranges[i].first, ranges[i].second); + require(ranges[i].second != 0 && (i + 1 == ranges.size() || finish <= ranges[i + 1].first)); + } +} +inline cache_file parse(const std::string &path, std::uint64_t size, + const reader &read, bool image_table = false) { + require(!path.empty() && path.front() == '/' && path.find('\0') == std::string::npos && size && size <= maximum_bytes); + const auto first = read(0, 24); + require(first.size() == 24); + std::string magic(first.begin(), first.begin() + 16); + while (!magic.empty() && (magic.back() == '\0' || magic.back() == ' ')) magic.pop_back(); + require(magic == "dyld_v1 arm64e" || magic == "dyld_v1 arm64" || + magic == "dyld_v1 x86_64" || magic == "dyld_v1 x86_64h"); + const auto header_size = number(first, 16, 4), count = number(first, 20, 4); + require(header_size >= 400 && header_size <= 4096 && count > 0 && count <= maximum_files); + const auto header = read(0, static_cast(header_size)); + require(header.size() == header_size); + cache_file result; + result.path = path; result.magic = magic; result.size = size; + result.id = identity(header, 88); + result.base = number(header, 224, 8); result.region_size = number(header, 232, 8); + end(result.base, result.region_size); + const auto table = [&](std::uint64_t offset, std::uint64_t entries, std::size_t stride, + std::size_t maximum = maximum_files) { + require(entries <= maximum && entries <= UINT64_MAX / stride); + end(offset, entries * stride, size); + auto data = read(offset, static_cast(entries * stride)); + require(data.size() == entries * stride); + return data; + }; + const auto mappings = table(header_size, count, 32); + std::vector> addresses, offsets; + for (std::size_t i = 0; i < count; ++i) { + const auto at = i * 32; + mapping item{number(mappings, at, 8), number(mappings, at + 8, 8), number(mappings, at + 16, 8), + static_cast(number(mappings, at + 24, 4)), static_cast(number(mappings, at + 28, 4))}; + require(item.size && !(item.initial & ~item.maximum)); + end(item.offset, item.size, size); + addresses.emplace_back(item.address, item.size); offsets.emplace_back(item.offset, item.size); + result.mappings.push_back(item); + } + disjoint(addresses); disjoint(offsets); + const auto slide_count = number(header, 316, 4); + require(slide_count == count); + const auto slides = table(number(header, 312, 4), slide_count, 56); + for (std::size_t i = 0; i < count; ++i) { + const auto at = i * 56; + const auto &item = result.mappings[i]; + require(item.address == number(slides, at, 8) && item.size == number(slides, at + 8, 8) && + item.offset == number(slides, at + 16, 8) && item.maximum == number(slides, at + 48, 4) && + item.initial == number(slides, at + 52, 4)); + end(number(slides, at + 24, 8), number(slides, at + 32, 8), size); + } + const bool modern = header_size > 456; + const auto children_count = number(header, 396, 4); + const std::size_t stride = modern ? 56 : 24; + const auto children = table(number(header, 392, 4), children_count, stride); + std::set ids; + std::set suffixes; + std::set child_offsets; + for (std::size_t i = 0; i < children_count; ++i) { + subcache child{identity(children, i * stride), number(children, i * stride + 16, 8), {}}; + if (modern) { + const auto begin = children.begin() + i * stride + 24; + const auto finish = std::find(begin, begin + 32, 0); + require(finish != begin + 32); + child.suffix.assign(begin, finish); + require(child.suffix.size() > 1 && child.suffix.front() == '.' && child.suffix.back() != '.' && + child.suffix.find("..") == std::string::npos && + child.suffix.find_first_not_of(".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") == std::string::npos); + } else child.suffix = "." + std::to_string(i + 1); + require(child.id != result.id && child.offset && ids.insert(child.id).second && + suffixes.insert(child.suffix).second && child_offsets.insert(child.offset).second); + result.children.push_back(std::move(child)); + } + end(number(header, 40, 8), number(header, 48, 8), size); + end(number(header, 72, 8), number(header, 80, 8), size); + if (image_table) { + const auto image_count = number(header, 144, 8); + require(image_count > 0); + const auto images = table(number(header, 136, 8), image_count, 32, maximum_images); + std::set image_addresses; + for (std::size_t i = 0; i < image_count; ++i) { + cache_image image{identity(images, i * 32), number(images, i * 32 + 16, 8), number(images, i * 32 + 24, 4)}; + require(image.size && image_addresses.insert(image.address).second); + end(image.address, image.size); + result.images.push_back(image); + } + } + return result; +} +struct loaded_image { + uuid id{}; + std::uint64_t address = 0, text_size = 0; + std::int64_t slide = 0; + std::uint32_t cpu = 0, flags = 0; + std::string ordinary_path; +}; +inline std::int64_t image_slide(std::uint64_t address, std::uint64_t unslid) { + if (address >= unslid) { + require(address - unslid <= static_cast(INT64_MAX)); + return static_cast(address - unslid); + } + const auto magnitude = unslid - address; + require(magnitude <= (std::uint64_t{1} << 63)); + return magnitude == (std::uint64_t{1} << 63) ? INT64_MIN : -static_cast(magnitude); +} +struct observation { + uuid cache_id{}; + std::uint64_t base = 0, size = 0, slide = 0, loader = 0; + std::vector images; +}; +// This attribution is equivalent to Linux/Windows loaded-path identity, not +// vnode attestation. The whole selected files remain subject to content hashing +// and the builder's independent post-compilation graph/content verification. +inline std::set attribute(const observation &active, + const std::vector &caches, + std::uint32_t cpu) { + require(!caches.empty() && caches.size() <= maximum_files && !active.images.empty() && active.images.size() <= 4096); + const cache_file *root = nullptr; + std::map by_path; + std::set cache_ids; + std::uint64_t total = 0; + for (const auto &file : caches) { + require(by_path.emplace(file.path, &file).second && cache_ids.insert(file.id).second); + total = end(total, file.size, maximum_bytes); + if (file.id == active.cache_id) { require(!root); root = &file; } + } + require(root && root->children.size() + 1 == caches.size() && root->base && + root->region_size == active.size && end(root->base, active.slide) == active.base); + const auto first_mapping = [](const cache_file &file) { + require(!file.mappings.empty()); + return std::min_element(file.mappings.begin(), file.mappings.end(), + [](const auto &a, const auto &b) { return a.address < b.address; })->address; + }; + require(first_mapping(*root) == root->base && !root->images.empty()); + for (const auto &child : root->children) { + const auto found = by_path.find(root->path + child.suffix); + require(found != by_path.end()); + const auto &file = *found->second; + require(file.id == child.id && file.children.empty() && file.magic == root->magic && + first_mapping(file) == end(root->base, child.offset)); + } + std::vector> ranges; + for (const auto &file : caches) for (const auto &map : file.mappings) { + require(map.address >= root->base); + end(map.address - root->base, map.size, root->region_size); + ranges.emplace_back(map.address, map.size); + } + disjoint(ranges); + std::map images; + for (const auto &image : root->images) require(images.emplace(image.address, &image).second); + std::set result; + for (const auto &file : caches) result.insert(file.path); + std::set seen; + bool loader_seen = false, cached = false; + const bool arm = root->magic.find("arm64") != std::string::npos; + require(cpu == (arm ? 0x0100000cU : 0x01000007U)); + for (const auto &image : active.images) { + require(image.cpu == cpu && image.address && image.text_size && seen.insert(image.address).second); + loader_seen |= image.address == active.loader; + const mapping *mapped = nullptr; + for (const auto &file : caches) for (const auto &map : file.mappings) { + const auto begin = end(map.address, active.slide); + if (image.address >= begin && image.address < end(begin, map.size)) { require(!mapped); mapped = ↦ } + } + if (mapped) { + require(image.address >= active.slide && image.slide >= 0 && static_cast(image.slide) == active.slide && (image.flags & 0x80000000U)); + const auto unslid = image.address - active.slide; + const auto found = images.find(unslid); + require(found != images.end() && found->second->id == image.id && found->second->size == image.text_size && + (mapped->initial & 5) == 5 && end(unslid, image.text_size) <= end(mapped->address, mapped->size)); + cached = true; + } else { + require(!(image.address >= active.base && image.address < end(active.base, active.size))); + require(!image.ordinary_path.empty() && image.ordinary_path.front() == '/' && image.ordinary_path.find('\0') == std::string::npos); + result.insert(image.ordinary_path); + } + } + require(loader_seen && cached); + return result; +} +} // namespace nil::detail::compiler_modules::darwin diff --git a/nilcompiler/src/entropy_support.hpp b/nilcompiler/src/entropy_support.hpp new file mode 100644 index 00000000..7e670287 --- /dev/null +++ b/nilcompiler/src/entropy_support.hpp @@ -0,0 +1,248 @@ +#ifndef NIL_ENTROPY_SUPPORT_HPP +#define NIL_ENTROPY_SUPPORT_HPP + +// Cryptographically secure random bytes for the NIL runtime. +// +// Before this, nothing in the standard library could produce an unpredictable +// value. `nil.std.math` is MINSTD — a 31-bit linear congruential generator +// whose global stream starts at the seed 12345 — and says in its own comments +// that it is not cryptographic; two draws reveal the whole stream. +// `nil.std.security` had base64 and hex and no random at all. /dev/urandom was +// not a way round it either: the file intrinsics refuse a character device by +// design, and the unchecked read returns nothing for one. +// +// That gap is not an inconvenience, it is a vulnerability generator. Session +// identifiers, CSRF tokens, password-reset links and nonces are all bearer +// credentials, and a credential drawn from MINSTD is a credential anyone can +// compute. Any library that needed one had to either invent something +// predictable or refuse to offer the feature. +// +// This asks the operating system and does nothing clever. There is no +// userspace pool to get wrong, no seeding, no state, and no fallback to a +// weak source: if the kernel cannot provide entropy, the answer is an error, +// because silently returning something guessable is how this goes wrong. + +#include +#include +#include +#include + +#if defined(_WIN32) +#ifndef NOMINMAX +#define NOMINMAX +#endif +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#include +#else +#include +#include +#include +#include +#if defined(__linux__) +#include +#elif defined(__APPLE__) +#include +#endif +#endif + +namespace ny::entropy_support { + +/** + * The most one call may ask for. + * + * A key is 32 bytes and a generous token is 64. A request for a megabyte of + * entropy is a program using the wrong tool — a stream cipher seeded from + * here is the right one — and the ceiling makes that a readable error instead + * of a quiet stall on a machine whose pool is cold. + */ +constexpr std::int64_t max_random_bytes = 1024; + +struct Drawn { + std::string bytes; + std::string error; + bool ok() const { return error.empty(); } +}; + +#if !defined(_WIN32) + +/** + * Fill from the kernel. + * + * getrandom(2) first, because it needs no file descriptor and so cannot fail + * because the process ran out of them or because something unmounted /dev. + * A short return is retried: the call is documented to give fewer bytes than + * asked for when a signal arrives, and treating that as failure would make an + * unrelated SIGWINCH break a token. + */ +inline bool fill_from_kernel(unsigned char *into, std::size_t count, + std::string &error) { +#if defined(__linux__) && defined(SYS_getrandom) + std::size_t filled = 0; + bool syscall_usable = true; + while (filled < count) { + const long taken = ::syscall(SYS_getrandom, into + filled, count - filled, 0); + if (taken > 0) { + filled += static_cast(taken); + continue; + } + if (taken < 0 && errno == EINTR) + continue; + // ENOSYS means a kernel older than the call; anything else here is real, + // but the device is still worth trying before giving up on the machine. + syscall_usable = false; + break; + } + if (syscall_usable && filled == count) + return true; +#elif defined(__APPLE__) + // Darwin supplies getentropy without opening a device. Each call accepts at + // most 256 bytes; a failed OS request never falls back to a userspace PRNG. + std::size_t filled = 0; + while (filled < count) { + const std::size_t chunk = count - filled > 256 ? 256 : count - filled; + if (::getentropy(into + filled, chunk) == 0) { + filled += chunk; + continue; + } + if (errno == EINTR) continue; + error = "random: no entropy source is available on this system"; + return false; + } + return true; +#endif + + // The device, for a kernel without the syscall. Opened and closed per call: + // this is not a hot path, and holding a descriptor open for the life of the + // process is a descriptor a program did not ask the runtime to keep. + std::FILE *device = std::fopen("/dev/urandom", "rb"); + if (device == nullptr) { + error = "random: no entropy source is available on this system"; + return false; + } + const std::size_t taken = std::fread(into, 1, count, device); + std::fclose(device); + if (taken != count) { + error = "random: the entropy source returned fewer bytes than requested"; + return false; + } + return true; +} + +#else + +inline bool fill_from_kernel(unsigned char *into, std::size_t count, + std::string &error) { + // Resolve the system DLL explicitly. This avoids a CRT include-order + // dependency and does not add a loader search through the working directory. + using Generate = NTSTATUS (WINAPI *)(BCRYPT_ALG_HANDLE, PUCHAR, ULONG, ULONG); + static const Generate generate = []() -> Generate { + const HMODULE library = ::LoadLibraryExW(L"bcrypt.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + if (!library) return nullptr; + const auto result = reinterpret_cast(::GetProcAddress(library, "BCryptGenRandom")); + if (!result) ::FreeLibrary(library); + // Keep a successful module loaded for the lifetime of the function pointer. + return result; + }(); + if (count > (std::numeric_limits::max)() || !generate || + generate(nullptr, into, static_cast(count), BCRYPT_USE_SYSTEM_PREFERRED_RNG) < 0) { + error = "random: no entropy source is available on this system"; + return false; + } + return true; +} + +#endif + +inline Drawn random_bytes(std::int64_t count) { + Drawn drawn; + if (count < 1) { + drawn.error = "random: ask for at least one byte"; + return drawn; + } + if (count > max_random_bytes) { + drawn.error = "random: a single draw is limited to " + + std::to_string(max_random_bytes) + " bytes"; + return drawn; + } + std::string buffer(static_cast(count), '\0'); + if (!fill_from_kernel(reinterpret_cast(buffer.data()), + buffer.size(), drawn.error)) + return drawn; + drawn.bytes = std::move(buffer); + return drawn; +} + +/** + * The same bytes as lower-case hex. + * + * Here rather than left to the caller because the caller is often a Matrix + * page, and Matrix cannot index the byte array this would otherwise return. + * A session id wants to be text anyway, and hex is the encoding that survives + * a cookie, a URL and a log line without further escaping. + */ +inline Drawn random_hex(std::int64_t count) { + Drawn drawn = random_bytes(count); + if (!drawn.ok()) + return drawn; + static const char digits[] = "0123456789abcdef"; + std::string out; + out.reserve(drawn.bytes.size() * 2); + for (const unsigned char byte : drawn.bytes) { + out.push_back(digits[(byte >> 4) & 0x0F]); + out.push_back(digits[byte & 0x0F]); + } + drawn.bytes = std::move(out); + return drawn; +} + +/** + * A uniform number in [0, bound). + * + * By rejection, not by remainder. Taking a 64-bit draw modulo the bound makes + * the low values more likely whenever the bound does not divide 2^64, and for + * a small bound the bias is small enough to never be noticed and large enough + * to matter to anything counting on uniformity. + */ +struct Chosen { + std::int64_t value = 0; + std::string error; + bool ok() const { return error.empty(); } +}; + +inline Chosen random_below(std::int64_t bound) { + Chosen chosen; + if (bound < 1) { + chosen.error = "random: the bound must be at least one"; + return chosen; + } + const std::uint64_t span = static_cast(bound); + // The largest multiple of the span that fits; draws at or above it are + // thrown away so every remaining draw maps to exactly one result. + const std::uint64_t limit = std::uint64_t(-1) - (std::uint64_t(-1) % span) - 1; + + // Bounded attempts: with the worst-case span just over half the range, the + // chance of thirty-two rejections in a row is below one in four billion, so + // reaching the end of this loop means the entropy source is not random. + for (int attempt = 0; attempt < 32; ++attempt) { + Drawn drawn = random_bytes(8); + if (!drawn.ok()) { + chosen.error = drawn.error; + return chosen; + } + std::uint64_t raw = 0; + for (int byte = 0; byte < 8; ++byte) + raw = (raw << 8) | static_cast(drawn.bytes[static_cast(byte)]); + if (raw <= limit) { + chosen.value = static_cast(raw % span); + return chosen; + } + } + chosen.error = "random: the entropy source did not yield a usable draw"; + return chosen; +} + +} // namespace ny::entropy_support +#endif diff --git a/nilcompiler/src/file_time_support.hpp b/nilcompiler/src/file_time_support.hpp new file mode 100644 index 00000000..0d36d8e0 --- /dev/null +++ b/nilcompiler/src/file_time_support.hpp @@ -0,0 +1,89 @@ +#pragma once + +#include "../../common/strict_json.hpp" +#include +#include +#include +#include + +#ifdef _WIN32 +#include "../../common/windows_environment.hpp" +#else +#include +#endif + +namespace ny::file_time_support { + +// Floor to Unix milliseconds, including times before 1970. No clock readings +// participate in this conversion: unchanged metadata has an unchanged result. +inline std::optional from_unix_parts(std::int64_t seconds, + std::int64_t nanoseconds) { + if (nanoseconds < 0 || nanoseconds >= 1000000000) return std::nullopt; + const auto fraction = static_cast(nanoseconds / 1000000); + constexpr auto maximum = static_cast( + (std::numeric_limits::max)()); + if (seconds >= 0) { + const auto whole = static_cast(seconds); + if (whole > maximum / 1000) return std::nullopt; + const auto base = whole * 1000; + if (fraction > maximum - base) return std::nullopt; + return static_cast(base + fraction); + } + const auto magnitude = static_cast(-(seconds + 1)) + 1; + constexpr auto minimum_magnitude = maximum + 1; + if (magnitude > (minimum_magnitude + 999) / 1000) return std::nullopt; + const auto result = magnitude * 1000 - fraction; + if (result > minimum_magnitude) return std::nullopt; + if (result == minimum_magnitude) return (std::numeric_limits::min)(); + return -static_cast(result); +} + +// FILETIME is an unsigned count of 100 ns ticks since 1601. The epoch offset +// is exactly divisible by a millisecond, so division before subtraction also +// floors negative Unix timestamps correctly and cannot overflow an i64. +inline std::int64_t from_windows_ticks(std::uint64_t ticks) { + constexpr std::int64_t epoch_milliseconds = 11644473600000LL; + return static_cast(ticks / 10000) - epoch_milliseconds; +} + +// Follow symlinks as last_write_time did, and retain directory timestamps. +// Text paths are strict UTF-8 on every host; never pass an embedded NUL to the +// OS. This API intentionally returns no path-bearing host diagnostic. +inline std::optional modified_at(const std::string &path) { + if (path.empty() || path.find('\0') != std::string::npos) return std::nullopt; + try { + nil::detail::valid_utf8(path); +#ifdef _WIN32 + const auto wide = nil::detail::environment_utf16(path); + if (!wide) return std::nullopt; + const HANDLE file = CreateFileW(wide->c_str(), FILE_READ_ATTRIBUTES, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, + OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); + if (file == INVALID_HANDLE_VALUE) return std::nullopt; + FILETIME written{}; + const bool obtained = GetFileTime(file, nullptr, nullptr, &written) != FALSE; + const bool closed = CloseHandle(file) != FALSE; + if (!obtained || !closed) return std::nullopt; + const auto ticks = (static_cast(written.dwHighDateTime) << 32) | + written.dwLowDateTime; + return from_windows_ticks(ticks); +#else + struct stat state{}; + if (::stat(path.c_str(), &state) != 0) return std::nullopt; +#ifdef __APPLE__ + const auto written = state.st_mtimespec; +#else + const auto written = state.st_mtim; +#endif + static_assert(std::numeric_limits::is_signed && + std::numeric_limits::digits <= 63, + "file timestamps require a signed at-most-64-bit time_t"); + return from_unix_parts(static_cast(written.tv_sec), + static_cast(written.tv_nsec)); +#endif + } catch (...) { + return std::nullopt; + } +} + +} // namespace ny::file_time_support diff --git a/nilcompiler/src/formatted_text.hpp b/nilcompiler/src/formatted_text.hpp index 1aff2053..a2022cce 100644 --- a/nilcompiler/src/formatted_text.hpp +++ b/nilcompiler/src/formatted_text.hpp @@ -52,6 +52,26 @@ inline formatted_text_scan scan_formatted_text(const std::string &source, bool matrix, unsigned depth = 0); +// A multiline formatted block. The caller has already removed the block's +// common indentation and joined its lines, so 'content' is the logical text +// and every piece offset is an offset into it; the caller maps those back to +// original source coordinates, which is what keeps a hole's tokens editable. +// A block has no closing quote: its terminator is a line-anchored keyword the +// caller consumed, so a newline is ordinary literal text here. Literal bytes +// are never decoded, matching the raw block the form extends. +inline formatted_text_scan scan_formatted_block(const std::string &content, + bool matrix, + unsigned depth = 0); + +// A markup block's content (NIL-0027). The hole rules are the block's, with +// one exception: inside a raw-text element a brace is an ordinary character, +// because CSS and JavaScript are full of them and an author must be able to +// paste either one unaltered. A raw-text element whose body is exactly one +// hole still interpolates, which is how a generated script or stylesheet is +// spliced in. +inline formatted_text_scan scan_markup_content(const std::string &content, + bool matrix, unsigned depth = 0); + inline std::size_t formatted_expression_end(const std::string &source, std::size_t begin, bool matrix, unsigned depth, @@ -224,5 +244,215 @@ inline formatted_text_scan scan_formatted_text(const std::string &source, return result; } +inline formatted_text_scan scan_formatted_block(const std::string &content, + bool matrix, unsigned depth) { + formatted_text_scan result; + result.end = content.size(); + const auto fail = [&](std::size_t at, const std::string &message) { + result.error_offset = at; + result.error = message; + }; + if (depth >= 64) { + fail(0, "interpolation nesting exceeds 64 levels"); + return result; + } + std::size_t cursor = 0; + std::size_t literal_start = 0; + std::string literal; + const auto flush = [&]() { + if (!literal.empty()) + result.pieces.push_back({false, std::move(literal), literal_start, + cursor - literal_start}); + literal.clear(); + }; + while (cursor < content.size()) { + const char c = content[cursor]; + if (c == '{') { + if (cursor + 1 < content.size() && content[cursor + 1] == '{') { + literal += '{'; + cursor += 2; + continue; + } + flush(); + const auto begin = cursor + 1; + const auto end = formatted_expression_end(content, begin, matrix, depth + 1, result); + if (!result.error.empty()) return result; + result.pieces.push_back({true, content.substr(begin, end - begin), begin, end - begin}); + cursor = end + 1; + literal_start = cursor; + continue; + } + if (c == '}') { + if (cursor + 1 < content.size() && content[cursor + 1] == '}') { + literal += '}'; + cursor += 2; + continue; + } + fail(cursor, "unmatched '}' in formatted text; write '}}' for a literal brace"); + return result; + } + literal += c; + ++cursor; + } + flush(); + return result; +} + +// The byte ranges of raw-text element bodies, in order. Only 'script' and +// 'style' are raw-text elements in HTML; an unterminated one is left to the +// markup parser to report, so scanning stops at the end of the content. +struct markup_raw_region { + std::size_t begin = 0, end = 0; + bool single_hole = false; +}; + +inline bool markup_name_follows(const std::string &content, std::size_t at, + const char *name) { + std::size_t index = 0; + for (; name[index]; ++index) { + if (at + index >= content.size()) return false; + char letter = content[at + index]; + if (letter >= 'A' && letter <= 'Z') letter += 'a' - 'A'; + if (letter != name[index]) return false; + } + const auto after = at + index; + if (after >= content.size()) return true; + const unsigned char next = static_cast(content[after]); + return std::isspace(next) || next == '/' || next == '>'; +} + +inline std::vector markup_raw_regions(const std::string &content, + bool matrix, unsigned depth) { + std::vector regions; + // A marker inside a comment, quoted attribute, or interpolation expression + // is not an element. Walk literal tags before looking for raw-text bodies; + // otherwise a harmless attribute such as title=" +

{{literal braces}} and {raw "trusted"}

+ + + end markup +end paragraph + +main returns override integer begin paragraph + value names as array of text is ["alpha", "b", "gamma"] + value signed is page with signed-in being true, who being "Na", + names being names, query being "a b&c", target being "/dashboard" + say nil.std.markup::render with doc being signed + value guest is page with signed-in being false, who being "guest", + names being [], query being "x", target being "javascript:alert(1)" + say nil.std.markup::render with doc being guest + say nil.std.markup::render with doc being (nil.std.markup::empty with nothing) + give 0 +end paragraph diff --git a/tests/language/markup-matrix.stdout b/tests/language/markup-matrix.stdout new file mode 100644 index 00000000..a9a62b62 --- /dev/null +++ b/tests/language/markup-matrix.stdout @@ -0,0 +1,57 @@ + + Report & Review + +
+

Pro & Con

+

3 items

+
+ +
    + +
  • alpha
  • + +
  • b<ad>
  • + +
  • gamma
  • + +
+ target + search +
+ + + + +

{literal braces} and trusted

+ + + + Report & Review + +
+

Pro & Con

+

0 items

+
+ +
    + +
+ target + search +
+ + + + +

{literal braces} and trusted

+ + + diff --git a/tests/language/markup.ny b/tests/language/markup.ny new file mode 100644 index 00000000..c617d880 --- /dev/null +++ b/tests/language/markup.ny @@ -0,0 +1,54 @@ +import nil.std.markup.escaping + +# A component: an ordinary function returning a document, so that composing +# pages does not go back through text. +fn card(title: text, count: i32) -> document + return $markup +
+

{title}

+

{count} items

+
+ end markup +end fn + +fn page(signed_in: bool, who: text, names: array, query: text, target: text) -> document + return $markup + + Report & Review + + {card("Pro & Con", names.len() as i32)} + +
    + {for name in names} +
  • {name}
  • + {end for} +
+ target + search +
+ + + + +

{{literal braces}} and {raw "trusted"}

+ + + end markup +end fn + +fn main() -> i32 + let names = ["alpha", "b", "gamma"] + println(nil.std.markup::render(page(true, "Na", names, "a b&c", "/dashboard"))) + let empty: array = [] + println(nil.std.markup::render(page(false, "guest", empty, "x", "javascript:alert(1)"))) + println(nil.std.markup::render(nil.std.markup::empty())) + return 0 +end fn diff --git a/tests/language/markup.stdout b/tests/language/markup.stdout new file mode 100644 index 00000000..a9a62b62 --- /dev/null +++ b/tests/language/markup.stdout @@ -0,0 +1,57 @@ + + Report & Review + +
+

Pro & Con

+

3 items

+
+ +
    + +
  • alpha
  • + +
  • b<ad>
  • + +
  • gamma
  • + +
+ target + search +
+ + + + +

{literal braces} and trusted

+ + + + Report & Review + +
+

Pro & Con

+

0 items

+
+ +
    + +
+ target + search +
+ + + + +

{literal braces} and trusted

+ + + diff --git a/tests/language/reject-unbacked-io-security.ny b/tests/language/reject-unbacked-io-security.ny index 4cf2d6d6..0a02becb 100644 --- a/tests/language/reject-unbacked-io-security.ny +++ b/tests/language/reject-unbacked-io-security.ny @@ -3,11 +3,20 @@ module ny.tests.reject_unbacked_io_security import ny.lang.io.* import ny.lang.security.* +# A declaration with no runtime provider must be rejected at compile time +# rather than accepted and then failing at the point of use. Compression, +# hashing and encryption are still designs under experimental/ with no +# intrinsic behind them, so each of these is undefined. +# +# `random_bytes` was on this list until nil.std.security.random gained an +# entropy intrinsic. It is now backed, and the fixture that covers it is +# tests/matrix/execution/entropy.matrix. It was removed from here rather than +# left to fail, because the thing this test guards is the gap between a +# declaration and a provider, and for that call there is no longer a gap. fn main() -> i32 let bytes: vec = vec.new() gzip_compress(bytes) sha256("data") - random_bytes(16) aes_encrypt(bytes, bytes) return 0 end fn diff --git a/tests/language/reject-unbacked-io-security.stderr b/tests/language/reject-unbacked-io-security.stderr index 938258ce..fb6a16d1 100644 --- a/tests/language/reject-unbacked-io-security.stderr +++ b/tests/language/reject-unbacked-io-security.stderr @@ -1,5 +1,4 @@ Type checking failed: - {{SOURCE}}:8: Undefined identifier 'gzip_compress' - {{SOURCE}}:9: Undefined identifier 'sha256' - {{SOURCE}}:10: Undefined identifier 'random_bytes' - {{SOURCE}}:11: Undefined identifier 'aes_encrypt' + {{SOURCE}}:18: Undefined identifier 'gzip_compress' + {{SOURCE}}:19: Undefined identifier 'sha256' + {{SOURCE}}:20: Undefined identifier 'aes_encrypt' diff --git a/tests/matrix/execution/byte-access.expected b/tests/matrix/execution/byte-access.expected new file mode 100644 index 00000000..d3972138 --- /dev/null +++ b/tests/matrix/execution/byte-access.expected @@ -0,0 +1,13 @@ +65 +A +-1 +-1 +2 +195 +169 +2 +1 +1 +255 +0 +0 diff --git a/tests/matrix/execution/byte-access.matrix b/tests/matrix/execution/byte-access.matrix new file mode 100644 index 00000000..126e6ab3 --- /dev/null +++ b/tests/matrix/execution/byte-access.matrix @@ -0,0 +1,52 @@ +!!! Byte access to text. +!!! +!!! nil.std.text slices and measures by byte, and until text.byte_at and +!!! text.from_byte existed there was no way to read a byte as a number or +!!! write one as text. The conversion that looked like it would do it, +!!! convert.char_to_i32, takes a whole Unicode scalar and aborts the process +!!! on a partial one, so a byte inside a multi-byte character was unreachable +!!! and so was percent-encoding anything outside ASCII. +!!! +!!! What this page pins down is that these two are about bytes and not about +!!! characters, which is the whole reason they were added. + +book matrix.byte-access + +read nil.std.core +read nil.std.text.string_utils + +main returns number begin paragraph + !!! An ASCII byte reads as its code and writes back as itself. + say nil.std.text::byte_at with s being "A", index being 0 + say nil.std.text::from_byte with code being 65 + + !!! Out of range answers rather than raising, so a scan can discover the + !!! end by reading past it. + say nil.std.text::byte_at with s being "A", index being 1 + say nil.std.text::byte_at with s being "A", index being 0 minus 1 + + !!! Two bytes built one at a time make one character, because joining text + !!! joins bytes. This is the case convert.i32_to_char cannot express: it + !!! would give the two bytes that encode U+00C3 for a request for 195. + value one-character is (nil.std.text::from_byte with code being 195) + plus (nil.std.text::from_byte with code being 169) + say nil.std.text::length with s being one-character + say nil.std.text::byte_at with s being one-character, index being 0 + say nil.std.text::byte_at with s being one-character, index being 1 + + !!! The same character written as a scalar is the same two bytes, which is + !!! what proves the byte route and the scalar route agree. + value as-scalar is nil.std.core::Convert.i32_to_char with code being 233 + say nil.std.text::length with s being as-scalar + say (as-scalar equals one-character) becomes text + + !!! Every byte value is reachable, including zero and the high half that + !!! is not valid UTF-8 on its own. + say nil.std.text::length with s being (nil.std.text::from_byte with code being 0) + say nil.std.text::byte_at with s being (nil.std.text::from_byte with code being 255), index being 0 + + !!! Outside the byte range there is nothing to give. + say nil.std.text::length with s being (nil.std.text::from_byte with code being 256) + say nil.std.text::length with s being (nil.std.text::from_byte with code being 0 minus 1) + give 0 +end paragraph diff --git a/tests/matrix/execution/byte-access.needs-stdlib b/tests/matrix/execution/byte-access.needs-stdlib new file mode 100644 index 00000000..e69de29b diff --git a/tests/matrix/execution/entropy.expected b/tests/matrix/execution/entropy.expected new file mode 100644 index 00000000..7a0531b2 --- /dev/null +++ b/tests/matrix/execution/entropy.expected @@ -0,0 +1,10 @@ +32 +64 +-1 +-1 +1 +1 +8 +1 +0 +refused diff --git a/tests/matrix/execution/entropy.matrix b/tests/matrix/execution/entropy.matrix new file mode 100644 index 00000000..6b81168b --- /dev/null +++ b/tests/matrix/execution/entropy.matrix @@ -0,0 +1,112 @@ +!!! Cryptographically secure random values. +!!! +!!! nil.std.math is MINSTD, seeded at 12345, and says itself that it is not +!!! cryptographic. Before nil.std.security's random intrinsics there was no +!!! alternative anywhere in the standard library, and /dev/urandom was no way +!!! round it: the file intrinsics refuse a character device by design. Any +!!! library needing a session id, a token or a nonce had to either invent +!!! something predictable or drop the feature. +!!! +!!! Nothing here can assert a particular value, which is the point. What it +!!! asserts is the shape, the bounds, and that two draws differ. The last is +!!! only a constant-output sanity check; it does not prove cryptographic quality. + +book matrix.entropy + +read nil.std.core +read nil.std.text.string_utils +read nil.std.security.random + +!!! Reports a Result as a width, or minus one if it failed, so the +!!! expected output does not depend on the bytes drawn. +hex-width receives byte-count as number returns number begin paragraph + value drawn is nil.std.security::random_hex with byte_count being byte-count + consider drawn + when Result.Ok holding token then give nil.std.text::length with s being token + otherwise give 0 minus 1 + end consider +end paragraph + +hex-of receives byte-count as number returns text begin paragraph + value drawn is nil.std.security::random_hex with byte_count being byte-count + consider drawn + when Result.Ok holding token then give token + otherwise give "" + end consider +end paragraph + +!!! Whether every character is a lower-case hex digit. A generator that +!!! returned raw bytes through the hex door would fail here. +all-hex receives token as text returns boolean begin paragraph + value width is nil.std.text::length with s being token + when width is less than 1 then + give false + end when + variable index as number is 0 + repeat while index is less than width + value code is nil.std.text::byte_at with s being token, index being index + value is-digit is code is at least 48 and code is at most 57 + value is-lower is code is at least 97 and code is at most 102 + when not (is-digit or is-lower) then + give false + end when + set index to index plus 1 + end repeat + give true +end paragraph + +main returns number begin paragraph + !!! Two hex characters per byte, which is what makes the width predictable + !!! even though the content is not. + say hex-width with byte-count being 16 + say hex-width with byte-count being 32 + + !!! The bounds are refused rather than clamped: a caller asking for zero + !!! bytes of entropy has a bug, and so has one asking for a megabyte. + say hex-width with byte-count being 0 + say hex-width with byte-count being 1025 + + value token is hex-of with byte-count being 16 + say (all-hex with token being token) becomes text + + !!! The check that matters. Two draws from a secure source differ; two + !!! draws from a counter or a seeded LCG at a fixed start do not. + say (not ((hex-of with byte-count being 16) equals (hex-of with byte-count being 16))) becomes text + + !!! Raw bytes come back as text and are read by byte, not by character. + value raw is nil.std.security::random_bytes with byte_count being 8 + consider raw + when Result.Ok holding bytes then say nil.std.text::length with s being bytes + otherwise say 0 minus 1 + end consider + + !!! A bounded draw lands inside its bound, every time. + variable inside as boolean is true + variable round as number is 0 + repeat while round is less than 50 + value picked is nil.std.security::random_below with bound being 10 + consider picked + when Result.Ok holding amount then begin block + when amount is less than 0 or amount is at least 10 then + set inside to false + end when + end block + otherwise set inside to false + end consider + set round to round plus 1 + end repeat + say inside becomes text + + !!! A bound of one has exactly one answer, and a bound below one is a bug. + value only is nil.std.security::random_below with bound being 1 + consider only + when Result.Ok holding amount then say amount + otherwise say 0 minus 1 + end consider + value impossible is nil.std.security::random_below with bound being 0 + consider impossible + when Result.Ok holding amount then say amount + otherwise say "refused" + end consider + give 0 +end paragraph diff --git a/tests/matrix/execution/entropy.needs-stdlib b/tests/matrix/execution/entropy.needs-stdlib new file mode 100644 index 00000000..e69de29b diff --git a/tests/matrix/execution/file-time.expected b/tests/matrix/execution/file-time.expected new file mode 100644 index 00000000..bf318ba7 --- /dev/null +++ b/tests/matrix/execution/file-time.expected @@ -0,0 +1,5 @@ +1 +1 +1 +17 +-1 diff --git a/tests/matrix/execution/file-time.matrix b/tests/matrix/execution/file-time.matrix new file mode 100644 index 00000000..11c409f3 --- /dev/null +++ b/tests/matrix/execution/file-time.matrix @@ -0,0 +1,53 @@ +!!! When a file was last written. +!!! +!!! nil.std.io had a size and no timestamp, which left two ordinary jobs +!!! unreachable: a cache could not tell whether what it held was still +!!! current without re-reading the file, and an HTTP server could not send +!!! Last-Modified or answer If-Modified-Since. +!!! +!!! Nothing here can assert a particular instant. What it asserts is that the +!!! answer is a plausible epoch time in milliseconds, that writing again +!!! moves it forward rather than back, and that an absent file is reported +!!! rather than given a zero that a comparison would read as 1970. + +book matrix.file-time + +read nil.std.core +read nil.std.io.file + +stamp-of receives path as text returns number begin paragraph + value found is nil.std.io::modified_at with path being path + consider found + when Result.Ok holding stamped then give stamped + otherwise give 0 minus 1 + end consider +end paragraph + +main returns number begin paragraph + value path is "matrix-file-time-fixture.txt" + evaluate nil.std.io::write_text with path being path, content being "first" + value first-stamp is stamp-of with path being path + + !!! Milliseconds since the Unix epoch, so any real file is well past the + !!! start of 2020 and well short of the year 2200. A seconds-based answer + !!! would fail the lower bound, which is the mistake worth catching. + say (first-stamp is greater than 1577836800000) becomes text + say (first-stamp is less than 7258118400000) becomes text + + evaluate nil.std.io::write_text with path being path, content being "second and longer" + value second-stamp is stamp-of with path being path + say (second-stamp is at least first-stamp) becomes text + + !!! The size moved too, which is what a validator pairs with the time. + value measured is nil.std.io::size with path being path + consider measured + when Result.Ok holding width then say width + otherwise say 0 minus 1 + end consider + + evaluate nil.std.io::delete with path being path + !!! An absent file is reported, not given a zero that a comparison would + !!! read as the epoch and treat as very old indeed. + say stamp-of with path being path + give 0 +end paragraph diff --git a/tests/matrix/execution/file-time.needs-stdlib b/tests/matrix/execution/file-time.needs-stdlib new file mode 100644 index 00000000..e69de29b diff --git a/tests/matrix/execution/guide-constructs.expected b/tests/matrix/execution/guide-constructs.expected index ae82df4f..f9cc8bd4 100644 --- a/tests/matrix/execution/guide-constructs.expected +++ b/tests/matrix/execution/guide-constructs.expected @@ -47,4 +47,11 @@ cleanup 255 255 3 +Count: 4; who: Nathan +Result: one +{literal braces} and 3 + + Home + + 1 diff --git a/tests/matrix/execution/guide-constructs.matrix b/tests/matrix/execution/guide-constructs.matrix index ad2f1d53..7b3ab3b7 100644 --- a/tests/matrix/execution/guide-constructs.matrix +++ b/tests/matrix/execution/guide-constructs.matrix @@ -79,6 +79,11 @@ end paragraph answer returns number begin paragraph give 42 end paragraph +choose receives first as text, second as text returns text begin paragraph + when (the length of first) is greater than 0 then give first end when + give second +end paragraph + total-of receives more values as number returns number begin paragraph variable sum as number is 0 variable i as number is 0 @@ -235,6 +240,21 @@ main returns number begin paragraph value encoded is apply encode-sample across pixels say encoded at 0 + value who as text is "Nathan" + value tally as number is 3 + say formatted "Count: {tally plus 1}; who: {who}" + say formatted "Result: {choose with first being "one", second being "two"}" + say formatted "{{literal braces}} and {tally}" + + value page-title as text is "Home" + value page-nav as text is "Dashboard" + say formatted begin text + + {page-title} + + + end text + value started as override long is now in milliseconds sleep for 1 milliseconds say (now in milliseconds) is at least started diff --git a/tests/matrix/execution/guide-queries-patterns.expected b/tests/matrix/execution/guide-queries-patterns.expected new file mode 100644 index 00000000..be7e052a --- /dev/null +++ b/tests/matrix/execution/guide-queries-patterns.expected @@ -0,0 +1,20 @@ +2 +110 +82 +4 +1 +110 +82 +1 +12 +12 + + Home & Away + + + + diff --git a/tests/matrix/execution/guide-queries-patterns.matrix b/tests/matrix/execution/guide-queries-patterns.matrix new file mode 100644 index 00000000..93ff8c0e --- /dev/null +++ b/tests/matrix/execution/guide-queries-patterns.matrix @@ -0,0 +1,68 @@ +!!! Verbatim constructs from MATRIX-GUIDE.md sections 20, 21 and 22, which need +!!! the standard library: queries read nil.std.collections terminals, patterns +!!! are compiled by nil.std.text, and a markup document is nil.std.markup's. +book tour.guide-queries + +read everything from nil.std.text +read everything from nil.std.collections +read everything from nil.std.markup + +dollar-amount begin pattern + capture amount matching one or more ascii digits + require next to match literal " USD" +end pattern + +page-shell receives title as text, signed-in as boolean, who as text + returns nil.std.markup.document begin paragraph + give begin markup + + {title} + + + + + end markup +end paragraph + +main returns number begin paragraph + value scores as array of number is [41, 7, 19, 55] + value top is collect begin query + from each score in scores + where score is greater than 10 + order by score descending + take 2 + give score times 2 + end query + say the length of top + say top at 0 + say top at 1 + + value plain is collect begin query + from each score in scores + give score + end query + say query-count with items being plain + say query-any with items being top + say (query-first with items being top).unwrap with nothing + say (query-last with items being top).unwrap with nothing + say (query-single with items being top).is-error with nothing + + value compiled is (compile_pattern with source being dollar-amount).unwrap with nothing + value found is ((first_match with pattern being compiled, + input being "price: 12 USD").unwrap with nothing).unwrap with nothing + say (capture_named with found being found, name being "amount").unwrap with nothing + + value conventional is (compile_pattern with source being "(?[0-9]+)(?= USD)").unwrap with nothing + value same is ((first_match with pattern being conventional, + input being "price: 12 USD").unwrap with nothing).unwrap with nothing + say (capture_named with found being same, name being "amount").unwrap with nothing + value page is page-shell with title being "Home & Away", signed-in being true, who being "" + say nil.std.markup::render with doc being page + give 0 +end paragraph diff --git a/tests/matrix/execution/guide-queries-patterns.needs-stdlib b/tests/matrix/execution/guide-queries-patterns.needs-stdlib new file mode 100644 index 00000000..e69de29b diff --git a/tests/matrix/run_execution_tests.py b/tests/matrix/run_execution_tests.py index bb0828da..0bce6ed1 100755 --- a/tests/matrix/run_execution_tests.py +++ b/tests/matrix/run_execution_tests.py @@ -478,7 +478,9 @@ def carries(prefix, name, marker): def check_debug_metadata(compiler, interpreter, workdir): """Matrix paragraphs and methods need source maps for editor breakpoints.""" - page = pathlib.Path(workdir) / "source-debug.matrix" + # Source maps use canonical paths. Resolve Windows short-name TEMP aliases + # (and POSIX temporary-directory symlinks) before checking serialized bytes. + page = pathlib.Path(workdir).resolve() / "source-debug.matrix" page.write_text( "book debug-check\n\n" "Reading begin record\n field level as number\nend record\n" @@ -508,7 +510,7 @@ def check_debug_metadata(compiler, interpreter, workdir): # same string and this reduces to the original check. spelling = str(page) if binary else str(page).replace("\\", "\\\\") if (spelling.encode() in data) != debug: - failures.append(f"source debug: path presence incorrect for debug={debug}, binary={binary}") + failures.append(f"source debug: path presence incorrect for debug={debug}, binary={binary}, expected={spelling!r}") if debug and not binary: functions = [part for part in data.decode().split("\nfunction ")[1:]] if len(functions) != 3 or any("# source_debug " not in part.split("end function")[0] for part in functions): diff --git a/tests/matrix/run_lexer_tests.py b/tests/matrix/run_lexer_tests.py index e179adb8..97104812 100755 --- a/tests/matrix/run_lexer_tests.py +++ b/tests/matrix/run_lexer_tests.py @@ -62,6 +62,9 @@ # NIL-0023 keeps ordinary variables and parameters named flag valid. "flag", "expect", + # NIL-0027: 'markup' is contextual after 'begin', and 'raw' only in front + # of an expression inside a markup hole. Both stay ordinary names. + "markup", "raw", } # Lowercase markers inside a numeric token are not standalone word terminals. diff --git a/tests/native_windows_header_contract.cpp b/tests/native_windows_header_contract.cpp new file mode 100644 index 00000000..abdbfa10 --- /dev/null +++ b/tests/native_windows_header_contract.cpp @@ -0,0 +1,27 @@ +// Compile the native-codegen interface after the real Windows RPC headers. +// These headers define `small` as `char`; the compiler's declarations must +// coexist with that caller environment without undefining its macros. +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#include + +#ifndef small +#error Windows RPC small macro is required for this regression +#endif + +#include "llvm_backend.hpp" +#include + +static_assert(std::is_same::value, + "Including the compiler interface must preserve Windows macros"); +static_assert(std::is_enum::value, + "Native code-model declarations must remain valid after Windows headers"); + +void native_windows_header_contract(ny::NativeCodegen &codegen) { + using Model = ny::NativeCodegen::CodeModel; + codegen.setCodeModel(Model::Host); + codegen.setCodeModel(Model::Small); + codegen.setCodeModel(Model::Large); +} diff --git a/tests/net_lifecycle_tests.cpp b/tests/net_lifecycle_tests.cpp new file mode 100644 index 00000000..5e792e48 --- /dev/null +++ b/tests/net_lifecycle_tests.cpp @@ -0,0 +1,157 @@ +// The VM must release sockets while its host process is still alive. +#include "net_support.hpp" +#include "codegen.hpp" +#include "nil_loader.hpp" +#include "vm.hpp" +#include +#include +#include +#include + +namespace { +namespace net = ny::net_support; +int checks = 0; +void require(bool value, const std::string &message) { + ++checks; + if (!value) throw std::runtime_error(message); +} +struct Socket { + net::socket_descriptor value; + explicit Socket(net::socket_descriptor descriptor) : value(descriptor) {} + ~Socket() { if (value != net::invalid_socket) net::close_descriptor(value); } + Socket(const Socket &) = delete; + Socket &operator=(const Socket &) = delete; +}; +int unused_port() { + std::string error; + require(net::ensure_started(error), error); + Socket probe(::socket(AF_INET, SOCK_STREAM, 0)); + require(probe.value != net::invalid_socket, "open loopback port probe"); + sockaddr_in address{}; address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + require(::bind(probe.value, reinterpret_cast(&address), sizeof(address)) == 0, "bind port probe"); +#ifdef _WIN32 + int size = sizeof(address); +#else + socklen_t size = sizeof(address); +#endif + require(::getsockname(probe.value, reinterpret_cast(&address), &size) == 0, "read port probe"); + return ntohs(address.sin_port); +} +void endpoint(int port, bool held, const std::string &phase) { + const auto probe = net::listen_on("127.0.0.1", port, 8); + Socket owned(probe.descriptor); + require(probe.ok() != held, phase + (held ? " must retain the actual listening socket" : " must release the actual listening socket")); +} +const std::string result_type = "nil::std::core::Result"; +ny::nil_function function(std::string name, std::vector parameters, + std::string result, std::vector body) { + ny::nil_function value; + value.name = std::move(name); value.params = std::move(parameters); + value.return_type = std::move(result); value.body = std::move(body); + return value; +} +struct Program { + std::vector code; + std::unordered_map constants; +}; +Program fixture(int port, bool binary, bool fault) { + ny::nil_module module; + module.module_name = "net_lifecycle"; + module.enums = {{result_type, {{"Ok", "i64"}, {"Error", "text"}}}}; + module.string_constants = {{"host", "127.0.0.1"}}; + std::vector body{ + {"PUSH_CONST_STR", {"host"}}, {"PUSH_CONST", {std::to_string(port)}}, {"PUSH_CONST", {"8"}}, + {"INTEROP_CALL", {"intrinsic", "net.listen", "3", result_type}}, + {"UNWRAP_VARIANT", {"1", result_type, "i64"}}, {"STORE_GLOBAL", {"opened", "i64"}}, + {"LOAD_GLOBAL", {"opened", "i64"}}, {"PUSH_CONST", {"1"}}, + {"INTEROP_CALL", {"intrinsic", "net.watch", "2", result_type}}, + {"UNWRAP_VARIANT", {"1", result_type, "i64"}}, {"STORE_GLOBAL", {"watched", "i64"}}}; + if (fault) { + body.push_back({"PUSH_CONST", {"1"}}); body.push_back({"PUSH_CONST", {"0"}}); + body.push_back({"DIV", {"i64"}}); + } else body.push_back({"PUSH_CONST", {"0"}}); + body.push_back({"RET", {}}); + module.functions.push_back(function("main", {}, "i64", std::move(body))); + module.functions.push_back(function("watch_count", {}, "i64", + {{"INTEROP_CALL", {"intrinsic", "net.watch_count", "0", "i64"}}, {"RET", {}}})); + module.functions.push_back(function("is_open", {"handle:i64"}, "bool", + {{"STORE_LOCAL", {"handle"}}, {"LOAD_LOCAL", {"handle"}}, + {"INTEROP_CALL", {"intrinsic", "net.is_open", "1", "bool"}}, {"RET", {}}})); + std::ostringstream bytes(std::ios::out | std::ios::binary); + if (binary) { + ny::codegen_options options; ny::nil_binary_writer writer(bytes, options); writer.emit_binary_module(module); + } else { ny::nil_writer writer(bytes); writer.emit_text_module(module); } + std::istringstream input(bytes.str(), std::ios::in | std::ios::binary); + Program program; + program.code = binary ? ny::parseBinaryProgram(input, program.constants) : ny::parseProgram(input, program.constants); + return program; +} +std::int64_t pause(ny::VM &vm, const Program &program, int port) { + vm.loadProgram(program.code, program.constants); + vm.resetExecution("main"); + for (int i = 0; i < 50 && !vm.hasGlobal("watched"); ++i) + require(vm.stepInstruction(), "program must reach its watched listener"); + require(vm.hasGlobal("watched") && std::get(vm.getGlobal("watched")) == 1, + "exactly one listener is watched before pause"); + endpoint(port, true, "paused invocation"); + const auto token = std::get(vm.getGlobal("opened")); + require(token > 0, "listener token is positive"); + return token; +} +void cleared(ny::VM &vm, std::int64_t token) { + vm.pushValue(token); vm.callFunction("is_open", 1); + require(std::get(vm.getStack().back()) == 0, "terminal socket token is stale"); + vm.callFunction("watch_count", 0); + require(std::get(vm.getStack().back()) == 0, "terminal clears the watch set"); +} +void lifecycle(bool binary) { + const int port = unused_port(); + const auto normal = fixture(port, binary, false); + const auto faulty = fixture(port, binary, true); + ny::VM vm; + const auto first = pause(vm, normal, port); + vm.execute(false); + endpoint(port, false, "normal completion"); + cleared(vm, first); + + const auto second = pause(vm, normal, port); + require(second > first, "completed invocation tokens are never reissued"); + vm.resetExecution("main"); + endpoint(port, false, "debugger reset"); + // resetExecution leaves a prepared main frame, so invoking a separate host + // function here would escape that frame without RET. The bind probe above + // observes actual release without changing or executing the prepared frame. + + const auto third = pause(vm, normal, port); + require(third > second, "reset invocation tokens are never reissued"); + vm.loadProgram(normal.code, normal.constants); + endpoint(port, false, "program replacement"); + cleared(vm, third); + + const auto fourth = pause(vm, faulty, port); + bool failed = false; + try { vm.execute(false); } + catch (const std::runtime_error &error) { failed = std::string(error.what()).find("Division by zero") != std::string::npos; } + require(failed, "fault fixture must actually execute its division error"); + endpoint(port, false, "uncaught runtime error"); + require(fourth > third, "faulting invocation also receives a fresh token"); + + { + auto paused = std::make_unique(); + pause(*paused, normal, port); + paused.reset(); + } + endpoint(port, false, "paused VM destruction"); +} +} + +int main() { + try { + lifecycle(false); lifecycle(true); + std::cout << "Socket VM lifecycle: " << checks << " checks passed\n"; + return 0; + } catch (const std::exception &error) { + std::cerr << "Socket lifecycle failure: " << error.what() << '\n'; + return 1; + } +} diff --git a/tests/net_support_tests.cpp b/tests/net_support_tests.cpp new file mode 100644 index 00000000..954870be --- /dev/null +++ b/tests/net_support_tests.cpp @@ -0,0 +1,230 @@ +// Exercise the OS socket/entropy adapters without an interpreter or public port. +// Include the CRT first: entropy must not depend on an earlier feature macro. +#include +#include "net_support.hpp" +#include "entropy_support.hpp" +#include +#include +#include +#include +#include +#if !defined(_WIN32) +#include +#endif + +namespace { +namespace net = ny::net_support; +int checks = 0; +void require(bool value, const std::string &message) { + ++checks; + if (!value) throw std::runtime_error(message); +} +struct Socket { + net::socket_descriptor value = net::invalid_socket; + explicit Socket(net::socket_descriptor opened = net::invalid_socket) : value(opened) {} + ~Socket() { if (value != net::invalid_socket) net::close_descriptor(value); } + Socket(const Socket &) = delete; + Socket &operator=(const Socket &) = delete; +}; +int unused_port() { + std::string error; + require(net::ensure_started(error), error); + Socket probe(::socket(AF_INET, SOCK_STREAM, 0)); + require(probe.value != net::invalid_socket, "open port probe"); + sockaddr_in address{}; + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + require(::bind(probe.value, reinterpret_cast(&address), sizeof(address)) == 0, "bind port probe"); +#if defined(_WIN32) + int size = sizeof(address); +#else + socklen_t size = sizeof(address); +#endif + require(::getsockname(probe.value, reinterpret_cast(&address), &size) == 0, "read port probe"); + return ntohs(address.sin_port); +} +void binding() { + const std::string truncated("127.0.0.1\0suffix", 16); + const auto invalid_bind = net::listen_on(truncated, 31337, 8); + require(!invalid_bind.ok() && invalid_bind.error.find("NUL") != std::string::npos, "bind rejects embedded NUL"); + const auto invalid_connect = net::connect_to(truncated, 31337); + require(!invalid_connect.ok() && invalid_connect.error.find("NUL") != std::string::npos, "connect rejects NUL before resolution"); + const int port = unused_port(); + const auto first = net::listen_on("127.0.0.1", port, 8); + require(first.ok(), first.error); + Socket one(first.descriptor); + const auto second = net::listen_on("127.0.0.1", port, 8); + Socket two(second.descriptor); + require(!second.ok(), "ordinary listeners must not share an endpoint"); + require(!net::set_timeout(one.value, -1).empty(), "negative timeout rejected"); + require(!net::set_timeout(one.value, net::max_timeout_ms + 1).empty(), "overflowing timeout rejected"); + std::vector ready; + require(!net::poll_wait({{1, one.value, net::ready_readable}}, net::max_timeout_ms + 1, ready).empty(), + "overflowing poll timeout rejected"); + require(!net::poll_wait({{1, one.value, 8}}, 0, ready).empty(), "unknown watch flags rejected"); + + const int shared_port = unused_port(); + const auto shared_first = net::listen_on("127.0.0.1", shared_port, 8, true); + Socket shared_one(shared_first.descriptor); +#if defined(SO_REUSEPORT) && !defined(_WIN32) + require(shared_first.ok(), shared_first.error); + const auto shared_second = net::listen_on("127.0.0.1", shared_port, 8, true); + Socket shared_two(shared_second.descriptor); + require(shared_second.ok(), shared_second.error); + const auto ordinary = net::listen_on("127.0.0.1", shared_port, 8); + Socket accidental(ordinary.descriptor); + require(!ordinary.ok(), "ordinary listener cannot join an explicitly shared endpoint"); +#else + require(!shared_first.ok() && shared_first.error.find("no shared-listener") != std::string::npos, + "unsupported shared listening is an explicit error"); +#endif +} + +void stream() { + const int port = unused_port(); + const auto opened = net::listen_on("127.0.0.1", port, 8); + require(opened.ok(), opened.error); + Socket listener(opened.descriptor); + require(net::set_blocking(listener.value, false).empty(), "nonblocking listener"); + require(net::accept_on(listener.value).would_block, "idle accept is would-block"); + const auto connected = net::connect_to("127.0.0.1", port); + require(connected.ok(), connected.error); + Socket client(connected.descriptor); + std::vector ready; + require(net::poll_wait({{37, listener.value, net::ready_readable}}, 1000, ready).empty() && + ready.size() == 1 && ready[0].token == 37 && (ready[0].flags & net::ready_readable), "listener readiness"); + const auto accepted = net::accept_on(listener.value); + require(accepted.ok(), accepted.error); + Socket server(accepted.descriptor); + require(net::set_timeout(server.value, 1000).empty(), "server timeout"); + require(net::set_timeout(client.value, 1000).empty(), "client timeout"); +#if defined(SO_NOSIGPIPE) + for (const auto descriptor : {client.value, server.value}) { + int enabled = 0; socklen_t size = sizeof(enabled); + require(::getsockopt(descriptor, SOL_SOCKET, SO_NOSIGPIPE, &enabled, &size) == 0 && enabled == 1, + "both connecting and accepted sockets suppress SIGPIPE"); + } +#endif + require(net::set_blocking(server.value, false).empty(), "nonblocking receive"); + const auto idle = net::read_some(server.value, 32); + require(idle.ok() && idle.would_block && !idle.closed && idle.data.empty(), "would-block differs from EOF"); + const std::string bytes("prefix\0\xfftail", 12); + const auto sent = net::write_some(client.value, bytes, 6); + require(sent.ok() && sent.count == 6 && !sent.would_block, "send from exact byte offset"); + require(net::poll_wait({{9, server.value, net::ready_readable}}, 1000, ready).empty() && !ready.empty(), "data readiness"); + const auto data = net::read_some(server.value, 32); + require(data.ok() && data.data == bytes.substr(6) && data.count == 6 && !data.closed && !data.would_block, + "nonblocking bytes survive zero and invalid UTF-8"); + require(net::write_some(client.value, bytes, bytes.size()).count == 0, "empty remainder succeeds"); + + // A peer that does not read eventually applies backpressure. Bound traffic + // and memory instead of depending on a host's default socket-buffer size. + const int buffer = 4096; + require(::setsockopt(client.value, SOL_SOCKET, SO_SNDBUF, + reinterpret_cast(&buffer), sizeof(buffer)) == 0, "small send buffer"); + require(net::set_blocking(client.value, false).empty(), "nonblocking send"); + const std::string payload(256 * 1024, 'x'); + bool blocked = false; + std::int64_t total = 0; + for (int attempt = 0; attempt < 1024 && total < 32 * 1024 * 1024; ++attempt) { + const auto part = net::write_some(client.value, payload, 0); + require(part.ok(), part.error); + if (part.would_block) { blocked = true; break; } + require(part.count > 0 && part.count <= static_cast(payload.size()), "bounded positive partial send"); + total += part.count; + } + require(blocked && total > 0, "slow peer produces would-block without blocking the process"); +} + +void peer_close() { + const int port = unused_port(); + const auto opened = net::listen_on("127.0.0.1", port, 8); + require(opened.ok(), opened.error); + Socket listener(opened.descriptor); + const auto connected = net::connect_to("127.0.0.1", port); + require(connected.ok(), connected.error); + Socket client(connected.descriptor); + const auto accepted = net::accept_on(listener.value); + require(accepted.ok(), accepted.error); + Socket server(accepted.descriptor); + require(net::set_timeout(client.value, 1000).empty(), "closed-peer client timeout"); + linger reset{}; reset.l_onoff = 1; reset.l_linger = 0; + require(::setsockopt(server.value, SOL_SOCKET, SO_LINGER, + reinterpret_cast(&reset), sizeof(reset)) == 0, "peer reset configuration"); + net::close_descriptor(server.value); server.value = net::invalid_socket; + std::vector ready; + require(net::poll_wait({{1, client.value, net::ready_readable}}, 1000, ready).empty() && !ready.empty(), "peer-close observed"); + // The first failure can be ECONNRESET; a subsequent write is what exposes + // an unsuppressed EPIPE. The process uses SIGPIPE's default disposition. + int failed = 0; + for (int attempt = 0; attempt < 4; ++attempt) + if (!net::write_all(client.value, "closed peer").ok()) ++failed; + require(failed > 0, "closed-peer writes report failure without terminating"); +} + +#if !defined(_WIN32) +volatile std::sig_atomic_t interruptions = 0; +void interrupted(int) { ++interruptions; } +void interrupted_wait() { + const auto opened = net::listen_on("127.0.0.1", unused_port(), 8); + require(opened.ok(), opened.error); + Socket listener(opened.descriptor); + struct sigaction action{}, before{}; + action.sa_handler = interrupted; + sigemptyset(&action.sa_mask); + require(sigaction(SIGUSR1, &action, &before) == 0, "install owned-thread signal handler"); + const pthread_t target = pthread_self(); + std::thread signaler([target] { + for (int i = 0; i < 20; ++i) { + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + pthread_kill(target, SIGUSR1); + } + }); + const auto start = std::chrono::steady_clock::now(); + std::vector ready; + const auto error = net::poll_wait({{1, listener.value, net::ready_readable}}, 100, ready); + const auto elapsed = std::chrono::steady_clock::now() - start; + signaler.join(); + require(sigaction(SIGUSR1, &before, nullptr) == 0, "restore signal disposition"); + require(error.empty() && ready.empty() && interruptions > 0, "signals do not turn a wait into an error"); + require(elapsed >= std::chrono::milliseconds(70) && elapsed < std::chrono::milliseconds(300), + "finite deadline is not restarted by repeated interruptions"); +} +#endif + +void entropy() { + namespace random = ny::entropy_support; + require(!random::random_bytes(0).ok() && !random::random_bytes(1025).ok(), "entropy byte bounds"); + const auto bytes = random::random_bytes(257); + require(bytes.ok() && bytes.bytes.size() == 257, "OS entropy fills the complete buffer"); + const auto text = random::random_hex(32); + require(text.ok() && text.bytes.size() == 64 && text.bytes.find_first_not_of("0123456789abcdef") == std::string::npos, + "OS entropy hex encoding"); + for (const auto bound : {std::int64_t{1}, std::int64_t{3}, (std::numeric_limits::max)()}) { + for (int i = 0; i < 20; ++i) { + const auto value = random::random_below(bound); + require(value.ok() && value.value >= 0 && value.value < bound, "OS entropy bounded value"); + } + } + require(!random::random_below(0).ok(), "entropy invalid bound"); +} +} + +int main() { + try { +#if defined(_WIN32) + static_assert(std::is_same::value, "socket storage must not narrow a Winsock handle"); +#else + std::signal(SIGPIPE, SIG_DFL); +#endif + binding(); stream(); peer_close(); entropy(); +#if !defined(_WIN32) + interrupted_wait(); +#endif + std::cout << "Socket/entropy support: " << checks << " checks passed\n"; + return 0; + } catch (const std::exception &error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/tests/routes/ny_route_matrix.py b/tests/routes/ny_route_matrix.py index eb18e852..0462e4ae 100644 --- a/tests/routes/ny_route_matrix.py +++ b/tests/routes/ny_route_matrix.py @@ -26,6 +26,9 @@ from pathlib import Path from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from runtime_provider_environment import provider_environments + EXECUTION_ROUTES: Tuple[str, ...] = ( "text-interpreter", @@ -1341,11 +1344,18 @@ def run_route( max_output_chars: int, ) -> Dict[str, Any]: route_dir.mkdir(parents=True, exist_ok=False) + env, runtime_env, declared_provider = provider_environments(env) result = new_route_result(route, route_dir) checks: List[Dict[str, Any]] = result["checks"] def record_command(name: str, process: CommandResult) -> None: result["commands"][name] = process.as_dict(max_output_chars) + if declared_provider is not None: + result["commands"][name]["runtime_loader"] = { + "dyld_fallback_library_path": None if name == "compile" else declared_provider, + "role": "build" if name == "compile" else + "combined JIT compilation/execution" if name == "compile_and_run" else "execution", + } if route in ("text-interpreter", "binary-interpreter"): binary = route == "binary-interpreter" @@ -1385,7 +1395,7 @@ def record_command(name: str, process: CommandResult) -> None: ], route_dir, timeout_seconds, - env, + runtime_env, ) record_command("run", run_process) check_process_contract( @@ -1427,7 +1437,7 @@ def record_command(name: str, process: CommandResult) -> None: ) if compile_process.exit_code == 0 and artifact["valid"]: run_process = run_command( - [str(output)], route_dir, timeout_seconds, env + [str(output)], route_dir, timeout_seconds, runtime_env ) record_command("run", run_process) check_process_contract( @@ -1455,7 +1465,7 @@ def record_command(name: str, process: CommandResult) -> None: jit_command, route_dir, timeout_seconds, - env, + runtime_env, ) record_command("compile_and_run", jit_process) if llvm_is_unavailable(jit_process): @@ -1544,7 +1554,7 @@ def record_command(name: str, process: CommandResult) -> None: [str(expected_executable), *program_args], route_dir, timeout_seconds, - env, + runtime_env, ) record_command("run", run_process) check_process_contract( @@ -1592,7 +1602,7 @@ def record_command(name: str, process: CommandResult) -> None: elif route == "interpreter-reject": run_process = run_command( - [str(interpreter), str(source)], route_dir, timeout_seconds, env + [str(interpreter), str(source)], route_dir, timeout_seconds, runtime_env ) record_command("run", run_process) check_process_contract( diff --git a/tests/runtime_provider_environment.py b/tests/runtime_provider_environment.py new file mode 100644 index 00000000..bee80b5b --- /dev/null +++ b/tests/runtime_provider_environment.py @@ -0,0 +1,31 @@ +"""Explicit test-only Darwin provider paths, separated from build processes.""" +from __future__ import annotations + +from pathlib import PurePosixPath +import sys + +DECLARATION = 'NIL_TEST_DARWIN_RUNTIME_LIBRARY_PATH' +LOADER_VARIABLE = 'DYLD_FALLBACK_LIBRARY_PATH' + + +def provider_environments(environment, *, platform_name=None): + """Return build/runtime environments and the optional declared search path. + + Without the CI/test declaration, ambient loader settings retain their + existing meaning. With it, a conflicting loader override is an error, not + a setting silently removed to make a cache or qualification test pass. + """ + build = dict(environment) + declared = build.pop(DECLARATION, None) + if declared is None: + return build, dict(build), None + if (platform_name or sys.platform) != 'darwin': + raise ValueError('the declared runtime library search path requires macOS') + if (not isinstance(declared, str) or not declared or len(declared) > 32768 or + any(char in declared for char in '\0\r\n') or + any(not part or not PurePosixPath(part).is_absolute() for part in declared.split(':'))): + raise ValueError('runtime library path must contain ordered absolute nonempty Darwin directories') + if any(key.startswith('DYLD_') and value for key, value in build.items()): + raise ValueError('declared runtime library path conflicts with ambient DYLD overrides') + runtime = dict(build, **{LOADER_VARIABLE: declared}) + return build, runtime, declared diff --git a/tests/sdk_freestanding_imports_tests.py b/tests/sdk_freestanding_imports_tests.py new file mode 100644 index 00000000..324d6089 --- /dev/null +++ b/tests/sdk_freestanding_imports_tests.py @@ -0,0 +1,255 @@ +#!/usr/bin/env python3 +"""Cross-format parser controls and actual built freestanding archive contract.""" +from __future__ import annotations + +import argparse +import hashlib +import importlib.util +from pathlib import Path +import struct +import sys +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'scripts')) +from sdk_freestanding_imports import (ALLOWED_IMPORTS, REQUIRED_DEFINITIONS, + freestanding_archive_imports, object_symbols) +from sdk_native_archive_validation import ar_member, sysv_index_payload, bsd_index_payload + + +def symbols(imports): + return [(name, True) for name in sorted(REQUIRED_DEFINITIONS)] + [(name, False) for name in imports] + + +def elf(imports=(), *, machine=62, weak=False): + strings = bytearray(b'\0') + entries = bytearray(24) + for name, defined in symbols(imports): + index = len(strings) + strings.extend(name.encode() + b'\0') + entries.extend(struct.pack('\n' + index + names + member + extra + + +class Imports(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory(prefix='nil-freestanding-imports-') + self.addCleanup(self.temporary.cleanup) + self.path = Path(self.temporary.name) / 'runtime.a' + + def read(self, value): + self.path.write_bytes(value) + return freestanding_archive_imports(self.path) + + def test_all_six_profiles_and_archive_names(self): + for make, machine in ((elf, 62), (elf, 183), (coff, 0x8664), (coff, 0xaa64), + (macho, 0x1000007), (macho, 0x100000c)): + for naming in ({}, {'bsd': True}, {'gnu': True}): + with self.subTest(format=make.__name__, machine=machine, naming=naming): + self.assertEqual(self.read(archive(make(machine=machine), **naming)), set()) + self.assertEqual(self.read(archive(make(['memcpy', 'memset', '__ny_host_fail'], machine=machine), + **naming)), {'memcpy', 'memset', '__ny_host_fail'}) + + def test_weak_import_is_not_silently_ignored(self): + for make in (elf, coff, macho): + with self.subTest(format=make.__name__): + self.assertEqual(self.read(archive(make(['memcpy'], weak=True))), {'memcpy'}) + + def test_definitions_resolve_imports(self): + for make in (elf, coff, macho): + self.assertEqual(self.read(archive(make(['nyrt_abi_version']))), set()) + + def test_got_exception_applies_only_to_elf(self): + self.assertEqual(self.read(archive(elf(['_GLOBAL_OFFSET_TABLE_']))), set()) + for make in (coff, macho): + self.assertEqual(self.read(archive(make(['_GLOBAL_OFFSET_TABLE_']))), {'_GLOBAL_OFFSET_TABLE_'}) + self.assertNotIn('_GLOBAL_OFFSET_TABLE_', ALLOWED_IMPORTS) + + def test_incompatible_member_cannot_resolve_an_import(self): + for second in (elf(machine=183), coff(), macho()): + # An incompatible second member is rejected even when it does not + # happen to resolve an import; cross-target archives are unsupported. + with self.assertRaises(ValueError): + self.read(archive(elf(['memcpy']), extra=ar_member(second, b'second.o/'))) + + def test_external_definitions_require_real_sections(self): + e = bytearray(elf(['memcpy'])) + section, = struct.unpack_from('\n', valid[:-1], valid + b'x', b'!\n' + valid[8:]): + with self.assertRaises(ValueError): + self.read(value) + + def test_malformed_symbol_and_string_tables(self): + mutations = [] + e = bytearray(elf(['memcpy'])) + section, = struct.unpack_from(' i32\r\n' + ' let sample_value = 7\r\n' + ' println($"\"\"\r\n' + ' {{literal}} {sam|}\r\n' + ' "\"\")\r\n' + ' return 0\r\nend fn\r\n') + assert set(items(query(block_ny, name='formatted-block.ny'))) == {'sample_value'} + query(block_ny.replace('{sam|}', '{{sam|}}'), name='formatted_block_literal.ny', + available=False, status=0) + # Independent result budgets withhold the optional query rather than # return truncated candidates or omit contributing source snapshots. large = '# ' + 'x' * (1500 * 1024) + '\n' diff --git a/tests/tools/context_manifest.py b/tests/tools/context_manifest.py index 628f5ada..758707ea 100644 --- a/tests/tools/context_manifest.py +++ b/tests/tools/context_manifest.py @@ -289,7 +289,8 @@ def inspect(artifact: Path) -> dict[str, object]: raise AssertionError("Darwin context retention requires the host clang++ driver") stripped = root / (artifact.stem + "-dead-strip") success(run([driver, "-pthread", "-Wl,-dead_strip", "-o", stripped, - obj, tools["runtime"]]), "Darwin dead-strip linking") + obj, tools["runtime"], "-framework", "Cocoa", "-framework", "CoreGraphics"]), + "Darwin dead-strip linking") if inspect(stripped) != source_manifest: raise AssertionError("Darwin dead stripping removed or changed context") success(run([stripped]), "dead-stripped context execution", EXPECTED) diff --git a/tests/tools/darwin_heap_accounting_contract.py b/tests/tools/darwin_heap_accounting_contract.py new file mode 100644 index 00000000..161d8306 --- /dev/null +++ b/tests/tools/darwin_heap_accounting_contract.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +"""Linux --wrap regression for Darwin heap-oracle bookkeeping, not Mach-O proof.""" +from __future__ import annotations + +import argparse +import json +import os +from pathlib import Path +import sys +import tempfile + +import ordinary_io_parity_contract as support + +ROOT = Path(__file__).resolve().parents[2] +BUDGET = 96 * 1024 * 1024 + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--cc', default='cc') + parser.add_argument('--output-parent', type=Path) + args = parser.parse_args() + if not sys.platform.startswith('linux'): + raise AssertionError('The portable --wrap control requires Linux; use real injection on Darwin') + parent = args.output_parent.resolve() if args.output_parent else Path(tempfile.gettempdir()) + parent.mkdir(parents=True, exist_ok=True) + output = Path(tempfile.mkdtemp(prefix='nil-heap-accounting-', dir=parent)) + ownership = support.load_ownership(ROOT) + environment = {key: value for key, value in os.environ.items() + if not key.startswith(('NIL_', 'NY_', 'DYLD_'))} + report = {'schema': 'nil.heap-accounting-portable.v1', 'status': 'FAIL', + 'darwin_injection_qualified': False, 'commands': [], 'cases': []} + + def command(name, argv, *, variables=None, code=0, stdout=b''): + row = support.run_process(ownership, list(map(str, argv)), ROOT, + environment | (variables or {}), output, name, 30) + report['commands'].append(row) + actual = (output / row['stdout']).read_bytes() + errors = (output / row['stderr']).read_bytes() + if (row.get('exit_code') != code or not row.get('cleanup_verified') or + row.get('error') or actual != stdout or errors): + raise AssertionError(f'{name}: exit={row.get("exit_code")} expected={code}; ' + f'stdout={actual[:1024]!r}; stderr={errors[:1024]!r}') + row['status'] = 'PASS' + + try: + for nested in (False, True): + kind = 'nested' if nested else 'ordinary' + executable = output / kind + argv = [args.cc, '-std=c11', '-D_DEFAULT_SOURCE', '-DNIL_HEAP_BUDGET_PORTABLE_TEST', + '-Wall', '-Wextra', '-Werror', '-O2', '-fno-builtin', '-rdynamic', '-pthread', + ROOT / 'tests/darwin_native_heap_budget.c', + ROOT / 'tests/darwin_native_heap_budget_control.c'] + if nested: + argv += ['-DNIL_HEAP_BUDGET_NESTED_TEST', ROOT / 'tests/darwin_native_heap_budget_nested.c'] + argv += ['-ldl'] + ['-Wl,--wrap=' + name for name in + ('malloc', 'calloc', 'realloc', 'free', 'aligned_alloc', 'posix_memalign', 'valloc')] + command(kind + '-build', argv + ['-o', executable]) + for case, mode, withhold, target in ( + ('positive', 'positive', False, 65536), ('retained', 'retain', False, 65536), + ('withheld', 'positive', True, 65536), + ('guarded-positive', 'guarded-positive', False, 65540), + ('guarded-retained', 'guarded-retain', False, 65540), + ('guarded-withheld', 'guarded-positive', True, 65540)): + name = kind + '-' + case + events_path = output / (name + '.jsonl') + negative = not case.endswith('positive') + positive_output = b'heap-controls-ok\n' if target == 65536 else b'guarded-controls-ok\n' + command(name, [executable, mode], code=86 if negative else 0, + stdout=b'' if negative else positive_output, variables={ + 'NIL_DARWIN_HEAP_REPORT': str(events_path), 'NIL_DARWIN_HEAP_TOKEN': 'a' * 32, + 'NIL_DARWIN_HEAP_TARGET': str(target), 'NIL_DARWIN_HEAP_WITHHOLD': str(int(withhold))}) + events = [json.loads(line) for line in events_path.read_text().splitlines()] + if len(events) != 2 or events[0]['event'] != 'ready': + raise AssertionError(name + ': initialization/final event pair absent') + initial, final = events + if final['event'] != ('budget' if negative else 'final') or final['budget'] != BUDGET: + raise AssertionError(name + ': wrong terminal event or budget') + if nested and (initial['cumulative'] != 1024 or initial['allocations'] != 1 or initial['live']): + raise AssertionError(name + ': pre-initialization nested request was not counted exactly once') + if negative: + if not (BUDGET - target <= final['live'] <= BUDGET and final['denied_request'] == target): + raise AssertionError(name + ': retained payload did not trigger the real budget') + if withhold and not final['withheld']: + raise AssertionError(name + ': actual target frees were not withheld') + elif (final['live'] or final['cumulative'] - initial['cumulative'] <= 2 * 1024**3 or + final['target_allocations'] - initial['target_allocations'] < + 34000 + (4 if nested and target == 65536 else 0) + or final['unknown_frees'] - initial['unknown_frees']): + raise AssertionError(name + ': sustained/nested/threaded allocation coverage differs') + report['cases'].append({'name': name, 'events': events}) + report['status'] = 'PASS' + except Exception as error: + report['error'] = str(error) + raise + finally: + report['cleanup_verified'] = bool(report['commands']) and all( + item.get('cleanup_verified') for item in report['commands']) + (output / 'report.json').write_text(json.dumps(report, indent=2) + '\n') + print(report['status'] + ': ' + str(output / 'report.json')) + + +if __name__ == '__main__': + main() diff --git a/tests/tools/darwin_native_heap_budget.py b/tests/tools/darwin_native_heap_budget.py new file mode 100644 index 00000000..9e182b5b --- /dev/null +++ b/tests/tools/darwin_native_heap_budget.py @@ -0,0 +1,254 @@ +"""Bounded Darwin-only requested-allocation retention proof for native fixtures. + +This is not a whole-process address-space, committed-memory, or RSS limit. +The unchanged native fixture must independently demonstrate its expected calloc +payload count, and withheld real frees must make that same executable fail. +The tracker is injected only into fresh owned consumers, never the compiler or +the Python process-ownership gate. Raw successes and failures are retained. +""" +from __future__ import annotations + +import argparse +import json +import os +from pathlib import Path +import re +import secrets +import sys +import tempfile +import time + +import ordinary_io_parity_contract as support + +ROOT = Path(__file__).resolve().parents[2] +BUDGET = 96 * 1024 * 1024 +METRIC = '96 MiB live requested native heap allocations (Darwin test interposer)' +ALLOCATORS = {'malloc', 'calloc', 'realloc', 'free', 'aligned_alloc', 'posix_memalign', 'valloc'} +ALLOCATORS |= {'malloc_type_' + name for name in tuple(ALLOCATORS)} + + +def add_arguments(parser): + parser.add_argument('--heap-budget-library', type=Path) + parser.add_argument('--heap-budget-control', type=Path) + parser.add_argument('--output-parent', type=Path, + help='Retain Darwin raw evidence in a fresh child directory') + + +class DarwinHeapBudget: + def __init__(self, args, name, *, seconds, fixtures): + if sys.platform != 'darwin': + raise AssertionError('Darwin interposition requires actual macOS execution') + if not args.heap_budget_library or not args.heap_budget_control: + raise AssertionError('Darwin lifetime tests require the heap budget library and control executable') + self.library = args.heap_budget_library.resolve(strict=True) + self.control = args.heap_budget_control.resolve(strict=True) + parent = args.output_parent.resolve() if args.output_parent else Path(tempfile.gettempdir()) + parent.mkdir(parents=True, exist_ok=True) + self.output = Path(tempfile.mkdtemp(prefix='darwin-' + name + '-', dir=parent)) + self.work = self.output / 'work' + self.logs = self.output / 'logs' + self.work.mkdir(); self.logs.mkdir() + self.environment = {key: value for key, value in os.environ.items() + if not key.startswith(('NY_', 'NIL_', 'DYLD_'))} + self.deadline = time.monotonic() + seconds + self.ownership = support.load_ownership(ROOT) + self.expected_fixtures = fixtures + self.report = {'schema': 'nil.darwin-native-heap-budget.v1', 'status': 'FAIL', + 'metric': METRIC, 'budget_bytes': BUDGET, + 'scope': 'intercepted malloc-family requested payload retention; excludes tracker storage, ' + 'allocator overhead, VM mappings and allocation paths bypassing these functions', + 'whole_process_memory_parity': False, 'whole_seconds': seconds, + 'commands': [], 'runs': [], 'imports': [], 'fixtures': [], + 'inputs': {}} + inputs = [self.library, self.control, Path(__file__), + ROOT / 'tests/darwin_native_heap_budget.c', + ROOT / 'tests/darwin_native_heap_budget.h', + ROOT / 'tests/darwin_native_heap_budget_control.c', + ROOT / 'validation/ny-3d/scripts/debugger_process.py'] + if fixtures: + inputs.extend([args.compiler.resolve(strict=True), + ROOT / ('tests/tools/native_' + name.replace('-', '_') + '.py')]) + for path in inputs: + self.report['inputs'][str(path)] = support.file_record(path) + + def __enter__(self): + # Preparation is inside the lifetime script's with block so its failure + # also reaches __exit__ and retains the report. + return str(self.work) + + def __exit__(self, kind, error, traceback): + if error is not None: + self.report['error'] = str(error) + else: + try: + if len(self.report['fixtures']) != self.expected_fixtures: + raise AssertionError('not all requested native fixtures completed') + for path, record in self.report['inputs'].items(): + if support.file_record(Path(path)) != record: + raise AssertionError('contract input changed during execution: ' + path) + self.report['status'] = 'PASS' + except Exception as failure: + self.report['error'] = str(failure) + self.report['cleanup_verified'] = bool(self.report['commands']) and all( + row.get('cleanup_verified') is True for row in self.report['commands']) + if not self.report['cleanup_verified']: + self.report['status'] = 'FAIL' + path = self.output / 'report.json' + path.write_text(json.dumps(self.report, indent=2) + '\n', encoding='utf-8') + print(self.report['status'] + ': ' + str(path)) + if error is None and self.report['status'] != 'PASS': + raise AssertionError(self.report.get('error', 'owned cleanup failed')) + return False + + def command(self, name, command, *, stdout=b'', stderr=b'', exit_code=0, seconds=30): + remaining = self.deadline - time.monotonic() + if remaining <= 0: + raise AssertionError('whole Darwin heap contract deadline exhausted') + row = support.run_process(self.ownership, [str(part) for part in command], self.work, + self.environment, self.logs, name, min(seconds, remaining)) + row['expected_exit_code'] = exit_code + self.report['commands'].append(row) + actual = (self.logs / row['stdout']).read_bytes() + errors = (self.logs / row['stderr']).read_bytes() + if (row.get('exit_code') != exit_code or not row.get('cleanup_verified') or + row.get('error') or (stdout is not None and actual != stdout) or errors != stderr): + raise AssertionError(name + ': output/exit/deadline/owned-cleanup contract failed; ' + f"exit={row.get('exit_code')} expected={exit_code}, " + f"cleanup={row.get('cleanup_verified')}, " + f"error={row.get('error')!r}, stdout={actual[:1024]!r}, " + f"stderr={errors[:1024]!r}") + row['status'] = 'PASS' # Expected negative exits are evidence, not skipped tests. + return actual + + def inspect(self, name, executable, *, fixture=False): + imports = self.command(name + '-imports', ['/usr/bin/nm', '-u', executable], stdout=None) + self.command(name + '-dependencies', ['/usr/bin/otool', '-L', executable], stdout=None) + candidates = set(re.findall(rb'\b_((?:malloc|calloc|realloc|free|aligned_alloc|posix_memalign|valloc)[A-Za-z0-9_]*)\b', imports)) + candidates = {value.decode('ascii') for value in candidates} + # Queries do not allocate. Zone allocation, reallocf, and future typed + # entry points remain fail-closed until explicitly covered and tested. + candidates -= {'malloc_size', 'malloc_good_size', 'malloc_zone_from_ptr', 'malloc_default_zone'} + unsupported = candidates - ALLOCATORS + row = {'name': name, 'path': str(executable), 'allocator_imports': sorted(candidates), + 'unsupported_allocator_imports': sorted(unsupported), **support.file_record(executable)} + self.report['imports'].append(row) + if unsupported: + raise AssertionError(name + ': uncovered allocator imports: ' + ', '.join(sorted(unsupported))) + if fixture and (not candidates & {'calloc', 'malloc_type_calloc'} or + not candidates & {'free', 'malloc_type_free'}): + raise AssertionError(name + ': expected native runtime calloc/free imports absent') + + def injected(self, name, executable, arguments=(), *, target, withhold=False, + wanted=b'', negative=False): + events_path = self.logs / (name + '.heap.jsonl') + token = secrets.token_hex(16) + # The ownership helper starts a Python gate. Supplying DYLD_* in its + # environment would instrument that gate before exec. env assignments + # are parsed only after the gate execs this system utility. + command = ['/usr/bin/env', 'DYLD_INSERT_LIBRARIES=' + str(self.library), + 'NIL_DARWIN_HEAP_REPORT=' + str(events_path), + 'NIL_DARWIN_HEAP_TOKEN=' + token, + 'NIL_DARWIN_HEAP_TARGET=' + str(target), + 'NIL_DARWIN_HEAP_WITHHOLD=' + ('1' if withhold else '0'), + str(executable), *arguments] + self.command(name, command, stdout=wanted, exit_code=86 if negative else 0, seconds=45) + record = support.file_record(events_path, 8192) + events = [json.loads(line) for line in events_path.read_text(encoding='ascii').splitlines()] + if (len(events) != 2 or events[0].get('event') != 'ready' or + events[1].get('event') != ('budget' if negative else 'final')): + raise AssertionError(name + ': missing or malformed injection/final handshake') + for event in events: + if (event.get('token') != token or event.get('version') != 1 or + event.get('budget') != BUDGET or event.get('target_size') != target or + not isinstance(event.get('pid'), int) or event['pid'] <= 0 or + event['pid'] == os.getpid() or not 0 <= event.get('live', -1) <= event.get('peak', -1) <= BUDGET): + raise AssertionError(name + ': wrong process/configuration/accounting handshake') + initial, final = events + if initial['pid'] != final['pid']: + raise AssertionError(name + ': report crossed process identity') + since_ready = {} + for key in ('allocations', 'frees', 'cumulative', 'target_allocations', 'target_frees', + 'typed_calls', 'reallocations', 'failures', 'unknown_frees', 'withheld'): + if final[key] < initial[key]: + raise AssertionError(name + ': allocator counters moved backwards') + since_ready[key] = final[key] - initial[key] + if negative: + if (not 0 < final['denied_request'] <= target or + final['live'] < BUDGET - 2 * target or + since_ready['target_allocations'] * target < BUDGET * .8 or + (withhold and since_ready['withheld'] == 0)): + raise AssertionError(name + ': retention did not cause the bounded allocation rejection') + elif final['withheld'] or final['denied_request']: + raise AssertionError(name + ': ordinary fixture used negative-control behavior') + self.report['runs'].append({'name': name, 'expected_budget_rejection': negative, + 'withhold_target_frees': withhold, 'events': events, + 'since_ready': since_ready, + 'report_file': events_path.name, **record}) + return since_ready + + def prepare(self): + self.command('host-version', ['/usr/bin/sw_vers'], stdout=None) + self.command('host-machine', ['/usr/bin/uname', '-m'], stdout=None) + self.command('clang-version', ['/usr/bin/xcrun', 'clang', '--version'], stdout=None) + self.command('sdk-version', ['/usr/bin/xcrun', '--show-sdk-version'], stdout=None) + self.inspect('interposer', self.library) + self.inspect('control', self.control) + self.command('injection-absent', [self.control, 'positive'], exit_code=90, + stderr=b'heap budget injection absent\n') + positive = self.injected('control-positive', self.control, ['positive'], target=65536, + wanted=b'heap-controls-ok\n') + if (positive['cumulative'] <= 2 * 1024**3 or positive['target_allocations'] < 34000 or + positive['target_frees'] < 34000 or positive['failures'] < 2): + raise AssertionError('positive/realloc/failure control coverage insufficient') + self.injected('control-retained-untouched', self.control, ['retain'], target=65536, negative=True) + self.injected('control-withheld-free', self.control, ['positive'], target=65536, + withhold=True, negative=True) + guarded = self.injected('control-guarded-positive', self.control, ['guarded-positive'], + target=65540, wanted=b'guarded-controls-ok\n') + if guarded['target_allocations'] != 34000 or guarded['target_frees'] != 34000: + raise AssertionError('guarded native-buffer allocation/free control coverage differs') + self.injected('control-guarded-retained', self.control, ['guarded-retain'], target=65540, + negative=True) + self.injected('control-guarded-withheld', self.control, ['guarded-positive'], target=65540, + withhold=True, negative=True) + + def compile(self, name, command): + self.command(name, command, seconds=90) + + def fixture(self, name, source, executable, expected, *, target, count): + source_record = support.file_record(source) + executable_record = support.file_record(executable) + self.inspect(name, executable, fixture=True) + final = self.injected(name, executable, target=target, wanted=expected.encode('utf-8')) + if (final['target_allocations'] < count or final['target_frees'] < count - 1 or + final['cumulative'] < target * count): + raise AssertionError(name + ': expected unchanged-loop payload coverage absent') + # The exact same native executable must fail when actual target frees + # are withheld. No Ny source mutation or runtime GC counters are used. + self.injected(name + '-withheld-free', executable, target=target, + withhold=True, negative=True) + if (support.file_record(source) != source_record or + support.file_record(executable) != executable_record): + raise AssertionError(name + ': source or executable changed between controls') + self.report['fixtures'].append({'name': name, 'source': str(source), + 'source_record': source_record, 'executable_record': executable_record, + 'expected_target_allocations': count, 'target_bytes': target, + 'positive_and_withheld_free_replay': 'PASS'}) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + add_arguments(parser) + parser.add_argument('--controls-only', action='store_true', required=True, + help='Qualify injection and allocation accounting without building native fixtures') + args = parser.parse_args() + harness = DarwinHeapBudget(args, 'heap-controls', seconds=180, fixtures=0) + with harness: + # The identical positive, untouched-retention, and withheld-free controls + # used by both lifetime contracts run before the expensive compiler build. + # Passing this command does not qualify either native lifetime fixture. + harness.prepare() + + +if __name__ == '__main__': + main() diff --git a/tests/tools/file_modified_at_contract.py b/tests/tools/file_modified_at_contract.py new file mode 100644 index 00000000..269ad408 --- /dev/null +++ b/tests/tools/file_modified_at_contract.py @@ -0,0 +1,181 @@ +#!/usr/bin/env python3 +"""Compare real file metadata with the public VM/JIT/AOT timestamp operation.""" +from __future__ import annotations + +import argparse +import hashlib +import json +import os +from pathlib import Path +import sys +import tempfile +import time + +import ordinary_io_parity_contract as support + +ROOT = Path(__file__).resolve().parents[2] +sys.path.insert(0, str(ROOT / 'tests')) +from native_output_staging import native_output_path + +ERROR = 'file.modified_at: the file has no readable timestamp' + + +def literal(value: str) -> str: + return '"' + value.replace('\\', '\\\\').replace('"', '\\"') + '"' + + +def source_for(paths: list[Path], nul_case: bool = False) -> str: + calls = '\n'.join(' emit(' + literal(path.name) + ')' for path in paths) + if nul_case: + calls = ' emit(' + literal(paths[0].name)[:-1] + '\\0ignored")' + return '''import nil.std.core +import nil.std.io.file +fn emit(path: text) + match nil::std::io::modified_at(path) + Result.Ok(value) -> println(value) + Result.Error(message) -> println(message) + end match +end fn +fn main() -> i32 +''' + ('' if nul_case else ' for index in range(100)\n emit(' + literal(paths[0].name) + ')\n end for\n') + calls + ''' + return 0 +end fn +''' + + +def signature(paths: list[Path]) -> dict: + result = {} + for path in paths: + if not path.exists(): + result[path.name] = {'missing': True} + continue + info = path.stat() + result[path.name] = {'modified_ns': info.st_mtime_ns, + 'milliseconds': info.st_mtime_ns // 1_000_000, + 'size': info.st_size, + 'directory': path.is_dir()} + if path.is_file(): + result[path.name]['sha256'] = hashlib.sha256(path.read_bytes()).hexdigest() + return result + + +def expected(paths: list[Path]) -> bytes: + values = [str(path.stat().st_mtime_ns // 1_000_000) if path.exists() else ERROR + for path in paths] + return ('\n'.join([values[0]] * 100 + values) + '\n').encode('utf-8') + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--compiler', type=Path, required=True) + parser.add_argument('--interpreter', type=Path, required=True) + parser.add_argument('--nilstd', type=Path, required=True) + parser.add_argument('--llvm', choices=('ON', 'OFF'), required=True) + destination = parser.add_mutually_exclusive_group() + destination.add_argument('--output', type=Path, + help='Fresh exact evidence directory') + destination.add_argument('--output-parent', type=Path, + help='Keep each run in a fresh child of this directory') + args = parser.parse_args() + compiler = args.compiler.resolve(strict=True) + interpreter = args.interpreter.resolve(strict=True) + nilstd = args.nilstd.resolve(strict=True) + if args.output_parent: + parent = args.output_parent.resolve() + parent.mkdir(parents=True, exist_ok=True) + output = Path(tempfile.mkdtemp(prefix='nil-file-time-contract-', dir=parent)) + elif args.output: + output = args.output.resolve() + output.mkdir(parents=True, exist_ok=False) + else: + output = Path(tempfile.mkdtemp(prefix='nil-file-time-contract-')) + work = output / 'work' + work.mkdir() + logs = output / 'logs' + logs.mkdir() + report = {'schema': 'nil.file-modified-at.v1', 'status': 'FAIL', + 'llvm_enabled': args.llvm == 'ON', 'commands': [], 'routes': [], + 'native_routes': 'enabled' if args.llvm == 'ON' else 'disabled by build configuration', + 'native_text_scope': 'NUL-free; embedded NUL checked on interpreter routes', + 'epoch_rule': 'floor native last-write timestamp to Unix milliseconds', + 'whole_seconds': 160} + deadline = time.monotonic() + 160 + ownership = support.load_ownership(ROOT) + environment = {key: value for key, value in os.environ.items() + if not key.startswith(('NIL_', 'NY_'))} + + def run(name, command, wanted=b'', limit=30): + remaining = deadline - time.monotonic() + if remaining <= 0: + raise AssertionError('whole timestamp contract deadline exhausted') + row = support.run_process(ownership, [str(value) for value in command], work, + environment, logs, name, min(limit, remaining)) + report['commands'].append(row) + actual = (logs / row['stdout']).read_bytes() + error = (logs / row['stderr']).read_bytes() + if row['status'] != 'PASS' or not row['cleanup_verified'] or actual != wanted or error: + raise AssertionError(name + ': exact timestamp/exit/stderr/cleanup contract failed') + + try: + paths = [work / 'fraction-café-日本語-🦊.txt', work / 'epoch.txt', + work / 'boundary.txt', work / 'before-epoch.txt', + work / 'directory-café', work / 'absent.txt'] + requested = [1600000000123456700, 0, 1600000000999999900, -1000100] + for path, nanoseconds in zip(paths[:4], requested): + path.write_bytes(b'unchanged metadata fixture\n') + os.utime(path, ns=(nanoseconds, nanoseconds)) + paths[4].mkdir() + os.utime(paths[4], ns=(requested[0], requested[0])) + report['requested_file_times_ns'] = dict(zip((p.name for p in paths[:4]), requested)) + report['before'] = signature(paths) + source = work / 'metadata.ny' + source.write_text(source_for(paths), encoding='utf-8', newline='\n') + base = [compiler, '--quiet', '--no-stdlib', '--sdk-path', nilstd] + commands = [] + for encoding in ('text', 'binary'): + nil = work / (encoding + '.nil') + flags = [] if encoding == 'text' else ['--emit-binary-nil'] + run('compile-' + encoding, [*base, '--no-bundle', *flags, '-o', nil, source]) + if nil.read_bytes().startswith(b'NIL\0') != (encoding == 'binary'): + raise AssertionError('compiled NIL encoding differs') + commands.append((encoding + '-interpreter', [interpreter, '--no-ai', nil])) + if args.llvm == 'ON': + native = work / 'native' + run('compile-native', [*base, '--native', '-o', native, source], limit=60) + commands.append(('llvm-aot', [native_output_path(native)])) + commands.append(('llvm-jit', [*base, '--native', '--jit', '-o', work / 'jit', source])) + for stage in ('initial', 'updated'): + if stage == 'updated': + old = paths[0].stat().st_mtime_ns + os.utime(paths[0], ns=(old + 2345678900, old + 2345678900)) + if paths[0].stat().st_mtime_ns // 1_000_000 == old // 1_000_000: + raise AssertionError('host did not change the fixture timestamp') + before = signature(paths) + for name, command in commands: + run(stage + '-' + name, command, expected(paths), limit=60) + report['routes'].append({'stage': stage, 'route': name, 'status': 'PASS', + 'stable_repetitions': 100}) + if signature(paths) != before: + raise AssertionError('timestamp reads changed the fixtures') + # NUL is a reserved DOS device name, even with a file extension. + nul = work / 'embedded-zero-path.ny' + nul.write_text(source_for(paths, nul_case=True), encoding='utf-8', newline='\n') + for encoding in ('text', 'binary'): + output_nil = work / ('nul-' + encoding + '.nil') + flags = [] if encoding == 'text' else ['--emit-binary-nil'] + run('compile-nul-' + encoding, [*base, '--no-bundle', *flags, '-o', output_nil, nul]) + run('nul-' + encoding, [interpreter, '--no-ai', output_nil], (ERROR + '\n').encode()) + report['after'] = signature(paths) + report['status'] = 'PASS' + except Exception as error: + report['error'] = str(error) + report['cleanup_verified'] = all(row.get('cleanup_verified') is True for row in report['commands']) + (output / 'report.json').write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8') + print(report['status'] + ': ' + str(output / 'report.json')) + if report['status'] != 'PASS': + print(report.get('error', 'timestamp contract failed'), file=sys.stderr) + return 0 if report['status'] == 'PASS' else 1 + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/tests/tools/formatted_text_contract.py b/tests/tools/formatted_text_contract.py index 8f8ba250..19c57bbc 100644 --- a/tests/tools/formatted_text_contract.py +++ b/tests/tools/formatted_text_contract.py @@ -43,6 +43,79 @@ def main(): assert output.read_bytes() == b'previous artifact', (path, flags) assert result.stderr.strip(), (path, flags) checks += 1 + # Ny's multiline block rejects the same malformed holes. + ny_blocks = [ + 'hello\n', # never closed + 'a } b\n"\"\"\n', # unmatched brace + '{1 +\n2}\n"\"\"\n', # hole spans a line + '{}\n"\"\"\n', # empty hole + '{unknown}\n"\"\"\n', # undefined identifier + '{[1, 2]}\n"\"\"\n', # no text contract + ] + for index, block in enumerate(ny_blocks): + source = ('fn main() -> i32\n' + f'let result = $"\"\"\n{block}' + 'return 0\nend fn\n') + path = root / f'invalid-ny-block-{index}.ny' + path.write_text(source, newline='\n') + for flags in routes: + output = root / 'protected-output' + output.write_bytes(b'previous artifact') + result = subprocess.run([compiler, '--quiet', '--no-stdlib', + *flags, '--output', output, path], + capture_output=True, text=True, timeout=30) + assert result.returncode != 0, (path, flags, result.stdout) + assert output.read_bytes() == b'previous artifact', (path, flags) + assert result.stderr.strip(), (path, flags) + checks += 1 + + # A Ny block opener owns the rest of its line. Previously the lexer + # silently discarded trailing content instead of diagnosing the source. + for index, tail in enumerate(('lost content', ' \t{42}', '\t# comment')): + source = ('fn main() -> i32\n' + f'let result = $"\"\"{tail}\nkept\n"\"\"\n' + 'return 0\nend fn\n') + path = root / f'invalid-ny-block-header-{index}.ny' + path.write_text(source, newline='\n') + for flags in routes: + output = root / 'protected-output' + output.write_bytes(b'previous artifact') + result = subprocess.run([compiler, '--quiet', '--no-stdlib', + *flags, '--output', output, path], + capture_output=True, text=True, timeout=30) + assert result.returncode != 0, (path, flags, result.stdout) + assert output.read_bytes() == b'previous artifact', (path, flags) + assert 'formatted block opener must end its line' in result.stderr, (path, flags, result.stderr) + checks += 1 + + # A multiline Matrix block rejects the same malformed holes, and an + # unterminated block is a lexer error rather than a silent literal. + blocks = [ + 'hello\n', # never closed + 'a } b\nend text\n', # unmatched brace + '{1 plus\n2}\nend text\n', # hole spans a line + '{}\nend text\n', # empty hole + '{unknown}\nend text\n', # undefined identifier + '{[1, 2]}\nend text\n', # no text contract + ] + for index, block in enumerate(blocks): + source = ('book invalid_formatted_block\n' + 'main returns override integer begin paragraph\n' + f'value result is formatted begin text\n{block}' + 'give 0\nend paragraph\n') + path = root / f'invalid-block-{index}.matrix' + path.write_text(source, newline='\n') + for flags in routes: + output = root / 'protected-output' + output.write_bytes(b'previous artifact') + result = subprocess.run([compiler, '--quiet', '--no-stdlib', + *flags, '--output', output, path], + capture_output=True, text=True, timeout=30) + assert result.returncode != 0, (path, flags, result.stdout) + assert output.read_bytes() == b'previous artifact', (path, flags) + assert result.stderr.strip(), (path, flags) + checks += 1 + # A function identifier has a return type, but is not a scalar value. path = root / 'function.ny' path.write_text('fn number() -> i32\nreturn 1\nend fn\n' diff --git a/tests/tools/incremental_build_contract.py b/tests/tools/incremental_build_contract.py index e0b1010c..dc0bf716 100644 --- a/tests/tools/incremental_build_contract.py +++ b/tests/tools/incremental_build_contract.py @@ -191,6 +191,13 @@ def event(kind): module.write_bytes(next_contents.read_bytes()) os.utime(module, ns=(previous.st_atime_ns,previous.st_mtime_ns)) next_contents.unlink() +next_cache=entry.parent/'cache-content-after' +if next_cache.is_file(): + module=entry.parent/'dyld_shared_cache_arm64e.1' + previous=module.stat() + module.write_bytes(next_cache.read_bytes()) + os.utime(module, ns=(previous.st_atime_ns,previous.st_mtime_ns)) + next_cache.unlink() event('finish') ''', newline='\n') script.chmod(0o755) @@ -204,7 +211,11 @@ def run(*arguments, cwd=root, status=0, tool=fake): command = [str(builder), *map(str, arguments)] if arguments[0] in ('build', 'run', 'check'): command += ['--compiler', str(tool), '--sdk', str(stdlib)] - result = subprocess.run(command, cwd=cwd, text=True, capture_output=True, timeout=60) + # Actual whole-cache SHA on Intel has measured a 104 s first pass. + # Keep fake/containment checks at 60 s and bound real Darwin builds + # separately; this changes no assertion or cache admission rule. + timeout = 300 if sys.platform == 'darwin' and tool == compiler else 60 + result = subprocess.run(command, cwd=cwd, text=True, capture_output=True, timeout=timeout) checks += 1 assert result.returncode == status, (command, result.returncode, result.stdout, result.stderr) return result @@ -298,6 +309,24 @@ def create(directory, name, references=()): assert artifact.read_bytes()==original and state.read_bytes()==old_state run('build','--project',project) assert artifact.read_bytes()!=original and not state.exists() + + # A compiler may report a source or manifest as a loaded input as + # well. Shared toolchain hashing must not hide its fresh source + # check, including preserved-length/timestamp mutation. + project=create(root/'overlapping-source-module','app') + dependency=project.parent/'dependency.input' + dependency.write_bytes(b'source version 1') + (project.parent/'compiler-modules.json').write_text(json.dumps([ + str(project), str(project.parent/'main.matrix'), str(dependency)])) + run('build','--project',project) + artifact=project.parent/'build/app.nil';state=artifact.parent/'.nilbuild/app.nil.nildata' + original,old_state=artifact.read_bytes(),state.read_bytes() + stamp=dependency.stat() + (project.parent/'dependency-content-after').write_bytes(b'source version 2') + rejected=run('build','--project',project,'--rebuild',status=2) + assert 'build input changed during the build' in rejected.stderr and str(dependency) in rejected.stderr,rejected.stderr + assert artifact.read_bytes()==original and state.read_bytes()==old_state + assert dependency.stat().st_mtime_ns==stamp.st_mtime_ns print(f'nilbuild compiler identity: {checks} explicit certification, uncached build and source protection checks passed') return @@ -505,8 +534,32 @@ def create(directory, name, references=()): assert content_artifact.read_bytes()!=old_artifact assert 'up to date:' in run('build','--project',content_project).stderr - # Positive parity remains required here, including on Darwin where - # incomplete loaded-image discovery deliberately prevents reuse. + # Whole shared-cache files use the same ordinary content identities. + # Changing a tail byte with the header/length/timestamp unchanged must + # invalidate a hit and reject publication if it occurs during compile. + cache_project=create(root/'cache-content','cache-content') + cache_dir=cache_project.parent + backing=cache_dir/'dyld_shared_cache_arm64e.1' + cache_bytes=b'unchanged cache header and UUID' + bytes(1024 * 1024) + b'1' + backing.write_bytes(cache_bytes) + (cache_dir/'compiler-modules.json').write_text(json.dumps([str(backing)])) + run('build','--project',cache_project) + assert 'up to date:' in run('build','--project',cache_project).stderr + stamp=backing.stat() + backing.write_bytes(cache_bytes[:-1]+b'2') + os.utime(backing,ns=(stamp.st_atime_ns,stamp.st_mtime_ns)) + assert 'up to date:' not in run('build','--project',cache_project).stderr + cache_artifact=cache_dir/'build/cache-content.nil' + cache_state=cache_dir/'build/.nilbuild/cache-content.nil.nildata' + original_cache_artifact,original_cache_state=cache_artifact.read_bytes(),cache_state.read_bytes() + (cache_dir/'main.matrix').write_text('version two\n',newline='\n') + (cache_dir/'cache-content-after').write_bytes(cache_bytes[:-1]+b'3') + changed=run('build','--project',cache_project,'--rebuild',status=2) + assert 'build input changed during the build' in changed.stderr and str(backing) in changed.stderr,changed.stderr + assert cache_artifact.read_bytes()==original_cache_artifact and cache_state.read_bytes()==original_cache_state + assert backing.stat().st_mtime_ns==stamp.st_mtime_ns and backing.read_bytes()==cache_bytes[:-1]+b'3' + + # Positive reuse is required on every supported host. # Containment is covered independently by --compiler-identity-only. # Real compiler smoke: unchanged imports skip compilation for both routes. actual=root/'real';actual.mkdir() diff --git a/tests/tools/markup_contract.py b/tests/tools/markup_contract.py new file mode 100644 index 00000000..3079b59e --- /dev/null +++ b/tests/tools/markup_contract.py @@ -0,0 +1,343 @@ +#!/usr/bin/env python3 +"""A markup block rejects malformed documents and escapes what it interpolates. + +The rejections are the point of the construct: a document whose elements do not +balance, a hole that would name an element or an attribute, and a conditional +arm that opens an element it does not close are all errors here rather than +broken HTML at run time. The positive checks prove the escaping that makes the +rejections worth having. +""" + +import argparse +import os +from pathlib import Path +import subprocess +import tempfile + + +HEAD = 'book markup_rejection\nread everything from nil.std.markup\n' + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--compiler', type=Path, required=True) + parser.add_argument('--interpreter', type=Path, required=True) + parser.add_argument('--stdlib', type=Path, required=True) + parser.add_argument('--llvm', choices=('ON', 'OFF'), required=True) + args = parser.parse_args() + compiler = args.compiler.resolve() + stdlib = args.stdlib.resolve() + routes = [('--no-bundle',), ('--no-bundle', '--emit-binary-nil')] + if args.llvm == 'ON': + routes += [('--native',), ('--native', '--jit')] + checks = 0 + + rejected = [ + # (name, body of main, whether nil.std.markup is read) + ('mismatched-close', '

x

', True), + ('unclosed-element', '

x

', True), + ('stray-close', '

', True), + ('arm-opens-only', '
{when flag}

a{end when}

', True), + ('arm-closes-foreign', '{repeat for each n in rows}
{end repeat}', True), + ('hole-names-attribute', '
y
', True), + ('hole-names-element', '<{label}>y', True), + ('unquoted-attribute', '
y
', True), + ('document-in-attribute', '
y
', True), + ('script-hole-without-raw', '', True), + ('comment-hole', '', True), + ('end-without-open', '

x

{end when}', True), + ('repeat-over-scalar', '{repeat for each n in flag}

x

{end repeat}', True), + ('condition-not-boolean', '{when label}

x

{end when}', True), + ('unsupported-hole-type', '

{rows}

', True), + ('missing-read', '

x

', False), + ] + with tempfile.TemporaryDirectory(prefix='nil-markup-contract-') as directory: + root = Path(directory) + for name, body, reads in rejected: + head = HEAD if reads else 'book markup_rejection\n' + source = (head + 'main returns override integer begin paragraph\n' + ' value flag is true\n' + ' value label as text is "cls"\n' + ' value rows as array of number is [1, 2]\n' + + (' value inner is begin markup\n x\n end markup\n' + if reads else '') + + ' value page is begin markup\n' + f' {body}\n' + ' end markup\n' + ' give 0\n' + 'end paragraph\n') + path = root / f'{name}.matrix' + path.write_text(source, encoding='utf-8', newline='\n') + for flags in routes: + output = root / 'protected-output' + output.write_bytes(b'previous artifact') + result = subprocess.run([compiler, '--quiet', '-I', str(stdlib), + *flags, '--output', output, path], + capture_output=True, text=True, encoding='utf-8', timeout=60) + assert result.returncode != 0, (name, flags, result.stdout) + assert output.read_bytes() == b'previous artifact', (name, flags) + assert result.stderr.strip(), (name, flags) + checks += 1 + + # Ny rejects the same documents through the same shared structure, + # with its own control words. + ny_rejected = [ + ('ny-mismatched-close', '

x

'), + ('ny-unclosed-element', '

x

'), + ('ny-arm-opens-only', '
{if flag}

a{end if}

'), + ('ny-hole-names-attribute', '
y
'), + ('ny-unquoted-attribute', '
y
'), + ('ny-document-in-attribute', '
y
'), + ('ny-script-hole-without-raw', ''), + ('ny-end-without-open', '

x

{end if}'), + ('ny-condition-not-boolean', '{if label}

x

{end if}'), + ('ny-unsupported-hole-type', '

{rows}

'), + ] + for name, body in ny_rejected: + source = ('import nil.std.markup.escaping\n' + 'fn main() -> i32\n' + ' let flag = true\n' + ' let label = "cls"\n' + ' let rows = [1, 2]\n' + ' let inner = $markup\n x\n end markup\n' + ' let page = $markup\n' + f' {body}\n' + ' end markup\n' + ' return 0\n' + 'end fn\n') + path = root / f'{name}.ny' + path.write_text(source, encoding='utf-8', newline='\n') + for flags in routes: + output = root / 'protected-output' + output.write_bytes(b'previous artifact') + result = subprocess.run([compiler, '--quiet', '-I', str(stdlib), + *flags, '--output', output, path], + capture_output=True, text=True, encoding='utf-8', timeout=60) + assert result.returncode != 0, (name, flags, result.stdout) + assert output.read_bytes() == b'previous artifact', (name, flags) + assert result.stderr.strip(), (name, flags) + checks += 1 + + # HTML escaping is not JavaScript/CSS/nested-document escaping. These + # contexts and URL lists require an explicit trusted raw value. Reject + # at compile time on every route, preserving the previous artifact. + shared_rejected = [ + ('event-handler', ''), + ('mixed-event-handler', ''), + ('style-attribute', '

x

'), + ('nested-document', ''), + ('srcset', ''), + ('imagesrcset', ''), + ('ping', 'x'), + ('dynamic-scheme', 'x'), + ('dynamic-scheme-fragment', 'x'), + ('two-url-holes', 'x'), + ('executable-prefix', 'x'), + ('entity-scheme-prefix', 'x'), + ('raw-attribute-name', '
y
'), + ('raw-comment', ''), + ('unfinished-comment', ''), + ('split-boolean-attribute', f''), + ('self-closing-arm-parent', f'
{{label}}
'), + ] + for name, body in shared_rejected + contextual_rejected: + if language == 'ny': + source = ('import nil.std.markup.escaping\nfn main() -> i32\n' + 'let flag = true\nlet label = "marker"\n' + f'let page = $markup\n{body}\nend markup\nreturn 0\nend fn\n') + else: + source = (HEAD + 'main returns override integer begin paragraph\n' + 'value flag is true\nvalue label as text is "marker"\n' + f'value page is begin markup\n{body}\nend markup\ngive 0\nend paragraph\n') + path = root / f'context-{name}.{language}' + path.write_text(source, encoding='utf-8', newline='\n') + for flags in routes: + output = root / 'protected-context-output' + output.write_bytes(b'previous artifact') + result = subprocess.run([compiler, '--quiet', '-I', str(stdlib), + *flags, '--output', output, path], + capture_output=True, text=True, encoding='utf-8', timeout=60) + assert result.returncode != 0, (name, language, flags, result.stdout) + assert output.read_bytes() == b'previous artifact', (name, language, flags) + assert result.stderr.strip(), (name, language, flags) + checks += 1 + + def render_routes(name, language, source, expected): + nonlocal checks + path = root / f'{name}.{language}' + path.write_text(source, encoding='utf-8', newline='\n') + for route, flags in enumerate(routes): + artifact = root / f'{name}-{language}-{route}' + result = subprocess.run([compiler, '--quiet', '-I', str(stdlib), + *flags, '--output', artifact, path], + capture_output=True, text=True, encoding='utf-8', timeout=60) + assert result.returncode == 0, (name, flags, result.stderr) + if '--jit' in flags: + actual = result.stdout + else: + executable = artifact.with_suffix('.exe') if os.name == 'nt' else artifact + command = ([executable] if '--native' in flags else + [args.interpreter.resolve(), artifact]) + run = subprocess.run(command, capture_output=True, text=True, encoding='utf-8', timeout=60) + assert run.returncode == 0, (name, flags, run.stderr) + actual = run.stdout + assert actual == expected, (name, language, flags, actual, expected) + checks += 1 + + # Both surfaces use the same context decisions, and literal script + # lookalikes inside comments/quoted attributes never hide later holes. + for language in ('ny', 'matrix'): + opening, alternative, end = (('if', 'else', 'end if') if language == 'ny' + else ('when', 'otherwise', 'end when')) + body = ('path\n' + 'absolute\n' + 'query\n' + 'fragment\n' + 'whole\n' + 'svg\n' + '

{label}

\n' + '\n

{label}

\n' + '\n' + '\n' + '\n' + f'
x
\n' + f'

text

\n' + '') + expected = ('path\n' + 'absolute\n' + 'query\n' + 'fragment\n' + 'whole\n' + 'svg\n' + '

<marker>&

\n' + '\n

<marker>&

\n' + '\n' + '\n' + '\n' + '
x
\n' + '

text

\n' + '\n') + if language == 'ny': + source = ('import nil.std.markup.escaping\nfn main() -> i32\n' + 'let flag = true\nlet label = "&"\n' + 'let target = "JaVaScRiPt:marker"\nlet trusted = "marker()"\n' + f'let page = $markup\n{body}\nend markup\n' + 'println(nil.std.markup::render(page))\nreturn 0\nend fn\n') + else: + source = (HEAD + 'main returns override integer begin paragraph\n' + 'value flag is true\nvalue label as text is "&"\n' + 'value target as text is "JaVaScRiPt:marker"\nvalue trusted as text is "marker()"\n' + f'value page is begin markup\n{body}\nend markup\n' + 'say nil.std.markup::render with doc being page\ngive 0\nend paragraph\n') + render_routes('context-positive', language, source, expected) + + # The whole URL decision follows browser control normalization. These + # strings remain data; no browser or JavaScript execution is needed. + controls = [('java\\tscript:marker', 'java\tscript%3amarker'), + ('java\\nscript:marker', 'java\nscript%3amarker'), + ('java\\rscript:marker', 'java\nscript%3amarker'), + ('\\v\\f JaVaScRiPt:marker', '\v\f JaVaScRiPt%3amarker'), + ('\x01javascript:marker', '\x01javascript%3amarker')] + source = 'import nil.std.markup.escaping\nfn main() -> i32\n' + expected = '' + for index, (value, escaped) in enumerate(controls): + source += f'let value{index} = "{value}"\nlet page{index} = $markup\nx\nend markup\n' + source += f'println(nil.std.markup::render(page{index}))\n' + expected += f'x\n' + source += ('let page = $markup\n

{"a\\\"}b"}

\n

{"x" /* } */}

\nend markup\n' + 'println(nil.std.markup::render(page))\nreturn 0\nend fn\n') + expected += '

a"}b

\n

x

\n' + render_routes('url-controls-and-ny-holes', 'ny', source, expected) + + # The line-anchored terminator is a keyword, not the prefix of a + # longer body word, and Ny still permits expression continuation. + source = ('import nil.std.markup.escaping\nfn main() -> i32\n' + 'println(nil.std.markup::render($markup\n

\n' + 'end markupExtra\nend markup_extra\nend markupé\n

\n' + 'end markup))\nreturn 0\nend fn\n') + render_routes('ny-terminator-boundary', 'ny', source, + '

\nend markupExtra\nend markup_extra\nend markupé\n

\n') + + # An unterminated Ny block is a lexer error too. + path = root / 'ny-unterminated.ny' + path.write_text('import nil.std.markup.escaping\n' + 'fn main() -> i32\n let page = $markup\n

x

\n' + ' return 0\nend fn\n', encoding='utf-8', newline='\n') + result = subprocess.run([compiler, '--quiet', '-I', str(stdlib), '--no-bundle', + '--output', root / 'ny-unterminated.nil', path], + capture_output=True, text=True, encoding='utf-8', timeout=60) + assert result.returncode != 0 and 'markup block' in result.stderr, result.stderr + checks += 1 + + # An unterminated block is a lexer error, before any markup parsing. + path = root / 'unterminated.matrix' + path.write_text(HEAD + 'main returns override integer begin paragraph\n' + ' value page is begin markup\n

x

\n' + ' give 0\nend paragraph\n', encoding='utf-8', newline='\n') + result = subprocess.run([compiler, '--quiet', '-I', str(stdlib), '--no-bundle', + '--output', root / 'unterminated.nil', path], + capture_output=True, text=True, encoding='utf-8', timeout=60) + assert result.returncode != 0 and 'begin markup' in result.stderr, result.stderr + checks += 1 + + # Hostile values reach the page escaped, in every context, and a + # document composed into another is not escaped a second time. + hostile = (HEAD + + 'inner receives name as text returns nil.std.markup.document begin paragraph\n' + ' give begin markup\n {name}\n end markup\n' + 'end paragraph\n' + 'main returns override integer begin paragraph\n' + ' value attack as text is ""\n' + ' value page is begin markup\n' + '
{attack}
\n' + ' q\n' + ' {inner with name being attack}\n' + ' end markup\n' + ' say nil.std.markup::render with doc being page\n' + ' give 0\n' + 'end paragraph\n') + path = root / 'escaping.matrix' + path.write_text(hostile, encoding='utf-8', newline='\n') + artifact = root / 'escaping.nil' + result = subprocess.run([compiler, '--quiet', '-I', str(stdlib), '--no-bundle', + '--output', artifact, path], + capture_output=True, text=True, encoding='utf-8', timeout=60) + assert result.returncode == 0, result.stderr + run = subprocess.run([args.interpreter.resolve(), artifact], + capture_output=True, text=True, encoding='utf-8', timeout=60) + assert run.returncode == 0, run.stderr + page = run.stdout + # The only '<' that survive are the ones the document itself wrote. + assert '' not in page, page + assert '<script>alert(1)</script>' in page, page + assert 'class="<script>alert(1)</script>"' in page, page + assert '/q?v=%3cscript%3ealert(1)%3c%2fscript%3e' in page, page + assert '<script>alert(1)</script>' in page, page + assert '&lt;' not in page, page # composed documents escape once + checks += 6 + print(f'{checks} markup contract checks passed') + + +if __name__ == '__main__': + main() diff --git a/tests/tools/native_managed_lifetime.py b/tests/tools/native_managed_lifetime.py index a7892ff9..27882bb7 100644 --- a/tests/tools/native_managed_lifetime.py +++ b/tests/tools/native_managed_lifetime.py @@ -11,6 +11,7 @@ import tempfile from native_memory_support import memory_metric, run_memory_bounded, verify_memory_limit +from darwin_native_heap_budget import DarwinHeapBudget, METRIC, add_arguments def main() -> int: @@ -19,11 +20,15 @@ def main() -> int: parser.add_argument("--stdlib", type=Path, required=True) parser.add_argument("--llvm", choices=("ON", "OFF"), required=True) parser.add_argument("--memory-launcher", type=Path) + add_arguments(parser) args = parser.parse_args() - if sys.platform not in ("linux", "win32") or args.llvm == "OFF": + if sys.platform not in ("linux", "win32", "darwin") or args.llvm == "OFF": return 77 - with tempfile.TemporaryDirectory(prefix="ny-managed-lifetime café ") as temporary: + darwin = (DarwinHeapBudget(args, 'managed-lifetime', seconds=210, fixtures=1) + if sys.platform == 'darwin' else None) + context = darwin if darwin else tempfile.TemporaryDirectory(prefix="ny-managed-lifetime café ") + with context as temporary: root = Path(temporary) source = root / "sustained.ny" output = root / ("sustained.exe" if os.name == "nt" else "sustained") @@ -41,20 +46,28 @@ def main() -> int: end fn ''', encoding="utf-8", newline='\n') environment = {key: value for key, value in os.environ.items() if not key.startswith("NY_")} + if darwin: + darwin.prepare() verify_memory_limit(args.memory_launcher, cwd=root, environment=environment) - compiled = subprocess.run([str(args.compiler.resolve()), "--quiet", "--no-stdlib", - "--sdk-path", str(args.stdlib.resolve()), "--native", "--output", str(output), str(source)], - cwd=root, env=environment, text=True, capture_output=True, timeout=90) - if compiled.returncode != 0 or compiled.stdout != "" or compiled.stderr != "": - raise AssertionError(compiled) + command = [str(args.compiler.resolve()), "--quiet", "--no-stdlib", + "--sdk-path", str(args.stdlib.resolve()), "--native", "--output", str(output), str(source)] + if darwin: + darwin.compile('compile-managed', command) + else: + compiled = subprocess.run(command, cwd=root, env=environment, text=True, capture_output=True, timeout=90) + if compiled.returncode != 0 or compiled.stdout != "" or compiled.stderr != "": + raise AssertionError(compiled) # Apply to the compiled consumer only, never compiler/JIT host. # The unchanged loop allocates >625 MiB cumulatively. - result = run_memory_bounded(output, cwd=root, environment=environment, - launcher=args.memory_launcher) - if result.returncode != 0 or result.stdout != "799980000\n" or result.stderr != "": - raise AssertionError(result) - print("native managed lifetime: sustained allocation fits " + memory_metric()) + if darwin: + darwin.fixture('managed', source, output, "799980000\n", target=16384, count=40000) + else: + result = run_memory_bounded(output, cwd=root, environment=environment, + launcher=args.memory_launcher) + if result.returncode != 0 or result.stdout != "799980000\n" or result.stderr != "": + raise AssertionError(result) + print("native managed lifetime: sustained allocation fits " + (METRIC if darwin else memory_metric())) return 0 diff --git a/tests/tools/native_owned_lifetime.py b/tests/tools/native_owned_lifetime.py index 2072be4d..c7df9e70 100644 --- a/tests/tools/native_owned_lifetime.py +++ b/tests/tools/native_owned_lifetime.py @@ -8,6 +8,7 @@ import sys import tempfile from native_memory_support import memory_metric, run_memory_bounded, verify_memory_limit +from darwin_native_heap_budget import DarwinHeapBudget, METRIC, add_arguments def main() -> int: @@ -15,8 +16,9 @@ def main() -> int: parser.add_argument("--compiler", type=Path, required=True) parser.add_argument("--llvm", choices=("ON", "OFF"), required=True) parser.add_argument("--memory-launcher", type=Path) + add_arguments(parser) args = parser.parse_args() - if sys.platform not in ("linux", "win32") or args.llvm == "OFF": + if sys.platform not in ("linux", "win32", "darwin") or args.llvm == "OFF": return 77 source_text = """struct LifetimeProblem code:i32 @@ -55,24 +57,37 @@ def main() -> int: end fn """ environment = {key: value for key, value in os.environ.items() if not key.startswith("NY_")} - with tempfile.TemporaryDirectory(prefix="ny-owned-lifetime café ") as temporary: + darwin = (DarwinHeapBudget(args, 'owned-lifetime', seconds=330, fixtures=2) + if sys.platform == 'darwin' else None) + context = darwin if darwin else tempfile.TemporaryDirectory(prefix="ny-owned-lifetime café ") + with context as temporary: root = Path(temporary) + if darwin: + darwin.prepare() verify_memory_limit(args.memory_launcher, cwd=root, environment=environment) source = root / "sustained.ny" source.write_text(source_text, encoding="utf-8", newline='\n') for name, flags in (("managed", []), ("unmanaged", ["--unmanaged"])): output = root / (name + (".exe" if os.name == "nt" else "")) - compiled = subprocess.run([str(args.compiler.resolve()), "--no-stdlib", "--quiet", - "--native", *flags, "--output", str(output), str(source)], cwd=root, - env=environment, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=90) - if compiled.returncode != 0 or compiled.stderr: - raise AssertionError(f"{name} compile: {compiled}") - ran = run_memory_bounded(output, cwd=root, environment=environment, - launcher=args.memory_launcher) - if ran.returncode != 0 or ran.stderr or ran.stdout != "35994000\n": - raise AssertionError(f"{name} lifetime: {ran}") + command = [str(args.compiler.resolve()), "--no-stdlib", "--quiet", + "--native", *flags, "--output", str(output), str(source)] + if darwin: + darwin.compile('compile-' + name, command) + # The native raw-buffer ABI allocates one additional i32 guard + # element, so this unchanged owned(16384) loop requests + # exactly 65540 bytes for each payload, in both memory modes. + darwin.fixture(name, source, output, "35994000\n", target=65540, count=36000) + else: + compiled = subprocess.run(command, cwd=root, + env=environment, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=90) + if compiled.returncode != 0 or compiled.stderr: + raise AssertionError(f"{name} compile: {compiled}") + ran = run_memory_bounded(output, cwd=root, environment=environment, + launcher=args.memory_launcher) + if ran.returncode != 0 or ran.stderr or ran.stdout != "35994000\n": + raise AssertionError(f"{name} lifetime: {ran}") print("Owned normal/return/continue/fault/recover scopes: >2 GiB cumulative storage fits " + - memory_metric() + " in both memory modes") + (METRIC if darwin else memory_metric()) + " in both memory modes") return 0 diff --git a/tests/tools/native_symbol_surface.py b/tests/tools/native_symbol_surface.py index 343dd6ff..50acc2dc 100644 --- a/tests/tools/native_symbol_surface.py +++ b/tests/tools/native_symbol_surface.py @@ -7,13 +7,18 @@ satisfy with stubs it should never need, and a libc symbol arriving by accident is exactly the kind of drift that is invisible on a hosted host. -The ELF symbol table is read directly rather than shelling out to nm, so the -contract does not depend on binutils being installed. +The ELF symbol table is read directly. COFF and Mach-O use an explicitly +provided llvm-nm, with no demangling and only the Darwin ABI's one leading +underscore removed. The relocation checks apply only to ELF x86_64; the +import/export and LLVM-IR checks run on every supported object profile. """ from __future__ import annotations import argparse +from functools import partial from pathlib import Path +import platform +import re import struct import subprocess import sys @@ -41,7 +46,65 @@ freestanding_concat_imports = {'nyrt_str_length', 'nyrt_mem_copy', 'nyrt_alloc'} -def undefined_symbols(path: Path) -> set[str]: +def object_profile(path: Path) -> tuple[str, str]: + """Validate a single supported relocatable object, never an archive/image.""" + data = path.read_bytes() + if data.startswith(b'\x7fELF'): + if len(data) < 64 or data[4:7] != b'\x02\x01\x01': + raise AssertionError(f'expected ELF64 little-endian version 1: {path}') + kind, machine = struct.unpack_from(' command_bytes // 8 or command_bytes > len(data) - 32): + raise AssertionError(f'expected x86_64/arm64 Mach-O MH_OBJECT: {path}') + return 'Mach-O', 'x86_64' if cpu == 0x1000007 else 'arm64' + if len(data) >= 20: + machine, sections, _timestamp, _symbols, _symbol_count, optional, flags = struct.unpack_from(' set[str]: + names = set() + for name in output.splitlines(): + # The fixture's ABI names are ASCII identifiers; prefixes such as an + # archive/member heading, an address/type column, or demangled spaces + # must never silently become a symbol. Preserve all language underscores. + if not re.fullmatch(r'[A-Za-z_.$?@][A-Za-z_0-9.$?@]*', name): + raise AssertionError(f'unexpected llvm-nm just-symbols output: {name!r}') + if profile[0] == 'Mach-O': + if not name.startswith('_'): + raise AssertionError(f'Mach-O external symbol lacks Darwin ABI underscore: {name!r}') + name = name[1:] + if not name or name in names: + raise AssertionError(f'empty or duplicate external symbol: {name!r}') + names.add(name) + return names + + +def undefined_symbols(path: Path, llvm_nm: Path | None = None) -> tuple[set[str], set[str]]: + profile = object_profile(path) + if profile[0] != 'ELF': + if llvm_nm is None or not llvm_nm.is_file(): + raise AssertionError(f'{profile[0]} symbol checks require an explicit existing --llvm-nm executable') + sets = [] + for selection in ('--undefined-only', '--defined-only'): + command = [str(llvm_nm), '--format=just-symbols', '--extern-only', + '--no-demangle', selection, str(path)] + result = subprocess.run(command, stdin=subprocess.DEVNULL, capture_output=True, + text=True, timeout=30) + if result.returncode or result.stderr: + raise AssertionError((command, result.returncode, result.stdout, result.stderr)) + sets.append(nm_names(result.stdout, profile)) + return sets[0], sets[1] data = path.read_bytes() if data[:4] != b'\x7fELF': raise SystemExit(f'not an ELF object: {path}') @@ -117,6 +180,8 @@ def absolute_relocation_count(obj: Path) -> int: misbehaving. Read from the section headers directly, so the check needs no binutils. """ + if object_profile(obj) != ('ELF', 'x86_64'): + raise AssertionError('32-bit absolute relocation check requires ELF x86_64') data = obj.read_bytes() if data[:4] != b'\x7fELF' or data[4] != 2 or data[5] != 1: raise SystemExit(f'expected a little-endian 64-bit object: {obj}') @@ -143,7 +208,7 @@ def absolute_relocation_count(obj: Path) -> int: def emit(compiler: Path, assembler: Path, root: Path, name: str, source: str, freestanding: bool = False, no_red_zone: bool = False, code_model: str | None = None, - ir: Path | None = None): + ir: Path | None = None, llvm_nm: Path | None = None): path = root / f'{name}.ny' path.write_text(source, newline='\n') nil = root / f'{name}.nil' @@ -162,23 +227,24 @@ def emit(compiler: Path, assembler: Path, root: Path, name: str, source: str, result = subprocess.run(list(map(str, command)), stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=120) assert result.returncode == 0, (command, result.returncode, result.stdout, result.stderr) - return undefined_symbols(obj) + return undefined_symbols(obj, llvm_nm) def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--compiler', type=Path, required=True) parser.add_argument('--assembler', type=Path, required=True) + parser.add_argument('--llvm-nm', type=Path, + help='Explicit LLVM symbol reader, required for COFF and Mach-O objects') args = parser.parse_args() - if sys.platform != 'linux': - print('SKIP: the native symbol surface is pinned on ELF hosts') - return 77 checks = 0 with tempfile.TemporaryDirectory(prefix='nil-symbol-surface-') as directory: root = Path(directory) + emit_case = partial(emit, args.compiler, args.assembler, root, llvm_nm=args.llvm_nm) trivial = 'fn main() -> i64 {\n let total: i64 = 0;\n return total;\n}\n' - plain, plain_exports = emit(args.compiler, args.assembler, root, 'plain', trivial) + plain, plain_exports = emit_case('plain', trivial) + profile = object_profile(root / 'plain.o') assert plain <= runtime_abi, ( 'an object that uses no hosted facility imports only the native runtime ABI; ' f'unexpected: {sorted(plain - runtime_abi)}') @@ -194,7 +260,7 @@ def main() -> int: ' let b: text = "right";\n' ' let joined: text = a + b;\n' ' return 0;\n}\n') - concat, _ = emit(args.compiler, args.assembler, root, 'concat', + concat, _ = emit_case('concat', concatenating) assert concat_imports <= concat, ( 'a concatenating program must still import the helper dependencies; ' @@ -211,8 +277,7 @@ def main() -> int: # dependencies become runtime hooks, and no libc name may survive -- # a freestanding consumer satisfying calloc at all is the reverse- # engineered-shim problem this exists to remove. - free_concat, _ = emit(args.compiler, args.assembler, root, - 'free_concat', concatenating, freestanding=True) + free_concat, _ = emit_case('free_concat', concatenating, freestanding=True) assert freestanding_concat_imports <= free_concat, ( 'a freestanding concatenating program must reach memory through the ' 'runtime hooks; missing: ' @@ -235,7 +300,7 @@ def main() -> int: assert 'main' in plain_exports and '__ny_entry' not in plain_exports, ( f'a hosted object is entered through main; exports: {sorted(plain_exports)}') checks += 1 - free, free_exports = emit(args.compiler, args.assembler, root, 'free', + free, free_exports = emit_case('free', trivial, freestanding=True) assert '__ny_entry' in free_exports and 'main' not in free_exports, ( 'a freestanding object is entered through __ny_entry and defines no C main; ' @@ -256,14 +321,14 @@ def main() -> int: '@interop("host")\n' f'fn {host_symbol}(message: text) -> i64\n' 'end fn\n\n') - unused, _ = emit(args.compiler, args.assembler, root, 'host_unused', + unused, _ = emit_case('host_unused', declaration + 'fn main() -> i64 {\n return 0;\n}\n', freestanding=True) assert host_symbol not in unused, ( 'a host symbol that is never called must not be imported; ' f'imports: {sorted(unused)}') checks += 1 - called, _ = emit(args.compiler, args.assembler, root, 'host_called', + called, _ = emit_case('host_called', declaration + 'fn main() -> i64 {\n' ' let greeting: text = "from ny";\n' f' return {host_symbol}(greeting);\n' @@ -288,31 +353,31 @@ def main() -> int: # string constants of a program that never asked to be low. The large # model removes them, and the first assertion keeps the second from # passing because the program had no addresses to materialize. - emit(args.compiler, args.assembler, root, 'small', concatenating, - freestanding=True) - small_absolutes = absolute_relocation_count(root / 'small.o') - assert small_absolutes > 0, ( - 'the default code model is expected to use 32-bit absolute ' - 'relocations; this check would otherwise prove nothing') - checks += 1 - emit(args.compiler, args.assembler, root, 'large', concatenating, - freestanding=True, code_model='large') - large_absolutes = absolute_relocation_count(root / 'large.o') - assert large_absolutes == 0, ( - 'an object emitted for the large code model must have no 32-bit ' - f'absolute relocations; found {large_absolutes}') - checks += 1 + relocation_checks = 0 + if profile == ('ELF', 'x86_64'): + emit_case('small', concatenating, freestanding=True) + small_absolutes = absolute_relocation_count(root / 'small.o') + assert small_absolutes > 0, ( + 'the default code model is expected to use 32-bit absolute ' + 'relocations; this check would otherwise prove nothing') + relocation_checks += 1 + emit_case('large', concatenating, freestanding=True, code_model='large') + large_absolutes = absolute_relocation_count(root / 'large.o') + assert large_absolutes == 0, ( + 'an object emitted for the large code model must have no 32-bit ' + f'absolute relocations; found {large_absolutes}') + relocation_checks += 1 # The red zone is a separate decision from the entry shape: a target # whose interrupt entry switches stacks keeps it, a kernel-reentrant # one cannot. So it is its own option and is checked on its own. hosted_ir = root / 'hosted.ll' - emit(args.compiler, args.assembler, root, 'hosted_ir', trivial, ir=hosted_ir) + emit_case('hosted_ir', trivial, ir=hosted_ir) assert definitions_without_noredzone(hosted_ir), ( 'the red zone stays available unless it is turned off') checks += 1 guarded_ir = root / 'guarded.ll' - emit(args.compiler, args.assembler, root, 'guarded_ir', trivial, + emit_case('guarded_ir', trivial, freestanding=True, no_red_zone=True, ir=guarded_ir) missing = definitions_without_noredzone(guarded_ir) assert not missing, ( @@ -320,7 +385,19 @@ def main() -> int: f'without it: {missing}') checks += 1 - print(f'Native symbol surface: {checks} checks passed') + expected_format = {'linux': 'ELF', 'win32': 'COFF', 'darwin': 'Mach-O'}.get(sys.platform) + expected_architecture = {'x86_64': 'x86_64', 'amd64': 'x86_64', + 'arm64': 'arm64', 'aarch64': 'arm64'}.get(platform.machine().lower()) + assert profile == (expected_format, expected_architecture), ( + f'default object profile {profile} differs from host {(expected_format, expected_architecture)}') + assert all(object_profile(path) == profile for path in root.glob('*.o')), ( + 'object profile changed between native surface fixtures') + + print(f'Native symbol surface ({profile[0]} {profile[1]}): {checks} generic ABI/IR checks passed') + if relocation_checks: + print(f'ELF x86_64 small/large-model relocation checks: {relocation_checks} passed') + else: + print(f'Relocation coverage is limited to ELF x86_64; {profile[0]} {profile[1]} is outside that profile') return 0 diff --git a/tests/tools/net_socket_contract.py b/tests/tools/net_socket_contract.py new file mode 100644 index 00000000..3c03d985 --- /dev/null +++ b/tests/tools/net_socket_contract.py @@ -0,0 +1,1099 @@ +#!/usr/bin/env python3 +"""The network intrinsics, against a real peer. + +A socket cannot be checked by comparing stdout: the interesting behaviour is +what the other end of the connection sees. So each case here runs a Matrix +page as a process and plays the other side from Python. + +What is being pinned down is the contract the standard library promises, not +the implementation: + + * a program can listen, accept, read and write, and what arrives on the wire + is byte-for-byte what the page sent; + * a handle is opaque -- a number the program was never given is refused with + a sentence, not used as a file descriptor; + * a listener and a connection are not interchangeable; + * the runtime closes what a program leaves open, so a port does not stay + bound against the next run; + * a read deadline expires instead of hanging the process forever; and + * a peer that vanishes mid-write is an error the program can see, rather + than a signal that kills it. + +That last one is the reason this file exists rather than a simpler fixture. A +server that dies when a browser closes a tab passes every test that does not +close a connection early. +""" + +from __future__ import annotations + +import argparse +import os +import queue +import struct +import threading +import socket +import subprocess +import sys +import tempfile +import time +from pathlib import Path + + +def free_port() -> int: + """A port the kernel says is free, released before the page binds it. + + There is a race here in principle. In practice the kernel does not hand + the same ephemeral port out twice in the time between these two lines, and + the alternative -- a fixed port -- fails whenever anything else on the + machine happens to be using it, which is worse because it looks like a + code failure. + """ + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as probe: + probe.bind(("127.0.0.1", 0)) + return int(probe.getsockname()[1]) + + +def build(compiler: Path, stdlib: Path, source: str, workdir: Path, name: str) -> Path: + page = workdir / f"{name}.matrix" + page.write_text(source) + artifact = workdir / f"{name}.nil" + built = subprocess.run( + [str(compiler), "--no-bundle", "-I", str(stdlib), + "--output", str(artifact), str(page)], + capture_output=True, text=True, timeout=60) + if built.returncode != 0: + raise AssertionError( + f"{name} did not compile:\n{built.stdout}{built.stderr}") + return artifact + + +class PageOutput: + """Bounded background drain: readiness deadlines cannot block in readline.""" + def __init__(self, process, stream): + self.process, self.stream = process, stream + self.events = queue.Queue() + self.buffer = b"" + self.ended = False + self.reader = threading.Thread(target=self._drain, name="socket-test-output") + self.reader.start() + + def _drain(self): + total = 0 + try: + while True: + chunk = os.read(self.stream.fileno(), 4096) + if not chunk: + break + total += len(chunk) + if total > 1024 * 1024: + self.events.put(AssertionError("page output exceeded 1 MiB")) + self.process.kill() + break + self.events.put(chunk) + except Exception as error: + self.events.put(error) + finally: + self.events.put(None) + + def _take(self, deadline): + remaining = deadline - time.monotonic() + if remaining <= 0: + raise AssertionError("page output deadline expired") + try: + value = self.events.get(timeout=remaining) + except queue.Empty as error: + raise AssertionError("page output deadline expired") from error + if isinstance(value, Exception): + raise value + if value is None: + self.ended = True + else: + self.buffer += value + + def readline(self, timeout=20.0): + deadline = time.monotonic() + timeout + while b"\n" not in self.buffer and not self.ended: + self._take(deadline) + if b"\n" in self.buffer: + line, self.buffer = self.buffer.split(b"\n", 1) + return (line + b"\n").decode("utf-8", errors="replace") + line, self.buffer = self.buffer, b"" + return line.decode("utf-8", errors="replace") + + def read(self, timeout=20.0): + deadline = time.monotonic() + timeout + while not self.ended: + self._take(deadline) + value, self.buffer = self.buffer, b"" + return value.decode("utf-8", errors="replace") + + def close(self): + self.reader.join(timeout=5) + if self.reader.is_alive(): + raise AssertionError("page output reader did not finish after owned process cleanup") + self.stream.close() + + +def cleanup_page(page): + if page.poll() is None: + page.kill() + page.wait(timeout=10) + page.stdout.close() + + +def wait_for_line(process: subprocess.Popen, marker: str, timeout: float = 20.0) -> None: + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + line = process.stdout.readline(timeout=max(0.001, deadline - time.monotonic())) + if line == "": + raise AssertionError(f"the page exited before printing {marker!r}") + if line.strip() == marker: + return + raise AssertionError(f"the page never printed {marker!r}") + + +ECHO_PAGE = """ +book net.contract.echo + +read nil.std.core +read nil.std.text.string_utils +read nil.std.net.socket + +main returns number begin paragraph + value opened is nil.std.net::listen with host being "127.0.0.1", port being PORT, backlog being 8 + consider opened + when Result.Error holding why then begin block + say "listen failed: " plus why + give 1 + end block + when Result.Ok holding listener then begin block + say "ready" + value taken is nil.std.net::accept with handle being listener + consider taken + when Result.Ok holding client then begin block + value heard is nil.std.net::read with handle being client, maximum_bytes being 4096 + consider heard + when Result.Ok holding incoming then begin block + evaluate nil.std.net::write with handle being client, data being incoming + say "echoed " plus ((nil.std.text::length with s being incoming) becomes text) + end block + when Result.Error holding why then say "read failed: " plus why + otherwise say "read gave neither" + end consider + evaluate nil.std.net::close with handle being client + end block + when Result.Error holding why then say "accept failed: " plus why + otherwise say "accept gave neither" + end consider + evaluate nil.std.net::close with handle being listener + say "done" + give 0 + end block + otherwise give 1 + end consider +end paragraph +""" + + +HANDLE_PAGE = """ +book net.contract.handles + +read nil.std.core +read nil.std.text.string_utils +read nil.std.net.socket + +!!! Reports a Result as its error text, or "ok" when it succeeded, so the +!!! refusals can be asserted without depending on a system message. Two of +!!! them, because read gives back text and the rest give back a count, and +!!! Matrix has no overload sets. +reason-for receives outcome as Result of number, text returns text begin paragraph + consider outcome + when Result.Ok holding amount then give "ok" + when Result.Error holding why then give why + otherwise give "neither" + end consider +end paragraph + +reason-for-read receives outcome as Result of text, text returns text begin paragraph + consider outcome + when Result.Ok holding incoming then give "ok" + when Result.Error holding why then give why + otherwise give "neither" + end consider +end paragraph + +main returns number begin paragraph + !!! A handle the program was never given. If these were descriptors, 1 + !!! would be this process's standard output. + say reason-for-read with outcome being (nil.std.net::read with handle being 1, maximum_bytes being 10) + say reason-for with outcome being (nil.std.net::write with handle being 1, data being "x") + say (nil.std.net::is_open with handle being 1) becomes text + + !!! Closing something never opened is not a failure: it is what a cleanup + !!! path does when an earlier one already ran. + say reason-for with outcome being (nil.std.net::close with handle being 404) + + value opened is nil.std.net::listen with host being "127.0.0.1", port being PORT, backlog being 4 + consider opened + when Result.Ok holding listener then begin block + say (nil.std.net::is_open with handle being listener) becomes text + !!! A listener is not a connection and will not pretend to be one. + say reason-for-read with outcome being (nil.std.net::read with handle being listener, maximum_bytes being 4) + say reason-for with outcome being (nil.std.net::write with handle being listener, data being "x") + evaluate nil.std.net::close with handle being listener + say (nil.std.net::is_open with handle being listener) becomes text + !!! And it stays closed. + say reason-for-read with outcome being (nil.std.net::read with handle being listener, maximum_bytes being 4) + end block + when Result.Error holding why then say "listen failed: " plus why + otherwise say "listen gave neither" + end consider + + !!! A bind address is numeric, empty or localhost; a name is refused + !!! rather than resolved, because binding must not do DNS. + say reason-for with outcome being + (nil.std.net::listen with host being "example.com", port being PORT, backlog being 4) + say reason-for with outcome being + (nil.std.net::listen with host being "127.0.0.1", port being 0, backlog being 4) + say reason-for with outcome being + (nil.std.net::listen with host being "127.0.0.1", port being 70000, backlog being 4) + give 0 +end paragraph +""" + + +LEAK_PAGE = """ +book net.contract.leak + +read nil.std.core +read nil.std.net.socket + +!!! Binds and then exits without closing anything. The runtime has to clean +!!! up after it, or the next run of this same page cannot bind the port. +main returns number begin paragraph + value opened is nil.std.net::listen with host being "127.0.0.1", port being PORT, backlog being 4 + consider opened + when Result.Ok holding listener then say "bound" + when Result.Error holding why then say "listen failed: " plus why + otherwise say "neither" + end consider + give 0 +end paragraph +""" + + +TIMEOUT_PAGE = """ +book net.contract.timeout + +read nil.std.core +read nil.std.net.socket + +!!! A connection that opens and says nothing must not hold the process. With +!!! no deadline this page would wait as long as the client cared to. +main returns number begin paragraph + value opened is nil.std.net::listen with host being "127.0.0.1", port being PORT, backlog being 4 + consider opened + when Result.Ok holding listener then begin block + say "ready" + value taken is nil.std.net::accept with handle being listener + consider taken + when Result.Ok holding client then begin block + evaluate nil.std.net::set_timeout with handle being client, milliseconds being 300 + value heard is nil.std.net::read with handle being client, maximum_bytes being 16 + consider heard + when Result.Ok holding incoming then say "read returned" + when Result.Error holding why then say "timed out" + otherwise say "neither" + end consider + evaluate nil.std.net::close with handle being client + end block + otherwise say "accept failed" + end consider + evaluate nil.std.net::close with handle being listener + say "done" + give 0 + end block + otherwise give 1 + end consider +end paragraph +""" + + +HANGUP_PAGE = """ +book net.contract.hangup + +read nil.std.core +read nil.std.net.socket + +!!! Writes a great deal to a peer that has already gone. Without the runtime +!!! suppressing SIGPIPE this page does not reach its last line: the default +!!! disposition ends the process, and a server built on it dies whenever a +!!! browser closes a tab. +main returns number begin paragraph + value opened is nil.std.net::listen with host being "127.0.0.1", port being PORT, backlog being 4 + consider opened + when Result.Ok holding listener then begin block + say "ready" + value taken is nil.std.net::accept with handle being listener + consider taken + when Result.Ok holding client then begin block + variable payload as text is "0123456789" + variable doubling as number is 0 + repeat while doubling is less than 16 + set payload to payload plus payload + set doubling to doubling plus 1 + end repeat + variable attempt-count as number is 0 + variable reported as boolean is false + repeat while attempt-count is less than 20 and not reported + value sent is nil.std.net::write with handle being client, data being payload + consider sent + when Result.Error holding why then set reported to true + otherwise set attempt-count to attempt-count plus 1 + end consider + end repeat + say "write reported: " plus (reported becomes text) + evaluate nil.std.net::close with handle being client + end block + otherwise say "accept failed" + end consider + evaluate nil.std.net::close with handle being listener + say "survived" + give 0 + end block + otherwise give 1 + end consider +end paragraph +""" + + +CLIENT_PAGE = """ +book net.contract.client + +read nil.std.core +read nil.std.text.string_utils +read nil.std.net.socket + +!!! The other direction: connecting out, and resolving a name while doing it. +main returns number begin paragraph + value opened is nil.std.net::connect with host being "localhost", port being PORT + consider opened + when Result.Error holding why then begin block + say "connect failed: " plus why + give 1 + end block + when Result.Ok holding server then begin block + evaluate nil.std.net::set_no_delay with handle being server, enabled being true + value peer is nil.std.net::peer_address with handle being server + consider peer + when Result.Ok holding address then say "peer seen: " + plus ((nil.std.text::contains with s being address, needle being ":") becomes text) + otherwise say "peer unavailable" + end consider + evaluate nil.std.net::write with handle being server, data being "ping" + value heard is nil.std.net::read with handle being server, maximum_bytes being 64 + consider heard + when Result.Ok holding incoming then say "heard " plus incoming + when Result.Error holding why then say "read failed: " plus why + otherwise say "neither" + end consider + !!! The orderly close of a peer is empty text, not an error, which + !!! is how a read loop knows the request is finished. + value after-close is nil.std.net::read with handle being server, maximum_bytes being 64 + consider after-close + when Result.Ok holding incoming then say "closed reads empty: " + plus ((incoming equals "") becomes text) + when Result.Error holding why then say "read after close failed: " plus why + otherwise say "neither" + end consider + evaluate nil.std.net::close with handle being server + say "done" + give 0 + end block + otherwise give 1 + end consider +end paragraph +""" + + + +POLL_PAGE = """ +book net.contract.poll + +read nil.std.core +read nil.std.net.socket + +reason-for receives outcome as Result of number, text returns text begin paragraph + consider outcome + when Result.Ok holding amount then give "ok" + when Result.Error holding why then give why + otherwise give "neither" + end consider +end paragraph + +amount-of receives outcome as Result of number, text returns number begin paragraph + consider outcome + when Result.Ok holding amount then give amount + otherwise give 0 minus 1 + end consider +end paragraph + +main returns number begin paragraph + value opened is nil.std.net::listen with host being "127.0.0.1", port being PORT, backlog being 8 + consider opened + when Result.Ok holding listener then begin block + !!! An empty watch set does not wait: a loop that watched nothing + !!! by mistake would otherwise be an invisible stall. + say amount-of with outcome being (nil.std.net::wait with timeout_milliseconds being 10000) + say nil.std.net::watch_count with nothing + + say reason-for with outcome being (nil.std.net::set_blocking with handle being listener, blocking being false) + evaluate nil.std.net::watch with handle being listener, flags being nil.std.net::ready_readable + say nil.std.net::watch_count with nothing + !!! Watching again replaces the flags rather than adding a second entry. + evaluate nil.std.net::watch with handle being listener, flags being nil.std.net::ready_readable + say nil.std.net::watch_count with nothing + + !!! Nothing is connecting, so nothing is ready, and the drained + !!! ready set reads as zero. + say amount-of with outcome being (nil.std.net::wait with timeout_milliseconds being 50) + say amount-of with outcome being (nil.std.net::next_ready with nothing) + + !!! A non-blocking accept with nothing pending is zero, not an error. + say amount-of with outcome being (nil.std.net::accept with handle being listener) + + !!! Unwatching something not watched succeeds. + say reason-for with outcome being (nil.std.net::unwatch with handle being 991) + say reason-for with outcome being (nil.std.net::unwatch with handle being listener) + say nil.std.net::watch_count with nothing + + !!! A closed socket is dropped from the watch set by the next wait + !!! rather than polled on a descriptor that may have been reissued. + evaluate nil.std.net::watch with handle being listener, flags being nil.std.net::ready_readable + evaluate nil.std.net::close with handle being listener + say amount-of with outcome being (nil.std.net::wait with timeout_milliseconds being 10) + say nil.std.net::watch_count with nothing + + !!! Packing: handle and flags travel as one number. + say nil.std.net::ready_handle with packed being 41 + say nil.std.net::ready_flags with packed being 41 + say (nil.std.net::is_readable with packed being 41) becomes text + say (nil.std.net::is_finished with packed being 41) becomes text + say (nil.std.net::is_writable with packed being 41) becomes text + give 0 + end block + when Result.Error holding why then begin block + say "listen failed: " plus why + give 1 + end block + otherwise give 1 + end consider +end paragraph +""" + + +EVENT_LOOP_PAGE = """ +book net.contract.event-loop + +read nil.std.core +read nil.std.text.string_utils +read nil.std.net.socket + +pending begin record + field handle as number + field gathered as text + field live as boolean +end record + +find-slot receives slots as array of pending, handle as number returns number begin paragraph + variable index as number is 0 + repeat while index is less than the length of slots + value one is slots at index + when one.live and one.handle equals handle then + give index + end when + set index to index plus 1 + end repeat + give 0 minus 1 +end paragraph + +main returns number begin paragraph + value opened is nil.std.net::listen with host being "127.0.0.1", port being PORT, backlog being 128 + consider opened + when Result.Error holding why then begin block + say "listen failed: " plus why + give 1 + end block + when Result.Ok holding listener then begin block + evaluate nil.std.net::set_blocking with handle being listener, blocking being false + evaluate nil.std.net::watch with handle being listener, flags being nil.std.net::ready_readable + say "ready" + + variable slots as array of pending is [] + variable answered as number is 0 + variable live-now as number is 0 + variable most-at-once as number is 0 + variable partial-count as number is 0 + + repeat while answered is less than EXPECTED + evaluate nil.std.net::wait with timeout_milliseconds being 10000 + variable draining as boolean is true + repeat while draining + value packed is nil.std.net::next_ready with nothing + consider packed + when Result.Ok holding value-held then begin block + when value-held equals 0 then + set draining to false + end when + value handle is nil.std.net::ready_handle with packed being value-held + when value-held is greater than 0 and handle equals listener then + variable accepting as boolean is true + repeat while accepting + value taken is nil.std.net::accept with handle being listener + consider taken + when Result.Ok holding client then begin block + when client equals 0 then + set accepting to false + end when + when client is greater than 0 then + evaluate nil.std.net::set_timeout with handle being client, milliseconds being 10000 + evaluate nil.std.net::watch with handle being client, flags being nil.std.net::ready_readable + append build pending with handle being client, gathered being "", live being true to slots + set live-now to live-now plus 1 + when live-now is greater than most-at-once then + set most-at-once to live-now + end when + end when + end block + otherwise set accepting to false + end consider + end repeat + end when + when value-held is greater than 0 and not (handle equals listener) then + value slot is find-slot with slots being slots, handle being handle + when slot is at least 0 then + value heard is nil.std.net::read with handle being handle, maximum_bytes being 4096 + consider heard + when Result.Ok holding piece then begin block + value held is slots at slot + value whole is held.gathered plus piece + value done is nil.std.text::contains with s being whole, needle being "END" + value gone is piece equals "" + when not done and not gone then + when held.gathered equals "" then + set partial-count to partial-count plus 1 + when partial-count equals EXPECTED then + say "all-partial" + end when + end when + set slots at slot to build pending with handle being handle, + gathered being whole, live being true + end when + when done then + evaluate nil.std.net::write with handle being handle, + data being "ok:" plus ((nil.std.text::length with s being whole) becomes text) + end when + when done or gone then + evaluate nil.std.net::unwatch with handle being handle + evaluate nil.std.net::close with handle being handle + set slots at slot to build pending with handle being 0, gathered being "", live being false + set live-now to live-now minus 1 + set answered to answered plus 1 + end when + end block + otherwise begin block + evaluate nil.std.net::unwatch with handle being handle + evaluate nil.std.net::close with handle being handle + set slots at slot to build pending with handle being 0, gathered being "", live being false + set live-now to live-now minus 1 + set answered to answered plus 1 + end block + end consider + end when + end when + end block + otherwise set draining to false + end consider + end repeat + end repeat + + evaluate nil.std.net::close with handle being listener + say "answered " plus (answered becomes text) + say "most-at-once " plus (most-at-once becomes text) + give 0 + end block + otherwise give 1 + end consider +end paragraph +""" + +NONBLOCK_PAGE = """ +book net.contract.nonblocking +read nil.std.core +read nil.std.text.string_utils +read nil.std.net.socket + +amount receives result as Result of number, text returns number begin paragraph + consider result + when Result.Ok holding amount-held then give amount-held + when Result.Error holding why then begin block + say "error " plus why + give 0 minus 2 + end block + otherwise give 0 minus 3 + end consider +end paragraph + +main returns number begin paragraph + value listener is amount with result being (nil.std.net::listen with host being "127.0.0.1", port being PORT, backlog being 8) + say "ready" + value client is amount with result being (nil.std.net::accept with handle being listener) + evaluate nil.std.net::set_blocking with handle being client, blocking being false + evaluate nil.std.net::watch with handle being client, flags being nil.std.net::ready_readable + value idle is amount with result being (nil.std.net::receive with handle being client, maximum_bytes being 64) + say "idle " plus (idle becomes text) + evaluate nil.std.net::wait with timeout_milliseconds being 10000 + value taken is amount with result being (nil.std.net::receive with handle being client, maximum_bytes being 64) + say "received " plus (taken becomes text) + consider nil.std.net::received with nothing + when Result.Ok holding incoming then begin block + value sent is amount with result being (nil.std.net::send_some with handle being client, + data being "xx" plus incoming, offset being 2) + say "sent " plus (sent becomes text) + end block + otherwise give 1 + end consider + evaluate nil.std.net::wait with timeout_milliseconds being 10000 + value closed is amount with result being (nil.std.net::receive with handle being client, maximum_bytes being 64) + say "closed " plus (closed becomes text) + consider nil.std.net::received with nothing + when Result.Ok holding incoming then say "cleared " plus ((nil.std.text::length with s being incoming) becomes text) + otherwise give 1 + end consider + evaluate nil.std.net::close with handle being client + value stale is amount with result being (nil.std.net::next_ready with nothing) + say "stale-ready " plus (stale becomes text) + evaluate nil.std.net::close with handle being listener + say "done" + give 0 +end paragraph +""" + +SHARED_PAGE = """ +book net.contract.shared +read nil.std.core +read nil.std.net.socket + +main returns number begin paragraph + consider nil.std.net::listen_reuse_port with host being "127.0.0.1", port being PORT, backlog being 8 + when Result.Error holding why then say "unsupported " plus why + when Result.Ok holding first then begin block + consider nil.std.net::listen_reuse_port with host being "127.0.0.1", port being PORT, backlog being 8 + when Result.Ok holding second then begin block + say "both shared" + evaluate nil.std.net::close with handle being second + end block + when Result.Error holding why then say "second failed " plus why + otherwise give 1 + end consider + consider nil.std.net::listen with host being "127.0.0.1", port being PORT, backlog being 8 + when Result.Error holding why then say "ordinary refused" + when Result.Ok holding accidental then begin block + say "ordinary admitted" + evaluate nil.std.net::close with handle being accidental + end block + otherwise give 1 + end consider + evaluate nil.std.net::close with handle being first + end block + otherwise give 1 + end consider + give 0 +end paragraph +""" + + +def run_page(interpreter: Path, artifact: Path) -> subprocess.Popen: + process = subprocess.Popen( + [str(interpreter), str(artifact)], + stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + bufsize=0) + process.stdout = PageOutput(process, process.stdout) + return process + + +def case_echo(compiler, interpreter, stdlib, workdir) -> list[str]: + """What the page writes is what the wire carries, bytes included.""" + problems: list[str] = [] + port = free_port() + artifact = build(compiler, stdlib, + ECHO_PAGE.replace("PORT", str(port)), workdir, "echo") + page = run_page(interpreter, artifact) + try: + wait_for_line(page, "ready") + # Deliberately not valid UTF-8 and containing a zero byte: a socket + # carries bytes, and text that has been through an encoder somewhere + # would come back changed. + payload = b"ping\x00\xc3\xa9\xff\xfe end" + with socket.create_connection(("127.0.0.1", port), timeout=10) as client: + client.sendall(payload) + client.shutdown(socket.SHUT_WR) + received = b"" + while len(received) < len(payload): + chunk = client.recv(4096) + if not chunk: + break + received += chunk + if received != payload: + problems.append( + f"echo: the wire carried {received!r}, not {payload!r}") + output = page.stdout.read() + if f"echoed {len(payload)}" not in output: + problems.append( + f"echo: the page did not report {len(payload)} bytes read; it said {output!r}") + if "done" not in output: + problems.append(f"echo: the page did not finish; it said {output!r}") + finally: + cleanup_page(page) + return problems + + +def case_handles(compiler, interpreter, stdlib, workdir) -> list[str]: + """A handle is a token into the runtime, not a descriptor.""" + port = free_port() + artifact = build(compiler, stdlib, + HANDLE_PAGE.replace("PORT", str(port)), workdir, "handles") + finished = subprocess.run([str(interpreter), str(artifact)], + capture_output=True, text=True, timeout=60) + lines = finished.stdout.splitlines() + expected = [ + ("read on an unknown handle", lambda v: "not open" in v), + ("write on an unknown handle", lambda v: "not open" in v), + ("is_open on an unknown handle", lambda v: v == "0"), + ("close on an unknown handle", lambda v: v == "ok"), + ("is_open on a listener", lambda v: v == "1"), + ("read on a listener", lambda v: "listener" in v), + ("write on a listener", lambda v: "listener" in v), + ("is_open after close", lambda v: v == "0"), + ("read after close", lambda v: "not open" in v), + ("listen on a name", lambda v: "numeric" in v), + ("listen on port zero", lambda v: "between 1 and 65535" in v), + ("listen above the port range", lambda v: "between 1 and 65535" in v), + ] + problems: list[str] = [] + if len(lines) != len(expected): + return [f"handles: expected {len(expected)} lines, got {len(lines)}: " + f"{finished.stdout!r}{finished.stderr!r}"] + for (label, holds), value in zip(expected, lines): + if not holds(value): + problems.append(f"handles: {label} gave {value!r}") + return problems + + +def case_port_released(compiler, interpreter, stdlib, workdir) -> list[str]: + """A page that leaves a listener open must not keep the port.""" + port = free_port() + artifact = build(compiler, stdlib, + LEAK_PAGE.replace("PORT", str(port)), workdir, "leak") + problems: list[str] = [] + for attempt in (1, 2, 3): + finished = subprocess.run([str(interpreter), str(artifact)], + capture_output=True, text=True, timeout=60) + if "bound" not in finished.stdout: + problems.append( + f"port-released: run {attempt} could not bind; it said " + f"{finished.stdout!r}{finished.stderr!r}") + break + return problems + + +def case_timeout(compiler, interpreter, stdlib, workdir) -> list[str]: + """A read deadline expires rather than holding the process.""" + problems: list[str] = [] + port = free_port() + artifact = build(compiler, stdlib, + TIMEOUT_PAGE.replace("PORT", str(port)), workdir, "timeout") + page = run_page(interpreter, artifact) + try: + wait_for_line(page, "ready") + with socket.create_connection(("127.0.0.1", port), timeout=10): + # Connect and say nothing at all. The page's own deadline is what + # has to end this, so hold the connection open across it. + time.sleep(1.2) + page.wait(timeout=20) + output = page.stdout.read() + if "timed out" not in output: + problems.append( + f"timeout: the read did not expire; the page said {output!r}") + except subprocess.TimeoutExpired: + problems.append("timeout: the page never finished, so the deadline did nothing") + finally: + cleanup_page(page) + return problems + + +def case_peer_hangup(compiler, interpreter, stdlib, workdir) -> list[str]: + """A peer that vanishes mid-write is reported, not fatal.""" + problems: list[str] = [] + port = free_port() + artifact = build(compiler, stdlib, + HANGUP_PAGE.replace("PORT", str(port)), workdir, "hangup") + page = run_page(interpreter, artifact) + try: + wait_for_line(page, "ready") + client = socket.create_connection(("127.0.0.1", port), timeout=10) + # Reset rather than a graceful close, so the next write fails outright + # instead of being accepted into a buffer nobody will drain. + client.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, + struct.pack("HH" if os.name == "nt" else "ii", 1, 0)) + client.close() + page.wait(timeout=30) + output = page.stdout.read() + if "survived" not in output: + problems.append( + "peer-hangup: the page did not reach its last line, so a lost " + f"peer ended the process; it said {output!r}") + if page.returncode != 0: + problems.append( + f"peer-hangup: the page exited {page.returncode}, which is how " + "an unsuppressed SIGPIPE looks") + except subprocess.TimeoutExpired: + problems.append("peer-hangup: the page never finished") + finally: + cleanup_page(page) + return problems + + +def case_client(compiler, interpreter, stdlib, workdir) -> list[str]: + """Connecting out, resolving a name, and reading a closed peer.""" + problems: list[str] = [] + listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + listener.bind(("127.0.0.1", 0)) + listener.listen(4) + port = int(listener.getsockname()[1]) + artifact = build(compiler, stdlib, + CLIENT_PAGE.replace("PORT", str(port)), workdir, "client") + page = run_page(interpreter, artifact) + try: + listener.settimeout(20) + connection, _ = listener.accept() + with connection: + greeting = connection.recv(64) + if greeting != b"ping": + problems.append(f"client: the page sent {greeting!r}, not b'ping'") + connection.sendall(b"pong") + page.wait(timeout=30) + output = page.stdout.read() + for marker in ("peer seen: 1", "heard pong", "closed reads empty: 1", "done"): + if marker not in output: + problems.append(f"client: expected {marker!r} in {output!r}") + except (socket.timeout, subprocess.TimeoutExpired): + problems.append("client: the page did not complete its exchange") + finally: + listener.close() + cleanup_page(page) + return problems + + + +def case_poll(compiler, interpreter, stdlib, workdir) -> list[str]: + """The watch set, the ready set, and the packing between them.""" + port = free_port() + artifact = build(compiler, stdlib, + POLL_PAGE.replace("PORT", str(port)), workdir, "poll") + finished = subprocess.run([str(interpreter), str(artifact)], + capture_output=True, text=True, timeout=120) + lines = finished.stdout.splitlines() + expected = [ + ("an empty watch set does not wait", lambda v: v == "0"), + ("nothing is watched yet", lambda v: v == "0"), + ("a listener can be made non-blocking", lambda v: v == "ok"), + ("watching counts one", lambda v: v == "1"), + ("watching again replaces rather than adds", lambda v: v == "1"), + ("nothing connecting means nothing ready", lambda v: v == "0"), + ("a drained ready set reads zero", lambda v: v == "0"), + ("a non-blocking accept with nothing pending is zero", lambda v: v == "0"), + ("unwatching something unwatched succeeds", lambda v: v == "ok"), + ("unwatching a watched socket succeeds", lambda v: v == "ok"), + ("the watch set is empty again", lambda v: v == "0"), + ("a closed socket is not polled", lambda v: v == "0"), + ("a closed socket leaves the watch set", lambda v: v == "0"), + ("the packed handle is recovered", lambda v: v == "5"), + ("the packed flags are recovered", lambda v: v == "1"), + ("packed readability is read", lambda v: v == "1"), + ("packed finishedness is read", lambda v: v == "0"), + ("packed writability is read", lambda v: v == "0"), + ] + if len(lines) != len(expected): + return [f"poll: expected {len(expected)} lines, got {len(lines)}: " + f"{finished.stdout!r}{finished.stderr!r}"] + problems: list[str] = [] + for (label, holds), value in zip(expected, lines): + if not holds(value): + problems.append(f"poll: {label} gave {value!r}") + return problems + + +def case_event_loop(compiler, interpreter, stdlib, workdir) -> list[str]: + """Every first fragment must be processed before any client sends its tail. + + A serial read-until-complete server cannot reach this barrier. This proves + overlapping request processing without deriving it from a throughput number. + """ + clients = 64 + release_tails = threading.Event() + problems: list[str] = [] + port = free_port() + artifact = build(compiler, stdlib, + EVENT_LOOP_PAGE.replace("PORT", str(port)) + .replace("EXPECTED", str(clients)), + workdir, "event_loop") + page = run_page(interpreter, artifact) + answered: list[bytes] = [] + failures: list[str] = [] + guard = threading.Lock() + + def one_client() -> None: + try: + with socket.create_connection(("127.0.0.1", port), timeout=30) as client: + client.sendall(b"part-one ") + if not release_tails.wait(timeout=25): + raise AssertionError("server did not process every partial request") + client.sendall(b"END") + reply = client.recv(64) + with guard: + (answered if reply.startswith(b"ok:") else failures).append(reply) + except Exception as failure: # noqa: BLE001 + with guard: + failures.append(repr(failure)) + + workers = [] + try: + wait_for_line(page, "ready") + workers = [threading.Thread(target=one_client) for _ in range(clients)] + for worker in workers: + worker.start() + wait_for_line(page, "all-partial") + release_tails.set() + deadline = time.monotonic() + 35 + for worker in workers: + worker.join(timeout=max(0, deadline - time.monotonic())) + if any(worker.is_alive() for worker in workers): + raise AssertionError("client threads exceeded their shared deadline") + + if len(answered) != clients: + problems.append( + f"event-loop: {len(answered)} of {clients} clients were answered; " + f"first failures {failures[:2]}") + page.wait(timeout=30) + output = page.stdout.read() + if f"answered {clients}" not in output: + problems.append(f"event-loop: the page said {output!r}") + # The point of the whole exercise: they were open simultaneously. + if f"most-at-once {clients}" not in output: + problems.append( + "event-loop: the server never held all the connections at once, " + f"so it was draining them in batches; it said {output!r}") + except (AssertionError, subprocess.TimeoutExpired) as failure: + problems.append(f"event-loop: {failure!r}") + finally: + release_tails.set() + cleanup_page(page) + deadline = time.monotonic() + 35 + for worker in workers: + worker.join(timeout=max(0, deadline - time.monotonic())) + if any(worker.is_alive() for worker in workers): + raise AssertionError("client thread cleanup exceeded its shared deadline") + return problems + +def case_nonblocking(compiler, interpreter, stdlib, workdir) -> list[str]: + port = free_port() + artifact = build(compiler, stdlib, NONBLOCK_PAGE.replace("PORT", str(port)), workdir, "nonblocking") + page = run_page(interpreter, artifact) + try: + wait_for_line(page, "ready") + with socket.create_connection(("127.0.0.1", port), timeout=10) as client: + wait_for_line(page, "idle -1") + payload = b"abc\0\xff" + client.sendall(payload) + received = b"" + while len(received) < len(payload): + piece = client.recv(64) + if not piece: break + received += piece + if received != payload: + return [f"nonblocking: actual bytes {received!r} differ from {payload!r}"] + client.shutdown(socket.SHUT_WR) + page.wait(timeout=20) + output = page.stdout.read() + expected = ["received 5", "sent 5", "closed 0", "cleared 0", "stale-ready 0", "done"] + if page.returncode != 0 or output.splitlines() != expected: + return [f"nonblocking: exit {page.returncode}, output {output!r}"] + return [] + finally: + cleanup_page(page) + + +def case_shared_listener(compiler, interpreter, stdlib, workdir) -> list[str]: + artifact = build(compiler, stdlib, SHARED_PAGE.replace("PORT", str(free_port())), workdir, "shared_listener") + result = subprocess.run([str(interpreter), str(artifact)], stdin=subprocess.DEVNULL, + capture_output=True, text=True, timeout=20) + expected = (["unsupported net: this platform has no shared-listener SO_REUSEPORT support"] + if os.name == "nt" else ["both shared", "ordinary refused"]) + if result.returncode != 0 or result.stderr or result.stdout.splitlines() != expected: + return [f"shared listener: exit {result.returncode}, output {result.stdout!r}, error {result.stderr!r}"] + return [] + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--compiler", type=Path, required=True) + parser.add_argument("--interpreter", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + + compiler = args.compiler.resolve(strict=True) + interpreter = args.interpreter.resolve(strict=True) + stdlib = (args.root / "nilstd" / "src").resolve(strict=True) + + cases = ( + ("echo", case_echo), + ("handles", case_handles), + ("port-released", case_port_released), + ("timeout", case_timeout), + ("peer-hangup", case_peer_hangup), + ("client", case_client), + ("poll", case_poll), + ("event-loop", case_event_loop), + ("nonblocking", case_nonblocking), + ("shared-listener", case_shared_listener), + ) + + problems: list[str] = [] + with tempfile.TemporaryDirectory() as raw: + workdir = Path(raw) + for name, run in cases: + try: + problems.extend(run(compiler, interpreter, stdlib, workdir)) + except Exception as failure: # noqa: BLE001 - report, never abort the run + problems.append(f"{name}: raised {failure!r}") + + if problems: + print("Network intrinsic contract failures:") + for problem in problems: + print(f" - {problem}") + return 1 + + print(f"Network intrinsics: {len(cases)} contract case(s) held — " + "listen/accept/read/write carry bytes unchanged, handles are opaque, " + "a listener is not a connection, the runtime releases a leaked port, " + "a read deadline expires, a lost peer is reported rather than fatal, " + "connecting out resolves a name, the watch set behaves, and an event " + "loop holds many connections at once instead of draining them in " + "turn.") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/tools/project_build_contract.py b/tests/tools/project_build_contract.py index 296ddcbd..4b3fede9 100644 --- a/tests/tools/project_build_contract.py +++ b/tests/tools/project_build_contract.py @@ -140,8 +140,15 @@ def save(value=base, *, file=project): file.write_text(json.dumps(value), encoding='utf8', newline='\n') def invoke(command, *flags, status=0, cwd=root, explicit_tools=True, sdk=True, - project_flag=True, environment=None, timeout=45): + project_flag=True, environment=None, timeout=None): nonlocal count + measured_build = sys.platform == 'darwin' and command in ('build', 'run') and explicit_tools + if timeout is None: + # Darwin independently hashes full loaded-image/shared-cache + # inputs before and after real builds. Hosted VM builds have + # exceeded 45 seconds; native builds measured 144 seconds before execution. + native = any(left == '--target' and right == 'native' for left, right in zip(flags, flags[1:])) + timeout = (300 if native else 180) if measured_build else 45 call = [args.builder, command] if project_flag: call += ['--project', str(project)] @@ -150,8 +157,15 @@ def invoke(command, *flags, status=0, cwd=root, explicit_tools=True, sdk=True, if sdk: call += ['--sdk', args.stdlib] call += list(map(str, flags)) - result = subprocess.run(call, cwd=cwd, text=True, capture_output=True, - encoding='utf-8', timeout=timeout, env=environment) + if measured_build: + print(f'nilbuild contract: starting {command} ({timeout}s bound)', flush=True) + started = time.monotonic() + try: + result = subprocess.run(call, cwd=cwd, text=True, capture_output=True, + encoding='utf-8', timeout=timeout, env=environment) + finally: + if measured_build: + print(f'nilbuild contract: {command} elapsed {time.monotonic() - started:.3f}s', flush=True) assert result.returncode == status, (call, result.returncode, result.stdout, result.stderr) count += 1 return result diff --git a/tests/tools/semantic_contract.py b/tests/tools/semantic_contract.py index c5d91f25..44386b19 100644 --- a/tests/tools/semantic_contract.py +++ b/tests/tools/semantic_contract.py @@ -324,6 +324,50 @@ def rename_local(result, file, target, new_name): snapshot = next(item['text'] for item in renamed['semantic']['sources'] if item['path'] == str(root / name)) assert '{{' + spelling + '}}' in snapshot + # A multiline formatted block removes the block's common indentation + # from its literal text only. Every hole keeps its original file + # coordinates, so a rename edits the expression and leaves identical + # words in the dedented literal untouched. + block = ('!!! \u00e9\U0001F600\r\nbook formatted-block-editing\r\n' + 'message returns number begin paragraph\r\n' + ' value local-value is 7\r\n' + ' say formatted begin text\r\n' + ' {{local-value}} {local-value plus 1}\r\n' + ' end text\r\n' + ' give local-value\r\n' + 'end paragraph\r\n') + name = 'formatted-block.matrix' + result = analyze({name: block}, name) + target = binding(result, name, '{local-value plus', 'local-value') + assert target['rename_safe'] and target['references_complete'], target + assert len([use for use in result['semantic']['references'] + if use['symbol_id'] == target['id']]) == 2 + renamed = rename_local(result, name, target, 'renamed-value') + snapshot = next(item['text'] for item in renamed['semantic']['sources'] + if item['path'] == str(root / name)) + assert '{{local-value}}' in snapshot, snapshot + assert '{renamed-value plus 1}' in snapshot, snapshot + + # Ny closes the block inside the enclosing call. Its independently + # emitted tokens must preserve the same UTF-8/CRLF editing spans. + block_ny = ('# \u00e9\U0001F600\r\nfn message() -> i32\r\n' + ' let local_value = 7\r\n' + ' println($"\"\" \t\r\n' + ' {{local_value}} {local_value + 1}\r\n' + ' "\"\")\r\n' + ' return local_value\r\nend fn\r\n') + name = 'formatted-block.ny' + result = analyze({name: block_ny}, name) + target = binding(result, name, '{local_value +', 'local_value') + assert target['rename_safe'] and target['references_complete'], target + assert len([use for use in result['semantic']['references'] + if use['symbol_id'] == target['id']]) == 2 + renamed = rename_local(result, name, target, 'renamed_value') + snapshot = next(item['text'] for item in renamed['semantic']['sources'] + if item['path'] == str(root / name)) + assert '{{local_value}}' in snapshot, snapshot + assert '{renamed_value + 1}' in snapshot, snapshot + interpolation = ('fn message(input: i32) -> i32\nprintln("\\n{input}")\nreturn input\nend fn\n') result = analyze({'interpolation.ny': interpolation}, 'interpolation.ny') assert len(result['semantic']['references']) == 1 diff --git a/tests/tools/test_ui_appkit_display_contract.py b/tests/tools/test_ui_appkit_display_contract.py new file mode 100644 index 00000000..4d5e3dbf --- /dev/null +++ b/tests/tools/test_ui_appkit_display_contract.py @@ -0,0 +1,283 @@ +#!/usr/bin/env python3 +"""Portable evidence/launch controls; actual AppKit input needs a Mac desktop.""" +import contextlib +import argparse +import copy +import importlib.util +import io +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +from types import SimpleNamespace +import unittest +from unittest import mock + +spec = importlib.util.spec_from_file_location('appkit_display_contract', + Path(__file__).with_name('ui_display_contract.py')) +contract = importlib.util.module_from_spec(spec) +spec.loader.exec_module(contract) + +options = argparse.ArgumentParser(add_help=False) +options.add_argument('--compile-driver', type=Path) +options.add_argument('--input-driver', type=Path) +fixture_options, remaining = options.parse_known_args() +sys.argv = [sys.argv[0], *remaining] + + +class AppKitHarnessTests(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory(prefix='nil-appkit-harness-unit-') + self.addCleanup(self.temporary.cleanup) + self.root = Path(self.temporary.name).resolve() + self.driver = self.root / 'test-only.dylib' + self.driver.write_bytes(b'not a loadable library; mock launch only') + self.token = 'a' * 32 + self.parent = 700 + + def evidence(self, directory=None, token=None, route='vm'): + directory = directory or self.root + token = token or self.token + tests = [] + for offset, (name, stage, keys) in enumerate((('keyboard_events', 3, 8), ('close_request', 1, 0)), 1): + record = {'schema': 'nil.appkit.input.v1', 'token': token, 'route': route, + 'test': name, 'pid': self.parent + offset, 'parentPid': self.parent, + 'status': 'passed', 'reason': 'fixture', 'stage': stage, 'keys': keys, + 'scope': 'owned-process AppKit responder input; no Accessibility or global input'} + (directory / f'{token}-{record["pid"]}.json').write_text(json.dumps(record), encoding='utf-8') + tests.append({'name': name, 'suite': 'nil.std.ui.gtk.interaction', 'route': route, + 'status': 'passed', 'exitCode': 77, 'signal': 0, 'timedOut': False, + 'cancelled': False, 'stdout': 'language assertions passed', + 'stderr': 'NIL_APPKIT_INPUT ' + json.dumps(record) + '\n'}) + return {'schema': 'nil.test.results.v1', 'errors': [], 'tests': tests} + + def case_directory(self): + directory = self.root / 'evidence' + directory.mkdir() + return directory + + def test_exact_language_cases_and_private_input_records_are_required(self): + directory = self.case_directory() + report = self.evidence(directory) + self.assertEqual(len(contract.validate_appkit_interaction(report, 'vm', directory, + self.token, self.parent)), 2) + for mutate in (lambda value: value['tests'].pop(), + lambda value: value['tests'].append(value['tests'][0]), + lambda value: value['tests'][0].update(route='native'), + lambda value: value['tests'][0].update(status='skipped'), + lambda value: value['tests'][0].update(exitCode=0), + lambda value: value['tests'][0].update(timedOut=True), + lambda value: value['tests'][0].update(signal=15), + lambda value: value['tests'][0].update(cancelled=True), + lambda value: value['tests'][0].update(stderr=''), + lambda value: value['errors'].append('compile failed')): + altered = copy.deepcopy(report) + mutate(altered) + with self.assertRaises(AssertionError): + contract.validate_appkit_interaction(altered, 'vm', directory, self.token, self.parent) + + def test_foreign_pid_token_route_or_incomplete_delivery_cannot_qualify(self): + directory = self.case_directory() + report = self.evidence(directory) + for change in ({'parentPid': self.parent + 90}, {'token': 'b' * 32}, {'route': 'native'}, + {'pid': self.parent}, {'pid': -1}, {'pid': True}, {'test': 'close_request'}, + {'keys': 7}, {'stage': 2}, {'status': 'failed'}): + altered = copy.deepcopy(report) + record = json.loads(altered['tests'][0]['stderr'].removeprefix('NIL_APPKIT_INPUT ')) + record.update(change) + altered['tests'][0]['stderr'] = 'NIL_APPKIT_INPUT ' + json.dumps(record) + with self.subTest(change=change), self.assertRaises(AssertionError): + contract.validate_appkit_interaction(altered, 'vm', directory, self.token, self.parent) + # Python bool compares equal to 0/1; typed record fields must reject it + # even when the corresponding language case's expected value is 0/1. + for field in ('stage', 'keys'): + altered = copy.deepcopy(report) + record = json.loads(altered['tests'][1]['stderr'].removeprefix('NIL_APPKIT_INPUT ')) + record[field] = bool(record[field]) + altered['tests'][1]['stderr'] = 'NIL_APPKIT_INPUT ' + json.dumps(record) + with self.subTest(boolean_field=field), self.assertRaises(AssertionError): + contract.validate_appkit_interaction(altered, 'vm', directory, self.token, self.parent) + + def test_extra_duplicate_missing_or_tampered_records_fail(self): + directory = self.case_directory() + report = self.evidence(directory) + duplicate = copy.deepcopy(report) + duplicate['tests'][0]['stderr'] *= 2 + with self.assertRaises(AssertionError): + contract.validate_appkit_interaction(duplicate, 'vm', directory, self.token, self.parent) + extra = directory / 'unexpected.json' + extra.write_text('{}') + with self.assertRaises(AssertionError): + contract.validate_appkit_interaction(report, 'vm', directory, self.token, self.parent) + extra.unlink() + path = next(directory.iterdir()) + original = path.read_text() + path.write_text('{}') + with self.assertRaises(AssertionError): + contract.validate_appkit_interaction(report, 'vm', directory, self.token, self.parent) + path.unlink() + with self.assertRaises(AssertionError): + contract.validate_appkit_interaction(report, 'vm', directory, self.token, self.parent) + path.write_text(original) + + def test_explicit_injection_preserves_environment_and_rejects_ambient_composition(self): + original = {'PATH': '/fixture/bin', 'DYLD_FALLBACK_LIBRARY_PATH': '/fixture/runtime'} + result = contract.appkit_interaction_environment(original, self.driver, self.root, self.token, 'vm', self.driver) + self.assertEqual(result['PATH'], original['PATH']) + self.assertEqual(result['DYLD_FALLBACK_LIBRARY_PATH'], original['DYLD_FALLBACK_LIBRARY_PATH']) + self.assertNotIn('DYLD_INSERT_LIBRARIES', original) + self.assertEqual(result['DYLD_INSERT_LIBRARIES'], str(self.driver)) + for ambient in (dict(original, DYLD_INSERT_LIBRARIES='/other.dylib'), + dict(original, NIL_TEST_APPKIT_INPUT_TOKEN=self.token)): + with self.assertRaises(AssertionError): + contract.appkit_interaction_environment(ambient, self.driver, self.root, self.token, 'vm', self.driver) + with self.assertRaises(AssertionError): + contract.appkit_interaction_environment(original, self.root / 'absent', self.root, self.token, 'vm', self.driver) + + def test_actual_route_orchestration_scopes_injection_and_retains_complete_evidence(self): + source = self.root / 'unchanged.matrix' + source.write_text('owned test fixture') + args = SimpleNamespace(interaction_source=source, appkit_input_driver=self.driver, + appkit_compile_driver=self.driver, compiler=Path(sys.executable).resolve(), + appkit_evidence_directory=self.root / 'runs', llvm='ON') + ambient = {'PATH': '/owned/tools'} + seen = [] + def checked(command, environment, timeout, capture_prefix): + self.assertEqual(timeout, 120) + self.assertEqual(command[-1], source) + route = command[command.index('--target') + 1] + self.assertEqual(environment['NIL_TEST_APPKIT_INPUT_ROUTE'], route) + self.assertEqual(environment['DYLD_INSERT_LIBRARIES'], str(self.driver)) + self.assertEqual(environment['NIL_TEST_APPKIT_INPUT_COMPILER'], str(args.compiler)) + self.assertEqual(command[command.index('--compiler') + 1], args.appkit_compile_driver) + self.assertEqual(capture_prefix.name, route) + seen.append(route) + report = self.evidence(Path(environment['NIL_TEST_APPKIT_INPUT_DIRECTORY']), + environment['NIL_TEST_APPKIT_INPUT_TOKEN'], route) + return SimpleNamespace(stdout=json.dumps(report), stderr='', pid=self.parent) + with mock.patch.object(contract, 'checked', side_effect=checked), contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(contract.run_appkit_interaction(args, ['owned-niltest', 'run', '--compiler', args.compiler], ambient), 4) + self.assertEqual(seen, ['vm', 'native']) + self.assertEqual(ambient, {'PATH': '/owned/tools'}) + report = json.loads(next(args.appkit_evidence_directory.glob('*/report.json')).read_text()) + self.assertEqual(report['status'], 'passed') + self.assertEqual([route['languageCases'] for route in report['routes']], [2, 2]) + self.assertEqual(report['crossProcessAccessibility'], 'not exercised') + + def test_failed_language_completion_retains_failure_instead_of_accepting_input_only(self): + source = self.root / 'unchanged.matrix' + source.write_text('owned test fixture') + args = SimpleNamespace(interaction_source=source, appkit_input_driver=self.driver, + appkit_compile_driver=self.driver, compiler=Path(sys.executable).resolve(), + appkit_evidence_directory=self.root / 'runs', llvm='OFF') + def failed(command, environment, timeout, capture_prefix): + report = self.evidence(Path(environment['NIL_TEST_APPKIT_INPUT_DIRECTORY']), + environment['NIL_TEST_APPKIT_INPUT_TOKEN']) + report['tests'][0]['exitCode'] = 1 + return SimpleNamespace(stdout=json.dumps(report), stderr='', pid=self.parent) + with mock.patch.object(contract, 'checked', side_effect=failed), contextlib.redirect_stdout(io.StringIO()): + with self.assertRaises(AssertionError): + contract.run_appkit_interaction(args, ['owned-niltest', 'run', '--compiler', args.compiler], {}) + report = json.loads(next(args.appkit_evidence_directory.glob('*/report.json')).read_text()) + self.assertEqual(report['status'], 'failed') + + def test_owned_process_must_exit_naturally_with_success(self): + prefix = self.root / 'raw' + result = contract.checked([sys.executable, '-c', 'print("owned")'], os.environ.copy(), + timeout=5, capture_prefix=prefix) + self.assertEqual(result.stdout.strip(), 'owned') + self.assertEqual(prefix.with_suffix('.stdout').read_text(), 'owned\n') + self.assertGreater(result.pid, 0) + with self.assertRaises(AssertionError): + contract.checked([sys.executable, '-c', + 'import sys; print("failed stdout"); print("failed stderr", file=sys.stderr); raise SystemExit(7)'], + os.environ.copy(), timeout=5, capture_prefix=prefix) + self.assertEqual(prefix.with_suffix('.stdout').read_text(), 'failed stdout\n') + self.assertEqual(prefix.with_suffix('.stderr').read_text(), 'failed stderr\n') + # The production Windows display harness uses Jobs. This POSIX helper + # never owns a timed-out Windows process in the real display workflow. + if os.name != 'nt': + with self.assertRaises(subprocess.TimeoutExpired): + contract.checked([sys.executable, '-c', + 'import sys,time; print("partial stdout", flush=True); print("partial stderr", file=sys.stderr, flush=True); time.sleep(30)'], + os.environ.copy(), timeout=.2, capture_prefix=prefix) + self.assertEqual(prefix.with_suffix('.stdout').read_text(), 'partial stdout\n') + self.assertEqual(prefix.with_suffix('.stderr').read_text(), 'partial stderr\n') + + +if fixture_options.compile_driver: + class CompiledAdapterTests(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory(prefix='nil-appkit-adapter-unit-') + self.addCleanup(self.temporary.cleanup) + self.root = Path(self.temporary.name).resolve() + self.wrapper = fixture_options.compile_driver.resolve(strict=True) + if sys.platform == 'darwin': + self.assertIsNotNone(fixture_options.input_driver, 'Darwin adapter controls need the real compatible input dylib') + self.driver = fixture_options.input_driver.resolve(strict=True) + else: + self.driver = self.root / 'input.dylib' + self.driver.write_bytes(b'non-Darwin environment control; never loaded') + # No inherited override is removed silently. The control requires a + # clean caller and then supplies its own explicit fixture profile. + self.assertFalse(os.environ.get('DYLD_INSERT_LIBRARIES')) + self.env = contract.appkit_interaction_environment(dict(os.environ), self.driver, self.root, + 'b' * 32, 'native', Path(sys.executable).resolve(strict=True)) + self.env['NIL_TEST_PRESERVED_SETTING'] = 'ordinary value with spaces $ and quotes' + self.env['DYLD_FALLBACK_LIBRARY_PATH'] = str(self.root) + + def test_real_adapter_cleans_compile_environment_and_preserves_arguments(self): + program = ('import json,os,sys; print(json.dumps({' + '"env":{key:os.environ.get(key) for key in ("PATH","NIL_TEST_PRESERVED_SETTING","DYLD_FALLBACK_LIBRARY_PATH","DYLD_INSERT_LIBRARIES")},' + '"fixtureNames":[key for key in os.environ if key.startswith("NIL_TEST_APPKIT_INPUT_")],' + '"args":sys.argv[1:],"pid":os.getpid(),"parentPid":os.getppid()}))') + process = subprocess.Popen([str(self.wrapper), '-c', program, 'argument with spaces', '$literal'], + env=self.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + try: + output, errors = process.communicate(timeout=10) + finally: + if process.poll() is None: + process.kill(); process.communicate(timeout=3) + self.assertEqual(process.returncode, 0, errors) + observed = json.loads(output) + self.assertEqual(observed['pid'], process.pid, 'exec must preserve the owned compiler PID') + self.assertEqual(observed['parentPid'], os.getpid(), 'exec must preserve the parent relationship') + self.assertEqual(observed['args'], ['argument with spaces', '$literal']) + self.assertIsNone(observed['env']['DYLD_INSERT_LIBRARIES']) + self.assertEqual(observed['fixtureNames'], []) + self.assertEqual(observed['env']['NIL_TEST_PRESERVED_SETTING'], self.env['NIL_TEST_PRESERVED_SETTING']) + self.assertEqual(observed['env']['PATH'], self.env['PATH']) + self.assertEqual(observed['env'].get('DYLD_FALLBACK_LIBRARY_PATH'), self.env['DYLD_FALLBACK_LIBRARY_PATH']) + # The parent environment still drives niltest's application route. + self.assertEqual(self.env['DYLD_INSERT_LIBRARIES'], str(self.driver)) + + def test_real_adapter_propagates_compiler_status(self): + result = subprocess.run([str(self.wrapper), '-c', 'raise SystemExit(7)'], env=self.env, + capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 7, result.stderr) + + def test_real_adapter_rejects_mismatched_missing_and_recursive_profiles(self): + other = self.root / 'other-driver.dylib' + other.write_bytes(b'declared only; never loaded') + nonexecutable = self.root / 'nonexecutable-compiler' + nonexecutable.write_bytes(b'not executable') + for change in ({'NIL_TEST_APPKIT_INPUT_DRIVER': str(other)}, + {'DYLD_INSERT_LIBRARIES': ''}, + {'NIL_TEST_APPKIT_INPUT_DRIVER': ''}, + {'NIL_TEST_APPKIT_INPUT_COMPILER': str(self.root / 'absent')}, + {'NIL_TEST_APPKIT_INPUT_COMPILER': 'relative/compiler'}, + {'NIL_TEST_APPKIT_INPUT_COMPILER': str(self.wrapper)}, + {'NIL_TEST_APPKIT_INPUT_COMPILER': str(nonexecutable)}, + {'NIL_TEST_APPKIT_INPUT_DIRECTORY': str(self.root / 'absent')}): + with self.subTest(change=change): + result = subprocess.run([str(self.wrapper), '-c', 'print("UNEXPECTED COMPILER EXECUTION")'], + env=dict(self.env, **change), capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 85, result.stderr) + self.assertNotIn('UNEXPECTED COMPILER EXECUTION', result.stdout) + self.assertIn('AppKit test compiler adapter:', result.stderr) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/tools/tool_discovery_contract.py b/tests/tools/tool_discovery_contract.py index e736254b..efeb259e 100644 --- a/tests/tools/tool_discovery_contract.py +++ b/tests/tools/tool_discovery_contract.py @@ -7,7 +7,9 @@ import os from pathlib import Path import subprocess +import sys import tempfile +import time def main() -> None: @@ -51,11 +53,29 @@ def main() -> None: path_environment = {**environment, 'PATH': os.pathsep.join( [str(work / 'missing tool directory'), str(compiler.parent), *retained_path])} - def run(command, *, env=adjacent_environment, status=0, executable=None): + def run(command, *, env=adjacent_environment, status=0, executable=None, timeout=60): nonlocal checks - result = subprocess.run(list(map(str, command)), cwd=work, env=env, - stdin=subprocess.DEVNULL, capture_output=True, - text=True, timeout=60, executable=executable) + argv = list(map(str, command)) + phase = f'{checks + 1}: {Path(argv[0]).name} {argv[1]}' + print(f'tool discovery phase {phase}: starting, timeout={timeout}s', flush=True) + started = time.monotonic() + try: + result = subprocess.run(argv, cwd=work, env=env, + stdin=subprocess.DEVNULL, capture_output=True, + text=True, timeout=timeout, executable=executable) + except subprocess.TimeoutExpired as error: + # TimeoutExpired may carry bytes even when text=True. Preserve + # captured diagnostics before the traceback loses those fields. + def captured(value): + return value.decode('utf-8', errors='replace') if isinstance(value, bytes) else value or '' + print(json.dumps({'phase': phase, 'command': argv, + 'elapsed_seconds': time.monotonic() - started, + 'timeout_seconds': timeout, + 'stdout': captured(error.stdout), + 'stderr': captured(error.stderr)}), file=sys.stderr, flush=True) + raise + print(f'tool discovery phase {phase}: exit={result.returncode}, ' + f'elapsed={time.monotonic() - started:.2f}s', flush=True) assert result.returncode == status, (command, result.returncode, result.stdout, result.stderr) checks += 1 @@ -68,7 +88,11 @@ def verify_build_tools(build_command, flags=(), *, env=adjacent_environment): checked = run([build_command, 'check', *build_options, '--format', 'json', *flags], env=env) assert json.loads(checked.stdout)['success'] - ran = run([build_command, 'run', *build_options, *flags], env=env) + # Real Darwin project runs fingerprint the complete toolchain before + # executing the VM. Use the measured project VM budget for only + # these two calls; discovery/check/error paths retain sixty seconds. + ran = run([build_command, 'run', *build_options, *flags], env=env, + timeout=180 if sys.platform == 'darwin' else 60) assert ran.stdout == 'discovered application\n', ran tested = run([build_command, 'test', *build_options, '--format', 'json', *flags], env=env) diff --git a/tests/tools/ui_display_contract.py b/tests/tools/ui_display_contract.py index f64bdc1e..e959c206 100644 --- a/tests/tools/ui_display_contract.py +++ b/tests/tools/ui_display_contract.py @@ -7,6 +7,7 @@ from contextlib import contextmanager import ctypes as c import ctypes.util +import hashlib import json import os from pathlib import Path @@ -229,20 +230,153 @@ def check_errors(self): assert not unexpected, ('unexpected X11 protocol error', unexpected) -def checked(command, env, timeout=180): +def checked(command, env, timeout=180, capture_prefix=None): process = subprocess.Popen(list(map(str, command)), env=env, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, start_new_session=True) + output, errors = '', '' + def partial(value): + return value.decode('utf-8', errors='replace') if isinstance(value, bytes) else value or '' try: - output, errors = process.communicate(timeout=timeout) + try: + output, errors = process.communicate(timeout=timeout) + except subprocess.TimeoutExpired as expired: + # communicate() can expose bytes even in text mode. Keep the raw + # partial streams before cancellation, then drain the owned child + # once more so its cancellation diagnostics are preserved as well. + output, errors = partial(expired.output), partial(expired.stderr) + terminate(process) + try: + output, errors = process.communicate(timeout=3) + except subprocess.TimeoutExpired as draining: + output = partial(draining.output) or output + errors = partial(draining.stderr) or errors + raise assert process.returncode == 0, (command, process.returncode, output, errors) - return subprocess.CompletedProcess(command, process.returncode, output, errors) + result = subprocess.CompletedProcess(command, process.returncode, output, errors) + result.pid = process.pid + return result + finally: + try: + if capture_prefix is not None: + Path(str(capture_prefix) + '.stdout').write_text(output, encoding='utf-8') + Path(str(capture_prefix) + '.stderr').write_text(errors, encoding='utf-8') + finally: + # Give niltest time to forward cancellation to its separately grouped + # compiler/test children before killing the harness session. + terminate(process) + if process.stdout: process.stdout.close() + if process.stderr: process.stderr.close() + + +def validate_desktop_report(report, llvm): + names = {'widgets_layout_and_model_binding', 'window_ownership_and_size_errors', + 'dynamic_subtrees_and_effective_state'} + routes = {'vm', 'native'} if llvm == 'ON' else {'vm'} + expected = {(name, 'nil.std.ui.gtk.desktop', route) for name in names for route in routes} + tests = report['tests'] + assert report['schema'] == 'nil.test.results.v1' and not report['errors'], report + assert len(tests) == len(expected), report + assert {(test['name'], test['suite'], test['route']) for test in tests} == expected, report + assert all(test['status'] == 'passed' and test['exitCode'] == 77 and + not test['signal'] and not test['timedOut'] and not test['cancelled'] + for test in tests), report + + +def appkit_interaction_environment(environment, driver, directory, token, route, compiler): + """Explicit fixture injection; ordinary display/build commands keep their env.""" + assert driver.is_absolute() and driver.is_file(), driver + assert compiler.is_absolute() and compiler.is_file(), compiler + assert directory.is_absolute() and directory.is_dir(), directory + assert len(token) == 32 and all(byte in '0123456789abcdef' for byte in token), token + assert route in ('vm', 'native'), route + assert not environment.get('DYLD_INSERT_LIBRARIES'), 'AppKit input fixture cannot compose ambient injection' + assert not any(key.startswith('NIL_TEST_APPKIT_INPUT_') for key in environment), 'AppKit fixture declaration already present' + # The same-architecture test compiler adapter validates this declaration, + # then removes it before exec'ing the real compiler. niltest and its actual + # application children keep the input driver; system tools never load it. + return dict(environment, DYLD_INSERT_LIBRARIES=str(driver), + NIL_TEST_APPKIT_INPUT_TOKEN=token, NIL_TEST_APPKIT_INPUT_DIRECTORY=str(directory), + NIL_TEST_APPKIT_INPUT_ROUTE=route, NIL_TEST_APPKIT_INPUT_DRIVER=str(driver), + NIL_TEST_APPKIT_INPUT_COMPILER=str(compiler)) + + +def validate_appkit_interaction(report, route, directory, token, parent_pid): + expected = {'keyboard_events': (3, 8), 'close_request': (1, 0)} + assert report['schema'] == 'nil.test.results.v1' and not report['errors'], report + tests = report['tests'] + assert len(tests) == len(expected), report + assert {(test['name'], test['suite'], test['route']) for test in tests} == { + (name, 'nil.std.ui.gtk.interaction', route) for name in expected}, report + pids, paths, records = set(), set(), [] + for test in tests: + assert test['status'] == 'passed' and test['exitCode'] == 77 and not ( + test['signal'] or test['timedOut'] or test['cancelled']), test + markers = [line.removeprefix('NIL_APPKIT_INPUT ') for line in test['stderr'].splitlines() + if line.startswith('NIL_APPKIT_INPUT ')] + assert len(markers) == 1 and len(markers[0]) <= 8192, test + record = json.loads(markers[0]) + pid = record['pid'] + assert type(pid) is int and 0 < pid <= 0x7fffffff and pid != parent_pid and pid not in pids, record + assert all(type(record[field]) is int for field in ('parentPid', 'stage', 'keys')), record + assert (record['schema'], record['token'], record['route'], record['test'], record['parentPid'], + record['status'], record['stage'], record['keys']) == ( + 'nil.appkit.input.v1', token, route, test['name'], parent_pid, + 'passed', *expected[test['name']]), record + assert record['scope'] == 'owned-process AppKit responder input; no Accessibility or global input', record + path = directory / f'{token}-{pid}.json' + assert path.is_file() and not path.is_symlink() and path.stat().st_size <= 8192, path + assert json.loads(path.read_text(encoding='utf-8')) == record, (path, record) + pids.add(pid); paths.add(path); records.append(record) + assert set(directory.iterdir()) == paths, ('unexpected/missing input evidence', directory) + return records + + +def run_appkit_interaction(args, base, environment): + assert args.interaction_source and args.appkit_input_driver and args.appkit_compile_driver, 'macOS interaction qualification requires its input and compiler fixtures' + driver = args.appkit_input_driver.resolve(strict=True) + compile_driver = args.appkit_compile_driver.resolve(strict=True) + compiler = args.compiler.resolve(strict=True) + assert compile_driver.is_file() and compiler.is_file(), (compile_driver, compiler) + command_base = list(base) + compiler_slot = command_base.index('--compiler') + 1 + assert Path(command_base[compiler_slot]).resolve() == compiler, 'niltest real compiler must match the explicit fixture declaration' + command_base[compiler_slot] = compile_driver + source = args.interaction_source.resolve(strict=True) + parent = args.appkit_evidence_directory + if parent: + parent.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='nil-appkit-input-', dir=parent)).resolve() + summary = {'schema': 'nil.appkit.interaction.v1', 'status': 'running', 'routes': [], + 'inputDriver': str(driver), 'inputDriverSha256': hashlib.sha256(driver.read_bytes()).hexdigest(), + 'compileDriver': str(compile_driver), 'compileDriverSha256': hashlib.sha256(compile_driver.read_bytes()).hexdigest(), + 'compiler': str(compiler), + 'source': str(source), 'sourceSha256': hashlib.sha256(source.read_bytes()).hexdigest(), + 'scope': 'in-process AppKit responder input; test compiler adapter removes fixture injection before compilation; not default-cache proof', + 'crossProcessAccessibility': 'not exercised', 'evidenceDirectory': str(root)} + print(f'AppKit interaction evidence: {root}', flush=True) + try: + for route in (['vm', 'native'] if args.llvm == 'ON' else ['vm']): + directory = root / route + directory.mkdir(mode=0o700) + token = secrets.token_hex(16) + env = appkit_interaction_environment(environment, driver, directory, token, route, compiler) + command = [*command_base, '--target', route, '--filter', 'nil.std.ui.gtk.interaction', source] + result = checked(command, env, timeout=120, capture_prefix=root / route) + report = json.loads(result.stdout) + print(json.dumps(report, indent=2), flush=True) + records = validate_appkit_interaction(report, route, directory, token, result.pid) + summary['routes'].append({'route': route, 'parentPid': result.pid, + 'naturalCompletion': True, 'languageCases': len(report['tests']), + 'inputs': records}) + summary['status'] = 'passed' + return sum(route['languageCases'] for route in summary['routes']) + except BaseException as error: + summary.update(status='failed', error=str(error)) + raise finally: - # Give niltest time to forward cancellation to its separately grouped - # compiler/test children before killing the harness session. - terminate(process) - if process.stdout: process.stdout.close() - if process.stderr: process.stderr.close() + (root / 'report.json').write_text(json.dumps(summary, indent=2) + '\n', encoding='utf-8') + print(json.dumps(summary, indent=2), flush=True) def darwin(args): @@ -269,16 +403,14 @@ def darwin(args): desktop = checked([*base, '--target', 'all' if args.llvm == 'ON' else 'vm', '--filter', 'nil.std.ui.gtk.desktop', args.source.resolve()], env) report = json.loads(desktop.stdout) - assert report['tests'] and not report['errors'], report - assert all(test['status'] == 'passed' for test in report['tests']), report - # The interaction suite is deliberately not run here. It needs keystrokes - # delivered to another process, and on macOS posting events to a different - # process requires accessibility trust that a hosted runner does not grant. - # Real input is covered in-process instead, by the provider fixture's own - # input probe, so this is a limit of cross-process injection rather than of - # the provider. Saying so beats quietly reporting a narrower contract. - print(f'macOS desktop contract passed: {len(report["tests"])} language cases, ' - 'interaction suite not covered (cross-process input needs accessibility trust)') + # Preserve each route's actual results in CTest's retained output, and reject + # missing, duplicated or skipped cases rather than accepting any green subset. + print(json.dumps(report, indent=2), flush=True) + validate_desktop_report(report, args.llvm) + interaction_cases = run_appkit_interaction(args, base, env) + print(f'macOS desktop contract passed: {len(report["tests"])} desktop + ' + f'{interaction_cases} interaction language cases; in-process AppKit input, ' + 'cross-process Accessibility not exercised') return 0 @@ -294,6 +426,9 @@ def main(): parser.add_argument('--interaction-source', type=Path) parser.add_argument('--provider-test', type=Path) parser.add_argument('--lifecycle-test', type=Path) + parser.add_argument('--appkit-input-driver', type=Path) + parser.add_argument('--appkit-compile-driver', type=Path) + parser.add_argument('--appkit-evidence-directory', type=Path) parser.add_argument('--llvm', choices=('ON', 'OFF'), default='OFF') args = parser.parse_args() if sys.platform == 'darwin': diff --git a/tests/ui_appkit_compile_driver.cpp b/tests/ui_appkit_compile_driver.cpp new file mode 100644 index 00000000..fbd66bba --- /dev/null +++ b/tests/ui_appkit_compile_driver.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2008 - 2026 Execute Software. All rights reserved. + * SPDX-License-Identifier: MIT + */ +// Test-only niltest compiler adapter. Native application execution still uses +// the injected AppKit input driver; discovery/compilation and system linker +// children must not inherit it (Apple's launcher can have an arm64e slice). +#include +#include +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; +namespace { +int fail(const char *message) { + std::fprintf(stderr, "AppKit test compiler adapter: %s\n", message); + return 85; +} +const char *setting(const char *name) { + const char *value = std::getenv(name); + return value && *value && std::strlen(value) <= 32768 ? value : nullptr; +} +bool regular_absolute(const char *value) { + if (!value || !fs::path(value).is_absolute()) return false; + std::error_code error; + return fs::is_regular_file(value, error) && !error; +} +} + +int main(int argc, char **argv) { + const char *compiler = setting("NIL_TEST_APPKIT_INPUT_COMPILER"); + const char *driver = setting("NIL_TEST_APPKIT_INPUT_DRIVER"); + const char *injection = setting("DYLD_INSERT_LIBRARIES"); + const char *token = setting("NIL_TEST_APPKIT_INPUT_TOKEN"); + const char *directory = setting("NIL_TEST_APPKIT_INPUT_DIRECTORY"); + const char *route = setting("NIL_TEST_APPKIT_INPUT_ROUTE"); + if (!regular_absolute(compiler) || !regular_absolute(driver)) return fail("declared compiler and input driver must be absolute regular files"); + if (!injection || std::strcmp(injection, driver)) return fail("injection does not match the declared fixture driver"); + if (!token || std::strlen(token) != 32 || std::strspn(token, "0123456789abcdef") != 32 || + !route || (std::strcmp(route, "vm") && std::strcmp(route, "native"))) return fail("invalid fixture token or route"); + std::error_code error; + if (!directory || !fs::path(directory).is_absolute() || !fs::is_directory(directory, error) || error) + return fail("fixture evidence directory is not an absolute directory"); + if (fs::equivalent(compiler, argv[0], error) || error) return fail("recursive or unresolved compiler adapter target"); + // Keep a private copy before removing the declaration that owns getenv's + // storage. All ordinary environment settings and arguments remain intact. + std::string selected(compiler); + std::vector arguments; + arguments.reserve(static_cast(argc) + 1); + arguments.push_back(selected.data()); + for (int index = 1; index < argc; ++index) arguments.push_back(argv[index]); + arguments.push_back(nullptr); + for (const char *name : {"DYLD_INSERT_LIBRARIES", "NIL_TEST_APPKIT_INPUT_COMPILER", + "NIL_TEST_APPKIT_INPUT_DRIVER", "NIL_TEST_APPKIT_INPUT_TOKEN", + "NIL_TEST_APPKIT_INPUT_DIRECTORY", "NIL_TEST_APPKIT_INPUT_ROUTE"}) + if (unsetenv(name)) return fail("could not remove fixture-owned environment declaration"); + execv(selected.c_str(), arguments.data()); + return fail("could not execute the declared compiler"); +} diff --git a/tests/ui_appkit_input_driver.mm b/tests/ui_appkit_input_driver.mm new file mode 100644 index 00000000..f2398afb --- /dev/null +++ b/tests/ui_appkit_input_driver.mm @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2008 - 2026 Execute Software. All rights reserved. + * SPDX-License-Identifier: MIT + */ +// Test-only in-process input for the unchanged Matrix interaction suite. +// This library is never installed. It neither requests Accessibility trust nor +// posts global/cross-process input. The provider's poll-driven main run loop +// invokes the timer; actual AppKit responders then generate provider events. +#import +#import + +#include +#include +#include +#include +#include +#include + +@interface NilAppKitInputDriver : NSObject +@property(nonatomic, strong) NSTimer *timer; +@property(nonatomic, copy) NSString *token; +@property(nonatomic, copy) NSString *directory; +@property(nonatomic, copy) NSString *route; +@property(nonatomic, copy) NSString *test; +@property(nonatomic, weak) NSWindow *window; +@property(nonatomic, weak) NSTextField *entry; +@property(nonatomic, weak) NSButton *checkbox; +@property(nonatomic, weak) NSButton *button; +@property(nonatomic) NSTimeInterval deadline; +@property(nonatomic) NSUInteger stage; +@property(nonatomic) NSUInteger keys; +@property(nonatomic) pid_t parent; +@property(nonatomic) BOOL driving; +- (void)tick:(NSTimer *)timer; +@end + +// Keep the callback receiver alive through its final report, even when timer +// invalidation removes the run loop's last reference during that callback. +static NilAppKitInputDriver *active_driver; + +@implementation NilAppKitInputDriver +- (void)finish:(NSString *)status reason:(NSString *)reason { + [self.timer invalidate]; self.timer = nil; + NSDictionary *record = @{@"schema": @"nil.appkit.input.v1", @"token": self.token, + @"route": self.route, @"test": self.test ?: @"unknown", @"pid": @(getpid()), + @"parentPid": @(self.parent), @"status": status, @"reason": reason, + @"keys": @(self.keys), @"stage": @(self.stage), + @"scope": @"owned-process AppKit responder input; no Accessibility or global input"}; + NSData *data = [NSJSONSerialization dataWithJSONObject:record options:NSJSONWritingSortedKeys error:nullptr]; + NSString *path = [self.directory stringByAppendingPathComponent: + [NSString stringWithFormat:@"%@-%d.json", self.token, getpid()]]; + const int fd = open(path.fileSystemRepresentation, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW, 0600); + if (!data || fd < 0) { std::fputs("NIL AppKit input evidence creation failed\n", stderr); _exit(85); } + const char *bytes = (const char *)data.bytes; + std::size_t pending = data.length; + while (pending) { + const auto count = write(fd, bytes, pending); + if (count < 0 && errno == EINTR) continue; + if (count <= 0) { close(fd); _exit(85); } + bytes += count; pending -= static_cast(count); + } + if (close(fd)) _exit(85); + std::fputs("NIL_APPKIT_INPUT ", stderr); + std::fwrite(data.bytes, 1, data.length, stderr); + std::fputc('\n', stderr); std::fflush(stderr); + if (![status isEqualToString:@"passed"]) _exit(85); +} +- (void)fail:(NSString *)reason { [self finish:@"failed" reason:reason]; } +- (BOOL)findControls:(NSView *)root { + if ([root isKindOfClass:[NSTextField class]] && ((NSTextField *)root).editable) { + if (self.entry) return NO; + self.entry = (NSTextField *)root; + } else if ([root isKindOfClass:[NSButton class]]) { + NSButton *control = (NSButton *)root; + if ([control.title isEqualToString:@"Enable setting"]) { + if (self.checkbox) return NO; + self.checkbox = control; + } else if ([control.title isEqualToString:@"Finish probe"]) { + if (self.button) return NO; + self.button = control; + } else return NO; + } + for (NSView *child in root.subviews) if (![self findControls:child]) return NO; + return YES; +} +- (void)key:(NSString *)characters code:(unsigned short)code { + NSWindow *window = self.window; + if (!window || !window.visible) { [self fail:@"owned fixture window disappeared before input"]; return; } + const auto event = ^NSEvent *(NSEventType type) { + return [NSEvent keyEventWithType:type location:NSZeroPoint modifierFlags:0 + timestamp:NSProcessInfo.processInfo.systemUptime windowNumber:window.windowNumber + context:nil characters:characters charactersIgnoringModifiers:characters + isARepeat:NO keyCode:code]; + }; + NSEvent *down = event(NSEventTypeKeyDown), *up = event(NSEventTypeKeyUp); + if (!down || !up) { [self fail:@"could not construct owned-window key events"]; return; } + // Queue key-up first so a control's synchronous key tracking can consume it; + // direct key-down targets this verified NSWindow even if the app is inactive. + // No activation policy, system focus, HID tap or other process is modified. + [NSApp postEvent:up atStart:NO]; + self.driving = YES; + [window sendEvent:down]; + self.driving = NO; + ++self.keys; +} +- (void)tick:(NSTimer *)timer { + (void)timer; + @autoreleasepool { + if (self.driving) return; + if (![NSThread isMainThread]) { [self fail:@"input timer left the process main thread"]; return; } + if (!self.test) { + if (!NSApp) return; // Discovery/compiler processes never create fixture windows. + NSWindow *found = nil; + for (NSWindow *candidate in NSApp.windows) { + if (!candidate.visible || !([candidate.title isEqualToString:@"NIL2 UI keyboard probe"] || + [candidate.title isEqualToString:@"NIL2 UI close probe"])) continue; + if (found) { self.test = @"ambiguous"; [self fail:@"more than one owned fixture window"]; return; } + found = candidate; + } + if (!found) return; + self.window = found; + self.deadline = NSProcessInfo.processInfo.systemUptime + 10; + if ([found.title isEqualToString:@"NIL2 UI close probe"]) { + self.test = @"close_request"; + [found performClose:nil]; + if (!found.visible) { [self fail:@"close request destroyed the live language handle"]; return; } + self.stage = 1; + [self finish:@"passed" reason:@"owned window-manager close action delivered"]; + return; + } + self.test = @"keyboard_events"; + if (![self findControls:found.contentView] || !self.entry || !self.checkbox || !self.button || + !self.entry.enabled || !self.checkbox.enabled || !self.button.enabled || + self.entry.currentEditor != found.firstResponder || self.entry.stringValue.length) { + [self fail:@"keyboard fixture controls or initial language-requested focus did not match"]; return; + } + self.stage = 1; + for (NSString *character in @[@"M", @"a", @"t", @"r", @"i", @"x"]) [self key:character code:0]; + return; + } + if (NSProcessInfo.processInfo.systemUptime > self.deadline) { + [self fail:@"owned input phases exceeded ten seconds"]; return; + } + if (self.stage == 1) { + if (![self.entry.stringValue isEqualToString:@"Matrix"]) return; + if (![self.window makeFirstResponder:self.checkbox]) { [self fail:@"checkbox refused in-process focus"]; return; } + self.stage = 2; [self key:@" " code:49]; + } else if (self.stage == 2) { + if (self.checkbox.state != NSControlStateValueOn) return; + if (![self.window makeFirstResponder:self.button]) { [self fail:@"button refused in-process focus"]; return; } + self.stage = 3; [self key:@" " code:49]; + [self finish:@"passed" reason:@"owned entry text and checkbox state observed; button key delivered"]; + } + } +} +@end + +__attribute__((constructor)) static void nil_appkit_input_start() { + const char *token = std::getenv("NIL_TEST_APPKIT_INPUT_TOKEN"); + if (!token) return; + const char *directory = std::getenv("NIL_TEST_APPKIT_INPUT_DIRECTORY"); + const char *route = std::getenv("NIL_TEST_APPKIT_INPUT_ROUTE"); + if (std::strlen(token) != 32 || std::strspn(token, "0123456789abcdef") != 32 || + !directory || *directory != '/' || !route || (std::strcmp(route, "vm") && std::strcmp(route, "native"))) _exit(85); + @autoreleasepool { + NilAppKitInputDriver *driver = [[NilAppKitInputDriver alloc] init]; + active_driver = driver; + driver.token = [NSString stringWithUTF8String:token]; + driver.directory = [NSString stringWithUTF8String:directory]; + driver.route = [NSString stringWithUTF8String:route]; + driver.parent = getppid(); + driver.timer = [NSTimer timerWithTimeInterval:0.01 target:driver selector:@selector(tick:) + userInfo:nil repeats:YES]; + [[NSRunLoop mainRunLoop] addTimer:driver.timer forMode:NSDefaultRunLoopMode]; + } +} diff --git a/tests/ui_layout_provider_contract.hpp b/tests/ui_layout_provider_contract.hpp new file mode 100644 index 00000000..71150070 --- /dev/null +++ b/tests/ui_layout_provider_contract.hpp @@ -0,0 +1,85 @@ +#pragma once +#include "ui_runtime.hpp" +#include +#include +#include + +// The same public-provider assertions run on GTK, Win32 and AppKit. Compare +// allocation changes and equal-control requisitions, never font-specific pixels. +template +void ui_layout_provider_contract(Check check) { + ny::ui::Context context; + const auto ok = [&](const ny::ui::Result &result) { + check(static_cast(result), result.error.c_str()); return result.number; + }; + const auto width = [&](std::int64_t widget) { return ok(context.read_number(widget, "width")); }; + const auto height = [&](std::int64_t widget) { return ok(context.read_number(widget, "height")); }; + const auto await = [&](std::int64_t window, auto condition, const char *message) { + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(3); + bool passed = false; + do { + const auto event = ok(context.poll(window)); + if (event) ok(context.release_event(event)); + if (condition()) { passed = true; break; } + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } while (std::chrono::steady_clock::now() < deadline); + check(passed, message); + }; + + const auto window = ok(context.window("NIL layout visibility and expansion", 720, 240)); + const auto row = ok(context.widget("row", window, "")); + const auto fixed = ok(context.widget("button", row, "Fixed sibling")); + const auto nested = ok(context.widget("column", row, "")); + const auto entry = ok(context.widget("entry", nested, "entry")); + ok(context.set_number(row, "spacing", 7)); + ok(context.set_number(entry, "hexpand", 1)); + ok(context.set_number(window, "visible", 1)); + await(window, [&] { return width(row) > 600 && width(entry) > 300 && width(fixed) > 0; }, + "visible descendants propagate expansion through nested containers"); + const auto before_hidden = width(entry), fixed_width = width(fixed); + ok(context.set_number(fixed, "visible", 0)); + await(window, [&] { return width(entry) >= before_hidden + fixed_width; }, + "hiding a child releases its size and spacing to the visible expanding sibling"); + ok(context.set_number(fixed, "visible", 1)); + await(window, [&] { return width(entry) == before_hidden; }, "showing a child restores the original layout"); + const auto expanded = width(nested); + ok(context.set_number(nested, "hexpand", 0)); + await(window, [&] { return width(nested) < expanded; }, "explicit false overrides descendant expansion"); + ok(context.set_number(nested, "hexpand", 1)); + await(window, [&] { return width(nested) == expanded; }, "explicit true restores expansion"); + ok(context.set_number(window, "width", 900)); + await(window, [&] { return width(entry) > before_hidden + 100; }, "resizing reaches nested expanding controls"); + ok(context.close(window)); + + const auto grid_window = ok(context.window("NIL layout spans and grid growth", 640, 320)); + const auto reference_row = ok(context.widget("row", grid_window, "")); + const std::string title = "A complete spanning control"; + const auto reference = ok(context.widget("button", reference_row, title)); + const auto grid = ok(context.widget("grid", grid_window, "")); + const auto spanning = ok(context.widget("button", grid, title)); + ok(context.grid_attach(grid, spanning, 0, 0, 3, 2)); + ok(context.set_number(grid, "spacing", 3)); + ok(context.set_number(grid_window, "visible", 1)); + await(grid_window, [&] { return width(reference) > 0 && height(reference) > 0 && + width(spanning) >= width(reference) && height(spanning) >= height(reference); }, + "a lone multi-column and multi-row child preserves its full natural requisition"); + const auto fixed_span_width = width(spanning), fixed_span_height = height(spanning); + ok(context.set_number(spanning, "hexpand", 1)); + ok(context.set_number(spanning, "vexpand", 1)); + await(grid_window, [&] { return width(spanning) > fixed_span_width + 100 && height(spanning) > fixed_span_height + 100; }, + "grid tracks distribute available horizontal and vertical space to expanding spans"); + const auto old_grid_width = width(spanning), old_grid_height = height(spanning); + ok(context.set_number(grid_window, "width", 800)); + ok(context.set_number(grid_window, "height", 480)); + await(grid_window, [&] { return width(spanning) > old_grid_width + 100 && height(spanning) > old_grid_height + 100; }, + "native grid allocations continue to expand after resizing"); + ok(context.close(grid_window)); + + const auto fill_window = ok(context.window("NIL layout cross-axis fill", 480, 180)); + const auto label = ok(context.widget("label", fill_window, "fill")); + ok(context.set_number(label, "hexpand", 0)); + ok(context.set_number(fill_window, "visible", 1)); + await(fill_window, [&] { return width(label) == width(fill_window) && width(label) > 400; }, + "default cross-axis fill is independent of requesting extra major-axis space"); + ok(context.close(fill_window)); +} diff --git a/tests/ui_layout_tests.cpp b/tests/ui_layout_tests.cpp new file mode 100644 index 00000000..8e24c0ee --- /dev/null +++ b/tests/ui_layout_tests.cpp @@ -0,0 +1,100 @@ +#include "../nilcompiler/src/ui_layout.hpp" + +#include +#include +#include +#include + +namespace { +unsigned checks = 0; +void check(bool value, const char *message) { + ++checks; + if (!value) { std::cerr << "UI layout: " << message << '\n'; std::exit(1); } +} +using ny::ui::layout::arrange; +using ny::ui::layout::expands; +using ny::ui::layout::Item; + +void invariants(const std::vector &items, std::int64_t spacing, std::int64_t available) { + const auto measured = arrange(items, spacing), placed = arrange(items, spacing, available); + check(placed.natural == measured.natural, "allocation does not alter requisition"); + std::vector occupied(placed.sizes.size()); + bool expandable = false; + for (const auto &item : items) { + if (!item.visible) continue; + expandable = expandable || item.expand; + check(placed.extent(item.start, item.span) >= item.natural, "every spanning control fits its full requisition"); + for (std::int64_t i = 0; i < item.span; ++i) occupied[static_cast(item.start + i)] = true; + } + std::int64_t end = 0; bool previous = false; + for (std::size_t index = 0; index < occupied.size(); ++index) { + if (!occupied[index]) continue; + check(placed.sizes[index] >= 0, "track size is nonnegative"); + check(placed.positions[index] == end + (previous ? spacing : 0), "occupied tracks have exactly one gap and never overlap"); + end = placed.positions[index] + placed.sizes[index]; previous = true; + } + check(end == placed.allocated, "all allocated pixels are accounted for"); + check(placed.allocated == (expandable ? std::max(available, measured.natural) : measured.natural), + "spare pixels go only to expanding tracks with no rounding loss"); +} +} + +int main() { + const auto lone = arrange({{0, 3, 101, false}}, 5); + check(lone.natural == 101 && lone.extent(0, 3) == 101, "lone spanning control is not divided down to its first cell"); + check(lone.sizes.size() == 3 && lone.sizes[0] == 31 && lone.sizes[1] == 30 && lone.sizes[2] == 30, + "span remainder uses deterministic integer pixels"); + const auto empty = arrange({{0, 1, 900, true, false}}, 7, 300); + check(empty.natural == 0 && empty.allocated == 0 && empty.sizes.empty(), "hidden-only container has no dimensions or gaps"); + const auto shown = arrange({{0, 1, 40, false}, {1, 1, 20, true}, {2, 1, 30, false}}, 5, 200); + const auto hidden = arrange({{0, 1, 40, false, false}, {1, 1, 20, true}, {2, 1, 30, false}}, 5, 200); + check(hidden.sizes[1] == shown.sizes[1] + 45 && hidden.positions[1] == 0, + "hiding a sibling releases its full requisition and gap to the expanding control"); + const auto mixed = arrange({{0, 1, 10, false}, {1, 1, 20, true}, {0, 3, 90, true}}, 3, 240); + check(mixed.sizes[0] == 10 && mixed.sizes[2] == 0 && mixed.sizes[1] == 224, + "expanding span uses an existing expanding track without growing fixed tracks"); + const auto last = arrange({{65535, 1, 17, false}}, 4096); + check(last.natural == 17 && last.positions[65535] == 0, "unused coordinate gaps do not reserve empty tracks"); + const auto maximum_span = arrange({{0, 65536, 65537, true}}, 0, 131073); + check(maximum_span.natural == 65537 && maximum_span.extent(0, 65536) == 131073 && maximum_span.sizes[0] == 3, + "full coordinate-range span preserves requisition and expansion remainder"); + + const bool leaf = expands(true, true, false); + check(expands(std::nullopt, true, expands(std::nullopt, true, leaf)), "nested containers inherit descendant expansion by default"); + check(!expands(false, true, leaf), "explicit false overrides descendant expansion"); + check(!expands(std::nullopt, true, expands(true, false, false)), "hidden descendants cannot request expansion"); + check(expands(true, true, false), "explicit true expands an empty visible container"); + check(!expands(true, false, true), "hidden container does not expand"); + + std::int64_t nested = 1; + for (unsigned level = 0; level < 12; ++level) { + const auto grid = arrange({{0, 1, nested}, {1, 65535, 1}}, 4096); + const auto next = ny::ui::layout::native_extent(grid.natural); + check(next >= nested && next <= std::numeric_limits::max(), + "nested sparse grids saturate representable native requisitions without wrapping"); + nested = next; + } + check(nested == std::numeric_limits::max(), "nested maximum spans reach the native coordinate limit safely"); + + // A small independently checked domain covers overlapping axis spans from + // controls on different rows, sparse coordinates, hidden nodes and resizing. + std::mt19937 random(0x4e494cU); + for (unsigned trial = 0; trial < 160; ++trial) { + std::vector items; + for (unsigned i = 0, count = 1 + random() % 20; i < count; ++i) { + const auto start = static_cast(random() % 48); + items.push_back({start, static_cast(1 + random() % (64 - start)), static_cast(random() % 400), + random() % 3 == 0, random() % 4 != 0}); + } + invariants(items, random() % 12, random() % 3000); + } + // The public maxima must not turn each span into a 65,536-step scan. + std::vector maximal; + for (std::int64_t index = 0; index < 4096; ++index) maximal.push_back({0, 65536, 65536 + index, index == 0}); + const auto bounded = arrange(maximal, 1, 500000); + check(bounded.natural == 69631 && bounded.allocated == 500000, + "4096 full-range spanning constraints remain bounded and conserve allocation"); + for (const auto &item : maximal) + check(bounded.extent(item.start, item.span) >= item.natural, "maximum-domain child is never clipped"); + std::cout << "UI layout: " << checks << " checks passed\n"; +} diff --git a/tests/ui_runtime_appkit_tests.mm b/tests/ui_runtime_appkit_tests.mm index c5aaf846..70813f77 100644 --- a/tests/ui_runtime_appkit_tests.mm +++ b/tests/ui_runtime_appkit_tests.mm @@ -15,6 +15,7 @@ // main() runs on the process main thread, which is where AppKit requires the // provider to be used. #include "ui_runtime.hpp" +#include "ui_layout_provider_contract.hpp" #include #include @@ -64,6 +65,125 @@ void rejected(const ny::ui::Result &result, const char *fragment, const char *wh return 0; } +NSWindow *native_window(NSString *title) { + NSWindow *found = nil; + for (NSWindow *candidate in NSApp.windows) { + if (![candidate.title isEqualToString:title]) continue; + require(found == nil, "fixture window title is unique within this process"); + found = candidate; + } + require(found != nil, "fixture window belongs to this application"); + return found; +} + +NSControl *native_control(NSView *root, NSString *text) { + if ([root isKindOfClass:[NSButton class]] && [((NSButton *)root).title isEqualToString:text]) + return (NSControl *)root; + if ([root isKindOfClass:[NSTextField class]] && [((NSTextField *)root).stringValue isEqualToString:text]) + return (NSControl *)root; + for (NSView *child in root.subviews) + if (NSControl *found = native_control(child, text)) return found; + return nil; +} + +// Native changes deliberately bypass Context setters: otherwise stale cached +// focus and model-only sensitivity can pass without affecting real controls. +void interaction_state_contract() { + @autoreleasepool { + ny::ui::Context context; + const auto window = ok(context.window("NIL AppKit interaction state", 420, 280), "create state window"); + const auto group = ok(context.widget("column", window, ""), "create state group"); + const auto nested = ok(context.widget("row", group, ""), "create nested state group"); + const auto entry = ok(context.widget("entry", nested, "editable"), "create state entry"); + const auto box = ok(context.widget("checkbox", nested, "Toggle state"), "create state checkbox"); + const auto button = ok(context.widget("button", group, "Local disabled"), "create state button"); + const auto sibling = ok(context.widget("button", window, "Outside group"), "create state sibling"); + ok(context.set_number(window, "visible", 1), "show state window"); + NSWindow *top = native_window(@"NIL AppKit interaction state"); + auto *field = (NSTextField *)native_control(top.contentView, @"editable"); + auto *check = (NSButton *)native_control(top.contentView, @"Toggle state"); + auto *local = (NSButton *)native_control(top.contentView, @"Local disabled"); + auto *outside = (NSButton *)native_control(top.contentView, @"Outside group"); + require(field && check && local && outside, "all fixture controls are native children"); + + ok(context.set_number(entry, "focus", 1), "focus state entry"); + require(field.currentEditor == top.firstResponder, "entry uses the native field editor"); + require(ok(context.read_number(entry, "focus"), "read field focus") == 1, "field editor counts as entry focus"); + require([top makeFirstResponder:outside], "native responder changes without a provider request"); + require(ok(context.read_number(entry, "focus"), "old field focus") == 0, "old focus request is not cached as current focus"); + require(ok(context.read_number(sibling, "focus"), "new native focus") == 1, "native focus change reaches the provider"); + require(ok(context.read_number(group, "focus"), "group focus outside") == 0, "unrelated group does not own focus"); + require([top makeFirstResponder:field], "native field focus changes without a provider request"); + require(ok(context.read_number(nested, "focus"), "nested field focus") == 1, "field editor belongs to its logical ancestors"); + + ok(context.set_number(button, "sensitive", 0), "disable local button"); + ok(context.set_number(group, "sensitive", 0), "disable ancestor of focused entry"); + require(!field.enabled && !check.enabled && !local.enabled && outside.enabled, + "ancestor sensitivity disables native descendants and preserves siblings"); + require(field.currentEditor == nil && ok(context.read_number(entry, "focus"), "disabled entry focus") == 0, + "disabling a focused subtree ends native editing"); + require(ok(context.read_number(box, "sensitive"), "local checkbox sensitivity") == 1, + "ancestor disable preserves the local enabled flag"); + [check performClick:nil]; [local performClick:nil]; + require(check.state == NSControlStateValueOff && next_event(context, window) == 0, + "native activation of disabled descendants changes neither state nor events"); + const auto late = ok(context.widget("button", nested, "Created disabled"), "create child under disabled ancestor"); + auto *late_native = native_control(top.contentView, @"Created disabled"); + require(late_native && !late_native.enabled, "new native children inherit disabled state"); + rejected(context.set_number(late, "focus", 1), "focus", "disabled child cannot receive focus"); + ok(context.set_number(nested, "sensitive", 0), "disable nested ancestor locally"); + ok(context.set_number(group, "sensitive", 1), "restore outer ancestor"); + require(!field.enabled && !check.enabled && !late_native.enabled && !local.enabled, + "restoring outer ancestor preserves nested and local disabled flags"); + ok(context.set_number(nested, "sensitive", 1), "restore nested ancestor"); + require(field.enabled && check.enabled && late_native.enabled && !local.enabled, + "restoring nested ancestor restores only locally enabled controls"); + [check performClick:nil]; + const auto event = next_event(context, window); + require(event != 0 && ok(context.event_number(event, "kind"), "native toggle kind") == 3 && + ok(context.event_number(event, "target"), "native toggle target") == box && + ok(context.event_number(event, "checked"), "native toggle state") == 1, + "restored native checkbox delivers its actual state change"); + ok(context.release_event(event), "release native toggle"); + + ok(context.set_number(group, "hexpand", 1), "expand state group"); + const auto old_width = ok(context.read_number(group, "width"), "state group initial width"); + [top setContentSize:NSMakeSize(620, 380)]; + (void)next_event(context, window); + require(ok(context.read_number(group, "width"), "state group resized width") > old_width, + "native window resize relayouts expanding children without a provider size write"); + + ok(context.set_text(entry, std::string(16383, 'a')), "set entry one scalar below limit"); + ok(context.set_number(entry, "focus", 1), "focus bounded entry"); + NSTextView *editor = (NSTextView *)field.currentEditor; + require(editor != nil, "bounded entry has an actual field editor"); + [editor setSelectedRange:NSMakeRange(editor.string.length, 0)]; + [editor insertText:@"\U0001f642" replacementRange:NSMakeRange(NSNotFound, 0)]; + const auto inserted = next_event(context, window); + require(inserted != 0 && ok(context.event_number(inserted, "kind"), "boundary edit kind") == 2, + "native edit at exactly 16384 scalars produces a text event"); + const std::string expected = std::string(16383, 'a') + "\xf0\x9f\x99\x82"; + require(context.event_text(inserted).text == expected && context.read_text(entry).text == expected, + "entry limit counts astral characters as one scalar, not two UTF-16 units"); + ok(context.release_event(inserted), "release boundary edit"); + [editor insertText:@"x" replacementRange:NSMakeRange(NSNotFound, 0)]; + require(context.read_text(entry).text == expected && next_event(context, window) == 0, + "native input beyond the scalar limit is rejected without a change event"); + [editor insertText:@"ok" replacementRange:NSMakeRange(0, editor.string.length)]; + const auto replaced = next_event(context, window); + require(replaced != 0 && context.read_text(entry).text == "ok", "a bounded whole-field replacement remains editable"); + ok(context.release_event(replaced), "release replacement edit"); + + // An invalid NSString arriving from the toolkit must not become a valid + // prefix at the C++ boundary, even if it bypasses the editing formatter. + require([top makeFirstResponder:outside], "finish native entry editing"); + const unichar nul_text[] = {'o', 'k', 0, 'x'}; + field.stringValue = [NSString stringWithCharacters:nul_text length:4]; + rejected(context.read_text(entry), "UTF-8", "native NUL is rejected rather than truncated"); + ok(context.close(window), "close state fixture"); + } +} + void contract() { ny::ui::Context context; const auto window = ok(context.window("NIL AppKit provider", 420, 320), "create window"); @@ -266,10 +386,12 @@ int main(int argc, char **argv) { if (mode == "--display") { require(desktop, "--display requires a window server session"); contract(); + ui_layout_provider_contract(require); + interaction_state_contract(); std::cout << "UI runtime: " << checks << " checks passed (--display)\n"; return 0; } - if (desktop) contract(); + if (desktop) { contract(); interaction_state_contract(); ui_layout_provider_contract(require); } else { ny::ui::Context context; rejected(context.window("no session", 320, 240), "window server", diff --git a/tests/ui_runtime_tests.cpp b/tests/ui_runtime_tests.cpp index 8a9e8463..cbd4b7d2 100644 --- a/tests/ui_runtime_tests.cpp +++ b/tests/ui_runtime_tests.cpp @@ -3,6 +3,7 @@ * SPDX-License-Identifier: MIT */ #include "ui_runtime.hpp" +#include "ui_layout_provider_contract.hpp" #include "ui_registry_observer.hpp" #include @@ -468,7 +469,7 @@ int main(int argc, char **argv) { if (mode == "--darwin-mixed-provider") preload_mixed_provider(); #endif basic_validation(); - if (mode == "--display") display_tests(); + if (mode == "--display") { display_tests(); ui_layout_provider_contract(require); } else if (mode == "--no-display") no_display(); else if (mode == "--noninteractive-display") noninteractive_display(); else if (mode == "--darwin-worker-first") { diff --git a/tests/ui_runtime_win32_tests.cpp b/tests/ui_runtime_win32_tests.cpp index 549767fb..bb400c66 100644 --- a/tests/ui_runtime_win32_tests.cpp +++ b/tests/ui_runtime_win32_tests.cpp @@ -14,6 +14,7 @@ // process has an interactive window station and otherwise checks that window // creation is refused, so the same test is meaningful in a service session. #include "ui_runtime.hpp" +#include "ui_layout_provider_contract.hpp" #include #include @@ -315,11 +316,12 @@ int main(int argc, char **argv) { if (mode == "--display") { require(desktop, "--display requires an interactive window station"); contract(); + ui_layout_provider_contract(require); input_probe(false); std::cout << "UI runtime: " << checks << " checks passed (--display)\n"; return 0; } - if (desktop) contract(); + if (desktop) { contract(); ui_layout_provider_contract(require); } else { // A service session has no desktop: creation must fail, and say why. ny::ui::Context context; diff --git a/vscode/src/test/taskEvidence.test.ts b/vscode/src/test/taskEvidence.test.ts new file mode 100644 index 00000000..5d99af75 --- /dev/null +++ b/vscode/src/test/taskEvidence.test.ts @@ -0,0 +1,2 @@ +// Fixture diagnostics are pure Node controls; they do not qualify an editor host. +require("../../test/task-evidence.test.cjs"); diff --git a/vscode/test/project-checks.cjs b/vscode/test/project-checks.cjs index 40c2c8ae..9b73e2a0 100644 --- a/vscode/test/project-checks.cjs +++ b/vscode/test/project-checks.cjs @@ -10,12 +10,23 @@ async function eventually(operation, description) { throw new Error(`Timed out: ${description}`); } const findings = uri => vscode.languages.getDiagnostics(uri).filter(item => item.source === "nil-analysis"); +// Imported files which are not open are reported by their compiler-resolved +// path. A workspace may use a physical alias (notably /tmp on Darwin). Inspect +// every matching URI without deduplication, so duplicate Problems still fail. +async function importedFindings(uri) { + const results = []; + for (const [candidate, diagnostics] of vscode.languages.getDiagnostics()) { + if (candidate.scheme === "file" && await sameFile(candidate.fsPath, uri.fsPath)) + results.push(...diagnostics.filter(item => item.source === "nil-analysis")); + } + return results; +} async function replace(document, text) { const edit = new vscode.WorkspaceEdit(); edit.replace(document.uri, new vscode.Range(document.positionAt(0), document.positionAt(document.getText().length)), text); assert.equal(await vscode.workspace.applyEdit(edit), true); } -exports.run = async ({ check, workspace }) => { +exports.run = async ({ check, workspace, taskEvidence }) => { for (const language of ["ny", "matrix"]) { const directory = path.join(workspace, `project ${language}`); await fs.mkdir(path.join(directory, "src"), { recursive: true }); @@ -59,13 +70,27 @@ exports.run = async ({ check, workspace }) => { ["native", "ny.checkNativeProject", "ny.buildNativeProject", "ny.runNativeProject"] ]) { await check(`${language}: ${target} project check, build and run tasks`, async () => { - assert.equal(await vscode.commands.executeCommand(checkCommand), 0); - assert.equal(await vscode.commands.executeCommand(buildCommand), 0); - assert.equal(await vscode.commands.executeCommand(runCommand), 0); + const phases = []; + for (const command of [checkCommand, buildCommand, runCommand]) { + const start = Date.now(); + const dispatch = taskEvidence?.dispatch(command); + console.log(`Starting ${language} ${target} project command: ${command}`); + const exitCode = await vscode.commands.executeCommand(command); + if (dispatch) taskEvidence.complete(dispatch, exitCode); + assert.equal(exitCode, 0, command); + const milliseconds = Date.now() - start; + phases.push({ command, milliseconds }); + console.log(`Completed ${language} ${target} project command: ${command} (${milliseconds} ms)`); + } const output = path.join(directory, "build", "hello" + (target === "vm" ? ".nil" : process.platform === "win32" ? ".exe" : "")); assert.ok((await fs.stat(output)).size > 0); - return { target, output }; - }); + return { target, output, phases }; + // Darwin fingerprints the complete dyld backing files in independent + // preparation/verification phases. This group executes three real tools; + // retain those checks with a bounded aggregate budget and phase timings. + // Intel hosted native builds measured 144 seconds before the separate + // run command, which verifies the same dependency closure again. + }, process.platform === "darwin" ? (target === "native" ? 300_000 : 180_000) : 45_000); } await check(`${language}: semantic rules and project warning policy agree with project checks`, async () => { const warning = language === "ny" ? 'import nil.std.text\nfn main() -> i32\n try_char_at("abc", 0)\n return 0\n println("unreachable")\nend fn\n' : @@ -105,7 +130,7 @@ exports.run = async ({ check, workspace }) => { await vscode.window.showTextDocument(document); await eventually(() => vscode.commands.executeCommand("ny.analyzeCurrentFile"), "current compiler check"); } - const errors = findings(depUri).filter(item => item.severity === vscode.DiagnosticSeverity.Error); + const errors = (await importedFindings(depUri)).filter(item => item.severity === vscode.DiagnosticSeverity.Error); assert.equal(errors.length, 1, "Two entry reports should not duplicate a dependency finding."); assert.equal(errors[0].range.start.line, 3, "UTF-8 and CRLF offsets must map the imported source exactly."); const depDocument = await vscode.workspace.openTextDocument(depUri); @@ -113,17 +138,17 @@ exports.run = async ({ check, workspace }) => { await replace(depDocument, broken.replace('"bad"', "1")); await vscode.window.showTextDocument(entries[0]); await eventually(() => vscode.commands.executeCommand("ny.analyzeCurrentFile"), "current compiler check"); - await eventually(() => findings(depUri).length === 0, "dirty import diagnostic removal"); + await eventually(async () => (await importedFindings(depUri)).length === 0, "dirty import diagnostic removal"); assert.equal(await fs.readFile(dependency, "utf8"), broken, "Analysis must not save imported buffers."); assert.equal(await depDocument.save(), true); - await eventually(() => entries.every(document => findings(document.uri).length === 0) && findings(depUri).length === 0, "saved import diagnostics refreshed"); + await eventually(async () => entries.every(document => findings(document.uri).length === 0) && (await importedFindings(depUri)).length === 0, "saved import diagnostics refreshed"); // A disk-only edit must invalidate entries even when the import is closed. await vscode.window.showTextDocument(depDocument); await vscode.commands.executeCommand("workbench.action.closeActiveEditor"); await fs.writeFile(dependency, broken); - await eventually(() => findings(depUri).some(item => item.range.start.line === 3), "external import change refreshed Problems"); + await eventually(async () => (await importedFindings(depUri)).some(item => item.range.start.line === 3), "external import change refreshed Problems"); await fs.writeFile(dependency, broken.replace('"bad"', "1")); - await eventually(() => findings(depUri).length === 0, "external import repair cleared Problems"); + await eventually(async () => (await importedFindings(depUri)).length === 0, "external import repair cleared Problems"); return { importedLine: 4, duplicateCount: errors.length }; }); }; diff --git a/vscode/test/run-host.cjs b/vscode/test/run-host.cjs index daac7d10..7547180f 100644 --- a/vscode/test/run-host.cjs +++ b/vscode/test/run-host.cjs @@ -9,13 +9,19 @@ const { resolveEditor, hostTimeout, writeIsolatedProfileSettings, validateReport const extensionRoot = path.resolve(__dirname, ".."); const repository = path.resolve(extensionRoot, ".."); -const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "nil vscode host café 中文-")); +// macOS stores its editor IPC socket below the profile. Its ordinary temporary +// directory (or a deep CI TMPDIR) can exceed the Unix socket path limit before +// the editor starts. Keep this owned fixture short, retaining Unicode paths. +const temporary = fs.mkdtempSync(path.join(process.platform === "darwin" ? "/tmp" : os.tmpdir(), "nil vscode host café 中文-")); const workspace = path.join(temporary, "workspace"); const profile = path.join(temporary, "profile"); const reportDirectory = path.join(repository, "build", "vscode-host-smoke"); fs.mkdirSync(path.join(workspace, ".vscode"), { recursive: true }); fs.mkdirSync(reportDirectory, { recursive: true }); const runId = randomUUID(); +fs.writeFileSync(path.join(reportDirectory, `${runId}-launch.json`), JSON.stringify({ + runId, temporary, workspace, profile, platform: process.platform +}, null, 2)); const report = path.join(reportDirectory, `${runId}.json`); const tools = path.resolve(process.env.NIL_VSCODE_TOOL_DIRECTORY || process.env.NY_VSCODE_TOOL_DIRECTORY || path.join(repository, "build", "cmake", "bin")); const executable = resolveEditor(process.env.VSCODE_EXECUTABLE); diff --git a/vscode/test/semantic-checks.cjs b/vscode/test/semantic-checks.cjs index a886e552..a858519b 100644 --- a/vscode/test/semantic-checks.cjs +++ b/vscode/test/semantic-checks.cjs @@ -2,7 +2,26 @@ const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const path = require("node:path"); const vscode = require("vscode"); -const { createFileAlias } = require("./compiler-fixture-support.cjs"); +const { createFileAlias, sameExistingFile } = require("./compiler-fixture-support.cjs"); + +function hasTab(document) { + const expected = document.uri.toString(); + return vscode.window.tabGroups.all.some(group => group.tabs.some(tab => { + if (tab.input instanceof vscode.TabInputText) return tab.input.uri.toString() === expected; + if (tab.input instanceof vscode.TabInputTextDiff) { + return tab.input.original.toString() === expected || tab.input.modified.toString() === expected; + } + return false; + })); +} +function targetUriMatches(uri, document) { + if (uri.toString() === document.uri.toString()) return true; + // Captured editor inputs must retain their authoritative URI, even when an + // open tab is clean. A clean cached model with no tab is only a disk source; + // the real compiler returns its canonical physical path (/private/tmp on Mac). + return uri.scheme === "file" && document.uri.scheme === "file" && + !document.isDirty && !hasTab(document) && sameExistingFile(uri.fsPath, document.uri.fsPath); +} const pause = ms => new Promise(resolve => setTimeout(resolve, ms)); async function eventually(operation, description) { @@ -53,7 +72,7 @@ async function expectDefinition(document, useContext, useToken, target, declarat const [result] = results; assert.ok(result.originSelectionRange && result.targetSelectionRange && result.targetRange, "Semantic navigation must return a LocationLink with exact original-source selections."); - assert.equal(result.targetUri.toString(), target.uri.toString()); + assert.ok(targetUriMatches(result.targetUri, target), "Definition must retain an editor input URI or name the exact uncaptured disk file."); assert.ok(result.originSelectionRange.isEqual(origin), "Origin must select the actual source spelling."); assert.ok(result.targetSelectionRange.isEqual(selection), "Target must select the resolved declaration token."); assert.ok(result.targetRange.isEqual(selection), "Target range must not widen to an unrelated statement."); @@ -89,9 +108,14 @@ async function expectReferences(document, context, token, occurrences) { }, `references of ${context}`); // A nonempty wrong result must fail immediately, including duplicate // lexical LSP guesses or a partial set presented as complete references. - assert.deepEqual(results.map(item => locationKey(item.uri, item.range)).sort(), - occurrences.map(([source, sourceContext, sourceToken = token]) => - locationKey(source.uri, rangeIn(source, sourceContext, sourceToken))).sort()); + const remaining = occurrences.map(([source, sourceContext, sourceToken = token]) => + ({ source, range: rangeIn(source, sourceContext, sourceToken) })); + assert.equal(results.length, remaining.length, "References must include every certified occurrence exactly once."); + for (const result of results) { + const index = remaining.findIndex(expected => targetUriMatches(result.uri, expected.source) && result.range.isEqual(expected.range)); + assert.notEqual(index, -1, `Unexpected or duplicate reference: ${locationKey(result.uri, result.range)}`); + remaining.splice(index, 1); + } return results; } async function rename(document, context, token, newName) { @@ -191,6 +215,11 @@ exports.run = async ({ check, workspace }) => { const header = dependencyLanguage === "ny" ? `fn ${declared}(` : `public ${declared} receives`; await analyze(entry); await expectDefinition(entry, use, use, dependency, header, declared); + await vscode.window.showTextDocument(dependency, { preview: false }); + await analyze(entry); + const cleanOpen = await expectDefinition(entry, use, use, dependency, header, declared); + assert.equal(dependency.isDirty, false); + assert.equal(cleanOpen.targetUri.toString(), dependency.uri.toString(), "Even a clean open import must retain its editor URI."); const comment = dependencyLanguage === "ny" ? "# é😀 dirty declaration\r\n\r\n" : "!!! é😀 dirty declaration\r\n\r\n"; const dirtyText = comment + depText.replace(/\n/g, "\r\n"); await replace(dependency, dirtyText); diff --git a/vscode/test/suite.cjs b/vscode/test/suite.cjs index b77a41c2..aad813f3 100644 --- a/vscode/test/suite.cjs +++ b/vscode/test/suite.cjs @@ -6,6 +6,7 @@ const { promisify } = require("node:util"); const vscode = require("vscode"); const executeFile = promisify(execFile); const { sameFile } = require("./host-support.cjs"); +const { createTaskEvidence } = require("./task-evidence.cjs"); const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); async function bounded(promise, description, timeout = 25_000) { @@ -41,15 +42,19 @@ exports.run = async function run() { const checks = []; const runId = process.env.NIL_VSCODE_TEST_RUN_ID; assert.ok(runId, "Isolated host run identity is required."); - const report = { status: "RUNNING", runId, vscode: vscode.version, platform: process.platform, workspace, checks, skippedChecks: [] }; - const check = async (name, operation) => { + const report = { status: "RUNNING", runId, vscode: vscode.version, platform: process.platform, workspace, checks, skippedChecks: [], + fullSuiteCompleted: false, completedModules: [] }; + const taskEvidence = createTaskEvidence(); + report.taskLifecycle = taskEvidence.report; + const check = async (name, operation, timeout = 45_000) => { const start = Date.now(); - const detail = await bounded(operation(), name, 45_000); + const detail = await bounded(operation(), name, timeout); checks.push({ name, status: "PASS", milliseconds: Date.now() - start, ...(detail || {}) }); console.log(`PASS ${name}`); }; const runModule = async name => { - await require(`./${name}.cjs`).run({ check, workspace }); + await require(`./${name}.cjs`).run({ check, workspace, taskEvidence }); + report.completedModules.push(name); }; const events = []; report.tasks = events; @@ -66,10 +71,16 @@ exports.run = async function run() { })().catch(error => { capture.error = String(error); }); }); const taskSubscription = vscode.tasks.onDidEndTaskProcess((event) => { + taskEvidence.event("process-end", event.execution, { exitCode: event.exitCode }); const execution = event.execution.task.execution; events.push({ task: event.execution.task.definition.task, source: event.execution.task.definition.source, exitCode: event.exitCode, command: execution?.command || execution?.process, args: execution?.args }); }); + const lifecycleSubscriptions = [ + vscode.tasks.onDidStartTask(event => taskEvidence.event("task-start", event.execution)), + vscode.tasks.onDidStartTaskProcess(event => taskEvidence.event("process-start", event.execution, { processId: event.processId })), + vscode.tasks.onDidEndTask(event => taskEvidence.event("task-end", event.execution)) + ]; try { assert.equal(vscode.workspace.isTrusted, true); const extension = vscode.extensions.getExtension("exec-software.ny-language-support"); @@ -275,8 +286,16 @@ exports.run = async function run() { await eventually(() => events.length > start, "rejected compiler task"); assert.equal(events[start].task, "compile"); assert.notEqual(events[start].exitCode, 0); - const diagnostics = await eventually(() => { - const items = vscode.languages.getDiagnostics(invalid.uri).filter((item) => item.source === "nilcompiler"); + const diagnostics = await eventually(async () => { + // Compiler diagnostics use physical source paths. On macOS /tmp and + // /private/tmp identify the same file but have different URI spellings. + // Require the correct file identity and compiler source in Problems. + const items = []; + for (const [uri, entries] of vscode.languages.getDiagnostics()) { + if (uri.scheme === "file" && await sameFile(uri.fsPath, invalid.uri.fsPath)) { + items.push(...entries.filter((item) => item.source === "nilcompiler")); + } + } return items.length && items; }, "Matrix compiler problem matcher"); assert.ok(diagnostics.some((item) => item.range.start.line === 2 && /Type mismatch/.test(item.message))); @@ -370,15 +389,22 @@ exports.run = async function run() { for (const module of ["analyzer-checks", "project-checks", "workspace-checks", "semantic-checks", "completion-checks", "member-signature-checks", "language-values-checks", "memory-profile-checks", "niltest-checks"]) await runModule(module); + report.fullSuiteCompleted = true; report.status = "PASS"; } catch (error) { report.status = "FAIL"; report.error = error.stack || String(error); + try { + const directory = path.join(path.dirname(reportFile), `${runId}-task-diagnostics`); + await fs.mkdir(directory, { recursive: true }); + await taskEvidence.failure(report.error, vscode.tasks.taskExecutions, workspace, directory); + } catch (diagnosticError) { report.taskDiagnosticError = String(diagnosticError); } report.diagnostics = vscode.languages.getDiagnostics().map(([uri, items]) => ({ uri: uri.toString(), items: items.map(item => ({ source: item.source, message: item.message, line: item.range.start.line })) })); throw error; } finally { taskSubscription.dispose(); + lifecycleSubscriptions.forEach(subscription => subscription.dispose()); terminalSubscription?.dispose(); await fs.writeFile(reportFile, JSON.stringify(report, null, 2) + "\n"); } diff --git a/vscode/test/task-evidence.cjs b/vscode/test/task-evidence.cjs new file mode 100644 index 00000000..b741dc88 --- /dev/null +++ b/vscode/test/task-evidence.cjs @@ -0,0 +1,79 @@ +// Test-only lifecycle evidence. This never starts, stops or retries a NIL task. +const path = require("node:path"); +const fs = require("node:fs/promises"); +const { execFile } = require("node:child_process"); +const { promisify } = require("node:util"); +const execute = promisify(execFile); + +async function inspectProcess(task, workspace, output, run = execute) { + const evidence = { processId: task.processId, expectedCommand: task.command, expectedArgs: task.args }; + // These diagnostics run only on Darwin. Keep their path contract explicit + // so the same mocked controls also exercise it on Windows. + if (!Number.isSafeInteger(task.processId) || task.processId <= 0 || !path.posix.isAbsolute(task.command || "") || + !task.args?.some(value => typeof value === "string" && value.startsWith(workspace + path.posix.sep))) { + return { ...evidence, sampled: false, reason: "No owned workspace task process declaration" }; + } + const options = { timeout: 2500, maxBuffer: 64 * 1024, encoding: "utf8" }; + try { + evidence.metrics = (await run("/bin/ps", ["-ww", "-p", String(task.processId), "-o", + "pid=,ppid=,pgid=,time=,%cpu=,stat=,etime="], options)).stdout; + evidence.command = (await run("/bin/ps", ["-ww", "-p", String(task.processId), "-o", "command="], options)).stdout.trim(); + // A task PID may already have exited. Reject a PID whose observed command + // no longer matches this run's executable and private workspace arguments. + // The command observation and subsequent sample are not atomic. + if (!evidence.command.startsWith(task.command + " ") || + !evidence.command.includes(workspace + path.posix.sep) || !task.args.every(value => evidence.command.includes(value))) { + return { ...evidence, sampled: false, reason: "Observed process does not match the owned task" }; + } + try { await fs.access(output); return { ...evidence, sampled: false, reason: "Sample path already exists" }; } + catch (error) { if (error.code !== "ENOENT") throw error; } + const sample = await run("/usr/bin/sample", [String(task.processId), "1", "1", "-file", output], + { timeout: 6000, maxBuffer: 256 * 1024, encoding: "utf8" }); + return { ...evidence, sampled: true, sample: path.basename(output), stdout: sample.stdout, stderr: sample.stderr }; + } catch (error) { + return { ...evidence, sampled: false, error: String(error), stdout: error.stdout, stderr: error.stderr }; + } +} + +function createTaskEvidence() { + const events = [], tasks = [], dispatches = [], identities = new Map(); + const stamp = () => ({ at: new Date().toISOString(), milliseconds: Date.now() }); + const identify = execution => { + let task = identities.get(execution); + if (!task) { + const process = execution.task.execution; + task = { id: `task-${tasks.length + 1}`, definition: { ...execution.task.definition }, + command: process?.process || process?.command, args: process?.args, ended: false }; + identities.set(execution, task); tasks.push(task); + } + return task; + }; + const event = (kind, execution, extra = {}) => { + const task = identify(execution); + events.push({ kind, id: task.id, ...stamp(), ...extra }); + if (kind === "process-start") task.processId = extra.processId; + if (kind === "process-end") { task.processEnded = true; task.exitCode = extra.exitCode; } + if (kind === "task-end") task.ended = true; + }; + return { + report: { events, tasks, dispatches, failureSnapshots: [] }, + event, + dispatch(command) { + const row = { command, ...stamp(), completed: false }; dispatches.push(row); return row; + }, + complete(row, exitCode) { row.completed = true; row.exitCode = exitCode; row.elapsedMilliseconds = Date.now() - row.milliseconds; }, + async failure(reason, activeExecutions, workspace, directory, platform = process.platform, inspect = inspectProcess) { + const active = activeExecutions.map(identify); + const snapshot = { reason, ...stamp(), active: active.map(task => task.id), processes: [] }; + this.report.failureSnapshots.push(snapshot); + if (platform !== "darwin") return; + // Bounded to the three first observed active processes, never enumerate + // unrelated processes or diagnose a task from a completed execution. + for (const task of active.filter(task => !task.ended && !task.processEnded && task.processId).slice(0, 3)) { + snapshot.processes.push(await inspect(task, workspace, path.join(directory, `${task.id}-sample.txt`))); + } + } + }; +} + +module.exports = { createTaskEvidence, inspectProcess }; diff --git a/vscode/test/task-evidence.test.cjs b/vscode/test/task-evidence.test.cjs new file mode 100644 index 00000000..0b280127 --- /dev/null +++ b/vscode/test/task-evidence.test.cjs @@ -0,0 +1,70 @@ +const assert = require("node:assert/strict"); +const test = require("node:test"); +const { createTaskEvidence, inspectProcess } = require("./task-evidence.cjs"); +const fixture = () => ({ task: { definition: { task: "project-build" }, execution: { + process: "/SDK λ/nilbuild", args: ["build", "--project", "/private/work/project/nil.project.json"] } } }); + +test("task evidence associates dispatch, lifecycle and process events without changing completion", async () => { + const evidence = createTaskEvidence(), execution = fixture(); + const dispatch = evidence.dispatch("ny.buildProject"); + evidence.event("task-start", execution); + evidence.event("process-start", execution, { processId: 123 }); + evidence.event("process-end", execution, { exitCode: 0 }); + evidence.event("task-end", execution); evidence.complete(dispatch, 0); + assert.equal(evidence.report.tasks.length, 1); + assert.equal(evidence.report.tasks[0].processId, 123); + assert.equal(evidence.report.tasks[0].ended, true); + assert.ok(evidence.report.events.every(row => row.id === "task-1")); + assert.equal(dispatch.completed, true); + await evidence.failure("late", [execution], "/private/work", "/tmp", "darwin", async () => assert.fail("ended process sampled")); + assert.deepEqual(evidence.report.failureSnapshots[0].processes, []); +}); + +test("failure evidence distinguishes dispatch-only, task-created and child-started", async () => { + const evidence = createTaskEvidence(), unstarted = fixture(), started = fixture(); + evidence.dispatch("ny.buildProject"); + evidence.event("task-start", unstarted); + evidence.event("process-start", started, { processId: 321 }); + const inspected = []; + await evidence.failure("timeout", [unstarted, started], "/private/work", "/tmp", "darwin", async task => { + inspected.push(task.processId); return { processId: task.processId }; + }); + assert.deepEqual(inspected, [321]); + assert.deepEqual(evidence.report.failureSnapshots[0].active, ["task-1", "task-2"]); + assert.equal(evidence.report.dispatches[0].completed, false); +}); + +test("a process-end suppresses sampling before the task-end notification arrives", async () => { + const evidence = createTaskEvidence(), execution = fixture(); + evidence.event("process-start", execution, { processId: 123 }); + evidence.event("process-end", execution, { exitCode: 0 }); + assert.equal(evidence.report.tasks[0].ended, false); + assert.equal(evidence.report.tasks[0].processEnded, true); + await evidence.failure("waiting for task end", [execution], "/private/work", "/tmp", "darwin", + async () => assert.fail("completed child sampled")); + assert.deepEqual(evidence.report.failureSnapshots[0].processes, []); +}); + +test("diagnostics refuse an unowned PID or mismatched process before sampling", async () => { + let calls = 0; + const task = { processId: 123, command: "/SDK λ/nilbuild", args: ["build", "/private/work/project/file"] }; + const run = async (command, args) => { + calls++; assert.equal(command, "/bin/ps"); + return { stdout: args.at(-1) === "command=" ? "/other/tool /private/work/project/file" : "123 1 1 0:00 0 S 1:00" }; + }; + assert.equal((await inspectProcess(task, "/private/work", "/uncreated/output", run)).sampled, false); + assert.equal(calls, 2); + await inspectProcess({ ...task, processId: -1 }, "/private/work", "/uncreated/output", async () => assert.fail()); + await inspectProcess({ ...task, args: ["/other/work/file"] }, "/private/work", "/uncreated/output", async () => assert.fail()); +}); + +test("matching owned task receives only bounded read-only process diagnostics", async () => { + const task = { processId: 123, command: "/SDK λ/nilbuild", args: ["build", "/private/work/project/file"] }; + const calls = []; + const result = await inspectProcess(task, "/private/work", "/no-existing-task-evidence-123/sample.txt", async (command, args, options) => { + calls.push({ command, args }); assert.ok(options.timeout > 0 && options.timeout <= 6000); + return { stdout: args.at(-1) === "command=" ? "/SDK λ/nilbuild build /private/work/project/file" : "metrics", stderr: "" }; + }); + assert.equal(result.sampled, true); + assert.deepEqual(calls.at(-1), { command: "/usr/bin/sample", args: ["123", "1", "1", "-file", "/no-existing-task-evidence-123/sample.txt"] }); +}); diff --git a/vscode/test/workspace-checks.cjs b/vscode/test/workspace-checks.cjs index f6592840..0f5fca76 100644 --- a/vscode/test/workspace-checks.cjs +++ b/vscode/test/workspace-checks.cjs @@ -139,15 +139,15 @@ process.stdin.on('data', chunk => input += chunk); process.stdin.on('end', () => { const request = JSON.parse(input); const dependencies = request.sources.filter(item => sameExistingFile(item.path, ${JSON.stringify(dependencyPath)})); - const dependency = dependencies[0]; + const dependency = dependencies[0] && canonicalSnapshot(dependencies[0]); const ours = sameExistingFile(primary, ${JSON.stringify(entryPath)}); fs.appendFileSync(${JSON.stringify(log)}, JSON.stringify({pid:process.pid,primary,text:dependency?.text,count:dependencies.length}) + '\\n'); const stale = dependency?.text.includes('WAIT'); const diagnostics = ours && dependency ? [{code:stale?'STALE':'IMPORT_CURRENT',severity:'warning',message:'Imported finding', - path:${JSON.stringify(dependencyPath)},start:6,length:2,hint:'',fix:{start:6,length:2,replacement:'',description:'Do not offer imported edits'}}] : []; + path:dependency.path,start:6,length:2,hint:'',fix:{start:6,length:2,replacement:'',description:'Do not offer imported edits'}}] : []; setTimeout(() => process.stdout.write(JSON.stringify({schema:'nil.analysis.v1',source:primary, language:primary.endsWith('.ny')?'ny':'matrix',success:true,reflection:{},diagnostics, - sources:ours && dependency ? [{path:${JSON.stringify(dependencyPath)},text:dependency.text}] : []})), ours && stale ? 5000 : 0); + sources:ours && dependency ? [dependency] : []})), ours && stale ? 5000 : 0); }); `); try {