Re: [PATCH] libsanitizer: fix SIGSEGV in fopen64 interceptor

2020-11-19 Thread Vyacheslav Barinov via Gcc-patches
Hello,

Okay, I proposed this check to upstream [1] and it has already been
accepted. We can either apply the fix or postpone it until next sync with
upstream.

Anyway the bug doesn't seem so bad if we were the only team who faced it during
all this time.

Best Regards,
Vyacheslav Barinov

[1]: https://reviews.llvm.org/D91782

Martin Liška  writes:

> On 11/19/20 12:28 PM, Slava Barinov via Gcc-patches wrote:
>> Null pointer in path argument leads to SIGSEGV in interceptor.
>
> Hello.
>
> I can't see we ever had the null check in master. I don't this it was lost
> during a merge from master.
>
> Why do we need the hunk?
> Thanks,
> Martin
>
>> libsanitizer/ChangeLog:
>>  * sanitizer_common/sanitizer_common_interceptors.inc: Check
>>  path for null before dereference in fopen64 interceptor.
>> ---
>> Notes:
>>  Apparently check has been lost during merge from upstream
>>   libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>> diff --git a/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc
>> b/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc
>> index 729eead43c0..2ef23d9a50b 100644
>> --- a/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc
>> +++ b/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc
>> @@ -6081,7 +6081,7 @@ INTERCEPTOR(__sanitizer_FILE *, freopen, const char 
>> *path, const char *mode,
>>   INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char 
>> *mode) {
>> void *ctx;
>> COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
>> -  COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
>> +  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 
>> 1);
>> COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
>> __sanitizer_FILE *res = REAL(fopen64)(path, mode);
>> COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
>> 



Re: [PATCH] Forbid section anchors for ASan build (PR sanitizer/81697)

2017-08-08 Thread Vyacheslav Barinov
Hello,

Andrew Pinski <pins...@gmail.com> writes:

> On Mon, Aug 7, 2017 at 11:15 PM, Slava Barinov <v.bari...@samsung.com> wrote:
>>gcc/
>>* varasm.c (use_object_blocks_p): Forbid section anchors for ASan
>>
>>gcc/testsuite/
>>* g++.dg/asan/global-alignment.cc: New test to test global
>>variables alignment.
>
>
> Can you describe this a little bit more?  What is going wrong here?
> Is it because there is no red zone between the variables?
I've described situation in bugzilla PR 81697 with compiled files dumps, but
briefly yes, red zone size is incorrect between global variables.

On certain platforms (we checked at least arm and ppc) the flow is following:
make_decl_rtl() calls create_block_symbol() on decl tree with ASan global
variable describing a string. But then get_variable_section() returns wrong
section (.rodata.str1.4 in my case) because check in asan_protect_global()
returns false due to !DECL_RTL_SET_P check.

When variable is placed not into .rodata, but into .rodata.str ld treats it as
string and just shrinks the large part of red zone silently (gold at least
prints warning about wrong string alignment). But in run time libasan expects
that red zone is still there and reports false positives.

In order to prevent the setting of RTL before ASan handling I tried to
reproduce the x86_64 behavior and found that use_object_blocks_p() returns
false for that platform. Accordingly to the function comment it reports if
'current compilation mode benefits from grouping', and just switching it off
for any ASan builds resolves the issue.

> Also I noticed you are using .cc as the testcase file name, why don't
> you use .C instead and then you won't need the other patch which you
> just posted.
Okay, attaching rebased version.

> Thanks,
> Andrew Pinski
>

Best Regards,
Vyacheslav Barinov
>From 88b27d5f39a5671a429db52750564245a3b35141 Mon Sep 17 00:00:00 2001
From: Slava Barinov <v.bari...@samsung.com>
Date: Thu, 3 Aug 2017 16:14:59 +0300
Subject: [PATCH] Forbid section anchors for ASan build (PR sanitizer/81697)

   gcc/
   * varasm.c (use_object_blocks_p): Forbid section anchors for ASan

   gcc/testsuite/
   * g++.dg/asan/global-alignment.C: New test to test global
   variables alignment.

Signed-off-by: Slava Barinov <v.bari...@samsung.com>
---
 gcc/ChangeLog|  6 ++
 gcc/testsuite/ChangeLog  |  6 ++
 gcc/testsuite/g++.dg/asan/global-alignment.C | 17 +
 gcc/varasm.c |  3 ++-
 4 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/asan/global-alignment.C

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index dde91ceea5b..d840825e7c8 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2017-08-08  Vyacheslav Barinov  <v.bari...@samsung.com>
+
+	PR sanitizer/81697
+	* varasm.c (use_object_blocks_p): Forbid section anchors for ASan
+	build.
+
 2017-08-08  Martin Liska  <mli...@suse.cz>
 
 	* asan.c: Include header files.
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index c2119f478ba..e0d65103d30 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2017-08-08  Vyacheslav Barinov  <v.bari...@samsung.com>
+
+	PR sanitizer/81697
+	* g++.dg/asan/global-alignment.C: New test to test global
+	variables alignment.
+
 2017-08-07  Michael Meissner  <meiss...@linux.vnet.ibm.com>
 
 	PR target/81593
diff --git a/gcc/testsuite/g++.dg/asan/global-alignment.C b/gcc/testsuite/g++.dg/asan/global-alignment.C
new file mode 100644
index 000..c011c703ea6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/asan/global-alignment.C
@@ -0,0 +1,17 @@
+/* { dg-options "-fmerge-all-constants" } */
+/* { dg-do compile } */
+/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */
+
+#include 
+#include 
+
+const char kRecoveryInstallString[] = "NEW";
+const char kRecoveryUpdateString[] = "UPDATE";
+const char kRecoveryUninstallationString[] = "UNINSTALL";
+
+const std::map<std::string, int> kStringToRequestMap = {
+  {kRecoveryInstallString, 0},
+  {kRecoveryUpdateString, 0},
+  {kRecoveryUninstallationString, 0},
+};
+/* { dg-final { scan-assembler-times {\.section\s+\.rodata\n(?:(?!\.section).)*\.(string|ascii|asciz)\s+"NEW} 1 } } */
diff --git a/gcc/varasm.c b/gcc/varasm.c
index e0834a1ff3b..dbeb8d9331e 100644
--- a/gcc/varasm.c
+++ b/gcc/varasm.c
@@ -346,7 +346,8 @@ get_section (const char *name, unsigned int flags, tree decl)
 static bool
 use_object_blocks_p (void)
 {
-  return flag_section_anchors;
+  return (flag_section_anchors
+	  && !(flag_sanitize & SANITIZE_ADDRESS));
 }
 
 /* Return the object_block structure for section SECT.  Create a new
-- 
2.13.3