Skip to content

[clang] Missed attribute dereferenceable() for complex argument in stack #129337

@weiguozhi

Description

@weiguozhi

Compile the following source code

clang -O3 -fno-exceptions -c test2.cc -o test2.o

#include<string>

char foo(std::string str, int i) {
  return str[i];
}

std::string ha; 
char qux() {
  foo(ha, 3); 
}

I got the following IR

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef signext i8 @_Z3fooNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi(ptr noundef %str, i32 noundef %i) #0 {
entry:
  %str.indirect_addr = alloca ptr, align 8
  %i.addr = alloca i32, align 4
  store ptr %str, ptr %str.indirect_addr, align 8, !tbaa !5
  store i32 %i, ptr %i.addr, align 4, !tbaa !10
  %0 = load i32, ptr %i.addr, align 4, !tbaa !10
  %conv = sext i32 %0 to i64
  %call = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %str, i64 noundef %conv)
  %1 = load i8, ptr %call, align 1, !tbaa !12
  ret i8 %1 
}

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef signext i8 @_Z3quxv() #0 {
entry:
  %agg.tmp = alloca %"class.std::__cxx11::basic_string", align 8
  call void @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC2ERKS4_(ptr noundef nonnull align 8 dereferenceable(32) %agg.tmp, ptr noundef nonnull align 8 dereferenceable(32) @_Z2haB5cxx11) 
  %call = call noundef signext i8 @_Z3fooNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi(ptr noundef %agg.tmp, i32 noundef 3)
  call void @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED2Ev(ptr noundef nonnull align 8 dereferenceable(32) %agg.tmp) #2
  unreachable
} 

The first parameter of foo is of type string, it's too complex, so it is actually allocated in the caller's stack, and the pointer is passed to foo. I expect it has the attribute dereferenceable(32), like the function @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm, but it is missed in function foo.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clangClang issues not falling into any other category

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions