| @@ -448,38 +448,88 @@ fn release_builds_strip_the_runtime_debug_information() { |
| 448 | 448 | |
| 449 | 449 | /// The linker leaves out the runtime functions a program doesn't use |
| 450 | 450 | /// (`--gc-sections`, issue #125): hello has no HTTP parser, and no TLS |
| 451 | | −/// (rustls, ring, the roots: ADR 0038). |
| 451 | +/// (rustls, ring, the roots: ADR 0038). Checked with each release runtime |
| 452 | +/// that is built (ADR 0031, 0032), each named explicitly: which one a |
| 453 | +/// plain release build picks depends on the machine (issue #228). The |
| 454 | +/// checked runtime is not one: it is not built for size. |
| 452 | 455 | #[test] |
| 453 | 456 | fn unused_runtime_functions_are_left_out() { |
| 454 | | − let lib = std::path::Path::new(env!("CARGO_BIN_EXE_louise")).with_file_name("liblouise_rt.a"); |
| 455 | | − if !lib.is_file() || Command::new("nm").arg("--version").output().is_err() { |
| 456 | | − eprintln!("skipping: liblouise_rt.a not built, or no nm"); |
| 457 | + let louise = std::path::Path::new(env!("CARGO_BIN_EXE_louise")); |
| 458 | + if Command::new("nm").arg("--version").output().is_err() { |
| 459 | + eprintln!("skipping: no nm"); |
| 457 | 460 | return; |
| 458 | 461 | } |
| 459 | | − let exe = std::path::PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("gc/hello"); |
| 460 | | − let _ = std::fs::remove_file(&exe); |
| 461 | | − // Release builds strip the symbol table (ADR 0042); `-g` keeps it. |
| 462 | | − let out = Command::new(env!("CARGO_BIN_EXE_louise")) |
| 463 | | − .args(["build", "--release", "../../tests/run/hello.lse", "-o"]) |
| 464 | | − .arg(&exe) |
| 465 | | − .env("LOUISE_CFLAGS", "-g") |
| 466 | | − .current_dir(env!("CARGO_MANIFEST_DIR")) |
| 467 | | − .output() |
| 468 | | − .unwrap(); |
| 469 | | − assert!( |
| 470 | | − out.status.success(), |
| 471 | | − "{}", |
| 472 | | − String::from_utf8_lossy(&out.stderr) |
| 473 | | − ); |
| 474 | | − let symbols = Command::new("nm").arg(&exe).output().unwrap().stdout; |
| 475 | | − let symbols = String::from_utf8_lossy(&symbols); |
| 476 | | − assert!(symbols.contains("tl_main_program"), "the runtime is linked"); |
| 477 | | − assert!( |
| 478 | | − !symbols.contains("tl_http_parse_request"), |
| 479 | | − "unused code is dropped" |
| 480 | | − ); |
| 481 | | − for tls in ["tl_tls_connect", "rustls", "ring_core", "webpki_roots"] { |
| 482 | | − assert!(!symbols.contains(tls), "{tls} is linked into hello"); |
| 462 | + for runtime in ["fast", "lto"] { |
| 463 | + if !louise |
| 464 | + .with_file_name(format!("liblouise_rt-{runtime}.a")) |
| 465 | + .is_file() |
| 466 | + { |
| 467 | + eprintln!("skipping the {runtime} runtime: liblouise_rt-{runtime}.a not built"); |
| 468 | + continue; |
| 469 | + } |
| 470 | + let exe = std::path::PathBuf::from(env!("CARGO_TARGET_TMPDIR")) |
| 471 | + .join(format!("gc/hello-{runtime}")); |
| 472 | + let _ = std::fs::remove_file(&exe); |
| 473 | + // Release builds strip the symbol table (ADR 0042); `-g` keeps it. |
| 474 | + let out = Command::new(louise) |
| 475 | + .args(["build", "--release", "../../tests/run/hello.lse", "-o"]) |
| 476 | + .arg(&exe) |
| 477 | + .env("LOUISE_RUNTIME", runtime) |
| 478 | + .env("LOUISE_CFLAGS", "-g") |
| 479 | + .current_dir(env!("CARGO_MANIFEST_DIR")) |
| 480 | + .output() |
| 481 | + .unwrap(); |
| 482 | + if !out.status.success() && runtime == "lto" { |
| 483 | + // The toolchain can't link it here (the reason is tested above). |
| 484 | + eprintln!( |
| 485 | + "skipping the lto runtime: {}", |
| 486 | + String::from_utf8_lossy(&out.stderr) |
| 487 | + ); |
| 488 | + continue; |
| 489 | + } |
| 490 | + assert!( |
| 491 | + out.status.success(), |
| 492 | + "{runtime}: {}", |
| 493 | + String::from_utf8_lossy(&out.stderr) |
| 494 | + ); |
| 495 | + // Demangled, so that a crate is matched by its paths (`rustls::`), |
| 496 | + // not by the crate name that ends the symbols of the generic code |
| 497 | + // it instantiates: the fast runtime keeps a few trait methods of |
| 498 | + // webpki's error type that way, which are not TLS. |
| 499 | + let symbols = Command::new("nm") |
| 500 | + .arg("-C") |
| 501 | + .arg(&exe) |
| 502 | + .output() |
| 503 | + .unwrap() |
| 504 | + .stdout; |
| 505 | + let symbols = String::from_utf8_lossy(&symbols); |
| 506 | + assert!( |
| 507 | + symbols.contains("tl_main_program"), |
| 508 | + "{runtime}: the runtime is linked" |
| 509 | + ); |
| 510 | + assert!( |
| 511 | + symbols.contains("core::"), |
| 512 | + "{runtime}: nm demangles Rust symbols" |
| 513 | + ); |
| 514 | + assert!( |
| 515 | + !symbols.contains("tl_http_parse_request"), |
| 516 | + "{runtime}: unused code is dropped" |
| 517 | + ); |
| 518 | + // A name at the start of an identifier: `ring::` is not `String::`. |
| 519 | + let mentions = |name: &str| { |
| 520 | + symbols.match_indices(name).any(|(i, _)| { |
| 521 | + !symbols[..i].ends_with(|c: char| c.is_ascii_alphanumeric() || c == '_') |
| 522 | + }) |
| 523 | + }; |
| 524 | + for tls in [ |
| 525 | + "tl_tls_connect", |
| 526 | + "rustls::", |
| 527 | + "ring::", |
| 528 | + "ring_core", |
| 529 | + "webpki_roots::", |
| 530 | + ] { |
| 531 | + assert!(!mentions(tls), "{runtime}: {tls} is linked into hello"); |
| 532 | + } |
| 483 | 533 | } |
| 484 | 534 | } |
| 485 | 535 | |