v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
arguments.h
Go to the documentation of this file.
1// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_EXECUTION_ARGUMENTS_H_
6#define V8_EXECUTION_ARGUMENTS_H_
7
11#include "src/objects/objects.h"
12#include "src/objects/slots.h"
13#include "src/sandbox/check.h"
16
17namespace v8 {
18namespace internal {
19
20// Arguments provides access to runtime call parameters.
21//
22// It uses the fact that the instance fields of Arguments
23// (length_, arguments_) are "overlayed" with the parameters
24// (no. of parameters, and the parameter pointer) passed so
25// that inside the C++ function, the parameters passed can
26// be accessed conveniently:
27//
28// Object Runtime_function(Arguments args) {
29// ... use args[i] here ...
30// }
31//
32// Note that length_ (whose value is in the integer range) is defined
33// as intptr_t to provide endian-neutrality on 64-bit archs.
34
35template <ArgumentsType arguments_type>
36class Arguments {
37 public:
38 // Scope to temporarily change the value of an argument.
40 public:
41 inline ChangeValueScope(Isolate* isolate, Arguments* args, int index,
42 Tagged<Object> value);
43 ~ChangeValueScope() { *location_ = (*old_value_).ptr(); }
44
45 private:
48 };
49
50 Arguments(int length, Address* arguments)
51 : length_(length), arguments_(arguments) {
53 }
54
56 return Tagged<Object>(*address_of_arg_at(index));
57 }
58
59 template <class S = Object>
60 V8_INLINE Handle<S> at(int index) const;
61
63
64 V8_INLINE int smi_value_at(int index) const;
65 V8_INLINE uint32_t positive_smi_value_at(int index) const;
66
67 V8_INLINE int tagged_index_value_at(int index) const;
68
69 V8_INLINE double number_value_at(int index) const;
70
71 V8_INLINE Handle<Object> atOrUndefined(Isolate* isolate, int index) const;
72
74 // Corruption of certain heap objects (see e.g. crbug.com/1507223) can lead
75 // to OOB arguments access, and therefore OOB stack access. This SBXCHECK
76 // defends against that.
77 // Note: "LE" is intentional: it's okay to compute the address of the
78 // first nonexistent entry.
79 SBXCHECK_LE(static_cast<uint32_t>(index), static_cast<uint32_t>(length_));
80 uintptr_t offset = index * kSystemPointerSize;
81 if (arguments_type == ArgumentsType::kJS) {
82 offset = (length_ - index - 1) * kSystemPointerSize;
83 }
84 return reinterpret_cast<Address*>(reinterpret_cast<Address>(arguments_) -
85 offset);
86 }
87
88 // Get the total number of arguments including the receiver.
89 V8_INLINE int length() const { return static_cast<int>(length_); }
90
91 private:
92 intptr_t length_;
94};
95
96template <ArgumentsType T>
97template <class S>
98Handle<S> Arguments<T>::at(int index) const {
99 Handle<Object> obj = Handle<Object>(address_of_arg_at(index));
100 return Cast<S>(obj);
101}
102
103template <ArgumentsType T>
105 Address* location = *reinterpret_cast<Address**>(address_of_arg_at(index));
106 return FullObjectSlot(location + offset);
107}
108
109#ifdef DEBUG
110#define CLOBBER_DOUBLE_REGISTERS() ClobberDoubleRegisters(1, 2, 3, 4);
111#else
112#define CLOBBER_DOUBLE_REGISTERS()
113#endif
114
115// TODO(cbruni): add global flag to check whether any tracing events have been
116// enabled.
117#ifdef V8_RUNTIME_CALL_STATS
118#define RUNTIME_ENTRY_WITH_RCS(Type, InternalType, Convert, Name) \
119 V8_NOINLINE static Type Stats_##Name(int args_length, Address* args_object, \
120 Isolate* isolate) { \
121 RCS_SCOPE(isolate, RuntimeCallCounterId::k##Name); \
122 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"), \
123 "V8.Runtime_" #Name); \
124 RuntimeArguments args(args_length, args_object); \
125 return Convert(__RT_impl_##Name(args, isolate)); \
126 }
127
128#define TEST_AND_CALL_RCS(Name) \
129 if (V8_UNLIKELY(TracingFlags::is_runtime_stats_enabled())) { \
130 return Stats_##Name(args_length, args_object, isolate); \
131 }
132
133#else // V8_RUNTIME_CALL_STATS
134#define RUNTIME_ENTRY_WITH_RCS(Type, InternalType, Convert, Name)
135#define TEST_AND_CALL_RCS(Name)
136
137#endif // V8_RUNTIME_CALL_STATS
138
139#define RUNTIME_FUNCTION_RETURNS_TYPE(Type, InternalType, Convert, Name) \
140 static V8_INLINE InternalType __RT_impl_##Name(RuntimeArguments args, \
141 Isolate* isolate); \
142 RUNTIME_ENTRY_WITH_RCS(Type, InternalType, Convert, Name) \
143 Type Name(int args_length, Address* args_object, Isolate* isolate) { \
144 DCHECK(isolate->context().is_null() || IsContext(isolate->context())); \
145 DCHECK(isolate->IsOnCentralStack()); \
146 CLOBBER_DOUBLE_REGISTERS(); \
147 TEST_AND_CALL_RCS(Name) \
148 RuntimeArguments args(args_length, args_object); \
149 return Convert(__RT_impl_##Name(args, isolate)); \
150 } \
151 \
152 static InternalType __RT_impl_##Name(RuntimeArguments args, Isolate* isolate)
153
154#ifdef DEBUG
155#define BUILTIN_CONVERT_RESULT(x) (isolate->VerifyBuiltinsResult(x)).ptr()
156#define BUILTIN_CONVERT_RESULT_PAIR(x) isolate->VerifyBuiltinsResult(x)
157#else // DEBUG
158#define BUILTIN_CONVERT_RESULT(x) (x).ptr()
159#define BUILTIN_CONVERT_RESULT_PAIR(x) (x)
160#endif // DEBUG
161
162#define RUNTIME_FUNCTION(Name) \
163 RUNTIME_FUNCTION_RETURNS_TYPE(Address, Tagged<Object>, \
164 BUILTIN_CONVERT_RESULT, Name)
165
166#define RUNTIME_FUNCTION_RETURN_PAIR(Name) \
167 RUNTIME_FUNCTION_RETURNS_TYPE(ObjectPair, ObjectPair, \
168 BUILTIN_CONVERT_RESULT_PAIR, Name)
169
170} // namespace internal
171} // namespace v8
172
173#endif // V8_EXECUTION_ARGUMENTS_H_
#define SBXCHECK_LE(lhs, rhs)
Definition check.h:67
ChangeValueScope(Isolate *isolate, Arguments *args, int index, Tagged< Object > value)
V8_INLINE int length() const
Definition arguments.h:89
V8_INLINE FullObjectSlot slot_from_address_at(int index, int offset) const
Definition arguments.h:104
V8_INLINE int smi_value_at(int index) const
Arguments(int length, Address *arguments)
Definition arguments.h:50
V8_INLINE int tagged_index_value_at(int index) const
V8_INLINE uint32_t positive_smi_value_at(int index) const
V8_INLINE double number_value_at(int index) const
V8_INLINE Handle< S > at(int index) const
V8_INLINE Handle< Object > atOrUndefined(Isolate *isolate, int index) const
V8_INLINE Address * address_of_arg_at(int index) const
Definition arguments.h:73
V8_INLINE Tagged< Object > operator[](int index) const
Definition arguments.h:55
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
int32_t offset
constexpr int kSystemPointerSize
Definition globals.h:410
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define V8_INLINE
Definition v8config.h:500