Skip to content

Commit 07ddd6e

Browse files
Add #guard_page and #crypto_key
1 parent 0974cb3 commit 07ddd6e

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Mmap::Region.open(16384) do |mmap|
3232
mmap.to_slice
3333
3434
35-
# Not a Slice
35+
# SubRegions reference the original Mmap::Region, tracking the offset if resized
3636
rw_region = mmap[0, 4096]
3737
# Do something with the first 4k
3838
rw_region.to_slice
@@ -45,7 +45,7 @@ Mmap::Region.open(16384) do |mmap|
4545
4646
# Create a guard page
4747
guard_region = mmap[12288, 4096]
48-
guard_region.noaccess
48+
guard_region.guard_page
4949
# Crashes program if accessed
5050
guard_region.to_slice[0] = 0_u8
5151
end

spec/mmap_spec.cr

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
require "./spec_helper"
22
require "../src/mmap"
33

4+
# Check restricting type to Module
5+
private def can_to_slice(mmap : Mmap) : Bytes
6+
mmap.to_slice
7+
end
8+
49
describe Mmap do
510
it "maps anon memory" do
611
initial_size = 2048
712
Mmap::Region.open(initial_size) do |mmap|
8-
mmap.to_slice[7] = 7_u8
13+
can_to_slice(mmap)[7] = 7_u8
914
sub1 = mmap[5, 10]
10-
sub1.to_slice[2].should eq 7_u8
15+
can_to_slice(sub1)[2].should eq 7_u8
1116
sub2 = sub1[2, 1]
12-
sub2.to_slice[0].should eq 7_u8
17+
can_to_slice(sub2)[0].should eq 7_u8
1318

1419
expect_raises IndexError do
1520
mmap[initial_size, 1]
@@ -53,6 +58,21 @@ describe Mmap do
5358
pending "msync" do
5459
end
5560

61+
it "guard_page" do
62+
Mmap::Region.open(8192) do |mmap|
63+
mmap.guard_page
64+
# TODO: trap
65+
# mmap.to_slice[0] = 1_u8
66+
end
67+
end
68+
69+
it "guard_page" do
70+
Mmap::Region.open(8192) do |mmap|
71+
mmap.crypto_key
72+
mmap.to_slice[0] = 1_u8
73+
end
74+
end
75+
5676
it "mlock" do
5777
Mmap::Region.open(8192) do |mmap|
5878
mmap.mlock

src/mmap/mmap.cr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require "./*"
22

33
module Mmap
4+
PAGE_SIZE = LibC.sysconf LibC::SC_PAGESIZE
5+
46
class Error < Exception
57
class Closed < Error
68
end
@@ -34,6 +36,9 @@ module Mmap
3436
# Nonblock = LibC::MAP_NONBLOCK # Linux only (requires POPULATE) - currently turns POPULATE in to noop
3537
# Possibly same as Linux POPULATE & NONBLOCK but functional
3638
# PreFaultRead = LibC::MAP_PREFAULT_READ # BSD only
39+
# CryptoKey = LibC::MAP_DONTDUMP | LibC::MAP_DONTFORK # Fails on Linux
40+
CryptoKey = LibC::MAP_DONTDUMP
41+
GuardPage = LibC::MAP_DONTDUMP
3742
end
3843

3944
def self.open(*args)
@@ -87,6 +92,15 @@ module Mmap
8792
raise RuntimeError.from_errno("munlock") if r != 0
8893
end
8994

95+
def crypto_key : Nil
96+
madvise Flags::CryptoKey
97+
end
98+
99+
def guard_page : Nil
100+
madvise Flags::GuardPage
101+
noaccess
102+
end
103+
90104
def to_slice : Bytes
91105
Slice.new to_unsafe, @size
92106
end

0 commit comments

Comments
 (0)