Skip to content

Commit

Permalink
Generate serializers for DDScannerResult and NSValue
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=264915
rdar://118486039

Reviewed by Alex Christensen.

This patch starts the process of breaking out specific secure coding types into their own wrappers.

Starting with DDScannerResult, it optionally uses SPI on the object if it exists to grab a directly
serializable NSDictionary, or it falls back to just wrapping the object itself like any secure coding type.

That NSDictionary is known to contain NSValues that wrap an NSRange, so this patch also adds an NSValue wrapper.

Currently, any NSValues that are serialized would go through the secure coding path.
For this one wrapped type we're known to need (NSRange), we go through a directly serialized code path.

Headed down this road, the goal is to identify all specific types we need to serialize, have a direct wrapper
for each, and reduce our reliance on the secure coding code path to zero.

* Source/WebKit/DerivedSources-input.xcfilelist:
* Source/WebKit/DerivedSources.make:
* Source/WebKit/Shared/Cocoa/ArgumentCodersCocoa.h:
* Source/WebKit/Shared/Cocoa/ArgumentCodersCocoa.mm:
(IPC::getClass<DDScannerResult>):
(IPC::typeFromObject):
* Source/WebKit/Shared/Cocoa/CoreIPCDDScannerResult.h: Copied from Source/WebKit/Shared/Cocoa/CoreIPCDictionary.h.
(WebKit::CoreIPCDDScannerResult::CoreIPCDDScannerResult):
* Source/WebKit/Shared/Cocoa/CoreIPCDDScannerResult.mm: Copied from Source/WebKit/Shared/Cocoa/CoreIPCSecureCoding.mm.
(WebKit::shouldWrapDDScannerResult):
(WebKit::CoreIPCDDScannerResult::CoreIPCDDScannerResult):
(WebKit::CoreIPCDDScannerResult::toID const):
* Source/WebKit/Shared/Cocoa/CoreIPCDDScannerResult.serialization.in: Added.
* Source/WebKit/Shared/Cocoa/CoreIPCDictionary.h:
* Source/WebKit/Shared/Cocoa/CoreIPCNSCFObject.h:
* Source/WebKit/Shared/Cocoa/CoreIPCNSCFObject.mm:
(WebKit::valueFromID):
* Source/WebKit/Shared/Cocoa/CoreIPCNSValue.h: Copied from Source/WebKit/Shared/Cocoa/CoreIPCDictionary.h.
(WebKit::CoreIPCNSValue::CoreIPCNSValue):
* Source/WebKit/Shared/Cocoa/CoreIPCNSValue.mm: Copied from Source/WebKit/Shared/Cocoa/CoreIPCSecureCoding.mm.
(WebKit::CoreIPCNSValue::CoreIPCNSValue):
(WebKit::CoreIPCNSValue::toID const):
(WebKit::CoreIPCNSValue::shouldWrapValue):
* Source/WebKit/Shared/Cocoa/CoreIPCNSValue.serialization.in: Added.
* Source/WebKit/Shared/Cocoa/CoreIPCSecureCoding.h:
(WebKit::CoreIPCSecureCoding::CoreIPCSecureCoding):
* Source/WebKit/Shared/Cocoa/CoreIPCSecureCoding.mm:
(WebKit::CoreIPCSecureCoding::CoreIPCSecureCoding):
* Source/WebKit/Shared/Cocoa/WebCoreArgumentCodersCocoa.mm:
(IPC::getClass<DDScannerResult>): Deleted.
* Source/WebKit/WebKit.xcodeproj/project.pbxproj:

Since NSNumber is itself an NSValue, refactor these tests to explicitly emplace into the testing variant:
* Tools/TestWebKitAPI/Tests/IPC/IPCSerialization.mm:
(TEST):
(fakeDataDetectorResultForTesting):

Canonical link: https://commits.webkit.org/270840@main
  • Loading branch information
beidson committed Nov 16, 2023
1 parent 62d17dc commit 72c2238
Show file tree
Hide file tree
Showing 19 changed files with 524 additions and 31 deletions.
3 changes: 3 additions & 0 deletions Source/WebCore/testing/Internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
#include "InternalsAdditions.h"
#endif

#if ENABLE(DATA_DETECTION)
OBJC_CLASS DDScannerResult;
#endif

OBJC_CLASS VKCImageAnalysis;

namespace WebCore {
Expand Down
2 changes: 2 additions & 0 deletions Source/WebKit/DerivedSources-input.xcfilelist
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ $(PROJECT_DIR)/Shared/Cocoa/CacheStoragePolicy.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCArray.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCCFType.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCColor.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCDDScannerResult.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCData.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCDate.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCDictionary.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCError.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCFont.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCNSCFObject.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCNSValue.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCSecureCoding.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCString.serialization.in
$(PROJECT_DIR)/Shared/Cocoa/CoreIPCURL.serialization.in
Expand Down
2 changes: 2 additions & 0 deletions Source/WebKit/DerivedSources.make
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,14 @@ SERIALIZATION_DESCRIPTION_FILES = \
Shared/Cocoa/CoreIPCArray.serialization.in \
Shared/Cocoa/CoreIPCCFType.serialization.in \
Shared/Cocoa/CoreIPCColor.serialization.in \
Shared/Cocoa/CoreIPCDDScannerResult.serialization.in \
Shared/Cocoa/CoreIPCData.serialization.in \
Shared/Cocoa/CoreIPCDate.serialization.in \
Shared/Cocoa/CoreIPCDictionary.serialization.in \
Shared/Cocoa/CoreIPCError.serialization.in \
Shared/Cocoa/CoreIPCFont.serialization.in \
Shared/Cocoa/CoreIPCNSCFObject.serialization.in \
Shared/Cocoa/CoreIPCNSValue.serialization.in \
Shared/Cocoa/CoreIPCSecureCoding.serialization.in \
Shared/Cocoa/CoreIPCString.serialization.in \
Shared/Cocoa/CoreIPCURL.serialization.in \
Expand Down
11 changes: 11 additions & 0 deletions Source/WebKit/Shared/Cocoa/ArgumentCodersCocoa.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

#import <wtf/RetainPtr.h>

#if ENABLE(DATA_DETECTION)
OBJC_CLASS DDScannerResult;
#endif

namespace IPC {

#ifdef __OBJC__
Expand All @@ -57,6 +61,9 @@ class CoreIPCRetainPtr : public RetainPtr<T> {
enum class NSType : uint8_t {
Array,
Color,
#if ENABLE(DATA_DETECTION)
DDScannerResult,
#endif
Data,
Date,
Error,
Expand All @@ -66,12 +73,16 @@ enum class NSType : uint8_t {
SecureCoding,
String,
URL,
NSValue,
CF,
Unknown,
};
NSType typeFromObject(id);
bool isSerializableValue(id);

#if ENABLE(DATA_DETECTION)
template<> Class getClass<DDScannerResult>();
#endif

void encodeObjectWithWrapper(Encoder&, id);
std::optional<RetainPtr<id>> decodeObjectFromWrapper(Decoder&, const HashSet<Class>& allowedClasses);
Expand Down
13 changes: 13 additions & 0 deletions Source/WebKit/Shared/Cocoa/ArgumentCodersCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ - (instancetype)initWithCGColor:(CGColorRef)color

#pragma mark - Helpers

#if ENABLE(DATA_DETECTION)
template<> Class getClass<DDScannerResult>()
{
return PAL::getDDScannerResultClass();
}
#endif

NSType typeFromObject(id object)
{
ASSERT(object);
Expand All @@ -270,6 +277,10 @@ NSType typeFromObject(id object)
return NSType::Array;
if ([object isKindOfClass:[WebCore::CocoaColor class]])
return NSType::Color;
#if ENABLE(DATA_DETECTION)
if (PAL::isDataDetectorsCoreFrameworkAvailable() && [object isKindOfClass:[PAL::getDDScannerResultClass() class]])
return NSType::DDScannerResult;
#endif
if ([object isKindOfClass:[NSData class]])
return NSType::Data;
if ([object isKindOfClass:[NSDate class]])
Expand All @@ -282,6 +293,8 @@ NSType typeFromObject(id object)
return NSType::Font;
if ([object isKindOfClass:[NSNumber class]])
return NSType::Number;
if ([object isKindOfClass:[NSValue class]])
return NSType::NSValue;
if ([object isKindOfClass:[NSString class]])
return NSType::String;
if ([object isKindOfClass:[NSURL class]])
Expand Down
70 changes: 70 additions & 0 deletions Source/WebKit/Shared/Cocoa/CoreIPCDDScannerResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2023 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.
*/

#pragma once

#if ENABLE(DATA_DETECTION)
#if PLATFORM(COCOA)

#include "ArgumentCodersCocoa.h"
#include "CoreIPCDictionary.h"
#include "CoreIPCSecureCoding.h"
#include <wtf/RetainPtr.h>

OBJC_CLASS DDScannerResult;

namespace WebKit {

class CoreIPCNSCFObject;

class CoreIPCDDScannerResult {
public:
CoreIPCDDScannerResult(DDScannerResult *);
CoreIPCDDScannerResult(const RetainPtr<DDScannerResult>& result)
: CoreIPCDDScannerResult(result.get())
{
}

RetainPtr<id> toID() const;

private:
friend struct IPC::ArgumentCoder<CoreIPCDDScannerResult, void>;

using Value = std::variant<CoreIPCDictionary, CoreIPCSecureCoding>;

static Value valueFromDDScannerResult(DDScannerResult *);

CoreIPCDDScannerResult(Value&& value)
: m_value(WTFMove(value))
{
}

Value m_value;
};

} // namespace WebKit

#endif // PLATFORM(COCOA)
#endif // ENABLE(DATA_DETECTION)
87 changes: 87 additions & 0 deletions Source/WebKit/Shared/Cocoa/CoreIPCDDScannerResult.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2023 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.
*/

#import "config.h"
#import "CoreIPCDDScannerResult.h"

#if ENABLE(DATA_DETECTION)
#if PLATFORM(COCOA)

#import "CoreIPCDictionary.h"
#import "CoreIPCNSCFObject.h"
#import <pal/cocoa/DataDetectorsCoreSoftLink.h>

@interface DDScannerResult ()
- (NSDictionary *)_webKitPropertyListData;
- (id)_initWithWebKitPropertyListData:(NSDictionary *)plist;
@end

namespace WebKit {

static bool shouldWrapDDScannerResult()
{
static std::once_flag onceFlag;
static bool shouldWrap;
std::call_once(onceFlag, [&] {
shouldWrap = PAL::getDDScannerResultClass()
&& [PAL::getDDScannerResultClass() instancesRespondToSelector:@selector(_webKitPropertyListData)]
&& [PAL::getDDScannerResultClass() instancesRespondToSelector:@selector(_initWithWebKitPropertyListData:)];
});

return shouldWrap;
}

CoreIPCDDScannerResult::Value CoreIPCDDScannerResult::valueFromDDScannerResult(DDScannerResult *result)
{
if (shouldWrapDDScannerResult())
return CoreIPCDictionary { [result _webKitPropertyListData] };
return CoreIPCSecureCoding { result };
}

CoreIPCDDScannerResult::CoreIPCDDScannerResult(DDScannerResult *result)
: m_value(valueFromDDScannerResult(result))
{
}

RetainPtr<id> CoreIPCDDScannerResult::toID() const
{
RetainPtr<id> result;

WTF::switchOn(m_value,
[&](const CoreIPCDictionary& dictionary) {
ASSERT(shouldWrapDDScannerResult());
result = adoptNS([[PAL::getDDScannerResultClass() alloc] _initWithWebKitPropertyListData:dictionary.toID().get()]);
}, [&](const CoreIPCSecureCoding& secureCoding) {
result = secureCoding.toID();
}
);

return result;
}

} // namespace WebKit

#endif // PLATFORM(COCOA)
#endif // ENABLE(DATA_DETECTION)
31 changes: 31 additions & 0 deletions Source/WebKit/Shared/Cocoa/CoreIPCDDScannerResult.serialization.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (C) 2023 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.

#if PLATFORM(COCOA) && ENABLE(DATA_DETECTION)

webkit_platform_headers: "CoreIPCDDScannerResult.h"

[WebKitPlatform] class WebKit::CoreIPCDDScannerResult {
WebKit::CoreIPCDDScannerResult::Value m_value
}

#endif // PLATFORM(COCOA) && ENABLE(DATA_DETECTION)
1 change: 1 addition & 0 deletions Source/WebKit/Shared/Cocoa/CoreIPCDictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace WebKit {
class CoreIPCNSCFObject;

class CoreIPCDictionary {
WTF_MAKE_FAST_ALLOCATED;
public:
CoreIPCDictionary(NSDictionary *);

Expand Down
6 changes: 6 additions & 0 deletions Source/WebKit/Shared/Cocoa/CoreIPCNSCFObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
#include "CoreIPCArray.h"
#include "CoreIPCCFType.h"
#include "CoreIPCColor.h"
#include "CoreIPCDDScannerResult.h"
#include "CoreIPCData.h"
#include "CoreIPCDate.h"
#include "CoreIPCDictionary.h"
#include "CoreIPCError.h"
#include "CoreIPCFont.h"
#include "CoreIPCNSValue.h"
#include "CoreIPCNumber.h"
#include "CoreIPCSecureCoding.h"
#include "CoreIPCString.h"
Expand All @@ -51,11 +53,15 @@ class CoreIPCNSCFObject {
CoreIPCArray,
CoreIPCCFType,
CoreIPCColor,
#if ENABLE(DATA_DETECTION)
CoreIPCDDScannerResult,
#endif
CoreIPCData,
CoreIPCDate,
CoreIPCDictionary,
CoreIPCError,
CoreIPCFont,
CoreIPCNSValue,
CoreIPCNumber,
CoreIPCSecureCoding,
CoreIPCString,
Expand Down
6 changes: 6 additions & 0 deletions Source/WebKit/Shared/Cocoa/CoreIPCNSCFObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
return CoreIPCArray((NSArray *)object);
case IPC::NSType::Color:
return CoreIPCColor((WebCore::CocoaColor *)object);
#if ENABLE(DATA_DETECTION)
case IPC::NSType::DDScannerResult:
return CoreIPCDDScannerResult((DDScannerResult *)object);
#endif
case IPC::NSType::Data:
return CoreIPCData((NSData *)object);
case IPC::NSType::Date:
Expand All @@ -53,6 +57,8 @@
return CoreIPCError((NSError *)object);
case IPC::NSType::Font:
return CoreIPCFont((WebCore::CocoaFont *)object);
case IPC::NSType::NSValue:
return CoreIPCNSValue((NSValue *)object);
case IPC::NSType::Number:
return CoreIPCNumber(bridge_cast((NSNumber *)object));
case IPC::NSType::SecureCoding:
Expand Down
Loading

0 comments on commit 72c2238

Please sign in to comment.