Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Handle page protection errors by default as D errors on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Jul 11, 2018
1 parent b5d124a commit 288c872
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions changelog/trap_memory_error.dd
@@ -0,0 +1,18 @@
Page protection errors are now handled using D errors (exceptions) on Linux

Consider this simple faulty program:

---
void main() {
int* ptr = null;
(*ptr)++;
}
---

$(CONSOLE dmd -g -run test.d
etc.linux.memoryerror.NullPointerError@src/etc/linux/memoryerror.d(325)
$(NDASH)$(NDASH)$(NDASH)$(NDASH)$(NDASH)$(NDASH)$(NDASH)$(NDASH)$(NDASH)$(NDASH)
??:? void etc.linux.memoryerror.sigsegvUserspaceProcess(void*) [0x8353df09]
??:? void etc.linux.memoryerror.sigsegvDataHandler() [0x8353de4a]
test.d:2 _Dmain [0x8353c2b5]
)
7 changes: 7 additions & 0 deletions src/rt/dmain2.d
Expand Up @@ -454,6 +454,13 @@ extern (C) int _d_run_main(int argc, char **argv, MainFunc mainFunc)

bool trapExceptions = rt_trapExceptions;

// Handle page protection errors using D errors (exceptions) when supported
version (linux)
{
import etc.linux.memoryerror : registerMemoryErrorHandler;
registerMemoryErrorHandler();
}

version (Windows)
{
if (IsDebuggerPresent())
Expand Down
3 changes: 2 additions & 1 deletion test/exceptions/Makefile
Expand Up @@ -3,7 +3,7 @@ include ../common.mak
TESTS:=stderr_msg unittest_assert invalid_memory_operation unknown_gc static_dtor future_message

ifeq ($(OS)-$(BUILD),linux-debug)
TESTS:=$(TESTS) line_trace rt_trap_exceptions
TESTS:=$(TESTS) line_trace rt_trap_exceptions rt_trap_memory_error
LINE_TRACE_DFLAGS:=-L--export-dynamic
endif
ifeq ($(OS)-$(BUILD),freebsd-debug)
Expand Down Expand Up @@ -48,6 +48,7 @@ $(ROOT)/future_message.done: STDERR_EXP="exception I have a custom message. exce
$(ROOT)/static_dtor.done: NEGATE=!
$(ROOT)/rt_trap_exceptions.done: STDERR_EXP="uncaught exception\nobject.Exception@rt_trap_exceptions.d(11): exception"
$(ROOT)/rt_trap_exceptions.done: NEGATE=!
$(ROOT)/rt_trap_memory_error.done: STDERR_EXP="etc.linux.memoryerror.NullPointerError@src/etc/linux/memoryerror.d"
$(ROOT)/%.done: $(ROOT)/%
@echo Testing $*
$(NEGATE) $(QUIET)$(TIMELIMIT)$(ROOT)/$* $(RUN_ARGS) 2>&1 1>/dev/null | grep -qF $(STDERR_EXP)
Expand Down
4 changes: 4 additions & 0 deletions test/exceptions/src/rt_trap_memory_error.d
@@ -0,0 +1,4 @@
void main() {
int* ptr = null;
(*ptr)++;
}

0 comments on commit 288c872

Please sign in to comment.