IR: Fix unit test memory leak reported by ASan
[oota-llvm.git] / unittests / IR / MetadataTest.cpp
1 //===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/IR/Metadata.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/Type.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h"
18 using namespace llvm;
19
20 namespace {
21
22 class MetadataTest : public testing::Test {
23 protected:
24   LLVMContext Context;
25   MDNode *getNode() { return MDNode::get(Context, None); }
26   MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
27   MDNode *getNode(Metadata *MD1, Metadata *MD2) {
28     Metadata *MDs[] = {MD1, MD2};
29     return MDNode::get(Context, MDs);
30   }
31 };
32 typedef MetadataTest MDStringTest;
33
34 // Test that construction of MDString with different value produces different
35 // MDString objects, even with the same string pointer and nulls in the string.
36 TEST_F(MDStringTest, CreateDifferent) {
37   char x[3] = { 'f', 0, 'A' };
38   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
39   x[2] = 'B';
40   MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
41   EXPECT_NE(s1, s2);
42 }
43
44 // Test that creation of MDStrings with the same string contents produces the
45 // same MDString object, even with different pointers.
46 TEST_F(MDStringTest, CreateSame) {
47   char x[4] = { 'a', 'b', 'c', 'X' };
48   char y[4] = { 'a', 'b', 'c', 'Y' };
49
50   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
51   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
52   EXPECT_EQ(s1, s2);
53 }
54
55 // Test that MDString prints out the string we fed it.
56 TEST_F(MDStringTest, PrintingSimple) {
57   char *str = new char[13];
58   strncpy(str, "testing 1 2 3", 13);
59   MDString *s = MDString::get(Context, StringRef(str, 13));
60   strncpy(str, "aaaaaaaaaaaaa", 13);
61   delete[] str;
62
63   std::string Str;
64   raw_string_ostream oss(Str);
65   s->print(oss);
66   EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
67 }
68
69 // Test printing of MDString with non-printable characters.
70 TEST_F(MDStringTest, PrintingComplex) {
71   char str[5] = {0, '\n', '"', '\\', (char)-1};
72   MDString *s = MDString::get(Context, StringRef(str+0, 5));
73   std::string Str;
74   raw_string_ostream oss(Str);
75   s->print(oss);
76   EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
77 }
78
79 typedef MetadataTest MDNodeTest;
80
81 // Test the two constructors, and containing other Constants.
82 TEST_F(MDNodeTest, Simple) {
83   char x[3] = { 'a', 'b', 'c' };
84   char y[3] = { '1', '2', '3' };
85
86   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
87   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
88   ConstantAsMetadata *CI = ConstantAsMetadata::get(
89       ConstantInt::get(getGlobalContext(), APInt(8, 0)));
90
91   std::vector<Metadata *> V;
92   V.push_back(s1);
93   V.push_back(CI);
94   V.push_back(s2);
95
96   MDNode *n1 = MDNode::get(Context, V);
97   Metadata *const c1 = n1;
98   MDNode *n2 = MDNode::get(Context, c1);
99   Metadata *const c2 = n2;
100   MDNode *n3 = MDNode::get(Context, V);
101   MDNode *n4 = MDNode::getIfExists(Context, V);
102   MDNode *n5 = MDNode::getIfExists(Context, c1);
103   MDNode *n6 = MDNode::getIfExists(Context, c2);
104   EXPECT_NE(n1, n2);
105   EXPECT_EQ(n1, n3);
106   EXPECT_EQ(n4, n1);
107   EXPECT_EQ(n5, n2);
108   EXPECT_EQ(n6, (Metadata *)nullptr);
109
110   EXPECT_EQ(3u, n1->getNumOperands());
111   EXPECT_EQ(s1, n1->getOperand(0));
112   EXPECT_EQ(CI, n1->getOperand(1));
113   EXPECT_EQ(s2, n1->getOperand(2));
114
115   EXPECT_EQ(1u, n2->getNumOperands());
116   EXPECT_EQ(n1, n2->getOperand(0));
117 }
118
119 TEST_F(MDNodeTest, Delete) {
120   Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
121   Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
122
123   Metadata *const V = LocalAsMetadata::get(I);
124   MDNode *n = MDNode::get(Context, V);
125   TrackingMDRef wvh(n);
126
127   EXPECT_EQ(n, wvh);
128
129   delete I;
130 }
131
132 TEST_F(MDNodeTest, DeleteMDNodeFwdDecl) {
133   delete MDNode::getTemporary(Context, None);
134 }
135
136 TEST_F(MDNodeTest, SelfReference) {
137   // !0 = !{!0}
138   // !1 = !{!0}
139   {
140     MDNode *Temp = MDNode::getTemporary(Context, None);
141     Metadata *Args[] = {Temp};
142     MDNode *Self = MDNode::get(Context, Args);
143     Self->replaceOperandWith(0, Self);
144     MDNode::deleteTemporary(Temp);
145     ASSERT_EQ(Self, Self->getOperand(0));
146
147     // Self-references should be distinct, so MDNode::get() should grab a
148     // uniqued node that references Self, not Self.
149     Args[0] = Self;
150     MDNode *Ref1 = MDNode::get(Context, Args);
151     MDNode *Ref2 = MDNode::get(Context, Args);
152     EXPECT_NE(Self, Ref1);
153     EXPECT_EQ(Ref1, Ref2);
154   }
155
156   // !0 = !{!0, !{}}
157   // !1 = !{!0, !{}}
158   {
159     MDNode *Temp = MDNode::getTemporary(Context, None);
160     Metadata *Args[] = {Temp, MDNode::get(Context, None)};
161     MDNode *Self = MDNode::get(Context, Args);
162     Self->replaceOperandWith(0, Self);
163     MDNode::deleteTemporary(Temp);
164     ASSERT_EQ(Self, Self->getOperand(0));
165
166     // Self-references should be distinct, so MDNode::get() should grab a
167     // uniqued node that references Self, not Self itself.
168     Args[0] = Self;
169     MDNode *Ref1 = MDNode::get(Context, Args);
170     MDNode *Ref2 = MDNode::get(Context, Args);
171     EXPECT_NE(Self, Ref1);
172     EXPECT_EQ(Ref1, Ref2);
173   }
174 }
175
176 TEST_F(MDNodeTest, Print) {
177   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
178   MDString *S = MDString::get(Context, "foo");
179   MDNode *N0 = getNode();
180   MDNode *N1 = getNode(N0);
181   MDNode *N2 = getNode(N0, N1);
182
183   Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
184   MDNode *N = MDNode::get(Context, Args);
185
186   std::string Expected;
187   {
188     raw_string_ostream OS(Expected);
189     OS << "!{";
190     C->printAsOperand(OS);
191     OS << ", ";
192     S->printAsOperand(OS);
193     OS << ", null";
194     MDNode *Nodes[] = {N0, N1, N2};
195     for (auto *Node : Nodes)
196       OS << ", <" << (void *)Node << ">";
197     OS << "}\n";
198   }
199
200   std::string Actual;
201   {
202     raw_string_ostream OS(Actual);
203     N->print(OS);
204   }
205
206   EXPECT_EQ(Expected, Actual);
207 }
208
209 TEST_F(MDNodeTest, NullOperand) {
210   // metadata !{}
211   MDNode *Empty = MDNode::get(Context, None);
212
213   // metadata !{metadata !{}}
214   Metadata *Ops[] = {Empty};
215   MDNode *N = MDNode::get(Context, Ops);
216   ASSERT_EQ(Empty, N->getOperand(0));
217
218   // metadata !{metadata !{}} => metadata !{null}
219   N->replaceOperandWith(0, nullptr);
220   ASSERT_EQ(nullptr, N->getOperand(0));
221
222   // metadata !{null}
223   Ops[0] = nullptr;
224   MDNode *NullOp = MDNode::get(Context, Ops);
225   ASSERT_EQ(nullptr, NullOp->getOperand(0));
226   EXPECT_EQ(N, NullOp);
227 }
228
229 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
230   // !{}
231   MDNode *Empty = MDNode::get(Context, None);
232   ASSERT_TRUE(Empty->isResolved());
233   EXPECT_FALSE(Empty->isDistinct());
234
235   // !{!{}}
236   Metadata *Wrapped1Ops[] = {Empty};
237   MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
238   ASSERT_EQ(Empty, Wrapped1->getOperand(0));
239   ASSERT_TRUE(Wrapped1->isResolved());
240   EXPECT_FALSE(Wrapped1->isDistinct());
241
242   // !{!{!{}}}
243   Metadata *Wrapped2Ops[] = {Wrapped1};
244   MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
245   ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
246   ASSERT_TRUE(Wrapped2->isResolved());
247   EXPECT_FALSE(Wrapped2->isDistinct());
248
249   // !{!{!{}}} => !{!{}}
250   Wrapped2->replaceOperandWith(0, Empty);
251   ASSERT_EQ(Empty, Wrapped2->getOperand(0));
252   EXPECT_TRUE(Wrapped2->isDistinct());
253   EXPECT_FALSE(Wrapped1->isDistinct());
254 }
255
256 TEST_F(MDNodeTest, getDistinct) {
257   // !{}
258   MDNode *Empty = MDNode::get(Context, None);
259   ASSERT_TRUE(Empty->isResolved());
260   ASSERT_FALSE(Empty->isDistinct());
261   ASSERT_EQ(Empty, MDNode::get(Context, None));
262
263   // distinct !{}
264   MDNode *Distinct1 = MDNode::getDistinct(Context, None);
265   MDNode *Distinct2 = MDNode::getDistinct(Context, None);
266   EXPECT_TRUE(Distinct1->isResolved());
267   EXPECT_TRUE(Distinct2->isDistinct());
268   EXPECT_NE(Empty, Distinct1);
269   EXPECT_NE(Empty, Distinct2);
270   EXPECT_NE(Distinct1, Distinct2);
271
272   // !{}
273   ASSERT_EQ(Empty, MDNode::get(Context, None));
274 }
275
276 TEST_F(MDNodeTest, TempIsDistinct) {
277   MDNode *T = MDNode::getTemporary(Context, None);
278   EXPECT_TRUE(T->isDistinct());
279   MDNode::deleteTemporary(T);
280 }
281
282 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
283   // temporary !{}
284   MDNodeFwdDecl *Temp = MDNode::getTemporary(Context, None);
285   ASSERT_FALSE(Temp->isResolved());
286
287   // distinct !{temporary !{}}
288   Metadata *Ops[] = {Temp};
289   MDNode *Distinct = MDNode::getDistinct(Context, Ops);
290   EXPECT_TRUE(Distinct->isResolved());
291   EXPECT_EQ(Temp, Distinct->getOperand(0));
292
293   // temporary !{} => !{}
294   MDNode *Empty = MDNode::get(Context, None);
295   Temp->replaceAllUsesWith(Empty);
296   MDNode::deleteTemporary(Temp);
297   EXPECT_EQ(Empty, Distinct->getOperand(0));
298 }
299
300 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
301   // !0 = !{}
302   MDNode *N0 = MDNode::get(Context, None);
303
304   // !1 = !{!3, null}
305   MDNodeFwdDecl *Temp3 = MDNode::getTemporary(Context, None);
306   Metadata *Ops1[] = {Temp3, nullptr};
307   MDNode *N1 = MDNode::get(Context, Ops1);
308
309   // !2 = !{!3, !0}
310   Metadata *Ops2[] = {Temp3, N0};
311   MDNode *N2 = MDNode::get(Context, Ops2);
312
313   // !3 = !{!2}
314   Metadata *Ops3[] = {N2};
315   MDNode *N3 = MDNode::get(Context, Ops3);
316   Temp3->replaceAllUsesWith(N3);
317   delete Temp3;
318
319   // !4 = !{!1}
320   Metadata *Ops4[] = {N1};
321   MDNode *N4 = MDNode::get(Context, Ops4);
322
323   // Confirm that the cycle prevented RAUW from getting dropped.
324   EXPECT_TRUE(N0->isResolved());
325   EXPECT_FALSE(N1->isResolved());
326   EXPECT_FALSE(N2->isResolved());
327   EXPECT_FALSE(N3->isResolved());
328   EXPECT_FALSE(N4->isResolved());
329
330   // Create a couple of distinct nodes to observe what's going on.
331   //
332   // !5 = distinct !{!2}
333   // !6 = distinct !{!3}
334   Metadata *Ops5[] = {N2};
335   MDNode *N5 = MDNode::getDistinct(Context, Ops5);
336   Metadata *Ops6[] = {N3};
337   MDNode *N6 = MDNode::getDistinct(Context, Ops6);
338
339   // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
340   // This will ripple up, with !3 colliding with !4, and RAUWing.  Since !2
341   // references !3, this can cause a re-entry of handleChangedOperand() when !3
342   // is not ready for it.
343   //
344   // !2->replaceOperandWith(1, nullptr)
345   // !2: !{!3, !0} => !{!3, null}
346   // !2->replaceAllUsesWith(!1)
347   // !3: !{!2] => !{!1}
348   // !3->replaceAllUsesWith(!4)
349   N2->replaceOperandWith(1, nullptr);
350
351   // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
352   // under us.  Just check that the other nodes are sane.
353   //
354   // !1 = !{!4, null}
355   // !4 = !{!1}
356   // !5 = distinct !{!1}
357   // !6 = distinct !{!4}
358   EXPECT_EQ(N4, N1->getOperand(0));
359   EXPECT_EQ(N1, N4->getOperand(0));
360   EXPECT_EQ(N1, N5->getOperand(0));
361   EXPECT_EQ(N4, N6->getOperand(0));
362 }
363
364 typedef MetadataTest MetadataAsValueTest;
365
366 TEST_F(MetadataAsValueTest, MDNode) {
367   MDNode *N = MDNode::get(Context, None);
368   auto *V = MetadataAsValue::get(Context, N);
369   EXPECT_TRUE(V->getType()->isMetadataTy());
370   EXPECT_EQ(N, V->getMetadata());
371
372   auto *V2 = MetadataAsValue::get(Context, N);
373   EXPECT_EQ(V, V2);
374 }
375
376 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
377   MDNode *N = MDNode::get(Context, None);
378   Metadata *Ops[] = {N};
379   MDNode *N2 = MDNode::get(Context, Ops);
380   auto *V = MetadataAsValue::get(Context, N2);
381   EXPECT_TRUE(V->getType()->isMetadataTy());
382   EXPECT_EQ(N2, V->getMetadata());
383
384   auto *V2 = MetadataAsValue::get(Context, N2);
385   EXPECT_EQ(V, V2);
386
387   auto *V3 = MetadataAsValue::get(Context, N);
388   EXPECT_TRUE(V3->getType()->isMetadataTy());
389   EXPECT_NE(V, V3);
390   EXPECT_EQ(N, V3->getMetadata());
391 }
392
393 TEST_F(MetadataAsValueTest, MDNodeConstant) {
394   auto *C = ConstantInt::getTrue(Context);
395   auto *MD = ConstantAsMetadata::get(C);
396   Metadata *Ops[] = {MD};
397   auto *N = MDNode::get(Context, Ops);
398
399   auto *V = MetadataAsValue::get(Context, MD);
400   EXPECT_TRUE(V->getType()->isMetadataTy());
401   EXPECT_EQ(MD, V->getMetadata());
402
403   auto *V2 = MetadataAsValue::get(Context, N);
404   EXPECT_EQ(MD, V2->getMetadata());
405   EXPECT_EQ(V, V2);
406 }
407
408 typedef MetadataTest ValueAsMetadataTest;
409
410 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
411   Type *Ty = Type::getInt1PtrTy(Context);
412   std::unique_ptr<GlobalVariable> GV0(
413       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
414   auto *MD = ValueAsMetadata::get(GV0.get());
415   EXPECT_TRUE(MD->getValue() == GV0.get());
416   ASSERT_TRUE(GV0->use_empty());
417
418   std::unique_ptr<GlobalVariable> GV1(
419       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
420   GV0->replaceAllUsesWith(GV1.get());
421   EXPECT_TRUE(MD->getValue() == GV1.get());
422 }
423
424 typedef MetadataTest TrackingMDRefTest;
425
426 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
427   Type *Ty = Type::getInt1PtrTy(Context);
428   std::unique_ptr<GlobalVariable> GV0(
429       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
430   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
431   EXPECT_TRUE(MD->getValue() == GV0.get());
432   ASSERT_TRUE(GV0->use_empty());
433
434   std::unique_ptr<GlobalVariable> GV1(
435       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
436   GV0->replaceAllUsesWith(GV1.get());
437   EXPECT_TRUE(MD->getValue() == GV1.get());
438
439   // Reset it, so we don't inadvertently test deletion.
440   MD.reset();
441 }
442
443 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
444   Type *Ty = Type::getInt1PtrTy(Context);
445   std::unique_ptr<GlobalVariable> GV(
446       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
447   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
448   EXPECT_TRUE(MD->getValue() == GV.get());
449   ASSERT_TRUE(GV->use_empty());
450
451   GV.reset();
452   EXPECT_TRUE(!MD);
453 }
454
455 TEST(NamedMDNodeTest, Search) {
456   LLVMContext Context;
457   ConstantAsMetadata *C =
458       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
459   ConstantAsMetadata *C2 =
460       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
461
462   Metadata *const V = C;
463   Metadata *const V2 = C2;
464   MDNode *n = MDNode::get(Context, V);
465   MDNode *n2 = MDNode::get(Context, V2);
466
467   Module M("MyModule", Context);
468   const char *Name = "llvm.NMD1";
469   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
470   NMD->addOperand(n);
471   NMD->addOperand(n2);
472
473   std::string Str;
474   raw_string_ostream oss(Str);
475   NMD->print(oss);
476   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
477                oss.str().c_str());
478 }
479 }