1
0
mirror of https://github.com/golang/go synced 2024-11-19 10:44:47 -07:00

runtime: add mmap of null page just in case

R=r, iant, robert.swiecki, rsc1
CC=golang-dev
https://golang.org/cl/1904044
This commit is contained in:
Russ Cox 2010-09-28 20:30:01 -04:00
parent 9499856488
commit 649aab835f
24 changed files with 81 additions and 14 deletions

View File

@ -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,

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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,

View File

@ -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)
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);
}

View File

@ -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,

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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,

View File

@ -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)
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);
}

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -14,11 +14,11 @@ 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 {
printf("mmap: errno=%p\n", p);
}
exit(2); exit(2);
} }
printf("mmap: errno=%p\n", p);
throw("mmap");
}
return p; return p;
} }
@ -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);
}

View File

@ -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();

View File

@ -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

View File

@ -31,3 +31,7 @@ SysFree(void *v, uintptr n)
runtime_munmap(v, n); runtime_munmap(v, n);
} }
void
SysMemInit(void)
{
}

View File

@ -44,3 +44,7 @@ SysUnused(void *v, uintptr n)
USED(v, n); USED(v, n);
} }
void
SysMemInit(void)
{
}

View File

@ -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)
{
}