Repository: electron
Issue: Support for allocating memory larger than 2GiB via partition allocator
Labels: enhancement :sparkles:,platform/all

### Preflight Checklist

- [x] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/main/CONTRIBUTING.md) for this project.
- [x] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/main/CODE_OF_CONDUCT.md) that this project adheres to.
- [x] I have searched the [issue tracker](https://www.github.com/electron/electron/issues) for a bug report that matches the one I want to file, without success.

### Electron Version

32.1.0

### What operating system(s) are you using?

macOS

### Operating System Version

macOS Sequoia 15.0.1

### What arch are you using?

arm64 (including Apple Silicon)

### Last Known Working Electron version

_No response_

### Expected Behavior

When using a native module that calls the `posix_memalign` function to allocate large buffers, Electron should not crash.

The following native code should work well when loaded in Electron from the main process:
```cpp
#include "napi.h"
#include <unistd.h>
#include <mach/mach.h>

void test() {
    const long alloc_size = 1073741824;
    fprintf(stderr, "alloc_size: %ld\n", alloc_size);
    fflush(stderr);

    void * aligned_memory = NULL;
    int result = posix_memalign(&aligned_memory, sysconf(_SC_PAGESIZE), alloc_size);
    if (result != 0) {
        fputs("failed to allocate memory\n", stderr);
        fflush(stderr);
    } else {
        fputs("done\n", stderr);
        fflush(stderr);
    }
}
```

### Actual Behavior

The Electron process crashes.
Using the same native module from nodejs works fine, so this is an issue with Electron.

### Testcase Gist URL

_No response_

### Additional Information

This issue was brought up by people using [`node-llama-cpp`](https://github.com/withcatai/node-llama-cpp) in their Electron app ([old issue](https://github.com/electron/electron/issues/41513#issuecomment-2094327194), [new issue](https://github.com/withcatai/node-llama-cpp/issues/361)).
`node-llama-cpp` uses [`llama.cpp`](https://github.com/ggerganov/llama.cpp), and `llama.cpp` needs to allocate page-aligned buffers.
Sometimes, those buffers are very large, so it caused the process to crash with some models that need such large buffers.

It's worth noting that this issue only happens on Electron but not on nodejs, so this seems to be an issue with Electron.
To make it easy to reproduce this issue on Apple Silicon Macs with at least 32GB RAM, you can run these commands:
```bash
mkdir repro
cd repro
npm init -y
npm install node-llama-cpp@3.1.1 electron
npx --no node-llama-cpp source build --gpu false
npx electron ./node_modules/node-llama-cpp/dist/cli/cli.js chat --prompt 'Hi there!' --gpu false
# and then select Llama 3.1 8B from the list of models
```

A workaround to this issue that I've found is to use [`vm_allocate`](https://developer.apple.com/documentation/kernel/1585381-vm_allocate) instead of `posix_memalign`, but the issue with `posix_memalign` still remains.
See the [tests I've run](https://github.com/ggerganov/llama.cpp/pull/9875#issuecomment-2418072038) to demonstrate this issue, and how `vm_allocate` workarounds it.

Even though `vm_allocate` can be used to workaround this issue, I think it's worth finding the cause of it to ensure all native node modules work in Electron the same as they work in nodejs (expect the V8 memory cage buffer copy thing, which I don't this is related to this issue).