Skip to content

Strip the producers section in --strip #1875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 31, 2019
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
34 changes: 24 additions & 10 deletions src/passes/Strip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/

//
// Similar to strip-ing a native binary, this removes debug info
// and related things like source map URLs, names section, etc.
// Similar to strip-ing a native binary, this family of passes can
// removes debug info and other things.
//

#include <functional>

#include "wasm.h"
#include "wasm-binary.h"
#include "pass.h"
Expand All @@ -28,19 +30,20 @@ using namespace std;
namespace wasm {

struct Strip : public Pass {
// A function that returns true if the method should be removed.
typedef std::function<bool (UserSection&)> Decider;
Decider decider;

Strip(Decider decider) : decider(decider) {}

void run(PassRunner* runner, Module* module) override {
// Remove name and debug sections.
auto& sections = module->userSections;
sections.erase(
std::remove_if(
sections.begin(),
sections.end(),
[&](const UserSection& curr) {
return curr.name == BinaryConsts::UserSections::Name ||
curr.name == BinaryConsts::UserSections::SourceMapUrl ||
curr.name.find(".debug") == 0 ||
curr.name.find("reloc..debug") == 0;
}
decider
),
sections.end()
);
Expand All @@ -53,8 +56,19 @@ struct Strip : public Pass {
}
};

Pass *createStripPass() {
return new Strip();
Pass *createStripDebugPass() {
return new Strip([&](const UserSection& curr) {
return curr.name == BinaryConsts::UserSections::Name ||
curr.name == BinaryConsts::UserSections::SourceMapUrl ||
curr.name.find(".debug") == 0 ||
curr.name.find("reloc..debug") == 0;
});
}

Pass *createStripProducersPass() {
return new Strip([&](const UserSection& curr) {
return curr.name == BinaryConsts::UserSections::Producers;
});
}

} // namespace wasm
4 changes: 3 additions & 1 deletion src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ void PassRegistry::registerPasses() {
registerPass("souperify-single-use", "emit Souper IR in text form (single-use nodes only)", createSouperifySingleUsePass);
registerPass("spill-pointers", "spill pointers to the C stack (useful for Boehm-style GC)", createSpillPointersPass);
registerPass("ssa", "ssa-ify variables so that they have a single assignment", createSSAifyPass);
registerPass("strip", "strip debug info (including the names section)", createStripPass);
registerPass("strip", "deprecated; same as strip-debug", createStripDebugPass);
registerPass("strip-debug", "strip debug info (including the names section)", createStripDebugPass);
registerPass("strip-producers", "strip the wasm producers section", createStripProducersPass);
registerPass("trap-mode-clamp", "replace trapping operations with clamping semantics", createTrapModeClamp);
registerPass("trap-mode-js", "replace trapping operations with js semantics", createTrapModeJS);
registerPass("untee", "removes local.tees, replacing them with sets and gets", createUnteePass);
Expand Down
3 changes: 2 additions & 1 deletion src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ Pass* createSimplifyLocalsNoNestingPass();
Pass* createSimplifyLocalsNoTeePass();
Pass* createSimplifyLocalsNoStructurePass();
Pass* createSimplifyLocalsNoTeeNoStructurePass();
Pass* createStripPass();
Pass* createStripDebugPass();
Pass* createStripProducersPass();
Pass* createSouperifyPass();
Pass* createSouperifySingleUsePass();
Pass* createSpillPointersPass();
Expand Down
1 change: 1 addition & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ extern const char* Name;
extern const char* SourceMapUrl;
extern const char* Dylink;
extern const char* Linking;
extern const char* Producers;

enum Subsection {
NameFunction = 1,
Expand Down
2 changes: 1 addition & 1 deletion src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
} else {
// an unfamiliar custom section
if (sectionName.equals(BinaryConsts::UserSections::Linking)) {
std::cerr << "warning: linking section is present, which binaryen cannot handle yet - relocations will be invalidated!\n";
std::cerr << "warning: linking section is present, so this is not a standard wasm file - binaryen cannot handle this properly!\n";
}
wasm.userSections.resize(wasm.userSections.size() + 1);
auto& section = wasm.userSections.back();
Expand Down
1 change: 1 addition & 0 deletions src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const char* Name = "name";
const char* SourceMapUrl = "sourceMappingURL";
const char* Dylink = "dylink";
const char* Linking = "linking";
const char* Producers = "producers";
}
}

Expand Down
Binary file added test/metadatas.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions test/metadatas.wasm.fromBinary
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(module
(type $0 (func))
(export "a" (func $0))
(func $0 (; 0 ;) (type $0)
(nop)
)
;; custom section "emscripten_metadata", size 7
;; custom section "producers", size 187
)

17 changes: 17 additions & 0 deletions test/passes/metrics_strip-debug_metrics.bin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
total
[funcs] : 1
[total] : 1
nop : 1
total
[funcs] : 1
[total] : 1
nop : 1
(module
(type $0 (func))
(export "a" (func $0))
(func $0 (; 0 ;) (type $0)
(nop)
)
;; custom section "emscripten_metadata", size 7
;; custom section "producers", size 187
)
Binary file added test/passes/metrics_strip-debug_metrics.wasm
Binary file not shown.
16 changes: 16 additions & 0 deletions test/passes/metrics_strip-producers_metrics.bin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
total
[funcs] : 1
[total] : 1
nop : 1
total
[funcs] : 1
[total] : 1
nop : 1
(module
(type $0 (func))
(export "a" (func $0))
(func $0 (; 0 ;) (type $0)
(nop)
)
;; custom section "emscripten_metadata", size 7
)
Binary file added test/passes/metrics_strip-producers_metrics.wasm
Binary file not shown.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions test/passes/strip-producers.bin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(module
(type $0 (func (result i32)))
(import "env" "__linear_memory" (memory $0 0))
(import "env" "__indirect_function_table" (table $timport$1 0 funcref))
(func $0 (; 0 ;) (type $0) (result i32)
(local $0 i32)
(local.set $0
(i32.const 1)
)
(return
(local.get $0)
)
)
;; custom section ".debug_str", size 246
;; custom section ".debug_abbrev", size 52
;; custom section ".debug_info", size 69
;; custom section ".debug_macinfo", size 1
;; custom section ".debug_line", size 56
;; custom section "zinking", size 28
;; custom section "reloc..debug_info", size 47
;; custom section "reloc..debug_line", size 6
)
Binary file added test/passes/strip-producers.wasm
Binary file not shown.