diffkemp icon indicating copy to clipboard operation
diffkemp copied to clipboard

`getPointeeStructTypeInfo` does not handle usage of `typedef` instead of `struct` (LLVM >= 15)

Open PLukas2018 opened this issue 6 months ago • 0 comments

For LLVM >= 15 getPointeeStructTypeInfo function uses debug metadata for finding the pointee type, it does not currently handle situation when the struct type uses alias (typedef).

Example of a program that it does not handle

// old.c
#include <string.h>                                                             
typedef struct {                                                                
    int a;                                                                      
    int b;                                                                      
} s;                                                                            
void f(s *var, char x) {                                                        
    memset(var, x, sizeof(s));                                                  
}
// new.c
#include <string.h>                                                             
typedef struct {                                                                
    int a;                                                                      
    int b;                                                                      
    int c;                                                                      
} s;                                                                            
void f(s *var, char x) {                                                        
    memset(var, x, sizeof(s));                                                  
} 

compiled using

diffkemp build --clang-append="-O2" --clang-append="-g" --no-opt-override ...

This would be compared as equal if typedef was not used.

The problem is located on this line https://github.com/diffkemp/diffkemp/blob/c682da53c761c27ef6e6eadf34c886d842261d94/diffkemp/simpll/Utils.cpp#L641, because in case typedef was used the base type is not composite type but derived typedef type which base type contains info about the structure type

!13 = !DIDerivedType(tag: DW_TAG_typedef, name: "s", baseType: !14)
!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, ..., size: 96, elements: !15)

To solve this it would be necessary to check if it is not typedef and if it is then it would be necessary to recursively search the base types until we get to the DICompositeType (info about the structure type - mainly its size).

PLukas2018 avatar Jul 29 '24 14:07 PLukas2018