Skip to content

Commit 3710141

Browse files
authored
threads: add shared globals (#1480)
* threads: add `shared` globals This change allows wasm-tools to encode and decode `shared` global types as a part of the [shared-everything-threads] proposal. It includes some initial fuzzing support, though it all should be turned off by default. [shared-everything-threads]: https://github.com/WebAssembly/shared-everything-threads * review: remove wit-component TODOs * review: use enclosed parser variable * review: avoid shared-everything-threads in wasm-smith for now * review: emit new 'malformed' message * review: add imports to test * review: add 'missing-features' tests
1 parent 0ab473f commit 3710141

File tree

30 files changed

+179
-24
lines changed

30 files changed

+179
-24
lines changed

crates/fuzz-stats/src/bin/failed-instantiations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl State {
102102
let mut config = wasm_smith::Config::arbitrary(&mut u)?;
103103
config.allow_start_export = false;
104104

105-
// Wasmtime doesn't support this proposal yet.
105+
// Wasmtime doesn't support these proposals yet.
106106
config.gc_enabled = false;
107107

108108
let mut wasm = wasm_smith::Module::new(config, &mut u)?;

crates/wasm-encoder/src/core/globals.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{encode_section, ConstExpr, Encode, Section, SectionId, ValType};
1414
/// GlobalType {
1515
/// val_type: ValType::I32,
1616
/// mutable: false,
17+
/// shared: false,
1718
/// },
1819
/// &ConstExpr::i32_const(42),
1920
/// );
@@ -80,12 +81,21 @@ pub struct GlobalType {
8081
pub val_type: ValType,
8182
/// Whether this global is mutable or not.
8283
pub mutable: bool,
84+
/// Whether this global is shared or not.
85+
pub shared: bool,
8386
}
8487

8588
impl Encode for GlobalType {
8689
fn encode(&self, sink: &mut Vec<u8>) {
8790
self.val_type.encode(sink);
88-
sink.push(self.mutable as u8);
91+
let mut flag = 0;
92+
if self.mutable {
93+
flag |= 0b01;
94+
}
95+
if self.shared {
96+
flag |= 0b10;
97+
}
98+
sink.push(flag);
8999
}
90100
}
91101

@@ -96,6 +106,7 @@ impl TryFrom<wasmparser::GlobalType> for GlobalType {
96106
Ok(GlobalType {
97107
val_type: global_ty.content_type.try_into()?,
98108
mutable: global_ty.mutable,
109+
shared: global_ty.shared,
99110
})
100111
}
101112
}

crates/wasm-mutate/src/mutators/peephole.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ impl PeepholeMutator {
309309
index: _,
310310
tpe: ty,
311311
mutable,
312+
shared,
312313
} => {
313314
let (init, ty) = match ty {
314315
PrimitiveTypeInfo::I32 => {
@@ -329,8 +330,9 @@ impl PeepholeMutator {
329330
_ => unreachable!("Not valid for globals"),
330331
};
331332
let ty = wasm_encoder::GlobalType {
332-
mutable: *mutable,
333333
val_type: ty,
334+
mutable: *mutable,
335+
shared: *shared,
334336
};
335337
// Add to globals
336338
new_global_section.global(ty, &init);

crates/wasm-mutate/src/mutators/peephole/eggsy/encoder/expr2wasm.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub enum ResourceRequest {
2323
tpe: PrimitiveTypeInfo,
2424
/// If its mutable
2525
mutable: bool,
26+
/// If its shared
27+
shared: bool,
2628
},
2729
// TODO add other needed resources here, for example, needed locals, needed
2830
// memory etc. Notice that how this resources are translated to Wasm code,
@@ -363,6 +365,7 @@ pub fn expr2wasm(
363365
index: global_idx as usize,
364366
tpe: PrimitiveTypeInfo::I32,
365367
mutable: true,
368+
shared: false,
366369
};
367370
resources.push(request);
368371

@@ -375,6 +378,7 @@ pub fn expr2wasm(
375378
index: global_idx as usize,
376379
tpe: PrimitiveTypeInfo::I64,
377380
mutable: true,
381+
shared: false,
378382
};
379383
resources.push(request);
380384

@@ -387,6 +391,7 @@ pub fn expr2wasm(
387391
index: global_idx as usize,
388392
tpe: PrimitiveTypeInfo::F32,
389393
mutable: true,
394+
shared: false,
390395
};
391396
resources.push(request);
392397

@@ -399,6 +404,7 @@ pub fn expr2wasm(
399404
index: global_idx as usize,
400405
tpe: PrimitiveTypeInfo::F64,
401406
mutable: true,
407+
shared: false,
402408
};
403409
resources.push(request);
404410

crates/wasm-mutate/src/mutators/translate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ pub fn global_type(
170170
Ok(wasm_encoder::GlobalType {
171171
val_type: t.translate_ty(&ty.content_type)?,
172172
mutable: ty.mutable,
173+
shared: ty.shared,
173174
})
174175
}
175176

crates/wasm-smith/src/component.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ impl ComponentBuilder {
952952
Ok(crate::core::GlobalType {
953953
val_type: self.arbitrary_core_valtype(u)?,
954954
mutable: u.arbitrary()?,
955+
shared: false,
955956
})
956957
}
957958

crates/wasm-smith/src/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,7 @@ impl Module {
14241424
Ok(GlobalType {
14251425
val_type: self.arbitrary_valtype(u)?,
14261426
mutable: u.arbitrary()?,
1427+
shared: false,
14271428
})
14281429
}
14291430

@@ -1756,6 +1757,7 @@ impl Module {
17561757
GlobalType {
17571758
val_type: convert_val_type(&global_type.content_type),
17581759
mutable: global_type.mutable,
1760+
shared: global_type.shared,
17591761
},
17601762
u,
17611763
)?,

crates/wasm-smith/src/core/code_builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ impl CodeBuilderAllocations {
870870
module.globals.push(GlobalType {
871871
val_type: ty,
872872
mutable: true,
873+
shared: false,
873874
});
874875
module.defined_globals.push((global_idx, init));
875876

crates/wasm-smith/src/core/terminate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl Module {
2323
self.globals.push(GlobalType {
2424
val_type: ValType::I32,
2525
mutable: true,
26+
shared: false,
2627
});
2728
self.defined_globals
2829
.push((fuel_global, ConstExpr::i32_const(default_fuel as i32)));

crates/wasmparser/src/readers/core/globals.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ impl<'a> FromReader<'a> for Global<'a> {
3737

3838
impl<'a> FromReader<'a> for GlobalType {
3939
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
40+
let content_type = reader.read()?;
41+
let flags = reader.read_u8()?;
42+
if flags > 0b11 {
43+
bail!(reader.original_position() - 1, "malformed global flags")
44+
}
4045
Ok(GlobalType {
41-
content_type: reader.read()?,
42-
mutable: match reader.read_u8()? {
43-
0x00 => false,
44-
0x01 => true,
45-
_ => bail!(reader.original_position() - 1, "malformed mutability",),
46-
},
46+
content_type,
47+
mutable: (flags & 0b01) > 0,
48+
shared: (flags & 0b10) > 0,
4749
})
4850
}
4951
}

0 commit comments

Comments
 (0)