|
Added
Link Here
|
| 1 |
# Memory Allocation Patch Reference |
| 2 |
|
| 3 |
This document provides a detailed reference for each memory allocation patch in the FreeBSD Zen Browser port. |
| 4 |
|
| 5 |
## Patch Overview |
| 6 |
|
| 7 |
| Patch File | Target | Purpose | |
| 8 |
|------------|--------|---------| |
| 9 |
| `patch-memory_build_Fallback.cpp` | `memory/build/Fallback.cpp` | Include malloc_np.h and define HAVE_MEMALIGN | |
| 10 |
| `patch-memory_build_mozjemalloc.cpp` | `memory/build/mozjemalloc.cpp` | Remove noexcept(true) and disable RTLD_DEEPBIND | |
| 11 |
| `patch-memory_build_mozmemory.h` | `memory/build/mozmemory.h` | Conditional noexcept removal | |
| 12 |
| `patch-memory_build_mozmemory__wrap.cpp` | `memory/build/mozmemory_wrap.cpp` | Adapt wrapper declarations | |
| 13 |
| `patch-memory_mozalloc_mozalloc.cpp` | `memory/mozalloc/mozalloc.cpp` | Provide memalign() for non-jemalloc builds | |
| 14 |
| `patch-memory_mozalloc_mozalloc.h` | `memory/mozalloc/mozalloc.h` | Remove noexcept from mozalloc API | |
| 15 |
|
| 16 |
## Detailed Patch Analysis |
| 17 |
|
| 18 |
### patch-memory_build_Fallback.cpp |
| 19 |
|
| 20 |
**Purpose:** Enable the fallback memory allocator to work on FreeBSD |
| 21 |
|
| 22 |
**Changes:** |
| 23 |
```cpp |
| 24 |
+#ifdef __FreeBSD__ |
| 25 |
+# include <malloc_np.h> |
| 26 |
+# define HAVE_MEMALIGN 1 |
| 27 |
+#endif |
| 28 |
``` |
| 29 |
|
| 30 |
**Rationale:** |
| 31 |
- Includes FreeBSD's non-portable malloc extensions (`malloc_np.h`) |
| 32 |
- Defines `HAVE_MEMALIGN` to indicate memalign is available (via our wrapper) |
| 33 |
- Used when system allocator is preferred over jemalloc |
| 34 |
|
| 35 |
### patch-memory_build_mozjemalloc.cpp |
| 36 |
|
| 37 |
**Purpose:** Adapt Mozilla's jemalloc for FreeBSD |
| 38 |
|
| 39 |
**Changes:** |
| 40 |
1. Remove `noexcept(true)` from malloc declarations: |
| 41 |
```cpp |
| 42 |
+#if defined(__FreeBSD__) |
| 43 |
+#define NOTHROW_MALLOC_DECL(...) \ |
| 44 |
+ MOZ_MEMORY_API MACRO_CALL(GENERIC_MALLOC_DECL, (, __VA_ARGS__)) |
| 45 |
+#else |
| 46 |
#define NOTHROW_MALLOC_DECL(...) \ |
| 47 |
MOZ_MEMORY_API MACRO_CALL(GENERIC_MALLOC_DECL, (noexcept(true), __VA_ARGS__)) |
| 48 |
+#endif |
| 49 |
``` |
| 50 |
|
| 51 |
2. Disable RTLD_DEEPBIND usage: |
| 52 |
```cpp |
| 53 |
-#elif defined(RTLD_DEEPBIND) |
| 54 |
+#elif defined(RTLD_DEEPBIND) && !defined(__FreeBSD__) |
| 55 |
``` |
| 56 |
|
| 57 |
**Rationale:** |
| 58 |
- FreeBSD's C++ ABI handles exception specifications differently |
| 59 |
- `noexcept(true)` can cause ABI compatibility issues |
| 60 |
- `RTLD_DEEPBIND` is not available on FreeBSD |
| 61 |
- These changes allow jemalloc to compile without ABI mismatches |
| 62 |
|
| 63 |
### patch-memory_build_mozmemory.h |
| 64 |
|
| 65 |
**Purpose:** Adapt memory API declarations for FreeBSD |
| 66 |
|
| 67 |
**Changes:** |
| 68 |
```cpp |
| 69 |
+#if defined(__FreeBSD__) |
| 70 |
+#define NOTHROW_MALLOC_DECL(name, return_type, ...) \ |
| 71 |
+ MOZ_JEMALLOC_API return_type name(__VA_ARGS__); |
| 72 |
+#else |
| 73 |
#define NOTHROW_MALLOC_DECL(name, return_type, ...) \ |
| 74 |
MOZ_JEMALLOC_API return_type name(__VA_ARGS__) noexcept(true); |
| 75 |
+#endif |
| 76 |
``` |
| 77 |
|
| 78 |
**Rationale:** |
| 79 |
- Removes `noexcept(true)` from public API declarations on FreeBSD |
| 80 |
- Ensures API declarations match implementation (from mozjemalloc.cpp) |
| 81 |
- Prevents linker errors due to name mangling differences |
| 82 |
|
| 83 |
### patch-memory_build_mozmemory__wrap.cpp |
| 84 |
|
| 85 |
**Purpose:** Adapt memory function wrappers for FreeBSD |
| 86 |
|
| 87 |
**Changes:** |
| 88 |
```cpp |
| 89 |
+#if defined(__FreeBSD__) |
| 90 |
+#define NOTHROW_MALLOC_DECL(name, return_type, ...) \ |
| 91 |
+ MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__); |
| 92 |
+#else |
| 93 |
#define NOTHROW_MALLOC_DECL(name, return_type, ...) \ |
| 94 |
MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__) noexcept(true); |
| 95 |
+#endif |
| 96 |
``` |
| 97 |
|
| 98 |
**Rationale:** |
| 99 |
- Wrapper functions use `_impl` suffix internally |
| 100 |
- Must match exception specifications with actual implementations |
| 101 |
- Prevents wrapper/implementation ABI mismatches |
| 102 |
|
| 103 |
### patch-memory_mozalloc_mozalloc.cpp |
| 104 |
|
| 105 |
**Purpose:** Provide memalign() for builds without jemalloc |
| 106 |
|
| 107 |
**Changes:** |
| 108 |
```cpp |
| 109 |
+# if defined(__FreeBSD__) |
| 110 |
+# include <malloc_np.h> |
| 111 |
+extern "C" void* memalign(size_t boundary, size_t size) { |
| 112 |
+ void* ptr = nullptr; |
| 113 |
+ if (posix_memalign(&ptr, boundary, size) != 0) { |
| 114 |
+ return nullptr; |
| 115 |
+ } |
| 116 |
+ return ptr; |
| 117 |
+} |
| 118 |
+# endif |
| 119 |
``` |
| 120 |
|
| 121 |
**Rationale:** |
| 122 |
- When `MOZ_MEMORY` is not defined (jemalloc disabled), need system memalign |
| 123 |
- FreeBSD doesn't have native memalign(), so we provide a wrapper |
| 124 |
- Uses `posix_memalign()` which is POSIX standard and available on FreeBSD |
| 125 |
- Includes `malloc_np.h` for other memory functions needed by mozalloc |
| 126 |
|
| 127 |
### patch-memory_mozalloc_mozalloc.h |
| 128 |
|
| 129 |
**Purpose:** Adapt mozalloc API declarations for FreeBSD |
| 130 |
|
| 131 |
**Changes:** |
| 132 |
```cpp |
| 133 |
+# if defined(__FreeBSD__) |
| 134 |
+# define NOTHROW_MALLOC_DECL(name, return_type, ...) \ |
| 135 |
+ MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__); |
| 136 |
+# else |
| 137 |
# define NOTHROW_MALLOC_DECL(name, return_type, ...) \ |
| 138 |
MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__) noexcept(true); |
| 139 |
+# endif |
| 140 |
``` |
| 141 |
|
| 142 |
**Rationale:** |
| 143 |
- Ensures mozalloc header declarations match implementation |
| 144 |
- Removes `noexcept(true)` to match FreeBSD ABI requirements |
| 145 |
- Used by code that includes mozalloc.h |
| 146 |
|
| 147 |
## Patch Application Order |
| 148 |
|
| 149 |
Patches are applied in filename alphabetical order by the Makefile: |
| 150 |
|
| 151 |
1. Fallback.cpp - Sets up fallback allocator |
| 152 |
2. mozjemalloc.cpp - Core jemalloc implementation |
| 153 |
3. mozmemory.h - API declarations |
| 154 |
4. mozmemory_wrap.cpp - Wrapper implementations |
| 155 |
5. mozalloc_mozalloc.cpp - Alternative allocator implementation |
| 156 |
6. mozalloc_mozalloc.h - Alternative allocator API |
| 157 |
|
| 158 |
The order generally doesn't matter since patches target different files, but this ordering is logical (core → API → wrappers → alternatives). |
| 159 |
|
| 160 |
## Testing Patch Application |
| 161 |
|
| 162 |
Use the provided verification script: |
| 163 |
|
| 164 |
```bash |
| 165 |
cd files |
| 166 |
./verify_patches.sh |
| 167 |
``` |
| 168 |
|
| 169 |
This checks: |
| 170 |
- Proper patch format (unified diff) |
| 171 |
- No build artifacts in patches |
| 172 |
- Presence of diff hunks |
| 173 |
|
| 174 |
## Common Issues and Solutions |
| 175 |
|
| 176 |
### Issue: Patch Fails to Apply |
| 177 |
|
| 178 |
**Symptom:** `patch: malformed patch at line N` |
| 179 |
|
| 180 |
**Solution:** |
| 181 |
- Check that patch has proper `---` and `+++` headers |
| 182 |
- Ensure patch is in unified diff format (`diff -u` or `git diff`) |
| 183 |
- Verify line endings are Unix-style (LF, not CRLF) |
| 184 |
- Check context lines match the source file |
| 185 |
|
| 186 |
### Issue: Build Errors After Patching |
| 187 |
|
| 188 |
**Symptom:** Compilation fails with undefined symbols |
| 189 |
|
| 190 |
**Solution:** |
| 191 |
- Ensure wrapper headers (malloc.h, etc.) are in place |
| 192 |
- Verify `CPPFLAGS += -I${FILESDIR}` in Makefile |
| 193 |
- Check that all memory patches are applied |
| 194 |
- Confirm FreeBSD system has malloc_np.h |
| 195 |
|
| 196 |
### Issue: Runtime Memory Corruption |
| 197 |
|
| 198 |
**Symptom:** Segfaults or malloc errors during execution |
| 199 |
|
| 200 |
**Solution:** |
| 201 |
- Verify memalign() wrapper correctly implements alignment |
| 202 |
- Check that malloc_usable_size() is from malloc_np.h |
| 203 |
- Test with `test_malloc_wrapper.c` |
| 204 |
- Run with malloc debugging: `MALLOC_OPTIONS=AJ ./zen-bin` |
| 205 |
|
| 206 |
## Version Compatibility |
| 207 |
|
| 208 |
These patches are designed for: |
| 209 |
- FreeBSD 13.x and later |
| 210 |
- Mozilla/Gecko-based browsers (Firefox 100+) |
| 211 |
- Zen Browser 1.17.x |
| 212 |
|
| 213 |
Adjustments may be needed for: |
| 214 |
- Older FreeBSD versions (pre-13.0) |
| 215 |
- Significantly different Mozilla versions |
| 216 |
- Other BSD variants (OpenBSD, NetBSD, DragonflyBSD) |
| 217 |
|
| 218 |
## Contributing Patches |
| 219 |
|
| 220 |
When creating new memory-related patches: |
| 221 |
|
| 222 |
1. **Identify the issue** - Understand what fails and why |
| 223 |
2. **Minimal changes** - Only modify what's necessary |
| 224 |
3. **Test thoroughly** - Build and run tests |
| 225 |
4. **Document** - Explain the rationale |
| 226 |
5. **Follow format** - Use unified diff with `-p0` format |
| 227 |
|
| 228 |
Example patch creation: |
| 229 |
```bash |
| 230 |
cd work/ |
| 231 |
cp path/to/file.cpp path/to/file.cpp.orig |
| 232 |
# Edit file.cpp |
| 233 |
diff -u path/to/file.cpp.orig path/to/file.cpp > ../files/patch-path_to_file.cpp |
| 234 |
``` |
| 235 |
|
| 236 |
## See Also |
| 237 |
|
| 238 |
- [MEMORY_ALLOCATION.md](../MEMORY_ALLOCATION.md) - High-level architecture |
| 239 |
- [README.md](README.md) - Wrapper headers documentation |
| 240 |
- [FreeBSD Porter's Handbook](https://docs.freebsd.org/en/books/porters-handbook/) |