[ubsan] Fix for vptr check 29/131529/3 submit/tizen_base/20170613.074657 submit/tizen_base/20170705.023439
authorDenis Khalikov <d.khalikov@partner.samsung.com>
Mon, 29 May 2017 17:35:03 +0000 (20:35 +0300)
committerDongkyun Son <dongkyun.s@samsung.com>
Tue, 13 Jun 2017 07:13:15 +0000 (07:13 +0000)
Summary:
There can be a situation when vptr not initializing
by constructor of the object, and has a junk data
which should be properly checked, because c++ standard
says:

"If the new-initializer is omitted, the object is default-initialized (8.5).
[ Note: If no initialization is performed,
 the object has an indeterminate value. — end note ]

Change-Id: I6fd297dc10b2ddb54eaed9e6eb3a46310dafead4
Signed-off-by: Denis Khalikov <d.khalikov@partner.samsung.com>
gcc/testsuite/g++.dg/ubsan/pr332211-llvm.C [new file with mode: 0644]
libsanitizer/ubsan/ubsan_type_hash_itanium.cc

diff --git a/gcc/testsuite/g++.dg/ubsan/pr332211-llvm.C b/gcc/testsuite/g++.dg/ubsan/pr332211-llvm.C
new file mode 100644 (file)
index 0000000..c747754
--- /dev/null
@@ -0,0 +1,21 @@
+/* PR 332211 port from llvm */
+/* { dg-do run } */
+/* { dg-options "-fsanitize=vptr -frtti -fno-sanitize-recover=undefined"} */
+
+class Base {
+public:
+  int i;
+  virtual void print() {}
+};
+
+class Derived : public Base {
+public:
+  void print() {}
+};
+
+int main() {
+  char *c = new char[sizeof(Derived)];
+  Derived *list = (Derived *)c;
+  int foo = list->i;
+  return 0;
+}
index e4f1334..4716935 100644 (file)
@@ -191,7 +191,7 @@ struct VtablePrefix {
 };
 VtablePrefix *getVtablePrefix(void *Vtable) {
   VtablePrefix *Vptr = reinterpret_cast<VtablePrefix*>(Vtable);
-  if (!Vptr)
+  if (!IsAccessibleMemoryRange((uptr)Vptr, sizeof(VtablePrefix)))
     return 0;
   VtablePrefix *Prefix = Vptr - 1;
   if (!Prefix->TypeInfo)