| 1 |
use crate::config::Config; |
| 2 |
use arbitrary::{Arbitrary, Result, Unstructured}; |
| 3 |
use indexmap::{IndexMap, IndexSet}; |
| 4 |
use semver::Version; |
| 5 |
use std::collections::HashSet; |
| 6 |
use std::collections::hash_map::{Entry, HashMap}; |
| 7 |
use std::collections::hash_set::Intersection; |
| 8 |
use std::fmt::Write; |
| 9 |
use std::hash::RandomState; |
| 10 |
use std::mem; |
| 11 |
use std::rc::Rc; |
| 12 |
use std::str; |
| 13 |
use wit_parser::*; |
| 14 |
|
| 15 |
pub struct Generator { |
| 16 |
config: Config, |
| 17 |
packages: Packages, |
| 18 |
next_interface_id: u32, |
| 19 |
} |
| 20 |
|
| 21 |
#[derive(PartialEq, Eq, Hash)] |
| 22 |
pub struct PackageWorldKey { |
| 23 |
package_name: String, |
| 24 |
world_name: String, |
| 25 |
} |
| 26 |
|
| 27 |
struct InterfaceGenerator<'a> { |
| 28 |
generator: &'a mut Generator, |
| 29 |
file: &'a mut File, |
| 30 |
unique_names: HashSet<String>, |
| 31 |
types_in_interface: Vec<Type>, |
| 32 |
package_name: &'a str, |
| 33 |
version: Option<Version>, |
| 34 |
} |
| 35 |
|
| 36 |
#[derive(Clone)] |
| 37 |
struct Type { |
| 38 |
name: String, |
| 39 |
size: usize, |
| 40 |
is_resource: bool, |
| 41 |
} |
| 42 |
|
| 43 |
#[derive(Default)] |
| 44 |
struct Packages { |
| 45 |
list: Vec<Package>, |
| 46 |
packages_with_interfaces: Vec<usize>, |
| 47 |
packages_with_worlds: Vec<usize>, |
| 48 |
package_unique_names: IndexMap<PackageWorldKey, HashSet<String>>, |
| 49 |
} |
| 50 |
|
| 51 |
impl Packages { |
| 52 |
fn add_name(&mut self, package_name: String, world_name: String, name: String) { |
| 53 |
let key = PackageWorldKey { |
| 54 |
package_name, |
| 55 |
world_name, |
| 56 |
}; |
| 57 |
let world_names = self |
| 58 |
.package_unique_names |
| 59 |
.entry(key) |
| 60 |
.or_insert_with(HashSet::new); |
| 61 |
world_names.insert(name); |
| 62 |
} |
| 63 |
|
| 64 |
fn contains_name(&self, package_name: String, world_name: String, name: &str) -> bool { |
| 65 |
let key = PackageWorldKey { |
| 66 |
package_name, |
| 67 |
world_name, |
| 68 |
}; |
| 69 |
if let Some(world_names) = self.package_unique_names.get(&key) { |
| 70 |
return world_names.contains(name); |
| 71 |
} |
| 72 |
false |
| 73 |
} |
| 74 |
|
| 75 |
fn intersect( |
| 76 |
&self, |
| 77 |
current_world: PackageWorldKey, |
| 78 |
include_world: PackageWorldKey, |
| 79 |
) -> Option<Intersection<'_, String, RandomState>> { |
| 80 |
let current_world_names = self.package_unique_names.get(¤t_world); |
| 81 |
let include_world_names = self.package_unique_names.get(&include_world); |
| 82 |
|
| 83 |
if let (Some(current_world_names), Some(include_world_names)) = |
| 84 |
(current_world_names, include_world_names) |
| 85 |
{ |
| 86 |
let intersection = current_world_names.intersection(include_world_names); |
| 87 |
if intersection.clone().count() > 0 { |
| 88 |
return Some(intersection); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return None; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
pub struct Package { |
| 97 |
pub name: PackageName, |
| 98 |
pub sources: SourceMap, |
| 99 |
file: File, |
| 100 |
} |
| 101 |
|
| 102 |
#[derive(Clone, Debug)] |
| 103 |
pub struct PackageName { |
| 104 |
pub namespace: String, |
| 105 |
pub name: String, |
| 106 |
pub version: Option<Version>, |
| 107 |
} |
| 108 |
|
| 109 |
impl Generator { |
| 110 |
pub fn new(config: Config) -> Generator { |
| 111 |
Generator { |
| 112 |
config, |
| 113 |
packages: Default::default(), |
| 114 |
next_interface_id: 0, |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
pub fn generate(&mut self, u: &mut Unstructured<'_>) -> Result<Vec<Package>> { |
| 119 |
let mut names = HashSet::new(); |
| 120 |
while self.packages.list.len() < self.config.max_packages |
| 121 |
&& (self.packages.list.is_empty() || u.arbitrary()?) |
| 122 |
{ |
| 123 |
let pkg = self.gen_package(u, &mut names)?; |
| 124 |
let i = self.packages.list.len(); |
| 125 |
if pkg.file.interfaces.len() > 0 { |
| 126 |
self.packages.packages_with_interfaces.push(i); |
| 127 |
} |
| 128 |
if pkg.file.worlds.len() > 0 { |
| 129 |
self.packages.packages_with_worlds.push(i); |
| 130 |
} |
| 131 |
self.packages.list.push(pkg); |
| 132 |
} |
| 133 |
Ok(mem::take(&mut self.packages.list)) |
| 134 |
} |
| 135 |
|
| 136 |
fn gen_package( |
| 137 |
&mut self, |
| 138 |
u: &mut Unstructured<'_>, |
| 139 |
names: &mut HashSet<String>, |
| 140 |
) -> Result<Package> { |
| 141 |
let namespace = gen_unique_name(u, names)?; |
| 142 |
let package_name = gen_unique_name(u, names)?; |
| 143 |
|
| 144 |
let version = if u.arbitrary()? { |
| 145 |
Some(gen_version(u)?) |
| 146 |
} else { |
| 147 |
None |
| 148 |
}; |
| 149 |
let mut ret = Package { |
| 150 |
name: PackageName { |
| 151 |
namespace, |
| 152 |
name: package_name.clone(), |
| 153 |
version: version.clone(), |
| 154 |
}, |
| 155 |
file: File::default(), |
| 156 |
sources: SourceMap::new(), |
| 157 |
}; |
| 158 |
|
| 159 |
#[derive(Arbitrary, Clone)] |
| 160 |
enum Generate { |
| 161 |
Interface, |
| 162 |
Use, |
| 163 |
World, |
| 164 |
Done, |
| 165 |
} |
| 166 |
|
| 167 |
let mut items = 0; |
| 168 |
let mut empty = true; |
| 169 |
let mut files = vec![File::default()]; |
| 170 |
let mut package_names = HashSet::new(); |
| 171 |
log::debug!("===================== new package ===================="); |
| 172 |
while items < self.config.max_pkg_items { |
| 173 |
items += 1; |
| 174 |
let max = if files.len() < self.config.max_files_per_package { |
| 175 |
files.len() + 1 |
| 176 |
} else { |
| 177 |
files.len() |
| 178 |
}; |
| 179 |
let i = u.int_in_range(0..=max)?; |
| 180 |
let file = match files.get_mut(i) { |
| 181 |
Some(file) => file, |
| 182 |
None => { |
| 183 |
files.push(ret.file.clone()); |
| 184 |
files.last_mut().unwrap() |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
// Only generate Use/Done if we've already generated a world or interface. This ensures |
| 189 |
// that we never generate empty packages, which aren't representable. |
| 190 |
let generate = if empty { |
| 191 |
u.choose(&[Generate::World, Generate::Interface])?.clone() |
| 192 |
} else { |
| 193 |
u.arbitrary()? |
| 194 |
}; |
| 195 |
|
| 196 |
match generate { |
| 197 |
Generate::World => { |
| 198 |
let world_name = |
| 199 |
file.gen_unique_package_name(u, &mut package_names, DefinitionKind::World)?; |
| 200 |
log::debug!("new world `{world_name}` in {i}"); |
| 201 |
let world = |
| 202 |
self.gen_world(u, &world_name, file, &package_name, version.clone())?; |
| 203 |
file.items.push(world); |
| 204 |
|
| 205 |
// Insert the world at the package and file level, asserting |
| 206 |
// uniqueness. |
| 207 |
assert!(ret.file.worlds.insert(world_name.clone())); |
| 208 |
assert!(file.worlds.insert(world_name.clone())); |
| 209 |
let prev = ret.file.namespace.insert( |
| 210 |
world_name.clone(), |
| 211 |
(DefinitionLevel::Package, DefinitionKind::World), |
| 212 |
); |
| 213 |
assert!(prev.is_none()); |
| 214 |
|
| 215 |
// Insert the definition into all other files as well. |
| 216 |
for file in files.iter_mut() { |
| 217 |
if file.insert_definition(&world_name, DefinitionKind::World) { |
| 218 |
assert!(file.worlds.insert(world_name.clone())); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
empty = false; |
| 223 |
} |
| 224 |
Generate::Interface => { |
| 225 |
let name = file.gen_unique_package_name( |
| 226 |
u, |
| 227 |
&mut package_names, |
| 228 |
DefinitionKind::Interface, |
| 229 |
)?; |
| 230 |
log::debug!("new interface `{name}` in {i}"); |
| 231 |
let id = self.next_interface_id; |
| 232 |
self.next_interface_id += 1; |
| 233 |
let (src, types) = |
| 234 |
self.gen_interface(u, Some(&name), file, &package_name, None, None)?; |
| 235 |
file.items.push(src); |
| 236 |
if types.is_empty() { |
| 237 |
continue; |
| 238 |
} |
| 239 |
let interface = FileInterface { |
| 240 |
name, |
| 241 |
id, |
| 242 |
types: Rc::new(types), |
| 243 |
}; |
| 244 |
|
| 245 |
// This interface is defined at the package level, and it |
| 246 |
// must be unique. |
| 247 |
let prev = ret |
| 248 |
.file |
| 249 |
.interfaces |
| 250 |
.insert(interface.name.clone(), interface.clone()); |
| 251 |
assert!(prev.is_none()); |
| 252 |
let prev = ret.file.namespace.insert( |
| 253 |
interface.name.clone(), |
| 254 |
(DefinitionLevel::Package, DefinitionKind::Interface), |
| 255 |
); |
| 256 |
assert!(prev.is_none()); |
| 257 |
|
| 258 |
// This is also defined at the file level, and it must be |
| 259 |
// unique here too. |
| 260 |
let prev = file |
| 261 |
.interfaces |
| 262 |
.insert(interface.name.clone(), interface.clone()); |
| 263 |
assert!(prev.is_none()); |
| 264 |
|
| 265 |
// Insert the definition into all other files as well. |
| 266 |
for file in files.iter_mut() { |
| 267 |
if file.insert_definition(&interface.name, DefinitionKind::Interface) { |
| 268 |
let prev = file |
| 269 |
.interfaces |
| 270 |
.insert(interface.name.clone(), interface.clone()); |
| 271 |
assert!(prev.is_none()); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
empty = false; |
| 276 |
} |
| 277 |
Generate::Use => { |
| 278 |
let mut piece = String::new(); |
| 279 |
piece.push_str("use "); |
| 280 |
let (name, id, types) = |
| 281 |
match self.gen_interface_path(u, &mut ret.file, &mut piece)? { |
| 282 |
Some(i) => i, |
| 283 |
None => continue, |
| 284 |
}; |
| 285 |
let name = name.to_string(); |
| 286 |
let types = types.clone(); |
| 287 |
// If this interface's name already exist within this `file` |
| 288 |
// then this must be renamed with `as`. If the name exists |
| 289 |
// only at the package level then it's ok to replace it with |
| 290 |
// something else. |
| 291 |
// |
| 292 |
// If the name doesn't exist then use the fuzz input to |
| 293 |
// determine whether a rename should happen. |
| 294 |
let name = |
| 295 |
if matches!(file.namespace.get(&name), Some((DefinitionLevel::File, _))) |
| 296 |
|| u.arbitrary()? |
| 297 |
{ |
| 298 |
let name = file.gen_unique_file_name(u, DefinitionKind::Interface)?; |
| 299 |
piece.push_str(" as %"); |
| 300 |
piece.push_str(&name); |
| 301 |
name |
| 302 |
} else { |
| 303 |
file.namespace.insert( |
| 304 |
name.clone(), |
| 305 |
(DefinitionLevel::File, DefinitionKind::Interface), |
| 306 |
); |
| 307 |
name |
| 308 |
}; |
| 309 |
piece.push_str(";"); |
| 310 |
log::debug!("new use `{name}` in {i}"); |
| 311 |
file.worlds.swap_remove(&name); |
| 312 |
file.interfaces |
| 313 |
.insert(name.clone(), FileInterface { name, id, types }); |
| 314 |
file.items.push(piece) |
| 315 |
} |
| 316 |
Generate::Done => break, |
| 317 |
}; |
| 318 |
} |
| 319 |
|
| 320 |
shuffle(u, &mut files)?; |
| 321 |
for file in files.iter_mut() { |
| 322 |
shuffle(u, &mut file.items)?; |
| 323 |
} |
| 324 |
|
| 325 |
let mut has_name = false; |
| 326 |
let len = files.len(); |
| 327 |
for (i, file) in files.iter_mut().enumerate() { |
| 328 |
let mut s = String::new(); |
| 329 |
if u.arbitrary()? || (!has_name && i == len - 1) { |
| 330 |
has_name = true; |
| 331 |
s.push_str("package "); |
| 332 |
s.push_str("%"); |
| 333 |
s.push_str(&ret.name.namespace); |
| 334 |
s.push_str(":"); |
| 335 |
s.push_str("%"); |
| 336 |
s.push_str(&ret.name.name); |
| 337 |
if let Some(version) = &ret.name.version { |
| 338 |
s.push_str(&format!("@{version}")); |
| 339 |
} |
| 340 |
s.push_str(";\n\n"); |
| 341 |
} |
| 342 |
for piece in file.items.iter() { |
| 343 |
s.push_str(&piece); |
| 344 |
s.push_str("\n\n"); |
| 345 |
} |
| 346 |
log::trace!("==============================================="); |
| 347 |
log::trace!("{s}"); |
| 348 |
ret.sources.push(format!("wit{i}.wit").as_ref(), &s); |
| 349 |
} |
| 350 |
Ok(ret) |
| 351 |
} |
| 352 |
|
| 353 |
fn gen_world( |
| 354 |
&mut self, |
| 355 |
u: &mut Unstructured<'_>, |
| 356 |
name: &str, |
| 357 |
file: &mut File, |
| 358 |
package_name: &str, |
| 359 |
version: Option<Version>, |
| 360 |
) -> Result<String> { |
| 361 |
InterfaceGenerator::new(self, file, package_name, version).gen_world(u, name) |
| 362 |
} |
| 363 |
|
| 364 |
fn gen_interface( |
| 365 |
&mut self, |
| 366 |
u: &mut Unstructured<'_>, |
| 367 |
name: Option<&str>, |
| 368 |
file: &mut File, |
| 369 |
package_name: &str, |
| 370 |
world_name: Option<&str>, |
| 371 |
version: Option<Version>, |
| 372 |
) -> Result<(String, Vec<Type>)> { |
| 373 |
let mut generator = InterfaceGenerator::new(self, file, package_name, version); |
| 374 |
let ret = generator.gen_interface(u, name, world_name)?; |
| 375 |
Ok((ret, generator.types_in_interface)) |
| 376 |
} |
| 377 |
|
| 378 |
fn gen_interface_path<'a>( |
| 379 |
&'a self, |
| 380 |
u: &mut Unstructured<'_>, |
| 381 |
file: &'a mut File, |
| 382 |
dst: &mut String, |
| 383 |
) -> Result<Option<(&'a str, u32, &'a Rc<Vec<Type>>)>> { |
| 384 |
enum Choice { |
| 385 |
Interfaces, |
| 386 |
Packages, |
| 387 |
} |
| 388 |
let mut choices = Vec::new(); |
| 389 |
if !file.interfaces.is_empty() { |
| 390 |
choices.push(Choice::Interfaces); |
| 391 |
} |
| 392 |
if !self.packages.packages_with_interfaces.is_empty() { |
| 393 |
choices.push(Choice::Packages); |
| 394 |
} |
| 395 |
|
| 396 |
if choices.is_empty() { |
| 397 |
return Ok(None); |
| 398 |
} |
| 399 |
Ok(match u.choose(&choices)? { |
| 400 |
Choice::Interfaces => { |
| 401 |
let i = u.int_in_range(0..=file.interfaces.len() - 1)?; |
| 402 |
let (name, i) = file.interfaces.get_index(i).unwrap(); |
| 403 |
// Once a name is used from a file's local namespace then it |
| 404 |
// can't be overridden in that namespace so switch it to a file |
| 405 |
// definition from whatever it previously was. |
| 406 |
file.namespace.insert( |
| 407 |
name.clone(), |
| 408 |
(DefinitionLevel::File, DefinitionKind::Interface), |
| 409 |
); |
| 410 |
file.worlds.swap_remove(name); |
| 411 |
dst.push_str("%"); |
| 412 |
dst.push_str(&i.name); |
| 413 |
Some((&i.name, i.id, &i.types)) |
| 414 |
} |
| 415 |
Choice::Packages => { |
| 416 |
let pkg = u.choose(&self.packages.packages_with_interfaces)?; |
| 417 |
let pkg = &self.packages.list[*pkg]; |
| 418 |
dst.push_str("%"); |
| 419 |
dst.push_str(&pkg.name.namespace); |
| 420 |
dst.push_str(":"); |
| 421 |
dst.push_str("%"); |
| 422 |
dst.push_str(&pkg.name.name); |
| 423 |
dst.push_str("/"); |
| 424 |
let i = u.int_in_range(0..=pkg.file.interfaces.len() - 1)?; |
| 425 |
let i = &pkg.file.interfaces[i]; |
| 426 |
dst.push_str("%"); |
| 427 |
dst.push_str(&i.name); |
| 428 |
if let Some(version) = &pkg.name.version { |
| 429 |
dst.push_str(&format!("@{version}")); |
| 430 |
} |
| 431 |
Some((&i.name, i.id, &i.types)) |
| 432 |
} |
| 433 |
}) |
| 434 |
} |
| 435 |
|
| 436 |
fn gen_world_path<'a>( |
| 437 |
&'a self, |
| 438 |
u: &mut Unstructured<'_>, |
| 439 |
file: &'a mut File, |
| 440 |
dst: &mut String, |
| 441 |
includes: &mut HashSet<String>, |
| 442 |
) -> Result<WorldPath<'a>> { |
| 443 |
enum Choice { |
| 444 |
Worlds, |
| 445 |
Packages, |
| 446 |
} |
| 447 |
let mut choices = Vec::new(); |
| 448 |
if !file.worlds.is_empty() { |
| 449 |
choices.push(Choice::Worlds); |
| 450 |
} |
| 451 |
if !self.packages.packages_with_worlds.is_empty() { |
| 452 |
choices.push(Choice::Packages); |
| 453 |
} |
| 454 |
|
| 455 |
if choices.is_empty() { |
| 456 |
return Ok(WorldPath::None); |
| 457 |
} |
| 458 |
Ok(match u.choose(&choices)? { |
| 459 |
Choice::Worlds => { |
| 460 |
let i = u.int_in_range(0..=file.worlds.len() - 1)?; |
| 461 |
let name = &file.worlds[i]; |
| 462 |
|
| 463 |
if !includes.insert(name.to_string()) { |
| 464 |
return Ok(WorldPath::None); |
| 465 |
} |
| 466 |
|
| 467 |
dst.push_str("%"); |
| 468 |
dst.push_str(&name); |
| 469 |
// Same as `gen_interface_path`, once a name is used as a world |
| 470 |
// it's forced to always be a world so update its definition to |
| 471 |
// be a file-level world. |
| 472 |
|
| 473 |
file.namespace |
| 474 |
.insert(name.clone(), (DefinitionLevel::File, DefinitionKind::World)); |
| 475 |
WorldPath::Local(name) |
| 476 |
} |
| 477 |
Choice::Packages => { |
| 478 |
let pkg = u.choose(&self.packages.packages_with_worlds)?; |
| 479 |
let pkg = &self.packages.list[*pkg]; |
| 480 |
dst.push_str("%"); |
| 481 |
dst.push_str(&pkg.name.namespace); |
| 482 |
dst.push_str(":"); |
| 483 |
dst.push_str("%"); |
| 484 |
dst.push_str(&pkg.name.name); |
| 485 |
dst.push_str("/"); |
| 486 |
let i = u.int_in_range(0..=pkg.file.worlds.len() - 1)?; |
| 487 |
let w = &pkg.file.worlds[i]; |
| 488 |
dst.push_str("%"); |
| 489 |
dst.push_str(&w); |
| 490 |
if let Some(version) = &pkg.name.version { |
| 491 |
dst.push_str(&format!("@{version}")); |
| 492 |
} |
| 493 |
WorldPath::Remote |
| 494 |
} |
| 495 |
}) |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
impl<'a> InterfaceGenerator<'a> { |
| 500 |
fn new( |
| 501 |
generator: &'a mut Generator, |
| 502 |
file: &'a mut File, |
| 503 |
package_name: &'a str, |
| 504 |
version: Option<Version>, |
| 505 |
) -> InterfaceGenerator<'a> { |
| 506 |
InterfaceGenerator { |
| 507 |
generator, |
| 508 |
file, |
| 509 |
types_in_interface: Vec::new(), |
| 510 |
// Claim the name `memory` to avoid conflicting with the canonical |
| 511 |
// ABI always using a linear memory named `memory`. |
| 512 |
unique_names: HashSet::from_iter(["memory".to_string()]), |
| 513 |
package_name: package_name, |
| 514 |
version, |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
// Generate a feature gate annotation (@since, @unstable, or @deprecated) |
| 519 |
// If version is provided, ensures the annotation is compatible with the version |
| 520 |
fn gen_feature_annotation(&self, u: &mut Unstructured<'_>) -> Result<Option<String>> { |
| 521 |
if u.arbitrary()? { |
| 522 |
return Ok(None); |
| 523 |
} |
| 524 |
|
| 525 |
let feature_names = ["active", "inactive"]; |
| 526 |
#[derive(Arbitrary)] |
| 527 |
enum AnnotationType { |
| 528 |
Since, |
| 529 |
Unstable, |
| 530 |
Deprecated, |
| 531 |
} |
| 532 |
|
| 533 |
match self.version { |
| 534 |
None => { |
| 535 |
// No package version available |
| 536 |
return Ok(None); |
| 537 |
} |
| 538 |
Some(_) => match u.arbitrary()? { |
| 539 |
AnnotationType::Since => { |
| 540 |
let v = gen_version_less_than(u, &self.version)?; |
| 541 |
Ok(Some(format!("@since(version = {v})"))) |
| 542 |
} |
| 543 |
AnnotationType::Unstable => { |
| 544 |
let feature = u.choose(&feature_names)?; |
| 545 |
Ok(Some(format!("@unstable(feature = {feature})"))) |
| 546 |
} |
| 547 |
AnnotationType::Deprecated => { |
| 548 |
let depreciation_version = gen_version_less_than(u, &self.version)?; |
| 549 |
let since_version = |
| 550 |
gen_version_less_than(u, &Some(depreciation_version.clone()))?; |
| 551 |
Ok(Some(format!( |
| 552 |
"@deprecated(version = {depreciation_version})\n@since(version = {since_version})", |
| 553 |
))) |
| 554 |
} |
| 555 |
}, |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
fn gen_interface( |
| 560 |
&mut self, |
| 561 |
u: &mut Unstructured<'_>, |
| 562 |
name: Option<&str>, |
| 563 |
world_name: Option<&str>, |
| 564 |
) -> Result<String> { |
| 565 |
let mut ret = String::new(); |
| 566 |
|
| 567 |
if let Some(annotation) = self.gen_feature_annotation(u)? { |
| 568 |
ret.push_str(&annotation); |
| 569 |
ret.push_str("\n"); |
| 570 |
} |
| 571 |
|
| 572 |
ret.push_str("interface "); |
| 573 |
if let Some(name) = name { |
| 574 |
ret.push_str("%"); |
| 575 |
ret.push_str(name); |
| 576 |
ret.push_str(" "); |
| 577 |
} |
| 578 |
ret.push_str("{\n"); |
| 579 |
|
| 580 |
#[derive(Arbitrary)] |
| 581 |
enum Generate { |
| 582 |
Use, |
| 583 |
Type, |
| 584 |
Function, |
| 585 |
} |
| 586 |
|
| 587 |
let mut parts = Vec::new(); |
| 588 |
while parts.len() < self.generator.config.max_interface_items && u.arbitrary()? { |
| 589 |
let mut part = String::new(); |
| 590 |
if let Some(annotation) = self.gen_feature_annotation(u)? { |
| 591 |
part.push_str(&annotation); |
| 592 |
part.push_str("\n"); |
| 593 |
} |
| 594 |
|
| 595 |
match u.arbitrary()? { |
| 596 |
Generate::Use => { |
| 597 |
if !self.gen_use(u, &mut part, world_name)? { |
| 598 |
continue; |
| 599 |
} |
| 600 |
} |
| 601 |
Generate::Type => { |
| 602 |
let name = self.gen_unique_name(u)?; |
| 603 |
let ty = self.gen_typedef(u, &name, &mut part)?; |
| 604 |
let is_resource = ty.is_resource; |
| 605 |
self.types_in_interface.push(ty); |
| 606 |
if is_resource { |
| 607 |
if u.arbitrary()? { |
| 608 |
part.push_str(" {\n"); |
| 609 |
self.gen_resource_funcs(&name, u, &mut part)?; |
| 610 |
part.push_str("}"); |
| 611 |
} else { |
| 612 |
part.push_str(";"); |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
Generate::Function => { |
| 617 |
self.gen_func(u, &mut part)?; |
| 618 |
} |
| 619 |
} |
| 620 |
parts.push(part); |
| 621 |
} |
| 622 |
|
| 623 |
shuffle(u, &mut parts)?; |
| 624 |
for part in parts { |
| 625 |
ret.push_str(&part); |
| 626 |
ret.push_str("\n\n"); |
| 627 |
} |
| 628 |
|
| 629 |
ret.push_str("}"); |
| 630 |
Ok(ret) |
| 631 |
} |
| 632 |
|
| 633 |
fn gen_world(&mut self, u: &mut Unstructured<'_>, world_name: &str) -> Result<String> { |
| 634 |
let mut ret = String::new(); |
| 635 |
ret.push_str("world %"); |
| 636 |
ret.push_str(world_name); |
| 637 |
ret.push_str(" {\n"); |
| 638 |
|
| 639 |
#[derive(Arbitrary, Copy, Clone, Debug)] |
| 640 |
enum Direction { |
| 641 |
Import, |
| 642 |
Export, |
| 643 |
} |
| 644 |
|
| 645 |
#[derive(Arbitrary)] |
| 646 |
enum ItemKind { |
| 647 |
Func(Direction), |
| 648 |
Interface(Direction), |
| 649 |
AnonInterface(Direction), |
| 650 |
Type, |
| 651 |
Use, |
| 652 |
Include, |
| 653 |
} |
| 654 |
|
| 655 |
let mut parts = Vec::new(); |
| 656 |
let mut imported_interfaces = HashSet::new(); |
| 657 |
let mut exported_interfaces = HashSet::new(); |
| 658 |
let mut includes: HashSet<String> = HashSet::new(); |
| 659 |
|
| 660 |
// Claim the name `memory` to avoid conflicting with the canonical |
| 661 |
// ABI always using a linear memory named `memory`. |
| 662 |
let mut export_names = HashSet::from_iter(["memory".to_string()]); |
| 663 |
|
| 664 |
while parts.len() < self.generator.config.max_world_items |
| 665 |
&& !u.is_empty() |
| 666 |
&& u.arbitrary()? |
| 667 |
{ |
| 668 |
let kind = u.arbitrary::<ItemKind>()?; |
| 669 |
let (direction, named) = match kind { |
| 670 |
ItemKind::Func(dir) | ItemKind::AnonInterface(dir) => (Some(dir), true), |
| 671 |
ItemKind::Interface(dir) => (Some(dir), false), |
| 672 |
ItemKind::Type => (None, true), |
| 673 |
ItemKind::Use => (None, false), |
| 674 |
ItemKind::Include => (None, false), |
| 675 |
}; |
| 676 |
|
| 677 |
let mut part = String::new(); |
| 678 |
|
| 679 |
if let Some(annotation) = self.gen_feature_annotation(u)? { |
| 680 |
part.push_str(&annotation); |
| 681 |
part.push_str("\n"); |
| 682 |
} |
| 683 |
|
| 684 |
if let Some(dir) = direction { |
| 685 |
part.push_str(match dir { |
| 686 |
Direction::Import => "import ", |
| 687 |
Direction::Export => "export ", |
| 688 |
}); |
| 689 |
} |
| 690 |
|
| 691 |
let name = if named { |
| 692 |
let names = match direction { |
| 693 |
Some(Direction::Import) | None => &mut self.unique_names, |
| 694 |
Some(Direction::Export) => &mut export_names, |
| 695 |
}; |
| 696 |
let mut name = gen_unique_name(u, names)?; |
| 697 |
|
| 698 |
// check to see if any includes have a name clash, if so regenerate the name |
| 699 |
// this does have potential to throw away add a few names but that should be fine |
| 700 |
for i in includes.iter() { |
| 701 |
if self.generator.packages.contains_name( |
| 702 |
self.package_name.to_string(), |
| 703 |
i.to_string(), |
| 704 |
&name, |
| 705 |
) { |
| 706 |
name = gen_unique_name(u, names)?; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
if direction.is_some() { |
| 711 |
part.push_str("%"); |
| 712 |
part.push_str(&name); |
| 713 |
part.push_str(": "); |
| 714 |
} |
| 715 |
|
| 716 |
self.generator.packages.add_name( |
| 717 |
self.package_name.to_string(), |
| 718 |
world_name.to_string(), |
| 719 |
name.to_string(), |
| 720 |
); |
| 721 |
Some(name) |
| 722 |
} else { |
| 723 |
None |
| 724 |
}; |
| 725 |
|
| 726 |
match kind { |
| 727 |
ItemKind::Func(_) => { |
| 728 |
self.gen_func_sig(u, &mut part, false)?; |
| 729 |
} |
| 730 |
ItemKind::Interface(dir) => { |
| 731 |
let id = match self.generator.gen_interface_path(u, self.file, &mut part)? { |
| 732 |
Some((_name, id, _types)) => id, |
| 733 |
// If an interface couldn't be chosen or wasn't |
| 734 |
// chosen then skip this import. A unique name was |
| 735 |
// selecteed above but we just sort of leave that |
| 736 |
// floating in the wild to get handled by some other |
| 737 |
// test case. |
| 738 |
None => continue, |
| 739 |
}; |
| 740 |
|
| 741 |
// If this interface has already been imported or |
| 742 |
// exported this document can't do so again. Throw out |
| 743 |
// this item in that situation. |
| 744 |
let unique = match dir { |
| 745 |
Direction::Import => imported_interfaces.insert(id), |
| 746 |
Direction::Export => exported_interfaces.insert(id), |
| 747 |
}; |
| 748 |
if !unique { |
| 749 |
continue; |
| 750 |
} |
| 751 |
part.push_str(";"); |
| 752 |
} |
| 753 |
ItemKind::AnonInterface(_) => { |
| 754 |
let iface = |
| 755 |
InterfaceGenerator::new(self.generator, self.file, self.package_name, None) |
| 756 |
.gen_interface(u, None, Some(world_name))?; |
| 757 |
part.push_str(&iface); |
| 758 |
} |
| 759 |
|
| 760 |
ItemKind::Type => { |
| 761 |
let name = name.unwrap(); |
| 762 |
let ty = self.gen_typedef(u, &name, &mut part)?; |
| 763 |
let is_resource = ty.is_resource; |
| 764 |
self.types_in_interface.push(ty); |
| 765 |
|
| 766 |
if is_resource { |
| 767 |
if u.arbitrary()? { |
| 768 |
part.push_str(" {\n"); |
| 769 |
self.gen_resource_funcs(&name, u, &mut part)?; |
| 770 |
part.push_str("}"); |
| 771 |
} else { |
| 772 |
part.push_str(";"); |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
ItemKind::Use => { |
| 778 |
if !self.gen_use(u, &mut part, Some(world_name))? { |
| 779 |
continue; |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
ItemKind::Include => { |
| 784 |
part.push_str("include "); |
| 785 |
match self |
| 786 |
.generator |
| 787 |
.gen_world_path(u, self.file, &mut part, &mut includes)? |
| 788 |
{ |
| 789 |
WorldPath::Local(name) => { |
| 790 |
// rename things if there is an naming conflict with |
| 791 |
// the include and the world we are going into this |
| 792 |
// is a best effort, there are some edge cases where |
| 793 |
// we might not catch something in that case we just |
| 794 |
// throw away the generated world for fuzzing |
| 795 |
let current_world = PackageWorldKey { |
| 796 |
package_name: self.package_name.to_owned(), |
| 797 |
world_name: world_name.to_owned(), |
| 798 |
}; |
| 799 |
let include_world = PackageWorldKey { |
| 800 |
package_name: self.package_name.to_owned(), |
| 801 |
world_name: name.to_owned(), |
| 802 |
}; |
| 803 |
let intersection = self |
| 804 |
.generator |
| 805 |
.packages |
| 806 |
.intersect(current_world, include_world); |
| 807 |
if let Some(names) = intersection { |
| 808 |
part.push_str(" with { %"); |
| 809 |
|
| 810 |
for n in names { |
| 811 |
part.push_str(n); |
| 812 |
part.push_str(" as %"); |
| 813 |
// we know it is in one of the worlds, lets |
| 814 |
// add it here just for good measure |
| 815 |
self.unique_names.insert(n.to_string()); |
| 816 |
let new_name = gen_unique_name(u, &mut self.unique_names)?; |
| 817 |
part.push_str(&new_name); |
| 818 |
part.push_str(","); |
| 819 |
} |
| 820 |
part.push_str("}"); |
| 821 |
} else { |
| 822 |
// ; is only used if not renaming |
| 823 |
part.push_str(";"); |
| 824 |
} |
| 825 |
} |
| 826 |
WorldPath::Remote => { |
| 827 |
part.push_str(";"); |
| 828 |
} |
| 829 |
WorldPath::None => continue, |
| 830 |
}; |
| 831 |
} |
| 832 |
} |
| 833 |
parts.push(part); |
| 834 |
} |
| 835 |
|
| 836 |
shuffle(u, &mut parts)?; |
| 837 |
|
| 838 |
for part in parts { |
| 839 |
ret.push_str(&part); |
| 840 |
ret.push_str("\n"); |
| 841 |
} |
| 842 |
|
| 843 |
ret.push_str("}"); |
| 844 |
|
| 845 |
Ok(ret) |
| 846 |
} |
| 847 |
|
| 848 |
fn gen_resource_funcs( |
| 849 |
&mut self, |
| 850 |
resource_name: &str, |
| 851 |
u: &mut Unstructured<'_>, |
| 852 |
ret: &mut String, |
| 853 |
) -> Result<()> { |
| 854 |
let mut parts = Vec::new(); |
| 855 |
|
| 856 |
#[derive(Arbitrary)] |
| 857 |
enum Item { |
| 858 |
Constructor, |
| 859 |
Static, |
| 860 |
Method, |
| 861 |
} |
| 862 |
|
| 863 |
let mut has_constructor = false; |
| 864 |
let mut names = HashSet::new(); |
| 865 |
names.insert(resource_name.to_string()); |
| 866 |
while parts.len() < self.generator.config.max_resource_items |
| 867 |
&& !u.is_empty() |
| 868 |
&& u.arbitrary()? |
| 869 |
{ |
| 870 |
match u.arbitrary()? { |
| 871 |
Item::Constructor if has_constructor => {} |
| 872 |
Item::Constructor => { |
| 873 |
has_constructor = true; |
| 874 |
let mut part = String::new(); |
| 875 |
|
| 876 |
if let Some(annotation) = self.gen_feature_annotation(u)? { |
| 877 |
part.push_str(&annotation); |
| 878 |
part.push_str("\n"); |
| 879 |
} |
| 880 |
|
| 881 |
part.push_str("constructor"); |
| 882 |
self.gen_params(u, &mut part, false)?; |
| 883 |
part.push_str(";"); |
| 884 |
parts.push(part); |
| 885 |
} |
| 886 |
Item::Static => { |
| 887 |
let mut part = String::new(); |
| 888 |
|
| 889 |
if let Some(annotation) = self.gen_feature_annotation(u)? { |
| 890 |
part.push_str(&annotation); |
| 891 |
part.push_str("\n"); |
| 892 |
} |
| 893 |
|
| 894 |
part.push_str("%"); |
| 895 |
part.push_str(&gen_unique_name(u, &mut names)?); |
| 896 |
part.push_str(": static "); |
| 897 |
self.gen_func_sig(u, &mut part, false)?; |
| 898 |
parts.push(part); |
| 899 |
} |
| 900 |
Item::Method => { |
| 901 |
let mut part = String::new(); |
| 902 |
|
| 903 |
if let Some(annotation) = self.gen_feature_annotation(u)? { |
| 904 |
part.push_str(&annotation); |
| 905 |
part.push_str("\n"); |
| 906 |
} |
| 907 |
|
| 908 |
part.push_str("%"); |
| 909 |
part.push_str(&gen_unique_name(u, &mut names)?); |
| 910 |
part.push_str(": "); |
| 911 |
self.gen_func_sig(u, &mut part, true)?; |
| 912 |
parts.push(part); |
| 913 |
} |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
shuffle(u, &mut parts)?; |
| 918 |
|
| 919 |
for part in parts { |
| 920 |
ret.push_str(&part); |
| 921 |
ret.push_str("\n"); |
| 922 |
} |
| 923 |
Ok(()) |
| 924 |
} |
| 925 |
|
| 926 |
fn gen_use( |
| 927 |
&mut self, |
| 928 |
u: &mut Unstructured<'_>, |
| 929 |
part: &mut String, |
| 930 |
world_name: Option<&str>, |
| 931 |
) -> Result<bool> { |
| 932 |
let mut path = String::new(); |
| 933 |
let (_name, _id, types) = |
| 934 |
match self.generator.gen_interface_path(u, self.file, &mut path)? { |
| 935 |
Some(types) => types, |
| 936 |
None => return Ok(false), |
| 937 |
}; |
| 938 |
part.push_str("use "); |
| 939 |
part.push_str(&path); |
| 940 |
part.push_str(".{"); |
| 941 |
let ty = u.choose(types)?; |
| 942 |
part.push_str("%"); |
| 943 |
part.push_str(&ty.name); |
| 944 |
let size = ty.size; |
| 945 |
let is_resource = ty.is_resource; |
| 946 |
let name = if self.unique_names.contains(&ty.name) || u.arbitrary()? { |
| 947 |
part.push_str(" as %"); |
| 948 |
let name = self.gen_unique_name(u)?; |
| 949 |
part.push_str(&name); |
| 950 |
// if we name something then we need track it at the package level for includes |
| 951 |
if let Some(world_name) = world_name { |
| 952 |
self.generator.packages.add_name( |
| 953 |
self.package_name.to_string(), |
| 954 |
world_name.to_string(), |
| 955 |
name.to_string(), |
| 956 |
); |
| 957 |
} |
| 958 |
name |
| 959 |
} else { |
| 960 |
assert!(self.unique_names.insert(ty.name.clone())); |
| 961 |
ty.name.clone() |
| 962 |
}; |
| 963 |
self.types_in_interface.push(Type { |
| 964 |
name, |
| 965 |
size, |
| 966 |
is_resource, |
| 967 |
}); |
| 968 |
part.push_str("};"); |
| 969 |
Ok(true) |
| 970 |
} |
| 971 |
|
| 972 |
fn gen_typedef( |
| 973 |
&mut self, |
| 974 |
u: &mut Unstructured<'_>, |
| 975 |
name: &str, |
| 976 |
ret: &mut String, |
| 977 |
) -> Result<Type> { |
| 978 |
#[derive(Arbitrary)] |
| 979 |
pub enum Kind { |
| 980 |
Record, |
| 981 |
Flags, |
| 982 |
Variant, |
| 983 |
Enum, |
| 984 |
Anonymous, |
| 985 |
Resource, |
| 986 |
} |
| 987 |
|
| 988 |
let mut fuel = self.generator.config.max_type_size; |
| 989 |
|
| 990 |
let mut is_resource = false; |
| 991 |
match u.arbitrary()? { |
| 992 |
Kind::Record => { |
| 993 |
ret.push_str("record %"); |
| 994 |
ret.push_str(name); |
| 995 |
ret.push_str(" {\n"); |
| 996 |
for _ in 0..u.int_in_range(1..=self.generator.config.max_type_parts)? { |
| 997 |
ret.push_str(" %"); |
| 998 |
ret.push_str(&self.gen_unique_name(u)?); |
| 999 |
ret.push_str(": "); |
| 1000 |
self.gen_type(u, &mut fuel, ret)?; |
| 1001 |
ret.push_str(",\n"); |
| 1002 |
} |
| 1003 |
ret.push_str("}"); |
| 1004 |
} |
| 1005 |
Kind::Variant => { |
| 1006 |
ret.push_str("variant %"); |
| 1007 |
ret.push_str(name); |
| 1008 |
ret.push_str(" {\n"); |
| 1009 |
for _ in 0..u.int_in_range(1..=self.generator.config.max_type_parts)? { |
| 1010 |
ret.push_str(" %"); |
| 1011 |
ret.push_str(&self.gen_unique_name(u)?); |
| 1012 |
if u.arbitrary()? { |
| 1013 |
ret.push_str("("); |
| 1014 |
self.gen_type(u, &mut fuel, ret)?; |
| 1015 |
ret.push_str(")"); |
| 1016 |
} |
| 1017 |
ret.push_str(",\n"); |
| 1018 |
} |
| 1019 |
ret.push_str("}"); |
| 1020 |
} |
| 1021 |
Kind::Enum => { |
| 1022 |
ret.push_str("enum %"); |
| 1023 |
ret.push_str(name); |
| 1024 |
ret.push_str(" {\n"); |
| 1025 |
for _ in 0..u.int_in_range(1..=self.generator.config.max_type_parts)? { |
| 1026 |
ret.push_str(" %"); |
| 1027 |
ret.push_str(&self.gen_unique_name(u)?); |
| 1028 |
ret.push_str(",\n"); |
| 1029 |
} |
| 1030 |
ret.push_str("}"); |
| 1031 |
} |
| 1032 |
Kind::Flags => { |
| 1033 |
ret.push_str("flags %"); |
| 1034 |
ret.push_str(name); |
| 1035 |
ret.push_str(" {\n"); |
| 1036 |
for _ in 0..u.int_in_range(1..=self.generator.config.max_type_parts)? { |
| 1037 |
ret.push_str(" %"); |
| 1038 |
ret.push_str(&self.gen_unique_name(u)?); |
| 1039 |
ret.push_str(",\n"); |
| 1040 |
} |
| 1041 |
ret.push_str("}"); |
| 1042 |
} |
| 1043 |
Kind::Anonymous => { |
| 1044 |
ret.push_str("type %"); |
| 1045 |
ret.push_str(name); |
| 1046 |
ret.push_str(" = "); |
| 1047 |
self.gen_type(u, &mut fuel, ret)?; |
| 1048 |
ret.push_str(";"); |
| 1049 |
} |
| 1050 |
Kind::Resource => { |
| 1051 |
is_resource = true; |
| 1052 |
ret.push_str("resource %"); |
| 1053 |
ret.push_str(name); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
Ok(Type { |
| 1058 |
size: self.generator.config.max_type_size - fuel, |
| 1059 |
is_resource, |
| 1060 |
name: name.to_string(), |
| 1061 |
}) |
| 1062 |
} |
| 1063 |
|
| 1064 |
fn gen_type( |
| 1065 |
&mut self, |
| 1066 |
u: &mut Unstructured<'_>, |
| 1067 |
fuel: &mut usize, |
| 1068 |
dst: &mut String, |
| 1069 |
) -> Result<()> { |
| 1070 |
#[derive(Arbitrary)] |
| 1071 |
enum Kind { |
| 1072 |
Bool, |
| 1073 |
U8, |
| 1074 |
U16, |
| 1075 |
U32, |
| 1076 |
U64, |
| 1077 |
S8, |
| 1078 |
S16, |
| 1079 |
S32, |
| 1080 |
S64, |
| 1081 |
F32, |
| 1082 |
F64, |
| 1083 |
Char, |
| 1084 |
String, |
| 1085 |
Id, |
| 1086 |
Tuple, |
| 1087 |
Option, |
| 1088 |
Result, |
| 1089 |
List, |
| 1090 |
FixedSizeList, |
| 1091 |
Stream, |
| 1092 |
Future, |
| 1093 |
ErrorContext, |
| 1094 |
} |
| 1095 |
|
| 1096 |
*fuel = match fuel.checked_sub(1) { |
| 1097 |
Some(fuel) => fuel, |
| 1098 |
None => { |
| 1099 |
dst.push_str("bool"); |
| 1100 |
return Ok(()); |
| 1101 |
} |
| 1102 |
}; |
| 1103 |
loop { |
| 1104 |
break match u.arbitrary()? { |
| 1105 |
Kind::Bool => dst.push_str("bool"), |
| 1106 |
Kind::U8 => dst.push_str("u8"), |
| 1107 |
Kind::S8 => dst.push_str("s8"), |
| 1108 |
Kind::U16 => dst.push_str("u16"), |
| 1109 |
Kind::S16 => dst.push_str("s16"), |
| 1110 |
Kind::U32 => dst.push_str("u32"), |
| 1111 |
Kind::S32 => dst.push_str("s32"), |
| 1112 |
Kind::U64 => dst.push_str("u64"), |
| 1113 |
Kind::S64 => dst.push_str("s64"), |
| 1114 |
Kind::F32 => dst.push_str("f32"), |
| 1115 |
Kind::F64 => dst.push_str("f64"), |
| 1116 |
Kind::Char => dst.push_str("char"), |
| 1117 |
Kind::String => dst.push_str("string"), |
| 1118 |
Kind::Id => { |
| 1119 |
if self.types_in_interface.is_empty() { |
| 1120 |
continue; |
| 1121 |
} |
| 1122 |
let ty = u.choose(&self.types_in_interface)?; |
| 1123 |
*fuel = match fuel.checked_sub(ty.size) { |
| 1124 |
Some(fuel) => fuel, |
| 1125 |
None => continue, |
| 1126 |
}; |
| 1127 |
let own_wrapper = if ty.is_resource && u.arbitrary()? { |
| 1128 |
dst.push_str("own<"); |
| 1129 |
true |
| 1130 |
} else { |
| 1131 |
false |
| 1132 |
}; |
| 1133 |
dst.push_str("%"); |
| 1134 |
dst.push_str(&ty.name); |
| 1135 |
if own_wrapper { |
| 1136 |
dst.push_str(">"); |
| 1137 |
} |
| 1138 |
} |
| 1139 |
Kind::Tuple => { |
| 1140 |
let fields = u.int_in_range(1..=self.generator.config.max_type_parts)?; |
| 1141 |
*fuel = match fuel.checked_sub(fields) { |
| 1142 |
Some(fuel) => fuel, |
| 1143 |
None => continue, |
| 1144 |
}; |
| 1145 |
dst.push_str("tuple<"); |
| 1146 |
for i in 0..fields { |
| 1147 |
if i > 0 { |
| 1148 |
dst.push_str(", "); |
| 1149 |
} |
| 1150 |
self.gen_type(u, fuel, dst)?; |
| 1151 |
} |
| 1152 |
dst.push_str(">"); |
| 1153 |
} |
| 1154 |
Kind::Option => { |
| 1155 |
*fuel = match fuel.checked_sub(1) { |
| 1156 |
Some(fuel) => fuel, |
| 1157 |
None => continue, |
| 1158 |
}; |
| 1159 |
dst.push_str("option<"); |
| 1160 |
self.gen_type(u, fuel, dst)?; |
| 1161 |
dst.push_str(">"); |
| 1162 |
} |
| 1163 |
Kind::List => { |
| 1164 |
*fuel = match fuel.checked_sub(1) { |
| 1165 |
Some(fuel) => fuel, |
| 1166 |
None => continue, |
| 1167 |
}; |
| 1168 |
dst.push_str("list<"); |
| 1169 |
self.gen_type(u, fuel, dst)?; |
| 1170 |
dst.push_str(">"); |
| 1171 |
} |
| 1172 |
Kind::FixedSizeList => { |
| 1173 |
*fuel = match fuel.checked_sub(1) { |
| 1174 |
Some(fuel) => fuel, |
| 1175 |
None => continue, |
| 1176 |
}; |
| 1177 |
let elements = |
| 1178 |
u.int_in_range(1..=self.generator.config.max_type_parts as u32)?; |
| 1179 |
dst.push_str("list<"); |
| 1180 |
self.gen_type(u, fuel, dst)?; |
| 1181 |
dst.push_str(&format!(", {elements}>")); |
| 1182 |
} |
| 1183 |
Kind::Result => { |
| 1184 |
*fuel = match fuel.checked_sub(2) { |
| 1185 |
Some(fuel) => fuel, |
| 1186 |
None => continue, |
| 1187 |
}; |
| 1188 |
dst.push_str("result"); |
| 1189 |
let ok = u.arbitrary()?; |
| 1190 |
let err = u.arbitrary()?; |
| 1191 |
match (ok, err) { |
| 1192 |
(true, true) => { |
| 1193 |
dst.push_str("<"); |
| 1194 |
self.gen_type(u, fuel, dst)?; |
| 1195 |
dst.push_str(", "); |
| 1196 |
self.gen_type(u, fuel, dst)?; |
| 1197 |
dst.push_str(">"); |
| 1198 |
} |
| 1199 |
(true, false) => { |
| 1200 |
dst.push_str("<"); |
| 1201 |
self.gen_type(u, fuel, dst)?; |
| 1202 |
dst.push_str(">"); |
| 1203 |
} |
| 1204 |
(false, true) => { |
| 1205 |
dst.push_str("<_, "); |
| 1206 |
self.gen_type(u, fuel, dst)?; |
| 1207 |
dst.push_str(">"); |
| 1208 |
} |
| 1209 |
(false, false) => {} |
| 1210 |
} |
| 1211 |
} |
| 1212 |
Kind::Stream => { |
| 1213 |
*fuel = match fuel.checked_sub(1) { |
| 1214 |
Some(fuel) => fuel, |
| 1215 |
None => continue, |
| 1216 |
}; |
| 1217 |
dst.push_str("stream<"); |
| 1218 |
self.gen_type(u, fuel, dst)?; |
| 1219 |
dst.push_str(">"); |
| 1220 |
} |
| 1221 |
Kind::Future => { |
| 1222 |
*fuel = match fuel.checked_sub(1) { |
| 1223 |
Some(fuel) => fuel, |
| 1224 |
None => continue, |
| 1225 |
}; |
| 1226 |
if u.arbitrary()? { |
| 1227 |
dst.push_str("future<"); |
| 1228 |
self.gen_type(u, fuel, dst)?; |
| 1229 |
dst.push_str(">"); |
| 1230 |
} else { |
| 1231 |
dst.push_str("future"); |
| 1232 |
} |
| 1233 |
} |
| 1234 |
Kind::ErrorContext => { |
| 1235 |
dst.push_str("error-context"); |
| 1236 |
} |
| 1237 |
}; |
| 1238 |
} |
| 1239 |
|
| 1240 |
Ok(()) |
| 1241 |
} |
| 1242 |
|
| 1243 |
fn gen_func(&mut self, u: &mut Unstructured<'_>, ret: &mut String) -> Result<()> { |
| 1244 |
ret.push_str("%"); |
| 1245 |
ret.push_str(&self.gen_unique_name(u)?); |
| 1246 |
ret.push_str(": "); |
| 1247 |
self.gen_func_sig(u, ret, false)?; |
| 1248 |
Ok(()) |
| 1249 |
} |
| 1250 |
|
| 1251 |
fn gen_func_sig( |
| 1252 |
&mut self, |
| 1253 |
u: &mut Unstructured<'_>, |
| 1254 |
dst: &mut String, |
| 1255 |
method: bool, |
| 1256 |
) -> Result<()> { |
| 1257 |
if u.arbitrary()? { |
| 1258 |
dst.push_str("async "); |
| 1259 |
} |
| 1260 |
dst.push_str("func"); |
| 1261 |
self.gen_params(u, dst, method)?; |
| 1262 |
if u.arbitrary()? { |
| 1263 |
dst.push_str(" -> "); |
| 1264 |
let mut fuel = self.generator.config.max_type_size; |
| 1265 |
self.gen_type(u, &mut fuel, dst)?; |
| 1266 |
} |
| 1267 |
dst.push_str(";"); |
| 1268 |
Ok(()) |
| 1269 |
} |
| 1270 |
|
| 1271 |
fn gen_params( |
| 1272 |
&mut self, |
| 1273 |
u: &mut Unstructured<'_>, |
| 1274 |
dst: &mut String, |
| 1275 |
method: bool, |
| 1276 |
) -> Result<()> { |
| 1277 |
dst.push_str("("); |
| 1278 |
let mut names = HashSet::new(); |
| 1279 |
if method { |
| 1280 |
names.insert("self".to_string()); |
| 1281 |
} |
| 1282 |
let mut fuel = self.generator.config.max_type_size; |
| 1283 |
for i in 0..u.int_in_range(0..=self.generator.config.max_type_parts)? { |
| 1284 |
if i > 0 { |
| 1285 |
dst.push_str(", "); |
| 1286 |
} |
| 1287 |
dst.push_str("%"); |
| 1288 |
dst.push_str(&gen_unique_name(u, &mut names)?); |
| 1289 |
dst.push_str(": "); |
| 1290 |
self.gen_type(u, &mut fuel, dst)?; |
| 1291 |
} |
| 1292 |
dst.push_str(")"); |
| 1293 |
Ok(()) |
| 1294 |
} |
| 1295 |
|
| 1296 |
fn gen_unique_name(&mut self, u: &mut Unstructured<'_>) -> Result<String> { |
| 1297 |
gen_unique_name(u, &mut self.unique_names) |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
fn gen_unique_name(u: &mut Unstructured<'_>, set: &mut HashSet<String>) -> Result<String> { |
| 1302 |
let mut name = gen_name(u)?; |
| 1303 |
while !set.insert(name.clone()) { |
| 1304 |
write!(&mut name, "{}", set.len()).unwrap(); |
| 1305 |
} |
| 1306 |
Ok(name) |
| 1307 |
} |
| 1308 |
|
| 1309 |
fn gen_name(u: &mut Unstructured<'_>) -> Result<String> { |
| 1310 |
let size = u.arbitrary_len::<u8>()?; |
| 1311 |
let size = std::cmp::min(size, 20); |
| 1312 |
let name = match str::from_utf8(u.peek_bytes(size).unwrap()) { |
| 1313 |
Ok(s) => { |
| 1314 |
u.bytes(size).unwrap(); |
| 1315 |
s.to_string() |
| 1316 |
} |
| 1317 |
Err(e) => { |
| 1318 |
let i = e.valid_up_to(); |
| 1319 |
let valid = u.bytes(i).unwrap(); |
| 1320 |
str::from_utf8(valid).unwrap().to_string() |
| 1321 |
} |
| 1322 |
}; |
| 1323 |
let name = name |
| 1324 |
.chars() |
| 1325 |
.map(|x| if x.is_ascii_lowercase() { x } else { 'x' }) |
| 1326 |
.collect::<String>(); |
| 1327 |
Ok(if name.is_empty() { |
| 1328 |
"name".to_string() |
| 1329 |
} else { |
| 1330 |
name |
| 1331 |
}) |
| 1332 |
} |
| 1333 |
|
| 1334 |
fn shuffle<T>(u: &mut Unstructured<'_>, mut slice: &mut [T]) -> Result<()> { |
| 1335 |
while slice.len() > 0 { |
| 1336 |
let pos = u.int_in_range(0..=slice.len() - 1)?; |
| 1337 |
slice.swap(0, pos); |
| 1338 |
slice = &mut slice[1..]; |
| 1339 |
} |
| 1340 |
Ok(()) |
| 1341 |
} |
| 1342 |
|
| 1343 |
#[derive(Clone, Default)] |
| 1344 |
struct File { |
| 1345 |
items: Vec<String>, |
| 1346 |
namespace: HashMap<String, (DefinitionLevel, DefinitionKind)>, |
| 1347 |
interfaces: IndexMap<String, FileInterface>, |
| 1348 |
worlds: IndexSet<String>, |
| 1349 |
} |
| 1350 |
|
| 1351 |
#[derive(Clone)] |
| 1352 |
struct FileInterface { |
| 1353 |
name: String, |
| 1354 |
id: u32, |
| 1355 |
types: Rc<Vec<Type>>, |
| 1356 |
} |
| 1357 |
|
| 1358 |
#[derive(Debug, Copy, Clone, PartialEq)] |
| 1359 |
enum DefinitionLevel { |
| 1360 |
Package, |
| 1361 |
File, |
| 1362 |
} |
| 1363 |
|
| 1364 |
#[derive(Debug, Copy, Clone, PartialEq)] |
| 1365 |
enum DefinitionKind { |
| 1366 |
World, |
| 1367 |
Interface, |
| 1368 |
} |
| 1369 |
|
| 1370 |
enum WorldPath<'a> { |
| 1371 |
None, |
| 1372 |
Local(&'a str), |
| 1373 |
Remote, |
| 1374 |
} |
| 1375 |
|
| 1376 |
impl File { |
| 1377 |
fn gen_unique_package_name( |
| 1378 |
&mut self, |
| 1379 |
u: &mut Unstructured<'_>, |
| 1380 |
names: &mut HashSet<String>, |
| 1381 |
kind: DefinitionKind, |
| 1382 |
) -> Result<String> { |
| 1383 |
let mut name = gen_name(u)?; |
| 1384 |
loop { |
| 1385 |
// Find a package-unique name first |
| 1386 |
if !names.insert(name.clone()) { |
| 1387 |
write!(&mut name, "{}", names.len()).unwrap(); |
| 1388 |
continue; |
| 1389 |
} |
| 1390 |
|
| 1391 |
// Then make sure it's file-unique too |
| 1392 |
if self.claim_file_name(&mut name, kind) { |
| 1393 |
break; |
| 1394 |
} |
| 1395 |
} |
| 1396 |
Ok(name) |
| 1397 |
} |
| 1398 |
|
| 1399 |
fn gen_unique_file_name( |
| 1400 |
&mut self, |
| 1401 |
u: &mut Unstructured<'_>, |
| 1402 |
kind: DefinitionKind, |
| 1403 |
) -> Result<String> { |
| 1404 |
let mut name = gen_name(u)?; |
| 1405 |
while !self.claim_file_name(&mut name, kind) { |
| 1406 |
// try again on the next iteration |
| 1407 |
} |
| 1408 |
Ok(name) |
| 1409 |
} |
| 1410 |
|
| 1411 |
fn claim_file_name(&mut self, name: &mut String, kind: DefinitionKind) -> bool { |
| 1412 |
match self.namespace.entry(name.clone()) { |
| 1413 |
Entry::Occupied(mut e) => match e.get().0 { |
| 1414 |
// If this name is already claimed elsewhere in the package |
| 1415 |
// then that's ok as we're going to shadow it, so switch it |
| 1416 |
// to a file definition. |
| 1417 |
DefinitionLevel::Package => *e.get_mut() = (DefinitionLevel::File, kind), |
| 1418 |
|
| 1419 |
// If it's already defined in the file try to add more stuff |
| 1420 |
// to the name to make the next try not collide. |
| 1421 |
DefinitionLevel::File => { |
| 1422 |
name.push_str("y"); |
| 1423 |
write!(name, "{}", self.namespace.len()).unwrap(); |
| 1424 |
return false; |
| 1425 |
} |
| 1426 |
}, |
| 1427 |
|
| 1428 |
// Not defined? Claim it. |
| 1429 |
Entry::Vacant(v) => { |
| 1430 |
v.insert((DefinitionLevel::File, kind)); |
| 1431 |
} |
| 1432 |
} |
| 1433 |
true |
| 1434 |
} |
| 1435 |
|
| 1436 |
fn insert_definition(&mut self, name: &str, kind: DefinitionKind) -> bool { |
| 1437 |
match self.namespace.get(name) { |
| 1438 |
// This name is already defined, so it can't be inserted. |
| 1439 |
Some((DefinitionLevel::File, _)) => return false, |
| 1440 |
Some(other) => { |
| 1441 |
panic!("found duplicate definition when should be package-unique: {other:?}") |
| 1442 |
} |
| 1443 |
None => {} |
| 1444 |
} |
| 1445 |
let prev = self |
| 1446 |
.namespace |
| 1447 |
.insert(name.to_string(), (DefinitionLevel::Package, kind)); |
| 1448 |
assert!(prev.is_none()); |
| 1449 |
true |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
fn gen_version_less_than( |
| 1454 |
u: &mut Unstructured<'_>, |
| 1455 |
existing_version: &Option<Version>, |
| 1456 |
) -> Result<Version> { |
| 1457 |
const MAX_VERSION_RANGE: u64 = 10; |
| 1458 |
let (major, minor, patch) = match existing_version { |
| 1459 |
Some(v) => (v.major, v.minor, v.patch), |
| 1460 |
None => (MAX_VERSION_RANGE, MAX_VERSION_RANGE, MAX_VERSION_RANGE), |
| 1461 |
}; |
| 1462 |
|
| 1463 |
let new_version = Version { |
| 1464 |
major: u.int_in_range(0..=major)?, |
| 1465 |
minor: u.int_in_range(0..=minor)?, |
| 1466 |
patch: u.int_in_range(0..=patch)?, |
| 1467 |
pre: if (u.arbitrary()? && existing_version.is_none()) |
| 1468 |
|| existing_version.as_ref().is_some_and(|x| !x.pre.is_empty()) |
| 1469 |
{ |
| 1470 |
semver::Prerelease::new("alpha.0").unwrap() |
| 1471 |
} else { |
| 1472 |
semver::Prerelease::EMPTY |
| 1473 |
}, |
| 1474 |
build: if (u.arbitrary()? && existing_version.is_none()) |
| 1475 |
|| existing_version |
| 1476 |
.as_ref() |
| 1477 |
.is_some_and(|x| !x.build.is_empty()) |
| 1478 |
{ |
| 1479 |
semver::BuildMetadata::new("1.2.0").unwrap() |
| 1480 |
} else { |
| 1481 |
semver::BuildMetadata::EMPTY |
| 1482 |
}, |
| 1483 |
}; |
| 1484 |
|
| 1485 |
if let Some(v) = existing_version { |
| 1486 |
assert!(&new_version <= v, "{} <= {}", &new_version, v); |
| 1487 |
} |
| 1488 |
|
| 1489 |
Ok(new_version) |
| 1490 |
} |
| 1491 |
|
| 1492 |
fn gen_version(u: &mut Unstructured<'_>) -> Result<Version> { |
| 1493 |
gen_version_less_than(u, &None) |
| 1494 |
} |