From 38f8698ce6a272507bfc79a3415e1c8213d56dd7 Mon Sep 17 00:00:00 2001 From: deadalnix Date: Mon, 26 Mar 2012 03:18:11 +0200 Subject: [PATCH] Nullpointererror with makefile. --- posix.mak | 3 +++ src/core/nullpointererror.d | 42 ++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/posix.mak b/posix.mak index 11775b3a746..ab402c562d5 100644 --- a/posix.mak +++ b/posix.mak @@ -71,6 +71,7 @@ MANIFEST= \ src/core/exception.d \ src/core/math.d \ src/core/memory.d \ + src/core/nullpointererror.d \ src/core/runtime.d \ src/core/simd.d \ src/core/thread.d \ @@ -281,6 +282,7 @@ SRC_D_MODULES = \ core/exception \ core/math \ core/memory \ + core/nullpointererror \ core/runtime \ core/simd \ core/thread \ @@ -416,6 +418,7 @@ DOCS=\ $(DOCDIR)/core_exception.html \ $(DOCDIR)/core_math.html \ $(DOCDIR)/core_memory.html \ + $(DOCDIR)/core_nullpointererror.html \ $(DOCDIR)/core_runtime.html \ $(DOCDIR)/core_simd.html \ $(DOCDIR)/core_thread.html \ diff --git a/src/core/nullpointererror.d b/src/core/nullpointererror.d index c34c2dceec1..73dc6208d53 100644 --- a/src/core/nullpointererror.d +++ b/src/core/nullpointererror.d @@ -14,6 +14,7 @@ module core.nullpointererror; version(linux) { +private : import core.sys.posix.signal; import core.sys.posix.ucontext; @@ -221,8 +222,28 @@ version(X86_64) { } } +// This should be calculated by druntime. +enum PAGE_SIZE = 4096; + +// The first 64Kb are reserved for detecting null pointer deferences. +enum MEMORY_RESERVED_FOR_NULL_DEFERENCE = 4096 * 16; + // User space handler +void sigsegv_userspace_process(void* address) { + // The first page is protected to detect null deference. + if((cast(size_t) address) < MEMORY_RESERVED_FOR_NULL_DEFERENCE) { + throw new NullPointerError(); + } + + throw new SignalError(SIGSEGV); +} + +public : + +/** + * Thrown on posix system when a signal is recieved. Is only throw for SIGSEGV. + */ class SignalError : Error { private int _signum; @@ -236,12 +257,18 @@ class SignalError : Error { super("", file, line, next); } + /** + * Property that returns the signal number. + */ @property int signum() const { return _signum; } } +/** + * Throw on null pointer deference. + */ class NullPointerError : SignalError { this(string file = __FILE__, size_t line = __LINE__, Throwable next = null) { super(SIGSEGV, file, line, next); @@ -252,20 +279,5 @@ class NullPointerError : SignalError { } } -// This should be calculated by druntime. -enum PAGE_SIZE = 4096; - -// The first 64Kb are reserved for detecting null pointer deferences. -enum MEMORY_RESERVED_FOR_NULL_DEFERENCE = 4096 * 16; - -void sigsegv_userspace_process(void* address) { - // The first page is protected to detect null deference. - if((cast(size_t) address) < MEMORY_RESERVED_FOR_NULL_DEFERENCE) { - throw new NullPointerError(); - } - - throw new SignalError(SIGSEGV); -} - }