2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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 <sys/ptrace.h>
|
2019-07-15 17:53:18 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
|
2021-04-29 21:00:51 +08:00
|
|
|
#include "absl/base/attributes.h"
|
|
|
|
#include "absl/base/optimization.h"
|
2021-10-11 22:59:01 +08:00
|
|
|
#include "sandboxed_api/examples/stringop/stringop_params.pb.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/lenval_core.h"
|
|
|
|
|
|
|
|
// Protobuf examples.
|
|
|
|
extern "C" int pb_reverse_string(stringop::StringReverse* pb) {
|
|
|
|
if (pb->payload_case() == pb->kInput) {
|
|
|
|
std::string output = pb->input();
|
|
|
|
std::reverse(output.begin(), output.end());
|
|
|
|
pb->set_output(output);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int pb_duplicate_string(stringop::StringDuplication* pb) {
|
|
|
|
if (pb->payload_case() == pb->kInput) {
|
|
|
|
auto output = pb->input();
|
|
|
|
pb->set_output(output + output);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Examples on raw data - both allocate and replace the data pointer.
|
|
|
|
extern "C" int reverse_string(sapi::LenValStruct* input) {
|
|
|
|
char* new_buf = static_cast<char*>(malloc(input->size));
|
|
|
|
const char* src_buf = static_cast<const char*>(input->data);
|
|
|
|
input->size = input->size;
|
|
|
|
for (size_t i = 0; i < input->size; i++) {
|
|
|
|
new_buf[i] = src_buf[input->size - i - 1];
|
|
|
|
}
|
|
|
|
// Free old value.
|
|
|
|
free(input->data);
|
2019-03-26 22:54:02 +08:00
|
|
|
// Replace pointer to our new string.
|
2019-03-19 00:21:48 +08:00
|
|
|
input->data = new_buf;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int duplicate_string(sapi::LenValStruct* input) {
|
|
|
|
char* new_buf = static_cast<char*>(malloc(2 * input->size));
|
|
|
|
const char* src_buf = static_cast<const char*>(input->data);
|
|
|
|
|
|
|
|
for (size_t c = 0; c < 2; c++) {
|
|
|
|
for (size_t i = 0; i < input->size; i++) {
|
|
|
|
new_buf[i + input->size * c] = src_buf[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free old value.
|
|
|
|
free(input->data);
|
|
|
|
// Update structure.
|
|
|
|
input->size = 2 * input->size;
|
|
|
|
input->data = new_buf;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-11-22 22:44:03 +08:00
|
|
|
extern "C" const void* get_raw_c_string() { return "Ten chars."; }
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
extern "C" void nop() {}
|
|
|
|
|
2021-05-06 22:35:41 +08:00
|
|
|
// The "no tail-call" annotation and the additional indirection ensure that
|
|
|
|
// either this function or its calling function shows up in the violation stack
|
|
|
|
// trace. Otherwise, depending on optimization level and optimizer
|
|
|
|
// aggressiveness, functions may be inlined, hoisted or omitted (in case of tail
|
|
|
|
// calls).
|
2021-04-29 21:00:51 +08:00
|
|
|
static ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL void
|
|
|
|
ViolateIndirect() {
|
2021-03-23 18:50:39 +08:00
|
|
|
ptrace((__ptrace_request)990, 991, 992, 993);
|
2021-04-29 21:00:51 +08:00
|
|
|
}
|
|
|
|
|
2021-05-06 22:35:41 +08:00
|
|
|
// Using block syntax here, because SAPI's libclang based header generator won't
|
|
|
|
// parse the attribute annotations otherwise.
|
|
|
|
extern "C" {
|
|
|
|
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL void violate() {
|
2021-04-29 21:00:51 +08:00
|
|
|
ViolateIndirect();
|
2021-05-06 22:35:41 +08:00
|
|
|
}
|
2021-03-23 18:50:39 +08:00
|
|
|
}
|