Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(550)

Side by Side Diff: unittests/Bitcode/NaClMungedIoTest.cpp

Issue 1113023005: Add abilities to generate bitcode buffers from munged bitcode. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Initial version of code. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //===- llvm/unittest/Bitcode/NaClMungedIoTest.cpp -------------------===//
jvoung (off chromium) 2015/05/05 00:27:23 could fill up the --- to line up
Karl 2015/05/05 22:38:04 Done.
2 // Tests munging NaCl bitcode records.
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 // For class NaClMungedBitcode, Tests reading initial sequence of records and
jvoung (off chromium) 2015/05/05 00:27:23 ", Tests reading" -> ", tests reading"
Karl 2015/05/05 22:38:04 Done.
12 // writing out munged set of bitcode records.
13
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/Bitcode/NaCl/NaClBitcodeMungeUtils.h"
16 #include "llvm/Bitcode/NaCl/NaClBitcodeMunge.h"
17
18 #include "gtest/gtest.h"
19
20 using namespace llvm;
21
22 namespace {
23
24 static const uint64_t Terminator = 0x5768798008978675LL;
25
26 #define ARRAY_ARGS(name) name, array_lengthof(name), Terminator
27
28 std::string stringify(NaClMungedBitcode &MungedBitcode) {
29 std::string Buffer;
30 raw_string_ostream StrBuf(Buffer);
31 MungedBitcode.print(StrBuf);
32 return StrBuf.str();
33 }
34
35 typedef SmallVector<char, 1024> TextBuffer;
36
37 // Writes out a sequence of munged bitcode records, and writes them into
38 // the text buffer. Returns a corresponding memory buffer containing
39 // the munged bitcode records.
40 std::unique_ptr<MemoryBuffer> writeMungedBitcode(
41 NaClMungedBitcode &Bitcode, TextBuffer &Buffer) {
42 Bitcode.write(Buffer, /* AddHeader = */ true,
43 /* RecoverOnBadAbbrevId = */ false);
44 StringRef Input(Buffer.data(), Buffer.size());
45
46 // Now read the bitcode back into a sequence of records.
47 return MemoryBuffer::getMemBuffer(Input, "Test", false);
48 }
49
50 // Test writing bitcode to a memory buffer.
51 void testWriteMungedBitcode(NaClMungedBitcode &Bitcode) {
jvoung (off chromium) 2015/05/05 00:27:23 This seems a little weird that it's ignoring the r
Karl 2015/05/05 22:38:04 The reason for this function was because I used it
52 TextBuffer Buffer;
53 std::unique_ptr<MemoryBuffer> MemBuf(writeMungedBitcode(Bitcode, Buffer));
54 }
55
56 // Write out the bitcode, parse it back, and return the resulting
57 // munged bitcode.
58 std::string parseWrittenMungedBitcode(NaClMungedBitcode &OutBitcode) {
59 TextBuffer Buffer;
60 NaClMungedBitcode InBitcode(writeMungedBitcode(OutBitcode, Buffer));
61 return stringify(InBitcode);
62 }
63
64 /// Sample toy bitcode records.
jvoung (off chromium) 2015/05/05 00:27:23 nit: This isn't really an API or anything that nee
Karl 2015/05/05 22:38:04 Done.
65 const uint64_t Records[] = {
66 1, naclbitc::BLK_CODE_ENTER, 8, 2, Terminator,
67 3, naclbitc::MODULE_CODE_VERSION, 1, Terminator,
68 1, naclbitc::BLK_CODE_ENTER, 0, 2, Terminator,
69 3, naclbitc::BLOCKINFO_CODE_SETBID, 12, Terminator,
70 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1, 10, Terminator,
71 0, naclbitc::BLK_CODE_EXIT, Terminator,
72 1, naclbitc::BLK_CODE_ENTER, 17, 3, Terminator,
73 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 4, 1, 21, 0, 1, 1, 0, 3,
74 0, 1, 2, Terminator,
75 3, naclbitc::TYPE_CODE_NUMENTRY, 2, Terminator,
76 3, naclbitc::TYPE_CODE_VOID, Terminator,
77 4, naclbitc::TYPE_CODE_FUNCTION, 0, 0, Terminator,
78 0, naclbitc::BLK_CODE_EXIT, Terminator,
79 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 3, Terminator,
80 1, naclbitc::BLK_CODE_ENTER, 12, 3, Terminator,
81 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator,
82 4, naclbitc::FUNC_CODE_INST_RET, Terminator,
83 0, naclbitc::BLK_CODE_EXIT, Terminator,
84 0, naclbitc::BLK_CODE_EXIT, Terminator,
85 };
86
87 /// Show a more readable form of what the program is.
88 TEST(NaClMungedIoTest, TestDumpingBitcode) {
89 NaClObjDumpMunger DumpMunger(ARRAY_ARGS(Records));
90 EXPECT_TRUE(DumpMunger.runTest("Display assembly"));
91 EXPECT_EQ(
92 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, "
93 "88, 69)\n"
94 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n"
95 " | 0> |\n"
96 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n"
97 " 24:0| 3: <1, 1> | version 1;\n"
98 " 26:4| 1: <65535, 0, 2> | abbreviations { // BlockID "
99 "= 0\n"
100 " 36:0| 3: <1, 12> | function:\n"
101 " 38:4| 2: <65533, 1, 1, 10> | @a0 = abbrev <10>;\n"
102 " 40:4| 0: <65534> | }\n"
103 " 44:0| 1: <65535, 17, 3> | types { // BlockID = 17\n"
104 " 52:0| 2: <65533, 4, 1, 21, 0, | %a0 = abbrev <21, fixed(1),"
105 " \n"
106 " | 1, 1, 0, 3, 0, 1, 2> | array(fixed("
107 "2))>;\n"
108 " 56:7| 3: <1, 2> | count 2;\n"
109 " 59:4| 3: <2> | @t0 = void;\n"
110 " 61:3| 4: <21, 0, 0> | @t1 = void (); <%a0>\n"
111 " 62:7| 0: <65534> | }\n"
112 " 64:0| 3: <8, 1, 0, 0, 3> | define internal void @f0();\n"
113 " 68:6| 1: <65535, 12, 3> | function void @f0() { \n"
114 " | | // BlockID "
115 "= 12\n"
116 " 76:0| 3: <1, 1> | blocks 1;\n"
117 " | | %b0:\n"
118 " 78:5| 4: <10> | ret void; <@a0>\n"
119 " 79:0| 0: <65534> | }\n"
120 " 80:0|0: <65534> |}\n",
121 DumpMunger.getTestResults());
122 }
123
124 /// Test that we can write out bitcode, and then read it back in.
125 TEST(NaClMungedIoTest, TestWriteThenRead) {
126 // Create munged bitcode for the given records.
127 NaClMungedBitcode Bitcode(ARRAY_ARGS(Records));
128
129 // The expected output when stringifying this input.
130 const std::string ExpectedRecords(
131 " 1: [65535, 8, 2]\n"
132 " 3: [1, 1]\n"
133 " 1: [65535, 0, 2]\n"
134 " 3: [1, 12]\n"
135 " 2: [65533, 1, 1, 10]\n"
136 " 0: [65534]\n"
137 " 1: [65535, 17, 3]\n"
138 " 2: [65533, 4, 1, 21, 0, 1, 1, 0, 3, 0, 1, 2]\n"
139 " 3: [1, 2]\n"
140 " 3: [2]\n"
141 " 4: [21, 0, 0]\n"
142 " 0: [65534]\n"
143 " 3: [8, 1, 0, 0, 3]\n"
144 " 1: [65535, 12, 3]\n"
145 " 3: [1, 1]\n"
146 " 4: [10]\n"
147 " 0: [65534]\n"
148 " 0: [65534]\n");
149 EXPECT_EQ(ExpectedRecords, stringify(Bitcode));
150
151 // Write and read the bitcode back into a sequence of records.
152 EXPECT_EQ(ExpectedRecords, parseWrittenMungedBitcode(Bitcode));
153 }
154
155
156 // Test that writing truncated bitcode is difficult, due to word
157 // alignment requirements for bitcode files. Note: Bitcode files must
158 // be divisible by 4.
159 TEST(NaClMungedIoTest, TestTruncatedNonalignedBitcode) {
160 // Created an example of a truncated bitcode file.
161 NaClMungedBitcode Bitcode(ARRAY_ARGS(Records));
162 for (size_t i = 2, e = Bitcode.getBaseRecords().size(); i < e; ++i)
163 Bitcode.remove(i);
164
165 // The expected output when stringifying this input.
166 EXPECT_EQ(
167 " 1: [65535, 8, 2]\n"
168 " 3: [1, 1]\n",
169 stringify(Bitcode));
170
171 // Show that we can write the bitcode.
172 EXPECT_DEATH(testWriteMungedBitcode(Bitcode),
173 ".*Unflushed data remaining.*");
174 }
175
176 } // end of anonymous namespace
OLDNEW
« lib/Bitcode/NaCl/TestUtils/NaClBitcodeMungeWriter.cpp ('K') | « unittests/Bitcode/CMakeLists.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698