Repository: nodejs/node
Pull request: [v25.x] deps: V8: backport 209d2db9e24a

This PR fixes JIT compilation error and disassembling error when enabling the RISC-V C extension in V8.

This is only need in v25.x, the v8 version on the main branch already contains the fix.

Original commit message:

    [riscv] Fix compilation error and disassembling error when enabling the RISC-V C extension

    Change-Id: I34a930f7bcda514698ce64d132cbe05fa32b323c
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6934163
    Reviewed-by: Yahan Lu (LuYahan) <yahan@iscas.ac.cn>
    Reviewed-by: Kasper Lund <kasperl@rivosinc.com>
    Commit-Queue: Yahan Lu (LuYahan) <yahan@iscas.ac.cn>
    Cr-Commit-Position: refs/heads/main@{#102431}

Refs: https://github.com/v8/v8/commit/209d2db9e24a744e3a5bf924e4d5176da0d6be94

<!--
Before submitting a pull request, please read:

- the CONTRIBUTING guide at https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md
- the commit message formatting guidelines at
  https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

For code changes:
1. Include tests for any bug fixes or new features.
2. Update documentation if relevant.
3. Ensure that `make -j4 test` (UNIX), or `vcbuild test` (Windows) passes.

If you believe this PR should be highlighted in the Node.js CHANGELOG
please add the `notable-change` label.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
-->

Thanks for @luyahan for pointing out the fix and @RevySR for verifying it on node v25.2.1!

Changed files:
filename: common.gypi
patch: @@ -38,7 +38,7 @@
 
     # Reset this number to 0 on major V8 upgrades.
     # Increment by one for each non-official patch applied to deps/v8.
-    'v8_embedder_string': '-node.15',
+    'v8_embedder_string': '-node.16',
 
     ##### V8 defaults for Node.js #####
 

filename: deps/v8/src/codegen/constant-pool.cc
patch: @@ -554,11 +554,22 @@ void ConstantPool::EmitAndClear(Jump require_jump) {
   EmitPrologue(require_alignment);
   if (require_alignment == Alignment::kRequired) assm_->DataAlign(kInt64Size);
   EmitEntries();
+  // Emit padding data to ensure the constant pool size matches the expected
+  // constant count during disassembly.
+  if (v8_flags.riscv_c_extension) {
+    int code_size = assm_->SizeOfCodeGeneratedSince(&size_check);
+    DCHECK_LE(code_size, size);
+
+    while (code_size < size) {
+      assm_->db(0xcc);
+      code_size++;
+    }
+  }
   assm_->RecordComment("]");
   assm_->bind(&after_pool);
   DEBUG_PRINTF("\tConstant Pool end\n")
 
-  DCHECK_LE(assm_->SizeOfCodeGeneratedSince(&size_check) - size, 3);
+  DCHECK_EQ(size, assm_->SizeOfCodeGeneratedSince(&size_check));
   Clear();
 }
 
@@ -666,17 +677,30 @@ bool ConstantPool::ShouldEmitNow(Jump require_jump, size_t margin) const {
 int ConstantPool::ComputeSize(Jump require_jump,
                               Alignment require_alignment) const {
   int size_up_to_marker = PrologueSize(require_jump);
-  int alignment = require_alignment == Alignment::kRequired ? kInstrSize : 0;
+  // With RVC enabled, constant pool alignment must use kInt64Size to ensure
+  // sufficient padding space for 8-byte alignment; otherwise, alignment may
+  // fail.
+  //
+  // Example:
+  //   pc_offset = 0x22
+  //   Aligned(0x22, kInt64Size) = 0x28 → 6 bytes of padding needed.
+  int alignment = require_alignment == Alignment::kRequired
+                      ? (v8_flags.riscv_c_extension ? kInt64Size : kInstrSize)
+                      : 0;
   size_t size_after_marker =
       Entry32Count() * kInt32Size + alignment + Entry64Count() * kInt64Size;
   return size_up_to_marker + static_cast<int>(size_after_marker);
 }
 
 Alignment ConstantPool::IsAlignmentRequiredIfEmittedAt(Jump require_jump,
                                                        int pc_offset) const {
+  // When the RVC extension is enabled, constant pool entries must be aligned to
+  // kInstrSize to prevent unaligned 32-bit memory accesses.
   int size_up_to_marker = PrologueSize(require_jump);
-  if (Entry64Count() != 0 &&
-      !IsAligned(pc_offset + size_up_to_marker, kInt64Size)) {
+  if ((Entry64Count() != 0 &&
+       !IsAligned(pc_offset + size_up_to_marker, kInt64Size)) ||
+      (Entry32Count() != 0 && v8_flags.riscv_c_extension &&
+       !IsAligned(pc_offset + size_up_to_marker, kInstrSize))) {
     return Alignment::kRequired;
   }
   return Alignment::kOmitted;

filename: deps/v8/src/codegen/riscv/macro-assembler-riscv.cc
patch: @@ -5370,24 +5370,22 @@ void MacroAssembler::StoreReturnAddressAndCall(Register target) {
   // trigger GC, since the callee function will return to it.
 
   Assembler::BlockTrampolinePoolScope block_trampoline_pool(this);
-  int kNumInstructions = v8_flags.riscv_c_extension ? 5 : 6;
-  Label start;
+  Label start, end;
 
   // Make 'ra' point to the correct return location, just after the 'jalr t6'
   // instruction that does the call, and store 'ra' at the top of the stack.
   bind(&start);
-  auipc(ra, 0);  // Set 'ra' the current 'pc'.
-  AddWord(ra, ra, kNumInstructions * kInstrSize);
+  LoadAddress(ra, &end);
   StoreWord(ra, MemOperand(sp));      // Reserved in EnterExitFrame.
   AddWord(sp, sp, -kCArgsSlotsSize);  // Preserves stack alignment.
 
   // Call the C routine.
   Mv(t6, target);  // Function pointer in 't6' to conform to ABI for PIC.
   jalr(t6);
 
-  // Make sure the stored 'ra' points to this position. This way, the 'ra'
-  // value we stored on the stack matches the value of 'ra' during the call.
-  DCHECK_EQ(kNumInstructions, InstructionsGeneratedSince(&start));
+  // The 'ra' value we stored on the stack matches the value of 'ra' during the
+  // call.
+  bind(&end);
 }
 
 void MacroAssembler::Ret(Condition cond, Register rs, const Operand& rt) {
@@ -7358,7 +7356,11 @@ int MacroAssembler::CallCFunctionHelper(
       AddWord(sp, sp, Operand(stack_passed_arguments * kSystemPointerSize));
     }
     if (kMaxSizeOfMoveAfterFastCall > pc_offset() - before_offset) {
-      nop();
+      // If the RCV extension is enabled, we may have to emit multiple NOPs to
+      // have enough space for patching in the deopt trampoline.
+      do {
+        NOP();
+      } while (pc_offset() - before_offset != kMaxSizeOfMoveAfterFastCall);
     }
     // We assume that with the nop padding, the move instruction uses
     // kMaxSizeOfMoveAfterFastCall bytes. When we patch in the deopt trampoline,