Skip to content

Commit

Permalink
csa/tests: add test suite for custom allocation classes
Browse files Browse the repository at this point in the history
  • Loading branch information
pskrgag committed Jul 17, 2024
1 parent 7fd7080 commit d3e06bb
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions clang/test/Analysis/MismatchedDeallocator-checker-test.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@
void free(void *);
void __attribute((ownership_takes(malloc, 1))) my_free(void *);

void __attribute((ownership_returns(malloc1))) *my_malloc1(size_t);
void __attribute((ownership_takes(malloc1, 1))) my_free1(void *);

void __attribute((ownership_returns(malloc2))) *my_malloc2(size_t);

// The order of these declarations are important to verify that analisys still works even
// if there are less specific declarations of the same functions
void __attribute((ownership_returns(malloc3))) *my_malloc3(size_t);
void *my_malloc3(size_t);

void *my_malloc4(size_t);
void __attribute((ownership_returns(malloc4))) *my_malloc4(size_t);

//---------------------------------------------------------------
// Test if an allocation function matches deallocation function
//---------------------------------------------------------------
Expand Down Expand Up @@ -60,6 +73,41 @@ void testMalloc8() {
operator delete[](p); // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'operator delete[]'}}
}

void testMalloc9() {
int *p = (int *)my_malloc(sizeof(int));
my_free(p); // no warning
}

void testMalloc10() {
int *p = (int *)my_malloc1(sizeof(int));
my_free1(p); // no warning
}

void testMalloc11() {
int *p = (int *)my_malloc1(sizeof(int));
my_free(p); // expected-warning{{Memory allocated by 'my_malloc1()' should be deallocated by function that takes ownership of 'malloc1', not 'my_free()', which takes ownership of 'malloc'}}
}

void testMalloc12() {
int *p = (int *)my_malloc2(sizeof(int));
my_free1(p); // expected-warning{{Memory allocated by 'my_malloc2()' should be deallocated by function that takes ownership of 'malloc2', not 'my_free1()', which takes ownership of 'malloc1'}}
}

void testMalloc13() {
int *p = (int *)my_malloc1(sizeof(int));
free(p); // expected-warning{{Memory allocated by 'my_malloc1()' should be deallocated by function that takes ownership of 'malloc1', not 'free()'}}
}

void testMalloc14() {
int *p = (int *)my_malloc3(sizeof(int));
free(p); // expected-warning{{Memory allocated by 'my_malloc3()' should be deallocated by function that takes ownership of 'malloc3', not 'free()'}}
}

void testMalloc15() {
int *p = (int *)my_malloc4(sizeof(int));
free(p); // expected-warning{{Memory allocated by 'my_malloc4()' should be deallocated by function that takes ownership of 'malloc4', not 'free()'}}
}

void testAlloca() {
int *p = (int *)__builtin_alloca(sizeof(int));
delete p; // expected-warning{{Memory allocated by 'alloca()' should not be deallocated}}
Expand Down

0 comments on commit d3e06bb

Please sign in to comment.