llvm.org GIT mirror llvm / b451ba8
[C API] Prevent nullptr dereferences in C API for counting attributes. See https://reviews.llvm.org/D26392 Patch by @maleadt git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287044 91177308-0d34-0410-b5e6-96231b3b80d8 Amaury Sechet 4 years ago
6 changed file(s) with 103 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 ; RUN: llvm-as %s -o %t.bc
1 ; RUN: llvm-c-test --test-callsite-attributes < %t.bc
2 ; This used to segfault
3
4 define void @Y() {
5 ret void
6 }
7
8 define void @X() {
9 call void @X()
10 ret void
11 }
0 ; RUN: llvm-as %s -o %t.bc
1 ; RUN: llvm-c-test --test-function-attributes < %t.bc
2 ; This used to segfault
3
4 define void @X() {
5 ret void
6 }
3535 endif ()
3636
3737 add_llvm_tool(llvm-c-test
38 attributes.c
3839 calc.c
3940 diagnostic.c
4041 disassemble.c
0 /*===-- attributes.c - tool for testing libLLVM and llvm-c API ------------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file implements the --test-attributes and --test-callsite-attributes *|
10 |* commands in llvm-c-test. *|
11 |* *|
12 \*===----------------------------------------------------------------------===*/
13
14 #include "llvm-c-test.h"
15
16 #include
17
18 int llvm_test_function_attributes(void) {
19 LLVMEnablePrettyStackTrace();
20
21 LLVMModuleRef M = llvm_load_module(false, true);
22
23 LLVMValueRef F = LLVMGetFirstFunction(M);
24 while (F) {
25 // Read attributes
26 for (int Idx = LLVMAttributeFunctionIndex, ParamCount = LLVMCountParams(F);
27 Idx <= ParamCount; ++Idx) {
28 int AttrCount = LLVMGetAttributeCountAtIndex(F, Idx);
29 LLVMAttributeRef *Attrs =
30 (LLVMAttributeRef *)malloc(AttrCount * sizeof(LLVMAttributeRef));
31 LLVMGetAttributesAtIndex(F, Idx, Attrs);
32 free(Attrs);
33 }
34 F = LLVMGetNextFunction(F);
35 }
36
37 LLVMDisposeModule(M);
38
39 return 0;
40 }
41
42 int llvm_test_callsite_attributes(void) {
43 LLVMEnablePrettyStackTrace();
44
45 LLVMModuleRef M = llvm_load_module(false, true);
46
47 LLVMValueRef F = LLVMGetFirstFunction(M);
48 while (F) {
49 LLVMBasicBlockRef BB;
50 for (BB = LLVMGetFirstBasicBlock(F); BB; BB = LLVMGetNextBasicBlock(BB)) {
51 LLVMValueRef I;
52 for (I = LLVMGetFirstInstruction(BB); I; I = LLVMGetNextInstruction(I)) {
53 if (LLVMIsACallInst(I)) {
54 // Read attributes
55 for (int Idx = LLVMAttributeFunctionIndex,
56 ParamCount = LLVMCountParams(F);
57 Idx <= ParamCount; ++Idx) {
58 int AttrCount = LLVMGetCallSiteAttributeCount(I, Idx);
59 LLVMAttributeRef *Attrs = (LLVMAttributeRef *)malloc(
60 AttrCount * sizeof(LLVMAttributeRef));
61 LLVMGetCallSiteAttributes(I, Idx, Attrs);
62 free(Attrs);
63 }
64 }
65 }
66 }
67
68 F = LLVMGetNextFunction(F);
69 }
70
71 LLVMDisposeModule(M);
72
73 return 0;
74 }
5151 // diagnostic.c
5252 int llvm_test_diagnostic_handler(void);
5353
54 // attributes.c
55 int llvm_test_function_attributes();
56 int llvm_test_callsite_attributes();
57
5458 #ifdef __cplusplus
5559 }
5660 #endif /* !defined(__cplusplus) */
8787 return llvm_add_named_metadata_operand();
8888 } else if (argc == 2 && !strcmp(argv[1], "--set-metadata")) {
8989 return llvm_set_metadata();
90 } else if (argc == 2 && !strcmp(argv[1], "--test-function-attributes")) {
91 return llvm_test_function_attributes();
92 } else if (argc == 2 && !strcmp(argv[1], "--test-callsite-attributes")) {
93 return llvm_test_callsite_attributes();
9094 } else if (argc == 2 && !strcmp(argv[1], "--echo")) {
9195 return llvm_echo();
9296 } else if (argc == 2 && !strcmp(argv[1], "--test-diagnostic-handler")) {