xref: /aosp_15_r20/external/cronet/base/allocator/partition_allocator/src/partition_alloc/BUILD.gn (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2022 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/buildflag_header.gni")
6import("//build/config/android/config.gni")
7import("//build/config/cast.gni")
8import("//build/config/chromeos/ui_mode.gni")
9import("//build/config/compiler/compiler.gni")
10import("//build/config/dcheck_always_on.gni")
11import("//build/config/logging.gni")
12import("../../partition_alloc.gni")
13
14# Add partition_alloc.gni and import it for partition_alloc configs.
15
16# TODO(https://crbug.com/1467773): Split PartitionAlloc into a public and
17# private parts. The public config would include add the "./include" dir and
18# the private config would add the "./src" dir.
19# TODO(https://crbug.com/1467773): Move this config and several target into
20# "../..".
21config("public_includes") {
22  include_dirs = [
23    "..",
24    "$root_gen_dir/" + rebase_path("..", "//"),
25  ]
26}
27
28# Enable more warnings that were found when using PartitionAlloc in other
29# projects.
30#
31# This list was initially copied from Dawn, who gathered this list from its own
32# dependants.
33config("dependants_extra_warnings") {
34  # Add them only when building PartitionAlloc as part of Chrome, because we
35  # control which clang version we use. Otherwise we risk breaking dependants
36  # when they use a different clang version.
37  #
38  # Fuchsia has been excluded from the extra warnings: Dependency over
39  # fuchsia.kernel involves too many warning. This is not a real issue, because
40  # the header is only used by PartitionAlloc internally. The dependants do not
41  # include it transitively.
42  if (build_with_chromium && is_clang && !is_fuchsia) {
43    cflags = [
44      "-Wc++11-narrowing",
45      "-Wconditional-uninitialized",
46      "-Wcstring-format-directive",
47      "-Wctad-maybe-unsupported",
48      "-Wdeprecated-copy",
49      "-Wdeprecated-copy-dtor",
50      "-Wduplicate-enum",
51      "-Wextra-semi",
52      "-Wextra-semi-stmt",
53      "-Wimplicit-fallthrough",
54      "-Winconsistent-missing-destructor-override",
55      "-Winvalid-offsetof",
56      "-Wmissing-field-initializers",
57      "-Wnon-c-typedef-for-linkage",
58      "-Wpessimizing-move",
59      "-Wrange-loop-analysis",
60      "-Wredundant-move",
61      "-Wshadow-field",
62      "-Wstrict-prototypes",
63      "-Wsuggest-destructor-override",
64      "-Wsuggest-override",
65      "-Wtautological-unsigned-zero-compare",
66      "-Wunreachable-code-aggressive",
67      "-Wunused-but-set-variable",
68      "-Wunused-macros",
69    ]
70
71    # clang-cl doesn't know -pedantic, pass it explicitly to the clang driver
72    if (is_win) {
73      cflags += [ "/clang:-pedantic" ]
74    } else {
75      cflags += [ "-pedantic" ]
76    }
77  }
78}
79
80_remove_configs = []
81_add_configs = []
82if (!is_debug || partition_alloc_optimized_debug) {
83  _remove_configs += [ "//build/config/compiler:default_optimization" ]
84
85  # PartitionAlloc is relatively hot (>1% of cycles for users of CrOS).
86  # Use speed-focused optimizations for it.
87  _add_configs += [ "//build/config/compiler:optimize_speed" ]
88} else {
89  _remove_configs += [ "//build/config/compiler:default_optimization" ]
90  _add_configs += [ "//build/config/compiler:no_optimize" ]
91}
92
93component("raw_ptr") {
94  # `gn check` is unhappy with most `#includes` when PA isn't
95  # actually built.
96  check_includes = use_partition_alloc
97  public = [
98    "pointers/instance_tracer.h",
99    "pointers/raw_ptr.h",
100    "pointers/raw_ptr_cast.h",
101    "pointers/raw_ptr_exclusion.h",
102    "pointers/raw_ptr_noop_impl.h",
103    "pointers/raw_ref.h",
104  ]
105  sources = [ "pointers/instance_tracer.cc" ]
106  public_configs = [ ":public_includes" ]
107  configs += [ "//build/config/compiler:wexit_time_destructors" ]
108
109  if (enable_backup_ref_ptr_support) {
110    sources += [
111      "pointers/raw_ptr_backup_ref_impl.cc",
112      "pointers/raw_ptr_backup_ref_impl.h",
113    ]
114  } else if (use_hookable_raw_ptr) {
115    sources += [
116      "pointers/raw_ptr_hookable_impl.cc",
117      "pointers/raw_ptr_hookable_impl.h",
118    ]
119  } else if (use_asan_unowned_ptr) {
120    sources += [
121      "pointers/raw_ptr_asan_unowned_impl.cc",
122      "pointers/raw_ptr_asan_unowned_impl.h",
123    ]
124  } else {
125    sources += [ "pointers/raw_ptr_noop_impl.h" ]
126    sources += [ "pointers/empty.cc" ]
127  }
128  if (use_partition_alloc) {
129    public_deps = [ ":partition_alloc" ]
130  }
131  deps = [ ":buildflags" ]
132
133  # See also: `partition_alloc_base/component_export.h`
134  defines = [ "IS_RAW_PTR_IMPL" ]
135
136  configs -= _remove_configs
137  configs += _add_configs
138  configs += [ ":dependants_extra_warnings" ]
139}
140
141# Changes the freelist implementation to use pointer offsets in lieu
142# of full-on pointers. Defaults to false, which implies the use of
143# "encoded next" freelist entry.
144#
145# Only usable when pointers are 64-bit.
146use_freelist_pool_offsets = has_64_bit_pointers
147
148buildflag_header("partition_alloc_buildflags") {
149  header = "partition_alloc_buildflags.h"
150
151  _record_alloc_info = false
152
153  # GWP-ASan is tied to BRP's enablement.
154  _enable_gwp_asan_support = enable_backup_ref_ptr_support
155
156  # Pools are a logical concept when address space is 32-bit.
157  _glue_core_pools = glue_core_pools && has_64_bit_pointers
158
159  # Pointer compression requires 64-bit pointers.
160  _enable_pointer_compression =
161      enable_pointer_compression_support && has_64_bit_pointers
162
163  # TODO(crbug.com/1151236): Need to refactor the following buildflags.
164  # The buildflags (except RECORD_ALLOC_INFO) are used by both chrome and
165  # PartitionAlloc. For PartitionAlloc,
166  # gen/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_buildflags.h
167  # defines and PartitionAlloc includes the header file. For chrome,
168  # gen/base/allocator/buildflags.h defines and chrome includes.
169  flags = [
170    "HAS_64_BIT_POINTERS=$has_64_bit_pointers",
171    "HAS_MEMORY_TAGGING=$has_memory_tagging",
172
173    "USE_ALLOCATOR_SHIM=$use_allocator_shim",
174    "USE_LARGE_EMPTY_SLOT_SPAN_RING=$use_large_empty_slot_span_ring",
175    "USE_PARTITION_ALLOC=$use_partition_alloc",
176    "USE_PARTITION_ALLOC_AS_MALLOC=$use_partition_alloc_as_malloc",
177
178    "ENABLE_BACKUP_REF_PTR_SUPPORT=$enable_backup_ref_ptr_support",
179    "ENABLE_BACKUP_REF_PTR_SLOW_CHECKS=$enable_backup_ref_ptr_slow_checks",
180    "ENABLE_BACKUP_REF_PTR_FEATURE_FLAG=$enable_backup_ref_ptr_feature_flag",
181    "ENABLE_BACKUP_REF_PTR_INSTANCE_TRACER=$enable_backup_ref_ptr_instance_tracer",
182    "ENABLE_DANGLING_RAW_PTR_CHECKS=$enable_dangling_raw_ptr_checks",
183    "ENABLE_DANGLING_RAW_PTR_FEATURE_FLAG=$enable_dangling_raw_ptr_feature_flag",
184    "ENABLE_POINTER_SUBTRACTION_CHECK=$enable_pointer_subtraction_check",
185    "ENABLE_POINTER_ARITHMETIC_TRAIT_CHECK=$enable_pointer_arithmetic_trait_check",
186    "BACKUP_REF_PTR_EXTRA_OOB_CHECKS=$backup_ref_ptr_extra_oob_checks",
187    "BACKUP_REF_PTR_POISON_OOB_PTR=$backup_ref_ptr_poison_oob_ptr",
188    "USE_ASAN_BACKUP_REF_PTR=$use_asan_backup_ref_ptr",
189    "USE_ASAN_UNOWNED_PTR=$use_asan_unowned_ptr",
190    "USE_HOOKABLE_RAW_PTR=$use_hookable_raw_ptr",
191    "ENABLE_GWP_ASAN_SUPPORT=$_enable_gwp_asan_support",
192
193    "FORCE_ENABLE_RAW_PTR_EXCLUSION=$force_enable_raw_ptr_exclusion",
194
195    "USE_FULL_MTE=$use_full_mte",
196
197    "RECORD_ALLOC_INFO=$_record_alloc_info",
198    "USE_FREESLOT_BITMAP=$use_freeslot_bitmap",
199    "GLUE_CORE_POOLS=$_glue_core_pools",
200    "ENABLE_POINTER_COMPRESSION=$_enable_pointer_compression",
201    "ENABLE_SHADOW_METADATA_FOR_64_BITS_POINTERS=$enable_shadow_metadata",
202    "USE_FREELIST_POOL_OFFSETS=$use_freelist_pool_offsets",
203
204    "USE_STARSCAN=$use_starscan",
205
206    "STACK_SCAN_SUPPORTED=$stack_scan_supported",
207
208    "ENABLE_PKEYS=$enable_pkeys",
209    "ENABLE_THREAD_ISOLATION=$enable_pkeys",
210
211    "FORWARD_THROUGH_MALLOC=$forward_through_malloc",
212    "ASSERT_CPP_20=$assert_cpp20",
213  ]
214}
215
216buildflag_header("raw_ptr_buildflags") {
217  header = "raw_ptr_buildflags.h"
218
219  flags = [
220    "RAW_PTR_ZERO_ON_CONSTRUCT=$raw_ptr_zero_on_construct",
221    "RAW_PTR_ZERO_ON_MOVE=$raw_ptr_zero_on_move",
222    "RAW_PTR_ZERO_ON_DESTRUCT=$raw_ptr_zero_on_destruct",
223  ]
224}
225
226buildflag_header("chromecast_buildflags") {
227  header = "chromecast_buildflags.h"
228
229  flags = [
230    "PA_IS_CAST_ANDROID=$is_cast_android",
231    "PA_IS_CASTOS=$is_castos",
232  ]
233}
234
235buildflag_header("chromeos_buildflags") {
236  header = "chromeos_buildflags.h"
237
238  flags = [ "PA_IS_CHROMEOS_ASH=$is_chromeos_ash" ]
239}
240
241buildflag_header("debugging_buildflags") {
242  header = "debugging_buildflags.h"
243  header_dir = rebase_path(".", "//") + "/partition_alloc_base/debug"
244
245  # Duplicates the setup Chromium uses to define `DCHECK_IS_ON()`,
246  # but avails it as a buildflag.
247  _dcheck_is_on = is_debug || dcheck_always_on
248
249  flags = [
250    "PA_DCHECK_IS_ON=$_dcheck_is_on",
251    "PA_EXPENSIVE_DCHECKS_ARE_ON=$enable_expensive_dchecks",
252    "PA_DCHECK_IS_CONFIGURABLE=$dcheck_is_configurable",
253    "PA_CAN_UNWIND_WITH_FRAME_POINTERS=$can_unwind_with_frame_pointers",
254  ]
255}
256
257group("buildflags") {
258  public_deps = [
259    ":chromecast_buildflags",
260    ":chromeos_buildflags",
261    ":debugging_buildflags",
262    ":partition_alloc_buildflags",
263    ":raw_ptr_buildflags",
264  ]
265  public_configs = [ ":public_includes" ]
266}
267
268if (is_clang_or_gcc) {
269  config("partition_alloc_implementation") {
270    # See also: `partition_alloc_base/component_export.h`
271    defines = [ "IS_PARTITION_ALLOC_IMPL" ]
272  }
273
274  config("partition_alloc_base_implementation") {
275    # See also: `partition_alloc_base/component_export.h`
276    defines = [ "IS_PARTITION_ALLOC_BASE_IMPL" ]
277  }
278
279  config("allocator_shim_implementation") {
280    # See also: `partition_alloc_base/component_export.h`
281    defines = [ "IS_ALLOCATOR_SHIM_IMPL" ]
282  }
283
284  config("memory_tagging") {
285    if (current_cpu == "arm64" &&
286        (is_linux || is_chromeos || is_android || is_fuchsia)) {
287      # base/ has access to the MTE intrinsics because it needs to use them,
288      # but they're not backwards compatible. Use base::CPU::has_mte()
289      # beforehand to confirm or use indirect functions (ifuncs) to select
290      # an MTE-specific implementation at dynamic link-time.
291      cflags = [
292        "-Xclang",
293        "-target-feature",
294        "-Xclang",
295        "+mte",
296      ]
297    }
298  }
299
300  # Used to shim malloc symbols on Android. see //base/allocator/README.md.
301  config("wrap_malloc_symbols") {
302    ldflags = [
303      "-Wl,-wrap,calloc",
304      "-Wl,-wrap,free",
305      "-Wl,-wrap,malloc",
306      "-Wl,-wrap,memalign",
307      "-Wl,-wrap,posix_memalign",
308      "-Wl,-wrap,pvalloc",
309      "-Wl,-wrap,realloc",
310      "-Wl,-wrap,valloc",
311
312      # Not allocating memory, but part of the API
313      "-Wl,-wrap,malloc_usable_size",
314
315      # <cstdlib> functions
316      "-Wl,-wrap,realpath",
317
318      # <string.h> functions
319      "-Wl,-wrap,strdup",
320      "-Wl,-wrap,strndup",
321
322      # <unistd.h> functions
323      "-Wl,-wrap,getcwd",
324
325      # <cstdio> functions
326      "-Wl,-wrap,asprintf",
327      "-Wl,-wrap,vasprintf",
328    ]
329  }
330
331  config("mac_no_default_new_delete_symbols") {
332    if (!is_component_build) {
333      # This is already set when we compile libc++, see
334      # buildtools/third_party/libc++/BUILD.gn. But it needs to be set here as
335      # well, since the shim defines the symbols, to prevent them being exported.
336      cflags = [ "-fvisibility-global-new-delete=force-hidden" ]
337    }
338  }
339
340  if (is_fuchsia) {
341    config("fuchsia_sync_lib") {
342      libs = [
343        "sync",  # Used by spinning_mutex.h.
344      ]
345    }
346  }
347
348  if (enable_pkeys && is_debug) {
349    config("no_stack_protector") {
350      cflags = [ "-fno-stack-protector" ]
351    }
352  }
353
354  group("partition_alloc") {
355    public_deps = [
356      ":allocator_base",
357      ":allocator_core",
358      ":allocator_shim",
359    ]
360  }
361
362  component("allocator_core") {
363    visibility = [ ":*" ]
364
365    sources = [
366      "address_pool_manager.cc",
367      "address_pool_manager.h",
368      "address_pool_manager_bitmap.cc",
369      "address_pool_manager_bitmap.h",
370      "address_pool_manager_types.h",
371      "address_space_randomization.cc",
372      "address_space_randomization.h",
373      "address_space_stats.h",
374      "allocation_guard.cc",
375      "allocation_guard.h",
376      "compressed_pointer.cc",
377      "compressed_pointer.h",
378      "dangling_raw_ptr_checks.cc",
379      "dangling_raw_ptr_checks.h",
380      "flags.h",
381      "freeslot_bitmap.h",
382      "freeslot_bitmap_constants.h",
383      "gwp_asan_support.cc",
384      "gwp_asan_support.h",
385      "in_slot_metadata.h",
386      "internal_allocator.cc",
387      "internal_allocator.h",
388      "internal_allocator_forward.h",
389      "lightweight_quarantine.cc",
390      "lightweight_quarantine.h",
391      "memory_reclaimer.cc",
392      "memory_reclaimer.h",
393      "oom.cc",
394      "oom.h",
395      "oom_callback.cc",
396      "oom_callback.h",
397      "page_allocator.cc",
398      "page_allocator.h",
399      "page_allocator_constants.h",
400      "page_allocator_internal.h",
401      "partition_address_space.cc",
402      "partition_address_space.h",
403      "partition_alloc-inl.h",
404      "partition_alloc.cc",
405      "partition_alloc.h",
406      "partition_alloc_allocation_data.h",
407      "partition_alloc_check.h",
408      "partition_alloc_config.h",
409      "partition_alloc_constants.h",
410      "partition_alloc_forward.h",
411      "partition_alloc_hooks.cc",
412      "partition_alloc_hooks.h",
413      "partition_bucket.cc",
414      "partition_bucket.h",
415      "partition_bucket_lookup.h",
416      "partition_cookie.h",
417      "partition_dcheck_helper.cc",
418      "partition_dcheck_helper.h",
419      "partition_direct_map_extent.h",
420      "partition_freelist_entry.cc",
421      "partition_freelist_entry.h",
422      "partition_lock.h",
423      "partition_oom.cc",
424      "partition_oom.h",
425      "partition_page.cc",
426      "partition_page.h",
427      "partition_page_constants.h",
428      "partition_root.cc",
429      "partition_root.h",
430      "partition_stats.cc",
431      "partition_stats.h",
432      "partition_superpage_extent_entry.h",
433      "partition_tls.h",
434      "random.cc",
435      "random.h",
436      "reservation_offset_table.cc",
437      "reservation_offset_table.h",
438      "reverse_bytes.h",
439      "spinning_mutex.cc",
440      "spinning_mutex.h",
441      "stack/stack.cc",
442      "stack/stack.h",
443      "tagging.cc",
444      "tagging.h",
445      "thread_cache.cc",
446      "thread_cache.h",
447      "thread_isolation/alignment.h",
448      "thread_isolation/pkey.cc",
449      "thread_isolation/pkey.h",
450      "thread_isolation/thread_isolation.cc",
451      "thread_isolation/thread_isolation.h",
452      "yield_processor.h",
453    ]
454
455    if (use_starscan) {
456      sources += [
457        "starscan/logging.h",
458        "starscan/pcscan.cc",
459        "starscan/pcscan.h",
460        "starscan/pcscan_internal.cc",
461        "starscan/pcscan_internal.h",
462        "starscan/pcscan_scheduling.cc",
463        "starscan/pcscan_scheduling.h",
464        "starscan/raceful_worklist.h",
465        "starscan/scan_loop.h",
466        "starscan/snapshot.cc",
467        "starscan/snapshot.h",
468        "starscan/starscan_fwd.h",
469        "starscan/state_bitmap.h",
470        "starscan/stats_collector.cc",
471        "starscan/stats_collector.h",
472        "starscan/stats_reporter.h",
473        "starscan/write_protector.cc",
474        "starscan/write_protector.h",
475      ]
476    }
477
478    defines = []
479    if (is_win) {
480      sources += [
481        "page_allocator_internals_win.h",
482        "partition_tls_win.cc",
483      ]
484    } else if (is_posix) {
485      sources += [
486        "page_allocator_internals_posix.cc",
487        "page_allocator_internals_posix.h",
488      ]
489    } else if (is_fuchsia) {
490      sources += [ "page_allocator_internals_fuchsia.h" ]
491    }
492    if (is_android) {
493      # The Android NDK supports PR_MTE_* macros as of NDK r23.
494      defines += [ "HAS_PR_MTE_MACROS" ]
495    }
496    if (current_cpu == "x64") {
497      assert(stack_scan_supported)
498      sources += [ "stack/asm/x64/push_registers_asm.cc" ]
499    } else if (current_cpu == "x86") {
500      assert(stack_scan_supported)
501      sources += [ "stack/asm/x86/push_registers_asm.cc" ]
502    } else if (current_cpu == "arm") {
503      assert(stack_scan_supported)
504      sources += [ "stack/asm/arm/push_registers_asm.cc" ]
505    } else if (current_cpu == "arm64") {
506      assert(stack_scan_supported)
507      sources += [ "stack/asm/arm64/push_registers_asm.cc" ]
508    } else if (current_cpu == "riscv64") {
509      assert(stack_scan_supported)
510      sources += [ "stack/asm/riscv64/push_registers_asm.cc" ]
511    } else {
512      # To support a trampoline for another arch, please refer to v8/src/heap/base.
513      assert(!stack_scan_supported)
514    }
515    if (use_freelist_pool_offsets) {
516      sources += [ "pool_offset_freelist.h" ]
517    } else {
518      sources += [ "encoded_next_freelist.h" ]
519    }
520
521    public_deps = [
522      ":chromecast_buildflags",
523      ":chromeos_buildflags",
524      ":debugging_buildflags",
525      ":partition_alloc_buildflags",
526    ]
527
528    configs += [
529      ":partition_alloc_implementation",
530      ":memory_tagging",
531      "//build/config/compiler:wexit_time_destructors",
532    ]
533    deps = [ ":allocator_base" ]
534    public_configs = []
535    if (is_android) {
536      # tagging.cc requires __arm_mte_set_* functions.
537      deps += [ "//third_party/cpu_features:ndk_compat" ]
538    }
539    if (is_fuchsia) {
540      deps += [
541        "//third_party/fuchsia-sdk/sdk/fidl/fuchsia.kernel:fuchsia.kernel_cpp",
542        "//third_party/fuchsia-sdk/sdk/pkg/component_incoming_cpp",
543      ]
544      public_deps += [
545        "//third_party/fuchsia-sdk/sdk/pkg/sync",
546        "//third_party/fuchsia-sdk/sdk/pkg/zx",
547      ]
548
549      # Needed for users of spinning_mutex.h, which for performance reasons,
550      # contains inlined calls to `libsync` inside the header file.
551      # It appends an entry to the "libs" section of the dependent target.
552      public_configs += [ ":fuchsia_sync_lib" ]
553    }
554
555    frameworks = []
556    if (is_mac) {
557      # SecTaskGetCodeSignStatus needs:
558      frameworks += [ "Security.framework" ]
559    }
560
561    if (is_apple) {
562      frameworks += [
563        "CoreFoundation.framework",
564        "Foundation.framework",
565      ]
566    }
567
568    configs -= _remove_configs
569    configs += _add_configs
570    configs += [ ":dependants_extra_warnings" ]
571
572    # We want to be able to test pkey mode without access to the default pkey.
573    # This is incompatible with stack protectors since the TLS won't be pkey-tagged.
574    if (enable_pkeys && is_debug) {
575      configs += [ ":no_stack_protector" ]
576    }
577  }
578
579  component("allocator_base") {
580    visibility = [ ":*" ]
581
582    sources = [
583      "partition_alloc_base/atomic_ref_count.h",
584      "partition_alloc_base/augmentations/compiler_specific.h",
585      "partition_alloc_base/bit_cast.h",
586      "partition_alloc_base/bits.h",
587      "partition_alloc_base/check.cc",
588      "partition_alloc_base/check.h",
589      "partition_alloc_base/compiler_specific.h",
590      "partition_alloc_base/component_export.h",
591      "partition_alloc_base/cpu.cc",
592      "partition_alloc_base/cpu.h",
593      "partition_alloc_base/cxx20_is_constant_evaluated.h",
594      "partition_alloc_base/debug/alias.cc",
595      "partition_alloc_base/debug/alias.h",
596      "partition_alloc_base/debug/stack_trace.cc",
597      "partition_alloc_base/debug/stack_trace.h",
598      "partition_alloc_base/export_template.h",
599      "partition_alloc_base/immediate_crash.h",
600      "partition_alloc_base/log_message.cc",
601      "partition_alloc_base/log_message.h",
602      "partition_alloc_base/logging.cc",
603      "partition_alloc_base/logging.h",
604      "partition_alloc_base/memory/page_size.h",
605      "partition_alloc_base/memory/ref_counted.cc",
606      "partition_alloc_base/memory/ref_counted.h",
607      "partition_alloc_base/memory/scoped_policy.h",
608      "partition_alloc_base/memory/scoped_refptr.h",
609      "partition_alloc_base/no_destructor.h",
610      "partition_alloc_base/notreached.h",
611      "partition_alloc_base/numerics/checked_math.h",
612      "partition_alloc_base/numerics/checked_math_impl.h",
613      "partition_alloc_base/numerics/clamped_math.h",
614      "partition_alloc_base/numerics/clamped_math_impl.h",
615      "partition_alloc_base/numerics/safe_conversions.h",
616      "partition_alloc_base/numerics/safe_conversions_arm_impl.h",
617      "partition_alloc_base/numerics/safe_conversions_impl.h",
618      "partition_alloc_base/numerics/safe_math.h",
619      "partition_alloc_base/numerics/safe_math_arm_impl.h",
620      "partition_alloc_base/numerics/safe_math_clang_gcc_impl.h",
621      "partition_alloc_base/numerics/safe_math_shared_impl.h",
622      "partition_alloc_base/posix/eintr_wrapper.h",
623      "partition_alloc_base/process/process_handle.h",
624      "partition_alloc_base/rand_util.cc",
625      "partition_alloc_base/rand_util.h",
626      "partition_alloc_base/scoped_clear_last_error.h",
627      "partition_alloc_base/strings/cstring_builder.cc",
628      "partition_alloc_base/strings/cstring_builder.h",
629      "partition_alloc_base/strings/safe_sprintf.cc",
630      "partition_alloc_base/strings/safe_sprintf.h",
631      "partition_alloc_base/strings/string_util.cc",
632      "partition_alloc_base/strings/string_util.h",
633      "partition_alloc_base/strings/stringprintf.cc",
634      "partition_alloc_base/strings/stringprintf.h",
635      "partition_alloc_base/system/sys_info.h",
636      "partition_alloc_base/thread_annotations.h",
637      "partition_alloc_base/threading/platform_thread.cc",
638      "partition_alloc_base/threading/platform_thread.h",
639      "partition_alloc_base/threading/platform_thread_ref.h",
640      "partition_alloc_base/time/time.cc",
641      "partition_alloc_base/time/time.h",
642      "partition_alloc_base/time/time_override.cc",
643      "partition_alloc_base/time/time_override.h",
644      "partition_alloc_base/types/strong_alias.h",
645      "partition_alloc_base/win/win_handle_types.h",
646      "partition_alloc_base/win/win_handle_types_list.inc",
647      "partition_alloc_base/win/windows_types.h",
648    ]
649
650    if (is_win) {
651      sources += [
652        "partition_alloc_base/debug/stack_trace_win.cc",
653        "partition_alloc_base/memory/page_size_win.cc",
654        "partition_alloc_base/process/process_handle_win.cc",
655        "partition_alloc_base/rand_util_win.cc",
656        "partition_alloc_base/scoped_clear_last_error_win.cc",
657        "partition_alloc_base/threading/platform_thread_win.cc",
658        "partition_alloc_base/time/time_win.cc",
659      ]
660    } else if (is_posix) {
661      sources += [
662        "partition_alloc_base/debug/stack_trace_posix.cc",
663        "partition_alloc_base/files/file_util.h",
664        "partition_alloc_base/files/file_util_posix.cc",
665        "partition_alloc_base/memory/page_size_posix.cc",
666        "partition_alloc_base/posix/safe_strerror.cc",
667        "partition_alloc_base/posix/safe_strerror.h",
668        "partition_alloc_base/process/process_handle_posix.cc",
669        "partition_alloc_base/rand_util_posix.cc",
670        "partition_alloc_base/threading/platform_thread_internal_posix.h",
671        "partition_alloc_base/threading/platform_thread_posix.cc",
672        "partition_alloc_base/time/time_conversion_posix.cc",
673      ]
674
675      if (is_linux || is_chromeos) {
676        sources += [ "partition_alloc_base/debug/stack_trace_linux.cc" ]
677      }
678
679      if (is_android || is_chromeos_ash) {
680        sources += [ "partition_alloc_base/time/time_android.cc" ]
681      }
682      if (is_apple) {
683        # Request <dlfcn.h> to provide the `dladdr()` function. This is used to
684        # translate address to symbolic information.
685        defines = [ "HAVE_DLADDR" ]
686
687        sources += [
688          "partition_alloc_base/debug/stack_trace_mac.cc",
689          "partition_alloc_base/time/time_apple.mm",
690        ]
691      } else {
692        sources += [ "partition_alloc_base/time/time_now_posix.cc" ]
693      }
694    } else if (is_fuchsia) {
695      sources += [
696        "partition_alloc_base/fuchsia/fuchsia_logging.cc",
697        "partition_alloc_base/fuchsia/fuchsia_logging.h",
698        "partition_alloc_base/memory/page_size_posix.cc",
699        "partition_alloc_base/posix/safe_strerror.cc",
700        "partition_alloc_base/posix/safe_strerror.h",
701        "partition_alloc_base/rand_util_fuchsia.cc",
702        "partition_alloc_base/threading/platform_thread_internal_posix.h",
703        "partition_alloc_base/threading/platform_thread_posix.cc",
704        "partition_alloc_base/time/time_conversion_posix.cc",
705        "partition_alloc_base/time/time_fuchsia.cc",
706      ]
707    }
708    if (is_android) {
709      # Only android build requires native_library, and native_library depends
710      # on file_path. So file_path is added if is_android = true.
711      sources += [
712        "partition_alloc_base/debug/stack_trace_android.cc",
713        "partition_alloc_base/files/file_path.cc",
714        "partition_alloc_base/files/file_path.h",
715        "partition_alloc_base/native_library.cc",
716        "partition_alloc_base/native_library.h",
717        "partition_alloc_base/native_library_posix.cc",
718      ]
719    }
720    if (is_apple) {
721      # Apple-specific utilities
722      sources += [
723        "partition_alloc_base/apple/foundation_util.h",
724        "partition_alloc_base/apple/foundation_util.mm",
725        "partition_alloc_base/apple/mach_logging.cc",
726        "partition_alloc_base/apple/mach_logging.h",
727        "partition_alloc_base/apple/scoped_cftyperef.h",
728        "partition_alloc_base/apple/scoped_typeref.h",
729      ]
730      if (is_ios) {
731        sources += [
732          "partition_alloc_base/ios/ios_util.h",
733          "partition_alloc_base/ios/ios_util.mm",
734          "partition_alloc_base/system/sys_info_ios.mm",
735        ]
736      }
737      if (is_mac) {
738        sources += [
739          "partition_alloc_base/mac/mac_util.h",
740          "partition_alloc_base/mac/mac_util.mm",
741          "partition_alloc_base/system/sys_info_mac.mm",
742        ]
743      }
744    }
745
746    public_deps = [
747      ":chromecast_buildflags",
748      ":chromeos_buildflags",
749      ":debugging_buildflags",
750      ":partition_alloc_buildflags",
751    ]
752    public_configs = [ ":public_includes" ]
753    configs += [
754      ":partition_alloc_base_implementation",
755      "//build/config/compiler:wexit_time_destructors",
756    ]
757
758    deps = []
759    if (is_fuchsia) {
760      public_deps += [ "//third_party/fuchsia-sdk/sdk/pkg/fit" ]
761    }
762
763    frameworks = []
764    if (is_apple) {
765      frameworks += [
766        "CoreFoundation.framework",
767        "Foundation.framework",
768      ]
769    }
770
771    configs -= _remove_configs
772    configs += _add_configs
773    configs += [ ":dependants_extra_warnings" ]
774  }
775
776  component("allocator_shim") {
777    visibility = [ ":*" ]
778
779    sources = []
780    deps = []
781    all_dependent_configs = []
782    public_configs = [ ":public_includes" ]
783    configs += [
784      ":allocator_shim_implementation",
785      "//build/config/compiler:wexit_time_destructors",
786    ]
787    frameworks = []
788
789    configs -= _remove_configs
790    configs += _add_configs
791    configs += [ ":dependants_extra_warnings" ]
792
793    shim_headers = []
794    shim_sources = []
795
796    shim_sources += [
797      "shim/allocator_shim.cc",
798      "shim/allocator_shim_dispatch_to_noop_on_free.cc",
799    ]
800    shim_headers += [
801      "shim/allocator_shim.h",
802      "shim/allocator_shim_internals.h",
803      "shim/shim_alloc_functions.h",
804      "shim/allocator_shim_functions.h",
805      "shim/allocator_dispatch.h",
806      "shim/allocator_shim_dispatch_to_noop_on_free.h",
807    ]
808    if (use_partition_alloc) {
809      shim_sources += [
810        "shim/allocator_shim_default_dispatch_to_partition_alloc.cc",
811        "shim/nonscannable_allocator.cc",
812      ]
813      shim_headers += [
814        "shim/allocator_shim_default_dispatch_to_partition_alloc.h",
815        "shim/nonscannable_allocator.h",
816      ]
817    }
818    if (is_android) {
819      shim_headers += [
820        "shim/allocator_shim_override_cpp_symbols.h",
821        "shim/allocator_shim_override_linker_wrapped_symbols.h",
822      ]
823      shim_sources -= [ "shim/allocator_shim.cc" ]
824      shim_sources += [ "shim/allocator_shim_android.cc" ]
825      if (use_allocator_shim) {
826        all_dependent_configs += [ ":wrap_malloc_symbols" ]
827      }
828    }
829    if (is_apple) {
830      shim_headers += [
831        "shim/allocator_shim_override_apple_default_zone.h",
832        "shim/allocator_shim_override_apple_symbols.h",
833        "shim/early_zone_registration_constants.h",
834        "shim/allocator_interception_apple.h",
835        "shim/malloc_zone_functions_apple.h",
836      ]
837      shim_sources -= [ "shim/allocator_shim.cc" ]
838      shim_sources += [
839        "shim/allocator_interception_apple.mm",
840        "shim/allocator_shim_apple.cc",
841        "shim/malloc_zone_functions_apple.cc",
842      ]
843      frameworks += [ "CoreFoundation.framework" ]
844
845      if (use_allocator_shim) {
846        configs += [ ":mac_no_default_new_delete_symbols" ]
847
848        # Do not compile with ARC because this target has to interface with
849        # low-level Objective-C and having ARC would interfere.
850        configs -= [ "//build/config/compiler:enable_arc" ]
851      }
852    }
853    if (is_chromeos || is_linux) {
854      shim_headers += [
855        "shim/allocator_shim_override_cpp_symbols.h",
856        "shim/allocator_shim_override_glibc_weak_symbols.h",
857        "shim/allocator_shim_override_libc_symbols.h",
858      ]
859    }
860    if (is_win) {
861      shim_headers += [
862        "shim/allocator_shim_override_ucrt_symbols_win.h",
863        "shim/winheap_stubs_win.h",
864      ]
865      shim_sources -= [ "shim/allocator_shim.cc" ]
866      shim_sources += [ "shim/winheap_stubs_win.cc" ]
867
868      if (!is_component_build) {
869        shim_sources += [
870          "shim/allocator_shim_win_static.cc",
871          "shim/checked_multiply_win.h",
872        ]
873      } else {
874        shim_sources += [
875          "shim/allocator_shim_functions_win_component.cc",
876          "shim/allocator_shim_win_component.cc",
877        ]
878
879        # allocator_shim cannot depend on libc++ objects because they use malloc()
880        # internally.
881        no_default_deps = true
882      }
883    }
884
885    if (!use_partition_alloc_as_malloc) {
886      if (is_android) {
887        shim_sources += [
888          "shim/allocator_shim_default_dispatch_to_linker_wrapped_symbols.cc",
889        ]
890      }
891      if (is_apple) {
892        shim_sources +=
893            [ "shim/allocator_shim_default_dispatch_to_apple_zoned_malloc.cc" ]
894      }
895      if (is_chromeos || is_linux) {
896        shim_sources += [ "shim/allocator_shim_default_dispatch_to_glibc.cc" ]
897      }
898      if (is_win) {
899        shim_sources += [ "shim/allocator_shim_default_dispatch_to_winheap.cc" ]
900      }
901    }
902
903    sources = shim_headers
904    if (use_allocator_shim) {
905      sources += shim_sources
906    } else {
907      # To avoid "lld-link: error: <root>: undefined symbol: _DllMainCRTStartup",
908      # at least one object file is required when linking allocator_shim.dll.
909      sources += [ "shim/empty.cc" ]
910    }
911
912    deps += [
913      ":allocator_base",
914      ":allocator_core",
915      ":buildflags",
916    ]
917  }
918}  # if (is_clang_or_gcc)
919# TODO(crbug.com/1151236): After making partition_alloc a standalone library,
920# move test code here. i.e. test("partition_alloc_tests") { ... } and
921# test("partition_alloc_perftests").
922