@@ -20,6 +20,7 @@ use wasm::RuntimeError;
2020use wasm:: TableType ;
2121use wasm:: TrapError ;
2222use wasm:: ValType ;
23+ use wasm:: ValidationError ;
2324use wast:: core:: WastArgCore ;
2425use wast:: core:: WastRetCore ;
2526use wast:: { QuoteWat , WastArg , WastDirective , WastRet , Wat } ;
@@ -53,7 +54,98 @@ pub fn error_to_wasm_testsuite_string(runtime_error: &RuntimeError) -> Result<St
5354 RuntimeError :: HostFunctionSignatureMismatch => Ok ( "host function signature mismatch" ) ,
5455 _ => Err ( WastError :: UnrepresentedRuntimeError ) ,
5556 }
56- . map ( ToOwned :: to_owned)
57+ . map ( str:: to_owned)
58+ }
59+
60+ pub fn validation_error_to_wasm_testsuite_string (
61+ validation_error : & ValidationError ,
62+ ) -> Result < String , WastError > {
63+ std:: panic:: catch_unwind ( || {
64+ match validation_error {
65+ // _ => Err(WastError::UnrepresentedValidationError(
66+ // validation_error.clone(),
67+ // )),
68+ ValidationError :: InvalidMagic => Ok ( "magic header not detected" ) ,
69+ ValidationError :: InvalidBinaryFormatVersion => Ok ( "unknown binary version" ) ,
70+ ValidationError :: Eof => todo ! ( ) ,
71+ ValidationError :: MalformedUtf8 ( _) => Ok ( "malformed UTF-8 encoding" ) ,
72+ ValidationError :: MalformedSectionTypeDiscriminator ( _) => Ok ( "malformed section id" ) ,
73+ ValidationError :: MalformedNumTypeDiscriminator ( _) => Ok ( "malformed number type" ) ,
74+ ValidationError :: MalformedVecTypeDiscriminator ( _) => Ok ( "malformed vector type" ) ,
75+ ValidationError :: MalformedFuncTypeDiscriminator ( _) => Ok ( "malformed function type" ) ,
76+ ValidationError :: MalformedRefTypeDiscriminator ( _) => Ok ( "malformed reference type" ) ,
77+ ValidationError :: MalformedValType => todo ! ( ) ,
78+ ValidationError :: MalformedExportDescDiscriminator ( _) => Ok ( "malformed export kind" ) ,
79+ ValidationError :: MalformedImportDescDiscriminator ( _) => Ok ( "malformed import kind" ) ,
80+ ValidationError :: MalformedLimitsDiscriminator ( _) => todo ! ( ) ,
81+ ValidationError :: MalformedLimitsMinLargerThanMax { .. } => todo ! ( ) ,
82+ ValidationError :: MalformedMutDiscriminator ( _) => Ok ( "malformed mutability" ) ,
83+ ValidationError :: MalformedBlockTypeTypeIdx ( _) => todo ! ( ) ,
84+ ValidationError :: MalformedVariableLengthInteger => todo ! ( ) ,
85+ ValidationError :: MalformedElemKindDiscriminator ( _) => Ok ( "malformed element kind" ) ,
86+ ValidationError :: InvalidTypeIdx ( _) => todo ! ( ) ,
87+ ValidationError :: InvalidFuncIdx ( _) => todo ! ( ) ,
88+ ValidationError :: InvalidTableIdx ( _) => todo ! ( ) ,
89+ ValidationError :: InvalidMemIdx ( _) => todo ! ( ) ,
90+ ValidationError :: InvalidGlobalIdx ( _) => todo ! ( ) ,
91+ ValidationError :: InvalidElemIdx ( _) => todo ! ( ) ,
92+ ValidationError :: InvalidDataIdx ( _) => todo ! ( ) ,
93+ ValidationError :: InvalidLocalIdx ( _) => todo ! ( ) ,
94+ ValidationError :: InvalidLabelIdx ( _) => todo ! ( ) ,
95+ ValidationError :: InvalidLaneIdx ( _) => todo ! ( ) ,
96+ ValidationError :: SectionOutOfOrder ( section_ty) => todo ! ( ) ,
97+ ValidationError :: UnexpectedContentAfterLastSection => {
98+ Ok ( "unexpected content after last section" )
99+ }
100+ ValidationError :: InvalidCustomSectionLength => todo ! ( ) ,
101+ ValidationError :: ExprMissingEnd => todo ! ( ) ,
102+ ValidationError :: InvalidInstr ( _) => todo ! ( ) ,
103+ ValidationError :: InvalidMultiByteInstr ( _, _) => todo ! ( ) ,
104+ ValidationError :: EndInvalidValueStack => todo ! ( ) ,
105+ ValidationError :: InvalidValidationStackValType ( val_type) => todo ! ( ) ,
106+ ValidationError :: InvalidValidationStackType ( validation_stack_entry) => todo ! ( ) ,
107+ ValidationError :: ExpectedAnOperand => todo ! ( ) ,
108+ ValidationError :: MemoryTooLarge => todo ! ( ) ,
109+ ValidationError :: MutationOfConstGlobal => todo ! ( ) ,
110+ ValidationError :: ErroneousAlignment {
111+ alignment,
112+ minimum_required_alignment,
113+ } => todo ! ( ) ,
114+ ValidationError :: ValidationCtrlStackEmpty => todo ! ( ) ,
115+ ValidationError :: ElseWithoutMatchingIf => todo ! ( ) ,
116+ ValidationError :: IfWithoutMatchingElse => todo ! ( ) ,
117+ ValidationError :: MismatchedRefTypesDuringTableInit { table_ty, elem_ty } => todo ! ( ) ,
118+ ValidationError :: MismatchedRefTypesDuringTableCopy {
119+ source_table_ty,
120+ destination_table_ty,
121+ } => todo ! ( ) ,
122+ ValidationError :: MismatchedRefTypesOnValidationStack { expected, actual } => todo ! ( ) ,
123+ ValidationError :: IndirectCallToNonFuncRefTable ( ref_type) => todo ! ( ) ,
124+ ValidationError :: ExpectedReferenceTypeOnStack ( val_type) => todo ! ( ) ,
125+ ValidationError :: ReferencingAnUnreferencedFunction ( func_idx) => todo ! ( ) ,
126+ ValidationError :: InvalidSelectTypeVectorLength ( _) => todo ! ( ) ,
127+ ValidationError :: TooManyLocals ( _) => todo ! ( ) ,
128+ ValidationError :: DuplicateExportName => todo ! ( ) ,
129+ ValidationError :: UnsupportedMultipleMemoriesProposal => todo ! ( ) ,
130+ ValidationError :: CodeExprHasTrailingInstructions => todo ! ( ) ,
131+ ValidationError :: FunctionAndCodeSectionsHaveDifferentLengths => todo ! ( ) ,
132+ ValidationError :: DataCountAndDataSectionsLengthAreDifferent => todo ! ( ) ,
133+ ValidationError :: InvalidImportType => todo ! ( ) ,
134+ ValidationError :: InvalidStartFunctionSignature => todo ! ( ) ,
135+ ValidationError :: ActiveElementSegmentTypeMismatch => todo ! ( ) ,
136+ ValidationError :: I33IsNegative => todo ! ( ) ,
137+ ValidationError :: MissingDataCountSection => todo ! ( ) ,
138+ ValidationError :: InvalidDataSegmentMode ( _) => Ok ( "malformed data segement kind" ) ,
139+ ValidationError :: InvalidElementMode ( _) => Ok ( "malformed elements segment kind" ) ,
140+ ValidationError :: TooManyFunctions => todo ! ( ) ,
141+ ValidationError :: TooManyTables => todo ! ( ) ,
142+ ValidationError :: TooManyMemories => todo ! ( ) ,
143+ ValidationError :: TooManyGlobals => todo ! ( ) ,
144+ }
145+ . map ( str:: to_owned)
146+ } )
147+ . map_err ( WastError :: Panic )
148+ . flatten ( )
57149}
58150
59151/// Clear the bytes and runtime instance before calling this function
@@ -280,22 +372,27 @@ fn run_directive<'a>(
280372 wast:: WastDirective :: AssertMalformed {
281373 span,
282374 module : mut modulee,
283- message : _ ,
375+ message,
284376 }
285377 | wast:: WastDirective :: AssertInvalid {
286378 span,
287379 module : mut modulee,
288- message : _ ,
380+ message,
289381 } => {
290382 let line_number = get_linenum ( contents, span) ;
291383 let cmd = get_command ( contents, span) ;
292- let result = encode ( & mut modulee) . and_then ( |bytes| {
293- let bytes = arena. alloc_slice_clone ( & bytes) ;
294- validate_instantiate ( store, bytes, linker, last_instantiated_module)
295- } ) ;
384+
385+ // TODO: I think encoding should not produce errors
386+ let bytes = encode ( & mut modulee) . map_err ( |err| {
387+ ScriptError :: new ( filepath, err, "Failed to encode module" , line_number, cmd)
388+ } ) ?;
389+ let bytes = arena. alloc_slice_clone ( & bytes) ;
390+ let result = validate_instantiate ( store, bytes, linker, last_instantiated_module) ;
296391
297392 let maybe_assert_error = match result {
298- Ok ( _module) => Some ( WastError :: AssertInvalidButValid ) ,
393+ Ok ( _) | Err ( WastError :: FailedToLink | WastError :: WasmRuntimeError ( _) ) => {
394+ Some ( WastError :: AssertInvalidButValid )
395+ }
299396 Err ( panic_err @ WastError :: Panic ( _) ) => {
300397 return Err ( ScriptError :: new (
301398 filepath,
@@ -305,7 +402,24 @@ fn run_directive<'a>(
305402 cmd,
306403 ) )
307404 }
308- Err ( _other) => None ,
405+ Err ( WastError :: WasmError ( validation_error) ) => {
406+ match validation_error_to_wasm_testsuite_string ( & validation_error) {
407+ Ok ( actual_message) => {
408+ if actual_message == message {
409+ None
410+ } else {
411+ Some ( WastError :: UnexpectedValidationError {
412+ expected : message. to_owned ( ) ,
413+ actual : validation_error,
414+ } )
415+ }
416+ }
417+ Err ( err) => Some ( err) ,
418+ }
419+ }
420+ Err ( other_err) => {
421+ unreachable ! ( "no other errors should be possible here, got: {other_err}" )
422+ }
309423 } ;
310424
311425 Ok ( Some ( AssertOutcome {
0 commit comments