Introduce a compiler-optimization safe memset() alternative
memset() may be optimized away by the
compiler if zero'ing is considered to
have no side-effects.
This change adds always_memset(), an
implementation that indirects writes via
a volatile pointer and hence survives
optimization.
Tested against x86-64 Clang 3.9.0,
x86-64 Gcc 6.2, ARM64 gcc 4.8, and
ARM gcc 4.8.2.
BRANCH=none
BUG=none
TEST=compilation succeeds
Change-Id: Ib1d3d0c0450bf2c95b3ffcf566a484e1db6a908d
Signed-off-by: nagendra modadugu <ngm@google.com>
Reviewed-on: https://chromium-review.googlesource.com/400467
Commit-Ready: Nagendra Modadugu <ngm@google.com>
Tested-by: Nagendra Modadugu <ngm@google.com>
Reviewed-by: Marius Schilder <mschilder@chromium.org>
Reviewed-by: Andrey Pronin <apronin@chromium.org>
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..6f40509
--- /dev/null
+++ b/util.c
@@ -0,0 +1,24 @@
+// Copyright 2016 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#include "cryptoc/util.h"
+
+static void always_memset_impl(volatile char *s, int c, size_t n) {
+ while (n--)
+ *s++ = c;
+}
+
+void *always_memset(void *s, int c, size_t n) {
+ always_memset_impl(s, c, n);
+ return s;
+}