Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions crates/socket-patch-cli/CLI_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -1022,11 +1022,13 @@ Ruby inside the project is never run, and every run goes without the Socket cred

The first rebuild of a copy backs up the original compiled code to `.socket-patch-native-orig/` in
the gem directory (the install-time extension directory and `lib/` copies; a precompiled gem's
prebuilt binaries, each when it is first replaced), and every rebuild records
prebuilt binaries, each when it is first replaced; every file of the gem's own tree an in-place
rebuild replaced, with the names of those it added), and every rebuild records
`.socket-patch-native-build.json` there: the source hashes it was built from and the hash of every
artifact it put in place (`"grafted": true` for a precompiled gem's replaced binaries, whose paths
name the Ruby minors they were built for). A record whose sources are patched and whose marker
matches is `already_patched`; sources an earlier release patched without a rebuild are rebuilt,
name the Ruby minors they were built for; for an in-place rebuild, `libArtifacts` also lists what
the build left in the gem's own tree for Ruby to load). A record whose sources are patched and
whose marker matches is `already_patched`; sources an earlier release patched without a rebuild are rebuilt,
with a `native_extension_rebuilt` warning. `rollback` (and `remove`) puts the backed-up build back
with the sources, bytes and file mode; without a backup it rebuilds a gem RubyGems built from the
restored sources, and fails a precompiled gem with `native_extension_restore_failed` (`gem
Expand Down Expand Up @@ -1067,7 +1069,7 @@ a file the copy does not hold is not written: the record fails on it as on any m
| Code | Run result | Meaning |
|---|---|---|
| `native_extensions_unsupported` | refused, exit 1 | The record changes build inputs that cannot be rebuilt here: the copy is not a RubyGems install with a `specifications/` entry (a Bundler git checkout); the gem neither declares extensions nor is a platform gem; it is the JRuby (`java`) build; no Ruby that owns the install was found; a precompiled gem cannot be built from its source gem (the archive is neither in the home's `cache/` nor downloadable, is not the copy's own source, the record does not apply to it, a gem its build needs could not be staged, its build leaves a library in the gem's own tree, or the build has no counterpart among the gem's prebuilt binaries); a record changes an input a build removed and the source gem cannot be built. Nothing is written. Also: another run is rebuilding the same gem right now, or an interrupted rebuild could not be undone (the error says which). |
| `native_extension_build_failed` | refused, exit 1 | The extension did not build from the patched sources (no compiler or Ruby headers, a build error), or the finished build could not be moved into place. The installed copy is as it was before the run; the error carries the build output's tail (of the build from the source gem and of the in-place one, when both ran). |
| `native_extension_build_failed` | refused, exit 1 | The extension did not build from the patched sources (no compiler or Ruby headers, a build error, an in-place build that exited cleanly without compiling anything), or the finished build could not be moved into place. The installed copy is as it was before the run; the error carries the build output's tail (of the build from the source gem and of the in-place one, when both ran). |
| `native_extension_rebuilt` | warning | The sources were already patched (by a release before #1996, with `--no-gem-native-rebuild`, or by `apply` under another Ruby version of a precompiled gem) and the extension was rebuilt from them. Under `--dry-run`: the rebuild a real `apply` would run; no Ruby is started and nothing is downloaded to check that it can. |
| `native_extension_not_rebuilt` | warning | `--no-gem-native-rebuild`: the sources were written without a rebuild; Ruby loads the old build until the caller compiles it. |
| `native_extension_restore_failed` | rollback: that copy fails | The sources were restored, but the original compiled extension could not be put back. Run `gem pristine <gem> --extensions` (a precompiled gem: `gem pristine <gem>`). |
Expand Down
314 changes: 239 additions & 75 deletions crates/socket-patch-cli/tests/e2e_gem_native_ext_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2918,60 +2918,89 @@ fn wrap_c(value: u32) -> String {
format!("int sp_wrap_value(void) {{ return {value}; }}\n")
}

/// A gem that loads its library from its own tree is not left running the
/// old one: a build staged elsewhere cannot stand in for a library the
/// installed copy loads from where it was built, so the staged build is
/// set aside and the gem is rebuilt where it is installed.
#[test]
#[ignore = "host capstone: compiles a C extension with the real ruby/gem toolchain; \
the unpinned `test` job skips it, the pinned-Ruby e2e leg runs it via --ignored"]
fn a_gem_that_loads_its_library_from_its_own_tree_is_rebuilt_in_place() {
let Some(abi) = toolchain() else {
return;
};
let tmp = tempfile::tempdir().unwrap();
let scratch = tmp.path().to_path_buf();
let src = scratch.join("sp_wrap");
std::fs::create_dir_all(src.join("ext/sp_wrap")).unwrap();
std::fs::create_dir_all(src.join("lib")).unwrap();
std::fs::write(src.join("ext/sp_wrap/extconf.rb"), WRAP_EXTCONF).unwrap();
std::fs::write(src.join(WRAP_C_KEY), wrap_c(1)).unwrap();
std::fs::write(src.join("lib/sp_wrap.rb"), WRAP_LIB).unwrap();
std::fs::write(
src.join("sp_wrap.gemspec"),
"Gem::Specification.new do |s|\n s.name = \"sp_wrap\"\n s.version = \"1.0.0\"\n \
s.summary = \"t\"\n s.authors = [\"t\"]\n \
s.files = Dir[\"lib/**/*.rb\", \"ext/**/*.{c,rb}\"]\n \
s.extensions = [\"ext/sp_wrap/extconf.rb\"]\nend\n",
)
.unwrap();
gem(&scratch, &src, &["build", "sp_wrap.gemspec"], &[]);
let proj = scratch.join("proj");
let home = proj.join("vendor/bundle/ruby").join(&abi);
std::fs::create_dir_all(&home).unwrap();
std::fs::write(
proj.join("Gemfile"),
"source \"https://rubygems.org\"\ngem \"sp_wrap\"\n",
)
.unwrap();
gem(
&scratch,
&scratch,
&[
"install",
"--local",
"--no-document",
"--install-dir",
home.to_str().unwrap(),
src.join("sp_wrap-1.0.0.gem").to_str().unwrap(),
],
&[],
);
let gem_dir = home.join("gems/sp_wrap-1.0.0");
let value = || {
/// `sp_wrap` with a build that takes a library it finds for current, as a
/// Rakefile whose library task does not depend on the C sources does
/// (digest-crc): run again over an installed tree, it compiles nothing.
const STALE_EXTCONF: &str = r##"require "rbconfig"
Dir.chdir(__dir__)
name = RbConfig::CONFIG["host_os"] =~ /darwin/ ? "libsp_wrap.dylib" : "libsp_wrap.so"
cc = RbConfig::CONFIG["CC"].split
unless File.exist?(name)
abort "could not build #{name}" unless system(*cc, "-shared", "-fPIC", "-o", name, "wrap.c")
end
File.write("Makefile", "all:\n\ninstall:\n\nclean:\n\n")
"##;

/// A project with `sp_wrap` (built by `extconf`) installed into
/// `vendor/bundle/ruby/<abi>` by the real `gem`, its archive in the home's
/// `cache/`, and a record that changes `SpWrap.value` from 1 to 2.
struct WrapGem {
_tmp: tempfile::TempDir,
proj: PathBuf,
home: PathBuf,
gem_dir: PathBuf,
}

impl WrapGem {
fn new(abi: &str, extconf: &str) -> WrapGem {
let tmp = tempfile::tempdir().unwrap();
let scratch = tmp.path().to_path_buf();
let src = scratch.join("sp_wrap");
std::fs::create_dir_all(src.join("ext/sp_wrap")).unwrap();
std::fs::create_dir_all(src.join("lib")).unwrap();
std::fs::write(src.join("ext/sp_wrap/extconf.rb"), extconf).unwrap();
std::fs::write(src.join(WRAP_C_KEY), wrap_c(1)).unwrap();
std::fs::write(src.join("lib/sp_wrap.rb"), WRAP_LIB).unwrap();
std::fs::write(
src.join("sp_wrap.gemspec"),
"Gem::Specification.new do |s|\n s.name = \"sp_wrap\"\n s.version = \"1.0.0\"\n \
s.summary = \"t\"\n s.authors = [\"t\"]\n \
s.files = Dir[\"lib/**/*.rb\", \"ext/**/*.{c,rb}\"]\n \
s.extensions = [\"ext/sp_wrap/extconf.rb\"]\nend\n",
)
.unwrap();
gem(&scratch, &src, &["build", "sp_wrap.gemspec"], &[]);
let proj = scratch.join("proj");
let home = proj.join("vendor/bundle/ruby").join(abi);
std::fs::create_dir_all(&home).unwrap();
std::fs::write(
proj.join("Gemfile"),
"source \"https://rubygems.org\"\ngem \"sp_wrap\"\n",
)
.unwrap();
gem(
&scratch,
&scratch,
&[
"install",
"--local",
"--no-document",
"--install-dir",
home.to_str().unwrap(),
src.join("sp_wrap-1.0.0.gem").to_str().unwrap(),
],
&[],
);
assert!(home.join("cache/sp_wrap-1.0.0.gem").is_file());
write_record(
&proj.join(".socket"),
WRAP_PURL,
&[(WRAP_C_KEY, Some(&wrap_c(1)), &wrap_c(2))],
);
let gem_dir = home.join("gems/sp_wrap-1.0.0");
WrapGem {
_tmp: tmp,
proj,
home,
gem_dir,
}
}

/// `SpWrap.value` as the installed gem's Ruby loads it.
fn value(&self) -> String {
let out = Command::new("ruby")
.args(["-W0", "-e", "require 'sp_wrap'; print SpWrap.value"])
.env("GEM_PATH", &home)
.env("GEM_PATH", &self.home)
.env_remove("GEM_HOME")
.env_remove("RUBYOPT")
.output()
Expand All @@ -2982,43 +3011,178 @@ fn a_gem_that_loads_its_library_from_its_own_tree_is_rebuilt_in_place() {
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).into_owned()
}

fn c_file(&self) -> String {
std::fs::read_to_string(self.gem_dir.join(WRAP_C_KEY)).unwrap()
}

/// The library the gem loads, relative to the gem dir.
fn library(&self) -> String {
["libsp_wrap.so", "libsp_wrap.dylib"]
.iter()
.map(|name| format!("ext/sp_wrap/{name}"))
.find(|rel| self.gem_dir.join(rel).is_file())
.expect("the gem's library is in its own tree")
}

fn run(&self, args: &[&str]) -> (i32, String, String) {
common::run_with_env(&self.proj, args, &[("SOCKET_RUBYGEMS_URL", NO_REGISTRY)])
}
}

/// A gem that loads its library from its own tree is not left running the
/// old one: a build staged elsewhere cannot stand in for a library the
/// installed copy loads from where it was built, so the staged build is
/// set aside and the gem is rebuilt where it is installed. The library
/// that build replaces is backed up and recorded like one in the extension
/// dir: `apply --check` notices it changing, and `rollback` gives the
/// original back byte for byte (#2104).
#[test]
#[ignore = "host capstone: compiles a C extension with the real ruby/gem toolchain; \
the unpinned `test` job skips it, the pinned-Ruby e2e leg runs it via --ignored"]
fn a_gem_that_loads_its_library_from_its_own_tree_is_rebuilt_in_place() {
let Some(abi) = toolchain() else {
return;
};
assert_eq!(value(), "1");
assert!(home.join("cache/sp_wrap-1.0.0.gem").is_file());
let (before, after) = (wrap_c(1), wrap_c(2));
write_record(
&proj.join(".socket"),
WRAP_PURL,
&[(WRAP_C_KEY, Some(&before), &after)],
);
let run =
|args: &[&str]| common::run_with_env(&proj, args, &[("SOCKET_RUBYGEMS_URL", NO_REGISTRY)]);
let fx = WrapGem::new(&abi, WRAP_EXTCONF);
assert_eq!(fx.value(), "1");
let installed = walk_files(&fx.gem_dir);
let extensions = walk_files(&fx.home.join("extensions"));
let library = fx.library();

let (code, stdout, stderr) = run(&["apply", "--json", "--offline"]);
let (code, stdout, stderr) = fx.run(&["apply", "--json", "--offline"]);
assert_eq!(code, 0, "apply failed:\n{stdout}\n{stderr}");
assert_eq!(
value(),
fx.value(),
"2",
"Ruby must load the library built from the patched source:\n{stdout}"
);
assert_eq!(fx.c_file(), wrap_c(2));
assert!(!fx.home.join(".socket-patch-build").exists());
let marker: serde_json::Value = serde_json::from_str(
&std::fs::read_to_string(fx.gem_dir.join(".socket-patch-native-build.json")).unwrap(),
)
.unwrap();
assert!(
marker["libArtifacts"][&library].is_string(),
"the build record names the library built in the gem's tree: {marker}"
);
let (code, stdout, stderr) = fx.run(&["apply", "--check"]);
assert_eq!(code, 0, "a rebuilt patch checks clean:\n{stdout}\n{stderr}");
assert!(vex_attests(|args| fx.run(args)));

// A library that is no longer the one that was built is not attested.
let built = std::fs::read(fx.gem_dir.join(&library)).unwrap();
std::fs::write(fx.gem_dir.join(&library), b"not the build").unwrap();
let (code, stdout, stderr) = fx.run(&["apply", "--check"]);
assert_ne!(
code, 0,
"a replaced library must not check clean:\n{stdout}\n{stderr}"
);
assert!(!vex_attests(|args| fx.run(args)));
std::fs::write(fx.gem_dir.join(&library), built).unwrap();

let (code, stdout, stderr) = fx.run(&["rollback", "--json", "--offline"]);
assert_eq!(code, 0, "rollback failed:\n{stdout}\n{stderr}");
assert_eq!(fx.value(), "1", "Ruby must load the original library again");
assert_tree(&fx.gem_dir, &installed, "after rollback");
assert_tree(&fx.home.join("extensions"), &extensions, "after rollback");
}

/// A build that takes the library it finds in the gem's tree for current
/// compiles nothing when it runs over an installed copy. The products the
/// source gem's build was seen to make are removed first, as a fresh
/// install starts without them, so the library is built from the patched
/// source (#2105).
#[test]
#[ignore = "host capstone: compiles a C extension with the real ruby/gem toolchain; \
the unpinned `test` job skips it, the pinned-Ruby e2e leg runs it via --ignored"]
fn a_build_that_keeps_a_stale_library_is_made_to_rebuild_it() {
let Some(abi) = toolchain() else {
return;
};
let fx = WrapGem::new(&abi, STALE_EXTCONF);
assert_eq!(fx.value(), "1");
let installed = walk_files(&fx.gem_dir);

let (code, stdout, stderr) = fx.run(&["apply", "--json", "--offline"]);
assert_eq!(code, 0, "apply failed:\n{stdout}\n{stderr}");
assert_eq!(
std::fs::read_to_string(gem_dir.join(WRAP_C_KEY)).unwrap(),
after
fx.value(),
"2",
"Ruby must load the library built from the patched source:\n{stdout}"
);
assert!(gem_dir.join(".socket-patch-native-build.json").is_file());
assert!(!home.join(".socket-patch-build").exists());
let (code, stdout, stderr) = run(&["apply", "--check"]);
let (code, stdout, stderr) = fx.run(&["apply", "--check"]);
assert_eq!(code, 0, "a rebuilt patch checks clean:\n{stdout}\n{stderr}");
assert!(vex_attests(|args| fx.run(args)));

// (Rollback restores the sources and the extension dir. The library in
// the gem's own tree is no part of what an in-place rebuild backs up,
// here as before the stage existed.)
let (code, stdout, stderr) = run(&["rollback", "--json", "--offline"]);
let (code, stdout, stderr) = fx.run(&["rollback", "--json", "--offline"]);
assert_eq!(code, 0, "rollback failed:\n{stdout}\n{stderr}");
assert_eq!(fx.value(), "1", "Ruby must load the original library again");
assert_tree(&fx.gem_dir, &installed, "after rollback");

// A rollback that has lost the backup rebuilds from the restored
// source, and that build must not take the patched library for
// current either. (The first rollback dropped the record.)
write_record(
&fx.proj.join(".socket"),
WRAP_PURL,
&[(WRAP_C_KEY, Some(&wrap_c(1)), &wrap_c(2))],
);
let (code, stdout, stderr) = fx.run(&["apply", "--json", "--offline"]);
assert_eq!(code, 0, "apply failed:\n{stdout}\n{stderr}");
assert_eq!(fx.value(), "2");
std::fs::remove_dir_all(fx.gem_dir.join(".socket-patch-native-orig")).unwrap();
let (code, stdout, stderr) = fx.run(&["rollback", "--json", "--offline"]);
assert_eq!(code, 0, "rollback failed:\n{stdout}\n{stderr}");
assert_eq!(
std::fs::read_to_string(gem_dir.join(WRAP_C_KEY)).unwrap(),
before
fx.value(),
"1",
"the library is rebuilt from the restored source:\n{stdout}"
);
assert_eq!(fx.c_file(), wrap_c(1));
}

/// Without the source gem nothing says which files the build makes, so
/// none can be removed for it. A build that then exits cleanly and leaves
/// every library as it was has not built the patched source: the patch is
/// refused, the gem is left exactly as it was, and nothing attests it
/// (#2105).
#[test]
#[ignore = "host capstone: compiles a C extension with the real ruby/gem toolchain; \
the unpinned `test` job skips it, the pinned-Ruby e2e leg runs it via --ignored"]
fn a_build_that_compiles_nothing_is_refused_and_not_attested() {
let Some(abi) = toolchain() else {
return;
};
let fx = WrapGem::new(&abi, STALE_EXTCONF);
std::fs::remove_file(fx.home.join("cache/sp_wrap-1.0.0.gem")).unwrap();
let installed = walk_files(&fx.gem_dir);
let extensions = walk_files(&fx.home.join("extensions"));

let (code, stdout, stderr) = fx.run(&["apply", "--json", "--offline"]);
assert_ne!(
code, 0,
"a build that compiled nothing must not pass for a rebuild:\n{stdout}\n{stderr}"
);
let env = parse_json_envelope(&stdout);
let event = common::envelope::event_for(&env, WRAP_PURL);
assert!(
event["error"].as_str().is_some_and(|e| {
e.starts_with("native_extension_build_failed:") && e.contains("compiled nothing")
}),
"the refusal says the build compiled nothing:\n{stdout}"
);
assert_eq!(fx.value(), "1");
assert_eq!(fx.c_file(), wrap_c(1), "the source is put back");
assert_tree(&fx.gem_dir, &installed, "after the refused apply");
assert_tree(
&fx.home.join("extensions"),
&extensions,
"after the refused apply",
);
assert!(!vex_attests(|args| fx.run(args)));
}

/// A precompiled gem ships one binary per Ruby minor, and a graft replaces
Expand Down
Loading
Loading