Skip to content

Commit

Permalink
skip headers
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed May 5, 2024
1 parent d0db859 commit 34bd363
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ home = "0.5.9"
wasm-encoder = "0.202.0"
rustc_version_runtime = "0.3.0"

leb128 = "0.2.5"

# For hyper-tls
[target.'cfg(unix)'.dependencies]
openssl = { version = "=0.10.55", features = ["vendored"] }
Expand Down
19 changes: 17 additions & 2 deletions cmd/soroban-cli/src/commands/contract/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ impl Cmd {
let mut wasm = read_wasm_without_contractmeta(&contract_buf)?;
custom_section.append_to(&mut wasm);

let backup_path = target_file_path.with_extension("back.wasm");
fs::copy(&target_file_path, backup_path).map_err(Error::CopyingWasmFile)?;

let temp_file = format!("{}.{}.temp", target_file, rand::random::<u32>());
let temp_file_path = file_path.join(temp_file);

Expand Down Expand Up @@ -296,14 +299,17 @@ fn read_wasm_without_contractmeta(contract_buf: &[u8]) -> Result<Vec<u8>, Error>

println!("read_wasm_without_contractmeta =======================");
for payload in WasmParser::new(0).parse_all(contract_buf) {
match payload.map_err(Error::ParsingWasm)? {
let payload = payload.map_err(Error::ParsingWasm)?;
match payload {
Payload::CustomSection(s) => match s.name() {
"contractmetav0" => {
let range = s.range();
let data_offset = s.data_offset();
let section_header_size = calc_section_header_size(&range);
println!("data_offset: {data_offset}, range: {:?}", range);
assert!(range.start >= section_header_size);
if range.start > 0 {
module.extend_from_slice(&contract_buf[0..range.start]);
module.extend_from_slice(&contract_buf[0..(range.start - section_header_size)]);
}
if range.end < buf_len {
module.extend_from_slice(&contract_buf[range.end..buf_len]);
Expand All @@ -317,3 +323,12 @@ fn read_wasm_without_contractmeta(contract_buf: &[u8]) -> Result<Vec<u8>, Error>
module.shrink_to_fit();
Ok(module)
}

fn calc_section_header_size(range: &std::ops::Range<usize>) -> usize {
let len = range.end - range.start;
let mut buf = Vec::new();
let int_enc_size = leb128::write::unsigned(&mut buf, len as u64);
let int_enc_size = int_enc_size.expect("leb128 write");
let section_id_byte = 1;
int_enc_size + section_id_byte
}

0 comments on commit 34bd363

Please sign in to comment.