@@ -477,6 +477,7 @@ struct WasmValidator : public PostWalker<WasmValidator> {
477
477
void visitSelect (Select* curr) {
478
478
shouldBeUnequal (curr->ifTrue ->type , none, curr, " select left must be valid" );
479
479
shouldBeUnequal (curr->ifFalse ->type , none, curr, " select right must be valid" );
480
+ shouldBeTrue (curr->condition ->type == unreachable || curr->condition ->type == i32 , curr, " select condition must be valid" );
480
481
}
481
482
482
483
void visitDrop (Drop* curr) {
@@ -565,8 +566,19 @@ struct WasmValidator : public PostWalker<WasmValidator> {
565
566
labelNames.clear ();
566
567
}
567
568
568
- bool isConstant (Expression* curr) {
569
- return curr->is <Const>() || curr->is <GetGlobal>();
569
+ bool checkOffset (Expression* curr, Address add, Address max) {
570
+ if (curr->is <GetGlobal>()) return true ;
571
+ auto * c = curr->dynCast <Const>();
572
+ if (!c) return false ;
573
+ uint64_t raw = c->value .getInteger ();
574
+ if (raw > std::numeric_limits<Address::address_t >::max ()) {
575
+ return false ;
576
+ }
577
+ if (raw + uint64_t (add) > std::numeric_limits<Address::address_t >::max ()) {
578
+ return false ;
579
+ }
580
+ Address offset = raw;
581
+ return offset + add <= max;
570
582
}
571
583
572
584
void visitMemory (Memory *curr) {
@@ -575,7 +587,7 @@ struct WasmValidator : public PostWalker<WasmValidator> {
575
587
Index mustBeGreaterOrEqual = 0 ;
576
588
for (auto & segment : curr->segments ) {
577
589
if (!shouldBeEqual (segment.offset ->type , i32 , segment.offset , " segment offset should be i32" )) continue ;
578
- shouldBeTrue (isConstant (segment.offset ), segment.offset , " segment offset should be constant " );
590
+ shouldBeTrue (checkOffset (segment.offset , segment. data . size ( ), getModule ()-> memory . initial * Memory:: kPageSize ), segment.offset , " segment offset should be reasonable " );
579
591
Index size = segment.data .size ();
580
592
shouldBeTrue (size <= curr->initial * Memory::kPageSize , segment.data .size (), " segment size should fit in memory" );
581
593
if (segment.offset ->is <Const>()) {
@@ -590,7 +602,7 @@ struct WasmValidator : public PostWalker<WasmValidator> {
590
602
void visitTable (Table* curr) {
591
603
for (auto & segment : curr->segments ) {
592
604
shouldBeEqual (segment.offset ->type , i32 , segment.offset , " segment offset should be i32" );
593
- shouldBeTrue (isConstant (segment.offset ), segment.offset , " segment offset should be constant " );
605
+ shouldBeTrue (checkOffset (segment.offset , segment. data . size ( ), getModule ()-> table . initial * Table:: kPageSize ), segment.offset , " segment offset should be reasonable " );
594
606
}
595
607
}
596
608
void visitModule (Module *curr) {
0 commit comments