Skip to content

Commit 40fd3d0

Browse files
authored
Strip only Custom Sections with precompiled Wasm modules. (proxy-wasm#81)
Signed-off-by: Piotr Sikora <[email protected]>
1 parent 8d0727f commit 40fd3d0

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/v8/v8.cc

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,28 @@ wasm::vec<byte_t> V8::getStrippedSource() {
296296
if (section_len == static_cast<uint32_t>(-1) || pos + section_len > end) {
297297
return wasm::vec<byte_t>::invalid();
298298
}
299-
pos += section_len;
300299
if (section_type == 0 /* custom section */) {
301-
if (stripped.empty()) {
302-
const byte_t *start = source_.get();
303-
stripped.insert(stripped.end(), start, section_start);
300+
const auto section_data_start = pos;
301+
const auto section_name_len = parseVarint(pos, end);
302+
if (section_name_len == static_cast<uint32_t>(-1) || pos + section_name_len > end) {
303+
return wasm::vec<byte_t>::invalid();
304+
}
305+
auto section_name = std::string_view(pos, section_name_len);
306+
if (section_name.find("precompiled_") != std::string::npos) {
307+
// If this is the first "precompiled_" section, then save everything
308+
// before it, otherwise skip it.
309+
if (stripped.empty()) {
310+
const byte_t *start = source_.get();
311+
stripped.insert(stripped.end(), start, section_start);
312+
}
313+
}
314+
pos = section_data_start + section_len;
315+
} else {
316+
pos += section_len;
317+
// Save this section if we already saw a custom "precompiled_" section.
318+
if (!stripped.empty()) {
319+
stripped.insert(stripped.end(), section_start, pos /* section end */);
304320
}
305-
} else if (!stripped.empty()) {
306-
stripped.insert(stripped.end(), section_start, pos /* section end */);
307321
}
308322
}
309323

0 commit comments

Comments
 (0)