SlideShare a Scribd company logo
1 of 15
Download to read offline
SHARING C++ OBJECTS
IN LINUX
Roberto A.Vitillo (LBNL)
November 2011, Concurrency Workshop, Chicago
1
EXAMPLE USE CASE
Process A Process B
Database of objects in shared memory
object
object
object
object
VirtualMemoryA
VirtualMemoryB
Physical Memory
(simplified view...)
2
OBJECT SHARING
• Objects can be allocated in shared memory segments (e.g. custom allocators)
• Assumptions:
‣ GCC ≥ 3 (Itanium C++ ABI)
‣ Linux ≥ 2.6.12 (Address Space Layout Randomization)
‣ Changing the default visibility of symbols, with visibility pragmas or linker scripts for example, is not considered
‣ Binding references to global symbols to the definition within a shared library (e.g. ld’s -Bsymbolic option) is not
considered
• Issues:
‣ Static data
‣ Pointers and references
‣ Virtual functions
‣ Virtual inheritance
3
STATIC DATA
• Static data members have external linkage; each process has
its own copy
• Non-const static data members reside in the object
file’s .data section: changes are not shared between the
processes
• Const static members reside in the object’s file .rodata
section and their semantic is not altered
• Same argument applies to local static variables
4
POINTERS AND REFERENCES
• Pointers and references can be shared only when pointing to
objects within shared segments that are mapped at the same
virtual base address in all processes
‣ allocate needed shared segments before forking
‣ allocate shared segments at the same virtual base address in
all processes
‣ use “slow” offset pointers
5
VIRTUALTABLES
• A class that has virtual
methods or virtual bases has a
set of associated virtual tables
• The tables reside in
the .rodata section
VirtualTable Layout (Itanium ABI)
Virtual Call Offsets
Virtual Base Offsets
OffsetToTop
Typeinfo Pointer
Virtual Function Pointers
• An object of such class contains pointers to the above
mentioned tables (through itself or its contained base
objects)
vtable ptr
6
VIRTUALTABLES
class A{
public:
virtual void foo();
private:
int m_a;
};
class B{
public:
virtual void bar();
private:
int m_b;
};
class C: public virtual A, public virtual B{
public:
virtual void foo();
virtual void car();
};
Vtable AVtable A
0 offset_to_top(0)
8 A::rtti
16 A::foo()
Vtable BVtable B
0 offset_to_top(0)
8 B::rtti
16 B::bar()
Vtable CVtable C
0 offset_to_B(24)
8 offset_to_A(8)
16 offset_to_top(0)
24 C::rtti
32 C::foo()
40 C::car()
48 vcall_offset_foo(-8)
56 offset_to_top(-8)
64 C::rtti
72 C::foo() thunk
80 vcall_offset_bar(0)
88 offset_to_top(-18)
96 C::rtti
104 B::bar
7
VIRTUALTABLES
• VirtualTables reside in the object’s file .rodata section
• Methods and thunks in the .text section
• To be able to share objects that contain pointers to vtables, that in turn contain pointers to
methods, thunks and typeinfos, the vtables, methods, thunks and typeinfos need to have the
same virtual addresses on all processes
• Methods can in turn reference functions and data...
‣ object files have to be mapped at the same virtual base address on all processes
• Executables that don’t use shared libraries are loaded at a fixed address and their initial
virtual memory layout is the “same” even for unrelated processes
‣ if -fPIE is not enabled
8
SHARED LIBRARIES
• We know that the shared libraries need to
be mapped at the same address in all
processes
• If the shared library is dynamically linked
to an executable that forks the
cooperating processes, no changes are
needed as the mapping is inherited
vitillo@eniac $ ldd /usr/bin/gcc
linux-vdso.so.1 => (0x00007fff99e6f000)
libc.so.6 => /lib64/libc.so.6 (0x00002aefcc69d000)
libdl.so.2 => /lib64/libdl.so.2 (0x00002aefcc9f5000)
/lib64/ld-linux-x86-64.so.2 (0x00002aefcc27d000)
vitillo@eniac $ ldd /usr/bin/gcc
linux-vdso.so.1 => (0x00007fff1c12f000)
libc.so.6 => /lib64/libc.so.6 (0x00002aabda159000)
libdl.so.2 => /lib64/libdl.so.2 (0x00002aabda4b1000)
/lib64/ld-linux-x86-64.so.2 (0x00002aabd9d39000)
• For unrelated processes, that dynamically link a library, we can’t rely on the load order
defined in the .dynamic section
‣ Address Space Layout Randomization
‣ Preloading can change the load order
• Dynamic loading can be an issue for related and unrelated processes
ASLR in action
9
SHARED LIBRARIES
• Workaround:
• Use a linker script to set the first PT_LOAD segment’s p_vaddr to assign to each DSO a
fixed unique load address (or use prelink)
‣ feasible for x86-64
• The dynamic linker can be modified to enforce the loading of the shared library at the
specified address since otherwise p_vaddr is taken only as a suggestion
• One could think of more sophisticated solutions whose end effect reduces to the above
mentioned one
‣ E.g. set a flag in the .dynamic section of the shared libraries that contain the vtables of
the classes whose objects we want to share and set p_vaddr only for those
• Similar considerations can be made for dynamically loaded shared libraries
10
SHARED LIBRARIES
• The One Definition Rule requires to have a single definition even
if many objects in C++ are not clearly part of a single object
• The virtual table for a class is emitted in the same object
containing the definition of its key function
• The key function is the first non-pure virtual, non-inline function
at the point of class definition
• If there is no key function then the vtable is emitted
everywhere used
11
SHARED LIBRARIES
• E.g. each shared library that is using a class
with only inline virtual functions has a local
definition of the vtable
• All shared libraries that contain a copy of the
virtual table should be mapped at the same
address...
• ...unless we are certain that the first DSO
with the incriminated vtable is loaded at the
same virtual address in all processes
‣ relocation and symbol interposing take
care of the rest
• Workaround: classes whose objects need to
be shared are required to have a key function
DSO A
class A{
public:
virtual void foo(){};
};
got
vtable A
DSO B
got
vtable A
before relocation
after relocation
got got
vtable A
class A{
public:
virtual void foo(){};
};
12
VIRTUAL INHERITANCE
• Virtually inherited objects have pointers to address base
objects
• How those pointers are implemented is not defined by the
C++ standard
• On Linux the ABI requires them to be implemented as offsets
‣ no issue
13
CONCLUSION
• A slow, costly but portable solution would require a new layer of indirection
• The proposed workarounds may be non-portable but enable processes to share
objects without overheads and deep changes to the toolchain
• Proposed solution that requires a minimum amount of work:
‣ sharable classes have a key function
‣ an executable is dynamically linked to the libraries with the sharable vtables
‣ dynamic loading of libraries with sharable vtables is only allowed before forking
‣ the cooperating processes are forked
14
RESOURCES
• Boost Interprocess - http://www.boost.org
• C++, ISO/IEC 14882 International Standard
• SystemV ABI - http://www.sco.com/developers/gabi/latest/contents.html
• Itanium C++ ABI - http://sourcery.mentor.com/public/cxx-abi/abi.html
• Address Space Layout Randomization - http://en.wikipedia.org/wiki/Address_space_layout_randomization
• Prelink - http://people.redhat.com/jakub/prelink.pdf
• HowTo Write Shared Libraries - http://www.akkadia.org/drepper/dsohowto.pdf
• Executable and Linking Format (ELF) Specification - http://pdos.csail.mit.edu/6.828/2011/readings/elf.pdf
15

More Related Content

What's hot

What's new in Gerrit Code Review 3.0
What's new in Gerrit Code Review 3.0What's new in Gerrit Code Review 3.0
What's new in Gerrit Code Review 3.0Luca Milanesio
 
Open vSwitch Introduction
Open vSwitch IntroductionOpen vSwitch Introduction
Open vSwitch IntroductionHungWei Chiu
 
2021.02 new in Ceph Pacific Dashboard
2021.02 new in Ceph Pacific Dashboard2021.02 new in Ceph Pacific Dashboard
2021.02 new in Ceph Pacific DashboardCeph Community
 
Multi level inheritence
Multi level inheritenceMulti level inheritence
Multi level inheritenceRanaMOIN1
 
GFX Part 7 - Introduction to Rendering Targets in OpenGL ES
GFX Part 7 - Introduction to Rendering Targets in OpenGL ESGFX Part 7 - Introduction to Rendering Targets in OpenGL ES
GFX Part 7 - Introduction to Rendering Targets in OpenGL ESPrabindh Sundareson
 
Ceph Day Melbourne - Troubleshooting Ceph
Ceph Day Melbourne - Troubleshooting Ceph Ceph Day Melbourne - Troubleshooting Ceph
Ceph Day Melbourne - Troubleshooting Ceph Ceph Community
 
Steve Huffman - Lessons learned while at reddit.com
Steve Huffman - Lessons learned while at reddit.comSteve Huffman - Lessons learned while at reddit.com
Steve Huffman - Lessons learned while at reddit.comCarsonified Team
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritancezindadili
 
What CloudStackers Need To Know About LINSTOR/DRBD
What CloudStackers Need To Know About LINSTOR/DRBDWhat CloudStackers Need To Know About LINSTOR/DRBD
What CloudStackers Need To Know About LINSTOR/DRBDShapeBlue
 

What's hot (15)

CI-CD WITH GITLAB WORKFLOW
CI-CD WITH GITLAB WORKFLOWCI-CD WITH GITLAB WORKFLOW
CI-CD WITH GITLAB WORKFLOW
 
What's new in Gerrit Code Review 3.0
What's new in Gerrit Code Review 3.0What's new in Gerrit Code Review 3.0
What's new in Gerrit Code Review 3.0
 
Open vSwitch Introduction
Open vSwitch IntroductionOpen vSwitch Introduction
Open vSwitch Introduction
 
Namespaces in Linux
Namespaces in LinuxNamespaces in Linux
Namespaces in Linux
 
2021.02 new in Ceph Pacific Dashboard
2021.02 new in Ceph Pacific Dashboard2021.02 new in Ceph Pacific Dashboard
2021.02 new in Ceph Pacific Dashboard
 
The Java memory model made easy
The Java memory model made easyThe Java memory model made easy
The Java memory model made easy
 
Multi level inheritence
Multi level inheritenceMulti level inheritence
Multi level inheritence
 
GFX Part 7 - Introduction to Rendering Targets in OpenGL ES
GFX Part 7 - Introduction to Rendering Targets in OpenGL ESGFX Part 7 - Introduction to Rendering Targets in OpenGL ES
GFX Part 7 - Introduction to Rendering Targets in OpenGL ES
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
IP Multicast on ec2
IP Multicast on ec2IP Multicast on ec2
IP Multicast on ec2
 
Ceph Day Melbourne - Troubleshooting Ceph
Ceph Day Melbourne - Troubleshooting Ceph Ceph Day Melbourne - Troubleshooting Ceph
Ceph Day Melbourne - Troubleshooting Ceph
 
Salsa20
Salsa20Salsa20
Salsa20
 
Steve Huffman - Lessons learned while at reddit.com
Steve Huffman - Lessons learned while at reddit.comSteve Huffman - Lessons learned while at reddit.com
Steve Huffman - Lessons learned while at reddit.com
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritance
 
What CloudStackers Need To Know About LINSTOR/DRBD
What CloudStackers Need To Know About LINSTOR/DRBDWhat CloudStackers Need To Know About LINSTOR/DRBD
What CloudStackers Need To Know About LINSTOR/DRBD
 

Similar to Sharing C++ objects in Linux

Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++Mohammad Shaker
 
Chapter Seven(2)
Chapter Seven(2)Chapter Seven(2)
Chapter Seven(2)bolovv
 
Strategies to improve embedded Linux application performance beyond ordinary ...
Strategies to improve embedded Linux application performance beyond ordinary ...Strategies to improve embedded Linux application performance beyond ordinary ...
Strategies to improve embedded Linux application performance beyond ordinary ...André Oriani
 
Transactional writes to cloud storage with Eric Liang
Transactional writes to cloud storage with Eric LiangTransactional writes to cloud storage with Eric Liang
Transactional writes to cloud storage with Eric LiangDatabricks
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPrashant Rane
 
[Defcon24] Introduction to the Witchcraft Compiler Collection
[Defcon24] Introduction to the Witchcraft Compiler Collection[Defcon24] Introduction to the Witchcraft Compiler Collection
[Defcon24] Introduction to the Witchcraft Compiler CollectionMoabi.com
 
OpenJDK Concurrent Collectors
OpenJDK Concurrent CollectorsOpenJDK Concurrent Collectors
OpenJDK Concurrent CollectorsMonica Beckwith
 
Managing Data and Operation Distribution In MongoDB
Managing Data and Operation Distribution In MongoDBManaging Data and Operation Distribution In MongoDB
Managing Data and Operation Distribution In MongoDBJason Terpko
 
Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...
Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...
Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...mfrancis
 
Modularity and Domain Driven Design; a killer combination?
Modularity and Domain Driven Design; a killer combination?Modularity and Domain Driven Design; a killer combination?
Modularity and Domain Driven Design; a killer combination?ACA IT-Solutions
 
Whirlwind tour of the Runtime Dynamic Linker
Whirlwind tour of the Runtime Dynamic LinkerWhirlwind tour of the Runtime Dynamic Linker
Whirlwind tour of the Runtime Dynamic LinkerGonçalo Gomes
 

Similar to Sharing C++ objects in Linux (20)

Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++
 
Chapter Seven(2)
Chapter Seven(2)Chapter Seven(2)
Chapter Seven(2)
 
Strategies to improve embedded Linux application performance beyond ordinary ...
Strategies to improve embedded Linux application performance beyond ordinary ...Strategies to improve embedded Linux application performance beyond ordinary ...
Strategies to improve embedded Linux application performance beyond ordinary ...
 
Transactional writes to cloud storage with Eric Liang
Transactional writes to cloud storage with Eric LiangTransactional writes to cloud storage with Eric Liang
Transactional writes to cloud storage with Eric Liang
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
 
wk 4 -- linking.ppt
wk 4 -- linking.pptwk 4 -- linking.ppt
wk 4 -- linking.ppt
 
[OSS Upstream Training] 5 open stack liberty_recap
[OSS Upstream Training] 5 open stack liberty_recap[OSS Upstream Training] 5 open stack liberty_recap
[OSS Upstream Training] 5 open stack liberty_recap
 
open stackliberty_recap_by_VietOpenStack
open stackliberty_recap_by_VietOpenStackopen stackliberty_recap_by_VietOpenStack
open stackliberty_recap_by_VietOpenStack
 
[Defcon24] Introduction to the Witchcraft Compiler Collection
[Defcon24] Introduction to the Witchcraft Compiler Collection[Defcon24] Introduction to the Witchcraft Compiler Collection
[Defcon24] Introduction to the Witchcraft Compiler Collection
 
Freckle
FreckleFreckle
Freckle
 
OpenJDK Concurrent Collectors
OpenJDK Concurrent CollectorsOpenJDK Concurrent Collectors
OpenJDK Concurrent Collectors
 
C++ Boot Camp Part 2
C++ Boot Camp Part 2C++ Boot Camp Part 2
C++ Boot Camp Part 2
 
Benedutch 2011 ew_ppt
Benedutch 2011 ew_pptBenedutch 2011 ew_ppt
Benedutch 2011 ew_ppt
 
Jax retrospective
Jax retrospectiveJax retrospective
Jax retrospective
 
No sql Database
No sql DatabaseNo sql Database
No sql Database
 
Managing Data and Operation Distribution In MongoDB
Managing Data and Operation Distribution In MongoDBManaging Data and Operation Distribution In MongoDB
Managing Data and Operation Distribution In MongoDB
 
Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...
Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...
Modularity and Domain Driven Design - A killer combination - T De Wolf & S va...
 
Modularity and Domain Driven Design; a killer combination?
Modularity and Domain Driven Design; a killer combination?Modularity and Domain Driven Design; a killer combination?
Modularity and Domain Driven Design; a killer combination?
 
Whirlwind tour of the Runtime Dynamic Linker
Whirlwind tour of the Runtime Dynamic LinkerWhirlwind tour of the Runtime Dynamic Linker
Whirlwind tour of the Runtime Dynamic Linker
 
C++ development within OOo
C++ development within OOoC++ development within OOo
C++ development within OOo
 

More from Roberto Agostino Vitillo (14)

Telemetry Onboarding
Telemetry OnboardingTelemetry Onboarding
Telemetry Onboarding
 
Growing a Data Pipeline for Analytics
Growing a Data Pipeline for AnalyticsGrowing a Data Pipeline for Analytics
Growing a Data Pipeline for Analytics
 
Telemetry Datasets
Telemetry DatasetsTelemetry Datasets
Telemetry Datasets
 
Growing a SQL Query
Growing a SQL QueryGrowing a SQL Query
Growing a SQL Query
 
Telemetry Onboarding
Telemetry OnboardingTelemetry Onboarding
Telemetry Onboarding
 
All you need to know about Statistics
All you need to know about StatisticsAll you need to know about Statistics
All you need to know about Statistics
 
Spark meets Telemetry
Spark meets TelemetrySpark meets Telemetry
Spark meets Telemetry
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to know
 
Performance tools developments
Performance tools developmentsPerformance tools developments
Performance tools developments
 
Exploiting vectorization with ISPC
Exploiting vectorization with ISPCExploiting vectorization with ISPC
Exploiting vectorization with ISPC
 
GOoDA tutorial
GOoDA tutorialGOoDA tutorial
GOoDA tutorial
 
Callgraph analysis
Callgraph analysisCallgraph analysis
Callgraph analysis
 
Vectorization in ATLAS
Vectorization in ATLASVectorization in ATLAS
Vectorization in ATLAS
 
Inter-process communication on steroids
Inter-process communication on steroidsInter-process communication on steroids
Inter-process communication on steroids
 

Recently uploaded

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Sharing C++ objects in Linux

  • 1. SHARING C++ OBJECTS IN LINUX Roberto A.Vitillo (LBNL) November 2011, Concurrency Workshop, Chicago 1
  • 2. EXAMPLE USE CASE Process A Process B Database of objects in shared memory object object object object VirtualMemoryA VirtualMemoryB Physical Memory (simplified view...) 2
  • 3. OBJECT SHARING • Objects can be allocated in shared memory segments (e.g. custom allocators) • Assumptions: ‣ GCC ≥ 3 (Itanium C++ ABI) ‣ Linux ≥ 2.6.12 (Address Space Layout Randomization) ‣ Changing the default visibility of symbols, with visibility pragmas or linker scripts for example, is not considered ‣ Binding references to global symbols to the definition within a shared library (e.g. ld’s -Bsymbolic option) is not considered • Issues: ‣ Static data ‣ Pointers and references ‣ Virtual functions ‣ Virtual inheritance 3
  • 4. STATIC DATA • Static data members have external linkage; each process has its own copy • Non-const static data members reside in the object file’s .data section: changes are not shared between the processes • Const static members reside in the object’s file .rodata section and their semantic is not altered • Same argument applies to local static variables 4
  • 5. POINTERS AND REFERENCES • Pointers and references can be shared only when pointing to objects within shared segments that are mapped at the same virtual base address in all processes ‣ allocate needed shared segments before forking ‣ allocate shared segments at the same virtual base address in all processes ‣ use “slow” offset pointers 5
  • 6. VIRTUALTABLES • A class that has virtual methods or virtual bases has a set of associated virtual tables • The tables reside in the .rodata section VirtualTable Layout (Itanium ABI) Virtual Call Offsets Virtual Base Offsets OffsetToTop Typeinfo Pointer Virtual Function Pointers • An object of such class contains pointers to the above mentioned tables (through itself or its contained base objects) vtable ptr 6
  • 7. VIRTUALTABLES class A{ public: virtual void foo(); private: int m_a; }; class B{ public: virtual void bar(); private: int m_b; }; class C: public virtual A, public virtual B{ public: virtual void foo(); virtual void car(); }; Vtable AVtable A 0 offset_to_top(0) 8 A::rtti 16 A::foo() Vtable BVtable B 0 offset_to_top(0) 8 B::rtti 16 B::bar() Vtable CVtable C 0 offset_to_B(24) 8 offset_to_A(8) 16 offset_to_top(0) 24 C::rtti 32 C::foo() 40 C::car() 48 vcall_offset_foo(-8) 56 offset_to_top(-8) 64 C::rtti 72 C::foo() thunk 80 vcall_offset_bar(0) 88 offset_to_top(-18) 96 C::rtti 104 B::bar 7
  • 8. VIRTUALTABLES • VirtualTables reside in the object’s file .rodata section • Methods and thunks in the .text section • To be able to share objects that contain pointers to vtables, that in turn contain pointers to methods, thunks and typeinfos, the vtables, methods, thunks and typeinfos need to have the same virtual addresses on all processes • Methods can in turn reference functions and data... ‣ object files have to be mapped at the same virtual base address on all processes • Executables that don’t use shared libraries are loaded at a fixed address and their initial virtual memory layout is the “same” even for unrelated processes ‣ if -fPIE is not enabled 8
  • 9. SHARED LIBRARIES • We know that the shared libraries need to be mapped at the same address in all processes • If the shared library is dynamically linked to an executable that forks the cooperating processes, no changes are needed as the mapping is inherited vitillo@eniac $ ldd /usr/bin/gcc linux-vdso.so.1 => (0x00007fff99e6f000) libc.so.6 => /lib64/libc.so.6 (0x00002aefcc69d000) libdl.so.2 => /lib64/libdl.so.2 (0x00002aefcc9f5000) /lib64/ld-linux-x86-64.so.2 (0x00002aefcc27d000) vitillo@eniac $ ldd /usr/bin/gcc linux-vdso.so.1 => (0x00007fff1c12f000) libc.so.6 => /lib64/libc.so.6 (0x00002aabda159000) libdl.so.2 => /lib64/libdl.so.2 (0x00002aabda4b1000) /lib64/ld-linux-x86-64.so.2 (0x00002aabd9d39000) • For unrelated processes, that dynamically link a library, we can’t rely on the load order defined in the .dynamic section ‣ Address Space Layout Randomization ‣ Preloading can change the load order • Dynamic loading can be an issue for related and unrelated processes ASLR in action 9
  • 10. SHARED LIBRARIES • Workaround: • Use a linker script to set the first PT_LOAD segment’s p_vaddr to assign to each DSO a fixed unique load address (or use prelink) ‣ feasible for x86-64 • The dynamic linker can be modified to enforce the loading of the shared library at the specified address since otherwise p_vaddr is taken only as a suggestion • One could think of more sophisticated solutions whose end effect reduces to the above mentioned one ‣ E.g. set a flag in the .dynamic section of the shared libraries that contain the vtables of the classes whose objects we want to share and set p_vaddr only for those • Similar considerations can be made for dynamically loaded shared libraries 10
  • 11. SHARED LIBRARIES • The One Definition Rule requires to have a single definition even if many objects in C++ are not clearly part of a single object • The virtual table for a class is emitted in the same object containing the definition of its key function • The key function is the first non-pure virtual, non-inline function at the point of class definition • If there is no key function then the vtable is emitted everywhere used 11
  • 12. SHARED LIBRARIES • E.g. each shared library that is using a class with only inline virtual functions has a local definition of the vtable • All shared libraries that contain a copy of the virtual table should be mapped at the same address... • ...unless we are certain that the first DSO with the incriminated vtable is loaded at the same virtual address in all processes ‣ relocation and symbol interposing take care of the rest • Workaround: classes whose objects need to be shared are required to have a key function DSO A class A{ public: virtual void foo(){}; }; got vtable A DSO B got vtable A before relocation after relocation got got vtable A class A{ public: virtual void foo(){}; }; 12
  • 13. VIRTUAL INHERITANCE • Virtually inherited objects have pointers to address base objects • How those pointers are implemented is not defined by the C++ standard • On Linux the ABI requires them to be implemented as offsets ‣ no issue 13
  • 14. CONCLUSION • A slow, costly but portable solution would require a new layer of indirection • The proposed workarounds may be non-portable but enable processes to share objects without overheads and deep changes to the toolchain • Proposed solution that requires a minimum amount of work: ‣ sharable classes have a key function ‣ an executable is dynamically linked to the libraries with the sharable vtables ‣ dynamic loading of libraries with sharable vtables is only allowed before forking ‣ the cooperating processes are forked 14
  • 15. RESOURCES • Boost Interprocess - http://www.boost.org • C++, ISO/IEC 14882 International Standard • SystemV ABI - http://www.sco.com/developers/gabi/latest/contents.html • Itanium C++ ABI - http://sourcery.mentor.com/public/cxx-abi/abi.html • Address Space Layout Randomization - http://en.wikipedia.org/wiki/Address_space_layout_randomization • Prelink - http://people.redhat.com/jakub/prelink.pdf • HowTo Write Shared Libraries - http://www.akkadia.org/drepper/dsohowto.pdf • Executable and Linking Format (ELF) Specification - http://pdos.csail.mit.edu/6.828/2011/readings/elf.pdf 15