2020-08-07 19:01:41 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2020-08-07 19:01:41 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-10-20 21:48:06 +08:00
|
|
|
#include <cstdint>
|
2020-08-07 19:01:41 +08:00
|
|
|
#include <iostream>
|
2022-10-20 21:48:06 +08:00
|
|
|
#include <vector>
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-09-30 17:56:32 +08:00
|
|
|
#include "helpers.h" // NOLINT(build/include)
|
|
|
|
#include "sandbox.h" // NOLINT(build/include)
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/flags/parse.h"
|
|
|
|
#include "absl/log/check.h"
|
|
|
|
#include "absl/log/globals.h"
|
|
|
|
#include "absl/log/initialize.h"
|
2020-10-27 00:08:06 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-09-24 01:12:38 +08:00
|
|
|
void EncodeDecodeOneStep(SapiLodepngSandbox& sandbox, LodepngApi& api) {
|
2020-08-28 21:29:12 +08:00
|
|
|
// Generate the values.
|
2020-09-14 23:23:29 +08:00
|
|
|
std::vector<uint8_t> image = GenerateValues();
|
2020-08-28 03:53:06 +08:00
|
|
|
|
2020-08-29 02:00:52 +08:00
|
|
|
// Encode the image.
|
2020-09-08 22:21:57 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_image(kImgLen);
|
|
|
|
CHECK(std::copy(image.begin(), image.end(), sapi_image.GetData()))
|
2020-09-30 06:58:35 +08:00
|
|
|
<< "Could not copy values";
|
2020-09-08 22:21:57 +08:00
|
|
|
|
2020-08-21 00:37:45 +08:00
|
|
|
sapi::v::ConstCStr sapi_filename("/output/out_generated1.png");
|
2020-08-13 00:08:53 +08:00
|
|
|
|
2020-09-25 02:17:27 +08:00
|
|
|
absl::StatusOr<unsigned int> result = api.lodepng_encode32_file(
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi_filename.PtrBefore(), sapi_image.PtrBefore(), kWidth, kHeight);
|
2020-08-13 00:08:53 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(result.ok()) << "encode32_file call failed";
|
|
|
|
CHECK(!result.value()) << "Unexpected result from encode32_file call";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// After the image has been encoded, decode it to check that the
|
|
|
|
// pixel values are the same.
|
2020-09-14 23:23:29 +08:00
|
|
|
sapi::v::UInt sapi_width, sapi_height;
|
2020-09-24 01:12:38 +08:00
|
|
|
sapi::v::IntBase<uint8_t*> sapi_image_ptr(0);
|
2020-08-13 00:08:53 +08:00
|
|
|
|
|
|
|
result = api.lodepng_decode32_file(
|
2020-09-14 23:23:29 +08:00
|
|
|
sapi_image_ptr.PtrBoth(), sapi_width.PtrBoth(), sapi_height.PtrBoth(),
|
2020-08-13 00:08:53 +08:00
|
|
|
sapi_filename.PtrBefore());
|
2020-08-28 21:29:12 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(result.ok()) << "decode32_file call failes";
|
|
|
|
CHECK(!result.value()) << "Unexpected result from decode32_file call";
|
2020-08-13 00:08:53 +08:00
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
CHECK(sapi_width.GetValue() == kWidth) << "Widths differ";
|
|
|
|
CHECK(sapi_height.GetValue() == kHeight) << "Heights differ";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// The pixels have been allocated inside the sandboxed process
|
2020-08-13 00:08:53 +08:00
|
|
|
// memory, so we need to transfer them to this process.
|
|
|
|
// Transferring the memory has the following steps:
|
2020-08-29 02:00:52 +08:00
|
|
|
// 1) define an array with the required length.
|
|
|
|
// 2) set the remote pointer for the array to specify where the memory
|
|
|
|
// that will be transferred is located.
|
|
|
|
// 3) transfer the memory to this process (this step is why we need
|
|
|
|
// the pointer and the length).
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
|
|
|
|
sapi_pixels.SetRemote(sapi_image_ptr.GetValue());
|
2020-08-13 00:08:53 +08:00
|
|
|
|
2020-09-08 22:21:57 +08:00
|
|
|
CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok())
|
|
|
|
<< "Error during transfer from sandboxee";
|
2020-08-13 00:08:53 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Now, we can compare the values.
|
|
|
|
CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
|
2020-09-08 22:21:57 +08:00
|
|
|
sapi_pixels.GetData() + kImgLen))
|
|
|
|
<< "Values differ";
|
2020-08-28 21:29:12 +08:00
|
|
|
|
|
|
|
// Free the memory allocated inside the sandbox.
|
2022-06-10 19:23:39 +08:00
|
|
|
CHECK(sandbox.rpc_channel()->Free(sapi_image_ptr.GetValue()).ok())
|
2020-09-08 22:21:57 +08:00
|
|
|
<< "Could not free memory inside sandboxed process";
|
2020-08-07 19:01:41 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 01:12:38 +08:00
|
|
|
void EncodeDecodeTwoSteps(SapiLodepngSandbox& sandbox, LodepngApi& api) {
|
2020-08-28 21:29:12 +08:00
|
|
|
// Generate the values.
|
2020-09-14 23:23:29 +08:00
|
|
|
std::vector<uint8_t> image = GenerateValues();
|
2020-08-10 19:05:52 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Encode the image into memory first.
|
2020-09-08 22:21:57 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_image(kImgLen);
|
|
|
|
CHECK(std::copy(image.begin(), image.end(), sapi_image.GetData()))
|
2020-09-30 06:58:35 +08:00
|
|
|
<< "Could not copy values";
|
2020-09-08 22:21:57 +08:00
|
|
|
|
2020-08-21 00:37:45 +08:00
|
|
|
sapi::v::ConstCStr sapi_filename("/output/out_generated2.png");
|
2020-08-10 19:05:52 +08:00
|
|
|
|
2020-08-13 00:08:53 +08:00
|
|
|
sapi::v::ULLong sapi_pngsize;
|
2020-09-24 01:12:38 +08:00
|
|
|
sapi::v::IntBase<uint8_t*> sapi_png_ptr(0);
|
2020-08-10 19:05:52 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Encode it into memory.
|
2020-09-25 02:17:27 +08:00
|
|
|
absl::StatusOr<unsigned int> result =
|
2020-08-28 03:53:06 +08:00
|
|
|
api.lodepng_encode32(sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(),
|
|
|
|
sapi_image.PtrBefore(), kWidth, kHeight);
|
2020-08-13 00:08:53 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(result.ok()) << "encode32 call failed";
|
|
|
|
CHECK(!result.value()) << "Unexpected result from encode32 call";
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// The new array (pointed to by sapi_png_ptr) is allocated
|
2020-08-13 00:08:53 +08:00
|
|
|
// inside the sandboxed process so we need to transfer it to this
|
2020-08-28 21:29:12 +08:00
|
|
|
// process.
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_png_array(sapi_pngsize.GetValue());
|
|
|
|
sapi_png_array.SetRemote(sapi_png_ptr.GetValue());
|
2020-08-13 00:08:53 +08:00
|
|
|
|
2020-09-08 22:21:57 +08:00
|
|
|
CHECK(sandbox.TransferFromSandboxee(&sapi_png_array).ok())
|
|
|
|
<< "Error during transfer from sandboxee";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Write the image into the file (from memory).
|
2020-08-13 00:08:53 +08:00
|
|
|
result =
|
|
|
|
api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(),
|
|
|
|
sapi_filename.PtrBefore());
|
2020-08-07 20:25:15 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(result.ok()) << "save_file call failed";
|
|
|
|
CHECK(!result.value()) << "Unexpected result from save_file call";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Now, decode the image using the 2 steps in order to compare the values.
|
2020-09-14 23:23:29 +08:00
|
|
|
sapi::v::UInt sapi_width, sapi_height;
|
2020-09-24 01:12:38 +08:00
|
|
|
sapi::v::IntBase<uint8_t*> sapi_png_ptr2(0);
|
2020-08-13 00:08:53 +08:00
|
|
|
sapi::v::ULLong sapi_pngsize2;
|
2020-08-07 20:25:15 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Load the file in memory.
|
2020-08-13 00:08:53 +08:00
|
|
|
result =
|
|
|
|
api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(),
|
|
|
|
sapi_filename.PtrBefore());
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(result.ok()) << "load_file call failed";
|
|
|
|
CHECK(!result.value()) << "Unexpected result from load_file call";
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-09-08 22:21:57 +08:00
|
|
|
CHECK(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue())
|
|
|
|
<< "Png sizes differ";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Transfer the png array.
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_png_array2(sapi_pngsize2.GetValue());
|
|
|
|
sapi_png_array2.SetRemote(sapi_png_ptr2.GetValue());
|
2020-08-13 00:08:53 +08:00
|
|
|
|
2020-09-08 22:21:57 +08:00
|
|
|
CHECK(sandbox.TransferFromSandboxee(&sapi_png_array2).ok())
|
|
|
|
<< "Error during transfer from sandboxee";
|
2020-08-07 21:50:16 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// After the file is loaded, decode it so we have access to the values
|
|
|
|
// directly.
|
2020-09-24 01:12:38 +08:00
|
|
|
sapi::v::IntBase<uint8_t*> sapi_png_ptr3(0);
|
2020-08-13 00:08:53 +08:00
|
|
|
result = api.lodepng_decode32(
|
2020-09-14 23:23:29 +08:00
|
|
|
sapi_png_ptr3.PtrBoth(), sapi_width.PtrBoth(), sapi_height.PtrBoth(),
|
2020-08-13 00:08:53 +08:00
|
|
|
sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue());
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(result.ok()) << "decode32 call failed";
|
|
|
|
CHECK(!result.value()) << "Unexpected result from decode32 call";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
CHECK(sapi_width.GetValue() == kWidth) << "Widths differ";
|
|
|
|
CHECK(sapi_height.GetValue() == kHeight) << "Heights differ";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Transfer the pixels so they can be used here.
|
2020-08-28 03:53:06 +08:00
|
|
|
sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
|
|
|
|
sapi_pixels.SetRemote(sapi_png_ptr3.GetValue());
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-09-08 22:21:57 +08:00
|
|
|
CHECK(sandbox.TransferFromSandboxee(&sapi_pixels).ok())
|
|
|
|
<< "Error during transfer from sandboxee";
|
2020-08-13 00:08:53 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Compare the values.
|
|
|
|
CHECK(absl::equal(image.begin(), image.end(), sapi_pixels.GetData(),
|
2020-09-08 22:21:57 +08:00
|
|
|
sapi_pixels.GetData() + kImgLen))
|
|
|
|
<< "Values differ";
|
2020-08-28 03:53:06 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Free the memory allocated inside the sandbox.
|
2022-06-10 19:23:39 +08:00
|
|
|
CHECK(sandbox.rpc_channel()->Free(sapi_png_ptr.GetValue()).ok())
|
2020-09-08 22:21:57 +08:00
|
|
|
<< "Could not free memory inside sandboxed process";
|
2022-06-10 19:23:39 +08:00
|
|
|
CHECK(sandbox.rpc_channel()->Free(sapi_png_ptr2.GetValue()).ok())
|
2020-09-08 22:21:57 +08:00
|
|
|
<< "Could not free memory inside sandboxed process";
|
2022-06-10 19:23:39 +08:00
|
|
|
CHECK(sandbox.rpc_channel()->Free(sapi_png_ptr3.GetValue()).ok())
|
2020-09-08 22:21:57 +08:00
|
|
|
<< "Could not free memory inside sandboxed process";
|
2020-08-07 19:01:41 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 01:12:38 +08:00
|
|
|
int main(int argc, char* argv[]) {
|
2022-10-20 21:48:06 +08:00
|
|
|
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
|
|
|
|
absl::ParseCommandLine(argc, argv);
|
|
|
|
absl::InitializeLog();
|
2020-08-28 03:53:06 +08:00
|
|
|
|
|
|
|
const std::string images_path = CreateTempDirAtCWD();
|
2022-06-10 19:23:39 +08:00
|
|
|
CHECK(sapi::file_util::fileops::Exists(images_path, false))
|
2020-08-30 02:54:08 +08:00
|
|
|
<< "Temporary directory does not exist";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
SapiLodepngSandbox sandbox(images_path);
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(sandbox.Init().ok()) << "Error during sandbox initialization";
|
2020-08-07 19:01:41 +08:00
|
|
|
|
|
|
|
LodepngApi api(&sandbox);
|
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
EncodeDecodeOneStep(sandbox, api);
|
|
|
|
EncodeDecodeTwoSteps(sandbox, api);
|
|
|
|
|
2022-06-10 19:23:39 +08:00
|
|
|
if (sapi::file_util::fileops::DeleteRecursively(images_path)) {
|
2020-08-30 02:54:08 +08:00
|
|
|
LOG(WARNING) << "Temporary folder could not be deleted";
|
|
|
|
}
|
2020-08-13 00:08:53 +08:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2020-08-14 23:56:57 +08:00
|
|
|
}
|