From 453bd71f25627cc05087126b08943822ac4d3c3f Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Wed, 7 Feb 2024 21:48:18 -0500 Subject: [PATCH 1/3] feat: fix mremap on darwin --- pkg/mmap/mmap.go | 11 +++++++---- pkg/mmap/mremap_darwin.go | 13 +++++++++++++ pkg/mmap/mremap_linux.go | 7 +++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 pkg/mmap/mremap_darwin.go create mode 100644 pkg/mmap/mremap_linux.go diff --git a/pkg/mmap/mmap.go b/pkg/mmap/mmap.go index 8a32f617..3c0d219d 100644 --- a/pkg/mmap/mmap.go +++ b/pkg/mmap/mmap.go @@ -13,6 +13,9 @@ type MemoryMappedFile struct { file *os.File bytes []byte seek int64 + + // parameters used for remapping. + prot, flags int } var _ io.ReadWriteSeeker = &MemoryMappedFile{} @@ -35,13 +38,13 @@ func NewMemoryMappedFile(f *os.File, prot int) (*MemoryMappedFile, error) { return nil, fmt.Errorf("stat: %v", err) } if fi.Size() == 0 { - return &MemoryMappedFile{file: f, bytes: nil, seek: 0}, nil + return &MemoryMappedFile{file: f, bytes: nil, seek: 0, prot: prot, flags: unix.MAP_SHARED}, nil } b, err := unix.Mmap(int(fd), 0, int(fi.Size()), prot, unix.MAP_SHARED) if err != nil { return nil, fmt.Errorf("mmap: %v", err) } - return &MemoryMappedFile{file: f, bytes: b, seek: 0}, nil + return &MemoryMappedFile{file: f, bytes: b, seek: 0, prot: prot, flags: unix.MAP_SHARED}, nil } // Open is a convenience function to open a file and memory map it. @@ -141,13 +144,13 @@ func (m *MemoryMappedFile) WriteAt(b []byte, off int64) (int, error) { return 0, err } if m.bytes == nil { - m.bytes, err = unix.Mmap(int(m.file.Fd()), 0, int(fi.Size()), unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED) + m.bytes, err = unix.Mmap(int(m.file.Fd()), 0, int(fi.Size()), m.prot, m.flags) if err != nil { return 0, fmt.Errorf("mmap: %v", err) } return len(b), nil } - b, err := unix.Mremap(m.bytes, int(fi.Size()), unix.MREMAP_MAYMOVE) + b, err := mremap(m.bytes, int(m.file.Fd()), int(fi.Size()), m.prot, m.flags) if err != nil { return 0, fmt.Errorf("mmap: %v", err) } diff --git a/pkg/mmap/mremap_darwin.go b/pkg/mmap/mremap_darwin.go new file mode 100644 index 00000000..46f892b9 --- /dev/null +++ b/pkg/mmap/mremap_darwin.go @@ -0,0 +1,13 @@ +package mmap + +import "golang.org/x/sys/unix" + +func mremap(oldAddress []byte, fd, newSize, prot, flags int) ([]byte, error) { + // darwin doesn't have mremap, so we have to munmap and mmap the new size + + // unmap the old address + if err := unix.Munmap(oldAddress); err != nil { + return nil, err + } + return unix.Mmap(fd, 0, newSize, prot, flags) +} diff --git a/pkg/mmap/mremap_linux.go b/pkg/mmap/mremap_linux.go new file mode 100644 index 00000000..03c1c82d --- /dev/null +++ b/pkg/mmap/mremap_linux.go @@ -0,0 +1,7 @@ +package mmap + +import "golang.org/x/sys/unix" + +func mremap(oldAddress []byte, fd, newSize, prot, flags int) ([]byte, error) { + return unix.Mremap(oldAddress, newSize, unix.MREMAP_MAYMOVE) +} From a3c6d0bc11c0f1c386f0515fc6ac630a7387a2c5 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Wed, 7 Feb 2024 21:48:58 -0500 Subject: [PATCH 2/3] test --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aa672b79..11c9855e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,7 @@ on: jobs: go-test: - runs-on: ubuntu-latest + runs-on: [ubuntu-latest, macos-latest] steps: - uses: actions/checkout@v3 - uses: actions/setup-go@v4 @@ -20,7 +20,7 @@ jobs: - name: Use Node.js 18 uses: actions/setup-node@v3 with: - node-version: '18' + node-version: "18" - run: npm ci - run: npm run build - run: npm test From cdfe4a2d87749a14e293a4094542d2c0a7c03f68 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Wed, 7 Feb 2024 21:51:30 -0500 Subject: [PATCH 3/3] fix matrix --- .github/workflows/test.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 11c9855e..67e72fce 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,10 @@ on: jobs: go-test: - runs-on: [ubuntu-latest, macos-latest] + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - uses: actions/setup-go@v4