2009-12-20 00:10:26 +03:00
|
|
|
/*
|
2009-12-20 03:53:49 +03:00
|
|
|
* TCC - Tiny C Compiler - Support for -run switch
|
2009-12-20 00:10:26 +03:00
|
|
|
*
|
|
|
|
* Copyright (c) 2001-2004 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
#include "tcc.h"
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2012-03-05 23:15:56 +04:00
|
|
|
/* only native compiler supports -run */
|
|
|
|
#ifdef TCC_IS_NATIVE
|
|
|
|
|
2016-10-18 00:24:01 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
# include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
2016-10-18 00:24:01 +03:00
|
|
|
# ifndef _WIN32
|
|
|
|
# include <signal.h>
|
|
|
|
# ifndef __OpenBSD__
|
|
|
|
# include <sys/ucontext.h>
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# define ucontext_t CONTEXT
|
|
|
|
# endif
|
2011-08-11 18:55:30 +04:00
|
|
|
ST_DATA int rt_num_callers = 6;
|
|
|
|
ST_DATA const char **rt_bound_error_msg;
|
|
|
|
ST_DATA void *rt_prog_main;
|
2016-10-18 00:24:01 +03:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level);
|
|
|
|
static void rt_error(ucontext_t *uc, const char *fmt, ...);
|
|
|
|
static void set_exception_handler(void);
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
static void set_pages_executable(void *ptr, unsigned long length);
|
|
|
|
static int tcc_relocate_ex(TCCState *s1, void *ptr);
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2012-03-03 20:12:06 +04:00
|
|
|
#ifdef _WIN64
|
2016-10-19 20:21:27 +03:00
|
|
|
static void *win64_add_function_table(TCCState *s1);
|
|
|
|
static void win64_del_function_table(void *);
|
2011-07-14 21:09:49 +04:00
|
|
|
#endif
|
|
|
|
|
2016-11-20 16:50:56 +03:00
|
|
|
// #define HAVE_SELINUX
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
/* Do all relocations (needed before using tcc_get_symbol())
|
|
|
|
Returns -1 on error. */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2012-09-01 13:33:34 +04:00
|
|
|
LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2016-10-19 20:21:27 +03:00
|
|
|
int size; void *mem;
|
2012-09-01 13:33:34 +04:00
|
|
|
|
|
|
|
if (TCC_RELOCATE_AUTO != ptr)
|
|
|
|
return tcc_relocate_ex(s1, ptr);
|
|
|
|
|
2016-10-19 20:21:27 +03:00
|
|
|
size = tcc_relocate_ex(s1, NULL);
|
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
2013-02-12 22:13:28 +04:00
|
|
|
|
2010-04-24 14:28:54 +04:00
|
|
|
#ifdef HAVE_SELINUX
|
2014-01-12 09:26:41 +04:00
|
|
|
{ /* Use mmap instead of malloc for Selinux. Ref:
|
|
|
|
http://www.gnu.org/s/libc/manual/html_node/File-Size.html */
|
|
|
|
|
|
|
|
char tmpfname[] = "/tmp/.tccrunXXXXXX";
|
|
|
|
int fd = mkstemp (tmpfname);
|
2016-10-19 20:21:27 +03:00
|
|
|
void *wr_mem;
|
2014-01-12 09:26:41 +04:00
|
|
|
|
|
|
|
unlink (tmpfname);
|
2016-10-19 20:21:27 +03:00
|
|
|
ftruncate (fd, size);
|
2014-01-10 22:23:11 +04:00
|
|
|
|
2016-10-19 20:21:27 +03:00
|
|
|
wr_mem = mmap (NULL, size, PROT_READ|PROT_WRITE,
|
2014-01-12 09:26:41 +04:00
|
|
|
MAP_SHARED, fd, 0);
|
2016-10-19 20:21:27 +03:00
|
|
|
if (wr_mem == MAP_FAILED)
|
2014-01-12 09:26:41 +04:00
|
|
|
tcc_error("/tmp not writeable");
|
2016-10-19 20:21:27 +03:00
|
|
|
mem = mmap (NULL, size, PROT_READ|PROT_EXEC,
|
2014-01-12 09:26:41 +04:00
|
|
|
MAP_SHARED, fd, 0);
|
2016-10-19 20:21:27 +03:00
|
|
|
if (mem == MAP_FAILED)
|
2014-01-12 09:26:41 +04:00
|
|
|
tcc_error("/tmp not executable");
|
2013-02-12 22:13:28 +04:00
|
|
|
|
2016-10-19 20:21:27 +03:00
|
|
|
tcc_relocate_ex(s1, wr_mem);
|
|
|
|
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
|
|
|
|
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, wr_mem);
|
|
|
|
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem);
|
2010-04-24 14:28:54 +04:00
|
|
|
}
|
|
|
|
#else
|
2016-10-19 20:21:27 +03:00
|
|
|
mem = tcc_malloc(size);
|
|
|
|
tcc_relocate_ex(s1, mem); /* no more errors expected */
|
|
|
|
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, mem);
|
2010-04-24 14:28:54 +04:00
|
|
|
#endif
|
2016-10-19 20:21:27 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ST_FUNC void tcc_run_free(TCCState *s1)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < s1->nb_runtime_mem; ++i) {
|
|
|
|
#ifdef HAVE_SELINUX
|
|
|
|
int size = (int)(addr_t)s1->runtime_mem[i];
|
|
|
|
munmap(s1->runtime_mem[++i], size);
|
|
|
|
munmap(s1->runtime_mem[++i], size);
|
|
|
|
#else
|
|
|
|
# ifdef _WIN64
|
|
|
|
win64_del_function_table(*(void**)s1->runtime_mem[i]);
|
|
|
|
# endif
|
|
|
|
tcc_free(s1->runtime_mem[i]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
tcc_free(s1->runtime_mem);
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* launch the compiled program with the given arguments */
|
2011-08-11 18:55:30 +04:00
|
|
|
LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
int (*prog_main)(int, char **);
|
2010-04-24 14:28:54 +04:00
|
|
|
|
2012-09-01 13:33:34 +04:00
|
|
|
if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
|
2009-12-20 00:22:43 +03:00
|
|
|
return -1;
|
2013-08-29 00:55:05 +04:00
|
|
|
prog_main = tcc_get_symbol_err(s1, s1->runtime_main);
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
2010-09-15 15:43:40 +04:00
|
|
|
if (s1->do_debug) {
|
2009-12-20 00:22:43 +03:00
|
|
|
set_exception_handler();
|
2010-09-15 15:43:40 +04:00
|
|
|
rt_prog_main = prog_main;
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
#endif
|
|
|
|
|
2016-10-01 21:47:36 +03:00
|
|
|
errno = 0; /* clean errno value */
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
if (s1->do_bounds_check) {
|
|
|
|
void (*bound_init)(void);
|
|
|
|
void (*bound_exit)(void);
|
2015-03-26 07:47:45 +03:00
|
|
|
void (*bound_new_region)(void *p, addr_t size);
|
tccrun: Mark argv area as valid for bcheck
On my x86_64 box in i386 mode with address space randomization turned off,
I've observed the following:
tests$ ../tcc -B.. -b -run boundtest.c 1
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808da73 main()
With diagnostic patch (like in efd9d92b "lib/bcheck: Don't assume heap
goes right after bss") and bcheck traces for __bound_new_region,
__bound_ptr_indir, etc... here is how the program run looks like:
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd0b4
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd184, 0x4)
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808ea83 main()
So we are accessing something on stack, above stack entry for compiled
main. Investigating with gdb shows that this is argv:
tests$ gdb ../tcc
Reading symbols from /home/kirr/src/tools/tinycc/tcc...done.
(gdb) set args -B.. -b -run boundtest.c 1
(gdb) r
Starting program: /home/kirr/src/tools/tinycc/tests/../tcc -B.. -b -run boundtest.c 1
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd074
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd144, 0x4)
Program received signal SIGSEGV, Segmentation fault.
0x0808ea83 in ?? ()
(gdb) bt
#0 0x0808ea83 in ?? ()
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
#2 0x080492b0 in main (argc=6, argv=0xffffd134) at tcc.c:346
(gdb) f 1
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
132 ret = (*prog_main)(argc, argv);
132 ret = (*prog_main)(argc, argv);
(gdb) p argv
$1 = (char **) 0xffffd144
So before running compiled program, mark argv as valid region and we are
done - now the test passes.
P.S. maybe it would be better to just mark the whole vector kernel passes to
program (argv, env, auxv, etc...) as valid all at once...
2014-01-19 16:35:20 +04:00
|
|
|
int (*bound_delete_region)(void *p);
|
2016-10-01 21:47:36 +03:00
|
|
|
int i, ret;
|
tccrun: Mark argv area as valid for bcheck
On my x86_64 box in i386 mode with address space randomization turned off,
I've observed the following:
tests$ ../tcc -B.. -b -run boundtest.c 1
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808da73 main()
With diagnostic patch (like in efd9d92b "lib/bcheck: Don't assume heap
goes right after bss") and bcheck traces for __bound_new_region,
__bound_ptr_indir, etc... here is how the program run looks like:
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd0b4
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd184, 0x4)
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808ea83 main()
So we are accessing something on stack, above stack entry for compiled
main. Investigating with gdb shows that this is argv:
tests$ gdb ../tcc
Reading symbols from /home/kirr/src/tools/tinycc/tcc...done.
(gdb) set args -B.. -b -run boundtest.c 1
(gdb) r
Starting program: /home/kirr/src/tools/tinycc/tests/../tcc -B.. -b -run boundtest.c 1
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd074
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd144, 0x4)
Program received signal SIGSEGV, Segmentation fault.
0x0808ea83 in ?? ()
(gdb) bt
#0 0x0808ea83 in ?? ()
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
#2 0x080492b0 in main (argc=6, argv=0xffffd134) at tcc.c:346
(gdb) f 1
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
132 ret = (*prog_main)(argc, argv);
132 ret = (*prog_main)(argc, argv);
(gdb) p argv
$1 = (char **) 0xffffd144
So before running compiled program, mark argv as valid region and we are
done - now the test passes.
P.S. maybe it would be better to just mark the whole vector kernel passes to
program (argv, env, auxv, etc...) as valid all at once...
2014-01-19 16:35:20 +04:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* set error function */
|
|
|
|
rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
|
|
|
|
/* XXX: use .init section so that it also work in binary ? */
|
|
|
|
bound_init = tcc_get_symbol_err(s1, "__bound_init");
|
|
|
|
bound_exit = tcc_get_symbol_err(s1, "__bound_exit");
|
tccrun: Mark argv area as valid for bcheck
On my x86_64 box in i386 mode with address space randomization turned off,
I've observed the following:
tests$ ../tcc -B.. -b -run boundtest.c 1
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808da73 main()
With diagnostic patch (like in efd9d92b "lib/bcheck: Don't assume heap
goes right after bss") and bcheck traces for __bound_new_region,
__bound_ptr_indir, etc... here is how the program run looks like:
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd0b4
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd184, 0x4)
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808ea83 main()
So we are accessing something on stack, above stack entry for compiled
main. Investigating with gdb shows that this is argv:
tests$ gdb ../tcc
Reading symbols from /home/kirr/src/tools/tinycc/tcc...done.
(gdb) set args -B.. -b -run boundtest.c 1
(gdb) r
Starting program: /home/kirr/src/tools/tinycc/tests/../tcc -B.. -b -run boundtest.c 1
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd074
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd144, 0x4)
Program received signal SIGSEGV, Segmentation fault.
0x0808ea83 in ?? ()
(gdb) bt
#0 0x0808ea83 in ?? ()
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
#2 0x080492b0 in main (argc=6, argv=0xffffd134) at tcc.c:346
(gdb) f 1
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
132 ret = (*prog_main)(argc, argv);
132 ret = (*prog_main)(argc, argv);
(gdb) p argv
$1 = (char **) 0xffffd144
So before running compiled program, mark argv as valid region and we are
done - now the test passes.
P.S. maybe it would be better to just mark the whole vector kernel passes to
program (argv, env, auxv, etc...) as valid all at once...
2014-01-19 16:35:20 +04:00
|
|
|
bound_new_region = tcc_get_symbol_err(s1, "__bound_new_region");
|
|
|
|
bound_delete_region = tcc_get_symbol_err(s1, "__bound_delete_region");
|
2016-10-01 21:47:36 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
bound_init();
|
tccrun: Mark argv area as valid for bcheck
On my x86_64 box in i386 mode with address space randomization turned off,
I've observed the following:
tests$ ../tcc -B.. -b -run boundtest.c 1
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808da73 main()
With diagnostic patch (like in efd9d92b "lib/bcheck: Don't assume heap
goes right after bss") and bcheck traces for __bound_new_region,
__bound_ptr_indir, etc... here is how the program run looks like:
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd0b4
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd184, 0x4)
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808ea83 main()
So we are accessing something on stack, above stack entry for compiled
main. Investigating with gdb shows that this is argv:
tests$ gdb ../tcc
Reading symbols from /home/kirr/src/tools/tinycc/tcc...done.
(gdb) set args -B.. -b -run boundtest.c 1
(gdb) r
Starting program: /home/kirr/src/tools/tinycc/tests/../tcc -B.. -b -run boundtest.c 1
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd074
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd144, 0x4)
Program received signal SIGSEGV, Segmentation fault.
0x0808ea83 in ?? ()
(gdb) bt
#0 0x0808ea83 in ?? ()
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
#2 0x080492b0 in main (argc=6, argv=0xffffd134) at tcc.c:346
(gdb) f 1
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
132 ret = (*prog_main)(argc, argv);
132 ret = (*prog_main)(argc, argv);
(gdb) p argv
$1 = (char **) 0xffffd144
So before running compiled program, mark argv as valid region and we are
done - now the test passes.
P.S. maybe it would be better to just mark the whole vector kernel passes to
program (argv, env, auxv, etc...) as valid all at once...
2014-01-19 16:35:20 +04:00
|
|
|
/* mark argv area as valid */
|
|
|
|
bound_new_region(argv, argc*sizeof(argv[0]));
|
|
|
|
for (i=0; i<argc; ++i)
|
2016-10-01 21:47:36 +03:00
|
|
|
bound_new_region(argv[i], strlen(argv[i]) + 1);
|
tccrun: Mark argv area as valid for bcheck
On my x86_64 box in i386 mode with address space randomization turned off,
I've observed the following:
tests$ ../tcc -B.. -b -run boundtest.c 1
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808da73 main()
With diagnostic patch (like in efd9d92b "lib/bcheck: Don't assume heap
goes right after bss") and bcheck traces for __bound_new_region,
__bound_ptr_indir, etc... here is how the program run looks like:
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd0b4
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd184, 0x4)
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808ea83 main()
So we are accessing something on stack, above stack entry for compiled
main. Investigating with gdb shows that this is argv:
tests$ gdb ../tcc
Reading symbols from /home/kirr/src/tools/tinycc/tcc...done.
(gdb) set args -B.. -b -run boundtest.c 1
(gdb) r
Starting program: /home/kirr/src/tools/tinycc/tests/../tcc -B.. -b -run boundtest.c 1
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd074
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd144, 0x4)
Program received signal SIGSEGV, Segmentation fault.
0x0808ea83 in ?? ()
(gdb) bt
#0 0x0808ea83 in ?? ()
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
#2 0x080492b0 in main (argc=6, argv=0xffffd134) at tcc.c:346
(gdb) f 1
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
132 ret = (*prog_main)(argc, argv);
132 ret = (*prog_main)(argc, argv);
(gdb) p argv
$1 = (char **) 0xffffd144
So before running compiled program, mark argv as valid region and we are
done - now the test passes.
P.S. maybe it would be better to just mark the whole vector kernel passes to
program (argv, env, auxv, etc...) as valid all at once...
2014-01-19 16:35:20 +04:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
ret = (*prog_main)(argc, argv);
|
tccrun: Mark argv area as valid for bcheck
On my x86_64 box in i386 mode with address space randomization turned off,
I've observed the following:
tests$ ../tcc -B.. -b -run boundtest.c 1
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808da73 main()
With diagnostic patch (like in efd9d92b "lib/bcheck: Don't assume heap
goes right after bss") and bcheck traces for __bound_new_region,
__bound_ptr_indir, etc... here is how the program run looks like:
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd0b4
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd184, 0x4)
Runtime error: dereferencing invalid pointer
boundtest.c:222: at 0x808ea83 main()
So we are accessing something on stack, above stack entry for compiled
main. Investigating with gdb shows that this is argv:
tests$ gdb ../tcc
Reading symbols from /home/kirr/src/tools/tinycc/tcc...done.
(gdb) set args -B.. -b -run boundtest.c 1
(gdb) r
Starting program: /home/kirr/src/tools/tinycc/tests/../tcc -B.. -b -run boundtest.c 1
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
>>> TCC
etext: 0x8067ed8
edata: 0x807321d
end: 0x807d95c
brk: 0x807e000
stack: 0xffffd074
&errno: 0xf7dbd688
mark_invalid 0xfff80000 - (nil)
mark_invalid 0x80fa000 - 0x100fa000
new 808fdb0 808ff40 101 101 fd0 ff0
new 808ff44 808ff48 101 101 ff0 ff0
new 808ff49 8090049 101 101 ff0 1000
new 808fd20 808fd29 101 101 fd0 fd0
new 808fd2c 808fd6c 101 101 fd0 fd0
new 808fd6d 808fda0 101 101 fd0 fd0
E: __bound_ptr_indir4(0xffffd144, 0x4)
Program received signal SIGSEGV, Segmentation fault.
0x0808ea83 in ?? ()
(gdb) bt
#0 0x0808ea83 in ?? ()
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
#2 0x080492b0 in main (argc=6, argv=0xffffd134) at tcc.c:346
(gdb) f 1
#1 0x080639b3 in tcc_run (s1=s1@entry=0x807e008, argc=argc@entry=2, argv=argv@entry=0xffffd144) at tccrun.c:132
132 ret = (*prog_main)(argc, argv);
132 ret = (*prog_main)(argc, argv);
(gdb) p argv
$1 = (char **) 0xffffd144
So before running compiled program, mark argv as valid region and we are
done - now the test passes.
P.S. maybe it would be better to just mark the whole vector kernel passes to
program (argv, env, auxv, etc...) as valid all at once...
2014-01-19 16:35:20 +04:00
|
|
|
|
|
|
|
/* unmark argv area */
|
|
|
|
for (i=0; i<argc; ++i)
|
|
|
|
bound_delete_region(argv[i]);
|
|
|
|
bound_delete_region(argv);
|
2009-12-20 00:22:43 +03:00
|
|
|
bound_exit();
|
2016-10-01 21:47:36 +03:00
|
|
|
return ret;
|
2015-03-25 13:26:11 +03:00
|
|
|
}
|
2016-10-01 21:47:36 +03:00
|
|
|
#endif
|
|
|
|
return (*prog_main)(argc, argv);
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2017-01-07 00:56:19 +03:00
|
|
|
#ifdef TCC_TARGET_X86_64
|
2017-01-08 15:27:24 +03:00
|
|
|
#define RUN_SECTION_ALIGNMENT 63
|
2017-01-07 00:56:19 +03:00
|
|
|
#else
|
2017-01-08 15:27:24 +03:00
|
|
|
#define RUN_SECTION_ALIGNMENT 15
|
2017-01-07 00:56:19 +03:00
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* relocate code. Return -1 on error, required size if ptr is NULL,
|
|
|
|
otherwise copy code into buffer passed by the caller */
|
|
|
|
static int tcc_relocate_ex(TCCState *s1, void *ptr)
|
|
|
|
{
|
|
|
|
Section *s;
|
|
|
|
unsigned long offset, length;
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t mem;
|
2009-12-20 00:22:43 +03:00
|
|
|
int i;
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (NULL == ptr) {
|
2009-12-20 00:22:43 +03:00
|
|
|
s1->nb_errors = 0;
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
pe_output_file(s1, NULL);
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
2009-12-20 00:22:43 +03:00
|
|
|
tcc_add_runtime(s1);
|
|
|
|
relocate_common_syms();
|
|
|
|
tcc_add_linker_symbols(s1);
|
|
|
|
build_got_entries(s1);
|
|
|
|
#endif
|
|
|
|
if (s1->nb_errors)
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2013-02-04 19:08:06 +04:00
|
|
|
offset = 0, mem = (addr_t)ptr;
|
2017-01-08 15:27:24 +03:00
|
|
|
mem += -(int)mem & RUN_SECTION_ALIGNMENT;
|
2016-10-19 20:21:27 +03:00
|
|
|
#ifdef _WIN64
|
|
|
|
offset += sizeof (void*);
|
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
|
|
|
if (0 == (s->sh_flags & SHF_ALLOC))
|
|
|
|
continue;
|
2017-01-08 15:27:24 +03:00
|
|
|
offset = (offset + RUN_SECTION_ALIGNMENT) & ~RUN_SECTION_ALIGNMENT;
|
2016-10-19 20:21:27 +03:00
|
|
|
s->sh_addr = mem ? mem + offset : 0;
|
|
|
|
offset += s->data_offset;
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* relocate symbols */
|
2016-11-12 18:16:09 +03:00
|
|
|
relocate_syms(s1, s1->symtab, 1);
|
2009-12-20 00:22:43 +03:00
|
|
|
if (s1->nb_errors)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (0 == mem)
|
2017-01-08 15:27:24 +03:00
|
|
|
return offset + RUN_SECTION_ALIGNMENT;
|
2009-12-20 00:22:43 +03:00
|
|
|
|
|
|
|
/* relocate each section */
|
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
|
|
|
if (s->reloc)
|
|
|
|
relocate_section(s1, s);
|
|
|
|
}
|
2014-04-06 02:30:22 +04:00
|
|
|
relocate_plt(s1);
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2016-10-19 20:21:27 +03:00
|
|
|
#ifdef _WIN64
|
|
|
|
*(void**)ptr = win64_add_function_table(s1);
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
for(i = 1; i < s1->nb_sections; i++) {
|
|
|
|
s = s1->sections[i];
|
|
|
|
if (0 == (s->sh_flags & SHF_ALLOC))
|
|
|
|
continue;
|
|
|
|
length = s->data_offset;
|
2015-02-22 07:59:06 +03:00
|
|
|
// printf("%-12s %08lx %04x\n", s->name, s->sh_addr, length);
|
2013-02-04 19:08:06 +04:00
|
|
|
ptr = (void*)s->sh_addr;
|
2009-12-20 00:22:43 +03:00
|
|
|
if (NULL == s->data || s->sh_type == SHT_NOBITS)
|
|
|
|
memset(ptr, 0, length);
|
|
|
|
else
|
|
|
|
memcpy(ptr, s->data, length);
|
|
|
|
/* mark executable sections as executable in memory */
|
|
|
|
if (s->sh_flags & SHF_EXECINSTR)
|
|
|
|
set_pages_executable(ptr, length);
|
|
|
|
}
|
|
|
|
return 0;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
/* allow to run code in memory */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
static void set_pages_executable(void *ptr, unsigned long length)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
unsigned long old_protect;
|
|
|
|
VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
|
|
|
|
#else
|
2013-02-12 22:13:28 +04:00
|
|
|
#ifndef PAGESIZE
|
|
|
|
# define PAGESIZE 4096
|
|
|
|
#endif
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t start, end;
|
|
|
|
start = (addr_t)ptr & ~(PAGESIZE - 1);
|
|
|
|
end = (addr_t)ptr + length;
|
2009-12-20 00:22:43 +03:00
|
|
|
end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
|
2016-10-19 20:15:04 +03:00
|
|
|
if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC))
|
|
|
|
tcc_error("mprotect failed: did you mean to configure --with-selinux?");
|
|
|
|
#if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64
|
|
|
|
{ extern void __clear_cache(void *beginning, void *end);
|
|
|
|
__clear_cache(ptr, (char *)ptr + length); }
|
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2016-10-19 20:21:27 +03:00
|
|
|
#ifdef _WIN64
|
|
|
|
static void *win64_add_function_table(TCCState *s1)
|
|
|
|
{
|
|
|
|
void *p = NULL;
|
|
|
|
if (s1->uw_pdata) {
|
|
|
|
p = (void*)s1->uw_pdata->sh_addr;
|
2016-11-13 13:45:55 +03:00
|
|
|
RtlAddFunctionTable(
|
2016-10-19 20:21:27 +03:00
|
|
|
(RUNTIME_FUNCTION*)p,
|
|
|
|
s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
|
|
|
|
text_section->sh_addr
|
|
|
|
);
|
|
|
|
s1->uw_pdata = NULL;
|
|
|
|
}
|
|
|
|
return p;;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void win64_del_function_table(void *p)
|
|
|
|
{
|
|
|
|
if (p) {
|
|
|
|
RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
2009-12-20 00:10:26 +03:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
ST_FUNC void tcc_set_num_callers(int n)
|
2011-08-11 18:55:30 +04:00
|
|
|
{
|
|
|
|
rt_num_callers = n;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
/* print the position in the source file of PC value 'pc' by reading
|
|
|
|
the stabs debug information */
|
2013-02-04 19:08:06 +04:00
|
|
|
static addr_t rt_printline(addr_t wanted_pc, const char *msg)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
|
|
|
char func_name[128], last_func_name[128];
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t func_addr, last_pc, pc;
|
2009-12-20 00:10:26 +03:00
|
|
|
const char *incl_files[INCLUDE_STACK_SIZE];
|
|
|
|
int incl_index, len, last_line_num, i;
|
|
|
|
const char *str, *p;
|
|
|
|
|
2010-09-15 15:43:09 +04:00
|
|
|
Stab_Sym *stab_sym = NULL, *stab_sym_end, *sym;
|
|
|
|
int stab_len = 0;
|
|
|
|
char *stab_str = NULL;
|
|
|
|
|
|
|
|
if (stab_section) {
|
|
|
|
stab_len = stab_section->data_offset;
|
|
|
|
stab_sym = (Stab_Sym *)stab_section->data;
|
2014-03-09 18:52:31 +04:00
|
|
|
stab_str = (char *) stabstr_section->data;
|
2010-09-15 15:43:09 +04:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
incl_index = 0;
|
|
|
|
last_func_name[0] = '\0';
|
2013-02-04 19:08:06 +04:00
|
|
|
last_pc = (addr_t)-1;
|
2009-12-20 00:10:26 +03:00
|
|
|
last_line_num = 1;
|
2010-09-15 15:43:09 +04:00
|
|
|
|
|
|
|
if (!stab_sym)
|
|
|
|
goto no_stabs;
|
|
|
|
|
|
|
|
stab_sym_end = (Stab_Sym*)((char*)stab_sym + stab_len);
|
|
|
|
for (sym = stab_sym + 1; sym < stab_sym_end; ++sym) {
|
2009-12-20 00:10:26 +03:00
|
|
|
switch(sym->n_type) {
|
|
|
|
/* function start or end */
|
|
|
|
case N_FUN:
|
|
|
|
if (sym->n_strx == 0) {
|
|
|
|
/* we test if between last line and end of function */
|
|
|
|
pc = sym->n_value + func_addr;
|
|
|
|
if (wanted_pc >= last_pc && wanted_pc < pc)
|
|
|
|
goto found;
|
|
|
|
func_name[0] = '\0';
|
|
|
|
func_addr = 0;
|
|
|
|
} else {
|
2010-09-15 15:43:09 +04:00
|
|
|
str = stab_str + sym->n_strx;
|
2009-12-20 00:10:26 +03:00
|
|
|
p = strchr(str, ':');
|
|
|
|
if (!p) {
|
|
|
|
pstrcpy(func_name, sizeof(func_name), str);
|
|
|
|
} else {
|
|
|
|
len = p - str;
|
|
|
|
if (len > sizeof(func_name) - 1)
|
|
|
|
len = sizeof(func_name) - 1;
|
|
|
|
memcpy(func_name, str, len);
|
|
|
|
func_name[len] = '\0';
|
|
|
|
}
|
|
|
|
func_addr = sym->n_value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* line number info */
|
|
|
|
case N_SLINE:
|
|
|
|
pc = sym->n_value + func_addr;
|
|
|
|
if (wanted_pc >= last_pc && wanted_pc < pc)
|
|
|
|
goto found;
|
|
|
|
last_pc = pc;
|
|
|
|
last_line_num = sym->n_desc;
|
|
|
|
/* XXX: slow! */
|
|
|
|
strcpy(last_func_name, func_name);
|
|
|
|
break;
|
|
|
|
/* include files */
|
|
|
|
case N_BINCL:
|
2010-09-15 15:43:09 +04:00
|
|
|
str = stab_str + sym->n_strx;
|
2009-12-20 00:10:26 +03:00
|
|
|
add_incl:
|
|
|
|
if (incl_index < INCLUDE_STACK_SIZE) {
|
|
|
|
incl_files[incl_index++] = str;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case N_EINCL:
|
|
|
|
if (incl_index > 1)
|
|
|
|
incl_index--;
|
|
|
|
break;
|
|
|
|
case N_SO:
|
|
|
|
if (sym->n_strx == 0) {
|
|
|
|
incl_index = 0; /* end of translation unit */
|
|
|
|
} else {
|
2010-09-15 15:43:09 +04:00
|
|
|
str = stab_str + sym->n_strx;
|
2009-12-20 00:10:26 +03:00
|
|
|
/* do not add path */
|
|
|
|
len = strlen(str);
|
|
|
|
if (len > 0 && str[len - 1] != '/')
|
|
|
|
goto add_incl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-15 15:43:09 +04:00
|
|
|
no_stabs:
|
2009-12-20 00:10:26 +03:00
|
|
|
/* second pass: we try symtab symbols (no line number info) */
|
|
|
|
incl_index = 0;
|
2010-09-15 15:43:09 +04:00
|
|
|
if (symtab_section)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
|
|
|
ElfW(Sym) *sym, *sym_end;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
|
|
|
|
for(sym = (ElfW(Sym) *)symtab_section->data + 1;
|
|
|
|
sym < sym_end;
|
|
|
|
sym++) {
|
|
|
|
type = ELFW(ST_TYPE)(sym->st_info);
|
2010-10-10 18:23:25 +04:00
|
|
|
if (type == STT_FUNC || type == STT_GNU_IFUNC) {
|
2009-12-20 00:10:26 +03:00
|
|
|
if (wanted_pc >= sym->st_value &&
|
|
|
|
wanted_pc < sym->st_value + sym->st_size) {
|
|
|
|
pstrcpy(last_func_name, sizeof(last_func_name),
|
2014-03-09 18:52:31 +04:00
|
|
|
(char *) strtab_section->data + sym->st_name);
|
2009-12-20 00:10:26 +03:00
|
|
|
func_addr = sym->st_value;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* did not find any info: */
|
2010-09-15 15:42:52 +04:00
|
|
|
fprintf(stderr, "%s %p ???\n", msg, (void*)wanted_pc);
|
|
|
|
fflush(stderr);
|
2009-12-20 00:10:26 +03:00
|
|
|
return 0;
|
|
|
|
found:
|
2010-09-15 15:42:52 +04:00
|
|
|
i = incl_index;
|
|
|
|
if (i > 0)
|
|
|
|
fprintf(stderr, "%s:%d: ", incl_files[--i], last_line_num);
|
|
|
|
fprintf(stderr, "%s %p", msg, (void*)wanted_pc);
|
|
|
|
if (last_func_name[0] != '\0')
|
2009-12-20 00:10:26 +03:00
|
|
|
fprintf(stderr, " %s()", last_func_name);
|
2010-09-15 15:42:52 +04:00
|
|
|
if (--i >= 0) {
|
|
|
|
fprintf(stderr, " (included from ");
|
|
|
|
for (;;) {
|
|
|
|
fprintf(stderr, "%s", incl_files[i]);
|
|
|
|
if (--i < 0)
|
|
|
|
break;
|
|
|
|
fprintf(stderr, ", ");
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
fprintf(stderr, ")");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2010-09-15 15:42:52 +04:00
|
|
|
fflush(stderr);
|
2009-12-20 00:10:26 +03:00
|
|
|
return func_addr;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* emit a run time error at position 'pc' */
|
|
|
|
static void rt_error(ucontext_t *uc, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t pc;
|
2009-12-20 00:22:43 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(stderr, "Runtime error: ");
|
2010-09-15 15:43:40 +04:00
|
|
|
va_start(ap, fmt);
|
2009-12-20 00:22:43 +03:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2010-09-15 15:43:40 +04:00
|
|
|
va_end(ap);
|
2009-12-20 00:22:43 +03:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
for(i=0;i<rt_num_callers;i++) {
|
2009-12-20 00:22:43 +03:00
|
|
|
if (rt_get_caller_pc(&pc, uc, i) < 0)
|
|
|
|
break;
|
2010-09-15 15:42:52 +04:00
|
|
|
pc = rt_printline(pc, i ? "by" : "at");
|
2013-02-04 19:08:06 +04:00
|
|
|
if (pc == (addr_t)rt_prog_main && pc)
|
2009-12-20 00:22:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
/* signal handler for fatal errors */
|
|
|
|
static void sig_error(int signum, siginfo_t *siginf, void *puc)
|
|
|
|
{
|
|
|
|
ucontext_t *uc = puc;
|
|
|
|
|
|
|
|
switch(signum) {
|
|
|
|
case SIGFPE:
|
|
|
|
switch(siginf->si_code) {
|
|
|
|
case FPE_INTDIV:
|
|
|
|
case FPE_FLTDIV:
|
|
|
|
rt_error(uc, "division by zero");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rt_error(uc, "floating point exception");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIGBUS:
|
|
|
|
case SIGSEGV:
|
|
|
|
if (rt_bound_error_msg && *rt_bound_error_msg)
|
|
|
|
rt_error(uc, *rt_bound_error_msg);
|
|
|
|
else
|
|
|
|
rt_error(uc, "dereferencing invalid pointer");
|
|
|
|
break;
|
|
|
|
case SIGILL:
|
|
|
|
rt_error(uc, "illegal instruction");
|
|
|
|
break;
|
|
|
|
case SIGABRT:
|
|
|
|
rt_error(uc, "abort() called");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rt_error(uc, "caught signal %d", signum);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(255);
|
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
#ifndef SA_SIGINFO
|
|
|
|
# define SA_SIGINFO 0x00000004u
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* Generate a stack backtrace when a CPU exception occurs. */
|
|
|
|
static void set_exception_handler(void)
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
/* install TCC signal handlers to print debug info on fatal
|
|
|
|
runtime errors */
|
|
|
|
sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
|
|
|
|
sigact.sa_sigaction = sig_error;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigaction(SIGFPE, &sigact, NULL);
|
|
|
|
sigaction(SIGILL, &sigact, NULL);
|
|
|
|
sigaction(SIGSEGV, &sigact, NULL);
|
|
|
|
sigaction(SIGBUS, &sigact, NULL);
|
|
|
|
sigaction(SIGABRT, &sigact, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
2009-12-20 00:10:26 +03:00
|
|
|
#ifdef __i386__
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
/* fix for glibc 2.1 */
|
|
|
|
#ifndef REG_EIP
|
|
|
|
#define REG_EIP EIP
|
|
|
|
#define REG_EBP EBP
|
|
|
|
#endif
|
|
|
|
|
2013-02-04 20:24:03 +04:00
|
|
|
/* return the PC at frame level 'level'. Return negative if not found */
|
2013-02-04 19:08:06 +04:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2013-02-04 20:24:03 +04:00
|
|
|
addr_t fp;
|
2009-12-20 00:10:26 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (level == 0) {
|
2012-02-09 21:53:17 +04:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
*paddr = uc->uc_mcontext->__ss.__eip;
|
2014-04-12 09:42:46 +04:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
2009-12-20 00:10:26 +03:00
|
|
|
*paddr = uc->uc_mcontext.mc_eip;
|
|
|
|
#elif defined(__dietlibc__)
|
|
|
|
*paddr = uc->uc_mcontext.eip;
|
2014-04-12 09:42:46 +04:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
*paddr = uc->uc_mcontext.__gregs[_REG_EIP];
|
2016-10-15 15:50:17 +03:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
*paddr = uc->sc_eip;
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
|
|
|
*paddr = uc->uc_mcontext.gregs[REG_EIP];
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
} else {
|
2012-02-09 21:53:17 +04:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
fp = uc->uc_mcontext->__ss.__ebp;
|
2014-04-12 09:42:46 +04:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
2009-12-20 00:10:26 +03:00
|
|
|
fp = uc->uc_mcontext.mc_ebp;
|
|
|
|
#elif defined(__dietlibc__)
|
|
|
|
fp = uc->uc_mcontext.ebp;
|
2014-04-12 09:42:46 +04:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
fp = uc->uc_mcontext.__gregs[_REG_EBP];
|
2016-10-15 15:50:17 +03:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
*paddr = uc->sc_ebp;
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
|
|
|
fp = uc->uc_mcontext.gregs[REG_EBP];
|
|
|
|
#endif
|
|
|
|
for(i=1;i<level;i++) {
|
|
|
|
/* XXX: check address validity with program info */
|
|
|
|
if (fp <= 0x1000 || fp >= 0xc0000000)
|
|
|
|
return -1;
|
2013-02-04 20:24:03 +04:00
|
|
|
fp = ((addr_t *)fp)[0];
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2013-02-04 20:24:03 +04:00
|
|
|
*paddr = ((addr_t *)fp)[1];
|
2009-12-20 00:10:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
2009-12-20 00:10:26 +03:00
|
|
|
#elif defined(__x86_64__)
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2013-02-04 20:24:03 +04:00
|
|
|
/* return the PC at frame level 'level'. Return negative if not found */
|
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2013-02-04 20:24:03 +04:00
|
|
|
addr_t fp;
|
2009-12-20 00:10:26 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (level == 0) {
|
|
|
|
/* XXX: only support linux */
|
2012-02-09 21:53:17 +04:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
*paddr = uc->uc_mcontext->__ss.__rip;
|
2014-04-12 08:52:20 +04:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
2009-12-20 00:10:26 +03:00
|
|
|
*paddr = uc->uc_mcontext.mc_rip;
|
2014-04-12 09:42:46 +04:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
*paddr = uc->uc_mcontext.__gregs[_REG_RIP];
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
|
|
|
*paddr = uc->uc_mcontext.gregs[REG_RIP];
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
} else {
|
2012-02-09 21:53:17 +04:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
fp = uc->uc_mcontext->__ss.__rbp;
|
2014-04-12 08:52:20 +04:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
2009-12-20 00:10:26 +03:00
|
|
|
fp = uc->uc_mcontext.mc_rbp;
|
2014-04-12 09:42:46 +04:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
fp = uc->uc_mcontext.__gregs[_REG_RBP];
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
|
|
|
fp = uc->uc_mcontext.gregs[REG_RBP];
|
|
|
|
#endif
|
|
|
|
for(i=1;i<level;i++) {
|
|
|
|
/* XXX: check address validity with program info */
|
|
|
|
if (fp <= 0x1000)
|
|
|
|
return -1;
|
2013-02-04 20:24:03 +04:00
|
|
|
fp = ((addr_t *)fp)[0];
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2013-02-04 20:24:03 +04:00
|
|
|
*paddr = ((addr_t *)fp)[1];
|
2009-12-20 00:10:26 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2010-05-14 16:22:32 +04:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#elif defined(__arm__)
|
|
|
|
|
|
|
|
/* return the PC at frame level 'level'. Return negative if not found */
|
2013-02-04 20:24:03 +04:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
|
2010-05-14 16:22:32 +04:00
|
|
|
{
|
2013-02-04 20:24:03 +04:00
|
|
|
addr_t fp, sp;
|
2010-05-14 16:22:32 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (level == 0) {
|
|
|
|
/* XXX: only supports linux */
|
|
|
|
#if defined(__linux__)
|
|
|
|
*paddr = uc->uc_mcontext.arm_pc;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
#if defined(__linux__)
|
|
|
|
fp = uc->uc_mcontext.arm_fp;
|
|
|
|
sp = uc->uc_mcontext.arm_sp;
|
|
|
|
if (sp < 0x1000)
|
|
|
|
sp = 0x1000;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
/* XXX: specific to tinycc stack frames */
|
|
|
|
if (fp < sp + 12 || fp & 3)
|
|
|
|
return -1;
|
|
|
|
for(i = 1; i < level; i++) {
|
2013-02-04 20:24:03 +04:00
|
|
|
sp = ((addr_t *)fp)[-2];
|
2010-05-14 16:22:32 +04:00
|
|
|
if (sp < fp || sp - fp > 16 || sp & 3)
|
|
|
|
return -1;
|
2013-02-04 20:24:03 +04:00
|
|
|
fp = ((addr_t *)fp)[-3];
|
2010-05-14 16:22:32 +04:00
|
|
|
if (fp <= sp || fp - sp < 12 || fp & 3)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* XXX: check address validity with program info */
|
2013-02-04 20:24:03 +04:00
|
|
|
*paddr = ((addr_t *)fp)[-1];
|
2010-05-14 16:22:32 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 21:58:31 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
|
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
|
|
|
|
{
|
|
|
|
if (level < 0)
|
|
|
|
return -1;
|
|
|
|
else if (level == 0) {
|
|
|
|
*paddr = uc->uc_mcontext.pc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
addr_t *fp = (addr_t *)uc->uc_mcontext.regs[29];
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < level; i++)
|
|
|
|
fp = (addr_t *)fp[0];
|
|
|
|
*paddr = fp[1];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
2009-12-20 00:10:26 +03:00
|
|
|
#else
|
2009-12-20 00:22:43 +03:00
|
|
|
|
2009-12-20 00:10:26 +03:00
|
|
|
#warning add arch specific rt_get_caller_pc()
|
2013-02-04 20:24:03 +04:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif /* !__i386__ */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#else /* WIN32 */
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
EXCEPTION_RECORD *er = ex_info->ExceptionRecord;
|
2010-09-15 15:43:40 +04:00
|
|
|
CONTEXT *uc = ex_info->ContextRecord;
|
|
|
|
switch (er->ExceptionCode) {
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
if (rt_bound_error_msg && *rt_bound_error_msg)
|
|
|
|
rt_error(uc, *rt_bound_error_msg);
|
|
|
|
else
|
2015-07-29 23:53:57 +03:00
|
|
|
rt_error(uc, "access violation");
|
2010-09-15 15:43:40 +04:00
|
|
|
break;
|
|
|
|
case EXCEPTION_STACK_OVERFLOW:
|
|
|
|
rt_error(uc, "stack overflow");
|
|
|
|
break;
|
|
|
|
case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
|
|
|
rt_error(uc, "division by zero");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rt_error(uc, "exception caught");
|
|
|
|
break;
|
|
|
|
}
|
2013-02-05 17:27:38 +04:00
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* Generate a stack backtrace when a CPU exception occurs. */
|
|
|
|
static void set_exception_handler(void)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
SetUnhandledExceptionFilter(cpu_exception_handler);
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* return the PC at frame level 'level'. Return non zero if not found */
|
2013-02-04 19:08:06 +04:00
|
|
|
static int rt_get_caller_pc(addr_t *paddr, CONTEXT *uc, int level)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2013-02-04 19:08:06 +04:00
|
|
|
addr_t fp, pc;
|
2009-12-20 00:10:26 +03:00
|
|
|
int i;
|
2010-09-15 15:43:25 +04:00
|
|
|
#ifdef _WIN64
|
|
|
|
pc = uc->Rip;
|
|
|
|
fp = uc->Rbp;
|
|
|
|
#else
|
|
|
|
pc = uc->Eip;
|
|
|
|
fp = uc->Ebp;
|
|
|
|
#endif
|
|
|
|
if (level > 0) {
|
2015-07-29 23:53:57 +03:00
|
|
|
for(i=1;i<level;i++) {
|
|
|
|
/* XXX: check address validity with program info */
|
|
|
|
if (fp <= 0x1000 || fp >= 0xc0000000)
|
|
|
|
return -1;
|
|
|
|
fp = ((addr_t*)fp)[0];
|
|
|
|
}
|
2013-02-04 19:08:06 +04:00
|
|
|
pc = ((addr_t*)fp)[1];
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2010-09-15 15:43:25 +04:00
|
|
|
*paddr = pc;
|
|
|
|
return 0;
|
2009-12-20 00:22:43 +03:00
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
#endif /* CONFIG_TCC_BACKTRACE */
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
#ifdef CONFIG_TCC_STATIC
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* dummy function for profiling */
|
2011-08-11 18:55:30 +04:00
|
|
|
ST_FUNC void *dlopen(const char *filename, int flag)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
|
|
|
return NULL;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
|
|
|
|
2011-08-11 18:55:30 +04:00
|
|
|
ST_FUNC void dlclose(void *p)
|
2009-12-20 00:11:12 +03:00
|
|
|
{
|
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
|
|
|
|
ST_FUNC const char *dlerror(void)
|
2009-12-20 00:10:26 +03:00
|
|
|
{
|
2009-12-20 00:22:43 +03:00
|
|
|
return "error";
|
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
typedef struct TCCSyms {
|
|
|
|
char *str;
|
|
|
|
void *ptr;
|
|
|
|
} TCCSyms;
|
2009-12-20 00:10:26 +03:00
|
|
|
|
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
/* add the symbol you want here if no dynamic linking is done */
|
|
|
|
static TCCSyms tcc_syms[] = {
|
|
|
|
#if !defined(CONFIG_TCCBOOT)
|
2013-02-12 22:13:28 +04:00
|
|
|
#define TCCSYM(a) { #a, &a, },
|
2009-12-20 00:22:43 +03:00
|
|
|
TCCSYM(printf)
|
|
|
|
TCCSYM(fprintf)
|
|
|
|
TCCSYM(fopen)
|
|
|
|
TCCSYM(fclose)
|
2013-02-12 22:13:28 +04:00
|
|
|
#undef TCCSYM
|
2009-12-20 00:10:26 +03:00
|
|
|
#endif
|
2009-12-20 00:22:43 +03:00
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
2016-11-13 13:45:55 +03:00
|
|
|
ST_FUNC void *dlsym(void *handle, const char *symbol)
|
2009-12-20 00:22:43 +03:00
|
|
|
{
|
|
|
|
TCCSyms *p;
|
|
|
|
p = tcc_syms;
|
|
|
|
while (p->str != NULL) {
|
|
|
|
if (!strcmp(p->str, symbol))
|
|
|
|
return p->ptr;
|
|
|
|
p++;
|
2009-12-20 00:10:26 +03:00
|
|
|
}
|
2009-12-20 00:22:43 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-12-20 00:10:26 +03:00
|
|
|
|
2009-12-20 00:22:43 +03:00
|
|
|
#endif /* CONFIG_TCC_STATIC */
|
2013-02-04 19:08:06 +04:00
|
|
|
#endif /* TCC_IS_NATIVE */
|
2009-12-20 00:22:43 +03:00
|
|
|
/* ------------------------------------------------------------- */
|