Android is a Linux-based architecture. In addition to the original Linux driver, Android need other additional device driver, like Android Logger, Binder, Low Memory killer, Power Management for android(wakelock), ASHMEM, etc out of which ashmem ,logger and binder are all character device drivers.
1. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 1
Android Binder:
Getting Started
by
Zafar Shahid
Oct. 14 2018
Silicon Valley Code Camp
Paypal San Jose CA
2. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 2
Who are we
IPC: The heart of Android
Design Patterns
Proxy, Bridge, Mediator
Binder IPC Internals
Case Study (Graphics)
Conclusion
Agenda
3. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 3
iBridge interview preparation group
https://www.meetup.com/iBridge-Interview-Preparation-Group/
Meetup for latest trends in technology
White boarding opportunity
Share knowledge and learn from peers
Challenge yourself
Who are we
4. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 4
IPC: The heart of Android
6. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 6
Why IPC?
o Why
o Sandboxed process
o Isolation
o Security
o Reliability
o IPC In GNU/Linux
o Signal
o Pipe
o Socket
o Semaphore
o Message queue
o Shared memory
o IPC In Android
o Binder: lightweight RPC
7. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 7
•Developed under the name OpenBinder by Palm Inc. under the
leadership of Dianne Hackborn
•Android Binder is the customized re-implementation of
OpenBinder, which provides bindings to functions and data
from one execution environment to another
Binder History
8. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 8
8
Problem statement
• Applications and Services may run in
separate processes but must communicate
and share data
• IPC can introduce significant processing
overhead and security holes.
9. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 9
Binder: Android's Solution
• Driver to facilitate inter-process communication
• High performance through shared memory
• Per-process thread pool for processing requests
• Reference counting, and mapping of object
references across processes
• Synchronous calls between processes
“In the Android platform, the binder is used for
nearly everything that happens across processes
in the core platform. " – Dianne Hackborn
https://lkml.org/lkml/2009/6/25/3
10. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 10
caller
callee
In the same process
Method invocation
14. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 14
14
Proxy Pattern in Android
•Binder decomposes the method call and all its corresponding
data to a level that Linux can understand.
• Transmitting it from the local process and address space to
the remote process and address space.
• Reassembling and reenacting the call there.
caller
callee
Binder in kernel
callee
caller
Proxy
Binder Thread
Stub
interface
interface
interface
15. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 1515
Mediator Pattern
•With the mediator pattern, communication
between objects is encapsulated with
a mediator object.
16. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 1616
Bridge Pattern
•decouple an abstraction from its implementation
so that the two can vary independently
17. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 17
Proxy Design Pattern
A Proxy is basically a representative between
the Client and the Component.
It gives the Client a simple interface to the
Component.
18. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 18
Some Proxy Pattern Usage
Protection Proxy: Controls access to
Component based on the access rights
of Clients.
Cache Proxy: Saves results temporarily
so when Client requests same expensive
operation again, the saved results are
returned without making call to
Component.
Virtual Proxy: Expensive objects are
created on demand.
21. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 21
Mediator Pattern
Common problem: Multiple objects of
same or different classes must
communicate/interact. Multiple
dependencies complicate objects, lead to
“spaghetti code.”
Solution: Define an object that
encapsulates how a set of objects
interact and interconnect. Make that
object the hub of communications,
responsible for controlling and
coordinating the interactions of clients –
the colleague objects.
22. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 22
Law of Demeter
“… the methods of a class should
not depend in any way on the
structure of any class, except the
immediate (top-level) structure of
their own class. Further, each
method should send messages to
objects belonging to a very limited
set of classes only.”
23. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 23
Analogies/Metaphors
Air traffic control
Stock market
eBay
Linux File permissions
24. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 24
Linux
file
permissions
25. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 25
Air Traffic Control Tower Mediator
Air Traffic Control Tower: (Mediator)
• Control tower at a controlled airport :
• Pilot communicating with the Traffic control.
• Some constraints on take off and landing are enforced
by the tower
• Tower does not control the whole flight. It exists only
to enforce constraints in terminal area.
Air Traffic Controller
26. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 26
Motivation – Mozilla ver 1.4.1
GKGFX rendering library
From an abandoned code base
Lines are dependency relationships
27. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 27
Mutual Dependencies - Mozilla 1.4.1
Object Oriented Design
encourages distribution of
behavior among objects.
Such distribution can result
in an object structure with
many connections between
objects.
In the worst case, every
object ends up knowing
about every other.
www.castle-
cadenza.demon.co.uk/mediat.htm
28. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 28
Quote from Mozilla Developer
“Even though some of us used to work on Mozilla, we have
to admit that the Mozilla code is a gigantic, bloated mess,
not to mention slow, and with an internal API so
flamboyantly baroque that frankly we can't even
comprehend where to begin”
http://news.com.com/2100-1023-980492.html
30. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 30
Advantages
• Limits subclassing – localizes behavior that would otherwise be
distributed among several objects.
• Decouples colleagues – As the number of connections is limited
by redirecting to a common object.
• Promotes high level of reusability – It proliferates the
interconnections to help eventually reduce it.
• Due to loose coupling, both mediator and colleague classes can
be reused independent of each other.
• Changing the system behavior means subclassing the Mediator.
Danger
• Mediator can become monolithic, violating proscription
against “God” or manager classes and making it hard to
maintain.
31. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 3131
Bridge patterns in
linking Java and C++
•Mediator pattern
Bridge Pattern in Android
32. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 32
Binder in Action
Process BProcess A
34. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 34
•Binder framework
•Binder Object
–an instance of a class that implements the Binder interface.
–One Binder object can implement multiple Binders
•Binder Protocol
•IBinder Interface
–is a well-defined set of methods, properties and events that a
Binder can implement.
•Binder Token
–A numeric value that uniquely identifies a Binder
• Marshalling
• Parcels
• Binder driver
Binder Terminology
36. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 36
•Special Binder node with known Binder address
•Client does not know the address of remote Binder
–only Binder interface knows its own address
•Binder submits a name and its Binder token to SM
–Client retrieves Binder address with service name from SM
Service Manager (SM)
37. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 37
Get Service list from SM
•$ adb shell service list
Found 71 services:
0 stub_isms: [com.android.internal.telephony.ISms]
1 stub_phone: [com.android.internal.telephony.ITelephony]
2 stub_iphonesubinfo:
[com.android.internal.telephony.IPhoneSubInfo]
..
5 stub_telephony.registry:
[com.android.internal.telephony.ITelephonyRegistry]
...
7 stub_activity: [android.app.IActivityManager]
...
9 phone: [com.android.internal.telephony.ITelephony]
…
56 activity: [android.app.IActivityManager]
...
64 SurfaceFlinger: [android.ui.ISurfaceComposer]
...
38. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 38
Service Registration and Discovery
39. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 39
Implementation Layers of Binder
Implemented in C
Implemented in C++
Implemented in Java
40. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 40
API Layer
• AIDL (Android Interface Definition Languag
– Ease the implementation of
Android remote services
– Defines an interface with method
of remote services
• AIDL parser generates Java class
- Proxy class for Client
- Stub class for Service
• Java API Wrapper
- Introduce facilities to the binder
– Wraps the middleware layer
41. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 41
41
AIDL (Android Interface Definition Language)
• Jave like syntax
• Data Types
–Java Primitives
–Containers
•String, List, Map, CharSequence
•List<>
•Multidimensional Array
–Parcelable
–Interface Reference
• Direction - in, out, inout
• oneway
–android.os.IBinder.FLAG_ONEWAY
42. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 42
42
AIDL Compiler
Full-fledged Java(-only) Support
Stub and Proxy Generator
// Interface
interface IRemoteService {
void ping();
}
public class RemoteService extends Service {
public IBinder onBind(Intent intent) { return mBinder; }
private final IRemoteService.Stub mBinder =
new IRemoteService.Stub() {
public void ping() { // Nothing }
};
}
IRemoteService mService =
IRemoteService.Stub.asInterface(service);
Server
Client
43. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 43
• Problem: Kernel is in C language and does not
understand classes and other complex objects.
•Solution: Marshalling and Parcels.
• Simple inter process messaging system
• In an object oriented view, the transaction data is
called parcel.
• The procedure of building a parcel is called
marshalling an object.
• The procedure of rebuilding a object from a parcel
is called unmarshalling an object.
Parcels and Marshalling
44. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 44
”flatten” ”unflatten”
transmit
Delivering arguments of method
android.os.Parcel
45. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 45
Representation of Parcel
• Parcel is NOT for general-purpose serialization
–This class (and the corresponding Parcelable API for
placing arbitrary objects into a Parcel) is designed as
a high-performance IPC transport.
–Not appropriate to place any Parcel data into
persistent storage
• Functions for writing/reading primitive data types:
–writeByte(byte) / readByte()
–writeDouble(double) / readDouble()
–writeFloat(float) / readFloat()
–writeInt(int) / readInt()
–writeLong(long) / readLong()
–writeString(String) / readString()
46. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 46
Middleware Layer
• Implements the user space facilities
of the Binder framework in C++
• Implements structures and methods
to spawn and manage new threads
• Provides interaction with the Binder
kernel driver
49. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 49
socket binder
internal status associated to FD associated to PID
(FD can be shared among
threads in the same
process)
read & write
operation
stream I/O done at once by
ioctl
network
transparency
Yes No
expected local only
Binder vs UNIX socket
50. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 50
50
Binder Performance
• Good
• Compact method index
• Native binary marshalling
• Support of ashmem shortcut
• Bad
• Dalvik Parcel overhead
• ioctl() path is not optimal
• Interface name overhead
• Limitations
• Not for streaming data
• Only local
51. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 51
51
Binder Security
•Binder’s Security Features
–Securely Determined Client Identity
–Binder.getCallingUid(), Binder.getCallingPid()
–Similar to Unix Domain Socket
getsockopt(..., SO_PEERCRED, ...)
–Interface Reference Security
•Client cannot guess Interface Reference
•Service Manager
–Directory Service for System Services
•Serveice should check client permission
Context.checkPermission(permission, pid, uid)
56. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 56
Binder use case: Android Graphics
57. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 57
Binder IPC is used for communicating between Graphics client and service.
Ref: http://www.cnblogs.com/xl19862005/archive/2011/11/17/2215363.html
Use
Case
58. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 58
Surface
Source: frameworks/base/core/java/android/view/Surface.java
/* Handle on to a raw buffer that is being
managed by the screen compositor */
public class Surface implements Parcelable
{
public Surface() {
mCanvas = new CompatibleCanvas();
}
private class CompatibleCanvas
extends Canvas { /* ... */ }
}
Surface instances can be written to and restored from a Parcel.
59. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 59
”flatten” ”unflatten”
transmit
Delivering arguments of method
60. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 60
• Can combine 2D/3D surfaces and surfaces from multiple applications
• Surfaces passed as buffers via Binder IPC calls
• Can use OpenGL ES and 2D hardware acceleraton.
Android SurfaceFlinger
61. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 61
Everything is
around Binder
62. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 62
Camera + SurfaceFlinger + Binder
63. Zafar Shahid, PhD Android Binder Deep Dive Track (1/3) 63
Questions?
References:
• Android IPC Mechanism by Jim Huang
• Inter-process communication of Android, Tetsuyuki Kobayashi
• http://blog.goggb.com/?post=1580
• Android Binder – Android Interprocess Communication, Thorsten
Schreiber
• Design Patterns in the Android Framework, Prof. Sheng-De Wang
• Deep Dive into Android IPC/Binder Framework at Android Builders
Summit 2013, by Aleksandar Gargenta, Marakana Inc.