@@ -954,7 +954,10 @@ void WasmBinaryWriter::visitStore(Store *curr) {
954954void WasmBinaryWriter::visitAtomicRMW (AtomicRMW *curr) {
955955 if (debug) std::cerr << " zz node: AtomicRMW" << std::endl;
956956 recurse (curr->ptr );
957+ // stop if the rest isn't reachable anyhow
958+ if (curr->ptr ->type == unreachable) return ;
957959 recurse (curr->value );
960+ if (curr->value ->type == unreachable) return ;
958961
959962 if (curr->type == unreachable) {
960963 // don't even emit it; we don't know the right type
@@ -1005,8 +1008,12 @@ void WasmBinaryWriter::visitAtomicRMW(AtomicRMW *curr) {
10051008void WasmBinaryWriter::visitAtomicCmpxchg (AtomicCmpxchg *curr) {
10061009 if (debug) std::cerr << " zz node: AtomicCmpxchg" << std::endl;
10071010 recurse (curr->ptr );
1011+ // stop if the rest isn't reachable anyhow
1012+ if (curr->ptr ->type == unreachable) return ;
10081013 recurse (curr->expected );
1014+ if (curr->expected ->type == unreachable) return ;
10091015 recurse (curr->replacement );
1016+ if (curr->replacement ->type == unreachable) return ;
10101017
10111018 if (curr->type == unreachable) {
10121019 // don't even emit it; we don't know the right type
@@ -1041,23 +1048,39 @@ void WasmBinaryWriter::visitAtomicCmpxchg(AtomicCmpxchg *curr) {
10411048void WasmBinaryWriter::visitAtomicWait (AtomicWait *curr) {
10421049 if (debug) std::cerr << " zz node: AtomicWait" << std::endl;
10431050 recurse (curr->ptr );
1051+ // stop if the rest isn't reachable anyhow
1052+ if (curr->ptr ->type == unreachable) return ;
10441053 recurse (curr->expected );
1054+ if (curr->expected ->type == unreachable) return ;
10451055 recurse (curr->timeout );
1056+ if (curr->timeout ->type == unreachable) return ;
10461057
10471058 o << int8_t (BinaryConsts::AtomicPrefix);
10481059 switch (curr->expectedType ) {
1049- case i32 : o << int8_t (BinaryConsts::I32AtomicWait); break ;
1050- case i64 : o << int8_t (BinaryConsts::I64AtomicWait); break ;
1060+ case i32 : {
1061+ o << int8_t (BinaryConsts::I32AtomicWait);
1062+ emitMemoryAccess (4 , 4 , 0 );
1063+ break ;
1064+ }
1065+ case i64 : {
1066+ o << int8_t (BinaryConsts::I64AtomicWait);
1067+ emitMemoryAccess (8 , 8 , 0 );
1068+ break ;
1069+ }
10511070 default : WASM_UNREACHABLE ();
10521071 }
10531072}
10541073
10551074void WasmBinaryWriter::visitAtomicWake (AtomicWake *curr) {
10561075 if (debug) std::cerr << " zz node: AtomicWake" << std::endl;
10571076 recurse (curr->ptr );
1077+ // stop if the rest isn't reachable anyhow
1078+ if (curr->ptr ->type == unreachable) return ;
10581079 recurse (curr->wakeCount );
1080+ if (curr->wakeCount ->type == unreachable) return ;
10591081
10601082 o << int8_t (BinaryConsts::AtomicPrefix) << int8_t (BinaryConsts::AtomicWake);
1083+ emitMemoryAccess (4 , 4 , 0 );
10611084}
10621085
10631086void WasmBinaryWriter::visitConst (Const *curr) {
@@ -2555,7 +2578,7 @@ void WasmBinaryBuilder::visitSetGlobal(SetGlobal *curr) {
25552578 curr->finalize ();
25562579}
25572580
2558- void WasmBinaryBuilder::readMemoryAccess (Address& alignment, size_t bytes, Address& offset) {
2581+ void WasmBinaryBuilder::readMemoryAccess (Address& alignment, Address& offset) {
25592582 auto rawAlignment = getU32LEB ();
25602583 if (rawAlignment > 4 ) throw ParseException (" Alignment must be of a reasonable size" );
25612584 alignment = Pow2 (rawAlignment);
@@ -2599,7 +2622,7 @@ bool WasmBinaryBuilder::maybeVisitLoad(Expression*& out, uint8_t code, bool isAt
25992622 }
26002623
26012624 curr->isAtomic = isAtomic;
2602- readMemoryAccess (curr->align , curr->bytes , curr-> offset );
2625+ readMemoryAccess (curr->align , curr->offset );
26032626 curr->ptr = popNonVoidExpression ();
26042627 curr->finalize ();
26052628 out = curr;
@@ -2636,7 +2659,7 @@ bool WasmBinaryBuilder::maybeVisitStore(Expression*& out, uint8_t code, bool isA
26362659
26372660 curr->isAtomic = isAtomic;
26382661 if (debug) std::cerr << " zz node: Store" << std::endl;
2639- readMemoryAccess (curr->align , curr->bytes , curr-> offset );
2662+ readMemoryAccess (curr->align , curr->offset );
26402663 curr->value = popNonVoidExpression ();
26412664 curr->ptr = popNonVoidExpression ();
26422665 curr->finalize ();
@@ -2679,7 +2702,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicRMW(Expression*& out, uint8_t code) {
26792702
26802703 if (debug) std::cerr << " zz node: AtomicRMW" << std::endl;
26812704 Address readAlign;
2682- readMemoryAccess (readAlign, curr->bytes , curr-> offset );
2705+ readMemoryAccess (readAlign, curr->offset );
26832706 if (readAlign != curr->bytes ) throw ParseException (" Align of AtomicRMW must match size" );
26842707 curr->value = popNonVoidExpression ();
26852708 curr->ptr = popNonVoidExpression ();
@@ -2710,7 +2733,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicCmpxchg(Expression*& out, uint8_t code)
27102733
27112734 if (debug) std::cerr << " zz node: AtomicCmpxchg" << std::endl;
27122735 Address readAlign;
2713- readMemoryAccess (readAlign, curr->bytes , curr-> offset );
2736+ readMemoryAccess (readAlign, curr->offset );
27142737 if (readAlign != curr->bytes ) throw ParseException (" Align of AtomicCpxchg must match size" );
27152738 curr->replacement = popNonVoidExpression ();
27162739 curr->expected = popNonVoidExpression ();
@@ -2734,6 +2757,9 @@ bool WasmBinaryBuilder::maybeVisitAtomicWait(Expression*& out, uint8_t code) {
27342757 curr->timeout = popNonVoidExpression ();
27352758 curr->expected = popNonVoidExpression ();
27362759 curr->ptr = popNonVoidExpression ();
2760+ Address readAlign;
2761+ readMemoryAccess (readAlign, curr->offset );
2762+ if (readAlign != getWasmTypeSize (curr->expectedType )) throw ParseException (" Align of AtomicWait must match size" );
27372763 curr->finalize ();
27382764 out = curr;
27392765 return true ;
@@ -2747,6 +2773,9 @@ bool WasmBinaryBuilder::maybeVisitAtomicWake(Expression*& out, uint8_t code) {
27472773 curr->type = i32 ;
27482774 curr->wakeCount = popNonVoidExpression ();
27492775 curr->ptr = popNonVoidExpression ();
2776+ Address readAlign;
2777+ readMemoryAccess (readAlign, curr->offset );
2778+ if (readAlign != getWasmTypeSize (curr->type )) throw ParseException (" Align of AtomicWake must match size" );
27502779 curr->finalize ();
27512780 out = curr;
27522781 return true ;
0 commit comments