[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2019-02-21 Thread vries at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #15 from Tom de Vries  ---
(In reply to Jakub Jelinek from comment #13)
> You can try, but it seems upstream doesn't really care.

Done: https://reviews.llvm.org/D58493

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2019-02-21 Thread dennis.khalikov at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #14 from Denis Khalikov  ---
IMHO, this is a really hard to face in real word.
You, probably, have to write your program on assembly and then let "as" to add
section with debuginfo, because "as" can not generate dwarf tag
(DW_TAG_subprogram) which cares about address and name of the function, you
will get the poblem.

As far as I remember, the problem was faced in some "fault injection" project,
because the wrappers around libbactrace functions, were copy-pasted from
sanitizers, and does not really related to sanitizers in any cases.

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2019-02-21 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #13 from Jakub Jelinek  ---
You can try, but it seems upstream doesn't really care.

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2019-02-21 Thread vries at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #12 from Tom de Vries  ---
(In reply to Tom de Vries from comment #11)
> Created attachment 45652 [details]
> Tentative patch
> 
> Patch proposed in comment #10, added ChangeLog and rationale, bootstrapped
> and reg-tested.

Hi Jakub,

does this patch address your concerns mentioned in comment 6?

In other words, OK to propose at libsanitizer upstream?

Thanks,
- Tom

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2019-02-10 Thread vries at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #11 from Tom de Vries  ---
Created attachment 45652
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45652=edit
Tentative patch

Patch proposed in comment #10, added ChangeLog and rationale, bootstrapped and
reg-tested.

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2019-02-10 Thread vries at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

Tom de Vries  changed:

   What|Removed |Added

 CC||vries at gcc dot gnu.org

--- Comment #10 from Tom de Vries  ---
(In reply to Jakub Jelinek from comment #4)
> (In reply to Denis Khalikov from comment #3)
> > This fix
> > 111   AddressInfo *info = cdata->get_new_frame(addr);
> > 112   if (filename)
> > 113 info->file = internal_strdup(filename);
> > 114   info->line = lineno;
> > 115   if (function) {
> > 116 info->function = DemangleAlloc(function, /*always_alloc*/ true);
> > 117 cdata->frames_symbolized++;
> > 118   }
> > 119   return 0;
> > 120 }
> 
> I think you should just return 0; early if function == NULL and
> cdata->frames_symbolized > 0, trying to augment that case with
> backtrace_syminfo doesn't look right.

I don't think it can happen that frames_symbolized > 0 and backtrace_syminfo
gets called, because of the 'if (data.frames_symbolized > 0) return true' here:
...
bool LibbacktraceSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
  SymbolizeCodeCallbackArg data;
  data.first = stack;
  data.last = stack;
  data.frames_symbolized = 0;
  backtrace_pcinfo((backtrace_state *)state_, addr,
SymbolizeCodePCInfoCallback,
   ErrorCallback, );
  if (data.frames_symbolized > 0)
return true;
  backtrace_syminfo((backtrace_state *)state_, addr, SymbolizeCodeCallback,
ErrorCallback, );
  return (data.frames_symbolized > 0);
}
...

(In reply to Jakub Jelinek from comment #6)
> The thing is that for backtrace_pcinfo the callback can be called multiple
> times.
> And IMNSHO the hack you want to do (fill in file/line in the
> backtrace_pcinfo call and function during backtrace_syminfo) is something
> you want to do only if backtrace_pcinfo calls the callback just once.

So how about:
...
diff --git a/libsanitizer/sanitizer_common/sanitizer_symbolizer_libbacktrace.cc
b/libsanitizer/sanitizer_common
/sanitizer_symbolizer_libbacktrace.cc
index eebc30b124d..79db5d49fba 100644
--- a/libsanitizer/sanitizer_common/sanitizer_symbolizer_libbacktrace.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_symbolizer_libbacktrace.cc
@@ -109,14 +109,13 @@ static int SymbolizeCodePCInfoCallback(void *vdata,
uintptr_t addr,
const char *filename, int lineno,
const char *function) {
   SymbolizeCodeCallbackArg *cdata = (SymbolizeCodeCallbackArg *)vdata;
-  if (function) {
-AddressInfo *info = cdata->get_new_frame(addr);
+  AddressInfo *info = cdata->get_new_frame(addr);
+  if (filename)
+info->file = internal_strdup(filename);
+  info->line = lineno;
+  if (function)
 info->function = DemangleAlloc(function, /*always_alloc*/ true);
-if (filename)
-  info->file = internal_strdup(filename);
-info->line = lineno;
-cdata->frames_symbolized++;
-  }
+  cdata->frames_symbolized++;
   return 0;
 }

@@ -161,7 +160,11 @@ bool LibbacktraceSymbolizer::SymbolizePC(uptr addr,
SymbolizedStack *stack) {
   data.frames_symbolized = 0;
   backtrace_pcinfo((backtrace_state *)state_, addr,
SymbolizeCodePCInfoCallback,
ErrorCallback, );
-  if (data.frames_symbolized > 0)
+  if (data.frames_symbolized == 1 && data.last->info.function == 0)
+/* Augment the frame by trying to fill in the missing function with
+   backtrace_syminfo.  */
+data.frames_symbolized = 0;
+  else if (data.frames_symbolized > 0)
 return true;
   backtrace_syminfo((backtrace_state *)state_, addr, SymbolizeCodeCallback,
 ErrorCallback, );
...
?

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2017-06-16 Thread d.khalikov at partner dot samsung.com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #9 from Denis Khalikov  ---
I've added patch for review 
https://reviews.llvm.org/D34149

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2017-06-13 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #8 from Jakub Jelinek  ---
(In reply to Denis Khalikov from comment #7)
> Thanks, I was mistaken, fix should be in the libbacktrace.

No, I wasn't saying that.  I meant:
static int SymbolizeCodePCInfoCallback(void *vdata, uintptr_t addr,
   const char *filename, int lineno,
   const char *function) {
  SymbolizeCodeCallbackArg *cdata = (SymbolizeCodeCallbackArg *)vdata;
  if (function == NULL && cdata->frames_symbolized)
return 0;
  AddressInfo *info = cdata->get_new_frame(addr);
  if (filename)
info->file = internal_strdup(filename);
  info->line = lineno;
  if (function) {
info->function = DemangleAlloc(function, /*always_alloc*/ true);
cdata->frames_symbolized++;
  }
  return 0;
}
or so.

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2017-06-13 Thread d.khalikov at partner dot samsung.com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #7 from Denis Khalikov  ---
Thanks, I was mistaken, fix should be in the libbacktrace.

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2017-06-13 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #6 from Jakub Jelinek  ---
The thing is that for backtrace_pcinfo the callback can be called multiple
times.
And IMNSHO the hack you want to do (fill in file/line in the backtrace_pcinfo
call and function during backtrace_syminfo) is something you want to do only if
backtrace_pcinfo calls the callback just once.

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2017-06-13 Thread d.khalikov at partner dot samsung.com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #5 from Denis Khalikov  ---
As I understood libbacktrace has two main calls to symbolize pc.

1. backtrace_pcinfo 

/* Given a PC, find the file name, line number, and function name.  */
164 
165 int
166 backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
167   backtrace_full_callback callback,
168   backtrace_error_callback error_callback, void *data)
169 {

which initializing data with filename, line number and function name from
debuginfo sections

817   if (!backtrace_dwarf_add (state, base_address,
818 sections[DEBUG_INFO].data,
819 sections[DEBUG_INFO].size,
820 sections[DEBUG_LINE].data,
821 sections[DEBUG_LINE].size,
822 sections[DEBUG_ABBREV].data,
823 sections[DEBUG_ABBREV].size,
824 sections[DEBUG_RANGES].data,
825 sections[DEBUG_RANGES].size,
826 sections[DEBUG_STR].data,
827 sections[DEBUG_STR].size,
828 ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
829 error_callback, data, fileline_fn))
830 goto fail;
831 
832   *found_dwarf = 1;

179 /* Given a PC, find the symbol for it, and its value.  */

2. backtrace_syminfo

int
182 backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,
183backtrace_syminfo_callback callback,
184backtrace_error_callback error_callback, void *data)
185 {


Which is using only symbol table to intialize specific data. No debug info
needed.

748   if (!elf_initialize_syminfo (state, base_address,
749symtab_view.data, symtab_shdr->sh_size,
750strtab_view.data, strtab_shdr->sh_size,
751error_callback, data, sdata))


Those two calls using in function

156 bool LibbacktraceSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack)
{
157   SymbolizeCodeCallbackArg data;
158   data.first = stack;
159   data.last = stack;
160   data.frames_symbolized = 0;
161   backtrace_pcinfo((backtrace_state *)state_, addr,
SymbolizeCodePCInfoCallback,
162ErrorCallback, );
163   if (data.frames_symbolized > 0)
164 return true;
165   backtrace_syminfo((backtrace_state *)state_, addr, SymbolizeCodeCallback,
166 ErrorCallback, );
167   return (data.frames_symbolized > 0);
168 }

with two callback:
1.
106 extern "C" {
107 static int SymbolizeCodePCInfoCallback(void *vdata, uintptr_t addr,
108const char *filename, int lineno,
109const char *function) {
110   SymbolizeCodeCallbackArg *cdata = (SymbolizeCodeCallbackArg *)vdata;
111   if (function) {
112 AddressInfo *info = cdata->get_new_frame(addr);
113 info->function = DemangleAlloc(function, /*always_alloc*/ true);
114 if (filename)
115   info->file = internal_strdup(filename);
116 info->line = lineno;
117 cdata->frames_symbolized++;
118   }
119   return 0;
120 }


2.122 static void SymbolizeCodeCallback(void *vdata, uintptr_t addr,
123   const char *symname, uintptr_t,
uintptr_t) {
124   SymbolizeCodeCallbackArg *cdata = (SymbolizeCodeCallbackArg *)vdata;
125   if (symname) {
126 AddressInfo *info = cdata->get_new_frame(addr);
127 info->function = DemangleAlloc(symname, /*always_alloc*/ true);
128 cdata->frames_symbolized++;
129   }
130 }

In my case we have partially valid data with line number, filename but we don't
have valid function name because debuginfo doesn't provide 
DW_TAG_subprogram and libbacktrace can not find valid symbol for specific
address, 
but this symbol was read from symbol table and have valid data.

My point is to read all data we have from first callback and if we don't have 
valid function name we assume that pc is not properly symbolized and we go
to bactrace_syminfo which will read symbol data and increment 

cdata->frames_symbolized++;

In case those two functions using completely different data to generate
backtrace.
Thanks.

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2017-06-13 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #4 from Jakub Jelinek  ---
(In reply to Denis Khalikov from comment #3)
> This fix
> 111   AddressInfo *info = cdata->get_new_frame(addr);
> 112   if (filename)
> 113 info->file = internal_strdup(filename);
> 114   info->line = lineno;
> 115   if (function) {
> 116 info->function = DemangleAlloc(function, /*always_alloc*/ true);
> 117 cdata->frames_symbolized++;
> 118   }
> 119   return 0;
> 120 }

I think you should just return 0; early if function == NULL and
cdata->frames_symbolized > 0, trying to augment that case with
backtrace_syminfo doesn't look right.

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2017-06-13 Thread d.khalikov at partner dot samsung.com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #3 from Denis Khalikov  ---
This fix
111   AddressInfo *info = cdata->get_new_frame(addr);
112   if (filename)
113 info->file = internal_strdup(filename);
114   info->line = lineno;
115   if (function) {
116 info->function = DemangleAlloc(function, /*always_alloc*/ true);
117 cdata->frames_symbolized++;
118   }
119   return 0;
120 }

solve my problem.

As far as I understood this chages should be made in llvm asan/

Should I send the patch Phabricator for review.

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2017-06-13 Thread jakub at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

--- Comment #2 from Jakub Jelinek  ---
libbacktrace upstream is in GCC, and the libsanitizer libbacktrace support,
while it is upstream, is not what LLVM uses (they have their own symbolizer
instead, using external program).

[Bug sanitizer/81081] [ASAN] ASAN is not properly calling libbacktrace to symbolize program written on assembler

2017-06-13 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81081

Richard Biener  changed:

   What|Removed |Added

   Keywords||diagnostic
 Status|UNCONFIRMED |NEW
   Last reconfirmed||2017-06-13
 CC||rguenth at gcc dot gnu.org
 Blocks||78063
 Ever confirmed|0   |1

--- Comment #1 from Richard Biener  ---
I've also seen this behavior (throwing away useful data) with PR78063.  So
fixing this would also help PR78063 for the cases ASAN backtraces are affected
(I needed to adjust the testsuite outcome for early LTO debug).

Confirmed.

Not sure if this is part of upstream asan (so should be reported there) or if
this is part of GCC local changes.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78063
[Bug 78063] libbacktrace fails to handle cross CU DW_AT_abstract_origin