From: "Michael S. Tsirkin" <mst@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: qemu-devel@nongnu.org, patches@linaro.org,
Igor Mammedov <imammedo@redhat.com>
Subject: Re: [Qemu-devel] [PATCH for-2.9] tests/bios-tables-test: Don't pass addresses of packed struct fields
Date: Mon, 27 Mar 2017 21:47:03 +0300 [thread overview]
Message-ID: <20170327214256-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <1490638725-19939-1-git-send-email-peter.maydell@linaro.org>
On Mon, Mar 27, 2017 at 07:18:45PM +0100, Peter Maydell wrote:
> Passing the address of a field in a packed struct to a function
> that expects a pointer to normally aligned data will result in
> a SEGBUS on architectures like SPARC that have strict alignment
> requirements.
>
> Pass addresses of local variables rather than addresses of packed
> structure fields to glib functions like g_file_get_contents() to
> avoid this bug.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
I wonder why do we pack the sdt structure. some more comments below.
> ---
> tests/bios-tables-test.c | 24 ++++++++++++++++++------
> 1 file changed, 18 insertions(+), 6 deletions(-)
>
> diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c
> index 88dbf97..e9a5092 100644
> --- a/tests/bios-tables-test.c
> +++ b/tests/bios-tables-test.c
> @@ -261,7 +261,10 @@ static void dump_aml_files(test_data *data, bool rebuild)
> fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
> S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
> } else {
> - fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
> + gchar *name;
> +
> + fd = g_file_open_tmp("aml-XXXXXX", &name, &error);
> + sdt->aml_file = name;
> g_assert_no_error(error);
> }
> g_assert(fd >= 0);
Let's move these assignments to after we tested the ret value?
Same with all other cases.
> @@ -291,8 +294,10 @@ static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
> gchar *out, *out_err;
> gboolean ret;
> int i;
> + gchar *name;
>
> - fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
> + fd = g_file_open_tmp("asl-XXXXXX.dsl", &name, &error);
> + sdt->asl_file = name;
> g_assert_no_error(error);
> close(fd);
>
> @@ -314,8 +319,12 @@ static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
> ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
> g_assert_no_error(error);
> if (ret) {
> - ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl,
> - &sdt->asl_len, &error);
> + gchar *contents;
> + gsize len;
> +
> + ret = g_file_get_contents(sdt->asl_file, &contents, &len, &error);
> + sdt->asl = contents;
> + sdt->asl_len = len;
> g_assert(ret);
> g_assert_no_error(error);
> ret = (sdt->asl_len > 0);
> @@ -371,6 +380,8 @@ static GArray *load_expected_aml(test_data *data)
> uint32_t signature;
> gchar *aml_file = NULL;
> const char *ext = data->variant ? data->variant : "";
> + gchar *aml_contents;
> + gsize aml_length;
>
> sdt = &g_array_index(data->tables, AcpiSdtTable, i);
>
> @@ -397,8 +408,9 @@ try_again:
> if (getenv("V")) {
> fprintf(stderr, "\nUsing expected file '%s'\n", aml_file);
> }
> - ret = g_file_get_contents(aml_file, &exp_sdt.aml,
> - &exp_sdt.aml_len, &error);
> + ret = g_file_get_contents(aml_file, &aml_contents, &aml_length, &error);
> + exp_sdt.aml = aml_contents;
> + exp_sdt.aml_len = aml_length;
> g_assert(ret);
> g_assert_no_error(error);
> g_assert(exp_sdt.aml);
> --
> 2.7.4
prev parent reply other threads:[~2017-03-27 18:47 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-27 18:18 [Qemu-devel] [PATCH for-2.9] tests/bios-tables-test: Don't pass addresses of packed struct fields Peter Maydell
2017-03-27 18:47 ` Michael S. Tsirkin [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170327214256-mutt-send-email-mst@kernel.org \
--to=mst@redhat.com \
--cc=imammedo@redhat.com \
--cc=patches@linaro.org \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).