Commit 649aab83 authored by Russ Cox's avatar Russ Cox

runtime: add mmap of null page just in case

R=r, iant, robert.swiecki, rsc1
CC=golang-dev
https://golang.org/cl/1904044
parent 94998564
...@@ -10,6 +10,7 @@ enum { ...@@ -10,6 +10,7 @@ enum {
PROT_EXEC = 0x4, PROT_EXEC = 0x4,
MAP_ANON = 0x1000, MAP_ANON = 0x1000,
MAP_PRIVATE = 0x2, MAP_PRIVATE = 0x2,
MAP_FIXED = 0x10,
MACH_MSG_TYPE_MOVE_RECEIVE = 0x10, MACH_MSG_TYPE_MOVE_RECEIVE = 0x10,
MACH_MSG_TYPE_MOVE_SEND = 0x11, MACH_MSG_TYPE_MOVE_SEND = 0x11,
MACH_MSG_TYPE_MOVE_SEND_ONCE = 0x12, MACH_MSG_TYPE_MOVE_SEND_ONCE = 0x12,
......
...@@ -38,8 +38,6 @@ TEXT write(SB),7,$0 ...@@ -38,8 +38,6 @@ TEXT write(SB),7,$0
TEXT ·mmap(SB),7,$0 TEXT ·mmap(SB),7,$0
MOVL $197, AX MOVL $197, AX
INT $0x80 INT $0x80
JAE 2(PC)
CALL notok(SB)
RET RET
TEXT ·munmap(SB),7,$0 TEXT ·munmap(SB),7,$0
......
...@@ -10,6 +10,7 @@ enum { ...@@ -10,6 +10,7 @@ enum {
PROT_EXEC = 0x4, PROT_EXEC = 0x4,
MAP_ANON = 0x1000, MAP_ANON = 0x1000,
MAP_PRIVATE = 0x2, MAP_PRIVATE = 0x2,
MAP_FIXED = 0x10,
MACH_MSG_TYPE_MOVE_RECEIVE = 0x10, MACH_MSG_TYPE_MOVE_RECEIVE = 0x10,
MACH_MSG_TYPE_MOVE_SEND = 0x11, MACH_MSG_TYPE_MOVE_SEND = 0x11,
MACH_MSG_TYPE_MOVE_SEND_ONCE = 0x12, MACH_MSG_TYPE_MOVE_SEND_ONCE = 0x12,
......
...@@ -103,8 +103,6 @@ TEXT ·mmap(SB),7,$0 ...@@ -103,8 +103,6 @@ TEXT ·mmap(SB),7,$0
MOVL 36(SP), R9 // arg 6 offset MOVL 36(SP), R9 // arg 6 offset
MOVL $(0x2000000+197), AX // syscall entry MOVL $(0x2000000+197), AX // syscall entry
SYSCALL SYSCALL
JCC 2(PC)
CALL notok(SB)
RET RET
TEXT ·munmap(SB),7,$0 TEXT ·munmap(SB),7,$0
......
...@@ -26,6 +26,7 @@ enum { ...@@ -26,6 +26,7 @@ enum {
$MAP_ANON = MAP_ANON, $MAP_ANON = MAP_ANON,
$MAP_PRIVATE = MAP_PRIVATE, $MAP_PRIVATE = MAP_PRIVATE,
$MAP_FIXED = MAP_FIXED,
$MACH_MSG_TYPE_MOVE_RECEIVE = MACH_MSG_TYPE_MOVE_RECEIVE, $MACH_MSG_TYPE_MOVE_RECEIVE = MACH_MSG_TYPE_MOVE_RECEIVE,
$MACH_MSG_TYPE_MOVE_SEND = MACH_MSG_TYPE_MOVE_SEND, $MACH_MSG_TYPE_MOVE_SEND = MACH_MSG_TYPE_MOVE_SEND,
......
...@@ -6,8 +6,15 @@ ...@@ -6,8 +6,15 @@
void* void*
SysAlloc(uintptr n) SysAlloc(uintptr n)
{ {
void *v;
mstats.sys += n; mstats.sys += n;
return runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, -1, 0); v = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, -1, 0);
if(v < (void*)4096) {
printf("mmap: errno=%p\n", v);
throw("mmap");
}
return v;
} }
void void
...@@ -25,3 +32,15 @@ SysFree(void *v, uintptr n) ...@@ -25,3 +32,15 @@ SysFree(void *v, uintptr n)
runtime_munmap(v, n); runtime_munmap(v, n);
} }
void
SysMemInit(void)
{
// Code generators assume that references to addresses
// on the first page will fault. Map the page explicitly with
// no permissions, to head off possible bugs like the system
// allocating that page as the virtual address space fills.
// Ignore any error, since other systems might be smart
// enough to never allow anything there.
// runtime_mmap(nil, 4096, PROT_NONE, MAP_FIXED|MAP_ANON|MAP_PRIVATE, -1, 0);
}
...@@ -10,6 +10,7 @@ enum { ...@@ -10,6 +10,7 @@ enum {
PROT_EXEC = 0x4, PROT_EXEC = 0x4,
MAP_ANON = 0x1000, MAP_ANON = 0x1000,
MAP_PRIVATE = 0x2, MAP_PRIVATE = 0x2,
MAP_FIXED = 0x10,
SA_SIGINFO = 0x40, SA_SIGINFO = 0x40,
SA_RESTART = 0x2, SA_RESTART = 0x2,
SA_ONSTACK = 0x1, SA_ONSTACK = 0x1,
......
...@@ -80,8 +80,6 @@ TEXT ·mmap(SB),7,$32 ...@@ -80,8 +80,6 @@ TEXT ·mmap(SB),7,$32
STOSL STOSL
MOVL $477, AX MOVL $477, AX
INT $0x80 INT $0x80
JAE 2(PC)
CALL notok(SB)
RET RET
TEXT ·munmap(SB),7,$-4 TEXT ·munmap(SB),7,$-4
......
...@@ -10,6 +10,7 @@ enum { ...@@ -10,6 +10,7 @@ enum {
PROT_EXEC = 0x4, PROT_EXEC = 0x4,
MAP_ANON = 0x1000, MAP_ANON = 0x1000,
MAP_PRIVATE = 0x2, MAP_PRIVATE = 0x2,
MAP_FIXED = 0x10,
SA_SIGINFO = 0x40, SA_SIGINFO = 0x40,
SA_RESTART = 0x2, SA_RESTART = 0x2,
SA_ONSTACK = 0x1, SA_ONSTACK = 0x1,
......
...@@ -112,8 +112,6 @@ TEXT ·mmap(SB),7,$0 ...@@ -112,8 +112,6 @@ TEXT ·mmap(SB),7,$0
MOVL 36(SP), R9 // arg 6 offset MOVL 36(SP), R9 // arg 6 offset
MOVL $477, AX MOVL $477, AX
SYSCALL SYSCALL
JCC 2(PC)
CALL notok(SB)
RET RET
TEXT ·munmap(SB),7,$0 TEXT ·munmap(SB),7,$0
......
...@@ -28,6 +28,7 @@ enum { ...@@ -28,6 +28,7 @@ enum {
$MAP_ANON = MAP_ANON, $MAP_ANON = MAP_ANON,
$MAP_PRIVATE = MAP_PRIVATE, $MAP_PRIVATE = MAP_PRIVATE,
$MAP_FIXED = MAP_FIXED,
$SA_SIGINFO = SA_SIGINFO, $SA_SIGINFO = SA_SIGINFO,
$SA_RESTART = SA_RESTART, $SA_RESTART = SA_RESTART,
......
...@@ -6,8 +6,15 @@ ...@@ -6,8 +6,15 @@
void* void*
SysAlloc(uintptr n) SysAlloc(uintptr n)
{ {
void *v;
mstats.sys += n; mstats.sys += n;
return runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, -1, 0); v = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, -1, 0);
if(v < (void*)4096) {
printf("mmap: errno=%p\n", p);
throw("mmap");
}
return v;
} }
void void
...@@ -25,3 +32,15 @@ SysFree(void *v, uintptr n) ...@@ -25,3 +32,15 @@ SysFree(void *v, uintptr n)
runtime_munmap(v, n); runtime_munmap(v, n);
} }
void
SysMemInit(void)
{
// Code generators assume that references to addresses
// on the first page will fault. Map the page explicitly with
// no permissions, to head off possible bugs like the system
// allocating that page as the virtual address space fills.
// Ignore any error, since other systems might be smart
// enough to never allow anything there.
runtime_mmap(nil, 4096, PROT_NONE, MAP_FIXED|MAP_ANON|MAP_PRIVATE, -1, 0);
}
...@@ -10,6 +10,7 @@ enum { ...@@ -10,6 +10,7 @@ enum {
PROT_EXEC = 0x4, PROT_EXEC = 0x4,
MAP_ANON = 0x20, MAP_ANON = 0x20,
MAP_PRIVATE = 0x2, MAP_PRIVATE = 0x2,
MAP_FIXED = 0x10,
SA_RESTART = 0x10000000, SA_RESTART = 0x10000000,
SA_ONSTACK = 0x8000000, SA_ONSTACK = 0x8000000,
SA_RESTORER = 0x4000000, SA_RESTORER = 0x4000000,
......
...@@ -10,6 +10,7 @@ enum { ...@@ -10,6 +10,7 @@ enum {
PROT_EXEC = 0x4, PROT_EXEC = 0x4,
MAP_ANON = 0x20, MAP_ANON = 0x20,
MAP_PRIVATE = 0x2, MAP_PRIVATE = 0x2,
MAP_FIXED = 0x10,
SA_RESTART = 0x10000000, SA_RESTART = 0x10000000,
SA_ONSTACK = 0x8000000, SA_ONSTACK = 0x8000000,
SA_RESTORER = 0x4000000, SA_RESTORER = 0x4000000,
......
...@@ -10,6 +10,7 @@ enum { ...@@ -10,6 +10,7 @@ enum {
PROT_EXEC = 0x4, PROT_EXEC = 0x4,
MAP_ANON = 0x20, MAP_ANON = 0x20,
MAP_PRIVATE = 0x2, MAP_PRIVATE = 0x2,
MAP_FIXED = 0x10,
SA_RESTART = 0x10000000, SA_RESTART = 0x10000000,
SA_ONSTACK = 0x8000000, SA_ONSTACK = 0x8000000,
SA_RESTORER = 0x4000000, SA_RESTORER = 0x4000000,
......
...@@ -27,6 +27,7 @@ enum { ...@@ -27,6 +27,7 @@ enum {
$MAP_ANON = MAP_ANONYMOUS, $MAP_ANON = MAP_ANONYMOUS,
$MAP_PRIVATE = MAP_PRIVATE, $MAP_PRIVATE = MAP_PRIVATE,
$MAP_FIXED = MAP_FIXED,
$SA_RESTART = SA_RESTART, $SA_RESTART = SA_RESTART,
$SA_ONSTACK = SA_ONSTACK, $SA_ONSTACK = SA_ONSTACK,
......
...@@ -47,6 +47,7 @@ enum { ...@@ -47,6 +47,7 @@ enum {
$MAP_ANON = MAP_ANONYMOUS, $MAP_ANON = MAP_ANONYMOUS,
$MAP_PRIVATE = MAP_PRIVATE, $MAP_PRIVATE = MAP_PRIVATE,
$MAP_FIXED = MAP_FIXED,
$SA_RESTART = SA_RESTART, $SA_RESTART = SA_RESTART,
$SA_ONSTACK = SA_ONSTACK, $SA_ONSTACK = SA_ONSTACK,
......
...@@ -31,6 +31,7 @@ enum { ...@@ -31,6 +31,7 @@ enum {
$MAP_ANON = MAP_ANONYMOUS, $MAP_ANON = MAP_ANONYMOUS,
$MAP_PRIVATE = MAP_PRIVATE, $MAP_PRIVATE = MAP_PRIVATE,
$MAP_FIXED = MAP_FIXED,
$SA_RESTART = SA_RESTART, $SA_RESTART = SA_RESTART,
$SA_ONSTACK = SA_ONSTACK, $SA_ONSTACK = SA_ONSTACK,
......
...@@ -14,10 +14,10 @@ SysAlloc(uintptr n) ...@@ -14,10 +14,10 @@ SysAlloc(uintptr n)
if(p == (void*)EACCES) { if(p == (void*)EACCES) {
printf("mmap: access denied\n"); printf("mmap: access denied\n");
printf("If you're running SELinux, enable execmem for this process.\n"); printf("If you're running SELinux, enable execmem for this process.\n");
} else { exit(2);
printf("mmap: errno=%p\n", p);
} }
exit(2); printf("mmap: errno=%p\n", p);
throw("mmap");
} }
return p; return p;
} }
...@@ -37,3 +37,14 @@ SysFree(void *v, uintptr n) ...@@ -37,3 +37,14 @@ SysFree(void *v, uintptr n)
runtime_munmap(v, n); runtime_munmap(v, n);
} }
void
SysMemInit(void)
{
// Code generators assume that references to addresses
// on the first page will fault. Map the page explicitly with
// no permissions, to head off possible bugs like the system
// allocating that page as the virtual address space fills.
// Ignore any error, since other systems might be smart
// enough to never allow anything there.
runtime_mmap(nil, 4096, PROT_NONE, MAP_FIXED|MAP_ANON|MAP_PRIVATE, -1, 0);
}
...@@ -245,6 +245,7 @@ allocmcache(void) ...@@ -245,6 +245,7 @@ allocmcache(void)
void void
mallocinit(void) mallocinit(void)
{ {
SysMemInit();
InitSizes(); InitSizes();
MHeap_Init(&mheap, SysAlloc); MHeap_Init(&mheap, SysAlloc);
m->mcache = allocmcache(); m->mcache = allocmcache();
......
...@@ -138,7 +138,7 @@ struct MLink ...@@ -138,7 +138,7 @@ struct MLink
void* SysAlloc(uintptr nbytes); void* SysAlloc(uintptr nbytes);
void SysFree(void *v, uintptr nbytes); void SysFree(void *v, uintptr nbytes);
void SysUnused(void *v, uintptr nbytes); void SysUnused(void *v, uintptr nbytes);
void SysMemInit(void);
// FixAlloc is a simple free-list allocator for fixed size objects. // FixAlloc is a simple free-list allocator for fixed size objects.
// Malloc uses a FixAlloc wrapped around SysAlloc to manages its // Malloc uses a FixAlloc wrapped around SysAlloc to manages its
......
...@@ -31,3 +31,7 @@ SysFree(void *v, uintptr n) ...@@ -31,3 +31,7 @@ SysFree(void *v, uintptr n)
runtime_munmap(v, n); runtime_munmap(v, n);
} }
void
SysMemInit(void)
{
}
...@@ -44,3 +44,7 @@ SysUnused(void *v, uintptr n) ...@@ -44,3 +44,7 @@ SysUnused(void *v, uintptr n)
USED(v, n); USED(v, n);
} }
void
SysMemInit(void)
{
}
...@@ -33,3 +33,8 @@ SysFree(void *v, uintptr n) ...@@ -33,3 +33,8 @@ SysFree(void *v, uintptr n)
{ {
stdcall(VirtualFree, 3, v, n, MEM_RELEASE); stdcall(VirtualFree, 3, v, n, MEM_RELEASE);
} }
void
SysMemInit(void)
{
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment