koffi 2.5.20 → 2.5.21-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/doc/contribute.md CHANGED
@@ -127,6 +127,18 @@ Each machine is configured to run a VNC server available locally, which you can
127
127
  node qemu.js info debian_x64
128
128
  ```
129
129
 
130
+ ## Making a release
131
+
132
+ First, change the version numbers in `package.json`. Then publish a new release with the following commands:
133
+
134
+ ```sh
135
+ node qemu.js test
136
+ node qemu.js dist
137
+
138
+ cd build/dist
139
+ npm publish
140
+ ```
141
+
130
142
  ## Code style
131
143
 
132
144
  Koffi is programmed in a mix of C++ and assembly code (architecture-specific code). It uses [node-addon-api](https://github.com/nodejs/node-addon-api) (C++ N-API wrapper) to interact with Node.js.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "2.5.20",
3
+ "version": "2.5.21-beta.1",
4
4
  "stable": "2.5.20",
5
5
  "description": "Fast and simple C FFI (foreign function interface) for Node.js",
6
6
  "keywords": [
package/src/index.js CHANGED
@@ -378,7 +378,7 @@ var require_package = __commonJS({
378
378
  "build/dist/src/koffi/package.json"(exports2, module2) {
379
379
  module2.exports = {
380
380
  name: "koffi",
381
- version: "2.5.20",
381
+ version: "2.5.21-beta.1",
382
382
  stable: "2.5.20",
383
383
  description: "Fast and simple C FFI (foreign function interface) for Node.js",
384
384
  keywords: [
@@ -413,6 +413,7 @@ var require_package = __commonJS({
413
413
  chalk: "^4.1.2",
414
414
  esbuild: "^0.19.2",
415
415
  "ffi-napi": "^4.0.3",
416
+ "ffi-rs": "^1.0.12",
416
417
  minimatch: "^5.0.1",
417
418
  "node-ssh": "^12.0.3",
418
419
  raylib: "^0.9.2",
@@ -66,6 +66,7 @@ set(KOFFI_SRC
66
66
  src/ffi.cc
67
67
  src/parser.cc
68
68
  src/util.cc
69
+ src/win32.cc
69
70
  ../../src/core/libcc/libcc.cc
70
71
  )
71
72
  if(CMAKE_SIZEOF_VOID_P EQUAL 8)
@@ -1568,6 +1568,32 @@ static Napi::Value UnloadLibrary(const Napi::CallbackInfo &info)
1568
1568
  return env.Undefined();
1569
1569
  }
1570
1570
 
1571
+ #ifdef _WIN32
1572
+ static HANDLE LoadWindowsLibrary(Napi::Env env, const char16_t *filename)
1573
+ {
1574
+ HANDLE module = LoadLibraryW((LPCWSTR)filename);
1575
+
1576
+ if (!module) {
1577
+ if (GetLastError() == ERROR_BAD_EXE_FORMAT) {
1578
+ int process = GetSelfMachine();
1579
+ int dll = GetDllMachine(filename);
1580
+
1581
+ if (process >= 0 && dll >= 0 && dll != process) {
1582
+ ThrowError<Napi::Error>(env, "Cannot load '%1' DLL in '%2' process",
1583
+ WindowsMachineNames.FindValue(dll, "Unknown"),
1584
+ WindowsMachineNames.FindValue(process, "Unknown"));
1585
+ return nullptr;
1586
+ }
1587
+ }
1588
+
1589
+ ThrowError<Napi::Error>(env, "Failed to load shared library: %1", GetWin32ErrorString());
1590
+ return nullptr;
1591
+ }
1592
+
1593
+ return module;
1594
+ }
1595
+ #endif
1596
+
1571
1597
  static Napi::Value LoadSharedLibrary(const Napi::CallbackInfo &info)
1572
1598
  {
1573
1599
  Napi::Env env = info.Env();
@@ -1592,12 +1618,10 @@ static Napi::Value LoadSharedLibrary(const Napi::CallbackInfo &info)
1592
1618
  #ifdef _WIN32
1593
1619
  if (info[0].IsString()) {
1594
1620
  std::u16string filename = info[0].As<Napi::String>();
1595
- module = LoadLibraryW((LPCWSTR)filename.c_str());
1621
+ module = LoadWindowsLibrary(env, filename.c_str());
1596
1622
 
1597
- if (!module) {
1598
- ThrowError<Napi::Error>(env, "Failed to load shared library: %1", GetWin32ErrorString());
1623
+ if (!module)
1599
1624
  return env.Null();
1600
- }
1601
1625
  } else {
1602
1626
  module = GetModuleHandle(nullptr);
1603
1627
  RG_ASSERT(module);
@@ -0,0 +1,155 @@
1
+ // Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ // this software and associated documentation files (the “Software”), to deal in
5
+ // the Software without restriction, including without limitation the rights to use,
6
+ // copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ // Software, and to permit persons to whom the Software is furnished to do so,
8
+ // subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in all
11
+ // copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
+ // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
+ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ // OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ #ifdef _WIN32
23
+
24
+ #include "win32.hh"
25
+
26
+ #ifndef NOMINMAX
27
+ #define NOMINMAX
28
+ #endif
29
+ #ifndef WIN32_LEAN_AND_MEAN
30
+ #define WIN32_LEAN_AND_MEAN
31
+ #endif
32
+ #include <windows.h>
33
+ #include <ntsecapi.h>
34
+ #include <processthreadsapi.h>
35
+
36
+ namespace RG {
37
+
38
+ const HashMap<int, const char *> WindowsMachineNames = {
39
+ { 0x184, "Alpha AXP, 32-bit" },
40
+ { 0x284, "Alpha 64" },
41
+ { 0x1d3, "Matsushita AM33" },
42
+ { 0x8664, "AMD x64" },
43
+ { 0x1c0, "ARM little endian" },
44
+ { 0xaa64, "ARM64 little endian" },
45
+ { 0x1c4, "ARM Thumb-2 little endian" },
46
+ { 0x284, "AXP 64" },
47
+ { 0xebc, "EFI byte code" },
48
+ { 0x14c, "Intel 386+" },
49
+ { 0x200, "Intel Itanium" },
50
+ { 0x6232, "LoongArch 32-bit" },
51
+ { 0x6264, "LoongArch 64-bit" },
52
+ { 0x9041, "Mitsubishi M32R little endian" },
53
+ { 0x266, "MIPS16" },
54
+ { 0x366, "MIPS with FPU" },
55
+ { 0x466, "MIPS16 with FPU" },
56
+ { 0x1f0, "Power PC little endian" },
57
+ { 0x1f1, "Power PC with FP support" },
58
+ { 0x166, "MIPS little endian" },
59
+ { 0x5032, "RISC-V 32-bit" },
60
+ { 0x5064, "RISC-V 64-bit" },
61
+ { 0x5128, "RISC-V 128-bit" },
62
+ { 0x1a2, "Hitachi SH3" },
63
+ { 0x1a3, "Hitachi SH3 DSP" },
64
+ { 0x1a6, "Hitachi SH4" },
65
+ { 0x1a8, "Hitachi SH5" },
66
+ { 0x1c2, "Thumb" },
67
+ { 0x169, "MIPS little-endian WCE v2" }
68
+ };
69
+
70
+ // Fails silently on purpose
71
+ static bool ReadAt(HANDLE h, int32_t offset, void *buf, int len)
72
+ {
73
+ OVERLAPPED ov = {};
74
+ DWORD read;
75
+
76
+ ov.Offset = offset & 0x7FFFFFFFu;
77
+
78
+ if (offset < 0)
79
+ return false;
80
+ if (!ReadFile(h, buf, (DWORD)len, &read, &ov))
81
+ return false;
82
+ if (read != len)
83
+ return false;
84
+
85
+ return true;
86
+ }
87
+
88
+ static int GetFileMachine(HANDLE h, bool check_dll)
89
+ {
90
+ PE_DOS_HEADER dos = {};
91
+ PE_NT_HEADERS nt = {};
92
+
93
+ if (!ReadAt(h, 0, &dos, RG_SIZE(dos)))
94
+ goto generic;
95
+ if (!ReadAt(h, dos.e_lfanew, &nt, RG_SIZE(nt)))
96
+ goto generic;
97
+
98
+ if (dos.e_magic != 0x5A4D) // MZ
99
+ goto generic;
100
+ if (nt.Signature != 0x00004550) // PE\0\0
101
+ goto generic;
102
+ if (check_dll && !(nt.FileHeader.Characteristics & IMAGE_FILE_DLL))
103
+ goto generic;
104
+
105
+ return (int)nt.FileHeader.Machine;
106
+
107
+ generic:
108
+ LogError("Invalid or forbidden %1 file: %2", check_dll ? "DLL" : "executable", GetWin32ErrorString());
109
+ return -1;
110
+ }
111
+
112
+ int GetSelfMachine()
113
+ {
114
+ const char *filename = GetApplicationExecutable();
115
+
116
+ HANDLE h;
117
+ if (IsWin32Utf8()) {
118
+ h = CreateFileA(filename, GENERIC_READ,
119
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
120
+ nullptr, OPEN_EXISTING, 0, nullptr);
121
+ } else {
122
+ wchar_t filename_w[4096];
123
+ if (ConvertUtf8ToWin32Wide(filename, filename_w) < 0)
124
+ return -1;
125
+
126
+ h = CreateFileW(filename_w, GENERIC_READ,
127
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
128
+ nullptr, OPEN_EXISTING, 0, nullptr);
129
+ }
130
+ if (h == INVALID_HANDLE_VALUE) {
131
+ LogError("Cannot open '%1': %2", filename, GetWin32ErrorString());
132
+ return -1;
133
+ }
134
+ RG_DEFER { CloseHandle(h); };
135
+
136
+ return GetFileMachine(h, false);
137
+ }
138
+
139
+ int GetDllMachine(const char16_t *filename)
140
+ {
141
+ HANDLE h = CreateFileW((LPCWSTR)filename, GENERIC_READ,
142
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
143
+ nullptr, OPEN_EXISTING, 0, nullptr);
144
+ if (h == INVALID_HANDLE_VALUE) {
145
+ LogError("Cannot open '%1': %2", filename, GetWin32ErrorString());
146
+ return -1;
147
+ }
148
+ RG_DEFER { CloseHandle(h); };
149
+
150
+ return GetFileMachine(h, true);
151
+ }
152
+
153
+ }
154
+
155
+ #endif
@@ -27,6 +27,45 @@
27
27
 
28
28
  namespace RG {
29
29
 
30
+ struct PE_DOS_HEADER {
31
+ uint16_t e_magic;
32
+ uint16_t e_cblp;
33
+ uint16_t e_cp;
34
+ uint16_t e_crlc;
35
+ uint16_t e_cparhdr;
36
+ uint16_t e_minalloc;
37
+ uint16_t e_maxalloc;
38
+ uint16_t e_ss;
39
+ uint16_t e_sp;
40
+ uint16_t e_csum;
41
+ uint16_t e_ip;
42
+ uint16_t e_cs;
43
+ uint16_t e_lfarlc;
44
+ uint16_t e_ovno;
45
+ uint16_t e_res[4];
46
+ uint16_t e_oemid;
47
+ uint16_t e_oeminfo;
48
+ uint16_t e_res2[10];
49
+ uint32_t e_lfanew;
50
+ };
51
+
52
+ struct PE_FILE_HEADER {
53
+ uint16_t Machine;
54
+ uint16_t NumberOfSections;
55
+ uint32_t TimeDateStamp;
56
+ uint32_t PointerToSymbolTable;
57
+ uint32_t NumberOfSymbols;
58
+ uint16_t SizeOfOptionalHeader;
59
+ uint16_t Characteristics;
60
+ };
61
+
62
+ struct PE_NT_HEADERS {
63
+ uint32_t Signature;
64
+ PE_FILE_HEADER FileHeader;
65
+
66
+ // ... OptionalHeader;
67
+ };
68
+
30
69
  #if _WIN64
31
70
 
32
71
  struct TEB {
@@ -70,4 +109,9 @@ static inline TEB *GetTEB()
70
109
  return teb;
71
110
  }
72
111
 
112
+ extern const HashMap<int, const char *> WindowsMachineNames;
113
+
114
+ int GetSelfMachine();
115
+ int GetDllMachine(const char16_t *filename);
116
+
73
117
  }