Skip to content

Commit 735d05b

Browse files
authored
Add AndroidNative cinterop for syscall id SYS_getrandom (#37)
1 parent 05819fe commit 735d05b

File tree

7 files changed

+71
-7
lines changed

7 files changed

+71
-7
lines changed

build-logic/src/main/kotlin/dokka.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ extensions.configure<DokkaExtension> {
5151
}
5252

5353
pluginsConfiguration.html {
54-
footerMessage.set("© 2025-${LocalDate.now().year} Copyright KotlinCrypto")
54+
footerMessage.set("© 2023-${LocalDate.now().year} Copyright KotlinCrypto")
5555
}
5656
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ org.gradle.caching=true
44

55
kotlin.code.style=official
66
kotlin.mpp.applyDefaultHierarchyTemplate=false
7+
kotlin.mpp.enableCInteropCommonization=true
78
kotlin.mpp.stability.nowarn=true
89
kotlin.native.ignoreDisabledTargets=true
910

library/crypto-rand/build.gradle.kts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
**/
16+
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
17+
import org.jetbrains.kotlin.konan.target.Family
18+
1619
plugins {
1720
id("configuration")
1821
}
@@ -48,5 +51,19 @@ kmpConfiguration {
4851
}
4952
}
5053
}
54+
55+
kotlin {
56+
val cInteropDir = projectDir
57+
.resolve("src")
58+
.resolve("nativeInterop")
59+
.resolve("cinterop")
60+
61+
targets.filterIsInstance<KotlinNativeTarget>().forEach target@ { target ->
62+
if (target.konanTarget.family != Family.ANDROID) return@target
63+
target.compilations["main"].cinterops.create("syscall") {
64+
definitionFile.set(cInteropDir.resolve("$name.def"))
65+
}
66+
}
67+
}
5168
}
5269
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025 KotlinCrypto
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
@file:Suppress("FunctionName", "NOTHING_TO_INLINE", "KotlinRedundantDiagnosticSuppress", "SpellCheckingInspection")
17+
18+
package org.kotlincrypto.random.internal
19+
20+
import kotlinx.cinterop.ExperimentalForeignApi
21+
22+
@OptIn(ExperimentalForeignApi::class)
23+
internal actual inline fun _SYS_getrandom(): Int = SYS_getrandom

library/crypto-rand/src/linuxAndroidMain/kotlin/org/kotlincrypto/random/internal/LinuxAndroidPlatform.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,20 @@ import kotlin.contracts.ExperimentalContracts
2424
import kotlin.contracts.InvocationKind
2525
import kotlin.contracts.contract
2626

27-
// https://docs.piston.rs/dev_menu/libc/constant.SYS_getrandom.html
28-
private const val SYS_getrandom: Long = 318L
29-
// https://docs.piston.rs/dev_menu/libc/constant.GRND_NONBLOCK.html
30-
private const val GRND_NONBLOCK: UInt = 0x0001u
27+
@Suppress("FunctionName")
28+
internal expect inline fun _SYS_getrandom(): Int
3129

3230
@OptIn(ExperimentalForeignApi::class, UnsafeNumber::class)
3331
private inline fun getrandom2(buf: CPointer<ByteVar>, buflen: size_t, flags: u_int): Int {
34-
return syscall(SYS_getrandom.convert(), buf, buflen, flags).convert()
32+
return syscall(_SYS_getrandom().convert(), buf, buflen, flags).convert()
3533
}
3634

3735
// getrandom(2) available for Linux Kernel 3.17+ (Android API 23+)
3836
@OptIn(ExperimentalForeignApi::class, UnsafeNumber::class)
3937
internal val HAS_GET_RANDOM: Boolean by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
4038
val buf = ByteArray(1)
4139
val result = buf.usePinned { pinned ->
42-
getrandom2(pinned.addressOf(0), buf.size.toULong().convert(), GRND_NONBLOCK)
40+
getrandom2(pinned.addressOf(0), buf.size.convert(), 0x0001u /* GRND_NONBLOCK */)
4341
}
4442
if (result >= 0) return@lazy true
4543

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2025 KotlinCrypto
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
@file:Suppress("FunctionName", "NOTHING_TO_INLINE", "KotlinRedundantDiagnosticSuppress", "SpellCheckingInspection")
17+
18+
package org.kotlincrypto.random.internal
19+
20+
import platform.linux.SYS_getrandom
21+
22+
internal actual inline fun _SYS_getrandom(): Int = SYS_getrandom
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SYS_getrandom for AndroidNative targets
2+
package = org.kotlincrypto.random.internal
3+
headers = sys/syscall.h

0 commit comments

Comments
 (0)