diff options
-rw-r--r-- | runtime/alloc.c | 41 | ||||
-rw-r--r-- | testsuite/systemtap.base/maxmemory.exp | 86 |
2 files changed, 107 insertions, 20 deletions
diff --git a/runtime/alloc.c b/runtime/alloc.c index 810ec5d5..b2578e06 100644 --- a/runtime/alloc.c +++ b/runtime/alloc.c @@ -216,13 +216,14 @@ static void _stp_mem_debug_validate(void *addr) } #endif -/* #define MAXMEMORY 8192 */ +/* #define STP_MAXMEMORY 8192 */ /* - * If MAXMEMORY is defined to a value (stap -DMAXMEMORY=8192 ...) then - * every memory allocation is checked to make sure the systemtap - * module doesn't use more than MAXMEMORY of memory. MAXMEMORY is - * specified in kilobytes, so, for example, '8192' means that the - * systemtap module won't use more than 8 megabytes of memory. + * If STP_MAXMEMORY is defined to a value (stap -DSTP_MAXMEMORY=8192 + * ...) then every memory allocation is checked to make sure the + * systemtap module doesn't use more than STP_MAXMEMORY of memory. + * STP_MAXMEMORY is specified in kilobytes, so, for example, '8192' + * means that the systemtap module won't use more than 8 megabytes of + * memory. * * Note 1: This size does include the size of the module itself, plus * any additional allocations. @@ -232,7 +233,7 @@ static void _stp_mem_debug_validate(void *addr) * directly report an error back to a user (so instead it uses * 'printk'). If the modules transport has been set up, the code that * calls the memory allocation functions - * (_stp_kmalloc/_stp_kzalloc/etc.) should report an error directly wto + * (_stp_kmalloc/_stp_kzalloc/etc.) should report an error directly to * the user. * * Note 3: This only tracks direct allocations by the systemtap @@ -240,7 +241,7 @@ static void _stp_mem_debug_validate(void *addr) * kprobes/uprobes/etc. internals). */ -#ifdef MAXMEMORY +#ifdef STP_MAXMEMORY #ifndef STAPCONF_GRSECURITY #define _STP_MODULE_CORE_SIZE (THIS_MODULE->core_size) #else @@ -251,9 +252,9 @@ static void _stp_mem_debug_validate(void *addr) static void *_stp_kmalloc(size_t size) { void *ret; -#ifdef MAXMEMORY +#ifdef STP_MAXMEMORY if ((_STP_MODULE_CORE_SIZE + _stp_allocated_memory + size) - > (MAXMEMORY * 1024)) { + > (STP_MAXMEMORY * 1024)) { return NULL; } #endif @@ -276,9 +277,9 @@ static void *_stp_kzalloc(size_t size) #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15) { void *ret; -#ifdef MAXMEMORY +#ifdef STP_MAXMEMORY if ((_STP_MODULE_CORE_SIZE + _stp_allocated_memory + size) - > (MAXMEMORY * 1024)) { + > (STP_MAXMEMORY * 1024)) { return NULL; } #endif @@ -301,9 +302,9 @@ static void *_stp_kzalloc(size_t size) #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15) */ { void *ret; -#ifdef MAXMEMORY +#ifdef STP_MAXMEMORY if ((_STP_MODULE_CORE_SIZE + _stp_allocated_memory + size) - > (MAXMEMORY * 1024)) { + > (STP_MAXMEMORY * 1024)) { return NULL; } #endif @@ -326,9 +327,9 @@ static void *_stp_kzalloc(size_t size) static void *_stp_vmalloc(unsigned long size) { void *ret; -#ifdef MAXMEMORY +#ifdef STP_MAXMEMORY if ((_STP_MODULE_CORE_SIZE + _stp_allocated_memory + size) - > (MAXMEMORY * 1024)) { + > (STP_MAXMEMORY * 1024)) { return NULL; } #endif @@ -360,10 +361,10 @@ static void *_stp_alloc_percpu(size_t size) if (size > _STP_MAX_PERCPU_SIZE) return NULL; -#ifdef MAXMEMORY +#ifdef STP_MAXMEMORY if ((_STP_MODULE_CORE_SIZE + _stp_allocated_memory + (size * num_online_cpus())) - > (MAXMEMORY * 1024)) { + > (STP_MAXMEMORY * 1024)) { return NULL; } #endif @@ -397,9 +398,9 @@ static void *_stp_alloc_percpu(size_t size) static void *_stp_kmalloc_node(size_t size, int node) { void *ret; -#ifdef MAXMEMORY +#ifdef STP_MAXMEMORY if ((_STP_MODULE_CORE_SIZE + _stp_allocated_memory + size) - > (MAXMEMORY * 1024)) { + > (STP_MAXMEMORY * 1024)) { return NULL; } #endif diff --git a/testsuite/systemtap.base/maxmemory.exp b/testsuite/systemtap.base/maxmemory.exp new file mode 100644 index 00000000..20827edf --- /dev/null +++ b/testsuite/systemtap.base/maxmemory.exp @@ -0,0 +1,86 @@ +if {![installtest_p]} {untested "MAXMEMORY"; return} + +set script { + global k + + probe begin { + print("systemtap starting probe\n") + k["foo"] = 0 + } + + probe kernel.function("vfs_read"), kernel.function("vfs_write") { + k["foo"]++ + } + probe end { + print("systemtap ending probe\n") + } +} + +# stap_run_maxmemory TEST_NAME EXPECT_ERROR +# TEST_NAME is the name of the current test +# EXPECT_ERROR lets us know to expect an error or not +# +# Additional arguments are passed to stap as-is. +proc stap_run_maxmemory { TEST_NAME EXPECT_ERROR args } { + + set cmd [concat {stap -v} $args] + eval spawn $cmd + expect { + -timeout 150 + -re {^Pass\ [1234]: [^\r]+real\ ms\.\r\n} {exp_continue} + -re {^Pass\ ([34]): using cached [^\r]+\r\n} {exp_continue} + -re {^Pass 5: starting run.\r\n} {exp_continue} + -re {^Error inserting module[^\r]+\r\n} { + if {$EXPECT_ERROR} { + pass "$TEST_NAME received expected insert module error" + } else { + fail "$TEST_NAME unexpected insert module error" + } + } + -re {ERROR: [^\r]+ allocation failed\r\n} { + if {$EXPECT_ERROR} { + pass "$TEST_NAME received expected allocation error" + } else { + fail "$TEST_NAME unexpected allocation error" + } + } + -re "^systemtap starting probe\r\n" { + exec kill -INT -[exp_pid] + + expect { + -timeout 10 + -re {^systemtap ending probe\r\n} { + if {$EXPECT_ERROR} { + fail "$TEST_NAME didn't receive expected allocation error" + } else { + pass "$TEST_NAME didn't receive allocation error" + } + } + -re {ERROR: .+ allocation failed\r\n} { + if {$EXPECT_ERROR} { + pass "$TEST_NAME received expected allocation error" + } else { + fail "$TEST_NAME received an unexpected allocation error" + } + } + } + } + -re "semantic error:" { fail "$TEST_NAME compilation" } + timeout { fail "$TEST_NAME startup (timeout)"; + exec kill -INT -[exp_pid] } + eof { fail "$TEST_NAME startup (eof)" } + } + catch close + wait +} + +# MAXMEMORY1 tests to make sure normal operation doesn't receive a +# max memory error +set test "MAXMEMORY1" +stap_run_maxmemory $test 0 -u -e $script + +# MAXMEMORY2 is the same script, but we're adjusting STP_MAXMEMORY to +# a low value so that we *will* get an allocation error or an insert +# module error. +set test "MAXMEMORY2" +stap_run_maxmemory $test 1 -u -DSTP_MAXMEMORY=200 -e $script |