summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvoisin2023-07-09 19:14:44 +0200
committerjvoisin2023-07-09 19:14:44 +0200
commite7c10a1b27be94a09bb03bf31b4b66233e3d1bdc (patch)
tree74eaf63f04fa673d81fe900f38dbae1b1241fefe
parentb333df29dcfabcd4ad0cf630fdb648eaa38ec1c7 (diff)
Add some malloc annotations
-rw-r--r--include/fortify-headers.h6
-rw-r--r--include/stdio.h42
-rw-r--r--include/stdlib.h8
-rw-r--r--tests/Makefile1
-rw-r--r--tests/test_malloc.c7
5 files changed, 62 insertions, 2 deletions
diff --git a/include/fortify-headers.h b/include/fortify-headers.h
index 24af881..42a283f 100644
--- a/include/fortify-headers.h
+++ b/include/fortify-headers.h
@@ -66,6 +66,12 @@
66#define __format(...) 66#define __format(...)
67#endif 67#endif
68 68
69#if defined __has_attribute && __has_attribute (malloc)
70#define __malloc(...) __attribute__ ((malloc, __VA_ARGS__))
71#else
72#define __malloc(...)
73#endif
74
69 75
70/* TODO(jvoisin) Figure a nice way to make use of __builtin_mul_overflow while ignoring the result. */ 76/* TODO(jvoisin) Figure a nice way to make use of __builtin_mul_overflow while ignoring the result. */
71/* TODO(jvoisin) Make use of C23's stdckdint header: https://gustedt.gitlabpages.inria.fr/c23-library/#stdckdint */ 77/* TODO(jvoisin) Make use of C23's stdckdint header: https://gustedt.gitlabpages.inria.fr/c23-library/#stdckdint */
diff --git a/include/stdio.h b/include/stdio.h
index 5d589f0..8cdd826 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -29,13 +29,25 @@ __extension__
29extern "C" { 29extern "C" {
30#endif 30#endif
31 31
32#undef fdopen
32#undef fgets 33#undef fgets
34#undef fmemopen
35#undef fopen
33#undef fread 36#undef fread
34#undef fwrite 37#undef fwrite
35#undef vsprintf 38#undef popen
36#undef vsnprintf 39#undef tmpfile
37#undef snprintf 40#undef snprintf
38#undef sprintf 41#undef sprintf
42#undef vsnprintf
43#undef vsprintf
44
45__access(read_only, 2)
46__malloc(malloc (fclose, 1))
47_FORTIFY_FN(fdopen) FILE *fdopen(int __f, const char* __m)
48{
49 return __orig_fdopen(__f, __m);
50}
39 51
40__access(write_only, 1, 2) 52__access(write_only, 1, 2)
41_FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f) 53_FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f)
@@ -47,6 +59,20 @@ _FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f)
47 return __orig_fgets(__s, __n, __f); 59 return __orig_fgets(__s, __n, __f);
48} 60}
49 61
62__malloc(malloc (fclose, 1))
63_FORTIFY_FN(fmemopen) FILE *fmemopen(void* __b, size_t __s, const char* __m)
64{
65 return __orig_fmemopen(__b, __s, __m);
66}
67
68__access(read_only, 1)
69__access(read_only, 2)
70__malloc(malloc (fclose, 1))
71_FORTIFY_FN(fopen) FILE *fopen(const char* __p, const char* __m)
72{
73 return __orig_fopen(__p, __m);
74}
75
50__access(write_only, 1) 76__access(write_only, 1)
51_FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n, 77_FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n,
52 size_t __m, FILE *__f) 78 size_t __m, FILE *__f)
@@ -73,6 +99,18 @@ _FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n,
73 return __orig_fwrite(__d, __n, __m, __f); 99 return __orig_fwrite(__d, __n, __m, __f);
74} 100}
75 101
102__malloc(malloc (pclose, 1))
103_FORTIFY_FN(popen) FILE *popen(const char* __c, const char* __t)
104{
105 return __orig_popen(__c, __t);
106}
107
108__malloc(malloc (fclose, 1))
109_FORTIFY_FN(tmpfile) FILE *tmpfile(void)
110{
111 return __orig_tmpfile();
112}
113
76__access(read_write, 1, 2) 114__access(read_write, 1, 2)
77_FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n, 115_FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n,
78 const char *__f, __builtin_va_list __v) 116 const char *__f, __builtin_va_list __v)
diff --git a/include/stdlib.h b/include/stdlib.h
index 38015d4..b661862 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -36,6 +36,14 @@ __extension__
36extern "C" { 36extern "C" {
37#endif 37#endif
38 38
39#undef malloc
40
41__malloc(malloc (free, 1))
42_FORTIFY_FN(malloc) void *malloc(size_t __s)
43{
44 return __orig_malloc(__s);
45}
46
39/* FIXME clang */ 47/* FIXME clang */
40#if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(__clang__) 48#if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(__clang__)
41#undef realpath 49#undef realpath
diff --git a/tests/Makefile b/tests/Makefile
index 49ae984..6075ec8 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -57,6 +57,7 @@ TARGETS= \
57 test_fwrite_overwrite \ 57 test_fwrite_overwrite \
58 test_vsnprintf \ 58 test_vsnprintf \
59 test_vsprintf \ 59 test_vsprintf \
60 test_malloc \
60 61
61.SILENT: 62.SILENT:
62 63
diff --git a/tests/test_malloc.c b/tests/test_malloc.c
new file mode 100644
index 0000000..360ee82
--- /dev/null
+++ b/tests/test_malloc.c
@@ -0,0 +1,7 @@
1#include "common.h"
2
3#include <stdlib.h>
4
5int main(int argc, char** argv) {
6 free(malloc(1));
7}