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

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Mon Jan 22 09:01:33 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
----------------
nickdesaulniers wrote:

That makes sense for the instance of `StackCheckGuard`.  Probably don't need to put the class definition in the anonymous namespace though.

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


More information about the libc-commits mailing list