current commit
260802.14 · ffi: reject fast calls after library close
#if HAVE_FFI

#include "ffi/fast.h"

#if defined(__riscv) && __riscv_xlen == 64

#include <sys/mman.h>
#include <unistd.h>

#include <stdint.h>

namespace {

using node::ffi::FastFFIType;

bool IsFloatType(FastFFIType type) {
  return type == FastFFIType::kFloat32 || type == FastFFIType::kFloat64;
}

bool IsNarrowType(FastFFIType type) {
  switch (type) {
    case FastFFIType::kInt8:
    case FastFFIType::kUint8:
    case FastFFIType::kInt16:
    case FastFFIType::kUint16:
      return true;
    default:
      return false;
  }
}

uint32_t Addi(unsigned rd, unsigned rs1, int imm) {
  return ((static_cast<uint32_t>(imm) & 0xfff) << 20) | (rs1 << 15) |
         (rd << 7) | 0x13;
}

uint32_t Ld(unsigned rd, unsigned rs1, int imm) {
  return ((static_cast<uint32_t>(imm) & 0xfff) << 20) | (rs1 << 15) |
         (3u << 12) | (rd << 7) | 0x03;
}

uint32_t Auipc(unsigned rd, int imm20) {
  return (static_cast<uint32_t>(imm20) << 12) | (rd << 7) | 0x17;
}

uint32_t Jalr(unsigned rd, unsigned rs1, int imm) {
  return ((static_cast<uint32_t>(imm) & 0xfff) << 20) | (rs1 << 15) |
         (rd << 7) | 0x67;
}

void Emit32(uint32_t** cursor, uint32_t value) {
  *(*cursor)++ = value;
}

void Emit64(uint32_t** cursor, uint64_t value) {
  uint64_t* slot = reinterpret_cast<uint64_t*>(*cursor);
  *slot = value;
  *cursor += 2;
}

void* AllocateCode(size_t code_size) {
  void* code = mmap(nullptr,
                    code_size,
                    PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANON,
                    -1,
                    0);
  return code == MAP_FAILED ? nullptr : code;
}

void FreeCode(void* code, size_t code_size) {
  munmap(code, code_size);
}

}  // namespace

extern "C" bool node_ffi_create_fast_trampoline(
    const node::ffi::FastFFITrampolineConfig& config,
    const node::ffi::FastFFIType* args,
    size_t argc,
    node::ffi::FastFFIType result,
    node::ffi::FastFFITrampoline* out) {
  if (config.target == nullptr || out == nullptr || IsNarrowType(result)) {
    return false;
  }

  size_t gp_count = 0;
  size_t fp_count = 0;
  for (size_t i = 0; i < argc; i++) {
    if (args[i] == FastFFIType::kBuffer) {
      return false;
    }
    if (IsFloatType(args[i])) {
      fp_count++;
    } else {
      gp_count++;
    }
  }

  // RISC-V LP64D passes integer arguments in a0..a7. V8's receiver occupies
  // a0, so user GP arguments arrive in a1..a7 and are shifted down before the
  // tail branch. FP arguments are already in fa0..fa7.
  if (gp_count > 7 || fp_count > 8) {
    return false;
  }

  constexpr size_t kCodeSize = 256;
  void* code = AllocateCode(kCodeSize);
  if (code == nullptr) {
    return false;
  }

  uint32_t* cursor = static_cast<uint32_t*>(code);
  unsigned gp_index = 0;
  for (size_t i = 0; i < argc; i++) {
    if (IsFloatType(args[i])) {
      continue;
    }
    const unsigned target_reg = 10 + gp_index;
    const unsigned incoming_reg = target_reg + 1;
    Emit32(&cursor, Addi(target_reg, incoming_reg, 0));
    gp_index++;
  }

  // Load the target address from a nearby literal into t0 and tail-branch.
  Emit32(&cursor, Auipc(5, 0));         // auipc t0, 0
  Emit32(&cursor, Ld(5, 5, 16));        // ld t0, literal(t0)
  Emit32(&cursor, Jalr(0, 5, 0));       // jr t0
  Emit32(&cursor, Addi(0, 0, 0));       // nop; align literal to 8 bytes
  Emit64(&cursor, reinterpret_cast<uintptr_t>(config.target));

  const size_t written = reinterpret_cast<uint8_t*>(cursor) -
                         static_cast<uint8_t*>(code);
  __builtin___clear_cache(static_cast<char*>(code),
                          static_cast<char*>(code) + written);

  if (mprotect(code, kCodeSize, PROT_READ | PROT_EXEC) != 0) {
    FreeCode(code, kCodeSize);
    return false;
  }

  out->code = code;
  out->size = kCodeSize;
  return true;
}

extern "C" void node_ffi_free_fast_trampoline(
    node::ffi::FastFFITrampoline* trampoline) {
  if (trampoline == nullptr || trampoline->code == nullptr) {
    return;
  }
  FreeCode(trampoline->code, trampoline->size);
  trampoline->code = nullptr;
  trampoline->size = 0;
}

#endif  // defined(__riscv) && __riscv_xlen == 64

#endif  // HAVE_FFI