Skip to content

Commit

Permalink
Merge branch 'jn/chkstk' of github.com:JuliaLang/julia
Browse files Browse the repository at this point in the history
Conflicts:
	Make.inc
	src/init.c
  • Loading branch information
JeffBezanson committed Mar 4, 2013
2 parents 3ca9743 + 7748339 commit 39be18b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
7 changes: 3 additions & 4 deletions Make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ override OS = WINNT
override ARCH = i686
override OPENBLAS_DYNAMIC_ARCH = 1
override CROSS_COMPILE=$(XC_HOST)-
export WINEPATH := '$(BUILD)/lib/julia;$(BUILD)/lib;$(shell $(CROSS_COMPILE)gcc -print-search-dirs | grep programs | sed "s/^programs: =//" | sed "s/:/;/g");C:\\MinGW\\bin;C:\\MinGW\\msys\\1.0\\bin'
else ifeq ($(XC_HOST),x86_64-w64-mingw32)
override OS = WINNT
override ARCH = x86_64
override OPENBLAS_DYNAMIC_ARCH = 1
override CROSS_COMPILE=$(XC_HOST)-
export WINEPATH := '$(BUILD)/lib/julia;$(BUILD)/lib;$(shell $(CROSS_COMPILE)gcc -print-search-dirs | grep programs | sed "s/^programs: =//" | sed "s/:/;/g");C:\\MinGW\\bin;C:\\MinGW\\msys\\1.0\\bin'
export WINEPATH := '$(BUILD)/lib/julia;$(BUILD)/lib;$(shell $(CROSS_COMPILE)gcc -print-search-dirs | grep programs | sed "s/^programs: =//" | sed "s/:/;/g");C:\\MinGW\\bin;C:\\MinGW\\msys\\1.0\\bin;$(BUILD)/Git/bin'
else
$(error "unknown XC_HOST variable set")
endif
Expand Down Expand Up @@ -111,7 +110,7 @@ CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
JCFLAGS = -std=gnu99 -pipe $(fPIC) -fno-strict-aliasing -D_FILE_OFFSET_BITS=64
JCXXFLAGS = -pipe $(fPIC) -fno-rtti
DEBUGFLAGS = -ggdb3 -DDEBUG
DEBUGFLAGS = -ggdb3 -DDEBUG -fstack-protector-all
SHIPFLAGS = -O3 -DNDEBUG -falign-functions
ifneq ($(ARCH), ppc64)
SHIPFLAGS += -momit-leaf-frame-pointer
Expand All @@ -123,7 +122,7 @@ CC = $(CROSS_COMPILE)clang
CXX = $(CROSS_COMPILE)clang++
JCFLAGS = -pipe $(fPIC) -fno-strict-aliasing -D_FILE_OFFSET_BITS=64
JCXXFLAGS = -pipe $(fPIC) -fno-rtti
DEBUGFLAGS = -g -DDEBUG
DEBUGFLAGS = -g -DDEBUG -fstack-protector-all
SHIPFLAGS = -O3 -DNDEBUG
ifeq ($(OS), Darwin)
CC += -mmacosx-version-min=10.6
Expand Down
26 changes: 25 additions & 1 deletion src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ using namespace llvm;
extern "C" {
#include "julia.h"
#include "builtin_proto.h"
void * __stack_chk_guard = NULL;
void __attribute__(()) __stack_chk_fail()
{
/* put your panic function or similar in here */
fprintf(stderr, "warning: stack corruption detected\n");
//assert(0 && "stack corruption detected");
//abort();
}
}

#define CONDITION_REQUIRES_BOOL
Expand Down Expand Up @@ -1981,7 +1989,15 @@ static Function *emit_function(jl_lambda_info_t *lam)
//TODO: this seems to cause problems, but should be made to work eventually
//if (jlrettype == (jl_value_t*)jl_bottom_type)
// f->setDoesNotReturn();

#ifdef DEBUG
#ifdef __WIN32__
AttrBuilder *attr = new AttrBuilder();
attr->addStackAlignmentAttr(16);
attr->addAlignmentAttr(16);
f->addAttribute(~0U, Attributes::get(f->getContext(), *attr));
#endif
f->addFnAttr(Attributes::StackProtectReq);
#endif
ctx.f = f;

// step 5. set up debug info context and create first basic block
Expand Down Expand Up @@ -2507,6 +2523,14 @@ static void init_julia_llvm_env(Module *m)
jl_ExecutionEngine->addGlobalMapping(jlpgcstack_var, (void*)&jl_pgcstack);
#endif

global_to_llvm("__stack_chk_guard", (void*)&__stack_chk_guard);
Function *jl__stack_chk_fail =
Function::Create(FunctionType::get(T_void, false),
Function::ExternalLinkage,
"__stack_chk_fail", jl_Module);
//jl__stack_chk_fail->setDoesNotReturn();
jl_ExecutionEngine->addGlobalMapping(jl__stack_chk_fail, (void*)&__stack_chk_fail);

jltrue_var = global_to_llvm("jl_true", (void*)&jl_true);
jlfalse_var = global_to_llvm("jl_false", (void*)&jl_false);
jlnull_var = global_to_llvm("jl_null", (void*)&jl_null);
Expand Down
16 changes: 15 additions & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,19 +564,33 @@ DLLEXPORT void jl_install_sigint_handler()
}


extern void * __stack_chk_guard;

DLLEXPORT int julia_trampoline(int argc, char **argv, int (*pmain)(int ac,char *av[]))
{
#if defined(_WIN32) //&& !defined(_WIN64)
SetUnhandledExceptionFilter(exception_handler);
#endif
unsigned char * p = (unsigned char *) &__stack_chk_guard;
char a = p[sizeof(__stack_chk_guard)-1];
char b = p[sizeof(__stack_chk_guard)-2];
char c = p[0];
/* If you have the ability to generate random numbers in your kernel then use them */
p[sizeof(__stack_chk_guard)-1] = 255;
p[sizeof(__stack_chk_guard)-2] = '\n';
p[0] = 0;
#ifdef COPY_STACKS
// initialize base context of root task
jl_root_task->stackbase = (char*)&argc;
if (jl_setjmp(jl_root_task->base_ctx, 0)) {
jl_switch_stack(jl_current_task, jl_jmp_target);
}
#endif
return pmain(argc, argv);
int ret = pmain(argc, argv);
p[sizeof(__stack_chk_guard)-1] = a;
p[sizeof(__stack_chk_guard)-2] = b;
p[0] = c;
return ret;
}

jl_function_t *jl_typeinf_func=NULL;
Expand Down

0 comments on commit 39be18b

Please sign in to comment.