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

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: Fix nits. 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
« no previous file with comments | « unittests/Bitcode/NaClAbbrevErrorTests.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 //===- llvm/unittest/Bitcode/NaClMungedIoTest.cpp -------------------------===//
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
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 StringRef Input(Buffer.data(), Buffer.size());
44 return MemoryBuffer::getMemBuffer(Input, "Test", false);
45 }
46
47 // Write out the bitcode, parse it back, and return the resulting
48 // munged bitcode.
49 std::string parseWrittenMungedBitcode(NaClMungedBitcode &OutBitcode) {
50 TextBuffer Buffer;
51 NaClMungedBitcode InBitcode(writeMungedBitcode(OutBitcode, Buffer));
52 return stringify(InBitcode);
53 }
54
55 // Sample toy bitcode records.
56 const uint64_t Records[] = {
57 1, naclbitc::BLK_CODE_ENTER, 8, 2, Terminator,
58 3, naclbitc::MODULE_CODE_VERSION, 1, Terminator,
59 1, naclbitc::BLK_CODE_ENTER, 0, 2, Terminator,
60 3, naclbitc::BLOCKINFO_CODE_SETBID, 12, Terminator,
61 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 1, 1, 10, Terminator,
62 0, naclbitc::BLK_CODE_EXIT, Terminator,
63 1, naclbitc::BLK_CODE_ENTER, 17, 3, Terminator,
64 2, naclbitc::BLK_CODE_DEFINE_ABBREV, 4, 1, 21, 0, 1, 1, 0, 3,
65 0, 1, 2, Terminator,
66 3, naclbitc::TYPE_CODE_NUMENTRY, 2, Terminator,
67 3, naclbitc::TYPE_CODE_VOID, Terminator,
68 4, naclbitc::TYPE_CODE_FUNCTION, 0, 0, Terminator,
69 0, naclbitc::BLK_CODE_EXIT, Terminator,
70 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 3, Terminator,
71 1, naclbitc::BLK_CODE_ENTER, 12, 3, Terminator,
72 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator,
73 4, naclbitc::FUNC_CODE_INST_RET, Terminator,
74 0, naclbitc::BLK_CODE_EXIT, Terminator,
75 0, naclbitc::BLK_CODE_EXIT, Terminator,
76 };
77
78 // Show a more readable form of what the program is.
79 TEST(NaClMungedIoTest, TestDumpingBitcode) {
80 NaClObjDumpMunger DumpMunger(ARRAY_ARGS(Records));
81 EXPECT_TRUE(DumpMunger.runTest("Display assembly"));
82 EXPECT_EQ(
83 " 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, "
84 "88, 69)\n"
85 " | 8, 0, 17, 0, 4, 0, 2, 0, 0, |PNaCl Version: 2\n"
86 " | 0> |\n"
87 " 16:0|1: <65535, 8, 2> |module { // BlockID = 8\n"
88 " 24:0| 3: <1, 1> | version 1;\n"
89 " 26:4| 1: <65535, 0, 2> | abbreviations { // BlockID "
90 "= 0\n"
91 " 36:0| 3: <1, 12> | function:\n"
92 " 38:4| 2: <65533, 1, 1, 10> | @a0 = abbrev <10>;\n"
93 " 40:4| 0: <65534> | }\n"
94 " 44:0| 1: <65535, 17, 3> | types { // BlockID = 17\n"
95 " 52:0| 2: <65533, 4, 1, 21, 0, | %a0 = abbrev <21, fixed(1),"
96 " \n"
97 " | 1, 1, 0, 3, 0, 1, 2> | array(fixed("
98 "2))>;\n"
99 " 56:7| 3: <1, 2> | count 2;\n"
100 " 59:4| 3: <2> | @t0 = void;\n"
101 " 61:3| 4: <21, 0, 0> | @t1 = void (); <%a0>\n"
102 " 62:7| 0: <65534> | }\n"
103 " 64:0| 3: <8, 1, 0, 0, 3> | define internal void @f0();\n"
104 " 68:6| 1: <65535, 12, 3> | function void @f0() { \n"
105 " | | // BlockID "
106 "= 12\n"
107 " 76:0| 3: <1, 1> | blocks 1;\n"
108 " | | %b0:\n"
109 " 78:5| 4: <10> | ret void; <@a0>\n"
110 " 79:0| 0: <65534> | }\n"
111 " 80:0|0: <65534> |}\n",
112 DumpMunger.getTestResults());
113 }
114
115 // Test that we can write out bitcode, and then read it back in.
116 TEST(NaClMungedIoTest, TestWriteThenRead) {
117 // Create munged bitcode for the given records.
118 NaClMungedBitcode Bitcode(ARRAY_ARGS(Records));
119
120 // The expected output when stringifying this input.
121 const std::string ExpectedRecords(
122 " 1: [65535, 8, 2]\n"
123 " 3: [1, 1]\n"
124 " 1: [65535, 0, 2]\n"
125 " 3: [1, 12]\n"
126 " 2: [65533, 1, 1, 10]\n"
127 " 0: [65534]\n"
128 " 1: [65535, 17, 3]\n"
129 " 2: [65533, 4, 1, 21, 0, 1, 1, 0, 3, 0, 1, 2]\n"
130 " 3: [1, 2]\n"
131 " 3: [2]\n"
132 " 4: [21, 0, 0]\n"
133 " 0: [65534]\n"
134 " 3: [8, 1, 0, 0, 3]\n"
135 " 1: [65535, 12, 3]\n"
136 " 3: [1, 1]\n"
137 " 4: [10]\n"
138 " 0: [65534]\n"
139 " 0: [65534]\n");
140 EXPECT_EQ(ExpectedRecords, stringify(Bitcode));
141
142 // Write and read the bitcode back into a sequence of records.
143 EXPECT_EQ(ExpectedRecords, parseWrittenMungedBitcode(Bitcode));
144 }
145
146
147 // Test that writing truncated bitcode is difficult, due to word
148 // alignment requirements for bitcode files. Note: Bitcode files must
149 // be divisible by 4.
150 TEST(NaClMungedIoTest, TestTruncatedNonalignedBitcode) {
151 // Created an example of a truncated bitcode file.
152 NaClMungedBitcode Bitcode(ARRAY_ARGS(Records));
153 for (size_t i = 2, e = Bitcode.getBaseRecords().size(); i < e; ++i)
154 Bitcode.remove(i);
155
156 // The expected output when stringifying this input.
157 EXPECT_EQ(
158 " 1: [65535, 8, 2]\n"
159 " 3: [1, 1]\n",
160 stringify(Bitcode));
161
162 // Show that we can't write the bitcode correctly.
163 TextBuffer Buffer;
164 EXPECT_DEATH(writeMungedBitcode(Bitcode, Buffer),
165 ".*Unflushed data remaining.*");
166 }
167
168 } // end of anonymous namespace
OLDNEW
« no previous file with comments | « unittests/Bitcode/NaClAbbrevErrorTests.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698