Skip to content

Commit 13779de

Browse files
rmacnak-googleCommit Queue
authored andcommitted
[vm] Give the native call wrappers C linkage.
This avoids undefined behavior. Cf. 18aa7d3 TEST=ci Change-Id: I2dd82517177ee812b3cd3ae4b1c748de129ec665 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/432720 Reviewed-by: Alexander Aprelev <[email protected]> Commit-Queue: Ryan Macnak <[email protected]>
1 parent 5c1f5ca commit 13779de

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

runtime/vm/native_entry.cc

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,14 @@ void NativeEntry::MaybePropagateError(NativeArguments* arguments) {
111111
}
112112
}
113113

114+
extern "C" void DRT_BootstrapNativeCall(Dart_NativeArguments args,
115+
Dart_NativeFunction func) {
116+
CHECK_STACK_ALIGNMENT;
117+
NativeEntry::BootstrapNativeCallWrapper(args, func);
118+
}
119+
114120
uword NativeEntry::BootstrapNativeCallWrapperEntry() {
115-
uword entry =
116-
reinterpret_cast<uword>(NativeEntry::BootstrapNativeCallWrapper);
121+
uword entry = reinterpret_cast<uword>(DRT_BootstrapNativeCall);
117122
#if defined(USING_SIMULATOR)
118123
entry = Simulator::RedirectExternalReference(
119124
entry, Simulator::kNativeCallWrapper,
@@ -124,7 +129,6 @@ uword NativeEntry::BootstrapNativeCallWrapperEntry() {
124129

125130
void NativeEntry::BootstrapNativeCallWrapper(Dart_NativeArguments args,
126131
Dart_NativeFunction func) {
127-
CHECK_STACK_ALIGNMENT;
128132
if (func == LinkNativeCall) {
129133
func(args);
130134
return;
@@ -151,8 +155,14 @@ void NativeEntry::BootstrapNativeCallWrapper(Dart_NativeArguments args,
151155
}
152156
}
153157

158+
extern "C" void DRT_NoScopeNativeCall(Dart_NativeArguments args,
159+
Dart_NativeFunction func) {
160+
CHECK_STACK_ALIGNMENT;
161+
NativeEntry::NoScopeNativeCallWrapper(args, func);
162+
}
163+
154164
uword NativeEntry::NoScopeNativeCallWrapperEntry() {
155-
uword entry = reinterpret_cast<uword>(NativeEntry::NoScopeNativeCallWrapper);
165+
uword entry = reinterpret_cast<uword>(DRT_NoScopeNativeCall);
156166
#if defined(USING_SIMULATOR)
157167
entry = Simulator::RedirectExternalReference(
158168
entry, Simulator::kNativeCallWrapper,
@@ -163,13 +173,6 @@ uword NativeEntry::NoScopeNativeCallWrapperEntry() {
163173

164174
void NativeEntry::NoScopeNativeCallWrapper(Dart_NativeArguments args,
165175
Dart_NativeFunction func) {
166-
CHECK_STACK_ALIGNMENT;
167-
NoScopeNativeCallWrapperNoStackCheck(args, func);
168-
}
169-
170-
void NativeEntry::NoScopeNativeCallWrapperNoStackCheck(
171-
Dart_NativeArguments args,
172-
Dart_NativeFunction func) {
173176
NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
174177
// Tell MemorySanitizer 'arguments' is initialized by generated code.
175178
MSAN_UNPOISON(arguments, sizeof(*arguments));
@@ -183,9 +186,14 @@ void NativeEntry::NoScopeNativeCallWrapperNoStackCheck(
183186
ASSERT(thread->execution_state() == Thread::kThreadInGenerated);
184187
}
185188

189+
extern "C" void DRT_AutoScopeNativeCall(Dart_NativeArguments args,
190+
Dart_NativeFunction func) {
191+
CHECK_STACK_ALIGNMENT;
192+
NativeEntry::AutoScopeNativeCallWrapper(args, func);
193+
}
194+
186195
uword NativeEntry::AutoScopeNativeCallWrapperEntry() {
187-
uword entry =
188-
reinterpret_cast<uword>(NativeEntry::AutoScopeNativeCallWrapper);
196+
uword entry = reinterpret_cast<uword>(DRT_AutoScopeNativeCall);
189197
#if defined(USING_SIMULATOR)
190198
entry = Simulator::RedirectExternalReference(
191199
entry, Simulator::kNativeCallWrapper,
@@ -196,13 +204,6 @@ uword NativeEntry::AutoScopeNativeCallWrapperEntry() {
196204

197205
void NativeEntry::AutoScopeNativeCallWrapper(Dart_NativeArguments args,
198206
Dart_NativeFunction func) {
199-
CHECK_STACK_ALIGNMENT;
200-
AutoScopeNativeCallWrapperNoStackCheck(args, func);
201-
}
202-
203-
void NativeEntry::AutoScopeNativeCallWrapperNoStackCheck(
204-
Dart_NativeArguments args,
205-
Dart_NativeFunction func) {
206207
NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args);
207208
// Tell MemorySanitizer 'arguments' is initialized by generated code.
208209
MSAN_UNPOISON(arguments, sizeof(*arguments));
@@ -249,8 +250,9 @@ static NativeFunction ResolveNativeFunction(Zone* zone,
249250
}
250251

251252
uword NativeEntry::LinkNativeCallEntry() {
252-
uword entry = reinterpret_cast<uword>(NativeEntry::LinkNativeCall);
253-
return entry;
253+
// This one does not need a simulator redirect because it is always called
254+
// through BootstrapNativeCallWrapper, not directly from generated code.
255+
return reinterpret_cast<uword>(NativeEntry::LinkNativeCall);
254256
}
255257

256258
void NativeEntry::LinkNativeCall(Dart_NativeArguments args) {
@@ -322,14 +324,10 @@ void NativeEntry::LinkNativeCall(Dart_NativeArguments args) {
322324
NativeEntry::BootstrapNativeCallWrapper(
323325
args, reinterpret_cast<Dart_NativeFunction>(target_function));
324326
} else if (is_auto_scope) {
325-
// Because this call is within a compilation unit, Clang doesn't respect
326-
// the ABI alignment here.
327-
NativeEntry::AutoScopeNativeCallWrapperNoStackCheck(
327+
NativeEntry::AutoScopeNativeCallWrapper(
328328
args, reinterpret_cast<Dart_NativeFunction>(target_function));
329329
} else {
330-
// Because this call is within a compilation unit, Clang doesn't respect
331-
// the ABI alignment here.
332-
NativeEntry::NoScopeNativeCallWrapperNoStackCheck(
330+
NativeEntry::NoScopeNativeCallWrapper(
333331
args, reinterpret_cast<Dart_NativeFunction>(target_function));
334332
}
335333
}

runtime/vm/native_entry.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ class NativeEntry : public AllStatic {
118118
static void LinkNativeCall(Dart_NativeArguments args);
119119

120120
private:
121-
static void NoScopeNativeCallWrapperNoStackCheck(Dart_NativeArguments args,
122-
Dart_NativeFunction func);
123-
static void AutoScopeNativeCallWrapperNoStackCheck(Dart_NativeArguments args,
124-
Dart_NativeFunction func);
125-
126121
static void MaybePropagateError(NativeArguments* arguments);
127122
};
128123

0 commit comments

Comments
 (0)