[libc-commits] [libc] Supporting stack protectors in linux x86-64 (PR #66456)

via libc-commits libc-commits at lists.llvm.org
Thu Sep 21 06:28:24 PDT 2023


================
@@ -54,7 +57,9 @@ void init_tls(TLSDescriptor &tls_descriptor) {
   // Per the x86_64 TLS ABI, the entry pointed to by the thread pointer is the
   // address of the TLS block. So, we add more size to accomodate this address
   // entry.
-  uintptr_t tlsSizeWithAddr = tlsSize + sizeof(uintptr_t);
+  // We also need to include space for the stack canary. The canary is at
+  // offset 0x28 (40) and is of size uintptr_t.
+  uintptr_t tlsSizeWithAddr = tlsSize + sizeof(uintptr_t) + 40;
----------------
tnv01 wrote:

Yes we could, though it might not be needed. Previously we were adding the sizeof(uintptr_t) since we wanted to include the address of the tls block. Now that space is already included in the 40 bytes needed to put the stack protector at the right place. 

So in particular in the new case:

uintptr_t tlsSizeWithAddr = tlsSize + sizeof(uintptr_t) + 40;

the original sizeof(uintptr_t) is not needed since we have allocated an extra 40 bytes. The sizeof(uintptr_t) is for the size of the stack guard.

I could update the comments to explain this better, or you still feel we should add the extra uintptr_t can do that too. Thanks for catching this though!



https://github.com/llvm/llvm-project/pull/66456


More information about the libc-commits mailing list