From 78b8e130d7fb00de648489692be9a3116177c8f1 Mon Sep 17 00:00:00 2001 From: Katarzyna Miernikiewicz Date: Tue, 8 Sep 2020 10:37:17 +0000 Subject: [PATCH] first jsonnet example --- .gitmodules | 3 + oss-internship-2020/jsonnet/CMakeLists.txt | 63 ++++++++++ .../jsonnet/examples/CMakeLists.txt | 16 +++ .../jsonnet/examples/jsonnet_example.cc | 108 ++++++++++++++++++ oss-internship-2020/jsonnet/jsonnet | 1 + oss-internship-2020/jsonnet/jsonnet_helper.cc | 51 +++++++++ oss-internship-2020/jsonnet/jsonnet_helper.h | 33 ++++++ 7 files changed, 275 insertions(+) create mode 100644 .gitmodules create mode 100644 oss-internship-2020/jsonnet/CMakeLists.txt create mode 100644 oss-internship-2020/jsonnet/examples/CMakeLists.txt create mode 100644 oss-internship-2020/jsonnet/examples/jsonnet_example.cc create mode 160000 oss-internship-2020/jsonnet/jsonnet create mode 100644 oss-internship-2020/jsonnet/jsonnet_helper.cc create mode 100644 oss-internship-2020/jsonnet/jsonnet_helper.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..12df9a5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "oss-internship-2020/jsonnet/jsonnet"] + path = oss-internship-2020/jsonnet/jsonnet + url = https://github.com/google/jsonnet.git diff --git a/oss-internship-2020/jsonnet/CMakeLists.txt b/oss-internship-2020/jsonnet/CMakeLists.txt new file mode 100644 index 0000000..37b05b9 --- /dev/null +++ b/oss-internship-2020/jsonnet/CMakeLists.txt @@ -0,0 +1,63 @@ +# 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 +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# 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. + +cmake_minimum_required(VERSION 3.10) + +project(jsonnet-sapi C CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +add_subdirectory(jsonnet) + +add_library(jsonnet_helper STATIC + jsonnet_helper.cc + jsonnet_helper.h + ${CMAKE_CURRENT_SOURCE_DIR}/jsonnet/cmd/utils.h + ${CMAKE_CURRENT_SOURCE_DIR}/jsonnet/cmd/utils.cpp +) + +target_link_libraries(jsonnet_helper + libjsonnet_for_binaries +) + +set(SAPI_ROOT "/usr/local/google/home/kasiamm/sandboxed-api" CACHE PATH "Path to the Sandboxed API source tree") +set(SAPI_ENABLE_EXAMPLES OFF CACHE BOOL "") +set(SAPI_ENABLE_TESTS OFF CACHE BOOL "") + +add_subdirectory("${SAPI_ROOT}" + "${CMAKE_BINARY_DIR}/sandboxed-api-build" + EXCLUDE_FROM_ALL) + +add_sapi_library(jsonnet_sapi + FUNCTIONS c_jsonnet_evaluate_snippet + c_jsonnet_make + c_jsonnet_destroy + c_read_input + c_free_input + c_write_output_file + c_jsonnet_realloc + INPUTS jsonnet_helper.h + LIBRARY jsonnet_helper + LIBRARY_NAME Jsonnet + NAMESPACE "" +) + +target_include_directories(jsonnet_sapi INTERFACE + "${PROJECT_BINARY_DIR}" +) + +target_link_libraries(jsonnet_sapi PUBLIC jsonnet_helper) + +add_subdirectory(examples) diff --git a/oss-internship-2020/jsonnet/examples/CMakeLists.txt b/oss-internship-2020/jsonnet/examples/CMakeLists.txt new file mode 100644 index 0000000..6cbc323 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/CMakeLists.txt @@ -0,0 +1,16 @@ + +#target_include_directories(jsonnet_helper PUBLIC +# "${CMAKE_CURRENT_SOURCE_DIR}/jsonnet" +# "${PROJECT_BINARY_DIR}" +#) + +add_executable(jsonnet_sandboxed + jsonnet_example.cc +) + +target_link_libraries(jsonnet_sandboxed PRIVATE + libjsonnet + jsonnet_helper + jsonnet_sapi + sapi::sapi +) diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_example.cc b/oss-internship-2020/jsonnet/examples/jsonnet_example.cc new file mode 100644 index 0000000..2ace227 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_example.cc @@ -0,0 +1,108 @@ +// 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +#include +#include + +#include +#include + +#include "jsonnet_sapi.sapi.h" +#include "sandboxed_api/util/flag.h" + +class JsonnetSapiSandbox : public JsonnetSandbox { + public: + explicit JsonnetSapiSandbox(std::string in_file, std::string out_file) + : in_file_(std::move(in_file)), out_file_(std::move(out_file)) {} + // explicit JsonnetSapiSandbox() {}; + + std::unique_ptr ModifyPolicy( + sandbox2::PolicyBuilder*) override { + return sandbox2::PolicyBuilder() + .AllowStaticStartup() + .AllowOpen() + .AllowRead() + .AllowWrite() + .AllowStat() + .AllowSystemMalloc() + .AllowExit() + .AllowSyscalls({ + __NR_futex, + __NR_close, + }) + .AddFile(in_file_) + .AddDirectoryAt(dirname(&out_file_[0]), "/output", /*is_ro=*/false) + .BuildOrDie(); + } + private: + std::string in_file_; + std::string out_file_; + +}; + +int main(int argc, char* argv[]) { + google::InitGoogleLogging(argv[0]); + gflags::ParseCommandLineFlags(&argc, &argv, true); + + if (argc != 3) { + std::cerr << "Usage: " << basename(argv[0]) << " absolute/path/to/INPUT.jsonnet" + << " absolute/path/to/OUTPUT\n"; + return EXIT_FAILURE; + } + + std::string in_file(argv[1]); + std::string out_file(argv[2]); + + // Initialize sandbox. + JsonnetSapiSandbox sandbox(in_file, out_file); + absl::Status status = sandbox.Init(); + CHECK(status.ok()) << "Sandbox initialization failed " << status; + + JsonnetApi api(&sandbox); + + // Initialize library's main structure. + sapi::StatusOr jsonnet_vm = api.c_jsonnet_make(); + sapi::v::RemotePtr vm_pointer(jsonnet_vm.value()); + CHECK(jsonnet_vm.ok()) << "JsonnetVm initialization failed: " << jsonnet_vm.status(); + + // Read input file. + sapi::v::ConstCStr in_file_var(in_file.c_str()); + sapi::StatusOr input = api.c_read_input(false, in_file_var.PtrBefore()); + CHECK(input.ok()) << "Reading input file failed " << input.status(); + + // Process jsonnet data. + sapi::v::RemotePtr input_pointer(input.value()); + sapi::v::Int error; + sapi::StatusOr output = api.c_jsonnet_evaluate_snippet(&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter()); + CHECK(output.ok() && !error.GetValue()) << "Jsonnet code evaluation failed" << output.status() << " " << error.GetValue(); + + // Write data to file. + std::string out_file_in_sandboxee(std::string("/output/") + basename(&out_file[0])); + sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); + sapi::v::RemotePtr output_pointer(output.value()); + sapi::StatusOr success = api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()); + CHECK(success.ok() && success.value()) << "Writing to output file failed " << success.status() << " " << success.value(); + + // Clean up. + sapi::StatusOr result = api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0); + CHECK(result.ok()) << "JsonnetVm realloc failed " << result.status(); + + status = api.c_jsonnet_destroy(&vm_pointer); + CHECK(status.ok()) << "JsonnetVm destroy failed " << status; + + status = api.c_free_input(&input_pointer); + CHECK(status.ok()) << "Input freeing failed " << status; + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/oss-internship-2020/jsonnet/jsonnet b/oss-internship-2020/jsonnet/jsonnet new file mode 160000 index 0000000..3e25595 --- /dev/null +++ b/oss-internship-2020/jsonnet/jsonnet @@ -0,0 +1 @@ +Subproject commit 3e25595d5c4acd32a1c3951a57471986b90d3bad diff --git a/oss-internship-2020/jsonnet/jsonnet_helper.cc b/oss-internship-2020/jsonnet/jsonnet_helper.cc new file mode 100644 index 0000000..2b0270e --- /dev/null +++ b/oss-internship-2020/jsonnet/jsonnet_helper.cc @@ -0,0 +1,51 @@ +// 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +#include "jsonnet_helper.h" +#include + +struct JsonnetVm* c_jsonnet_make(void) { + return jsonnet_make(); +} + +void c_jsonnet_destroy(struct JsonnetVm* vm) { + return jsonnet_destroy(vm); +} + +char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm, const char* filename, char* snippet, int* error) { + return jsonnet_evaluate_snippet(vm, filename, snippet, error); +} + +char* c_read_input(bool filename_is_code, const char* filename) { + std::string s_filename(filename); + std::string s_input; + bool check = read_input(filename_is_code, &s_filename, &s_input); + char* c_input = strdup(s_input.c_str()); + if (check) return c_input; + return nullptr; +} + +void c_free_input(char* input) { + free(input); + return; +} + +bool c_write_output_file(const char* output, const char* output_file) { + std::string s_output_file(output_file); + return write_output_file(output, s_output_file); +} + +char* c_jsonnet_realloc(JsonnetVm* vm, char* str, size_t sz) { + return jsonnet_realloc(vm, str, sz); +} \ No newline at end of file diff --git a/oss-internship-2020/jsonnet/jsonnet_helper.h b/oss-internship-2020/jsonnet/jsonnet_helper.h new file mode 100644 index 0000000..a586ac0 --- /dev/null +++ b/oss-internship-2020/jsonnet/jsonnet_helper.h @@ -0,0 +1,33 @@ +// 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +#include "jsonnet/cmd/utils.h" + +extern "C" { +#include +} + +extern "C" struct JsonnetVm* c_jsonnet_make(void); + +extern "C" void c_jsonnet_destroy(struct JsonnetVm* vm); + +extern "C" char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm, const char* filename, char* snippet, int* error); + +extern "C" char* c_read_input(bool filename_is_code, const char* filename); + +extern "C" void c_free_input(char* input); + +extern "C" bool c_write_output_file(const char* output, const char* output_file); + +extern "C" char* c_jsonnet_realloc(JsonnetVm* vm, char* str, size_t sz); \ No newline at end of file