Skip to content

Commit b936421

Browse files
wip
1 parent 5ca7aa2 commit b936421

File tree

2 files changed

+124
-10
lines changed

2 files changed

+124
-10
lines changed

tests/specification/reports.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::any::Any;
22

3-
use wasm::{RuntimeError, TrapError};
3+
use wasm::{RuntimeError, TrapError, ValidationError};
44

55
use super::test_errors::AssertEqError;
66

@@ -44,6 +44,8 @@ pub enum WastError {
4444
Wast(#[from] wast::Error),
4545
#[error("Runtime error not represented in WAST")]
4646
UnrepresentedRuntimeError,
47+
#[error("Validation error not represented in WAST: {0}")]
48+
UnrepresentedValidationError(ValidationError),
4749
#[error("{0}")]
4850
Io(#[from] std::io::Error),
4951
#[error("Some directive either referenced a non-existing Wasm module by its id or it did not specify an id at all and there was no other module defined prior to this directive.")]
@@ -54,6 +56,11 @@ pub enum WastError {
5456
UnknownGlobalReferenced,
5557
#[error("Module failed to link")]
5658
FailedToLink,
59+
#[error("Got unexpected validation error {actual}, expected {expected}")]
60+
UnexpectedValidationError {
61+
expected: String,
62+
actual: ValidationError,
63+
},
5764
}
5865

5966
impl From<wasm::ValidationError> for WastError {

tests/specification/run.rs

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use wasm::RuntimeError;
2020
use wasm::TableType;
2121
use wasm::TrapError;
2222
use wasm::ValType;
23+
use wasm::ValidationError;
2324
use wast::core::WastArgCore;
2425
use wast::core::WastRetCore;
2526
use wast::{QuoteWat, WastArg, WastDirective, WastRet, Wat};
@@ -53,7 +54,91 @@ 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+
match validation_error {
64+
// _ => Err(WastError::UnrepresentedValidationError(
65+
// validation_error.clone(),
66+
// )),
67+
ValidationError::InvalidMagic => "magic header not detected",
68+
ValidationError::InvalidBinaryFormatVersion => "unknown binary version",
69+
ValidationError::Eof => todo!(),
70+
ValidationError::MalformedUtf8(utf8_error) => Ok("malformed UTF-8 encoding"),
71+
ValidationError::MalformedSectionTypeDiscriminator(_) => todo!(),
72+
ValidationError::MalformedNumTypeDiscriminator(_) => todo!(),
73+
ValidationError::MalformedVecTypeDiscriminator(_) => todo!(),
74+
ValidationError::MalformedFuncTypeDiscriminator(_) => todo!(),
75+
ValidationError::MalformedRefTypeDiscriminator(_) => todo!(),
76+
ValidationError::MalformedValType => todo!(),
77+
ValidationError::MalformedExportDescDiscriminator(_) => todo!(),
78+
ValidationError::MalformedImportDescDiscriminator(_) => todo!(),
79+
ValidationError::MalformedLimitsDiscriminator(_) => todo!(),
80+
ValidationError::MalformedLimitsMinLargerThanMax { min, max } => todo!(),
81+
ValidationError::MalformedMutDiscriminator(_) => todo!(),
82+
ValidationError::MalformedBlockTypeTypeIdx(_) => todo!(),
83+
ValidationError::MalformedVariableLengthInteger => todo!(),
84+
ValidationError::MalformedElemKindDiscriminator(_) => todo!(),
85+
ValidationError::InvalidTypeIdx(_) => todo!(),
86+
ValidationError::InvalidFuncIdx(_) => todo!(),
87+
ValidationError::InvalidTableIdx(_) => todo!(),
88+
ValidationError::InvalidMemIdx(_) => todo!(),
89+
ValidationError::InvalidGlobalIdx(_) => todo!(),
90+
ValidationError::InvalidElemIdx(_) => todo!(),
91+
ValidationError::InvalidDataIdx(_) => todo!(),
92+
ValidationError::InvalidLocalIdx(_) => todo!(),
93+
ValidationError::InvalidLabelIdx(_) => todo!(),
94+
ValidationError::InvalidLaneIdx(_) => todo!(),
95+
ValidationError::SectionOutOfOrder(section_ty) => todo!(),
96+
ValidationError::InvalidCustomSectionLength => todo!(),
97+
ValidationError::ExprMissingEnd => todo!(),
98+
ValidationError::InvalidInstr(_) => todo!(),
99+
ValidationError::InvalidMultiByteInstr(_, _) => todo!(),
100+
ValidationError::EndInvalidValueStack => todo!(),
101+
ValidationError::InvalidValidationStackValType(val_type) => todo!(),
102+
ValidationError::InvalidValidationStackType(validation_stack_entry) => todo!(),
103+
ValidationError::ExpectedAnOperand => todo!(),
104+
ValidationError::MemoryTooLarge => todo!(),
105+
ValidationError::MutationOfConstGlobal => todo!(),
106+
ValidationError::ErroneousAlignment {
107+
alignment,
108+
minimum_required_alignment,
109+
} => todo!(),
110+
ValidationError::ValidationCtrlStackEmpty => todo!(),
111+
ValidationError::ElseWithoutMatchingIf => todo!(),
112+
ValidationError::IfWithoutMatchingElse => todo!(),
113+
ValidationError::MismatchedRefTypesDuringTableInit { table_ty, elem_ty } => todo!(),
114+
ValidationError::MismatchedRefTypesDuringTableCopy {
115+
source_table_ty,
116+
destination_table_ty,
117+
} => todo!(),
118+
ValidationError::MismatchedRefTypesOnValidationStack { expected, actual } => todo!(),
119+
ValidationError::IndirectCallToNonFuncRefTable(ref_type) => todo!(),
120+
ValidationError::ExpectedReferenceTypeOnStack(val_type) => todo!(),
121+
ValidationError::ReferencingAnUnreferencedFunction(func_idx) => todo!(),
122+
ValidationError::InvalidSelectTypeVectorLength(_) => todo!(),
123+
ValidationError::TooManyLocals(_) => todo!(),
124+
ValidationError::DuplicateExportName => todo!(),
125+
ValidationError::UnsupportedMultipleMemoriesProposal => todo!(),
126+
ValidationError::CodeExprHasTrailingInstructions => todo!(),
127+
ValidationError::FunctionAndCodeSectionsHaveDifferentLengths => todo!(),
128+
ValidationError::DataCountAndDataSectionsLengthAreDifferent => todo!(),
129+
ValidationError::InvalidImportType => todo!(),
130+
ValidationError::InvalidStartFunctionSignature => todo!(),
131+
ValidationError::ActiveElementSegmentTypeMismatch => todo!(),
132+
ValidationError::I33IsNegative => todo!(),
133+
ValidationError::MissingDataCountSection => todo!(),
134+
ValidationError::InvalidDataSegmentMode(_) => todo!(),
135+
ValidationError::InvalidElementMode(_) => todo!(),
136+
ValidationError::TooManyFunctions => todo!(),
137+
ValidationError::TooManyTables => todo!(),
138+
ValidationError::TooManyMemories => todo!(),
139+
ValidationError::TooManyGlobals => todo!(),
140+
}
141+
.map(str::to_owned)
57142
}
58143

59144
/// Clear the bytes and runtime instance before calling this function
@@ -280,22 +365,27 @@ fn run_directive<'a>(
280365
wast::WastDirective::AssertMalformed {
281366
span,
282367
module: mut modulee,
283-
message: _,
368+
message,
284369
}
285370
| wast::WastDirective::AssertInvalid {
286371
span,
287372
module: mut modulee,
288-
message: _,
373+
message,
289374
} => {
290375
let line_number = get_linenum(contents, span);
291376
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-
});
377+
378+
// TODO: I think encoding should not produce errors
379+
let bytes = encode(&mut modulee).map_err(|err| {
380+
ScriptError::new(filepath, err, "Failed to encode module", line_number, cmd)
381+
})?;
382+
let bytes = arena.alloc_slice_clone(&bytes);
383+
let result = validate_instantiate(store, bytes, linker, last_instantiated_module);
296384

297385
let maybe_assert_error = match result {
298-
Ok(_module) => Some(WastError::AssertInvalidButValid),
386+
Ok(_) | Err(WastError::FailedToLink | WastError::WasmRuntimeError(_)) => {
387+
Some(WastError::AssertInvalidButValid)
388+
}
299389
Err(panic_err @ WastError::Panic(_)) => {
300390
return Err(ScriptError::new(
301391
filepath,
@@ -305,7 +395,24 @@ fn run_directive<'a>(
305395
cmd,
306396
))
307397
}
308-
Err(_other) => None,
398+
Err(WastError::WasmError(validation_error)) => {
399+
match validation_error_to_wasm_testsuite_string(&validation_error) {
400+
Ok(actual_message) => {
401+
if actual_message == message {
402+
None
403+
} else {
404+
Some(WastError::UnexpectedValidationError {
405+
expected: message.to_owned(),
406+
actual: validation_error,
407+
})
408+
}
409+
}
410+
Err(err) => Some(err),
411+
}
412+
}
413+
Err(other_err) => {
414+
unreachable!("no other errors should be possible here, got: {other_err}")
415+
}
309416
};
310417

311418
Ok(Some(AssertOutcome {

0 commit comments

Comments
 (0)