Граф коммитов

14 Коммитов

Автор SHA1 Сообщение Дата
Rolf Bjarne Kvinge 350f597250
[runtime] Fix memory leak with BlockLiteral descriptors. Fixes #20503. (#20556)
We're using two different functions to atomically decrement a reference count,
the native `atomic_fetch_sub` and the managed `Interlocked.Decrement`.

Unfortunately the return value is not the same: `atomic_fetch_sub` returns the
original value before the subtraction, while `Interlocked.Decrement` returns
the subtracted value, while our code assumed the functions behaved the same.
This resulted in a memory leak, because we'd incorrectly expect `0` to be
returned from `atomic_fetch_sub` when the reference count reaches zero, and
thus not detect when the descriptor a block should be freed.

The fix is to update the expected return value from `atomic_fetch_sub` to be
`1` instead of `0`.

Fixes https://github.com/xamarin/xamarin-macios/issues/20503.
2024-05-06 09:05:58 +02:00
Rolf Bjarne Kvinge 21705ee361
[runtime] Use objc_[retain|release|autorelease] instead of sending messages. (#19415)
Calling the direct functions instead of sending messages is slightly faster,
and additionally it may make some static analyzers think we've enabled ARC for
our Objective-C code (which we don't, because we need to manually manage
reference counting).

These direct functions aren't in any public header (they're in a private header),
but they're documented as part of ARC here: https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support,
and clang emits references to these methods from user code, so it should be safe for us to use them.

Fixes https://github.com/xamarin/xamarin-macios/issues/19413.
2023-11-14 07:42:55 +01:00
Rolf Bjarne Kvinge 43f1d02dae
[CoreCLR] Implement xamarin_gchandle_get_target. (#11333)
This also meant reviewing calling code to make sure that MonoObject*s are
released when they should be, which meant reviewing every method that returns
a MonoObject*, and release the result.
2021-04-27 14:59:03 +02:00
Rolf Bjarne Kvinge 6d127660d5
[runtime] Move the code to wrap every managed thread with an NSAutoreleasePool to the MonoVM bridge. (#11257)
The current implementation is MonoVM-specific, and won't work with CoreCLR
(something else will have to be implemented for CoreCLR, which is tracked
here: https://github.com/xamarin/xamarin-macios/issues/11256).
2021-04-21 16:21:09 +02:00
Rolf Bjarne Kvinge 598dbc8ae3
Use pointer-sized GCHandles everywhere. (#10597)
* Convert the GCHandles interface from 32-bit ints to pointer size types

This involves:

* Stop using some bits of the GCHandle to store extra flags, instead add an extra
  field to store those flags.
* Define a INVALID_GCHANDLE constant and use it instead of 0/NULL. This is not
  strictly required, but it makes the code more self-documenting.
* Define a GCHandle type (typedef'ed to void*) and change all variables and parameters
  to use it instead of guint32.
* Use our own xamarin_gchandle_* methods (with pointer-sized types) that wraps
  the mono_gchandle_* embedding API (which uses 32-bit types) everywhere.
* Update managed code (registrars, runtime code, etc) accordingly.

* [runtime] Make debug code compile.

* Fix typo.

* Fix signature of xamarin_create_gchandle.

Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
2021-02-11 08:18:38 +01:00
Manuel de la Pena 63ab48e679
[Runtime] Enable the -Wsign-conversion warnings flag and fix some of the warnings. (#7405)
The flag adds a lot (A LOT) of warnings. The diff would be huge and
dangerous to review. We enable the flag and fix file by file.
2019-11-12 15:10:16 -05:00
Rolf Bjarne Kvinge 0418008046
[runtime] Use newer atomic functions. (#7119)
Fixes these warnings:

    shared.m:252:6: warning: 'OSAtomicDecrement32Barrier' is deprecated: first deprecated in watchOS 3.0 - Use std::atomic_fetch_sub() from <atomic> instead [-Wdeprecated-declarations]
            if (OSAtomicDecrement32Barrier (&bl->descriptor->ref_count) == 0) {
                ^
    /Applications/Xcode11.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS6.0.sdk/usr/include/libkern/OSAtomicDeprecated.h:201:9: note: 'OSAtomicDecrement32Barrier' has been explicitly marked deprecated here
    int32_t OSAtomicDecrement32Barrier( volatile int32_t *__theValue );
            ^
    shared.m:270:2: warning: 'OSAtomicIncrement32' is deprecated: first deprecated in watchOS 3.0 - Use std::atomic_fetch_add_explicit(std::memory_order_relaxed) from <atomic> instead [-Wdeprecated-declarations]
            OSAtomicIncrement32 (&source->descriptor->ref_count);
            ^
    /Applications/Xcode11.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS6.0.sdk/usr/include/libkern/OSAtomicDeprecated.h:171:9: note: 'OSAtomicIncrement32' has been explicitly marked deprecated here
    int32_t OSAtomicIncrement32( volatile int32_t *__theValue );
            ^
    monotouch-debug.m:309:10: warning: 'OSAtomicIncrement32Barrier' is deprecated: first deprecated in watchOS 3.0 - Use std::atomic_fetch_add() from <atomic> instead [-Wdeprecated-declarations]
            int c = OSAtomicIncrement32Barrier (&http_send_counter);
                    ^
    /Applications/Xcode11.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS6.0.sdk/usr/include/libkern/OSAtomicDeprecated.h:182:9: note: 'OSAtomicIncrement32Barrier' has been explicitly marked deprecated here
    int32_t OSAtomicIncrement32Barrier( volatile int32_t *__theValue );
            ^
2019-09-27 07:15:59 +02:00
Rolf Bjarne Kvinge 7f4a1db36f
[runtime] Use a single mono profiler for both newrefcount and NSAutoreleasePool thread hooks. (#5495)
Additionally don't use malloc'ed memory, to avoid having to free the memory.

This fixes a leak, since mono does not free the MonoProfiler argument:

    STACK OF 1 INSTANCE OF 'ROOT LEAK: <0x7fb94495c460>':
    [thread 0x10f3f85c0]:
    19  libdyld.dylib                      0x7fff6e009ed9 start + 1
    18  NameNotImportant                      0x10592dec4 main + 36  launcher.m:679
    17  NameNotImportant                      0x10592d039 xamarin_main + 1305  launcher.m:661
    16  NameNotImportant                      0x105973473 mono_main + 11731  driver.g.c:2484
    15  NameNotImportant                      0x10597020d mono_jit_exec + 349  driver.g.c:1236
    14  NameNotImportant                      0x105b4217e mono_runtime_exec_main_checked + 110  object.c:0
    13  NameNotImportant                      0x105b3af28 mono_runtime_invoke_checked + 136  object.c:2960
    12  NameNotImportant                      0x105a143a1 mono_jit_runtime_invoke + 513  mini-runtime.c:3011
    11  NameNotImportant                      0x105a103c1 mono_jit_compile_method_with_opt + 2577  mini-runtime.c:2411
    10  NameNotImportant                      0x105a21aa7 mono_jit_compile_method_inner + 1207  mini.c:4184
    9   NameNotImportant                      0x105b3b75f mono_runtime_class_init_full + 847  object.c:527
    8   NameNotImportant                      0x105b3c9d4 mono_runtime_try_invoke + 148  object.c:2960
    7   NameNotImportant                      0x105a147d3 mono_jit_runtime_invoke + 1587  mini-runtime.c:3148
    6   ???                                   0x106d00fb0 0x7fffffffffffffff + 9223372041264041905
    5   NameNotImportant                      0x105921b0c xamarin_initialize + 812  runtime.m:1412
    4   NameNotImportant                      0x10591e7e7 xamarin_install_nsautoreleasepool_hooks + 55  shared.m:241
    3   NameNotImportant                      0x105b4fd0a mono_profiler_install + 26  profiler.c:1019
    2   NameNotImportant                      0x105c3127b monoeg_malloc0 + 27  gmem.c:121
    1   libsystem_malloc.dylib             0x7fff6e1bacba calloc + 30
    0   libsystem_malloc.dylib             0x7fff6e1bad62 malloc_zone_calloc + 139

Also remove the call to mono_profiler_set_events, it doesn't do anything anymore ([1]).

[1]: 14b061ba65/mono/metadata/profiler.c (L1098-L1101)
2019-01-28 15:06:40 +01:00
Rolf Bjarne Kvinge 02fe5339a4
[runtime] Clean up public symbols. Fixes #5124. (#5155)
* [runtime] Clean up public symbols. Fixes #5124.

Clean up public symbols, by:

* Symbols that don't need to be public (most of them), can be private.
* Prefix all public symbols with `xamarin_`.
* Add a test to ensure we don't introduce new public symbols.
* Use C symbols instead of mangled C++ symbols, since those are easier to
  handle in the test.

This minimizes the chance of getting into a symbol clash with another native library.

Fixes https://github.com/xamarin/xamarin-macios/issues/5124.

* Some test fixes.
2018-11-21 11:48:15 -05:00
Rolf Bjarne Kvinge 679b981b66 [runtime] Work around static analyzer warning about incorrect refcount decrement. (#1718)
This warning:

    shared.m:216:3: warning: Incorrect decrement of the reference count of an object that is not owned at this point by the caller
                    [pool release];
                    ^~~~~~~~~~~~~~

is somewhat incorrect, because we're using NSAutoreleasePools in uncommon ways
(at the same time it's not entirely incorrect either, because we're not
following Apple's documentation about how to use NSAutoreleasePools).

Luckily calling [NSAutoreleasePool drain] is equivalent to [NSAutoreleasePool
release], so just replace the latter with the former to silence the warning,
since clang doesn't know those two are equivalent.
2017-02-20 19:30:23 +01:00
Rolf Bjarne Kvinge 36ea73ca65 Share the same block descriptors between copies of a block. Fixes #43592. (#683)
In bug #43592 the following occurs:

* App calls an API that takes a block.
* We create a stack-based ObjC block based on the delegate the app provided.
  This block has a pointer to a block description, describing the block
  in question (including the signature of the block, as an ObjC-type
  encoded string). We allocate a new block description for every block.
* Apple's API stores the pointer to the signature string somewhere.
* Apple calls _Block_copy to get a heap-based block.
* We create a heap-based block, and copy the entire description into the
  new heap-based block (including a copy of the signature).
* Apple returns from the API, and then we free the stack-based block
  (and the descriptor, and thus the signature string in the descriptor).
* Apple uses the pointer to the signature stored previously to investigate
  the signature of the block, and crashes because this signature has been
  freed.

The assumption in Apple's code is that the description will never be freed,
which is true for any Xcode project (clang will always be able to create the
block description at compile-time and emit it in the binary, which means the
memory will never be freed). We could potentially do the same thing in the
static registrar, but we'd still need a solution when using the dynamic
registrar.

To fix this instead of copying the entire description structure when creating
a heap-based block from the stack-based block, we make the description ref-
counted, and just use the same description in the heap-based block.

The signature will now stay in memory until both the heap-based and stack-
based blocks have been freed, and we hope Apple doesn't have any API that
needs the signature after all the blocks for that signature have been freed.

https://bugzilla.xamarin.com/show_bug.cgi?id=43592
2016-08-26 19:22:38 +02:00
Zoltan Varga 5270b63cee [runtime] Use the same coop gc macro names as mono master does. 2016-05-26 17:47:10 +02:00
Rolf Bjarne Kvinge af0d01c93a Initial review pass for COOP for watchOS. 2016-05-26 17:47:10 +02:00
Rolf Bjarne Kvinge ac418df815 Build our runtime. 2016-04-24 14:47:24 -04:00