Bug 1329019 - Baldr: Only compute bytecode hash in debug mode (r=yury)
authorLuke Wagner <luke@mozilla.com>
Fri, 22 Sep 2017 11:05:05 -0500
changeset 433263 1f23975f1d1f2416955226d1cfcfb87c5702329c
parent 433262 fd601c9bcd000f7352079cebde25c508cc62a56d
child 433264 30f8a995d4c1949e8763ddf52e2c971e1e6ba1e8
push id8114
push userjlorenzo@mozilla.com
push dateThu, 02 Nov 2017 16:33:21 +0000
treeherdermozilla-beta@73e0d89a540f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersyury
bugs1329019
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1329019 - Baldr: Only compute bytecode hash in debug mode (r=yury) MozReview-Commit-ID: GKxQzBznHJh
js/src/jit-test/tests/debug/wasm-12.js
js/src/wasm/WasmCode.cpp
js/src/wasm/WasmCode.h
js/src/wasm/WasmDebug.cpp
js/src/wasm/WasmGenerator.cpp
--- a/js/src/jit-test/tests/debug/wasm-12.js
+++ b/js/src/jit-test/tests/debug/wasm-12.js
@@ -1,27 +1,27 @@
 // |jit-test| test-also-no-wasm-baseline
 // Tests that wasm module scripts have special URLs.
 
 if (!wasmDebuggingIsSupported())
   quit();
 
 var g = newGlobal();
+var dbg = new Debugger(g);
 g.eval(`
 function initWasm(s) { return new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(s))); }
 o1 = initWasm('(module (func) (export "" 0))');
 o2 = initWasm('(module (func) (func) (export "" 1))');
 `);
 
 function isWasm(script) { return script.format === "wasm"; }
 
 function isValidWasmURL(url) {
    // The URLs will have the following format:
    //   wasm: [<uri-encoded-filename-of-host> ":"] <64-bit-hash>
    return /^wasm:(?:[^:]*:)*?[0-9a-f]{16}$/.test(url);
 }
 
-var dbg = new Debugger(g);
 var foundScripts = dbg.findScripts().filter(isWasm);
 assertEq(foundScripts.length, 2);
 assertEq(isValidWasmURL(foundScripts[0].source.url), true);
 assertEq(isValidWasmURL(foundScripts[1].source.url), true);
 assertEq(foundScripts[0].source.url != foundScripts[1].source.url, true);
--- a/js/src/wasm/WasmCode.cpp
+++ b/js/src/wasm/WasmCode.cpp
@@ -554,18 +554,17 @@ Metadata::serializedSize() const
 {
     return sizeof(pod()) +
            metadata(Tier::Serialized).serializedSize() +
            SerializedVectorSize(sigIds) +
            SerializedPodVectorSize(globals) +
            SerializedPodVectorSize(tables) +
            SerializedPodVectorSize(funcNames) +
            SerializedPodVectorSize(customSections) +
-           filename.serializedSize() +
-           sizeof(hash);
+           filename.serializedSize();
 }
 
 size_t
 Metadata::sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const
 {
     size_t sum = 0;
 
     for (auto t : tiers())
@@ -587,32 +586,30 @@ Metadata::serialize(uint8_t* cursor) con
     cursor = WriteBytes(cursor, &pod(), sizeof(pod()));
     cursor = metadata(Tier::Serialized).serialize(cursor);
     cursor = SerializeVector(cursor, sigIds);
     cursor = SerializePodVector(cursor, globals);
     cursor = SerializePodVector(cursor, tables);
     cursor = SerializePodVector(cursor, funcNames);
     cursor = SerializePodVector(cursor, customSections);
     cursor = filename.serialize(cursor);
-    cursor = WriteBytes(cursor, hash, sizeof(hash));
     return cursor;
 }
 
 /* static */ const uint8_t*
 Metadata::deserialize(const uint8_t* cursor)
 {
     (cursor = ReadBytes(cursor, &pod(), sizeof(pod()))) &&
     (cursor = metadata(Tier::Serialized).deserialize(cursor)) &&
     (cursor = DeserializeVector(cursor, &sigIds)) &&
     (cursor = DeserializePodVector(cursor, &globals)) &&
     (cursor = DeserializePodVector(cursor, &tables)) &&
     (cursor = DeserializePodVector(cursor, &funcNames)) &&
     (cursor = DeserializePodVector(cursor, &customSections)) &&
-    (cursor = filename.deserialize(cursor)) &&
-    (cursor = ReadBytes(cursor, hash, sizeof(hash)));
+    (cursor = filename.deserialize(cursor));
     debugEnabled = false;
     debugFuncArgTypes.clear();
     debugFuncReturnTypes.clear();
     return cursor;
 }
 
 struct ProjectFuncIndex
 {
--- a/js/src/wasm/WasmCode.h
+++ b/js/src/wasm/WasmCode.h
@@ -374,17 +374,18 @@ class Metadata : public ShareableBase<Me
     UniqueMetadataTier         metadata1_;
     mutable UniqueMetadataTier metadata2_;  // Access only when hasTier2() is true
     mutable Atomic<bool>       hasTier2_;
 
   public:
     explicit Metadata(UniqueMetadataTier tier, ModuleKind kind = ModuleKind::Wasm)
       : MetadataCacheablePod(kind),
         metadata1_(Move(tier)),
-        debugEnabled(false)
+        debugEnabled(false),
+        debugHash()
     {}
     virtual ~Metadata() {}
 
     MetadataCacheablePod& pod() { return *this; }
     const MetadataCacheablePod& pod() const { return *this; }
 
     void commitTier2() const;
     bool hasTier2() const { return hasTier2_; }
@@ -401,22 +402,22 @@ class Metadata : public ShareableBase<Me
     }
 
     SigWithIdVector       sigIds;
     GlobalDescVector      globals;
     TableDescVector       tables;
     NameInBytecodeVector  funcNames;
     CustomSectionVector   customSections;
     CacheableChars        filename;
-    ModuleHash            hash;
 
     // Debug-enabled code is not serialized.
     bool                  debugEnabled;
     FuncArgTypesVector    debugFuncArgTypes;
     FuncReturnTypesVector debugFuncReturnTypes;
+    ModuleHash            debugHash;
 
     bool usesMemory() const { return UsesMemory(memoryUsage); }
     bool hasSharedMemory() const { return memoryUsage == MemoryUsage::Shared; }
 
     // AsmJSMetadata derives Metadata iff isAsmJS(). Mostly this distinction is
     // encapsulated within AsmJS.cpp, but the additional virtual functions allow
     // asm.js to override wasm behavior in the handful of cases that can't be
     // easily encapsulated by AsmJS.cpp.
--- a/js/src/wasm/WasmDebug.cpp
+++ b/js/src/wasm/WasmDebug.cpp
@@ -607,40 +607,48 @@ DebugState::getGlobal(Instance& instance
 
 JSString*
 DebugState::debugDisplayURL(JSContext* cx) const
 {
     // Build wasm module URL from following parts:
     // - "wasm:" as protocol;
     // - URI encoded filename from metadata (if can be encoded), plus ":";
     // - 64-bit hash of the module bytes (as hex dump).
+
     js::StringBuffer result(cx);
     if (!result.append("wasm:"))
         return nullptr;
+
     if (const char* filename = metadata().filename.get()) {
         js::StringBuffer filenamePrefix(cx);
         // EncodeURI returns false due to invalid chars or OOM -- fail only
         // during OOM.
         if (!EncodeURI(cx, filenamePrefix, filename, strlen(filename))) {
             if (!cx->isExceptionPending())
                 return nullptr;
             cx->clearPendingException(); // ignore invalid URI
-        } else if (!result.append(filenamePrefix.finishString()) || !result.append(":")) {
+        } else if (!result.append(filenamePrefix.finishString())) {
             return nullptr;
         }
     }
 
-    const ModuleHash& hash = metadata().hash;
-    for (size_t i = 0; i < sizeof(ModuleHash); i++) {
-        char digit1 = hash[i] / 16, digit2 = hash[i] % 16;
-        if (!result.append((char)(digit1 < 10 ? digit1 + '0' : digit1 + 'a' - 10)))
+    if (metadata().debugEnabled) {
+        if (!result.append(":"))
             return nullptr;
-        if (!result.append((char)(digit2 < 10 ? digit2 + '0' : digit2 + 'a' - 10)))
-            return nullptr;
+
+        const ModuleHash& hash = metadata().debugHash;
+        for (size_t i = 0; i < sizeof(ModuleHash); i++) {
+            char digit1 = hash[i] / 16, digit2 = hash[i] % 16;
+            if (!result.append((char)(digit1 < 10 ? digit1 + '0' : digit1 + 'a' - 10)))
+                return nullptr;
+            if (!result.append((char)(digit2 < 10 ? digit2 + '0' : digit2 + 'a' - 10)))
+                return nullptr;
+        }
     }
+
     return result.finishString();
 }
 
 bool
 DebugState::getSourceMappingURL(JSContext* cx, MutableHandleString result) const
 {
     result.set(nullptr);
     if (!maybeBytecode_)
--- a/js/src/wasm/WasmGenerator.cpp
+++ b/js/src/wasm/WasmGenerator.cpp
@@ -1095,21 +1095,24 @@ ModuleGenerator::initSigTableElems(uint3
 }
 
 static_assert(sizeof(ModuleHash) <= sizeof(mozilla::SHA1Sum::Hash),
               "The ModuleHash size shall not exceed the SHA1 hash size.");
 
 void
 ModuleGenerator::generateBytecodeHash(const ShareableBytes& bytecode)
 {
+    if (!env_->debugEnabled())
+        return;
+
     mozilla::SHA1Sum::Hash hash;
     mozilla::SHA1Sum sha1Sum;
     sha1Sum.update(bytecode.begin(), bytecode.length());
     sha1Sum.finish(hash);
-    memcpy(metadata_->hash, hash, sizeof(ModuleHash));
+    memcpy(metadata_->debugHash, hash, sizeof(ModuleHash));
 }
 
 bool
 ModuleGenerator::finishMetadata(const ShareableBytes& bytecode)
 {
     // Convert the CallSiteAndTargetVector (needed during generation) to a
     // CallSiteVector (what is stored in the Module).
     if (!metadataTier_->callSites.appendAll(masm_.callSites()))