binutils-gdb/gdb/testsuite/gdb.arch/amd64-eval.cc

139 lines
3.3 KiB
C++
Raw Normal View History

Fix AMD64 return value ABI in expression evaluation The AMD64 System V ABI specifies that when a function has a return type classified as MEMORY, the caller provides space for the value and passes the address to this space as the first argument to the function (before even the "this" pointer). The classification of MEMORY is applied to struct that are sufficiently large, or ones with unaligned fields. The expression evaluator uses call_function_by_hand to call functions, and the hand-built frame has to push arguments in a way that matches the ABI of the called function. call_function_by_hand supports ABI-based struct returns, based on the value of gdbarch_return_value, however on AMD64 the implementation of the classifier incorrectly assumed that all non-POD types (implemented as "all types with a base class") should be classified as MEMORY and use the struct return. This ABI mismatch resulted in issues when calling a function that returns a class of size <16 bytes which has a base class, including issues such as the "this" pointer being incorrect (as it was passed as the second argument rather than the first). This is now fixed by checking for field alignment rather than POD-ness, and a testsuite is added to test expression evaluation for AMD64. gdb/ChangeLog: * amd64-tdep.c (amd64_classify_aggregate): Use cp_pass_by_reference rather than a hand-rolled POD check when checking for forced MEMORY classification. gdb/testsuite/ChangeLog: * gdb.arch/amd64-eval.cc: New file. * gdb.arch/amd64-eval.exp: New file.
2019-04-15 23:56:43 +08:00
/* This testcase is part of GDB, the GNU debugger.
Copyright 2019-2020 Free Software Foundation, Inc.
Fix AMD64 return value ABI in expression evaluation The AMD64 System V ABI specifies that when a function has a return type classified as MEMORY, the caller provides space for the value and passes the address to this space as the first argument to the function (before even the "this" pointer). The classification of MEMORY is applied to struct that are sufficiently large, or ones with unaligned fields. The expression evaluator uses call_function_by_hand to call functions, and the hand-built frame has to push arguments in a way that matches the ABI of the called function. call_function_by_hand supports ABI-based struct returns, based on the value of gdbarch_return_value, however on AMD64 the implementation of the classifier incorrectly assumed that all non-POD types (implemented as "all types with a base class") should be classified as MEMORY and use the struct return. This ABI mismatch resulted in issues when calling a function that returns a class of size <16 bytes which has a base class, including issues such as the "this" pointer being incorrect (as it was passed as the second argument rather than the first). This is now fixed by checking for field alignment rather than POD-ness, and a testsuite is added to test expression evaluation for AMD64. gdb/ChangeLog: * amd64-tdep.c (amd64_classify_aggregate): Use cp_pass_by_reference rather than a hand-rolled POD check when checking for forced MEMORY classification. gdb/testsuite/ChangeLog: * gdb.arch/amd64-eval.cc: New file. * gdb.arch/amd64-eval.exp: New file.
2019-04-15 23:56:43 +08:00
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/>. */
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cassert>
/* A simple structure with a single integer field. Should be returned in
a register. */
struct SimpleBase
{
SimpleBase (int32_t x) : x (x) {}
int32_t x;
};
/* A simple structure derived from the simple base. Should be returned in
a register. */
struct SimpleDerived : public SimpleBase
{
SimpleDerived (int32_t x) : SimpleBase (x) {}
};
/* A structure derived from the simple base with a non-trivial destructor.
Should be returned on the stack. */
struct NonTrivialDestructorDerived : public SimpleBase
{
NonTrivialDestructorDerived (int32_t x) : SimpleBase (x) {}
~NonTrivialDestructorDerived() { x = 1; }
};
/* A structure with unaligned fields. Should be returned on the stack. */
struct UnalignedFields
{
UnalignedFields (int32_t x, double y) : x (x), y (y) {}
int32_t x;
double y;
} __attribute__((packed));
/* A structure with unaligned fields in its base class. Should be
returned on the stack. */
struct UnalignedFieldsInBase : public UnalignedFields
{
UnalignedFieldsInBase (int32_t x, double y, int32_t x2)
: UnalignedFields (x, y), x2 (x2) {}
int32_t x2;
};
struct Bitfields
{
Bitfields(unsigned int x, unsigned int y)
: fld(x), fld2(y)
{}
unsigned fld : 7;
unsigned fld2 : 7;
};
Fix AMD64 return value ABI in expression evaluation The AMD64 System V ABI specifies that when a function has a return type classified as MEMORY, the caller provides space for the value and passes the address to this space as the first argument to the function (before even the "this" pointer). The classification of MEMORY is applied to struct that are sufficiently large, or ones with unaligned fields. The expression evaluator uses call_function_by_hand to call functions, and the hand-built frame has to push arguments in a way that matches the ABI of the called function. call_function_by_hand supports ABI-based struct returns, based on the value of gdbarch_return_value, however on AMD64 the implementation of the classifier incorrectly assumed that all non-POD types (implemented as "all types with a base class") should be classified as MEMORY and use the struct return. This ABI mismatch resulted in issues when calling a function that returns a class of size <16 bytes which has a base class, including issues such as the "this" pointer being incorrect (as it was passed as the second argument rather than the first). This is now fixed by checking for field alignment rather than POD-ness, and a testsuite is added to test expression evaluation for AMD64. gdb/ChangeLog: * amd64-tdep.c (amd64_classify_aggregate): Use cp_pass_by_reference rather than a hand-rolled POD check when checking for forced MEMORY classification. gdb/testsuite/ChangeLog: * gdb.arch/amd64-eval.cc: New file. * gdb.arch/amd64-eval.exp: New file.
2019-04-15 23:56:43 +08:00
class Foo
{
public:
SimpleBase
return_simple_base (int32_t x)
{
assert (this->tag == EXPECTED_TAG);
return SimpleBase (x);
}
SimpleDerived
return_simple_derived (int32_t x)
{
assert (this->tag == EXPECTED_TAG);
return SimpleDerived (x);
}
NonTrivialDestructorDerived
return_non_trivial_destructor (int32_t x)
{
assert (this->tag == EXPECTED_TAG);
return NonTrivialDestructorDerived (x);
}
UnalignedFields
return_unaligned (int32_t x, double y)
{
assert (this->tag == EXPECTED_TAG);
return UnalignedFields (x, y);
}
UnalignedFieldsInBase
return_unaligned_in_base (int32_t x, double y, int32_t x2)
{
assert (this->tag == EXPECTED_TAG);
return UnalignedFieldsInBase (x, y, x2);
}
Bitfields
return_bitfields (unsigned int x, unsigned int y)
{
assert (this->tag == EXPECTED_TAG);
return Bitfields(x, y);
}
Fix AMD64 return value ABI in expression evaluation The AMD64 System V ABI specifies that when a function has a return type classified as MEMORY, the caller provides space for the value and passes the address to this space as the first argument to the function (before even the "this" pointer). The classification of MEMORY is applied to struct that are sufficiently large, or ones with unaligned fields. The expression evaluator uses call_function_by_hand to call functions, and the hand-built frame has to push arguments in a way that matches the ABI of the called function. call_function_by_hand supports ABI-based struct returns, based on the value of gdbarch_return_value, however on AMD64 the implementation of the classifier incorrectly assumed that all non-POD types (implemented as "all types with a base class") should be classified as MEMORY and use the struct return. This ABI mismatch resulted in issues when calling a function that returns a class of size <16 bytes which has a base class, including issues such as the "this" pointer being incorrect (as it was passed as the second argument rather than the first). This is now fixed by checking for field alignment rather than POD-ness, and a testsuite is added to test expression evaluation for AMD64. gdb/ChangeLog: * amd64-tdep.c (amd64_classify_aggregate): Use cp_pass_by_reference rather than a hand-rolled POD check when checking for forced MEMORY classification. gdb/testsuite/ChangeLog: * gdb.arch/amd64-eval.cc: New file. * gdb.arch/amd64-eval.exp: New file.
2019-04-15 23:56:43 +08:00
private:
/* Use a tag to detect if the "this" value is correct. */
static const int EXPECTED_TAG = 0xF00F00F0;
int tag = EXPECTED_TAG;
};
int
main (int argc, char *argv[])
{
Foo foo;
foo.return_simple_base(1);
foo.return_simple_derived(2);
foo.return_non_trivial_destructor(3);
foo.return_unaligned(4, 5);
foo.return_unaligned_in_base(6, 7, 8);
foo.return_bitfields(23, 74);
Fix AMD64 return value ABI in expression evaluation The AMD64 System V ABI specifies that when a function has a return type classified as MEMORY, the caller provides space for the value and passes the address to this space as the first argument to the function (before even the "this" pointer). The classification of MEMORY is applied to struct that are sufficiently large, or ones with unaligned fields. The expression evaluator uses call_function_by_hand to call functions, and the hand-built frame has to push arguments in a way that matches the ABI of the called function. call_function_by_hand supports ABI-based struct returns, based on the value of gdbarch_return_value, however on AMD64 the implementation of the classifier incorrectly assumed that all non-POD types (implemented as "all types with a base class") should be classified as MEMORY and use the struct return. This ABI mismatch resulted in issues when calling a function that returns a class of size <16 bytes which has a base class, including issues such as the "this" pointer being incorrect (as it was passed as the second argument rather than the first). This is now fixed by checking for field alignment rather than POD-ness, and a testsuite is added to test expression evaluation for AMD64. gdb/ChangeLog: * amd64-tdep.c (amd64_classify_aggregate): Use cp_pass_by_reference rather than a hand-rolled POD check when checking for forced MEMORY classification. gdb/testsuite/ChangeLog: * gdb.arch/amd64-eval.cc: New file. * gdb.arch/amd64-eval.exp: New file.
2019-04-15 23:56:43 +08:00
return 0; // break-here
}