From: "Huang, Kai" <kai.huang@intel.com>
To: "linux-sgx@vger.kernel.org" <linux-sgx@vger.kernel.org>,
"jarkko@kernel.org" <jarkko@kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"Van Bulck, Jo" <jo.vanbulck@cs.kuleuven.be>
Cc: "dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>
Subject: Re: [PATCH 4/5] selftests/sgx: Ensure expected enclave data buffer size and placement.
Date: Fri, 18 Aug 2023 13:07:14 +0000 [thread overview]
Message-ID: <4022cb20af2759d0e71f72a1b4161b3e43181bca.camel@intel.com> (raw)
In-Reply-To: <a2732938-f3db-a0af-3d68-a18060f66e79@cs.kuleuven.be>
On Mon, 2023-08-07 at 11:41 +0200, Jo Van Bulck wrote:
> On 28.07.23 21:19, Jarkko Sakkinen wrote:
> > So, when exactly is it optimized away by the compiler? This is missing.
>
> The problem is that declaring encl_buf as static, implies that it will
> only be used in this file and the compiler is allowed to optimize away
> any entries that are never used within this compilation unit (e.g., when
> optimizing out the memcpy calls).
>
> In reality, the tests outside test_encl.elf rely on both the size and
> exact placement of encl_buf at the start of .data.
>
> For example, clang -Os generates the following (legal) code when
> encl_bug is declared as static:
>
> 0000000000001020 <do_encl_op_put_to_buf>:
> mov 0x8(%rdi),%al
> mov %al,0x1fd7(%rip) # 3000 <encl_buffer.0>
> mov 0x9(%rdi),%al
> mov %al,0x8fce(%rip) # a000 <encl_buffer.1.0>
> mov 0xa(%rdi),%al
> mov %al,0x8fd5(%rip) # a010 <encl_buffer.1.1>
> mov 0xb(%rdi),%al
> mov %al,0x8fce(%rip) # a012 <encl_buffer.1.2>
> mov 0xc(%rdi),%al
> mov %al,0x8fd3(%rip) # a020 <encl_buffer.1.3>
> mov 0xd(%rdi),%al
> mov %al,0x8fce(%rip) # a024 <encl_buffer.1.4>
> mov 0xe(%rdi),%al
> mov %al,0x8fd1(%rip) # a030 <encl_buffer.1.5>
> mov 0xf(%rdi),%al
> mov %al,0x8fca(%rip) # a032 <encl_buffer.1.6>
> ret
>
> Disassembly of section .data:
>
> 0000000000003000 <encl_buffer.0>:
> 3000: 01 00
> ...
> 0000000000004000 <encl_ssa_tcs1>:
>
> Thus, this proposed patch fixes both the size and location:
>
> 1. removing the static keyword from the encl_bug declaration ensures
> that the _entire_ buffer is preserved with expected size, as the
> compiler cannot anymore assume encl_buf is only used in this file.
Could we use "used" attribute?
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
used
This attribute, attached to a variable with static storage, means that
the variable must be emitted even if it appears that the variable is
not referenced.
When applied to a static data member of a C++ class template, the
attribute also means that the member is instantiated if the class
itself is instantiated.
>
> 2. adding _attribute__((section(".data.encl_buffer"))) ensures that we
> can control the expected location at the start of the .data section. I
> think this is optional, as encl_buf always seems to be placed at the
> start of .data in all my tests. But afaik this is not guaranteed as per
> the C standard and such constraints on exact placement should better be
> explicitly controlled in the linker script(?)
This looks sane.
next prev parent reply other threads:[~2023-08-18 13:08 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-24 16:58 [PATCH 0/5] selftests/sgx: Fix compilation errors Jo Van Bulck
2023-07-24 16:58 ` [PATCH 1/5] selftests/sgx: Fix uninitialized pointer dereference in error path Jo Van Bulck
2023-07-28 19:03 ` Jarkko Sakkinen
2023-08-07 6:06 ` Jo Van Bulck
2023-07-28 19:04 ` Jarkko Sakkinen
2023-08-03 3:51 ` Huang, Kai
2023-08-07 6:15 ` Jo Van Bulck
2023-07-24 16:58 ` [PATCH 2/5] selftests/sgx: Fix function pointer relocation in test enclave Jo Van Bulck
2023-07-28 19:05 ` Jarkko Sakkinen
2023-08-03 3:58 ` Huang, Kai
2023-08-07 7:13 ` Jo Van Bulck
2023-08-18 12:54 ` Huang, Kai
2023-08-19 2:30 ` Jo Van Bulck
2023-08-21 11:04 ` Huang, Kai
2023-08-21 13:24 ` Jo Van Bulck
2023-07-24 16:58 ` [PATCH 3/5] selftests/sgx: Ensure correct secinfo struct alignment " Jo Van Bulck
2023-07-28 19:05 ` Jarkko Sakkinen
2023-08-03 4:00 ` Huang, Kai
2023-08-07 9:21 ` Jo Van Bulck
2023-07-24 16:58 ` [PATCH 4/5] selftests/sgx: Ensure expected enclave data buffer size and placement Jo Van Bulck
2023-07-28 19:19 ` Jarkko Sakkinen
2023-08-07 9:41 ` Jo Van Bulck
2023-08-18 13:07 ` Huang, Kai [this message]
2023-08-19 1:11 ` Jo Van Bulck
2023-08-03 4:22 ` Huang, Kai
2023-08-07 9:50 ` Jo Van Bulck
2023-07-24 16:58 ` [PATCH 5/5] selftests/sgx: Enclave freestanding compilation + separate linker options Jo Van Bulck
2023-07-28 19:22 ` Jarkko Sakkinen
2023-08-07 10:03 ` Jo Van Bulck
2023-07-28 19:01 ` [PATCH 0/5] selftests/sgx: Fix compilation errors Jarkko Sakkinen
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=4022cb20af2759d0e71f72a1b4161b3e43181bca.camel@intel.com \
--to=kai.huang@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=jarkko@kernel.org \
--cc=jo.vanbulck@cs.kuleuven.be \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sgx@vger.kernel.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