Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit adc4171

Browse files
committed
Re-do static function dispatch.
Until now we had ifuncs for the string/memory routines in dynamic executables but trivial one-line assembler stubs branching to the default implementation in all cases for static executables. I did recently experiment with running the ifuncs earlier in static executables, but that actually broke one of our tests --- although our internal ifuncs are built very carefully, an ifunc built as a test ends up with shadow call stack trying to write and read x18 in the prolog and epilog, but the shadow call stack (and x18) isn't set up yet, leading to a crash (before any of the usual signal handlers have been installed, so without any useful diagnostics). While as far as I can tell, basically no-one outside of libc actually uses ifuncs (in part because they're not available on non-Linux systems, and if you're going to have to have a different implementation for other OSes, you usually end up preferring to just use the same implementation everywhere), *and* this only affects static executables, which are also barely used in practice, I still don't fancy the app compat risk of just saying "meh, anyone with ifuncs will have to make sure they're safe". Which leads us to this change, which is basically a poor man's ifunc, with function-scoped static function pointers as a poor man's GOT. (Which means that, while not ideal performance-wise, this isn't actually much worse than a dynamic executable.) This change also factors out the typedefs, since there were a handful of mistakes in some of the copies. Test: run the static benchmarks on various architectures Change-Id: Iec945b710174bebaa27cf71ed0101e7f5d769697
1 parent 7b2c6d4 commit adc4171

13 files changed

+587
-677
lines changed

libc/Android.bp

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,43 +1416,13 @@ cc_library_static {
14161416
}
14171417

14181418
// ========================================================
1419-
// libc_static_dispatch.a --- libc.a ifuncs
1419+
// libc_static_dispatch.a/libc_dynamic_dispatch.a --- string/memory "ifuncs"
1420+
// (Actually ifuncs for libc.so, but a home-grown alternative for libc.a.)
14201421
// ========================================================
1421-
cc_library_static {
1422-
defaults: ["libc_defaults"],
1423-
name: "libc_static_dispatch",
14241422

1425-
arch: {
1426-
x86_64: {
1427-
srcs: ["arch-x86_64/static_function_dispatch.S"],
1428-
},
1429-
x86: {
1430-
srcs: ["arch-x86/static_function_dispatch.S"],
1431-
},
1432-
arm: {
1433-
srcs: ["arch-arm/static_function_dispatch.S"],
1434-
},
1435-
arm64: {
1436-
srcs: ["arch-arm64/static_function_dispatch.S"],
1437-
},
1438-
riscv64: {
1439-
srcs: ["arch-riscv64/static_function_dispatch.S"],
1440-
},
1441-
},
1442-
}
1443-
1444-
// ========================================================
1445-
// libc_dynamic_dispatch.a --- libc.so ifuncs
1446-
// ========================================================
1447-
cc_library_static {
1423+
cc_defaults {
1424+
name: "libc_dispatch_defaults",
14481425
defaults: ["libc_defaults"],
1449-
name: "libc_dynamic_dispatch",
1450-
1451-
cflags: [
1452-
"-ffreestanding",
1453-
"-fno-stack-protector",
1454-
"-fno-jump-tables",
1455-
],
14561426
arch: {
14571427
x86_64: {
14581428
srcs: ["arch-x86_64/dynamic_function_dispatch.cpp"],
@@ -1470,6 +1440,30 @@ cc_library_static {
14701440
srcs: ["arch-riscv64/dynamic_function_dispatch.cpp"],
14711441
},
14721442
},
1443+
// Prevent the compiler from inserting calls to libc/taking the address of
1444+
// a jump table from within an ifunc (or, in the static case, code that
1445+
// can be executed arbitrarily early).
1446+
cflags: [
1447+
"-ffreestanding",
1448+
"-fno-stack-protector",
1449+
"-fno-jump-tables",
1450+
],
1451+
}
1452+
1453+
cc_library_static {
1454+
name: "libc_static_dispatch",
1455+
defaults: ["libc_dispatch_defaults"],
1456+
cflags: [
1457+
"-DBIONIC_STATIC_DISPATCH",
1458+
],
1459+
}
1460+
1461+
cc_library_static {
1462+
name: "libc_dynamic_dispatch",
1463+
defaults: ["libc_dispatch_defaults"],
1464+
cflags: [
1465+
"-DBIONIC_DYNAMIC_DISPATCH",
1466+
],
14731467
}
14741468

14751469
// ========================================================

0 commit comments

Comments
 (0)