forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPCTestingAPI.cpp
More file actions
2672 lines (2254 loc) · 106 KB
/
IPCTestingAPI.cpp
File metadata and controls
2672 lines (2254 loc) · 106 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "IPCTestingAPI.h"
#if ENABLE(IPC_TESTING_API)
#include "Encoder.h"
#include "FrameInfoData.h"
#include "GPUProcessConnection.h"
#include "IPCSemaphore.h"
#include "IPCStreamTesterMessages.h"
#include "JSIPCBinding.h"
#include "MessageArgumentDescriptions.h"
#include "NetworkProcessConnection.h"
#include "RemoteRenderingBackendCreationParameters.h"
#include "StreamClientConnection.h"
#include "StreamConnectionBuffer.h"
#include "WebCoreArgumentCoders.h"
#include "WebFrame.h"
#include "WebPage.h"
#include "WebProcess.h"
#include <JavaScriptCore/APICast.h>
#include <JavaScriptCore/BuiltinNames.h>
#include <JavaScriptCore/JSBigInt.h>
#include <JavaScriptCore/JSClassRef.h>
#include <JavaScriptCore/JSLock.h>
#include <JavaScriptCore/JSObject.h>
#include <JavaScriptCore/JSValueRef.h>
#include <JavaScriptCore/JavaScript.h>
#include <JavaScriptCore/OpaqueJSString.h>
#include <WebCore/DOMWrapperWorld.h>
#include <WebCore/Frame.h>
#include <WebCore/RegistrableDomain.h>
#include <WebCore/ScriptController.h>
#include <wtf/PageBlock.h>
namespace WebKit {
namespace IPCTestingAPI {
class JSIPC;
static constexpr auto processTargetNameUI = "UI"_s;
#if ENABLE(GPU_PROCESS)
static constexpr auto processTargetNameGPU = "GPU"_s;
#endif
static constexpr auto processTargetNameNetworking = "Networking"_s;
static std::optional<uint64_t> destinationIDFromArgument(JSC::JSGlobalObject*, JSValueRef, JSValueRef*);
static std::optional<uint64_t> messageIDFromArgument(JSC::JSGlobalObject*, JSValueRef, JSValueRef*);
static JSC::JSObject* jsResultFromReplyDecoder(JSC::JSGlobalObject*, IPC::MessageName, IPC::Decoder&);
static bool encodeArgument(IPC::Encoder&, JSContextRef, JSValueRef, JSValueRef* exception);
class JSIPCSemaphore : public RefCounted<JSIPCSemaphore> {
public:
static Ref<JSIPCSemaphore> create(IPC::Semaphore&& semaphore = { })
{
return adoptRef(*new JSIPCSemaphore(WTFMove(semaphore)));
}
JSObjectRef createJSWrapper(JSContextRef);
static JSIPCSemaphore* toWrapped(JSContextRef, JSValueRef);
void encode(IPC::Encoder& encoder) const { m_semaphore.encode(encoder); }
IPC::Semaphore exchange(IPC::Semaphore&& semaphore = { })
{
return std::exchange(m_semaphore, WTFMove(semaphore));
}
private:
JSIPCSemaphore(IPC::Semaphore&& semaphore)
: m_semaphore(WTFMove(semaphore))
{ }
static JSClassRef wrapperClass();
static JSIPCSemaphore* unwrap(JSObjectRef);
static void initialize(JSContextRef, JSObjectRef);
static void finalize(JSObjectRef);
static const JSStaticFunction* staticFunctions();
static JSValueRef signal(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef waitFor(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
IPC::Semaphore m_semaphore;
};
class JSIPCAttachment : public RefCounted<JSIPCAttachment> {
public:
static Ref<JSIPCAttachment> create(IPC::Attachment&& attachment)
{
return adoptRef(*new JSIPCAttachment(WTFMove(attachment)));
}
JSObjectRef createJSWrapper(JSContextRef);
static JSIPCAttachment* toWrapped(JSContextRef, JSValueRef);
void encode(IPC::Encoder& encoder) const { m_attachment.encode(encoder); }
private:
JSIPCAttachment(IPC::Attachment&& attachment)
: m_attachment(WTFMove(attachment))
{ }
static JSClassRef wrapperClass();
static JSIPCAttachment* unwrap(JSObjectRef);
static void initialize(JSContextRef, JSObjectRef);
static void finalize(JSObjectRef);
static const JSStaticFunction* staticFunctions();
IPC::Attachment m_attachment;
};
class JSIPCConnection : public RefCounted<JSIPCConnection>, private IPC::Connection::Client {
public:
static Ref<JSIPCConnection> create(IPC::Connection::Identifier&& testedConnectionIdentifier)
{
return adoptRef(*new JSIPCConnection(WTFMove(testedConnectionIdentifier)));
}
JSObjectRef createJSWrapper(JSContextRef);
static JSIPCConnection* toWrapped(JSContextRef, JSValueRef);
private:
JSIPCConnection(IPC::Connection::Identifier&& testedConnectionIdentifier)
: m_testedConnection { IPC::Connection::createServerConnection(testedConnectionIdentifier, *this) }
{
}
// IPC::Connection::Client overrides.
void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
bool didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, UniqueRef<IPC::Encoder>&) final;
void didClose(IPC::Connection&) final;
void didReceiveInvalidMessage(IPC::Connection&, IPC::MessageName) final;
static JSClassRef wrapperClass();
static JSIPCConnection* unwrap(JSObjectRef);
static void initialize(JSContextRef, JSObjectRef);
static void finalize(JSObjectRef);
static const JSStaticFunction* staticFunctions();
static JSValueRef open(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef invalidate(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef sendMessage(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef sendSyncMessage(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef waitForMessage(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
Ref<IPC::Connection> m_testedConnection;
};
class JSIPCStreamClientConnection : public RefCounted<JSIPCStreamClientConnection>, public CanMakeWeakPtr<JSIPCStreamClientConnection> {
public:
static Ref<JSIPCStreamClientConnection> create(JSIPC& jsIPC, IPC::Connection& connection, size_t bufferSize)
{
return adoptRef(*new JSIPCStreamClientConnection(jsIPC, connection, bufferSize));
}
JSObjectRef createJSWrapper(JSContextRef);
static JSIPCStreamClientConnection* toWrapped(JSContextRef, JSValueRef);
IPC::StreamClientConnection& connection() { return m_streamConnection; }
private:
friend class JSIPCStreamConnectionBuffer;
JSIPCStreamClientConnection(JSIPC& jsIPC, IPC::Connection& connection, size_t bufferSize)
: m_jsIPC(jsIPC)
, m_streamConnection { connection, bufferSize }
{ }
void setSemaphores(JSIPCSemaphore& jsWakeUpSemaphore, JSIPCSemaphore& jsClientWaitSemaphore) { m_streamConnection.setSemaphores(jsWakeUpSemaphore.exchange(), jsClientWaitSemaphore.exchange()); }
static JSClassRef wrapperClass();
static JSIPCStreamClientConnection* unwrap(JSObjectRef);
static void initialize(JSContextRef, JSObjectRef);
static void finalize(JSObjectRef);
static bool prepareToSendOutOfStreamMessage(JSContextRef, size_t argumentCount, const JSValueRef arguments[], JSIPC&, IPC::StreamClientConnection&, IPC::Encoder&, uint64_t destinationID, IPC::Timeout, JSValueRef* exception);
static const JSStaticFunction* staticFunctions();
static JSValueRef streamBuffer(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef setSemaphores(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef sendMessage(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef sendSyncMessage(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef sendIPCStreamTesterSyncCrashOnZero(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
WeakPtr<JSIPC> m_jsIPC;
IPC::StreamClientConnection m_streamConnection;
};
class JSIPCStreamConnectionBuffer : public RefCounted<JSIPCStreamConnectionBuffer> {
public:
static Ref<JSIPCStreamConnectionBuffer> create(JSIPCStreamClientConnection& streamConnection)
{
return adoptRef(*new JSIPCStreamConnectionBuffer(streamConnection));
}
JSObjectRef createJSWrapper(JSContextRef);
static JSIPCStreamConnectionBuffer* toWrapped(JSContextRef, JSValueRef);
void encode(IPC::Encoder& encoder) const { m_streamConnection->connection().streamBuffer().encode(encoder); }
private:
JSIPCStreamConnectionBuffer(JSIPCStreamClientConnection& streamConnection)
: m_streamConnection(streamConnection)
{ }
static JSClassRef wrapperClass();
static JSIPCStreamConnectionBuffer* unwrap(JSObjectRef);
static void initialize(JSContextRef, JSObjectRef);
static void finalize(JSObjectRef);
static const JSStaticFunction* staticFunctions();
static JSValueRef readHeaderBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef readDataBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef readBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception, Span<uint8_t>);
static JSValueRef writeHeaderBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef writeDataBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef writeBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception, Span<uint8_t>);
WeakPtr<JSIPCStreamClientConnection> m_streamConnection;
};
class JSSharedMemory : public RefCounted<JSSharedMemory> {
public:
static Ref<JSSharedMemory> create(size_t size)
{
return adoptRef(*new JSSharedMemory(size));
}
static Ref<JSSharedMemory> create(Ref<SharedMemory>&& sharedMemory)
{
return adoptRef(*new JSSharedMemory(WTFMove(sharedMemory)));
}
size_t size() const { return m_sharedMemory->size(); }
SharedMemory::Handle createHandle(SharedMemory::Protection);
JSObjectRef createJSWrapper(JSContextRef);
static JSSharedMemory* toWrapped(JSContextRef, JSValueRef);
private:
JSSharedMemory(size_t size)
: m_sharedMemory(*SharedMemory::allocate(size))
{ }
JSSharedMemory(Ref<SharedMemory>&& sharedMemory)
: m_sharedMemory(WTFMove(sharedMemory))
{
}
static JSClassRef wrapperClass();
static JSSharedMemory* unwrap(JSObjectRef);
static void initialize(JSContextRef, JSObjectRef);
static void finalize(JSObjectRef);
static const JSStaticFunction* staticFunctions();
static JSValueRef readBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef writeBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
Ref<SharedMemory> m_sharedMemory;
};
class JSMessageListener final : public IPC::Connection::MessageObserver {
WTF_MAKE_FAST_ALLOCATED;
public:
enum class Type { Incoming, Outgoing };
JSMessageListener(JSIPC&, Type, JSContextRef, JSObjectRef callback);
private:
void willSendMessage(const IPC::Encoder&, OptionSet<IPC::SendOption>) override;
void didReceiveMessage(const IPC::Decoder&) override;
JSC::JSObject* jsDescriptionFromDecoder(JSC::JSGlobalObject*, IPC::Decoder&);
WeakPtr<JSIPC> m_jsIPC;
Type m_type;
JSContextRef m_context;
JSObjectRef m_callback;
};
class JSIPC : public RefCounted<JSIPC>, public CanMakeWeakPtr<JSIPC> {
public:
static Ref<JSIPC> create(WebPage& webPage, WebFrame& webFrame)
{
return adoptRef(*new JSIPC(webPage, webFrame));
}
static JSIPC* toWrapped(JSContextRef, JSValueRef);
static JSClassRef wrapperClass();
WebFrame* webFrame() { return m_webFrame.get(); }
private:
JSIPC(WebPage& webPage, WebFrame& webFrame)
: m_webPage(webPage)
, m_webFrame(webFrame)
{ }
static JSIPC* unwrap(JSObjectRef);
static void initialize(JSContextRef, JSObjectRef);
static void finalize(JSObjectRef);
static const JSStaticFunction* staticFunctions();
static const JSStaticValue* staticValues();
static void addMessageListener(JSMessageListener::Type, JSContextRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef addIncomingMessageListener(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef addOutgoingMessageListener(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef sendMessage(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef sendSyncMessage(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef waitForMessage(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef createConnectionPair(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef createStreamClientConnection(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef createSemaphore(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef createSharedMemory(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
static JSValueRef vmPageSize(JSContextRef, JSObjectRef, JSStringRef, JSValueRef* exception);
static JSValueRef visitedLinkStoreID(JSContextRef, JSObjectRef, JSStringRef, JSValueRef* exception);
static JSValueRef webPageProxyID(JSContextRef, JSObjectRef, JSStringRef, JSValueRef* exception);
static JSValueRef sessionID(JSContextRef, JSObjectRef, JSStringRef, JSValueRef* exception);
static JSValueRef pageID(JSContextRef, JSObjectRef, JSStringRef, JSValueRef* exception);
static JSValueRef frameID(JSContextRef, JSObjectRef, JSStringRef, JSValueRef* exception);
static JSValueRef retrieveID(JSContextRef, JSObjectRef thisObject, JSValueRef* exception, const WTF::Function<uint64_t(JSIPC&)>&);
static JSValueRef messages(JSContextRef, JSObjectRef, JSStringRef, JSValueRef* exception);
static JSValueRef processTargets(JSContextRef, JSObjectRef, JSStringRef, JSValueRef* exception);
WeakPtr<WebPage> m_webPage;
WeakPtr<WebFrame> m_webFrame;
Vector<UniqueRef<JSMessageListener>> m_messageListeners;
};
static JSValueRef createTypeError(JSContextRef context, const String& message)
{
JSC::JSLockHolder lock(toJS(context)->vm());
return toRef(JSC::createTypeError(toJS(context), message));
}
static std::optional<uint64_t> convertToUint64(JSC::JSValue jsValue)
{
if (jsValue.isNumber()) {
double value = jsValue.asNumber();
if (value < 0 || trunc(value) != value)
return std::nullopt;
return value;
}
if (jsValue.isBigInt())
return JSC::JSBigInt::toBigUInt64(jsValue);
return std::nullopt;
}
static JSValueRef sendMessageWithJSArguments(IPC::Connection& connection, JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
auto* globalObject = toJS(context);
JSC::JSLockHolder lock(globalObject->vm());
auto destinationID = destinationIDFromArgument(globalObject, arguments[0], exception);
if (!destinationID)
return JSValueMakeUndefined(context);
auto messageID = messageIDFromArgument(globalObject, arguments[1], exception);
if (!messageID)
return JSValueMakeUndefined(context);
auto messageName = static_cast<IPC::MessageName>(*messageID);
auto encoder = makeUniqueRef<IPC::Encoder>(messageName, *destinationID);
JSValueRef returnValue = JSValueMakeUndefined(context);
bool hasReply = !!messageReplyArgumentDescriptions(messageName);
if (hasReply) {
uint64_t listenerID = IPC::nextAsyncReplyHandlerID();
encoder.get() << listenerID;
JSObjectRef resolve;
JSObjectRef reject;
ALLOW_NEW_API_WITHOUT_GUARDS_BEGIN
returnValue = JSObjectMakeDeferredPromise(context, &resolve, &reject, exception); // NOLINT
ALLOW_NEW_API_WITHOUT_GUARDS_END
if (!returnValue) // NOLINT
return JSValueMakeUndefined(context);
JSGlobalContextRetain(JSContextGetGlobalContext(context));
JSValueProtect(context, resolve);
JSValueProtect(context, reject);
IPC::addAsyncReplyHandler(connection, listenerID, [messageName, context, resolve, reject](IPC::Decoder* replyDecoder) {
auto* globalObject = toJS(context);
auto& vm = globalObject->vm();
JSC::JSLockHolder lock(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
auto* jsResult = jsResultFromReplyDecoder(globalObject, messageName, *replyDecoder);
if (auto* exception = scope.exception()) {
scope.clearException();
JSValueRef arguments[] = { toRef(globalObject, exception) };
JSObjectCallAsFunction(context, reject, reject, 1, arguments, nullptr);
} else {
JSValueRef arguments[] = { toRef(globalObject, jsResult) };
JSObjectCallAsFunction(context, resolve, resolve, 1, arguments, nullptr);
}
JSValueUnprotect(context, reject);
JSValueUnprotect(context, resolve);
JSGlobalContextRelease(JSContextGetGlobalContext(context));
});
}
if (argumentCount > 2) {
if (!encodeArgument(encoder.get(), context, arguments[2], exception))
return JSValueMakeUndefined(context);
}
// FIXME: Add the support for specifying IPC options.
connection.sendMessage(WTFMove(encoder), { });
return returnValue;
}
namespace {
struct SyncIPCMessageInfo {
uint64_t destinationID;
IPC::MessageName messageName;
IPC::Timeout timeout;
};
}
static std::optional<SyncIPCMessageInfo> extractSyncIPCMessageInfo(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
ASSERT(argumentCount >= 2);
auto* globalObject = toJS(context);
auto destinationID = destinationIDFromArgument(globalObject, arguments[0], exception);
if (!destinationID)
return std::nullopt;
auto messageID = messageIDFromArgument(globalObject, arguments[1], exception);
if (!messageID)
return std::nullopt;
Seconds timeoutDuration;
{
auto jsValue = toJS(globalObject, arguments[2]);
if (!jsValue.isNumber()) {
*exception = createTypeError(context, "Timeout must be a number"_s);
return std::nullopt;
}
timeoutDuration = Seconds { jsValue.asNumber() };
}
return { { *destinationID, static_cast<IPC::MessageName>(*messageID), { timeoutDuration } } };
}
static JSValueRef sendSyncMessageWithJSArguments(IPC::Connection& connection, JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
auto* globalObject = toJS(context);
JSC::JSLockHolder lock(globalObject->vm());
auto info = extractSyncIPCMessageInfo(context, argumentCount, arguments, exception);
if (!info)
return JSValueMakeUndefined(context);
auto [destinationID, messageName, timeout] = *info;
// FIXME: Support the options.
IPC::Connection::SyncRequestID syncRequestID;
auto encoder = connection.createSyncMessageEncoder(messageName, destinationID, syncRequestID);
if (argumentCount > 3) {
if (!encodeArgument(encoder.get(), context, arguments[3], exception))
return JSValueMakeUndefined(context);
}
if (auto replyDecoder = connection.sendSyncMessage(syncRequestID, WTFMove(encoder), timeout, { })) {
auto scope = DECLARE_CATCH_SCOPE(globalObject->vm());
auto* jsResult = jsResultFromReplyDecoder(globalObject, messageName, *replyDecoder);
if (scope.exception()) {
*exception = toRef(globalObject, scope.exception());
scope.clearException();
return JSValueMakeUndefined(context);
}
return toRef(globalObject, jsResult);
}
return JSValueMakeUndefined(context);
}
static JSValueRef waitForMessageWithJSArguments(IPC::Connection& connection, JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
auto* globalObject = toJS(context);
JSC::JSLockHolder lock(globalObject->vm());
auto info = extractSyncIPCMessageInfo(context, argumentCount, arguments, exception);
if (!info)
return JSValueMakeUndefined(context);
auto [destinationID, messageName, timeout] = *info;
if (auto decoder = connection.waitForMessageForTesting(messageName, destinationID, timeout, { })) {
auto scope = DECLARE_CATCH_SCOPE(globalObject->vm());
auto jsResult = jsValueForArguments(globalObject, messageName, *decoder);
if (scope.exception()) {
*exception = toRef(globalObject, scope.exception());
scope.clearException();
return JSValueMakeUndefined(context);
}
return jsResult ? toRef(globalObject, *jsResult) : JSValueMakeUndefined(context);
}
return JSValueMakeUndefined(context);
}
JSObjectRef JSIPCSemaphore::createJSWrapper(JSContextRef context)
{
auto* globalObject = toJS(context);
auto& vm = globalObject->vm();
JSC::JSLockHolder lock(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObjectRef wrapperObject = JSObjectMake(toGlobalRef(globalObject), wrapperClass(), this);
scope.clearException();
return wrapperObject;
}
JSClassRef JSIPCSemaphore::wrapperClass()
{
static JSClassRef jsClass;
if (!jsClass) {
JSClassDefinition definition = kJSClassDefinitionEmpty;
definition.className = "Semaphore";
definition.parentClass = nullptr;
definition.staticValues = nullptr;
definition.staticFunctions = staticFunctions();
definition.initialize = initialize;
definition.finalize = finalize;
jsClass = JSClassCreate(&definition);
}
return jsClass;
}
inline JSIPCSemaphore* JSIPCSemaphore::unwrap(JSObjectRef object)
{
return static_cast<JSIPCSemaphore*>(JSObjectGetPrivate(object));
}
JSIPCSemaphore* JSIPCSemaphore::toWrapped(JSContextRef context, JSValueRef value)
{
if (!context || !value || !JSValueIsObjectOfClass(context, value, wrapperClass()))
return nullptr;
return unwrap(JSValueToObject(context, value, nullptr));
}
void JSIPCSemaphore::initialize(JSContextRef, JSObjectRef object)
{
unwrap(object)->ref();
}
void JSIPCSemaphore::finalize(JSObjectRef object)
{
unwrap(object)->deref();
}
const JSStaticFunction* JSIPCSemaphore::staticFunctions()
{
static const JSStaticFunction functions[] = {
{ "signal", signal, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "waitFor", waitFor, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ 0, 0, 0 }
};
return functions;
}
JSObjectRef JSIPCAttachment::createJSWrapper(JSContextRef context)
{
auto* globalObject = toJS(context);
auto& vm = globalObject->vm();
JSC::JSLockHolder lock(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObjectRef wrapperObject = JSObjectMake(toGlobalRef(globalObject), wrapperClass(), this);
scope.clearException();
return wrapperObject;
}
JSClassRef JSIPCAttachment::wrapperClass()
{
static JSClassRef jsClass;
if (!jsClass) {
JSClassDefinition definition = kJSClassDefinitionEmpty;
definition.className = "Attachment";
definition.parentClass = nullptr;
definition.staticValues = nullptr;
definition.staticFunctions = staticFunctions();
definition.initialize = initialize;
definition.finalize = finalize;
jsClass = JSClassCreate(&definition);
}
return jsClass;
}
inline JSIPCAttachment* JSIPCAttachment::unwrap(JSObjectRef object)
{
return static_cast<JSIPCAttachment*>(JSObjectGetPrivate(object));
}
JSIPCAttachment* JSIPCAttachment::toWrapped(JSContextRef context, JSValueRef value)
{
if (!context || !value || !JSValueIsObjectOfClass(context, value, wrapperClass()))
return nullptr;
return unwrap(JSValueToObject(context, value, nullptr));
}
void JSIPCAttachment::initialize(JSContextRef, JSObjectRef object)
{
unwrap(object)->ref();
}
void JSIPCAttachment::finalize(JSObjectRef object)
{
unwrap(object)->deref();
}
const JSStaticFunction* JSIPCAttachment::staticFunctions()
{
static const JSStaticFunction functions[] = {
{ 0, 0, 0 }
};
return functions;
}
JSObjectRef JSIPCConnection::createJSWrapper(JSContextRef context)
{
auto* globalObject = toJS(context);
auto& vm = globalObject->vm();
JSC::JSLockHolder lock(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObjectRef wrapperObject = JSObjectMake(toGlobalRef(globalObject), wrapperClass(), this);
scope.clearException();
return wrapperObject;
}
JSClassRef JSIPCConnection::wrapperClass()
{
static JSClassRef jsClass;
if (!jsClass) {
JSClassDefinition definition = kJSClassDefinitionEmpty;
definition.className = "Connection";
definition.parentClass = nullptr;
definition.staticValues = nullptr;
definition.staticFunctions = staticFunctions();
definition.initialize = initialize;
definition.finalize = finalize;
jsClass = JSClassCreate(&definition);
}
return jsClass;
}
inline JSIPCConnection* JSIPCConnection::unwrap(JSObjectRef object)
{
return static_cast<JSIPCConnection*>(JSObjectGetPrivate(object));
}
JSIPCConnection* JSIPCConnection::toWrapped(JSContextRef context, JSValueRef value)
{
if (!context || !value || !JSValueIsObjectOfClass(context, value, wrapperClass()))
return nullptr;
return unwrap(JSValueToObject(context, value, nullptr));
}
void JSIPCConnection::initialize(JSContextRef, JSObjectRef object)
{
unwrap(object)->ref();
}
void JSIPCConnection::finalize(JSObjectRef object)
{
unwrap(object)->deref();
}
void JSIPCConnection::didReceiveMessage(IPC::Connection&, IPC::Decoder&)
{
ASSERT_NOT_REACHED();
}
bool JSIPCConnection::didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, UniqueRef<IPC::Encoder>&)
{
ASSERT_NOT_REACHED();
return false;
}
void JSIPCConnection::didClose(IPC::Connection&)
{
}
void JSIPCConnection::didReceiveInvalidMessage(IPC::Connection&, IPC::MessageName)
{
ASSERT_NOT_REACHED();
}
const JSStaticFunction* JSIPCConnection::staticFunctions()
{
static const JSStaticFunction functions[] = {
{ "open", open, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "invalidate", invalidate, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "sendMessage", sendMessage, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "sendSyncMessage", sendSyncMessage, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "waitForMessage", waitForMessage, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ 0, 0, 0 }
};
return functions;
}
JSValueRef JSIPCConnection::open(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef* exception)
{
RefPtr self = toWrapped(context, thisObject);
if (!self) {
*exception = createTypeError(context, "Wrong type"_s);
return JSValueMakeUndefined(context);
}
self->m_testedConnection->open();
return JSValueMakeUndefined(context);
}
JSValueRef JSIPCConnection::invalidate(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef* exception)
{
RefPtr self = toWrapped(context, thisObject);
if (!self) {
*exception = createTypeError(context, "Wrong type"_s);
return JSValueMakeUndefined(context);
}
self->m_testedConnection->invalidate();
return JSValueMakeUndefined(context);
}
JSValueRef JSIPCConnection::sendMessage(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
RefPtr self = toWrapped(context, thisObject);
if (!self) {
*exception = createTypeError(context, "Wrong type"_s);
return JSValueMakeUndefined(context);
}
if (argumentCount < 2) {
*exception = createTypeError(context, "Must specify the destination ID and message ID as the first two arguments"_s);
return JSValueMakeUndefined(context);
}
return sendMessageWithJSArguments(self->m_testedConnection, context, argumentCount, arguments, exception);
}
JSValueRef JSIPCConnection::sendSyncMessage(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
RefPtr self = toWrapped(context, thisObject);
if (!self) {
*exception = createTypeError(context, "Wrong type"_s);
return JSValueMakeUndefined(context);
}
if (argumentCount < 2) {
*exception = createTypeError(context, "Must specify the destination ID and message ID as the first two arguments"_s);
return JSValueMakeUndefined(context);
}
return sendSyncMessageWithJSArguments(self->m_testedConnection, context, argumentCount, arguments, exception);
}
JSValueRef JSIPCConnection::waitForMessage(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
RefPtr self = toWrapped(context, thisObject);
if (!self) {
*exception = createTypeError(context, "Wrong type"_s);
return JSValueMakeUndefined(context);
}
if (argumentCount < 2) {
*exception = createTypeError(context, "Must specify the destination ID and message ID as the first two arguments"_s);
return JSValueMakeUndefined(context);
}
return waitForMessageWithJSArguments(self->m_testedConnection, context, argumentCount, arguments, exception);
}
JSObjectRef JSIPCStreamClientConnection::createJSWrapper(JSContextRef context)
{
auto* globalObject = toJS(context);
auto& vm = globalObject->vm();
JSC::JSLockHolder lock(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
JSObjectRef wrapperObject = JSObjectMake(toGlobalRef(globalObject), wrapperClass(), this);
scope.clearException();
return wrapperObject;
}
JSClassRef JSIPCStreamClientConnection::wrapperClass()
{
static JSClassRef jsClass;
if (!jsClass) {
JSClassDefinition definition = kJSClassDefinitionEmpty;
definition.className = "StreamClientConnection";
definition.parentClass = nullptr;
definition.staticValues = nullptr;
definition.staticFunctions = staticFunctions();
definition.initialize = initialize;
definition.finalize = finalize;
jsClass = JSClassCreate(&definition);
}
return jsClass;
}
inline JSIPCStreamClientConnection* JSIPCStreamClientConnection::unwrap(JSObjectRef object)
{
return static_cast<JSIPCStreamClientConnection*>(JSObjectGetPrivate(object));
}
JSIPCStreamClientConnection* JSIPCStreamClientConnection::toWrapped(JSContextRef context, JSValueRef value)
{
if (!context || !value || !JSValueIsObjectOfClass(context, value, wrapperClass()))
return nullptr;
return unwrap(JSValueToObject(context, value, nullptr));
}
void JSIPCStreamClientConnection::initialize(JSContextRef, JSObjectRef object)
{
unwrap(object)->ref();
}
void JSIPCStreamClientConnection::finalize(JSObjectRef object)
{
unwrap(object)->deref();
}
const JSStaticFunction* JSIPCStreamClientConnection::staticFunctions()
{
static const JSStaticFunction functions[] = {
{ "streamBuffer", streamBuffer, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "setSemaphores", setSemaphores, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "sendMessage", sendMessage, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "sendSyncMessage", sendSyncMessage, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ "sendIPCStreamTesterSyncCrashOnZero", sendIPCStreamTesterSyncCrashOnZero, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
{ 0, 0, 0 }
};
return functions;
}
JSValueRef JSIPCStreamClientConnection::streamBuffer(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
auto* globalObject = toJS(context);
JSC::JSLockHolder lock(globalObject->vm());
RefPtr jsStreamConnection = toWrapped(context, thisObject);
if (!jsStreamConnection) {
*exception = createTypeError(context, "Wrong type"_s);
return JSValueMakeUndefined(context);
}
return JSIPCStreamConnectionBuffer::create(*jsStreamConnection)->createJSWrapper(context);
}
JSValueRef JSIPCStreamClientConnection::setSemaphores(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
auto* globalObject = toJS(context);
JSC::JSLockHolder lock(globalObject->vm());
RefPtr jsStreamConnection = toWrapped(context, thisObject);
if (!jsStreamConnection) {
*exception = createTypeError(context, "Wrong type"_s);
return JSValueMakeUndefined(context);
}
if (argumentCount < 2) {
*exception = createTypeError(context, "Must specify an IPC semaphore as the first and second argument"_s);
return JSValueMakeUndefined(context);
}
RefPtr jsWakeUpSemaphore = JSIPCSemaphore::toWrapped(context, arguments[0]);
if (!jsWakeUpSemaphore) {
*exception = createTypeError(context, "Wrong type (expected Semaphore)"_s);
return JSValueMakeUndefined(context);
}
RefPtr jsClientWaitSemaphore = JSIPCSemaphore::toWrapped(context, arguments[1]);
if (!jsClientWaitSemaphore) {
*exception = createTypeError(context, "Wrong type (expected Semaphore)"_s);
return JSValueMakeUndefined(context);
}
jsStreamConnection->setSemaphores(*jsWakeUpSemaphore, *jsClientWaitSemaphore);
return JSValueMakeUndefined(context);
}
struct IPCStreamMessageInfo {
uint64_t destinationID;
IPC::MessageName messageName;
IPC::Timeout timeout;
};
static std::optional<IPCStreamMessageInfo> extractIPCStreamMessageInfo(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
if (argumentCount < 3) {
*exception = createTypeError(context, "Must specify destination ID, message ID, and timeout as the first three arguments"_s);
return std::nullopt;
}
auto* globalObject = toJS(context);
auto destinationID = destinationIDFromArgument(globalObject, arguments[0], exception);
if (!destinationID)
return std::nullopt;
auto messageID = messageIDFromArgument(globalObject, arguments[1], exception);
if (!messageID)
return std::nullopt;
Seconds timeoutDuration;
{
auto jsValue = toJS(globalObject, arguments[2]);
if (!jsValue.isNumber()) {
*exception = createTypeError(context, "Timeout must be a number"_s);
return std::nullopt;
}
timeoutDuration = Seconds { jsValue.asNumber() };
}
return { { *destinationID, static_cast<IPC::MessageName>(*messageID), { timeoutDuration } } };
}
bool JSIPCStreamClientConnection::prepareToSendOutOfStreamMessage(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSIPC& jsIPC, IPC::StreamClientConnection& streamConnection, IPC::Encoder& encoder, uint64_t destinationID, IPC::Timeout timeout, JSValueRef* exception)
{
// FIXME: Add support for sending in-stream IPC messages when appropriate.
if (argumentCount > 3) {
if (!encodeArgument(encoder, context, arguments[3], exception))
return false;
}
if (!streamConnection.trySendDestinationIDIfNeeded(destinationID, timeout))
return false;
auto span = streamConnection.tryAcquire(timeout);
if (!span)
return false;
streamConnection.sendProcessOutOfStreamMessage(WTFMove(*span));
return true;
}
JSValueRef JSIPCStreamClientConnection::sendMessage(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
auto* globalObject = toJS(context);
JSC::JSLockHolder lock(globalObject->vm());
auto returnValue = JSValueMakeUndefined(context);
RefPtr jsStreamConnection = toWrapped(context, thisObject);
if (!jsStreamConnection) {
*exception = createTypeError(context, "Wrong type"_s);
return returnValue;
}
auto info = extractIPCStreamMessageInfo(context, argumentCount, arguments, exception);
if (!info)
return returnValue;
auto [destinationID, messageName, timeout] = *info;
auto& streamConnection = jsStreamConnection->connection();
auto& connection = streamConnection.connectionForTesting();
auto encoder = makeUniqueRef<IPC::Encoder>(messageName, destinationID);
if (prepareToSendOutOfStreamMessage(context, argumentCount, arguments, *jsStreamConnection->m_jsIPC, streamConnection, encoder.get(), destinationID, timeout, exception))
connection.sendMessage(WTFMove(encoder), { });
return returnValue;
}
JSValueRef JSIPCStreamClientConnection::sendSyncMessage(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
auto* globalObject = toJS(context);
JSC::JSLockHolder lock(globalObject->vm());
RefPtr jsStreamConnection = toWrapped(context, thisObject);
if (!jsStreamConnection) {
*exception = createTypeError(context, "Wrong type"_s);