first jsonnet example

pull/54/head
Katarzyna Miernikiewicz 2020-09-08 10:37:17 +00:00
parent b76cb15f26
commit 78b8e130d7
7 changed files with 275 additions and 0 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "oss-internship-2020/jsonnet/jsonnet"]
path = oss-internship-2020/jsonnet/jsonnet
url = https://github.com/google/jsonnet.git

View File

@ -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)

View File

@ -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
)

View File

@ -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 <syscall.h>
#include <libgen.h>
#include <cstdlib>
#include <iostream>
#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<sandbox2::Policy> 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<JsonnetVm*> 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<char*> 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<char *> 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<bool> 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<char *> 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;
}

@ -0,0 +1 @@
Subproject commit 3e25595d5c4acd32a1c3951a57471986b90d3bad

View File

@ -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 <string.h>
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);
}

View File

@ -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 <libjsonnet.h>
}
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);