Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions usvm-ts-fast-check/fast-check-adapter/src/execute-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import {
EntryPointInvocationError,
isNamedCheckFailure,
type NamedCheckFailure,
type ExecutionKind,
loadEntryPoint,
verifySourceHash,
Expand Down Expand Up @@ -461,18 +462,7 @@ function failureDetails(details: RunDetails<[JsConcreteValue[]]>): FastCheckFail
: details.errorInstance;

if (isNamedCheckFailure(error)) {
const description = error.exception === undefined
? describePropertyFailure(error)
: describePropertyFailure(error.exception.value);
return {
kind: 'property',
...description,
checkId: error.checkId,
...(error.exception === undefined ? {
actual: encodeJsValue(error.actual),
expected: encodeJsValue(error.expected),
} : {}),
};
return describeNamedCheckFailure(error);
}

return {
Expand All @@ -481,6 +471,22 @@ function failureDetails(details: RunDetails<[JsConcreteValue[]]>): FastCheckFail
};
}

export function describeNamedCheckFailure(error: NamedCheckFailure): FastCheckFailureDetails {
const description = error.exception === undefined
? describePropertyFailure(error)
: describePropertyFailure(error.exception.value);

return {
kind: 'property',
...description,
checkId: error.checkId,
...(error.exception === undefined ? {
actual: encodeJsValue(error.actual),
expected: encodeJsValue(error.expected),
} : {}),
};
}

export function describePropertyFailure(value: unknown): Pick<FastCheckFailureDetails, 'errorName' | 'message'> {
try {
if (value instanceof Error) {
Expand Down
29 changes: 20 additions & 9 deletions usvm-ts-fast-check/fast-check-adapter/src/external-witness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { AsyncLocalStorage } from 'node:async_hooks';
import { performance } from 'node:perf_hooks';
import fc from 'fast-check';
import { adapterDiagnostic } from './diagnostics.js';
import { EntryPointInvocationError, loadEntryPoint, sourceHashMatches, type LoadedEntryPoint } from './entry-point.js';
import { describePropertyFailure, invokePropertyOnce, validateRequest, type FastCheckFailureDetails } from './execute-property.js';
import { EntryPointInvocationError, isNamedCheckFailure, loadEntryPoint, sourceHashMatches, type LoadedEntryPoint } from './entry-point.js';
import { describeNamedCheckFailure, describePropertyFailure, invokePropertyOnce, validateRequest, type FastCheckFailureDetails } from './execute-property.js';
import { decodeJsValue, encodeJsValue, ProtocolError, protocolError, type JsConcreteValue, type TaggedJsValue } from './js-value.js';
import { buildPropertyArbitrary } from './property-arbitrary.js';
import { stringifyProtocolJson } from './protocol-json.js';
Expand Down Expand Up @@ -97,11 +97,13 @@ export async function executeWitness(value: unknown): Promise<WitnessExecutionSu
const operation = validateOperation((value as Record<string, unknown>).witness, request.manifest.inputs.length);
const identity = { witnessPropertyId: request.manifest.propertyId, witnessTarget: operation.target };
const target = operation.target;
const declaredCheck = request.manifest.assertions?.some((check) => check.id === target.id) === true;
const diagnosticCheck = request.manifest.predicate.checkResultExportName !== undefined && declaredCheck;
const validOriginalCheck = target.role !== 'PROPERTY' || (target.binding === undefined
? (request.manifest.assertions?.length ?? 0) === 0 && target.id === request.manifest.predicate.exportName
: target.binding.pointId === target.id && request.manifest.assertions?.some((check) => check.id === target.id));
? diagnosticCheck || (request.manifest.assertions?.length ?? 0) === 0 && target.id === request.manifest.predicate.exportName
: target.binding.pointId === target.id && declaredCheck);
if (!validOriginalCheck) {
throw protocolError(adapterDiagnostic.protocolRequestInvalid, 'Original named check requires its executable binding', 'witness.target');
throw protocolError(adapterDiagnostic.protocolRequestInvalid, 'Original named check requires a companion or executable binding', 'witness.target');
}
for (const source of operation.sources) {
const matches = await sourceHashMatches(source.module, source.sha256, request.sourceRoots);
Expand Down Expand Up @@ -145,7 +147,7 @@ export async function executeWitness(value: unknown): Promise<WitnessExecutionSu
: await loadEntryPoint(
request.manifest.precondition, request.sourceRoots, 'manifest.precondition', request.tsconfig,
);
const replay = (input: JsConcreteValue[]) => replayOnce(input, operation.target, predicate, precondition);
const replay = (input: JsConcreteValue[]) => replayOnce(input, operation.target, predicate, precondition, diagnosticCheck);

if (operation.mode === 'replay') {
const first = await replay(values);
Expand Down Expand Up @@ -235,6 +237,7 @@ class ExternalValueArbitrary extends fc.Arbitrary<JsConcreteValue[]> {

async function replayOnce(
values: JsConcreteValue[], target: WitnessTarget, predicate: LoadedEntryPoint, precondition: LoadedEntryPoint | undefined,
diagnosticCheck: boolean,
): Promise<WitnessNativeReplay> {
const context: WitnessContext = { events: [], active: false };
const result: WitnessNativeReplay = {
Expand All @@ -251,7 +254,10 @@ async function replayOnce(
result.outcome = holds ? 'HOLDS' : 'FALSE';
}
} catch (error: unknown) {
if (error instanceof EntryPointInvocationError) {
if (isNamedCheckFailure(error)) {
result.outcome = error.exception === undefined ? 'FALSE' : 'THREW';
result.failure = describeNamedCheckFailure(error);
} else if (error instanceof EntryPointInvocationError) {
result.outcome = 'THREW';
result.failure = { kind: 'property', ...describePropertyFailure(error.thrownValue) };
} else {
Expand All @@ -270,8 +276,11 @@ async function replayOnce(
: context.events.find((entry) => sameBinding(entry.binding, target.binding as WitnessBinding));
const propertyFailed = result.outcome === 'FALSE' || result.outcome === 'THREW';
const guardedEvent = event !== undefined && event.guard;
const checkMatched = diagnosticCheck
? result.failure?.checkId === target.id
: result.failure?.checkId === undefined || result.failure.checkId === target.id;
result.targetMatched = result.admission === 'ADMITTED' && (target.role === 'PROPERTY'
? propertyFailed && (target.expectedOutcome === undefined || target.expectedOutcome === result.outcome)
? propertyFailed && checkMatched && (target.expectedOutcome === undefined || target.expectedOutcome === result.outcome)
&& (target.binding === undefined || (guardedEvent && !event.holds))
: guardedEvent && (target.role === 'COVERAGE' || !event.holds));

Expand All @@ -280,7 +289,9 @@ async function replayOnce(

export function sameTarget(actual: WitnessNativeReplay, expected: WitnessNativeReplay): boolean {
return actual.admission === 'ADMITTED' && actual.targetMatched && actual.outcome === expected.outcome
&& actual.failure?.errorName === expected.failure?.errorName;
&& actual.failure?.checkId === expected.failure?.checkId
&& actual.failure?.errorName === expected.failure?.errorName
&& actual.failure?.message === expected.failure?.message;
}

function sameBinding(left: WitnessBinding, right: WitnessBinding): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,12 @@ internal class FastCheckWitnessCoordinator(

val target = candidate.target
val binding = target.binding
val declaredCheck = property.assertions.any { assertion -> assertion.id == target.id }
val diagnosticCheck = property.predicate.checkResultExportName != null && declaredCheck
val validCheck = target.role != WitnessRole.PROPERTY || if (binding == null) {
property.assertions.isEmpty() && target.id == property.predicate.exportName
diagnosticCheck || property.assertions.isEmpty() && target.id == property.predicate.exportName
} else {
binding.pointId == target.id && property.assertions.any { assertion -> assertion.id == target.id }
binding.pointId == target.id && declaredCheck
}
if (!validCheck) return WitnessReplayStatus.UNSUPPORTED_REPLAY

Expand Down Expand Up @@ -423,7 +425,9 @@ internal class FastCheckWitnessCoordinator(

fun sameTarget(actual: WitnessNativeReplay, expected: WitnessNativeReplay): Boolean =
actual.admission == WitnessAdmission.ADMITTED && actual.targetMatched &&
actual.outcome == expected.outcome && actual.failure?.errorName == expected.failure?.errorName
actual.outcome == expected.outcome && actual.failure?.checkId == expected.failure?.checkId &&
actual.failure?.errorName == expected.failure?.errorName &&
actual.failure?.message == expected.failure?.message

fun classify(replay: WitnessNativeReplay, candidate: ExternalWitnessCandidate): WitnessReplayStatus = when {
replay.diagnostic == FastCheckDiagnosticCode.ENTRY_POINT_SOURCE_HASH_MISMATCH ->
Expand Down
Loading
Loading