[libc][NFC] Make x86_64 fenv functions msan safe.
authorSiva Chandra Reddy <sivachandra@google.com>
Mon, 8 Mar 2021 23:12:22 +0000 (15:12 -0800)
committerSiva Chandra Reddy <sivachandra@google.com>
Mon, 8 Mar 2021 23:20:08 +0000 (15:20 -0800)
These functions used inline asm to read FPU state. This change adds
explicit unpoisoning in these functions as the sanitizers don't see the
read operations.

libc/src/__support/CMakeLists.txt
libc/utils/FPUtil/CMakeLists.txt
libc/utils/FPUtil/x86_64/FEnv.h

index 980b510..32f538e 100644 (file)
@@ -2,4 +2,5 @@ add_header_library(
   common
   HDRS
     common.h
+    sanitizer_annotations.h
 )
index 2c1c166..43ee643 100644 (file)
@@ -31,6 +31,7 @@ add_header_library(
     libc.include.math
     libc.include.errno
     libc.include.fenv
+    libc.src.__support.common
     libc.utils.CPP.standalone_cpp
 )
 
index 3fb4881..1196aba 100644 (file)
@@ -12,6 +12,8 @@
 #include <fenv.h>
 #include <stdint.h>
 
+#include "src/__support/sanitizer_annotations.h"
+
 namespace __llvm_libc {
 namespace fputil {
 
@@ -95,6 +97,7 @@ static_assert(
 static inline uint16_t getX87ControlWord() {
   uint16_t w;
   __asm__ __volatile__("fnstcw %0" : "=m"(w)::);
+  SANITIZER_MEMORY_INITIALIZED(&w, sizeof(w));
   return w;
 }
 
@@ -105,6 +108,7 @@ static inline void writeX87ControlWord(uint16_t w) {
 static inline uint16_t getX87StatusWord() {
   uint16_t w;
   __asm__ __volatile__("fnstsw %0" : "=m"(w)::);
+  SANITIZER_MEMORY_INITIALIZED(&w, sizeof(w));
   return w;
 }
 
@@ -115,6 +119,7 @@ static inline void clearX87Exceptions() {
 static inline uint32_t getMXCSR() {
   uint32_t w;
   __asm__ __volatile__("stmxcsr %0" : "=m"(w)::);
+  SANITIZER_MEMORY_INITIALIZED(&w, sizeof(w));
   return w;
 }
 
@@ -124,6 +129,7 @@ static inline void writeMXCSR(uint32_t w) {
 
 static inline void getX87StateDescriptor(X87StateDescriptor &s) {
   __asm__ __volatile__("fnstenv %0" : "=m"(s));
+  SANITIZER_MEMORY_INITIALIZED(&s, sizeof(s));
 }
 
 static inline void writeX87StateDescriptor(const X87StateDescriptor &s) {