typenative 0.4.1 → 0.6.0

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.
Files changed (270) hide show
  1. package/.github/workflows/ci.yml +2 -2
  2. package/.github/workflows/npm-publish-github-packages.yml +2 -2
  3. package/CHANGELOG.md +442 -1
  4. package/CONTRIBUTION.md +2 -1
  5. package/README.md +73 -25
  6. package/bin/cli.js +2 -0
  7. package/bin/index.js +75 -5
  8. package/bin/main.js +2 -0
  9. package/bin/node/assert.js +2 -2
  10. package/bin/node/async_hooks.js +1 -1
  11. package/bin/node/buffer.js +16 -5
  12. package/bin/node/child_process.js +7 -7
  13. package/bin/node/cluster.js +3 -3
  14. package/bin/node/core.js +41 -0
  15. package/bin/node/crypto_keys.js +21 -21
  16. package/bin/node/dgram.js +1 -1
  17. package/bin/node/diagnostics_channel.js +3 -3
  18. package/bin/node/dns.js +2 -2
  19. package/bin/node/events.js +18 -13
  20. package/bin/node/fetch.js +2 -2
  21. package/bin/node/fs.js +3 -3
  22. package/bin/node/fs_watch.js +5 -5
  23. package/bin/node/http.js +3 -3
  24. package/bin/node/http2.js +3 -3
  25. package/bin/node/net.js +8 -8
  26. package/bin/node/path.js +17 -3
  27. package/bin/node/process.js +47 -11
  28. package/bin/node/punycode.js +2 -2
  29. package/bin/node/readline.js +1 -1
  30. package/bin/node/registry.js +4 -1
  31. package/bin/node/sqlite.js +6 -6
  32. package/bin/node/stream.js +81 -15
  33. package/bin/node/stream_web.js +10 -10
  34. package/bin/node/system_extra.js +3 -3
  35. package/bin/node/test.js +674 -366
  36. package/bin/node/test_coverage.js +187 -0
  37. package/bin/node/test_report.js +1392 -0
  38. package/bin/node/test_run.js +575 -0
  39. package/bin/node/timers.js +8 -6
  40. package/bin/node/tls.js +5 -5
  41. package/bin/node/url.js +1 -1
  42. package/bin/node/util.js +31 -7
  43. package/bin/node/util_extra.js +7 -7
  44. package/bin/node/webview.js +654 -0
  45. package/bin/node/worker_threads.js +1 -1
  46. package/bin/node/zlib.js +3 -3
  47. package/bin/parse-node.js +6 -2
  48. package/bin/regex_vm.js +2057 -0
  49. package/bin/transpiler.js +32872 -11265
  50. package/bin/typecheck-node.js +2 -1
  51. package/bin/version.js +1 -1
  52. package/conformance.md +515 -345
  53. package/examples/README.md +71 -11
  54. package/examples/angular-templates/README.md +139 -0
  55. package/examples/angular-templates/bench.mjs +173 -0
  56. package/examples/angular-templates/dom-type-globals.d.ts +21 -0
  57. package/examples/angular-templates/dom-type-stubs.ts +21 -0
  58. package/examples/angular-templates/main.ts +135 -0
  59. package/examples/angular-templates/parity.mjs +157 -0
  60. package/examples/angular-templates/run-node.mjs +69 -0
  61. package/examples/angular-templates/vendor/angular/compiler/LICENSE +21 -0
  62. package/examples/desktop-notes/README.md +106 -0
  63. package/examples/desktop-notes/bench.mjs +125 -0
  64. package/examples/desktop-notes/main.ts +131 -0
  65. package/examples/desktop-notes/package-lock.json +726 -0
  66. package/examples/desktop-notes/package.json +9 -0
  67. package/examples/desktop-notes/ui.ts +176 -0
  68. package/examples/dupe-finder/README.md +78 -0
  69. package/examples/dupe-finder/bench.mjs +120 -0
  70. package/examples/dupe-finder/main.ts +87 -0
  71. package/examples/fibonacci/README.md +7 -7
  72. package/examples/hello-world/README.md +6 -6
  73. package/examples/json-server/README.md +19 -18
  74. package/examples/mandelbrot-png/README.md +63 -0
  75. package/examples/mandelbrot-png/bench.mjs +95 -0
  76. package/examples/mandelbrot-png/main.ts +98 -0
  77. package/examples/mandelbrot-png/preview.png +0 -0
  78. package/examples/marked-cli/README.md +160 -0
  79. package/examples/marked-cli/bench.mjs +178 -0
  80. package/examples/marked-cli/main.ts +292 -0
  81. package/examples/marked-cli/parity.mjs +185 -0
  82. package/examples/marked-cli/vendor/marked/LICENSE +44 -0
  83. package/examples/marked-cli/vendor/marked/VENDOR.md +26 -0
  84. package/examples/openai-chat/README.md +148 -0
  85. package/examples/openai-chat/bench.mjs +175 -0
  86. package/examples/openai-chat/main.ts +488 -0
  87. package/examples/react-compiler/README.md +170 -0
  88. package/examples/react-compiler/bench.mjs +181 -0
  89. package/examples/react-compiler/main.ts +158 -0
  90. package/examples/react-compiler/parity.mjs +362 -0
  91. package/examples/react-compiler/run-node.mjs +88 -0
  92. package/examples/react-compiler/vendor/babel/LICENSE +22 -0
  93. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +15 -0
  94. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +51 -0
  95. package/examples/react-compiler/vendor/charcodes/package.json +25 -0
  96. package/examples/react-compiler/vendor/invariant/LICENSE +21 -0
  97. package/examples/react-compiler/vendor/invariant/invariant.cjs +51 -0
  98. package/examples/react-compiler/vendor/invariant/invariant.d.cts +11 -0
  99. package/examples/react-compiler/vendor/invariant/types/LICENSE +21 -0
  100. package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +19 -0
  101. package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +19 -0
  102. package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +19 -0
  103. package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +19 -0
  104. package/examples/react-compiler/vendor/js-tokens/LICENSE +21 -0
  105. package/examples/react-compiler/vendor/js-tokens/index.cjs +23 -0
  106. package/examples/react-compiler/vendor/js-tokens/index.d.cts +13 -0
  107. package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +20 -0
  108. package/examples/react-compiler/vendor/jsesc/jsesc.cjs +337 -0
  109. package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +384 -0
  110. package/examples/react-compiler/vendor/jsesc/types/LICENSE +21 -0
  111. package/examples/react-compiler/vendor/picocolors/LICENSE +15 -0
  112. package/examples/react-compiler/vendor/picocolors/picocolors.cjs +75 -0
  113. package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +5 -0
  114. package/examples/react-compiler/vendor/picocolors/types.d.ts +51 -0
  115. package/examples/react-compiler/vendor/react-compiler/LICENSE +21 -0
  116. package/examples/react-compiler/vendor/stubs/babel-core.ts +31 -0
  117. package/examples/react-compiler/vendor/stubs/debug.ts +18 -0
  118. package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +13 -0
  119. package/examples/react-compiler/vendor/stubs/pretty-format.ts +18 -0
  120. package/examples/react-compiler/vendor/zod/LICENSE +21 -0
  121. package/examples/react-compiler/vendor/zod-validation-error/LICENSE +9 -0
  122. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +5 -0
  123. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +49 -0
  124. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +48 -0
  125. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +50 -0
  126. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +6 -0
  127. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +126 -0
  128. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +39 -0
  129. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +12 -0
  130. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +78 -0
  131. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +2 -0
  132. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +12 -0
  133. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +12 -0
  134. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +131 -0
  135. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +54 -0
  136. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +12 -0
  137. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +40 -0
  138. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +12 -0
  139. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +76 -0
  140. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +76 -0
  141. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +27 -0
  142. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +27 -0
  143. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +13 -0
  144. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +61 -0
  145. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +38 -0
  146. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +20 -0
  147. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +5 -0
  148. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +8 -0
  149. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +11 -0
  150. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +20 -0
  151. package/examples/react-compiler/vendor.mjs +688 -0
  152. package/examples/todo-cli/README.md +69 -0
  153. package/examples/todo-cli/bench.mjs +89 -0
  154. package/examples/todo-cli/main.ts +92 -0
  155. package/examples/yaml-cli/README.md +146 -0
  156. package/examples/yaml-cli/bench.mjs +265 -0
  157. package/examples/yaml-cli/main.ts +29 -0
  158. package/examples/yaml-cli/parity.mjs +156 -0
  159. package/examples/yaml-cli/vendor/yaml/LICENSE +13 -0
  160. package/examples/yaml-cli/vendor/yaml/VENDOR.md +25 -0
  161. package/govendor/github.com/jchv/go-webview2/LICENSE +22 -0
  162. package/govendor/github.com/jchv/go-webview2/README.md +21 -0
  163. package/govendor/github.com/jchv/go-webview2/common.go +84 -0
  164. package/govendor/github.com/jchv/go-webview2/go.mod +3 -0
  165. package/govendor/github.com/jchv/go-webview2/internal/w32/w32.go +194 -0
  166. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_386.go +14 -0
  167. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_64bit.go +15 -0
  168. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_COLOR.go +8 -0
  169. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND.go +9 -0
  170. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_KEY_EVENT_KIND.go +10 -0
  171. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_MOVE_FOCUS_REASON.go +9 -0
  172. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_PHYSICAL_KEY_STATUS.go +10 -0
  173. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_WEB_RESOURCE_CONTEXT.go +23 -0
  174. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventArgs.go +78 -0
  175. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventHandler.go +52 -0
  176. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller.go +132 -0
  177. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller2.go +74 -0
  178. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2CreateCoreWebView2ControllerCompletedHandler.go +48 -0
  179. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventArgs.go +17 -0
  180. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventHandler.go +48 -0
  181. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Settings.go +270 -0
  182. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequest.go +43 -0
  183. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventArgs.go +51 -0
  184. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventHandler.go +48 -0
  185. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceResponse.go +21 -0
  186. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_2.go +21 -0
  187. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_3.go +60 -0
  188. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebViewSettings.go +396 -0
  189. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium.go +361 -0
  190. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_386.go +24 -0
  191. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_amd64.go +22 -0
  192. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_arm64.go +26 -0
  193. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc.go +13 -0
  194. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go17.go +51 -0
  195. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go18.go +16 -0
  196. package/govendor/github.com/jchv/go-webview2/pkg/edge/corewebview2.go +411 -0
  197. package/govendor/github.com/jchv/go-webview2/pkg/edge/guid.go +223 -0
  198. package/govendor/github.com/jchv/go-webview2/webview.go +482 -0
  199. package/govendor/github.com/jchv/go-webview2/webviewloader/README.md +3 -0
  200. package/govendor/github.com/jchv/go-webview2/webviewloader/module.go +153 -0
  201. package/govendor/github.com/jchv/go-webview2/webviewloader/module_386.go +6 -0
  202. package/govendor/github.com/jchv/go-webview2/webviewloader/module_amd64.go +6 -0
  203. package/govendor/github.com/jchv/go-webview2/webviewloader/module_arm64.go +6 -0
  204. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/arm64/WebView2Loader.dll +0 -0
  205. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x64/WebView2Loader.dll +0 -0
  206. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x86/WebView2Loader.dll +0 -0
  207. package/govendor/github.com/jchv/go-winloader/LICENSE.md +7 -0
  208. package/govendor/github.com/jchv/go-winloader/README.md +124 -0
  209. package/govendor/github.com/jchv/go-winloader/common.go +11 -0
  210. package/govendor/github.com/jchv/go-winloader/go.mod +3 -0
  211. package/govendor/github.com/jchv/go-winloader/internal/loader/loader.go +25 -0
  212. package/govendor/github.com/jchv/go-winloader/internal/loader/machine.go +53 -0
  213. package/govendor/github.com/jchv/go-winloader/internal/memloader/cache.go +42 -0
  214. package/govendor/github.com/jchv/go-winloader/internal/memloader/loader.go +253 -0
  215. package/govendor/github.com/jchv/go-winloader/internal/pe/export.go +76 -0
  216. package/govendor/github.com/jchv/go-winloader/internal/pe/format.go +522 -0
  217. package/govendor/github.com/jchv/go-winloader/internal/pe/import.go +106 -0
  218. package/govendor/github.com/jchv/go-winloader/internal/pe/module.go +91 -0
  219. package/govendor/github.com/jchv/go-winloader/internal/pe/reloc.go +177 -0
  220. package/govendor/github.com/jchv/go-winloader/internal/pe/util.go +40 -0
  221. package/govendor/github.com/jchv/go-winloader/internal/vmem/common.go +55 -0
  222. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_other.go +71 -0
  223. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_windows.go +179 -0
  224. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_other.go +17 -0
  225. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_windows.go +73 -0
  226. package/govendor/github.com/jchv/go-winloader/internal/winloader/loader_windows.go +64 -0
  227. package/govendor/github.com/jchv/go-winloader/internal/winloader/machine_windows.go +32 -0
  228. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_386.go +9 -0
  229. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_amd64.go +9 -0
  230. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_arm64.go +9 -0
  231. package/govendor/github.com/jchv/go-winloader/loader_other.go +24 -0
  232. package/govendor/github.com/jchv/go-winloader/loader_windows.go +33 -0
  233. package/govendor/manifest.txt +3 -1
  234. package/package.json +8 -5
  235. package/performance/README.md +195 -0
  236. package/performance/any-dynamic.ts +21 -0
  237. package/performance/any-record-view.ts +20 -0
  238. package/performance/arr-hof.ts +21 -0
  239. package/performance/arr-push-index.ts +15 -0
  240. package/performance/arr-sort.ts +21 -0
  241. package/performance/async-await.ts +24 -0
  242. package/performance/class-dispatch.ts +52 -0
  243. package/performance/events-emit.ts +27 -0
  244. package/performance/exc-throw-rare.ts +25 -0
  245. package/performance/exc-try-catch.ts +26 -0
  246. package/performance/fn-closures.ts +29 -0
  247. package/performance/fn-recursion-tree.ts +31 -0
  248. package/performance/gen-for-of.ts +46 -0
  249. package/performance/json-roundtrip.ts +27 -0
  250. package/performance/map-delete.ts +24 -0
  251. package/performance/map-string-keys.ts +22 -0
  252. package/performance/num-arith-loop.ts +14 -0
  253. package/performance/num-bitwise.ts +21 -0
  254. package/performance/num-mod-int.ts +16 -0
  255. package/performance/num-to-string.ts +16 -0
  256. package/performance/obj-alloc.ts +32 -0
  257. package/performance/regex-exec.ts +29 -0
  258. package/performance/regex-site.ts +28 -0
  259. package/performance/set-ops.ts +20 -0
  260. package/performance/str-char-scan.ts +35 -0
  261. package/performance/str-concat-plus.ts +18 -0
  262. package/performance/str-concat.ts +24 -0
  263. package/performance/str-short-scan.ts +26 -0
  264. package/performance/str-split-join.ts +23 -0
  265. package/performance/str-template.ts +16 -0
  266. package/performance/union-switch.ts +57 -0
  267. package/tsparser/check.go +2 -1
  268. package/tsparser/main.go +5 -0
  269. package/types/typenative-npm.d.ts +193 -4
  270. package/types/typenative.d.ts +138 -41
@@ -0,0 +1,124 @@
1
+ # go-winloader
2
+
3
+ **Note:** This library is still experimental. There are no guarantees of API stability, or runtime stability. Proceed with caution.
4
+
5
+ go-winloader is a library that implements the Windows module loader algorithm in pure Go. The actual Windows module loader, accessible via `LoadLibrary`, only supports loading modules from disk, which is sometimes undesirable. With go-winloader, you can load modules directly from memory without needing to write intermediate temporary files to disk.
6
+
7
+ This is a bit more versatile than linking directly to object files, since you do not need object files for this approach. As a downside, it is a purely Windows-only approach.
8
+
9
+ Note: Unlike native APIs, in place of processor register sized values go-winloader uses `uint64` instead of `uintptr` except when calling native host functions. This allows it to remain neutral to processor architecture at runtime, which in the future may allow for more esoteric use cases. (See TODO for more information on potential future features.)
10
+
11
+ ## Example
12
+
13
+ ```go
14
+ // Load a module off disk (for simplicity; you can pass in any byte slice.)
15
+ b, _ := ioutil.ReadFile("my.dll")
16
+ mod, err := winloader.LoadFromMemory(b)
17
+ if err != nil {
18
+ log.Fatalln("error loading module:", err)
19
+ }
20
+
21
+ // Get a procedure.
22
+ addProc := mod.Proc("Add")
23
+ if proc == nil {
24
+ log.Fatalln("module my.dll is missing required procedure Add")
25
+ }
26
+
27
+ // Call the procedure!
28
+ result, _, _ := addProc.Call(1, 2)
29
+ log.Printf("1 + 2 = %d", result)
30
+ ```
31
+
32
+ ## TODO
33
+
34
+ * WinSXS support
35
+
36
+ * Some binaries have manifests requesting a specific version of a library.
37
+ There is an undocumented library at sxs.dll. Since it's undocumented,
38
+ it might be difficult to work out how to use it.
39
+
40
+ * It'd be nice to have a reimplementation of the whole SXS algorithm, just
41
+ for completeness sake. (It might even be useful to Wine.)
42
+
43
+ * Additional compatibility hacks
44
+
45
+ * Because we are not Windows loader, Windows loader's internal structures
46
+ do not update when we load binaries into the address space.
47
+
48
+ * Because of this, our HINSTANCE value may not always work properly.
49
+
50
+ * There's a hack that sends the process HINSTANCE instead.
51
+
52
+ * There's a stub for a hack that would inject the library into the PEB
53
+ loader data linked lists.
54
+
55
+ * This might fail catastrophically or have worse consequences, but it
56
+ would be interesting to explore.
57
+
58
+ * Another useful hack would be one that can override calls to important
59
+ Windows functions and implement their functionality for cases when our
60
+ own false HINSTANCE is used. This could be done on a process-wide level
61
+ (for best compatibility) or directly on the import table (usually enough,
62
+ but tricky modules will bypass this.)
63
+
64
+ * Better support for loading executable images.
65
+
66
+ * Right now, if you attempt to load an executable, it executes the
67
+ entrypoint eagerly when it tries to send the DLL attach message.
68
+
69
+ * Threading support.
70
+
71
+ * Perhaps have a helper function that can run a function in a new thread,
72
+ automatically calling `DLL_THREAD_ATTACH`/`DLL_THREAD_DETACH` as needed
73
+ on memory loaded modules.
74
+
75
+ * While it may not be necessary for all libraries, it is likely necessary
76
+ for libraries that have statically linked the MSVC runtime, and for
77
+ libraries that use thread-local storage. Otherwise, calling functions on
78
+ threads other than the initial one is likely to crash.
79
+
80
+ * Even better: if we can find a place to hook new threads, this would be a
81
+ nice hack to support.
82
+
83
+ * Versatility
84
+
85
+ * Operating systems other than Windows:
86
+
87
+ * Need to figure out how to make MSABI calls. Maybe CGo with msabi
88
+ function pointers, or maybe we need to write out the asm by hand.
89
+
90
+ * Emulator would need the ability to generate stub addresses that call
91
+ back into Go code, so we can use them to handle imports and whatnot.
92
+
93
+ * Would need a custom loader that lets you emulate calls to other
94
+ libraries.
95
+
96
+ * CPU emulation
97
+
98
+ * Should be possible implement virtual machines with emulated CPUs.
99
+
100
+ * Similar to the outside Windows case, we need a custom loader to
101
+ emulate library calls. Although we *can* use the host system's
102
+ libraries, we need to translate API calls so that they work
103
+ correctly, like translating addresses and marshaling/unmarshaling
104
+ data as necessary, and of course calling conventions are entirely
105
+ different.
106
+
107
+ * We should provide shims at least for running 32-bit binaries in
108
+ 64-bit processes using emulation. How much of the API to emulate
109
+ would be hard to guage, but at least for documented APIs it should
110
+ be relatively straightforward work.
111
+
112
+ * Legacy formats
113
+
114
+ * Currently go-winloader only supports loading PE32 or PE64 binaries.
115
+
116
+ * It might be useful to someone to implement loading very old legacy
117
+ binaries, like NE or LX.
118
+
119
+ * Expose lower level APIs
120
+
121
+ * In order to allow this library to be useful while it is still in
122
+ heavy flux, a very small surface area is exposed today. As it
123
+ matures, more of these internal libraries should be exposed as
124
+ public API.
@@ -0,0 +1,11 @@
1
+ package winloader
2
+
3
+ import (
4
+ "github.com/jchv/go-winloader/internal/loader"
5
+ )
6
+
7
+ // Proc represents a proc of a module.
8
+ type Proc = loader.Proc
9
+
10
+ // Module represents a loaded module.
11
+ type Module = loader.Module
@@ -0,0 +1,3 @@
1
+ module github.com/jchv/go-winloader
2
+
3
+ go 1.14
@@ -0,0 +1,25 @@
1
+ package loader
2
+
3
+ // Module represents a loaded Windows module.
4
+ type Module interface {
5
+ // Proc returns a procedure by symbol name. Returns nil if the symbol is
6
+ // not found.
7
+ Proc(name string) Proc
8
+
9
+ // Ordinal returns a procedure by ordinal.
10
+ Ordinal(ordinal uint64) Proc
11
+
12
+ // Free closes the module and frees the memory. After this, GetProcAddress
13
+ // will stop working and procedures will no longer function.
14
+ Free() error
15
+ }
16
+
17
+ // Loader represents a named module loader implementation.
18
+ type Loader interface {
19
+ Load(libname string) (Module, error)
20
+ }
21
+
22
+ // MemLoader represents a memory module loader implementation.
23
+ type MemLoader interface {
24
+ LoadMem(module []byte) (Module, error)
25
+ }
@@ -0,0 +1,53 @@
1
+ package loader
2
+
3
+ import "io"
4
+
5
+ // Proc represents a procedure in memory.
6
+ type Proc interface {
7
+ // Call calls the procedure. r1 and r2 contain the return value. lastErr
8
+ // contains the Windows error value after calling.
9
+ Call(a ...uint64) (r1, r2 uint64, lastErr error)
10
+
11
+ // Returns the raw address of this function.
12
+ Addr() uint64
13
+ }
14
+
15
+ // Memory is an interface for a block of allocated virtual memory.
16
+ type Memory interface {
17
+ io.ReadWriteSeeker
18
+ io.ReaderAt
19
+ io.WriterAt
20
+
21
+ // Free frees the virtual memory region.
22
+ Free()
23
+
24
+ // Addr returns the address of the region of memory in the virtual address
25
+ // space.
26
+ Addr() uint64
27
+
28
+ // Clear zeros the entire region of memory.
29
+ Clear()
30
+
31
+ // Protect changes the memory protection for a subregion of this allocated
32
+ // block. It should match the semantics of the VirtualProtect function on
33
+ // Windows.
34
+ Protect(addr, size uint64, protect int) error
35
+ }
36
+
37
+ // Machine is an abstract machine interface.
38
+ type Machine interface {
39
+ // IsArchitectureSupported returns whether or not an architecture is
40
+ // supported by this abstract machine. Machine is a PE machine ID.
41
+ IsArchitectureSupported(machine int) bool
42
+
43
+ // GetPageSize returns the size of a memory page on this abstract machine.
44
+ GetPageSize() uint64
45
+
46
+ // Alloc performs virtual memory allocation. It should match the semantics
47
+ // of VirtualAlloc/VirtualFree on Windows.
48
+ Alloc(addr, size uint64, allocType, protect int) Memory
49
+
50
+ // MemProc returns an object for interfacing with a procedure at addr in
51
+ // the abstract machine's virtual memory space.
52
+ MemProc(addr uint64) Proc
53
+ }
@@ -0,0 +1,42 @@
1
+ package memloader
2
+
3
+ import (
4
+ "strings"
5
+
6
+ "github.com/jchv/go-winloader/internal/loader"
7
+ )
8
+
9
+ // Cache implements a memory cache for PE modules.
10
+ type Cache struct {
11
+ next loader.Loader
12
+ cache map[string]loader.Module
13
+ }
14
+
15
+ // NewCache creates a new cache with the specified options.
16
+ func NewCache(next loader.Loader) *Cache {
17
+ return &Cache{
18
+ next: next,
19
+ cache: make(map[string]loader.Module),
20
+ }
21
+ }
22
+
23
+ // Load implements loader.Loader by loading from cache or falling back.
24
+ func (c *Cache) Load(libname string) (loader.Module, error) {
25
+ if m, ok := c.cache[strings.ToLower(libname)]; ok {
26
+ return m, nil
27
+ }
28
+ if m, ok := c.cache[strings.ToLower(libname)+".dll"]; ok {
29
+ return m, nil
30
+ }
31
+ return c.next.Load(libname)
32
+ }
33
+
34
+ // Add adds a module to the cache.
35
+ func (c *Cache) Add(libname string, m loader.Module) error {
36
+ libname = strings.ToLower(libname)
37
+ if strings.HasSuffix(libname, ".dll") {
38
+ libname = libname[0 : len(libname)-4]
39
+ }
40
+ c.cache[libname] = m
41
+ return nil
42
+ }
@@ -0,0 +1,253 @@
1
+ package memloader
2
+
3
+ import (
4
+ "bytes"
5
+ "encoding/binary"
6
+ "fmt"
7
+ "io"
8
+
9
+ "github.com/jchv/go-winloader/internal/loader"
10
+ "github.com/jchv/go-winloader/internal/pe"
11
+ "github.com/jchv/go-winloader/internal/vmem"
12
+ "github.com/jchv/go-winloader/internal/winloader"
13
+ )
14
+
15
+ // module implements a module for the memory loader.
16
+ type module struct {
17
+ machine loader.Machine
18
+ memory loader.Memory
19
+ pemod *pe.Module
20
+ exports *pe.ExportTable
21
+ hinstance uint64
22
+ }
23
+
24
+ // Proc implements loader.Module
25
+ func (m *module) Proc(name string) loader.Proc {
26
+ addr := m.exports.Proc(name)
27
+ if addr == 0 {
28
+ return nil
29
+ }
30
+ return m.machine.MemProc(addr)
31
+ }
32
+
33
+ // Ordinal implements loader.Module
34
+ func (m *module) Ordinal(ordinal uint64) loader.Proc {
35
+ addr := m.exports.Ordinal(uint16(ordinal))
36
+ if addr == 0 {
37
+ return nil
38
+ }
39
+ return m.machine.MemProc(addr)
40
+ }
41
+
42
+ // Free implements loader.Module
43
+ func (m *module) Free() error {
44
+ // Execute entrypoint for detach.
45
+ entry := m.machine.MemProc(m.memory.Addr() + uint64(m.pemod.Header.OptionalHeader.AddressOfEntryPoint))
46
+ entry.Call(uint64(m.memory.Addr()), 0, 0)
47
+
48
+ // Free memory.
49
+ m.memory.Free()
50
+ return nil
51
+ }
52
+
53
+ // Loader implements a memory loader for PE files.
54
+ type Loader struct {
55
+ next loader.Loader
56
+ machine loader.Machine
57
+ pebhacks bool
58
+ prochinst bool
59
+ }
60
+
61
+ // Options contains the options for creating a new memory loader.
62
+ type Options struct {
63
+ // Next specifies the loader to use for recursing to resolve modules by
64
+ // name.
65
+ Next loader.Loader
66
+
67
+ // Machine specifies the machine the module should be loaded into.
68
+ Machine loader.Machine
69
+
70
+ // HintAddModuleToPEB specifies that the memory loader should try to add
71
+ // the loaded module into the PEB so that certain things function as
72
+ // expected.
73
+ // NOTE: This is not implemented yet and may not be possible.
74
+ HintAddModuleToPEB bool
75
+
76
+ // HintUseProcessHInstance specifies that the memory loader should use the
77
+ // host process's HINSTANCE value for calling into entrypoints.
78
+ HintUseProcessHInstance bool
79
+ }
80
+
81
+ // New creates a new loader with the specified options.
82
+ func New(opts Options) loader.MemLoader {
83
+ return &Loader{
84
+ next: opts.Next,
85
+ machine: opts.Machine,
86
+ }
87
+ }
88
+
89
+ // LoadMem implements the loader.MemLoader interface.
90
+ func (l *Loader) LoadMem(data []byte) (loader.Module, error) {
91
+ bin, err := pe.LoadModule(bytes.NewReader(data))
92
+ if err != nil {
93
+ return nil, err
94
+ }
95
+
96
+ if !l.machine.IsArchitectureSupported(int(bin.Header.FileHeader.Machine)) {
97
+ return nil, fmt.Errorf("image architecture not %04x not supported by this machine", bin.Header.FileHeader.Machine)
98
+ }
99
+
100
+ pageSize := l.machine.GetPageSize()
101
+ imageSize := vmem.RoundUp(uint64(bin.Header.OptionalHeader.SizeOfImage), pageSize)
102
+
103
+ var mem loader.Memory
104
+
105
+ // If the image is not movable, allocate it at its preferred address.
106
+ if bin.Header.OptionalHeader.DllCharacteristics&pe.ImageDLLCharacteristicsDynamicBase == 0 {
107
+ mem = l.machine.Alloc(bin.Header.OptionalHeader.ImageBase, imageSize, vmem.MemCommit|vmem.MemReserve, vmem.PageExecuteReadWrite)
108
+ if mem == nil {
109
+ return nil, fmt.Errorf("image could not be mapped at preferred base 0x%08x and cannot be relocated", bin.Header.OptionalHeader.ImageBase)
110
+ }
111
+ }
112
+
113
+ // Allocate anywhere, so as long as the image won't span a 4 GiB
114
+ // alignment boundary.
115
+ failedAllocs := []loader.Memory{}
116
+ for mem == nil || mem.Addr()>>32 != (mem.Addr()+imageSize)>>32 {
117
+ if mem != nil {
118
+ failedAllocs = append(failedAllocs, mem)
119
+ }
120
+ if mem = l.machine.Alloc(0, imageSize, vmem.MemCommit|vmem.MemReserve, vmem.PageExecuteReadWrite); mem == nil {
121
+ return nil, fmt.Errorf("allocation of %d bytes failed", imageSize)
122
+ }
123
+ }
124
+ for _, i := range failedAllocs {
125
+ i.Free()
126
+ }
127
+
128
+ realBase := mem.Addr()
129
+ hdrsize := uint64(bin.Header.OptionalHeader.SizeOfHeaders)
130
+ vmem.Alloc(realBase, hdrsize, vmem.MemCommit, vmem.PageReadWrite).Write(data[0:hdrsize])
131
+
132
+ // Map sections into memory
133
+ for _, section := range bin.Sections {
134
+ addr := realBase + uint64(section.VirtualAddress)
135
+ if section.SizeOfRawData == 0 {
136
+ size := uint64(bin.Header.OptionalHeader.SectionAlignment)
137
+ if size != 0 {
138
+ vmem.Alloc(addr, size, vmem.MemCommit, vmem.PageReadWrite).Clear()
139
+ }
140
+ } else {
141
+ sectionData := data[section.PointerToRawData : section.PointerToRawData+section.SizeOfRawData]
142
+ vmem.Alloc(addr, uint64(section.SizeOfRawData), vmem.MemCommit, vmem.PageReadWrite).Write(sectionData)
143
+ }
144
+ // TODO: need to set Misc.PhysicalAddress?
145
+ }
146
+
147
+ // TODO: Detect native byte order for relocations.
148
+ order := binary.LittleEndian
149
+ machine := int(bin.Header.FileHeader.Machine)
150
+
151
+ // Perform relocations
152
+ relocs := pe.LoadBaseRelocs(bin, mem)
153
+ if err := pe.Relocate(machine, relocs, uint64(realBase), bin.Header.OptionalHeader.ImageBase, mem, order); err != nil {
154
+ return nil, err
155
+ }
156
+
157
+ // Perform runtime linking
158
+ if err := pe.LinkModule(bin, mem, l.next); err != nil {
159
+ return nil, err
160
+ }
161
+
162
+ // Set access flags.
163
+ for _, section := range bin.Sections {
164
+ executable := section.Characteristics&pe.ImageSectionCharacteristicsMemoryExecute != 0
165
+ readable := section.Characteristics&pe.ImageSectionCharacteristicsMemoryRead != 0
166
+ writable := section.Characteristics&pe.ImageSectionCharacteristicsMemoryWrite != 0
167
+ protect := vmem.PageNoAccess
168
+ switch {
169
+ case !executable && !readable && !writable:
170
+ protect = vmem.PageNoAccess
171
+ case !executable && !readable && writable:
172
+ protect = vmem.PageWriteCopy
173
+ case !executable && readable && !writable:
174
+ protect = vmem.PageReadOnly
175
+ case !executable && readable && writable:
176
+ protect = vmem.PageReadWrite
177
+ case executable && !readable && !writable:
178
+ protect = vmem.PageExecute
179
+ case executable && !readable && writable:
180
+ protect = vmem.PageExecuteWriteCopy
181
+ case executable && readable && !writable:
182
+ protect = vmem.PageExecuteRead
183
+ case executable && readable && writable:
184
+ protect = vmem.PageExecuteReadWrite
185
+ }
186
+ size := uint64(section.SizeOfRawData)
187
+ if size == 0 {
188
+ size = uint64(bin.Header.OptionalHeader.SectionAlignment)
189
+ }
190
+ err := mem.Protect(uint64(section.VirtualAddress), size, protect)
191
+ if err != nil {
192
+ return nil, err
193
+ }
194
+ }
195
+
196
+ // Handle HINSTANCE setup.
197
+ hinstance := realBase
198
+ if l.pebhacks {
199
+ // TODO: implement PEB loader hacks, see if it works.
200
+ }
201
+ if l.prochinst {
202
+ if prochinst, err := winloader.GetProcessHInstance(); err == nil {
203
+ hinstance = uint64(prochinst)
204
+ }
205
+ }
206
+
207
+ m := &module{
208
+ machine: l.machine,
209
+ memory: mem,
210
+ pemod: bin,
211
+ hinstance: hinstance,
212
+ }
213
+
214
+ // Execute TLS callbacks.
215
+ tlsdir := bin.Header.OptionalHeader.DataDirectory[pe.ImageDirectoryEntryTLS]
216
+ if tlsdir.Size > 0 {
217
+ mem.Seek(int64(tlsdir.VirtualAddress), io.SeekStart)
218
+ dir := pe.ImageTLSDirectory64{}
219
+ b := [8]byte{}
220
+ psize := 4
221
+ if bin.IsPE64 {
222
+ psize = 8
223
+ binary.Read(mem, binary.LittleEndian, &dir)
224
+ } else {
225
+ dir32 := pe.ImageTLSDirectory32{}
226
+ binary.Read(mem, binary.LittleEndian, &dir32)
227
+ dir = dir32.To64()
228
+ }
229
+ mem.Seek(int64(dir.AddressOfCallBacks-realBase), io.SeekStart)
230
+ if dir.AddressOfCallBacks != 0 {
231
+ for {
232
+ mem.Read(b[:psize])
233
+ addr := binary.LittleEndian.Uint64(b[:])
234
+ if addr == 0 {
235
+ break
236
+ }
237
+ cb := l.machine.MemProc(addr)
238
+ cb.Call(hinstance, 1, 0)
239
+ }
240
+ }
241
+ }
242
+
243
+ // Execute entrypoint for attach.
244
+ entry := l.machine.MemProc(realBase + uint64(bin.Header.OptionalHeader.AddressOfEntryPoint))
245
+ entry.Call(hinstance, 1, 0)
246
+
247
+ m.exports, err = pe.LoadExports(bin, mem, realBase)
248
+ if err != nil {
249
+ return nil, err
250
+ }
251
+
252
+ return m, nil
253
+ }
@@ -0,0 +1,76 @@
1
+ package pe
2
+
3
+ import (
4
+ "encoding/binary"
5
+ "io"
6
+ )
7
+
8
+ // ExportTable is a table of module exports.
9
+ type ExportTable struct {
10
+ symbols map[string]uint64
11
+ ordinals map[uint16]uint64
12
+ }
13
+
14
+ // Proc returns an exported function address by symbol, or 0 if it is not found.
15
+ func (t *ExportTable) Proc(symbol string) (addr uint64) {
16
+ return t.symbols[symbol]
17
+ }
18
+
19
+ // Ordinal returns an exported function address by ordinal, or 0 if it is not found.
20
+ func (t *ExportTable) Ordinal(ordinal uint16) (addr uint64) {
21
+ return t.ordinals[ordinal]
22
+ }
23
+
24
+ // LoadExports returns a symbol table.
25
+ func LoadExports(m *Module, mem io.ReadWriteSeeker, base uint64) (*ExportTable, error) {
26
+ table := &ExportTable{
27
+ symbols: map[string]uint64{},
28
+ ordinals: map[uint16]uint64{},
29
+ }
30
+
31
+ dir := m.Header.OptionalHeader.DataDirectory[ImageDirectoryEntryExport]
32
+ if dir.Size == 0 {
33
+ return table, nil
34
+ }
35
+
36
+ // Load export directory header
37
+ header := ImageExportDirectory{}
38
+ mem.Seek(int64(dir.VirtualAddress), io.SeekStart)
39
+ binary.Read(mem, binary.LittleEndian, &header)
40
+
41
+ // Load addresses
42
+ addresses := make([]uint32, header.NumberOfFunctions)
43
+ mem.Seek(int64(header.AddressOfFunctions), io.SeekStart)
44
+ for i := range addresses {
45
+ b := [4]byte{}
46
+ mem.Read(b[:])
47
+ addresses[i] = binary.LittleEndian.Uint32(b[:])
48
+ table.ordinals[uint16(i)] = base + uint64(addresses[i])
49
+ }
50
+
51
+ // Load name ordinals
52
+ nameords := make([]uint16, header.NumberOfNames)
53
+ mem.Seek(int64(header.AddressOfNameOrdinals), io.SeekStart)
54
+ for i := range nameords {
55
+ b := [2]byte{}
56
+ mem.Read(b[:])
57
+ nameords[i] = binary.LittleEndian.Uint16(b[:])
58
+ }
59
+
60
+ // Load name addresses
61
+ nameaddrs := make([]uint32, header.NumberOfNames)
62
+ mem.Seek(int64(header.AddressOfNames), io.SeekStart)
63
+ for i := range nameaddrs {
64
+ b := [4]byte{}
65
+ mem.Read(b[:])
66
+ nameaddrs[i] = binary.LittleEndian.Uint32(b[:])
67
+ }
68
+
69
+ // Load names
70
+ for i, nameaddr := range nameaddrs {
71
+ mem.Seek(int64(nameaddr), io.SeekStart)
72
+ table.symbols[readsz(mem)] = base + uint64(addresses[nameords[i]])
73
+ }
74
+
75
+ return table, nil
76
+ }