old-cross-binutils/gdb/testsuite/gdb.base/max-value-size.exp
Andrew Burgess 5fdf6324fa gdb: New set/show max-value-size command.
For languages with dynamic types, an incorrect program, or uninitialised
variables within a program, could result in an incorrect, overly large
type being associated with a value.  Currently, attempting to print such
a variable will result in gdb trying to allocate an overly large buffer.

If this large memory allocation fails then the result can be gdb either
terminating, or (due to memory contention) becoming unresponsive for the
user.

A new user visible variable in gdb helps guard against such problems,
two new commands are available:

   set max-value-size
   show max-value-size

The 'max-value-size' is the maximum size of memory in bytes that gdb
will allocate for the contents of a value.  Any attempt to allocate a
value with a size greater than this will result in an error.  The
initial default for this limit is set at 64k, this is based on a similar
limit that exists within the ada specific code.

It is possible for the user to set max-value-size to unlimited, in which
case the old behaviour is restored.

gdb/ChangeLog:

	* value.c (max_value_size): New variable.
	(MIN_VALUE_FOR_MAX_VALUE_SIZE): New define.
	(show_max_value_size): New function.
	(check_type_length_before_alloc): New function.
	(allocate_value_contents): Call check_type_length_before_alloc.
	(set_value_enclosing_type): Likewise.
	(_initialize_values): Add set/show handler for max-value-size.
	* NEWS: Mention new set/show command.

gdb/doc/ChangeLog:

	* gdb.texinfo (Value Sizes): New section.
	(Data): Add the 'Value Sizes' node to the menu.

gdb/testsuite/ChangeLog:

	* gdb.base/max-value-size.c: New file.
	* gdb.base/max-value-size.exp: New file.
	* gdb.base/huge.exp: Disable max-value-size for this test.
2016-02-01 18:05:35 +00:00

97 lines
3.1 KiB
Text

# Copyright (C) 2016 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
standard_testfile
if {[prepare_for_testing $testfile.exp $testfile $srcfile debug]} {
untested $testfile.exp
return -1
}
if ![runto_main] then {
fail "Can't run to main"
return 0
}
# Run "show max-value-size" and return the interesting bit of the
# result. This is either the maximum size in bytes, or the string
# "unlimited".
proc get_max_value_size {} {
global gdb_prompt
global decimal
gdb_test_multiple "show max-value-size" "" {
-re "Maximum value size is ($decimal) bytes.*$gdb_prompt $" {
return $expect_out(1,string)
}
-re "Maximum value size is unlimited.*$gdb_prompt $" {
return "unlimited"
}
}
}
# Assuming that MAX_VALUE_SIZE is the current value for
# max-value-size, print the test values. Use TEST_PREFIX to make the
# test names unique.
proc do_value_printing { max_value_size test_prefix } {
with_test_prefix ${test_prefix} {
gdb_test "p/d one" " = 0"
if { $max_value_size != "unlimited" && $max_value_size < 100 } then {
gdb_test "p/d one_hundred" \
"value requires 100 bytes, which is more than max-value-size"
} else {
gdb_test "p/d one_hundred" " = \\{0 <repeats 100 times>\\}"
}
gdb_test "p/d one_hundred \[99\]" " = 0"
}
}
# Install SET_VALUE as the value for max-value-size, then print the
# test values.
proc set_and_check_max_value_size { set_value } {
if { $set_value == "unlimited" } then {
set check_pattern "unlimited"
} else {
set check_pattern "${set_value} bytes"
}
gdb_test_no_output "set max-value-size ${set_value}"
gdb_test "show max-value-size" \
"Maximum value size is ${check_pattern}." \
"check that the value shows as ${check_pattern}"
do_value_printing ${set_value} "max-value-size is '${set_value}'"
}
# Check the default value is sufficient.
do_value_printing [get_max_value_size] "using initial max-value-size"
# Check some values for max-value-size that should prevent some
# allocations.
set_and_check_max_value_size 25
set_and_check_max_value_size 99
# Check values for max-value-size that should allow all allocations.
set_and_check_max_value_size 100
set_and_check_max_value_size 200
set_and_check_max_value_size "unlimited"
# Check that we can't set the maximum size stupidly low.
gdb_test "set max-value-size 1" \
"max-value-size set too low, increasing to \[0-9\]+ bytes"
gdb_test "set max-value-size 0" \
"max-value-size set too low, increasing to \[0-9\]+ bytes"
gdb_test "set max-value-size -5" \
"only -1 is allowed to set as unlimited"