Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ packages/react-native/ReactAndroid/src/main/jni/third-party/**
packages/react-native/ReactCommon/**/platform/windows/third-party/**
packages/react-native/ReactCommon/jsi/jsi/**
packages/react-native/ReactCommon/yoga/**
scripts/abi-check/__tests__/__fixtures__/**
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ vendor/
/packages/react-native/third-party/
fix_*.patch
*.xcframework
!/scripts/abi-check/__tests__/__fixtures__/**/*.xcframework

# Visual Studio Code (config dir - if present, this merges user defined
# workspace settings on top of react-native.code-workspace)
Expand Down
1 change: 1 addition & 0 deletions __docs__/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ TODO: Explain the different components of React Native at a high level.
- Android
- iOS
- C++
- [C++ ABI check](../scripts/abi-check/__docs__/README.md)
- JavaScript
- Flow
- TypeScript
Expand Down
174 changes: 174 additions & 0 deletions scripts/abi-check/__docs__/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# C++ ABI check

[🏠 Home](../../../__docs__/README.md)

The scripts in `scripts/abi-check/` find ABI breaks in the C++ stable API of
React Native. An ABI break is a change in the size or the memory layout of a
public type, in the order of its virtual methods, or in the values of its
enumerators. Code that was compiled against the earlier release then reads the
wrong memory or calls the wrong function, even though it still compiles against
the new headers.

The API snapshots in `scripts/cxx-api` and `scripts/js-api` record declarations,
not layouts, so they do not find these changes. For example, between 0.87 and
0.88 `facebook::yoga::Style` shrank from 280 to 160 bytes. This changed the
layout of `YogaStylableProps` and `BaseViewProps`, and no existing check
reported it.

The check compiles the stable API headers of two versions with clang, dumps the
layouts of their types, and compares the dumps. The base is always a version
published on Maven. The other side is another published version or a checkout of
this repository.

## 🚀 Usage

```bash
node scripts/abi-check/cli.js --platform ios --build-type release \
--base 0.88.0 --current .
```

| Flag | Value |
| -------------- | ------------------------------------------------------------------------------------------------------------------- |
| `--platform` | `ios` or `android`. |
| `--build-type` | `debug` or `release`. |
| `--base` | The published version to compare with, such as `0.88.0`. |
| `--current` | The root folder of a checkout, if that folder exists. Otherwise a published version. |
| `--cache-dir` | Where downloaded artifacts are kept. The default is `react-native-abi-check` in the temporary folder of the system. |
| `--json` | Prints the result as JSON instead of text. |

Each platform and build type is a separate ABI, so one run checks one pair.

The exit status is:

- `0`: no ABI breaks.
- `1`: one or more ABI breaks. The output lists each changed or removed type.
- `2`: bad arguments, or a tool error, such as a failed download or a header
that does not compile.

Requirements:

- iOS needs Xcode. The check finds the `iphoneos` SDK with `xcrun`.
- Android needs the Android NDK. The check uses `ANDROID_NDK_HOME`. Otherwise it
uses the newest NDK in the `ndk` folder of the Android SDK, from
`ANDROID_HOME`, `ANDROID_SDK_ROOT` or `~/Library/Android/sdk`.

To run the unit tests:

```bash
yarn jest --no-cache -i scripts/abi-check
```

## 📐 Design

A run has these stages: select the headers, lay out each side, dump the layouts
with clang, and compare the dumps.

### Header selection (`tiers.js`)

A header is in the stable API when the first `react/cxxstableapi/*Guard.h` it
includes is `UmbrellaGuard.h` or `FrameworksGuard.h`. This rule is a copy of the
one in `scripts/cxx-api/parser/tiers.py`. The check then removes the headers
that the C++ API snapshot excludes for the platform. The exclude patterns are a
copy of `_TEMPLATE_EXCLUDE_PATTERNS` in `scripts/cxx-api/parser/__main__.py` and
of the lists in `scripts/cxx-api/config.yml`. Keep all three in sync.

A header is identified by its include path, the path written in
`#include <...>`. So a header gets the same verdict in the source tree and in a
published artifact.

### Header layouts (`layout.js`)

A header layout is the data that clang needs for one side: the `-I` folders, the
`-F` folders, and a map from include path to file. When two layouts are merged,
a header that is in both comes from the first, as clang finds it.

### Published baseline (`artifacts.js`)

The check downloads the headers of a published version from Maven Central:

- iOS: the `reactnative-core` and `reactnative-dependencies-headers` tarballs of
`react-native-artifacts`, for the build type.
- Android: the `react-android` AAR, whose prefab modules hold the headers, and
the `fbjni` AAR at the version that the `react-android` POM names.

Each archive is kept in the cache folder. It gets its cache name only after its
SHA-1 matches the checksum that Maven lists. The headers are extracted next to
the archive once.

### Checkout side (`source.js`)

For a checkout, the check builds the layout that the published artifacts would
have:

- iOS: it writes the headers as the iOS prebuild spec plans them. It always uses
the prebuild scripts of this repository, whatever the version of the checkout.
- Android: it copies the headers by the prefab rules in
`ReactAndroid/build.gradle.kts`. It adds the `fbjni` headers at the version in
the Gradle version catalog.

A checkout has no third-party, codegen or Hermes headers. Clang finds them in
the base artifacts, after the headers of the checkout.

### Dumping (`dump.js`)

The check compiles one translation unit per side with the flags of React
Native's own builds for the platform and build type. Clang dumps:

- the record layout of each type: size, alignment, and field offsets,
- the vtable of each polymorphic class. Clang lays out a vtable only when the
translation unit uses it, so a second run adds code that uses each one,
- the enumerator values of each enum.

Only types in `facebook::react`, `facebook::yoga` and `facebook::jsi` are kept.

### Comparison (`check.js`, `diff.js`)

Both sides are dumped with the union of their stable API headers. A header that
only one side marks as stable is still dumped on both sides, so a change of tier
alone is not reported. Each side dumps only the headers that it has.

A type whose dump changed or that was removed is an ABI break. A type that was
added is not a break. The result counts added types but does not list them.

### Skip list (`skipped-headers.js`)

Some stable API headers do not compile in a published layout. The skip list
names each one with its platform and a reason. An entry can also name the
versions it applies to. It then applies when the base or the current version is
one of them. Without versions, it applies to all comparisons.

The check removes the skipped headers from both sides and prints their names.
Any other header that does not compile stops the run with exit status `2`.

To add an entry, add the header, the platform and the reason to
`SKIPPED_HEADERS`. Give `versions` when only some releases are affected. To
remove an entry, make sure that its cause is fixed in every version the check
compares with, then delete it.

## ⚠️ Known limits

- The check does not run in CI yet.
- It does not check exported symbols, or the Java and Kotlin API.
- Some published iOS headers do not compile alone, because the prebuilt package
flattens their subfolders. These headers are on the skip list. PR
[#60295](https://github.com/react/react-native/pull/60295) fixes the cause.

## 🔗 Relationship with other systems

### Part of

- Testing / C++ in the [root documentation](../../../__docs__/README.md).

### Used by this

- `packages/react-native/scripts/spm/download-spm-artifacts`: downloads the
Maven artifacts, with retries.
- [iOS prebuild](../../../packages/react-native/scripts/ios-prebuild/__docs__/README.md):
its headers spec gives the header layout of an iOS checkout.
- `scripts/cxx-api`: `parser/tiers.py` holds the stable API rule, and
`parser/__main__.py` and `config.yml` hold the exclude patterns. `tiers.js`
copies all three.

### Uses this

- Nothing yet. A CI workflow is a planned follow-up.
117 changes: 117 additions & 0 deletions scripts/abi-check/__tests__/__fixtures__/clang-layout-dump.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

*** Dumping AST Record Layout
0 | struct __NSConstantString_tag
0 | const int * isa
8 | int flags
16 | const char * str
24 | long length
| [sizeof=32, dsize=32, align=8,
| nvsize=32, nvalign=8]

*** Dumping AST Record Layout
0 | union facebook::react::Point::(unnamed at /Users/ci/react-native-0.87.1/Headers/react/Sample.h:3:33)
0 | int i
0 | float f
| [sizeof=4, dsize=4, align=4,
| nvsize=4, nvalign=4]

*** Dumping AST Record Layout
0 | struct facebook::react::Point
0 | int x
8 | double y
16 | union facebook::react::Point::(unnamed at /Users/ci/react-native-0.87.1/Headers/react/Sample.h:3:33) u
16 | int i
16 | float f
| [sizeof=24, dsize=24, align=8,
| nvsize=24, nvalign=8]

*** Dumping AST Record Layout
0 | class facebook::react::Shape
0 | (Shape vtable pointer)
8 | int id
| [sizeof=16, dsize=12, align=8,
| nvsize=12, nvalign=8]

*** Dumping AST Record Layout
0 | struct other::Outside
0 | int a
| [sizeof=4, dsize=4, align=4,
| nvsize=4, nvalign=4]

*** Dumping AST Record Layout
0 | class other::Poly
0 | (Poly vtable pointer)
| [sizeof=8, dsize=8, align=8,
| nvsize=8, nvalign=8]

*** Dumping IRgen Record Layout
Record: CXXRecordDecl 0x784f230700 </Users/ci/react-native-0.87.1/Headers/react/Sample.h:4:1, col:78> col:7 referenced class Shape definition
|-DefinitionData polymorphic has_constexpr_non_copy_move_ctor
| |-DefaultConstructor exists non_trivial constexpr needs_implicit defaulted_is_constexpr
| |-CopyConstructor simple non_trivial has_const_param needs_implicit implicit_has_const_param
| |-MoveConstructor
| |-CopyAssignment simple non_trivial has_const_param implicit_has_const_param
| |-MoveAssignment
| `-Destructor non_trivial user_declared
|-CXXRecordDecl 0x784f230818 <col:1, col:7> col:7 implicit referenced class Shape
|-AccessSpecDecl 0x784f2308a8 <col:15, col:21> col:15 public
|-CXXDestructorDecl 0x784f2309b0 <col:23, col:38> col:31 used ~Shape 'void () noexcept' virtual
|-CXXMethodDecl 0x784f230b38 <col:41, col:63> col:56 area 'double () const' virtual
|-FieldDecl 0x784f230bf8 <col:70, col:74> col:74 id 'int'
`-CXXMethodDecl 0x784f230d10 <col:7> col:7 implicit constexpr operator= 'Shape &(const Shape &)' inline default noexcept-unevaluated 0x784f230d10
`-ParmVarDecl 0x784f230e40 <col:7> col:7 'const Shape &'

Layout: <CGRecordLayout
LLVMType:%"class.facebook::react::Shape" = type <{ ptr, i32, [4 x i8] }>
NonVirtualBaseLLVMType:%"class.facebook::react::Shape.base" = type <{ ptr, i32 }>
IsZeroInitializable:1
BitFields:[
]>
Original map
Vtable for 'facebook::react::Shape' (5 entries).
0 | offset_to_top (0)
1 | facebook::react::Shape RTTI
-- (facebook::react::Shape, 0) vtable address --
2 | facebook::react::Shape::~Shape() [complete]
3 | facebook::react::Shape::~Shape() [deleting]
4 | double facebook::react::Shape::area() const

VTable indices for 'facebook::react::Shape' (3 entries).
0 | facebook::react::Shape::~Shape() [complete]
1 | facebook::react::Shape::~Shape() [deleting]
2 | double facebook::react::Shape::area() const


*** Dumping IRgen Record Layout
Record: CXXRecordDecl 0x784f233550 </Users/ci/react-native-0.87.1/Headers/react/Sample.h:7:46, col:84> col:52 referenced class Poly definition
|-DefinitionData polymorphic has_constexpr_non_copy_move_ctor can_const_default_init
| |-DefaultConstructor exists non_trivial constexpr needs_implicit defaulted_is_constexpr
| |-CopyConstructor simple non_trivial has_const_param needs_implicit implicit_has_const_param
| |-MoveConstructor
| |-CopyAssignment simple non_trivial has_const_param implicit_has_const_param
| |-MoveAssignment
| `-Destructor non_trivial user_declared
|-CXXRecordDecl 0x784f233668 <col:46, col:52> col:52 implicit referenced class Poly
|-AccessSpecDecl 0x784f2336f8 <col:59, col:65> col:59 public
|-CXXDestructorDecl 0x784f2337c8 <col:67, col:81> col:75 used ~Poly 'void () noexcept' virtual
`-CXXMethodDecl 0x784f233980 <col:52> col:52 implicit constexpr operator= 'Poly &(const Poly &)' inline default noexcept-unevaluated 0x784f233980
`-ParmVarDecl 0x784f233ab0 <col:52> col:52 'const Poly &'

Layout: <CGRecordLayout
LLVMType:%"class.other::Poly" = type { ptr }
NonVirtualBaseLLVMType:%"class.other::Poly" = type { ptr }
IsZeroInitializable:1
BitFields:[
]>
Original map
Vtable for 'other::Poly' (4 entries).
0 | offset_to_top (0)
1 | other::Poly RTTI
-- (other::Poly, 0) vtable address --
2 | other::Poly::~Poly() [complete]
3 | other::Poly::~Poly() [deleting]

VTable indices for 'other::Poly' (2 entries).
0 | other::Poly::~Poly() [complete]
1 | other::Poly::~Poly() [deleting]

10 changes: 10 additions & 0 deletions scripts/abi-check/__tests__/__fixtures__/dump/Inner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include "NotThere.h"
10 changes: 10 additions & 0 deletions scripts/abi-check/__tests__/__fixtures__/dump/Outer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include "Inner.h"
Loading
Loading