Skip to content

Commit ed0f3a5

Browse files
committed
review: copy original bytecode.
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 1dd2bc1 commit ed0f3a5

File tree

5 files changed

+12
-18
lines changed

5 files changed

+12
-18
lines changed

src/common/bytecode_util.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ bool BytecodeUtil::getStrippedSource(std::string_view bytecode, std::string &ret
219219
}
220220
}
221221
}
222+
if (ret.empty()) {
223+
// Copy the original source code if it is empty.
224+
ret = std::string(bytecode);
225+
}
222226
return true;
223227
}
224228

src/common/bytecode_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class BytecodeUtil {
6464
/**
6565
* getStrippedSource gets Wasm module without Custom Sections to save some memory in workers.
6666
* @param bytecode is the original bytecode.
67-
* @param ret is the reference to the stripped bytecode.
67+
* @param ret is the reference to the stripped bytecode or a copy of the original bytecode.
6868
* @return indicates whether parsing succeeded or not.
6969
*/
7070
static bool getStrippedSource(std::string_view bytecode, std::string &ret);

src/v8/v8.cc

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,7 @@ bool V8::load(const std::string &code, bool allow_precompiled) {
288288
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
289289
return false;
290290
};
291-
wasm::vec<byte_t> code_vec = wasm::vec<byte_t>::invalid();
292-
if (stripped.empty()) {
293-
// Use the original bytecode.
294-
code_vec = wasm::vec<byte_t>::make(code.size(), (char *)(code.data()));
295-
} else {
296-
// Othewise use the stripped bytecode.
297-
code_vec = wasm::vec<byte_t>::make(stripped.size(), stripped.data());
298-
}
291+
wasm::vec<byte_t> code_vec = wasm::vec<byte_t>::make(stripped.size(), stripped.data());
299292
module_ = wasm::Module::make(store_.get(), code_vec);
300293
}
301294

src/wasmtime/wasmtime.cc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,7 @@ bool Wasmtime::load(const std::string &code, bool allow_precompiled) {
128128
};
129129

130130
WasmByteVec source_vec;
131-
if (stripped_vec.empty()) {
132-
// Use the original bytecode.
133-
wasm_byte_vec_new(source_vec.get(), code.size(), code.data());
134-
} else {
135-
// Othewise pass the stripped source code.
136-
wasm_byte_vec_new(source_vec.get(), stripped_vec.size(), stripped_vec.data());
137-
}
131+
wasm_byte_vec_new(source_vec.get(), stripped_vec.size(), stripped_vec.data());
138132
module_ = wasm_module_new(store_.get(), source_vec.get());
139133

140134
if (module_) {

test/common/bytecode_util_test.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ TEST(TestWasmCommonUtil, getStrippedSource) {
7878
auto source = readTestWasmFile("abi_export.wasm");
7979
std::string actual;
8080
EXPECT_TRUE(BytecodeUtil::getStrippedSource(source, actual));
81-
// No `precompiled_` is found in the custom sections.
82-
EXPECT_TRUE(actual.empty());
81+
// If no `precompiled_` is found in the custom sections,
82+
// then the copy of the original should be returned.
83+
EXPECT_FALSE(actual.empty());
84+
EXPECT_TRUE(actual.data() != source.data());
85+
EXPECT_EQ(actual, source);
8386

8487
// Append "precompiled_test" custom section
8588
std::vector<char> custom_section = {// custom section id

0 commit comments

Comments
 (0)