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

Similar to Sharing C++ objects in Linux

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
 
Learning Apache HIVE - Data Warehouse and Query Language for Hadoop
Learning Apache HIVE - Data Warehouse and Query Language for HadoopLearning Apache HIVE - Data Warehouse and Query Language for Hadoop
Learning Apache HIVE - Data Warehouse and Query Language for HadoopSomeshwar Kale
 
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 MongoDBAntonios Giannopoulos
 
Scaling CouchDB with BigCouch
Scaling CouchDB with BigCouchScaling CouchDB with BigCouch
Scaling CouchDB with BigCouchCloudant
 

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

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
 
Learning Apache HIVE - Data Warehouse and Query Language for Hadoop
Learning Apache HIVE - Data Warehouse and Query Language for HadoopLearning Apache HIVE - Data Warehouse and Query Language for Hadoop
Learning Apache HIVE - Data Warehouse and Query Language for Hadoop
 
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
 
Scaling CouchDB with BigCouch
Scaling CouchDB with BigCouchScaling CouchDB with BigCouch
Scaling CouchDB with BigCouch
 

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

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 

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