1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <cstdint>
16 #include <iostream>
17 #include <vector>
18
19 #include "helpers.h" // NOLINT(build/include)
20 #include "sandbox.h" // NOLINT(build/include)
21 #include "absl/flags/parse.h"
22 #include "absl/log/check.h"
23 #include "absl/log/globals.h"
24 #include "absl/log/initialize.h"
25 #include "absl/status/statusor.h"
26
EncodeDecodeOneStep(SapiLodepngSandbox & sandbox,LodepngApi & api)27 void EncodeDecodeOneStep(SapiLodepngSandbox& sandbox, LodepngApi& api) {
28 // Generate the values.
29 std::vector<uint8_t> image = GenerateValues();
30
31 // Encode the image.
32 sapi::v::Array<uint8_t> sapi_image(kImgLen);
33 CHECK(std::copy(image.begin(), image.end(), sapi_image.GetData()))
34 << "Could not copy values";
35
36 sapi::v::ConstCStr sapi_filename("/output/out_generated1.png");
37
38 absl::StatusOr<unsigned int> result = api.lodepng_encode32_file(
39 sapi_filename.PtrBefore(), sapi_image.PtrBefore(), kWidth, kHeight);
40
41 CHECK(result.ok()) << "encode32_file call failed";
42 CHECK(!result.value()) << "Unexpected result from encode32_file call";
43
44 // After the image has been encoded, decode it to check that the
45 // pixel values are the same.
46 sapi::v::UInt sapi_width, sapi_height;
47 sapi::v::IntBase<uint8_t*> sapi_image_ptr(0);
48
49 result = api.lodepng_decode32_file(
50 sapi_image_ptr.PtrBoth(), sapi_width.PtrBoth(), sapi_height.PtrBoth(),
51 sapi_filename.PtrBefore());
52
53 CHECK(result.ok()) << "decode32_file call failes";
54 CHECK(!result.value()) << "Unexpected result from decode32_file call";
55
56 CHECK(sapi_width.GetValue() == kWidth) << "Widths differ";
57 CHECK(sapi_height.GetValue() == kHeight) << "Heights differ";
58
59 // The pixels have been allocated inside the sandboxed process
60 // memory, so we need to transfer them to this process.
61 // Transferring the memory has the following steps:
62 // 1) define an array with the required length.
63 // 2) set the remote pointer for the array to specify where the memory
64 // that will be transferred is located.
65 // 3) transfer the memory to this process (this step is why we need
66 // the pointer and the length).
67 sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
68 sapi_pixels.SetRemote(sapi_image_ptr.GetValue());
69
70 CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok())
71 << "Error during transfer from sandboxee";
72
73 // Now, we can compare the values.
74 CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
75 sapi_pixels.GetData() + kImgLen))
76 << "Values differ";
77
78 // Free the memory allocated inside the sandbox.
79 CHECK(sandbox.rpc_channel()->Free(sapi_image_ptr.GetValue()).ok())
80 << "Could not free memory inside sandboxed process";
81 }
82
EncodeDecodeTwoSteps(SapiLodepngSandbox & sandbox,LodepngApi & api)83 void EncodeDecodeTwoSteps(SapiLodepngSandbox& sandbox, LodepngApi& api) {
84 // Generate the values.
85 std::vector<uint8_t> image = GenerateValues();
86
87 // Encode the image into memory first.
88 sapi::v::Array<uint8_t> sapi_image(kImgLen);
89 CHECK(std::copy(image.begin(), image.end(), sapi_image.GetData()))
90 << "Could not copy values";
91
92 sapi::v::ConstCStr sapi_filename("/output/out_generated2.png");
93
94 sapi::v::ULLong sapi_pngsize;
95 sapi::v::IntBase<uint8_t*> sapi_png_ptr(0);
96
97 // Encode it into memory.
98 absl::StatusOr<unsigned int> result =
99 api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(),
100 sapi_image.PtrBefore(), kWidth, kHeight);
101
102 CHECK(result.ok()) << "encode32 call failed";
103 CHECK(!result.value()) << "Unexpected result from encode32 call";
104
105 // The new array (pointed to by sapi_png_ptr) is allocated
106 // inside the sandboxed process so we need to transfer it to this
107 // process.
108 sapi::v::Array<uint8_t> sapi_png_array(sapi_pngsize.GetValue());
109 sapi_png_array.SetRemote(sapi_png_ptr.GetValue());
110
111 CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok())
112 << "Error during transfer from sandboxee";
113
114 // Write the image into the file (from memory).
115 result =
116 api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(),
117 sapi_filename.PtrBefore());
118
119 CHECK(result.ok()) << "save_file call failed";
120 CHECK(!result.value()) << "Unexpected result from save_file call";
121
122 // Now, decode the image using the 2 steps in order to compare the values.
123 sapi::v::UInt sapi_width, sapi_height;
124 sapi::v::IntBase<uint8_t*> sapi_png_ptr2(0);
125 sapi::v::ULLong sapi_pngsize2;
126
127 // Load the file in memory.
128 result =
129 api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(),
130 sapi_filename.PtrBefore());
131
132 CHECK(result.ok()) << "load_file call failed";
133 CHECK(!result.value()) << "Unexpected result from load_file call";
134
135 CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue())
136 << "Png sizes differ";
137
138 // Transfer the png array.
139 sapi::v::Array<uint8_t> sapi_png_array2(sapi_pngsize2.GetValue());
140 sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue());
141
142 CHECK(sandbox.TransferFromSandboxee(&sapi_png_array2).ok())
143 << "Error during transfer from sandboxee";
144
145 // After the file is loaded, decode it so we have access to the values
146 // directly.
147 sapi::v::IntBase<uint8_t*> sapi_png_ptr3(0);
148 result = api.lodepng_decode32(
149 sapi_png_ptr3.PtrBoth(), sapi_width.PtrBoth(), sapi_height.PtrBoth(),
150 sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue());
151
152 CHECK(result.ok()) << "decode32 call failed";
153 CHECK(!result.value()) << "Unexpected result from decode32 call";
154
155 CHECK(sapi_width.GetValue() == kWidth) << "Widths differ";
156 CHECK(sapi_height.GetValue() == kHeight) << "Heights differ";
157
158 // Transfer the pixels so they can be used here.
159 sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
160 sapi_pixels.SetRemote(sapi_png_ptr3.GetValue());
161
162 CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok())
163 << "Error during transfer from sandboxee";
164
165 // Compare the values.
166 CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
167 sapi_pixels.GetData() + kImgLen))
168 << "Values differ";
169
170 // Free the memory allocated inside the sandbox.
171 CHECK(sandbox.rpc_channel()->Free(sapi_png_ptr.GetValue()).ok())
172 << "Could not free memory inside sandboxed process";
173 CHECK(sandbox.rpc_channel()->Free(sapi_png_ptr2.GetValue()).ok())
174 << "Could not free memory inside sandboxed process";
175 CHECK(sandbox.rpc_channel()->Free(sapi_png_ptr3.GetValue()).ok())
176 << "Could not free memory inside sandboxed process";
177 }
178
main(int argc,char * argv[])179 int main(int argc, char* argv[]) {
180 absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
181 absl::ParseCommandLine(argc, argv);
182 absl::InitializeLog();
183
184 const std::string images_path = CreateTempDirAtCWD();
185 CHECK(sapi::file_util::fileops::Exists(images_path, false))
186 << "Temporary directory does not exist";
187
188 SapiLodepngSandbox sandbox(images_path);
189 CHECK(sandbox.Init().ok()) << "Error during sandbox initialization";
190
191 LodepngApi api(&sandbox);
192
193 EncodeDecodeOneStep(sandbox, api);
194 EncodeDecodeTwoSteps(sandbox, api);
195
196 if (sapi::file_util::fileops::DeleteRecursively(images_path)) {
197 LOG(WARNING) << "Temporary folder could not be deleted";
198 }
199
200 return EXIT_SUCCESS;
201 }
202