Skip to content

Commit

Permalink
fix stack-smashing protector usage across platforms by introducing a …
Browse files Browse the repository at this point in the history
…HAVE_SSP define flag
  • Loading branch information
vtjnash committed Dec 14, 2014
1 parent cdb6f7c commit d3d6291
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 40 deletions.
20 changes: 16 additions & 4 deletions Make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ endif
# libc++ is standard on OS X 10.9, but not for earlier releases
USE_LIBCPP = 0

# assume we don't have LIBSSP support in our compiler, will enable later if likely true
HAVE_SSP = 0

# Prevent picking up $ARCH from the environment variables
ARCH=

Expand Down Expand Up @@ -355,16 +358,16 @@ CPP = $(CC) -E
AR := $(CROSS_COMPILE)ar
AS := $(CROSS_COMPILE)as
LD := $(CROSS_COMPILE)ld
else
else #USEMSVC
CPP = $(CC) -EP
AR := lib
ifeq ($(ARCH),x86_64)
AS := ml64
else
AS := ml
endif
endif #ARCH
LD := link
endif
endif #USEMSVC
RANLIB := $(CROSS_COMPILE)ranlib


Expand Down Expand Up @@ -674,17 +677,19 @@ OSLIBS += -ldl -Wl,-w -framework CoreFoundation -framework CoreServices $(LIBUNW
WHOLE_ARCHIVE = -Xlinker -all_load
NO_WHOLE_ARCHIVE =
JLDFLAGS =
HAVE_SSP = 1
endif

ifeq ($(OS), WINNT)
ifneq ($(USEMSVC), 1)
HAVE_SSP = 1
OSLIBS += -Wl,--export-all-symbols -Wl,--version-script=$(JULIAHOME)/src/julia.expmap \
$(NO_WHOLE_ARCHIVE) -lpsapi -lkernel32 -lws2_32 -liphlpapi -lwinmm -ldbghelp
JLDFLAGS = -Wl,--stack,8388608
ifeq ($(ARCH),i686)
JLDFLAGS += -Wl,--large-address-aware
endif
else
else #USEMSVC
OSLIBS += kernel32.lib ws2_32.lib psapi.lib advapi32.lib iphlpapi.lib shell32.lib winmm.lib
JLDFLAGS = -stack:8388608
endif
Expand Down Expand Up @@ -734,6 +739,13 @@ LIBFFTWNAME = libmkl_rt
LIBFFTWFNAME = libmkl_rt
endif

ifeq ($(HAVE_SSP),1)
JCPPFLAGS += -DHAVE_SSP=1
ifeq ($(USEGCC),1)
OSLIBS += -lssp
endif
endif

# ATLAS

# ATLAS must have been previously built with "make -C deps compile-atlas" (without -jN),
Expand Down
2 changes: 1 addition & 1 deletion deps/Versions.make
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LLVM_VER = 3.3
LLVM_VER = 3.5.0
LLVM_LIB_SUFFIX =
PCRE_VER = 8.36
DSFMT_VER = 2.2
Expand Down
1 change: 1 addition & 0 deletions doc/juliadoc
Submodule juliadoc added at bab3c6
7 changes: 0 additions & 7 deletions doc/manual/embedding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ We start with a simple C program that initializes Julia and calls some Julia cod

int main(int argc, char *argv[])
{
/* optional: randomize the stack guard */
char a=255, b='\n', c=0;
SWAP_STACK_CHK_GUARD(a,b,c);

/* required: setup the julia context */
jl_init(NULL);

Expand All @@ -34,9 +30,6 @@ We start with a simple C program that initializes Julia and calls some Julia cod
and run all finalizers
*/
jl_atexit_hook();

/* if the stack guard is set: reset the stack guard */
SWAP_STACK_CHK_GUARD(a,b,c);
return 0;
}

Expand Down
3 changes: 0 additions & 3 deletions examples/embedding.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ double my_c_sqrt(double x)

int main()
{
char a=255, b='\n', c=0;
SWAP_STACK_CHK_GUARD(a,b,c);
jl_init(NULL);

{
Expand Down Expand Up @@ -97,6 +95,5 @@ int main()
}

jl_atexit_hook();
SWAP_STACK_CHK_GUARD(a,b,c);
return 0;
}
7 changes: 6 additions & 1 deletion src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ extern "C" {

#include "builtin_proto.h"

extern void *__stack_chk_guard;
#ifdef HAVE_SSP
extern uintptr_t __stack_chk_guard;
extern void __stack_chk_fail();
#else
uintptr_t __stack_chk_guard = (uintptr_t)0xBAD57ACCBAD67ACC; // 0xBADSTACKBADSTACK
#if defined(_OS_WINDOWS_) && !defined(_COMPILER_MINGW_)
void __stack_chk_fail()
#else
Expand All @@ -132,6 +136,7 @@ void __attribute__(()) __stack_chk_fail()
fprintf(stderr, "fatal error: stack corruption detected\n");
abort(); // end with abort, since the compiler destroyed the stack upon entry to this function
}
#endif

#ifdef _OS_WINDOWS_
#if defined(_CPU_X86_64_)
Expand Down
4 changes: 0 additions & 4 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ jl_compileropts_t jl_compileropts = { NULL, // julia_home
int jl_boot_file_loaded = 0;
int exit_on_sigint = 0;

void *__stack_chk_guard = NULL;
DLLEXPORT unsigned char *jl_stack_chk_guard =
(unsigned char *)&__stack_chk_guard;

char *jl_stack_lo;
char *jl_stack_hi;
size_t jl_page_size;
Expand Down
17 changes: 0 additions & 17 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1347,23 +1347,6 @@ extern DLLEXPORT jl_compileropts_t jl_compileropts;
#define JL_COMPILEROPT_DUMPBITCODE_ON 1
#define JL_COMPILEROPT_DUMPBITCODE_OFF 2

/* If you have the ability to generate random numbers
* in your kernel then they should be used here */
DLLEXPORT extern unsigned char *jl_stack_chk_guard;
#define SWAP_STACK_CHK_GUARD(a,b,c) do { \
a ^= jl_stack_chk_guard[sizeof(void*)-1]; \
jl_stack_chk_guard[sizeof(void*)-1] ^= a; \
a ^= jl_stack_chk_guard[sizeof(void*)-1]; \
\
b ^= jl_stack_chk_guard[sizeof(void*)-2]; \
jl_stack_chk_guard[sizeof(void*)-2] ^= b; \
b ^= jl_stack_chk_guard[sizeof(void*)-2]; \
\
c ^= jl_stack_chk_guard[0]; \
jl_stack_chk_guard[0] ^= c; \
c ^= jl_stack_chk_guard[0]; \
} while (0)

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 0 additions & 3 deletions ui/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,6 @@ int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
argv[i] = (wchar_t*)arg;
}
#endif
char a=255,b='\n',c=0;
SWAP_STACK_CHK_GUARD(a,b,c);
libsupport_init();
parse_opts(&argc, (char***)&argv);
if (lisp_prompt) {
Expand All @@ -342,7 +340,6 @@ int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
int ret = true_main(argc, (char**)argv);
jl_atexit_hook();
julia_save();
SWAP_STACK_CHK_GUARD(a,b,c);
return ret;
}

Expand Down

0 comments on commit d3d6291

Please sign in to comment.