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

MySQL Storage Engines
MySQL Storage EnginesMySQL Storage Engines
MySQL Storage EnginesKarthik .P.R
 
Kernel Recipes 2015: Kernel packet capture technologies
Kernel Recipes 2015: Kernel packet capture technologiesKernel Recipes 2015: Kernel packet capture technologies
Kernel Recipes 2015: Kernel packet capture technologiesAnne Nicolas
 
Understanding the Android System Server
Understanding the Android System ServerUnderstanding the Android System Server
Understanding the Android System ServerOpersys inc.
 
Berkeley Packet Filters
Berkeley Packet FiltersBerkeley Packet Filters
Berkeley Packet FiltersKernel TLV
 
XPDDS17: Shared Virtual Memory Virtualization Implementation on Xen - Yi Liu,...
XPDDS17: Shared Virtual Memory Virtualization Implementation on Xen - Yi Liu,...XPDDS17: Shared Virtual Memory Virtualization Implementation on Xen - Yi Liu,...
XPDDS17: Shared Virtual Memory Virtualization Implementation on Xen - Yi Liu,...The Linux Foundation
 
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...MongoDB
 
Histogram-in-Parallel-universe-of-MySQL-and-MariaDB
Histogram-in-Parallel-universe-of-MySQL-and-MariaDBHistogram-in-Parallel-universe-of-MySQL-and-MariaDB
Histogram-in-Parallel-universe-of-MySQL-and-MariaDBMydbops
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)shimosawa
 
Secret of Intel Management Engine by Igor Skochinsky
Secret of Intel Management Engine  by Igor SkochinskySecret of Intel Management Engine  by Igor Skochinsky
Secret of Intel Management Engine by Igor SkochinskyCODE BLUE
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)shimosawa
 
Gpfs introandsetup
Gpfs introandsetupGpfs introandsetup
Gpfs introandsetupasihan
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDBLinaro
 
Desmistificando Replicação no PostgreSQL
Desmistificando Replicação no PostgreSQLDesmistificando Replicação no PostgreSQL
Desmistificando Replicação no PostgreSQLEuler Taveira
 
Lessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkLessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkSergey Petrunya
 
mysql 8.0 architecture and enhancement
mysql 8.0 architecture and enhancementmysql 8.0 architecture and enhancement
mysql 8.0 architecture and enhancementlalit choudhary
 
Introduction to CUDA
Introduction to CUDAIntroduction to CUDA
Introduction to CUDARaymond Tay
 
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Opersys inc.
 

What's hot (20)

MySQL Storage Engines
MySQL Storage EnginesMySQL Storage Engines
MySQL Storage Engines
 
Kernel Recipes 2015: Kernel packet capture technologies
Kernel Recipes 2015: Kernel packet capture technologiesKernel Recipes 2015: Kernel packet capture technologies
Kernel Recipes 2015: Kernel packet capture technologies
 
Andes open cl for RISC-V
Andes open cl for RISC-VAndes open cl for RISC-V
Andes open cl for RISC-V
 
Understanding the Android System Server
Understanding the Android System ServerUnderstanding the Android System Server
Understanding the Android System Server
 
Berkeley Packet Filters
Berkeley Packet FiltersBerkeley Packet Filters
Berkeley Packet Filters
 
XPDDS17: Shared Virtual Memory Virtualization Implementation on Xen - Yi Liu,...
XPDDS17: Shared Virtual Memory Virtualization Implementation on Xen - Yi Liu,...XPDDS17: Shared Virtual Memory Virtualization Implementation on Xen - Yi Liu,...
XPDDS17: Shared Virtual Memory Virtualization Implementation on Xen - Yi Liu,...
 
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...
 
Histogram-in-Parallel-universe-of-MySQL-and-MariaDB
Histogram-in-Parallel-universe-of-MySQL-and-MariaDBHistogram-in-Parallel-universe-of-MySQL-and-MariaDB
Histogram-in-Parallel-universe-of-MySQL-and-MariaDB
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Secret of Intel Management Engine by Igor Skochinsky
Secret of Intel Management Engine  by Igor SkochinskySecret of Intel Management Engine  by Igor Skochinsky
Secret of Intel Management Engine by Igor Skochinsky
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
 
Introduction to OpenCL
Introduction to OpenCLIntroduction to OpenCL
Introduction to OpenCL
 
Gpfs introandsetup
Gpfs introandsetupGpfs introandsetup
Gpfs introandsetup
 
Introduction to GPU Programming
Introduction to GPU ProgrammingIntroduction to GPU Programming
Introduction to GPU Programming
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
 
Desmistificando Replicação no PostgreSQL
Desmistificando Replicação no PostgreSQLDesmistificando Replicação no PostgreSQL
Desmistificando Replicação no PostgreSQL
 
Lessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkLessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmark
 
mysql 8.0 architecture and enhancement
mysql 8.0 architecture and enhancementmysql 8.0 architecture and enhancement
mysql 8.0 architecture and enhancement
 
Introduction to CUDA
Introduction to CUDAIntroduction to CUDA
Introduction to CUDA
 
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
 

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

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 

Recently uploaded (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 

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