feat(dev): probe explicit host targets for scalable forms
This commit is contained in:
1 parent
5f7a84c602
commit
2b294e7eed
2 files changed
+125
-6
No files matched your search
@@ -477,7 +477,7 @@ class ScalableProgressTests(unittest.TestCase):
|
||||
self.assertEqual(command[1], 'compile-leaf')
|
||||
self.assertEqual(command[command.index('--guest-profile') + 1], progress.SCALABLE_PROFILE)
|
||||
self.assertEqual(command[command.index('--guest-base') + 1], '0x10000000')
|
||||
self.assertEqual(command[command.index('--cpu-target') + 1], 'baseline')
|
||||
self.assertEqual(command[command.index('--cpu-target') + 1], 'x86_64-baseline')
|
||||
source = Path(command[2])
|
||||
output = Path(command[command.index('--output-dir') + 1])
|
||||
output.mkdir()
|
||||
@@ -1794,5 +1794,102 @@ class CompilerDepInfoGuards(unittest.TestCase):
|
||||
progress.parse_rust_dep_info(text)
|
||||
|
||||
|
||||
class SampleHostTargetTests(unittest.TestCase):
|
||||
def fake_compiler(self, manifest_target=None):
|
||||
def fake(command, **kwargs):
|
||||
target = command[command.index('--cpu-target') + 1]
|
||||
baseline_command = list(command)
|
||||
baseline_command[baseline_command.index('--cpu-target') + 1] = 'x86_64-baseline'
|
||||
result = ScalableProgressTests().fake_compiler()(baseline_command, **kwargs)
|
||||
manifest = json.loads(result.stdout)
|
||||
manifest['target_cpu'] = target if manifest_target is None else manifest_target
|
||||
return subprocess.CompletedProcess(command, 0, json.dumps(manifest), '')
|
||||
return fake
|
||||
|
||||
def probe(self, directory, **kwargs):
|
||||
return progress.compile_probe('cli', progress.RET, directory, mode='scalable',
|
||||
guest_profile=progress.SCALABLE_PROFILE, svl_bytes=16, svcr=3, **kwargs)
|
||||
|
||||
def test_absent_host_target_keeps_canonical_baseline(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
result = self.probe(Path(directory), run=self.fake_compiler())
|
||||
self.assertEqual(result['target_cpu'], 'x86_64-baseline')
|
||||
self.assertEqual(result['execution_profile'], {'id': progress.SCALABLE_PROFILE, 'svl_bytes': 16, 'svcr': 3})
|
||||
|
||||
def test_optional_host_target_is_forwarded_and_exact_manifest_target_retained(self):
|
||||
for target in ('x86_64-baseline+atomic-load16', 'x86_64-baseline+cx16',
|
||||
'x86_64-baseline+atomic-load16+cx16'):
|
||||
with self.subTest(target=target), tempfile.TemporaryDirectory() as directory:
|
||||
result = self.probe(Path(directory), cpu_target=target, run=self.fake_compiler())
|
||||
self.assertEqual(result['target_cpu'], target)
|
||||
self.assertEqual(result['execution_profile']['id'], progress.SCALABLE_PROFILE)
|
||||
|
||||
def test_mismatched_canonical_target_and_alias_native_targets_decline(self):
|
||||
for target in ('x86_64-baseline+cx16', 'baseline', 'native'):
|
||||
with self.subTest(target=target), tempfile.TemporaryDirectory() as directory:
|
||||
with self.assertRaisesRegex(ValueError, 'source/native metadata mismatch'):
|
||||
self.probe(Path(directory), cpu_target=target, run=self.fake_compiler('x86_64-baseline'))
|
||||
|
||||
def test_invalid_transport_values_decline_before_compiler(self):
|
||||
for target in ('', 1, True, [], {}, 'a' * 257, 'bad\nargument', 'bad\x00argument', '\ud800'):
|
||||
with self.subTest(target=target), tempfile.TemporaryDirectory() as directory:
|
||||
with patch.object(progress.subprocess, 'run') as run:
|
||||
with self.assertRaises(ValueError):
|
||||
self.probe(Path(directory), cpu_target=target, run=run)
|
||||
run.assert_not_called()
|
||||
self.assertEqual(list(Path(directory).iterdir()), [])
|
||||
sample = {'word': '0xd65f03c0', 'expected_mnemonics': ['ret'], 'svl_bytes': 16, 'svcr': 3, 'cpu_target': None}
|
||||
with self.assertRaises(ValueError):
|
||||
progress.validate_scalable_sample(sample, ScalableProgressTests().profile())
|
||||
|
||||
def test_target_syntax_and_capability_rejection_stays_with_rust(self):
|
||||
# Mock transport/error propagation; Rust's existing parser remains the
|
||||
# sole authority for unknown, duplicate and reordered suffixes.
|
||||
for target in ('x86_64-baseline+unknown', 'x86_64-baseline+cx16+cx16',
|
||||
'x86_64-baseline+cx16+atomic-load16'):
|
||||
with self.subTest(target=target), tempfile.TemporaryDirectory() as directory:
|
||||
calls = []
|
||||
def rejected(command, **kwargs):
|
||||
calls.append(command)
|
||||
return subprocess.CompletedProcess(command, 1, '', 'Rust target parser rejected target')
|
||||
with self.assertRaisesRegex(ValueError, 'Rust target parser rejected target'):
|
||||
self.probe(Path(directory), cpu_target=target, run=rejected)
|
||||
self.assertEqual(calls[0][calls[0].index('--cpu-target') + 1], target)
|
||||
|
||||
def test_sample_target_routes_to_supported_and_rejected_probes(self):
|
||||
registry = DeclaredScalableProgressTests().registry()
|
||||
row = registry['forms'][1]
|
||||
target = 'x86_64-baseline+atomic-load16'
|
||||
row['smoke_samples'][0]['cpu_target'] = target
|
||||
row['rejected_samples'] = [{'word': '0xd65f03c0', 'reason': 'scope limit', 'svl_bytes': 16,
|
||||
'svcr': 3, 'cpu_target': 'x86_64-baseline+cx16'}]
|
||||
rows = progress.validate_registry(registry)
|
||||
with tempfile.TemporaryDirectory() as directory, patch.object(progress, 'compile_probe', return_value={
|
||||
'compiler_source_sha256': COMPILER}) as probe:
|
||||
progress.smoke_forms('cli', rows, rows, Path(directory), mode='scalable',
|
||||
profile=registry['profile'], execution_profiles=registry['execution_profiles'])
|
||||
targets = [call.kwargs['cpu_target'] for call in probe.call_args_list if 'cpu_target' in call.kwargs]
|
||||
self.assertEqual(targets, [target, 'x86_64-baseline+cx16'])
|
||||
for call in probe.call_args_list:
|
||||
self.assertNotIn('cpu_target', call.kwargs.get('features', []))
|
||||
|
||||
def test_legacy_samples_cannot_silently_ignore_explicit_host_target(self):
|
||||
for collection in ('smoke_samples', 'rejected_samples'):
|
||||
registry = fixtures()
|
||||
if collection == 'smoke_samples':
|
||||
registry['forms'][0][collection][0]['cpu_target'] = 'x86_64-baseline+cx16'
|
||||
else:
|
||||
registry['forms'][0][collection] = [{'cpu_target': 'x86_64-baseline+cx16'}]
|
||||
with self.subTest(collection=collection), self.assertRaisesRegex(ValueError, 'context compiler lane'):
|
||||
progress.validate_registry(registry)
|
||||
|
||||
def test_host_target_is_not_a_guest_profile_declaration(self):
|
||||
profile = {**ScalableProgressTests().profile(), 'cpu_target': 'x86_64-baseline+cx16'}
|
||||
with self.assertRaises(ValueError):
|
||||
progress.validate_scalable_profile(profile)
|
||||
with tempfile.TemporaryDirectory() as directory, self.assertRaisesRegex(ValueError, 'context compiler lane'):
|
||||
progress.compile_probe('cli', progress.RET, Path(directory), cpu_target='x86_64-baseline+cx16')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -32,6 +32,8 @@ SCALABLE_FEATURES = (
|
||||
)
|
||||
SCALABLE_DECLARATION_FIELDS = ('features', 'max_svl_bytes', 'zt0_enabled', 'dit')
|
||||
CONTEXT_LEAF_ABI = 'x86_64-sysv-guest-state-v1-context-direct-ram'
|
||||
BASELINE_CPU_TARGET = 'x86_64-baseline'
|
||||
MAX_CPU_TARGET_LENGTH = 256
|
||||
CONTEXT_ENTRY_PREFIX_BYTES = 13
|
||||
SCALABLE_VECTOR_BYTES = (16, 32, 64, 128, 256)
|
||||
SCALABLE_GUEST_BASE = 0x10000000
|
||||
@@ -278,6 +280,9 @@ def validate_registry(registry):
|
||||
if scalable_profiles is not None else row.get('profile') != profile)):
|
||||
raise ValueError('Form support scope/profile missing')
|
||||
row_contract = scalable_profiles[row['profile']] if scalable_profiles is not None else None
|
||||
if row_contract is None and any('cpu_target' in sample for sample in
|
||||
row.get('smoke_samples', []) + row.get('rejected_samples', [])):
|
||||
raise ValueError('Host target samples require the context compiler lane')
|
||||
if row['status'] != 'unsupported':
|
||||
if not row.get('review') or not row.get('compiler_sources') or not row.get('smoke_samples'):
|
||||
raise ValueError('Support requires reviewed implementation and smoke samples')
|
||||
@@ -416,10 +421,24 @@ def validate_scalable_profiles(default_profile, execution_profiles=None):
|
||||
return profiles
|
||||
|
||||
|
||||
def validate_cpu_target(value):
|
||||
# Rust TargetCpu::FromStr owns syntax and capabilities. Successful probes
|
||||
# must bind the returned canonical TargetCpu::id to this exact argument.
|
||||
if (not isinstance(value, str) or not value or len(value) > MAX_CPU_TARGET_LENGTH
|
||||
or any(ord(char) < 32 or ord(char) == 127 for char in value)):
|
||||
raise ValueError('Host cpu_target must be a bounded nonempty argument')
|
||||
try:
|
||||
value.encode('utf-8')
|
||||
except UnicodeEncodeError:
|
||||
raise ValueError('Host cpu_target must be a UTF-8 argument') from None
|
||||
return value
|
||||
|
||||
|
||||
def validate_scalable_sample(sample, contract):
|
||||
allowed = {'word', 'expected_mnemonics', 'consumed_source_bytes', 'svl_bytes', 'svcr', 'reason'}
|
||||
allowed = {'word', 'expected_mnemonics', 'consumed_source_bytes', 'svl_bytes', 'svcr', 'reason', 'cpu_target'}
|
||||
if set(sample) - allowed:
|
||||
raise ValueError('Samples must use declared fields and select guest features/controls through the row profile')
|
||||
validate_cpu_target(sample.get('cpu_target', BASELINE_CPU_TARGET))
|
||||
return scalable_execution_profile(contract['id'], sample.get('svl_bytes'), sample.get('svcr'),
|
||||
**scalable_declaration(contract))
|
||||
|
||||
@@ -459,16 +478,17 @@ def validate_context_manifest(manifest, source_data, native_bytes, svl_bytes, sv
|
||||
|
||||
def compile_probe(cli, word, directory, expect_failure=None, run=subprocess.run, mode='leaf',
|
||||
expected_source_bytes=4, guest_profile=None, svl_bytes=None, svcr=None,
|
||||
features=None, max_svl_bytes=None, zt0_enabled=None, dit=None):
|
||||
features=None, max_svl_bytes=None, zt0_enabled=None, dit=None, cpu_target=None):
|
||||
if mode not in ('leaf', 'block', 'service', 'scalable'):
|
||||
raise ValueError('Unknown compiler probe mode')
|
||||
if type(expected_source_bytes) is not int or expected_source_bytes not in (4, 8):
|
||||
raise ValueError('Invalid declared compiler probe boundary')
|
||||
if mode == 'scalable':
|
||||
cpu_target = validate_cpu_target(BASELINE_CPU_TARGET if cpu_target is None else cpu_target)
|
||||
execution = scalable_execution_profile(guest_profile, svl_bytes, svcr, features=features,
|
||||
max_svl_bytes=max_svl_bytes, zt0_enabled=zt0_enabled, dit=dit)
|
||||
elif any(value is not None for value in (guest_profile, svl_bytes, svcr, features,
|
||||
max_svl_bytes, zt0_enabled, dit)):
|
||||
max_svl_bytes, zt0_enabled, dit, cpu_target)):
|
||||
raise ValueError('Scalable configuration requires the context compiler lane')
|
||||
source = directory / 'source.arm64'
|
||||
source.write_bytes(struct.pack('<II', word, RET))
|
||||
@@ -479,7 +499,7 @@ def compile_probe(cli, word, directory, expect_failure=None, run=subprocess.run,
|
||||
command.extend(['--guest-base', '0x10000000'])
|
||||
if mode == 'scalable':
|
||||
command.extend(['--guest-profile', guest_profile, '--svl-bytes', str(svl_bytes),
|
||||
'--svcr', str(svcr), '--cpu-target', 'baseline'])
|
||||
'--svcr', str(svcr), '--cpu-target', cpu_target])
|
||||
if guest_profile == DECLARED_SCALABLE_PROFILE:
|
||||
for feature in execution['features']:
|
||||
command.extend(['--guest-feature', feature])
|
||||
@@ -511,7 +531,7 @@ def compile_probe(cli, word, directory, expect_failure=None, run=subprocess.run,
|
||||
raise ValueError('Compiler probe native output identity mismatch')
|
||||
if mode == 'scalable':
|
||||
validate_context_manifest(manifest, source_data, native.stat().st_size, svl_bytes, svcr,
|
||||
guest_profile, **({key: execution[key] for key in SCALABLE_DECLARATION_FIELDS}
|
||||
guest_profile, expected_target_cpu=cpu_target, **({key: execution[key] for key in SCALABLE_DECLARATION_FIELDS}
|
||||
if guest_profile == DECLARED_SCALABLE_PROFILE else {}))
|
||||
fingerprint = manifest.get('compiler_source_sha256')
|
||||
if not isinstance(fingerprint, str) or len(fingerprint) != 64:
|
||||
@@ -561,6 +581,8 @@ def smoke_forms(cli, forms, overrides, directory, mode='leaf', canonical_facts=N
|
||||
configuration = declaration if sample is None else sample
|
||||
result.update(guest_profile=declaration['id'], svl_bytes=configuration['svl_bytes'],
|
||||
svcr=configuration['svcr'], **scalable_declaration(declaration))
|
||||
if 'cpu_target' in configuration:
|
||||
result['cpu_target'] = configuration['cpu_target']
|
||||
return result
|
||||
decoder = Cs(CS_ARCH_ARM64, CS_MODE_ARM)
|
||||
fingerprint = None
|
||||
|
||||
Reference in new issue
Block a user