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