aboutsummaryrefslogtreecommitdiff
path: root/sandboxed_api/var_ptr.h
blob: ad2568c69bd64a5d86f3459afdfee6a86b74b30e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2019 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
//
//     https://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.

#ifndef SANDBOXED_API_VAR_PTR_H_
#define SANDBOXED_API_VAR_PTR_H_

#include <algorithm>
#include <cstring>
#include <memory>
#include <string>

#include "absl/base/macros.h"
#include "absl/strings/str_format.h"
#include "sandboxed_api/var_abstract.h"
#include "sandboxed_api/var_reg.h"

namespace sapi::v {

// Class representing a pointer. Takes both Var* and regular pointers in the
// initializers.
class Ptr : public Reg<Var*> {
 public:
  Ptr() = delete;

  explicit Ptr(Var* value, SyncType sync_type) : sync_type_(sync_type) {
    Reg<Var*>::SetValue(value);
  }

  Var* GetPointedVar() const { return Reg<Var*>::GetValue(); }

  void SetValue(Var* ptr) final { value_->SetRemote(ptr); }

  Var* GetValue() const final {
    return reinterpret_cast<Var*>(value_->GetRemote());
  }

  const void* GetDataPtr() final {
    remote_ptr_cache_ = GetValue();
    return &remote_ptr_cache_;
  }

  void SetDataFromPtr(const void* ptr, size_t max_sz) final {
    void* tmp;
    memcpy(&tmp, ptr, std::min(sizeof(tmp), max_sz));
    SetValue(reinterpret_cast<Var*>(tmp));
  }

  // Getter/Setter for the sync_type_ field.
  SyncType GetSyncType() { return sync_type_; }
  void SetSyncType(SyncType sync_type) { sync_type_ = sync_type; }

  std::string ToString() const final {
    Var* var = GetPointedVar();
    return absl::StrFormat(
        "Ptr to obj:%p (type:'%s' val:'%s'), local:%p, remote:%p, size:%tx",
        var, var->GetTypeString(), var->ToString(), var->GetLocal(),
        var->GetRemote(), var->GetSize());
  }

 private:
  // GetDataPtr() interface requires of us to return a pointer to the data
  // (variable) that can be copied. We cannot get pointer to pointer with
  // Var::GetRemote(), hence we cache it, and return pointer to it.
  void* remote_ptr_cache_;

  // Shall we synchronize the underlying object before/after call.
  SyncType sync_type_;
};

// Good, old nullptr
class NullPtr : public Ptr {
 public:
  NullPtr() : Ptr(&void_obj_, SyncType::kSyncNone) {}

 private:
  Reg<void*> void_obj_;
};

// Pointer, which can only point to remote memory, and is never synchronized.
class RemotePtr : public Ptr {
 public:
  explicit RemotePtr(void* remote_addr)
      : Ptr(&pointed_obj_, SyncType::kSyncNone) {
    pointed_obj_.SetRemote(remote_addr);
  }

 private:
  Reg<void*> pointed_obj_;
};

}  // namespace sapi::v

#endif  // SANDBOXED_API_VAR_PTR_H_