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
9 changes: 0 additions & 9 deletions pkgs/objective_c/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const assetName = 'objective_c.dylib';

// TODO(https://github.com/dart-lang/native/issues/2272): Remove this from the
// main build.
const testFiles = ['test/util.c'];

final logger = Logger('')
..level = Level.INFO
Expand Down Expand Up @@ -59,14 +58,6 @@ void main(List<String> args) async {
}
}

// Only include the test utils on mac OS. They use memory functions that
// aren't supported on iOS, like mach_vm_region. We don't need them on iOS
// anyway since we only run memory tests on mac.
if (os == OS.macOS) {
cFiles.addAll(
testFiles.map((f) => input.packageRoot.resolve(f).toFilePath()),
);
}

final sysroot = sdkPath(codeConfig);
final minVersion = minOSVersion(codeConfig);
Expand Down
2 changes: 2 additions & 0 deletions pkgs/objective_c/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ dependency_overrides:
path: ../hooks
native_toolchain_c:
path: ../native_toolchain_c
objective_c_helper:
path: ../objective_c_helper
1 change: 0 additions & 1 deletion pkgs/objective_c/test/autorelease_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'dart:ffi';

import 'package:objective_c/objective_c.dart';
import 'package:test/test.dart';

import 'util.dart';

void main() {
Expand Down
1 change: 0 additions & 1 deletion pkgs/objective_c/test/interface_lists_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'dart:io';
import 'package:ffigen/src/code_generator/objc_built_in_types.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

import 'util.dart';

// The default expect error message for sets isn't very useful. In the common
Expand Down
1 change: 0 additions & 1 deletion pkgs/objective_c/test/nsdictionary_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'dart:ffi';

import 'package:objective_c/objective_c.dart';
import 'package:test/test.dart';

import 'util.dart';

void main() {
Expand Down
1 change: 0 additions & 1 deletion pkgs/objective_c/test/observer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'dart:ffi';

import 'package:objective_c/objective_c.dart';
import 'package:test/test.dart';

import 'util.dart';

void main() {
Expand Down
33 changes: 0 additions & 33 deletions pkgs/objective_c/test/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,4 @@ void doGC() {
calloc.free(gcNow);
}

@Native<Int Function(Pointer<Void>)>(isLeaf: true, symbol: 'isReadableMemory')
external int _isReadableMemory(Pointer<Void> ptr);

@Native<Uint64 Function(Pointer<Void>)>(
isLeaf: true,
symbol: 'getObjectRetainCount',
)
external int _getObjectRetainCount(Pointer<Void> object);

int objectRetainCount(Pointer<ObjCObjectImpl> object) {
if (_isReadableMemory(object.cast()) == 0) return 0;
final header = object.cast<Uint64>().value;

// package:objective_c's isValidObject function internally calls
// object_getClass then isValidClass. But object_getClass can occasionally
// crash for invalid objects. This masking logic is a simplified version of
// what object_getClass does internally. This is less likely to crash, but
// more likely to break due to ObjC runtime updates, which is a reasonable
// trade off to make in tests where we're explicitly calling it many times
// on invalid objects. In package:objective_c's case, it doesn't matter so
// much if isValidObject crashes, since it's a best effort attempt to give a
// nice stack trace before the real crash, but it would be a problem if
// isValidObject broke due to a runtime update.
// These constants are the ISA_MASK macro defined in runtime/objc-private.h.
const maskX64 = 0x00007ffffffffff8;
const maskArm = 0x0000000ffffffff8;
final mask = Abi.current() == Abi.macosX64 ? maskX64 : maskArm;
final clazz = Pointer<ObjCObjectImpl>.fromAddress(header & mask);

if (!internal_for_testing.isValidClass(clazz)) return 0;
return _getObjectRetainCount(object.cast());
}

String pkgDir = findPackageRoot('objective_c').toFilePath();
File renamed without changes.
37 changes: 37 additions & 0 deletions pkgs/objective_c_helper/lib/src/util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'dart:ffi';
import 'package:objective_c/objective_c.dart';
import 'package:objective_c/src/internal.dart' as internal_for_testing show isValidClass;

@Native<Int Function(Pointer<Void>)>(isLeaf: true, symbol: 'isReadableMemory')
external int _isReadableMemory(Pointer<Void> ptr);

@Native<Uint64 Function(Pointer<Void>)>(
isLeaf: true,
symbol: 'getObjectRetainCount',
)
external int _getObjectRetainCount(Pointer<Void> object);

int objectRetainCount(Pointer<ObjCObjectImpl> object) {
if (_isReadableMemory(object.cast()) == 0) return 0;
final header = object.cast<Uint64>().value;

// package:objective_c's isValidObject function internally calls
// object_getClass then isValidClass. But object_getClass can occasionally
// crash for invalid objects. This masking logic is a simplified version of
// what object_getClass does internally. This is less likely to crash, but
// more likely to break due to ObjC runtime updates, which is a reasonable
// trade off to make in tests where we're explicitly calling it many times
// on invalid objects. In package:objective_c's case, it doesn't matter so
// much if isValidObject crashes, since it's a best effort attempt to give a
// nice stack trace before the real crash, but it would be a problem if
// isValidObject broke due to a runtime update.
// These constants are the ISA_MASK macro defined in runtime/objc-private.h.
const maskX64 = 0x00007ffffffffff8;
const maskArm = 0x0000000ffffffff8;
final mask = Abi.current() == Abi.macosX64 ? maskX64 : maskArm;
final clazz = Pointer<ObjCObjectImpl>.fromAddress(header & mask);

if (!internal_for_testing.isValidClass(clazz)) return 0;
return _getObjectRetainCount(object.cast());
}

7 changes: 7 additions & 0 deletions pkgs/objective_c_helper/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: objective_c_helper
description: Helper package for objective_c tests
version: 0.0.1
environment:
sdk: ">=2.19.0 <3.0.0"
dependencies: {}
dev_dependencies: {}