11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_SANDBOX_SANDBOXED_POINTER_INL_H_
61cb0ef41Sopenharmony_ci#define V8_SANDBOX_SANDBOXED_POINTER_INL_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "include/v8-internal.h"
91cb0ef41Sopenharmony_ci#include "src/common/ptr-compr.h"
101cb0ef41Sopenharmony_ci#include "src/execution/isolate.h"
111cb0ef41Sopenharmony_ci#include "src/sandbox/sandboxed-pointer.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciV8_INLINE Address ReadSandboxedPointerField(Address field_address,
171cb0ef41Sopenharmony_ci                                            PtrComprCageBase cage_base) {
181cb0ef41Sopenharmony_ci#ifdef V8_SANDBOXED_POINTERS
191cb0ef41Sopenharmony_ci  SandboxedPointer_t sandboxed_pointer =
201cb0ef41Sopenharmony_ci      base::ReadUnalignedValue<SandboxedPointer_t>(field_address);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  Address offset = sandboxed_pointer >> kSandboxedPointerShift;
231cb0ef41Sopenharmony_ci  Address pointer = cage_base.address() + offset;
241cb0ef41Sopenharmony_ci  return pointer;
251cb0ef41Sopenharmony_ci#else
261cb0ef41Sopenharmony_ci  return ReadMaybeUnalignedValue<Address>(field_address);
271cb0ef41Sopenharmony_ci#endif
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciV8_INLINE void WriteSandboxedPointerField(Address field_address,
311cb0ef41Sopenharmony_ci                                          PtrComprCageBase cage_base,
321cb0ef41Sopenharmony_ci                                          Address pointer) {
331cb0ef41Sopenharmony_ci#ifdef V8_SANDBOXED_POINTERS
341cb0ef41Sopenharmony_ci  // The pointer must point into the sandbox.
351cb0ef41Sopenharmony_ci  CHECK(GetProcessWideSandbox()->Contains(pointer));
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  Address offset = pointer - cage_base.address();
381cb0ef41Sopenharmony_ci  SandboxedPointer_t sandboxed_pointer = offset << kSandboxedPointerShift;
391cb0ef41Sopenharmony_ci  base::WriteUnalignedValue<SandboxedPointer_t>(field_address,
401cb0ef41Sopenharmony_ci                                                sandboxed_pointer);
411cb0ef41Sopenharmony_ci#else
421cb0ef41Sopenharmony_ci  WriteMaybeUnalignedValue<Address>(field_address, pointer);
431cb0ef41Sopenharmony_ci#endif
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci}  // namespace internal
471cb0ef41Sopenharmony_ci}  // namespace v8
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci#endif  // V8_SANDBOX_SANDBOXED_POINTER_INL_H_
50