[libc-commits] [libc] [libc] Simple __stack_chk_guard implementation (PR #78804)

Saleem Abdulrasool via libc-commits libc-commits at lists.llvm.org
Sun Jan 21 10:42:54 PST 2024


================
@@ -0,0 +1,39 @@
+//===-- Implementation of __stack_chk_guard -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/compiler/__stack_chk_guard.h"
+#include "src/__support/OSUtil/syscall.h"
+#include "src/errno/libc_errno.h"
+
+#include <sys/syscall.h>
+
+extern "C" {
+
+uintptr_t __stack_chk_guard = 0;
+
+} // extern "C"
+
+namespace LIBC_NAMESPACE {
+namespace {
+
+class StackCheckGuard {
+public:
+  StackCheckGuard() {
+    // TODO: Use getauxval(AT_RANDOM) once available.
+    long retval =
+        syscall_impl(SYS_getrandom, reinterpret_cast<long>(&__stack_chk_guard),
+                     sizeof(uintptr_t), 0);
+    if (retval < 0)
+      __stack_chk_guard = 0x00000aff; // 0, 0, '\n', 255
+  }
+};
+
+StackCheckGuard stack_check_guard;
+
+} // anonymous namespace
----------------
compnerd wrote:

With C++, anonymous namespace were the recommended way to implement `static` storage (which was eventually changed with C++11 I believe, allowing both). If the value is in a named namespace, it is technically non-static. The anonymity is meant to make the decls unutterable, and thus `static`. 

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


More information about the libc-commits mailing list