diff mbox series

[3/4] hurd: Fix sc_i386_thread_state layout

Message ID 20230515083323.1358039-4-bugaevc@gmail.com
State New
Headers show
Series x86_64-gnu signal fixes | expand

Commit Message

Sergey Bugaev May 15, 2023, 8:33 a.m. UTC
The real i386_thread_state Mach structure has an alignment of 8 on
x86_64. However, in struct sigcontext, the compiler was packing sc_gs
(which is the first member of sc_i386_thread_state) into the same 8-byte
slot as sc_error; this resulted in the rest of sc_i386_thread_state
members having wrong offsets relative to each other, and the overall
sc_i386_thread_state layout mismatching that of i386_thread_state.

Fix this by explicitly adding the required padding members, and
statically asserting that this results in the desired alignment.

The same goes for sc_i386_float_state.

Checked on x86_64-gnu.

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 sysdeps/mach/hurd/x86/trampoline.c         | 6 ++++++
 sysdeps/mach/hurd/x86_64/bits/sigcontext.h | 8 ++++++++
 2 files changed, 14 insertions(+)

Comments

Samuel Thibault May 15, 2023, 4:29 p.m. UTC | #1
Applied, thanks!

Sergey Bugaev, le lun. 15 mai 2023 11:33:22 +0300, a ecrit:
> The real i386_thread_state Mach structure has an alignment of 8 on
> x86_64. However, in struct sigcontext, the compiler was packing sc_gs
> (which is the first member of sc_i386_thread_state) into the same 8-byte
> slot as sc_error; this resulted in the rest of sc_i386_thread_state
> members having wrong offsets relative to each other, and the overall
> sc_i386_thread_state layout mismatching that of i386_thread_state.
> 
> Fix this by explicitly adding the required padding members, and
> statically asserting that this results in the desired alignment.
> 
> The same goes for sc_i386_float_state.
> 
> Checked on x86_64-gnu.
> 
> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
> ---
>  sysdeps/mach/hurd/x86/trampoline.c         | 6 ++++++
>  sysdeps/mach/hurd/x86_64/bits/sigcontext.h | 8 ++++++++
>  2 files changed, 14 insertions(+)
> 
> diff --git a/sysdeps/mach/hurd/x86/trampoline.c b/sysdeps/mach/hurd/x86/trampoline.c
> index 1f92064e..6318c952 100644
> --- a/sysdeps/mach/hurd/x86/trampoline.c
> +++ b/sysdeps/mach/hurd/x86/trampoline.c
> @@ -242,11 +242,17 @@ _hurd_setup_sighandler (struct hurd_sigstate *ss, const struct sigaction *action
>  
>        /* struct sigcontext is laid out so that starting at sc_gs mimics a
>  	 struct i386_thread_state.  */
> +      _Static_assert (offsetof (struct sigcontext, sc_i386_thread_state)
> +		      % __alignof__ (struct i386_thread_state) == 0,
> +		      "sc_i386_thread_state layout mismatch");
>        memcpy (&scp->sc_i386_thread_state,
>  	      &state->basic, sizeof (state->basic));
>  
>        /* struct sigcontext is laid out so that starting at sc_fpkind mimics
>  	 a struct i386_float_state.  */
> +      _Static_assert (offsetof (struct sigcontext, sc_i386_float_state)
> +		      % __alignof__ (struct i386_float_state) == 0,
> +		      "sc_i386_float_state layout mismatch");
>        ok = machine_get_state (ss->thread, state, i386_FLOAT_STATE,
>  			      &state->fpu, &scp->sc_i386_float_state,
>  			      sizeof (state->fpu));
> diff --git a/sysdeps/mach/hurd/x86_64/bits/sigcontext.h b/sysdeps/mach/hurd/x86_64/bits/sigcontext.h
> index 3a3b34bc..63960544 100644
> --- a/sysdeps/mach/hurd/x86_64/bits/sigcontext.h
> +++ b/sysdeps/mach/hurd/x86_64/bits/sigcontext.h
> @@ -46,6 +46,11 @@ struct sigcontext
>      /* Error code associated with this signal (interpreted as `error_t').  */
>      int sc_error;
>  
> +    /* Make sure the below members are properly aligned, and not packed
> +       together with sc_error -- otherwise the layout won't match that of
> +       i386_thread_state.  */
> +    int sc_pad1;
> +
>      /* All following members are machine-dependent.  The rest of this
>         structure is written to be laid out identically to:
>         {
> @@ -86,6 +91,9 @@ struct sigcontext
>      long sc_ursp;		/* This stack pointer is used.  */
>      int sc_ss;			/* Stack segment register.  */
>  
> +    /* Make sure the below has the same layout as i386_float_state.  */
> +    int sc_pad2;
> +
>      /* Following mimics struct i386_float_state.  Structures and symbolic
>         values can be found in <mach/i386/fp_reg.h>.  */
>  #define sc_i386_float_state sc_fpkind
> -- 
> 2.40.1
> 
>
diff mbox series

Patch

diff --git a/sysdeps/mach/hurd/x86/trampoline.c b/sysdeps/mach/hurd/x86/trampoline.c
index 1f92064e..6318c952 100644
--- a/sysdeps/mach/hurd/x86/trampoline.c
+++ b/sysdeps/mach/hurd/x86/trampoline.c
@@ -242,11 +242,17 @@  _hurd_setup_sighandler (struct hurd_sigstate *ss, const struct sigaction *action
 
       /* struct sigcontext is laid out so that starting at sc_gs mimics a
 	 struct i386_thread_state.  */
+      _Static_assert (offsetof (struct sigcontext, sc_i386_thread_state)
+		      % __alignof__ (struct i386_thread_state) == 0,
+		      "sc_i386_thread_state layout mismatch");
       memcpy (&scp->sc_i386_thread_state,
 	      &state->basic, sizeof (state->basic));
 
       /* struct sigcontext is laid out so that starting at sc_fpkind mimics
 	 a struct i386_float_state.  */
+      _Static_assert (offsetof (struct sigcontext, sc_i386_float_state)
+		      % __alignof__ (struct i386_float_state) == 0,
+		      "sc_i386_float_state layout mismatch");
       ok = machine_get_state (ss->thread, state, i386_FLOAT_STATE,
 			      &state->fpu, &scp->sc_i386_float_state,
 			      sizeof (state->fpu));
diff --git a/sysdeps/mach/hurd/x86_64/bits/sigcontext.h b/sysdeps/mach/hurd/x86_64/bits/sigcontext.h
index 3a3b34bc..63960544 100644
--- a/sysdeps/mach/hurd/x86_64/bits/sigcontext.h
+++ b/sysdeps/mach/hurd/x86_64/bits/sigcontext.h
@@ -46,6 +46,11 @@  struct sigcontext
     /* Error code associated with this signal (interpreted as `error_t').  */
     int sc_error;
 
+    /* Make sure the below members are properly aligned, and not packed
+       together with sc_error -- otherwise the layout won't match that of
+       i386_thread_state.  */
+    int sc_pad1;
+
     /* All following members are machine-dependent.  The rest of this
        structure is written to be laid out identically to:
        {
@@ -86,6 +91,9 @@  struct sigcontext
     long sc_ursp;		/* This stack pointer is used.  */
     int sc_ss;			/* Stack segment register.  */
 
+    /* Make sure the below has the same layout as i386_float_state.  */
+    int sc_pad2;
+
     /* Following mimics struct i386_float_state.  Structures and symbolic
        values can be found in <mach/i386/fp_reg.h>.  */
 #define sc_i386_float_state sc_fpkind