Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .github/workflows/identity-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ jobs:
extra="-DCN1_INCLUDE_OIDC -DCN1_INCLUDE_APPLESIGNIN -DCN1_INCLUDE_WEBAUTHN"
fi
echo "::group::clang $sdk $label"
# -fno-objc-arc because the production target sets
# CLANG_ENABLE_OBJC_ARC = NO. This lint used to pass -fobjc-arc,
# so it checked these files under a memory model the app never
# builds with: CN1OidcBrowser.m handed an autoreleased callback
# URL across threads, which is correct under ARC and a
# use-after-free without it, and every sign-in on a device
# failed while this step stayed green. Same rule as
# call-vpn-native.yml. scripts/check-ios-mrc-block-writes.py
# catches that class of bug in every native.
#
# -fmodules because the port's Metal sources use @import, and the
# real build compiles with CLANG_ENABLE_MODULES=YES. The renderer
# header used to be unreachable here -- CN1_USE_METAL was a
Expand All @@ -342,7 +352,8 @@ jobs:
-fmodules-cache-path="$STUB/modulecache" \
-target "$target" \
-isysroot "$SDK" \
-fobjc-arc \
-fno-objc-arc \
-Werror=arc-bridge-casts-disallowed-in-nonarc \
-Werror=incompatible-pointer-types \
-Werror=objc-method-access \
-Wno-unused-parameter \
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/source-gates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ jobs:
shell: bash
run: scripts/check-android-gms-isolation.py

# The iOS port builds without ARC, so a block storing an autoreleased
# object into a __block variable hands the waiting thread a freed
# pointer. CN1OidcBrowser.m shipped that and broke every OIDC sign-in;
# no Xcode build or lint can see it. Absolute: no baseline.
- name: Unit-test the iOS __block ownership gate
if: ${{ !cancelled() }}
shell: bash
run: python3 scripts/test_check_ios_mrc_block_writes.py

- name: iOS natives own their __block stores, and no check compiles them with ARC
if: ${{ !cancelled() }}
shell: bash
run: scripts/check-ios-mrc-block-writes.py

# Unit-test the gate's classification boundaries before the scan, so a
# broken gate is reported as a broken gate rather than as a clean tree.
- name: Unit-test the 16 KB page alignment gate
Expand Down
21 changes: 21 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,27 @@ scripts/check-ios-private-api.py --sdk appletvos --project-dir <...-src>
scripts/check-builder-define-toggles.py
```

#### The port has no ARC, and a `__block` store can be a use-after-free

Every Apple target builds the port with `CLANG_ENABLE_OBJC_ARC = NO`; only a
native that says "requires ARC" gets a per-file `-fobjc-arc`. Without ARC a
`__block` object variable retains nothing, so an autoreleased value a block
stores into one (a completion handler, a main-queue block ending in
`POOL_END()`) is freed before the waiting thread reads it. ParparVM turns the
crash into a NullPointerException with no message. `CN1OidcBrowser.m` shipped
exactly this, and `CN1SmartHome.m` did the reverse: an autoreleased key stored
outside a block and read in a later completion.

It passed QA because `identity-stack.yml` linted those files with
`-fobjc-arc`, the one model under which the code is correct.

`scripts/check-ios-mrc-block-writes.py` (`source-gates.yml`) gates both shapes:
store an owned value (`copy`, `retain`, `alloc`) and release it in the reader,
or explain on the line with `// mrr: <reason>`. It also fails any script or
workflow that compiles with `-fobjc-arc` across the board: tests, lints and
benches build with `-fno-objc-arc`, like the app. Choosing the flag per file
from the "requires ARC" marker is allowed.

### Android compatibility: classic Android apps as Codename One apps

An Android Studio module's `src/main` dropped into `common/src/main/android`
Expand Down
8 changes: 6 additions & 2 deletions Ports/iOSPort/nativeSources/CN1AR.m
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ - (NSString *)createAnchorTx:(float)tx ty:(float)ty tz:(float)tz
cn1arRunOnMain(^{
[self.sceneView.session addAnchor:anchor];
});
[anchor release];
return anchorId;
}

Expand All @@ -223,11 +224,14 @@ - (NSString *)createAnchorFromHit:(int)hitId {
result = self.recentHits[@(hitId)];
if (result != nil) {
ARAnchor *anchor = [[ARAnchor alloc] initWithTransform:result.worldTransform];
anchorId = anchor.identifier.UUIDString;
// Copied: UUIDString is autoreleased into the main thread's pool,
// which can drain before the calling thread reads it.
anchorId = [anchor.identifier.UUIDString copy];
[self.sceneView.session addAnchor:anchor];
[anchor release];
}
});
return anchorId;
return [anchorId autorelease];
}

- (ARAnchor *)findAnchorById:(NSString *)anchorId {
Expand Down
18 changes: 14 additions & 4 deletions Ports/iOSPort/nativeSources/CN1OidcBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ - (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuth
// Single static slot to keep the session strongly referenced for the
// duration of a flow. The port compiles without ARC (CLANG_ENABLE_OBJC_ARC =
// NO), so there these hold the +1 reference from -alloc, and
// cn1OidcClearSession() gives it up. Under ARC (the iOS native-source lint
// compiles with -fobjc-arc) the slots are strong and assigning nil is enough.
// cn1OidcClearSession() gives it up. The objc_arc branches below keep the
// file correct if it is ever compiled with ARC.
static id g_cn1OidcCurrentSession = nil;
static id g_cn1OidcCurrentContext = nil;

Expand Down Expand Up @@ -143,7 +143,15 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_
dispatch_semaphore_t sem = dispatch_semaphore_create(0);

dispatch_async(dispatch_get_main_queue(), ^{
ASWebAuthenticationSession *session =
// A flow abandoned by the timeout below still owns the globals; give
// them up here. Its handler may yet fire, which is why every handler
// clears only while the globals are still its own session's.
if (g_cn1OidcCurrentSession != nil) {
[(ASWebAuthenticationSession *)g_cn1OidcCurrentSession cancel];
cn1OidcClearSession();
}
__block ASWebAuthenticationSession *session = nil;
session =
[[ASWebAuthenticationSession alloc] initWithURL:url
callbackURLScheme:redirectScheme
completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) {
Expand All @@ -161,7 +169,9 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_oidcStartAuthorization___java_lang_
failure = [error retain];
#endif
}
cn1OidcClearSession();
if (g_cn1OidcCurrentSession == session) {
cn1OidcClearSession();
}
dispatch_semaphore_signal(sem);
}];

Expand Down
13 changes: 10 additions & 3 deletions Ports/iOSPort/nativeSources/CN1SmartHome.m
Original file line number Diff line number Diff line change
Expand Up @@ -3025,12 +3025,17 @@ void com_codename1_impl_ios_IOSNative_homeRefresh___int(
nil]])];
}
if (releaseAfterMode != nil) {
NSArray *queued = [afterMode
objectForKey:releaseAfterMode];
// Retained: removing the key releases the array,
// which is walked right after.
NSArray *queued = [[[afterMode
objectForKey:releaseAfterMode]
retain] autorelease];
[afterMode removeObjectForKey:releaseAfterMode];
for (void (^waiting)(BOOL) in queued) {
waiting(!reported);
}
[releaseAfterMode release];
releaseAfterMode = nil;
}
finished++;
if (finished == outstanding) {
Expand Down Expand Up @@ -3076,7 +3081,9 @@ void com_codename1_impl_ios_IOSNative_homeRefresh___int(
// keyed by THIS entry, so a second mode write in the same
// batch releases nothing that was not waiting on it. Its own
// answer is reported by writeParts as usual.
releaseAfterMode = [NSString stringWithFormat:@"%lu",
// Retained until the last part's completion reads it: that
// runs long after this pool has drained. Released there.
releaseAfterMode = [[NSString alloc] initWithFormat:@"%lu",
(unsigned long) i];
}
writeParts();
Expand Down
46 changes: 30 additions & 16 deletions Ports/iOSPort/nativeSources/CN1WebAuthn.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,13 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_webauthnCreate___java_lang_String_R

CN1WebAuthnDelegate *del = [[CN1WebAuthnDelegate alloc] init];
del.completion = ^{
finalResult = del.resultJson;
finalErrCode = del.errorCode;
finalErrMsg = del.errorMessage;
// Copied: the port builds without ARC, and these are read on the
// waiting thread after this block returns. They are alive today
// only because the delegate is never freed; copying does not
// depend on that.
finalResult = [del.resultJson copy];
finalErrCode = [del.errorCode copy];
finalErrMsg = [del.errorMessage copy];
dispatch_semaphore_signal(sem);
};

Expand All @@ -336,14 +340,17 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_webauthnCreate___java_lang_String_R
g_cn1WebAuthnCurrentDelegate = nil;
g_cn1WebAuthnCurrentController = nil;

JAVA_OBJECT answer = JAVA_NULL;
if (finalErrCode != nil) {
return fromNSString(CN1_THREAD_GET_STATE_PASS_ARG
answer = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG
cn1WebAuthnErrorString(finalErrCode, finalErrMsg));
} else if (finalResult != nil) {
answer = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG finalResult);
}
if (finalResult == nil) {
return JAVA_NULL;
}
return fromNSString(CN1_THREAD_GET_STATE_PASS_ARG finalResult);
[finalResult release];
[finalErrCode release];
[finalErrMsg release];
return answer;
}

JAVA_OBJECT com_codename1_impl_ios_IOSNative_webauthnGet___java_lang_String_R_java_lang_String(
Expand Down Expand Up @@ -414,9 +421,13 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_webauthnGet___java_lang_String_R_ja

CN1WebAuthnDelegate *del = [[CN1WebAuthnDelegate alloc] init];
del.completion = ^{
finalResult = del.resultJson;
finalErrCode = del.errorCode;
finalErrMsg = del.errorMessage;
// Copied: the port builds without ARC, and these are read on the
// waiting thread after this block returns. They are alive today
// only because the delegate is never freed; copying does not
// depend on that.
finalResult = [del.resultJson copy];
finalErrCode = [del.errorCode copy];
finalErrMsg = [del.errorMessage copy];
dispatch_semaphore_signal(sem);
};

Expand All @@ -434,14 +445,17 @@ JAVA_OBJECT com_codename1_impl_ios_IOSNative_webauthnGet___java_lang_String_R_ja
g_cn1WebAuthnCurrentDelegate = nil;
g_cn1WebAuthnCurrentController = nil;

JAVA_OBJECT answer = JAVA_NULL;
if (finalErrCode != nil) {
return fromNSString(CN1_THREAD_GET_STATE_PASS_ARG
answer = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG
cn1WebAuthnErrorString(finalErrCode, finalErrMsg));
} else if (finalResult != nil) {
answer = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG finalResult);
}
if (finalResult == nil) {
return JAVA_NULL;
}
return fromNSString(CN1_THREAD_GET_STATE_PASS_ARG finalResult);
[finalResult release];
[finalErrCode release];
[finalErrMsg release];
return answer;
}

#else
Expand Down
2 changes: 1 addition & 1 deletion Ports/iOSPort/nativeSources/CodenameOne_GLViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ void screenSizeChangedC(int width, int height) {
dispatch_sync(dispatch_get_main_queue(), decodeImageData);
}
#else
img = CN1AppleImageWithDataCompat(nd);
img = CN1AppleImageWithDataCompat(nd); // mrr: no block in this branch; the watch-only block above is not compiled
#endif
if (img == nil) {
#if TARGET_OS_WATCH
Expand Down
Loading
Loading