@@ -71,8 +71,11 @@ int32_t WasmBinaryWriter::writeU32LEBPlaceholder() {
7171 return ret;
7272}
7373
74- void WasmBinaryWriter::writeResizableLimits (Address initial, Address maximum, bool hasMaximum) {
75- uint32_t flags = hasMaximum ? 1 : 0 ;
74+ void WasmBinaryWriter::writeResizableLimits (Address initial, Address maximum,
75+ bool hasMaximum, bool shared) {
76+ uint32_t flags =
77+ (hasMaximum ? (uint32_t ) BinaryConsts::HasMaximum : 0U ) |
78+ (shared ? (uint32_t ) BinaryConsts::IsShared : 0U );
7679 o << U32LEB (flags);
7780 o << U32LEB (initial);
7881 if (hasMaximum) {
@@ -113,7 +116,8 @@ void WasmBinaryWriter::writeMemory() {
113116 if (debug) std::cerr << " == writeMemory" << std::endl;
114117 auto start = startSection (BinaryConsts::Section::Memory);
115118 o << U32LEB (1 ); // Define 1 memory
116- writeResizableLimits (wasm->memory .initial , wasm->memory .max , wasm->memory .max != Memory::kMaxSize );
119+ writeResizableLimits (wasm->memory .initial , wasm->memory .max ,
120+ wasm->memory .max != Memory::kMaxSize , wasm->memory .shared );
117121 finishSection (start);
118122}
119123
@@ -161,11 +165,12 @@ void WasmBinaryWriter::writeImports() {
161165 case ExternalKind::Function: o << U32LEB (getFunctionTypeIndex (import ->functionType )); break ;
162166 case ExternalKind::Table: {
163167 o << S32LEB (BinaryConsts::EncodedType::AnyFunc);
164- writeResizableLimits (wasm->table .initial , wasm->table .max , wasm->table .max != Table::kMaxSize );
168+ writeResizableLimits (wasm->table .initial , wasm->table .max , wasm->table .max != Table::kMaxSize , /* shared= */ false );
165169 break ;
166170 }
167171 case ExternalKind::Memory: {
168- writeResizableLimits (wasm->memory .initial , wasm->memory .max , wasm->memory .max != Memory::kMaxSize );
172+ writeResizableLimits (wasm->memory .initial , wasm->memory .max ,
173+ wasm->memory .max != Memory::kMaxSize , wasm->memory .shared );
169174 break ;
170175 }
171176 case ExternalKind::Global:
@@ -368,7 +373,7 @@ void WasmBinaryWriter::writeFunctionTableDeclaration() {
368373 auto start = startSection (BinaryConsts::Section::Table);
369374 o << U32LEB (1 ); // Declare 1 table.
370375 o << S32LEB (BinaryConsts::EncodedType::AnyFunc);
371- writeResizableLimits (wasm->table .initial , wasm->table .max , wasm->table .max != Table::kMaxSize );
376+ writeResizableLimits (wasm->table .initial , wasm->table .max , wasm->table .max != Table::kMaxSize , /* shared= */ false );
372377 finishSection (start);
373378}
374379
@@ -1237,7 +1242,7 @@ void WasmBinaryBuilder::readMemory() {
12371242 throw ParseException (" Memory cannot be both imported and defined" );
12381243 }
12391244 wasm.memory .exists = true ;
1240- getResizableLimits (wasm.memory .initial , wasm.memory .max , Memory::kMaxSize );
1245+ getResizableLimits (wasm.memory .initial , wasm.memory .max , wasm. memory . shared , Memory::kMaxSize );
12411246}
12421247
12431248void WasmBinaryBuilder::readSignatures () {
@@ -1284,10 +1289,13 @@ Name WasmBinaryBuilder::getFunctionIndexName(Index i) {
12841289 }
12851290}
12861291
1287- void WasmBinaryBuilder::getResizableLimits (Address& initial, Address& max, Address defaultIfNoMax) {
1292+ void WasmBinaryBuilder::getResizableLimits (Address& initial, Address& max, bool &shared, Address defaultIfNoMax) {
12881293 auto flags = getU32LEB ();
12891294 initial = getU32LEB ();
1290- bool hasMax = flags & 0x1 ;
1295+ bool hasMax = flags & BinaryConsts::HasMaximum;
1296+ bool isShared = flags & BinaryConsts::IsShared;
1297+ if (isShared && !hasMax) throw ParseException (" shared memory must have max size" );
1298+ shared = isShared;
12911299 if (hasMax) max = getU32LEB ();
12921300 else max = defaultIfNoMax;
12931301}
@@ -1320,13 +1328,15 @@ void WasmBinaryBuilder::readImports() {
13201328 if (elementType != BinaryConsts::EncodedType::AnyFunc) throw ParseException (" Imported table type is not AnyFunc" );
13211329 wasm.table .exists = true ;
13221330 wasm.table .imported = true ;
1323- getResizableLimits (wasm.table .initial , wasm.table .max , Table::kMaxSize );
1331+ bool is_shared;
1332+ getResizableLimits (wasm.table .initial , wasm.table .max , is_shared, Table::kMaxSize );
1333+ if (is_shared) throw ParseException (" Tables may not be shared" );
13241334 break ;
13251335 }
13261336 case ExternalKind::Memory: {
13271337 wasm.memory .exists = true ;
13281338 wasm.memory .imported = true ;
1329- getResizableLimits (wasm.memory .initial , wasm.memory .max , Memory::kMaxSize );
1339+ getResizableLimits (wasm.memory .initial , wasm.memory .max , wasm. memory . shared , Memory::kMaxSize );
13301340 break ;
13311341 }
13321342 case ExternalKind::Global: {
@@ -1759,7 +1769,9 @@ void WasmBinaryBuilder::readFunctionTableDeclaration() {
17591769 wasm.table .exists = true ;
17601770 auto elemType = getS32LEB ();
17611771 if (elemType != BinaryConsts::EncodedType::AnyFunc) throw ParseException (" ElementType must be AnyFunc in MVP" );
1762- getResizableLimits (wasm.table .initial , wasm.table .max , Table::kMaxSize );
1772+ bool is_shared;
1773+ getResizableLimits (wasm.table .initial , wasm.table .max , is_shared, Table::kMaxSize );
1774+ if (is_shared) throw ParseException (" Tables may not be shared" );
17631775}
17641776
17651777void WasmBinaryBuilder::readTableElements () {
0 commit comments