From affd52885d24026eeaf8bcb43d11eceb79609e24 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 30 Aug 2023 15:21:48 -0700 Subject: [PATCH 1/5] work --- src/wasm-binary.h | 36 +++++++++++++++++++++++------------- src/wasm/wasm-binary.cpp | 35 ++++++++++++++++------------------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 0931a818005..c5b07a679ba 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -1526,34 +1526,44 @@ class WasmBinaryReader { MixedArena& allocator; const std::vector& input; + // Source map debugging support. + std::istream* sourceMap; - struct NextDebugLocation { - uint32_t availablePos; - uint32_t previousPos; - Function::DebugLocation next; - } nextDebugLocation; + // The binary position that the next debug location refers to. That is, this + // is the first item in a source map entry that we have read, but have not + // used yet (we use it when we read the expression at this debug location). + // + // This is set to 0 if we reach the end of the source and there is nothing + // left to read. + size_t nextDebugPos; + + // The debug location (file:line:col) corresponding to |nextDebugOffset|. If + // that location has no debug info, then this contains the info from the + // previous one (because in a source map, these fields are relative to their + // last appearance (we "skip" over a place without debug info). + Function::DebugLocation nextDebugLocation; - // Whether debug info is present next or not in the next debug location. A - // debug location can contain debug info (file:line:col) or it might not. We + // Whether debug info is present on |nextDebugOffset|. As mentioned there, we // need to track this boolean alongside |nextDebugLocation| - that is, we // can't just do something like std::optional or such - as we - // still need to track the values in |next|, as later positions are relative - // to them. That is, if we have line number 100, then no debug info, and then - // line number 500, then when we get to 500 we will see "+400" which is - // relative to the last existing line number (we "skip" over the place without - // debug info). + // still need to track the previous values anyhow (e.g. if we have line number + // 100, then no debug info, and then line number 500, then when we get to 500 + // we will see "+400" which is relative to the last existing line number). bool nextDebugLocationHasDebugInfo; + // Settings. + bool debugInfo = true; bool DWARF = false; bool skipFunctionBodies = false; + // Internal state. + size_t pos = 0; Index startIndex = -1; std::set debugLocation; size_t codeSectionLocation; - std::set seenSections; // All types defined in the type section diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index d6eb3b23504..10971ba46e9 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1639,7 +1639,7 @@ WasmBinaryReader::WasmBinaryReader(Module& wasm, FeatureSet features, const std::vector& input) : wasm(wasm), allocator(wasm.allocator), input(input), - sourceMap(nullptr), nextDebugLocation{0, 0, {0, 0, 0}}, + sourceMap(nullptr), nextDebugPos(0), nextDebugLocation{0, 0, 0}, nextDebugLocationHasDebugInfo(false), debugLocation() { wasm.features = features; } @@ -2798,7 +2798,7 @@ void WasmBinaryReader::readSourceMapHeader() { mustReadChar('\"'); if (maybeReadChar('\"')) { // empty mappings - nextDebugLocation.availablePos = 0; + nextDebugPos = 0; return; } // read first debug location @@ -2811,8 +2811,8 @@ void WasmBinaryReader::readSourceMapHeader() { uint32_t lineNumber = readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number uint32_t columnNumber = readBase64VLQ(*sourceMap); - nextDebugLocation = { - position, position, {fileIndex, lineNumber, columnNumber}}; + nextDebugPos = position; + nextDebugLocation = {fileIndex, lineNumber, columnNumber}; nextDebugLocationHasDebugInfo = true; } @@ -2821,21 +2821,19 @@ void WasmBinaryReader::readNextDebugLocation() { return; } - if (nextDebugLocation.availablePos == 0 && - nextDebugLocation.previousPos <= pos) { - // if source map file had already reached the end and cache position also - // cannot cover the pos clear the debug location + if (nextDebugPos == 0) { + // We reached the end of the source map; nothing left to read. debugLocation.clear(); return; } - while (nextDebugLocation.availablePos && - nextDebugLocation.availablePos <= pos) { + while (nextDebugPos && + nextDebugPos <= pos) { debugLocation.clear(); // use debugLocation only for function expressions if (currFunction) { if (nextDebugLocationHasDebugInfo) { - debugLocation.insert(nextDebugLocation.next); + debugLocation.insert(nextDebugLocation); } else { debugLocation.clear(); } @@ -2844,7 +2842,7 @@ void WasmBinaryReader::readNextDebugLocation() { char ch; *sourceMap >> ch; if (ch == '\"') { // end of records - nextDebugLocation.availablePos = 0; + nextDebugPos = 0; break; } if (ch != ',') { @@ -2852,10 +2850,9 @@ void WasmBinaryReader::readNextDebugLocation() { } int32_t positionDelta = readBase64VLQ(*sourceMap); - uint32_t position = nextDebugLocation.availablePos + positionDelta; + uint32_t position = nextDebugPos + positionDelta; - nextDebugLocation.previousPos = nextDebugLocation.availablePos; - nextDebugLocation.availablePos = position; + nextDebugPos = position; auto peek = sourceMap->peek(); if (peek == ',' || peek == '\"') { @@ -2865,14 +2862,14 @@ void WasmBinaryReader::readNextDebugLocation() { } int32_t fileIndexDelta = readBase64VLQ(*sourceMap); - uint32_t fileIndex = nextDebugLocation.next.fileIndex + fileIndexDelta; + uint32_t fileIndex = nextDebugLocation.fileIndex + fileIndexDelta; int32_t lineNumberDelta = readBase64VLQ(*sourceMap); - uint32_t lineNumber = nextDebugLocation.next.lineNumber + lineNumberDelta; + uint32_t lineNumber = nextDebugLocation.lineNumber + lineNumberDelta; int32_t columnNumberDelta = readBase64VLQ(*sourceMap); uint32_t columnNumber = - nextDebugLocation.next.columnNumber + columnNumberDelta; + nextDebugLocation.columnNumber + columnNumberDelta; - nextDebugLocation.next = {fileIndex, lineNumber, columnNumber}; + nextDebugLocation = {fileIndex, lineNumber, columnNumber}; nextDebugLocationHasDebugInfo = true; } } From 881173e98c516e6753db73ab5dfce73089b537c2 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 30 Aug 2023 15:22:00 -0700 Subject: [PATCH 2/5] format --- src/wasm/wasm-binary.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 10971ba46e9..9090253fd21 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1638,8 +1638,8 @@ void WasmBinaryWriter::writeField(const Field& field) { WasmBinaryReader::WasmBinaryReader(Module& wasm, FeatureSet features, const std::vector& input) - : wasm(wasm), allocator(wasm.allocator), input(input), - sourceMap(nullptr), nextDebugPos(0), nextDebugLocation{0, 0, 0}, + : wasm(wasm), allocator(wasm.allocator), input(input), sourceMap(nullptr), + nextDebugPos(0), nextDebugLocation{0, 0, 0}, nextDebugLocationHasDebugInfo(false), debugLocation() { wasm.features = features; } @@ -2827,8 +2827,7 @@ void WasmBinaryReader::readNextDebugLocation() { return; } - while (nextDebugPos && - nextDebugPos <= pos) { + while (nextDebugPos && nextDebugPos <= pos) { debugLocation.clear(); // use debugLocation only for function expressions if (currFunction) { @@ -2866,8 +2865,7 @@ void WasmBinaryReader::readNextDebugLocation() { int32_t lineNumberDelta = readBase64VLQ(*sourceMap); uint32_t lineNumber = nextDebugLocation.lineNumber + lineNumberDelta; int32_t columnNumberDelta = readBase64VLQ(*sourceMap); - uint32_t columnNumber = - nextDebugLocation.columnNumber + columnNumberDelta; + uint32_t columnNumber = nextDebugLocation.columnNumber + columnNumberDelta; nextDebugLocation = {fileIndex, lineNumber, columnNumber}; nextDebugLocationHasDebugInfo = true; From a8c09d2e1ad9eff26f498642729dc3eb6da91e68 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 30 Aug 2023 15:33:24 -0700 Subject: [PATCH 3/5] better --- src/wasm-binary.h | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/wasm-binary.h b/src/wasm-binary.h index c5b07a679ba..94543830aeb 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -1531,25 +1531,29 @@ class WasmBinaryReader { std::istream* sourceMap; // The binary position that the next debug location refers to. That is, this - // is the first item in a source map entry that we have read, but have not - // used yet (we use it when we read the expression at this debug location). + // is the first item in a source map entry that we have read (the "column", in + // source map terms, which for wasm means the offset in the binary). We have + // read this entry, but have not used it yet (we use it when we read the + // expression at this binary offset). // - // This is set to 0 if we reach the end of the source and there is nothing - // left to read. + // This is set to 0 as an invalid value if we reach the end of the source map + // and there is nothing left to read. size_t nextDebugPos; - // The debug location (file:line:col) corresponding to |nextDebugOffset|. If - // that location has no debug info, then this contains the info from the - // previous one (because in a source map, these fields are relative to their - // last appearance (we "skip" over a place without debug info). + // The debug location (file:line:col) corresponding to |nextDebugOffset|. That + // is, this is the next 3 fields in a source map entry that we have read, but + // not used yet. + // + // If that location has no debug info (it lacks those 3 fields), then this + // contains the info from the previous one, because in a source map, these + // fields are relative to their last appearance, so we cannot forget them (we + // can't just do something like std::optional or such); for + // example, if we have line number 100, then no debug info, and then line + // number 500, then when we get to 500 we will see "+400" which is relative to + // the last existing line number (we "skip" over a place without debug info). Function::DebugLocation nextDebugLocation; - // Whether debug info is present on |nextDebugOffset|. As mentioned there, we - // need to track this boolean alongside |nextDebugLocation| - that is, we - // can't just do something like std::optional or such - as we - // still need to track the previous values anyhow (e.g. if we have line number - // 100, then no debug info, and then line number 500, then when we get to 500 - // we will see "+400" which is relative to the last existing line number). + // Whether debug info is present on |nextDebugOffset| (see comment there). bool nextDebugLocationHasDebugInfo; // Settings. From 60f380d41905ba3b72c0bdba3f3f5ec97ccfd2a2 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 5 Sep 2023 12:40:31 -0700 Subject: [PATCH 4/5] Update src/wasm-binary.h Co-authored-by: Heejin Ahn --- src/wasm-binary.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 94543830aeb..c28ce7f20a7 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -1540,7 +1540,7 @@ class WasmBinaryReader { // and there is nothing left to read. size_t nextDebugPos; - // The debug location (file:line:col) corresponding to |nextDebugOffset|. That + // The debug location (file:line:col) corresponding to |nextDebugPos|. That // is, this is the next 3 fields in a source map entry that we have read, but // not used yet. // From ecea6597eefdffe2dbd527d3df6cab841ad82b89 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 5 Sep 2023 12:40:39 -0700 Subject: [PATCH 5/5] Update src/wasm-binary.h Co-authored-by: Heejin Ahn --- src/wasm-binary.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasm-binary.h b/src/wasm-binary.h index c28ce7f20a7..0ad24a459ce 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -1553,7 +1553,7 @@ class WasmBinaryReader { // the last existing line number (we "skip" over a place without debug info). Function::DebugLocation nextDebugLocation; - // Whether debug info is present on |nextDebugOffset| (see comment there). + // Whether debug info is present on |nextDebugPos| (see comment there). bool nextDebugLocationHasDebugInfo; // Settings.