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

Side by Side Diff: courgette/adjustment_method_unittest.cc

Issue 2854113002: [Courgette] Reduce AssemblyProgram to reduce Courgette-apply RAM floor and disk churn. (Closed)
Patch Set: Update courgette_fuzzer in libfuzzer. Created 3 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 | « no previous file | courgette/assembly_program.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory> 5 #include <memory>
6 #include <sstream>
6 #include <string> 7 #include <string>
7 #include <utility> 8 #include <utility>
8 #include <vector> 9 #include <vector>
9 10
10 #include "base/bind.h" 11 #include "base/bind.h"
11 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/strings/stringprintf.h"
12 #include "courgette/assembly_program.h" 14 #include "courgette/assembly_program.h"
13 #include "courgette/courgette.h" 15 #include "courgette/courgette.h"
14 #include "courgette/encoded_program.h" 16 #include "courgette/encoded_program.h"
15 #include "courgette/image_utils.h" 17 #include "courgette/image_utils.h"
16 #include "courgette/streams.h" 18 #include "courgette/streams.h"
17 19
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 21
20 namespace courgette { 22 namespace courgette {
21 23
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 EXPECT_TRUE(receptor->EmitAbs32(labelA)); 60 EXPECT_TRUE(receptor->EmitAbs32(labelA));
59 EXPECT_TRUE(receptor->EmitAbs32(labelA)); 61 EXPECT_TRUE(receptor->EmitAbs32(labelA));
60 EXPECT_TRUE(receptor->EmitAbs32(labelB)); 62 EXPECT_TRUE(receptor->EmitAbs32(labelB));
61 EXPECT_TRUE(receptor->EmitAbs32(labelA)); 63 EXPECT_TRUE(receptor->EmitAbs32(labelA));
62 EXPECT_TRUE(receptor->EmitAbs32(labelA)); 64 EXPECT_TRUE(receptor->EmitAbs32(labelA));
63 EXPECT_TRUE(receptor->EmitAbs32(labelB)); 65 EXPECT_TRUE(receptor->EmitAbs32(labelB));
64 return true; 66 return true;
65 }, 67 },
66 labelA, labelB); 68 labelA, labelB);
67 69
68 EXPECT_TRUE(prog->GenerateInstructions(gen, true)); 70 EXPECT_TRUE(prog->AnnotateLabels(gen));
69 EXPECT_EQ(6U, prog->abs32_label_annotations().size()); 71 EXPECT_EQ(6U, prog->abs32_label_annotations().size());
70 EXPECT_EQ(0U, prog->rel32_label_annotations().size()); 72 EXPECT_EQ(0U, prog->rel32_label_annotations().size());
71 73
72 if (kind == 0) { 74 if (kind == 0) {
73 labelA->index_ = 0; 75 labelA->index_ = 0;
74 labelB->index_ = 1; 76 labelB->index_ = 1;
75 } else { 77 } else {
76 labelA->index_ = 1; 78 labelA->index_ = 1;
77 labelB->index_ = 0; 79 labelB->index_ = 0;
78 } 80 }
79 prog->AssignRemainingIndexes(); 81 prog->AssignRemainingIndexes();
80 82
81 return prog; 83 return prog;
82 } 84 }
83 85
84 std::unique_ptr<AssemblyProgram> MakeProgramA() const { 86 std::unique_ptr<AssemblyProgram> MakeProgramA() const {
85 return MakeProgram(0); 87 return MakeProgram(0);
86 } 88 }
87 std::unique_ptr<AssemblyProgram> MakeProgramB() const { 89 std::unique_ptr<AssemblyProgram> MakeProgramB() const {
88 return MakeProgram(1); 90 return MakeProgram(1);
89 } 91 }
90 92
91 // Returns a string that is the serialized version of |program|. 93 // Returns a string that is the serialized version of |program| annotations.
92 // Deletes |program|. 94 std::string Serialize(AssemblyProgram* program) const {
93 std::string Serialize(std::unique_ptr<AssemblyProgram> program) const { 95 std::ostringstream oss;
94 std::unique_ptr<EncodedProgram> encoded; 96 for (const Label* label : program->abs32_label_annotations())
97 oss << "(" << label->rva_ << "," << label->index_ << ")";
98 oss << ";";
99 for (const Label* label : program->rel32_label_annotations())
100 oss << "(" << label->rva_ << "," << label->index_ << ")";
95 101
96 const Status encode_status = Encode(*program, &encoded); 102 EXPECT_GT(oss.str().length(), 1U); // Ensure results are non-trivial.
97 EXPECT_EQ(C_OK, encode_status); 103 return oss.str();
98
99 program.reset();
100
101 SinkStreamSet sinks;
102 const Status write_status = WriteEncodedProgram(encoded.get(), &sinks);
103 EXPECT_EQ(C_OK, write_status);
104
105 encoded.reset();
106
107 SinkStream sink;
108 bool can_collect = sinks.CopyTo(&sink);
109 EXPECT_TRUE(can_collect);
110
111 return std::string(reinterpret_cast<const char *>(sink.Buffer()),
112 sink.Length());
113 } 104 }
114 }; 105 };
115 106
116 void AdjustmentMethodTest::Test1() const { 107 void AdjustmentMethodTest::Test1() const {
117 std::unique_ptr<AssemblyProgram> prog1 = MakeProgramA(); 108 std::unique_ptr<AssemblyProgram> prog1 = MakeProgramA();
118 std::unique_ptr<AssemblyProgram> prog2 = MakeProgramB(); 109 std::unique_ptr<AssemblyProgram> prog2 = MakeProgramB();
119 std::string s1 = Serialize(std::move(prog1)); 110 std::string s1 = Serialize(prog1.get());
120 std::string s2 = Serialize(std::move(prog2)); 111 std::string s2 = Serialize(prog2.get());
121 112
122 // Don't use EXPECT_EQ because strings are unprintable. 113 // Don't use EXPECT_EQ because strings are unprintable.
123 EXPECT_FALSE(s1 == s2); // Unadjusted A and B differ. 114 EXPECT_FALSE(s1 == s2); // Unadjusted A and B differ.
124 115
125 std::unique_ptr<AssemblyProgram> prog5 = MakeProgramA(); 116 std::unique_ptr<AssemblyProgram> prog5 = MakeProgramA();
126 std::unique_ptr<AssemblyProgram> prog6 = MakeProgramB(); 117 std::unique_ptr<AssemblyProgram> prog6 = MakeProgramB();
127 Status can_adjust = Adjust(*prog5, prog6.get()); 118 Status can_adjust = Adjust(*prog5, prog6.get());
128 EXPECT_EQ(C_OK, can_adjust); 119 EXPECT_EQ(C_OK, can_adjust);
129 std::string s5 = Serialize(std::move(prog5)); 120 std::string s5 = Serialize(prog5.get());
130 std::string s6 = Serialize(std::move(prog6)); 121 std::string s6 = Serialize(prog6.get());
131 122
132 EXPECT_TRUE(s1 == s5); // Adjustment did not change A (prog5) 123 EXPECT_TRUE(s1 == s5); // Adjustment did not change A (prog5)
133 EXPECT_TRUE(s5 == s6); // Adjustment did change B into A 124 EXPECT_TRUE(s5 == s6); // Adjustment did change B into A
134 } 125 }
135 126
136 TEST_F(AdjustmentMethodTest, All) { 127 TEST_F(AdjustmentMethodTest, All) {
137 Test1(); 128 Test1();
138 } 129 }
139 130
140 } // namespace 131 } // namespace
141 132
142 } // namespace courgette 133 } // namespace courgette
OLDNEW
« no previous file with comments | « no previous file | courgette/assembly_program.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698