Skip to content
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

[lldb] Support DW_OP_WASM_location in DWARFExpression #78977

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

paolosevMSFT
Copy link
Contributor

Add support for DW_OP_WASM_location in DWARFExpression.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 22, 2024

@llvm/pr-subscribers-lldb

Author: Paolo Severini (paolosevMSFT)

Changes

Add support for DW_OP_WASM_location in DWARFExpression.


Full diff: https://github.com/llvm/llvm-project/pull/78977.diff

1 Files Affected:

  • (modified) lldb/source/Expression/DWARFExpression.cpp (+41)
diff --git a/lldb/source/Expression/DWARFExpression.cpp b/lldb/source/Expression/DWARFExpression.cpp
index fe4928d4f43a434..95033db5ed8f5a1 100644
--- a/lldb/source/Expression/DWARFExpression.cpp
+++ b/lldb/source/Expression/DWARFExpression.cpp
@@ -346,6 +346,16 @@ static offset_t GetOpcodeDataSize(const DataExtractor &data,
     return (offset - data_offset) + subexpr_len;
   }
 
+  case DW_OP_WASM_location: {
+    uint8_t wasm_op = data.GetU8(&offset);
+    if (wasm_op == 3) {
+      data.GetU32(&offset);
+    } else {
+      data.GetULEB128(&offset);
+    }
+    return offset - data_offset;
+  }
+
   default:
     if (!dwarf_cu) {
       return LLDB_INVALID_OFFSET;
@@ -2595,6 +2605,37 @@ bool DWARFExpression::Evaluate(
       break;
     }
 
+    case DW_OP_WASM_location: {
+      uint8_t wasm_op = opcodes.GetU8(&offset);
+      uint32_t index;
+
+      /* LLDB doesn't have an address space to represents WebAssembly locals,
+       * globals and operand stacks.
+       * We encode these elements into virtual registers:
+       *   | tag: 2 bits | index: 30 bits |
+       *   where tag is:
+       *    0: Not a WebAssembly location
+       *    1: Local
+       *    2: Global
+       *    3: Operand stack value
+       */
+      if (wasm_op == 3) {
+        index = opcodes.GetU32(&offset);
+        wasm_op = 2; // Global
+      } else {
+        index = opcodes.GetULEB128(&offset);
+      }
+
+      reg_num = (((wasm_op + 1) & 0x03) << 30) | (index & 0x3fffffff);
+
+      if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
+        stack.push_back(tmp);
+      else
+        return false;
+
+      break;
+    }
+
     default:
       if (dwarf_cu) {
         if (dwarf_cu->GetSymbolFileDWARF().ParseVendorDWARFOpcode(

@JDevlieghere
Copy link
Member

Is it possible to write a test for this in DWARFExpressionTest.cpp?

Copy link

github-actions bot commented Jan 29, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@paolosevMSFT
Copy link
Contributor Author

Adding unit tests turned out to be a little complicated.
There were already a couple of unit tests in file lldb/unittests/Expression/DWARFExpressionTest.cpp that actually were failing with these changes, so I replaced them.
From the tests I learned that the vendor-specific DWARF opcode DW_OP_WASM_location should be handled in plugin code and not in DWARFExpression.cpp.
Not sure what to do with the old tests, I don't think a DW_OP_WASM_location might ever be located in an ELF file, given that with "split symbols" Wasm uses the Wasm file format both for the file with the code and for the file with symbols. (Tested with emscripten).

@paolosevMSFT
Copy link
Contributor Author

Hi @JDevlieghere, could you take a look at the latest changes and unit tests?

@xujuntwt95329
Copy link
Contributor

Hi @JDevlieghere Could you please give us some suggestions about the next steps for this PR and #77949?

Thanks a lot!

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delay. LGTM!

@paolosevMSFT
Copy link
Contributor Author

Apologies for the delay. LGTM!

Thanks! I have not merged any changes in several years and I am not very familiar with the new process, now that llvm_project has moved to Github.
I have read https://llvm.org/docs/MyFirstTypoFix.html#myfirsttypofix-issues-after-landing-your-pr and if I am not wrong there is no safe gating on PR merge commits?
How do I make sure all tests pass in all platforms, before merging?

@tritao
Copy link
Contributor

tritao commented Oct 23, 2024

Given this was already approved, what's missing for the merge?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants