Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent device data collector may causing a rare app crash while calculating free memory #1861

Merged
2 commits merged into from
Jul 4, 2023
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* Prevent rare app crash while migrating old `SharedPreferences` data from older versions of `bugsnag-android`
[#1860](https://github.com/bugsnag/bugsnag-android/pull/1860)

* Prevent free memory calculation from potentially crashing the app when `ActivityManager` cannot be reached.
[#1861](https://github.com/bugsnag/bugsnag-android/pull/1861)

## 5.30.0 (2023-05-11)

### Enhancements
Expand Down
2 changes: 2 additions & 0 deletions bugsnag-android-core/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
<ID>SwallowedException:BugsnagEventMapper.kt$BugsnagEventMapper$catch (pe: IllegalArgumentException) { ndkDateFormatHolder.get()!!.parse(this) ?: throw IllegalArgumentException("cannot parse date $this") }</ID>
<ID>SwallowedException:ConnectivityCompat.kt$ConnectivityLegacy$catch (e: NullPointerException) { // in some rare cases we get a remote NullPointerException via Parcel.readException null }</ID>
<ID>SwallowedException:ContextExtensions.kt$catch (exc: RuntimeException) { null }</ID>
<ID>SwallowedException:DeviceDataCollector.kt$DeviceDataCollector$catch (e: Throwable) { null }</ID>
<ID>SwallowedException:DeviceDataCollector.kt$DeviceDataCollector$catch (e: Throwable) { return null }</ID>
<ID>SwallowedException:DeviceDataCollector.kt$DeviceDataCollector$catch (exc: Exception) { false }</ID>
<ID>SwallowedException:DeviceDataCollector.kt$DeviceDataCollector$catch (exception: Exception) { logger.w("Could not get battery status") }</ID>
<ID>SwallowedException:DeviceDataCollector.kt$DeviceDataCollector$catch (exception: Exception) { logger.w("Could not get locationStatus") }</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,26 @@ internal class DeviceDataCollector(
/**
* Get the amount of memory remaining on the device
*/
private fun calculateFreeMemory(): Long? {
fun calculateFreeMemory(): Long? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
val freeMemory = appContext.getActivityManager()
?.let { am -> ActivityManager.MemoryInfo().also { am.getMemoryInfo(it) } }
?.availMem

if (freeMemory != null) {
return freeMemory
try {
val freeMemory = appContext.getActivityManager()
?.let { am -> ActivityManager.MemoryInfo().also { am.getMemoryInfo(it) } }
?.availMem
if (freeMemory != null) {
return freeMemory
}
} catch (e: Throwable) {
return null
}
}

return runCatching {
return try {
@Suppress("PrivateApi")
AndroidProcess::class.java.getDeclaredMethod("getFreeMemory").invoke(null) as Long?
}.getOrNull()
} catch (e: Throwable) {
null
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,34 @@ import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import com.bugsnag.android.internal.BackgroundTaskService
import org.junit.Ignore
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.junit.MockitoJUnitRunner
import java.io.File
import kotlin.concurrent.thread

@RunWith(MockitoJUnitRunner::class)
@RunWith(MockitoJUnitRunner.Silent::class)
class DataCollectorTest {

@Ignore("Disabled until we're able to mock final classes or auto-open classes")
@Test
fun testConcurretAccess() {
private lateinit var collector: DeviceDataCollector

@Mock
lateinit var context: Context

@Mock
lateinit var logger: Logger

@Before
fun setUp() {
val res = Mockito.mock(Resources::class.java)
Mockito.`when`(res.configuration).thenReturn(Configuration())
`when`(res.configuration).thenReturn(Configuration())

val collector = DeviceDataCollector(
collector = DeviceDataCollector(
Mockito.mock(Connectivity::class.java),
Mockito.mock(Context::class.java),
res,
Expand All @@ -33,7 +43,17 @@ class DataCollectorTest {
Mockito.mock(BackgroundTaskService::class.java),
Mockito.mock(Logger::class.java)
)
}

@Test
fun testCalculateFreeMemoryWithException() {
`when`(collector.calculateFreeMemory()).thenThrow(RuntimeException())
collector.generateDeviceWithState(0)
Assert.assertNull(collector.calculateFreeMemory())
}

@Test
fun testConcurrentAccess() {
repeat(10) { index ->
collector.addRuntimeVersionInfo("key" + index, "value" + index)
}
Expand Down