BUILD.gn 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. # Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import("//build/config/dcheck_always_on.gni")
  5. import("//build/config/sanitizers/sanitizers.gni")
  6. import("//testing/libfuzzer/fuzzer_test.gni")
  7. import("//third_party/protobuf/proto_library.gni")
  8. import("//third_party/sqlite/sqlite_chromium_configuration_flags.gni")
  9. import("//third_party/sqlite/sqlite_common_configuration_flags.gni")
  10. import("//third_party/sqlite/sqlite_dev_configuration_flags.gni")
  11. # Compile-time options passed to SQLite.
  12. #
  13. # These options are used when building our own SQLite library, which happens
  14. # everywhere except on iOS. These compile-time options are exported via a
  15. # public_config to all targets using SQLite, because they're needed by the
  16. # sqlite.h header. To avoid name clashes (macro names are resolved using a
  17. # global namespace), this block should only contain preprocessor macros that
  18. # are unambiguously connected to SQLite.
  19. #
  20. # The vast majority of the macros here are documented at
  21. # https://www.sqlite.org/compile.html
  22. config("common_sqlite3_compile_options") {
  23. # Start with the configuration flags which are also baked into the SQLite
  24. # source when the amalgamation is generated. These are the same for all
  25. # platforms.
  26. defines = sqlite_common_configuration_flags
  27. # The flags not baked in, but common for all platforms.
  28. defines += [
  29. # Skip writing transaction rollback journals on f2fs.
  30. # f2fs tends to be used on Android, and may be used on ChromeOS.
  31. "SQLITE_ENABLE_BATCH_ATOMIC_WRITE",
  32. # Forces SQLite to only store temporary data in memory.
  33. #
  34. # This is the only supported setting on Android. Using it everywhere removes
  35. # a source of cross-platform variation, and saves us from having to reason
  36. # about SQLite storing data in the operating system's temporary directory.
  37. "SQLITE_TEMP_STORE=3",
  38. ]
  39. # On OSX, SQLite has extra logic for detecting the use of network
  40. # filesystems (e.g., AFS, NFS) and for working around locking problems in
  41. # these filesystems. This logic is gated by SQLITE_ENABLE_LOCKING_STYLE, which
  42. # is 1 by default on OSX and iOS, and 0 everywhere else.
  43. #
  44. # When enabled, SQLITE_ENABLE_LOCKING_STYLE results in a compile-time warning
  45. # on iOS. The recommended solution is to disable the flag on iOS, because
  46. # iOS doesn't (yet?) have networked filesystems. Since we have to do this,
  47. # might as well be explicit about the flag everywhere.
  48. if (is_mac) {
  49. defines += [ "SQLITE_ENABLE_LOCKING_STYLE=1" ]
  50. } else {
  51. defines += [ "SQLITE_ENABLE_LOCKING_STYLE=0" ]
  52. }
  53. if (using_sanitizer) {
  54. defines += [
  55. # Limit max length of data blobs and queries to 128 MB for fuzzing builds.
  56. "SQLITE_MAX_LENGTH=128000000",
  57. "SQLITE_MAX_SQL_LENGTH=128000000",
  58. "SQLITE_PRINTF_PRECISION_LIMIT=1280000",
  59. ]
  60. # During fuzz testing, valid SQL queries generated by fuzzing engine may
  61. # lead to large memory allocations. If that happens, fuzzer reports an
  62. # out-of-memory error. However, such errors are not valid bugs.
  63. # To avoid hitting those irrelevant OOMs, we limit max number of memory
  64. # pages, so fuzzer will not crash when reaching the limit.
  65. # Apply this for fuzzing builds only, not for all builds with sanitizers.
  66. if (use_fuzzing_engine) {
  67. defines += [
  68. "SQLITE_MAX_PAGE_COUNT=16384",
  69. # Used to deserialize a database from a libfuzzer-provided data blob.
  70. # This is to fuzz SQLite's resilience to database corruption.
  71. "SQLITE_ENABLE_DESERIALIZE",
  72. ]
  73. # The progress callback is used in fuzzing to cancel long-running queries
  74. # so we don't spend too much time on them.
  75. defines -= [ "SQLITE_OMIT_PROGRESS_CALLBACK" ]
  76. }
  77. }
  78. if (is_debug || dcheck_always_on) {
  79. if (use_fuzzing_engine && use_sanitizer_coverage) {
  80. # Enable SQLite's assert() macros.
  81. #
  82. # TODO(pwnall): Fix all the bugs preventing us from enabling this flag for
  83. # all DCHECK builds. See https://crbug.com/907371 and
  84. # https://crrev.com/c/1343529.
  85. defines += [ "SQLITE_DEBUG" ]
  86. }
  87. # Check preconditions when SQLite APIs are called. See
  88. # https://sqlite.org/compile.html#enable_api_armor
  89. #
  90. # fuzzing builds have this disabled because the fuzzers are guaranteed to
  91. # use the API correctly, and removing the checks opens up the possibility
  92. # that the fuzzers will get more code coverage.
  93. defines += [ "SQLITE_ENABLE_API_ARMOR" ]
  94. }
  95. }
  96. # These options add SQLite features that we need in Chrome.
  97. config("chromium_sqlite3_compile_options") {
  98. configs = [ ":common_sqlite3_compile_options" ]
  99. defines = sqlite_chromium_configuration_flags
  100. }
  101. # These options add SQLite features that we need in developer tools.
  102. config("dev_sqlite3_compile_options") {
  103. configs = [ ":common_sqlite3_compile_options" ]
  104. defines = sqlite_dev_configuration_flags
  105. }
  106. config("sqlite_warnings") {
  107. cflags = []
  108. if (is_clang) {
  109. cflags += [
  110. "-Wno-shadow",
  111. # sqlite contains a few functions that are unused, at least on
  112. # Windows with Chrome's sqlite patches applied
  113. # (interiorCursorEOF fts3EvalDeferredPhrase
  114. # fts3EvalSelectDeferred sqlite3Fts3InitHashTable
  115. # sqlite3Fts3InitTok).
  116. "-Wno-unused-function",
  117. ]
  118. if (is_debug || dcheck_always_on) {
  119. cflags += [
  120. # SQLite uses assert(!"description") to express
  121. # NOT_REACHED() << "description". This is considered an implicit
  122. # conversion from char[] to bool, and triggers a warning.
  123. "-Wno-string-conversion",
  124. ]
  125. }
  126. }
  127. if (is_linux || is_chromeos) {
  128. cflags += [
  129. # SQLite doesn't believe in compiler warnings, preferring testing.
  130. # http://www.sqlite.org/faq.html#q17
  131. "-Wno-int-to-pointer-cast",
  132. "-Wno-pointer-to-int-cast",
  133. ]
  134. }
  135. if (is_ios) {
  136. cflags += [
  137. # SQLite issues a #pragma warning on iOS.
  138. # http://sqlite.1065341.n5.nabble.com/Compiler-warning-quot-gethostuuid-is-disabled-quot-building-SQLite-for-iOS-td96881.html
  139. #
  140. # Contrary to what is said on the mailing list, setting
  141. # SQLITE_ENABLE_LOCKING_STYLE to 0 does not make the warning go away.
  142. "-Wno-#warnings",
  143. ]
  144. }
  145. if (is_win && !is_clang) {
  146. cflags += [ "/wd4101" ] # 'zTrace' unreferenced variable in src/src/vdbe.c
  147. }
  148. }
  149. # Naming the library "sqlite3" can cause conflicts with the system library.
  150. component("chromium_sqlite3") {
  151. visibility = [ ":*" ]
  152. public = [ "sqlite3.h" ]
  153. sources = [
  154. "sqlite3_shim.c",
  155. "sqlite3_shim_fixups.h",
  156. "src/amalgamation/rename_exports.h",
  157. "src/amalgamation/sqlite3.h",
  158. ]
  159. inputs = [
  160. # This file is #included into sqlite3_shim.c, which injects Chrome-specific
  161. # definitions into the SQLite amalgamation code.
  162. "src/amalgamation/sqlite3.c",
  163. ]
  164. cflags = []
  165. defines = []
  166. if (is_component_build) {
  167. if (is_win) {
  168. defines += [ "SQLITE_API=__declspec(dllexport)" ]
  169. } else {
  170. defines += [ "SQLITE_API=__attribute__((visibility(\"default\")))" ]
  171. }
  172. }
  173. if (is_linux || is_chromeos || is_android) {
  174. defines += [
  175. # Linux provides fdatasync(), a faster equivalent of fsync().
  176. "fdatasync=fdatasync",
  177. ]
  178. }
  179. if (is_posix || is_fuchsia) {
  180. defines += [
  181. # Allow xSleep() call on Unix to use usleep() rather than sleep(), so it
  182. # will have microsecond precision. Should only affect contended
  183. # databases via the busy callback. Browser profile databases are mostly
  184. # exclusive, but renderer databases may allow for contention.
  185. "HAVE_USLEEP=1",
  186. # Use pread/pwrite directly rather than emulating them.
  187. "USE_PREAD=1",
  188. ]
  189. }
  190. include_dirs = [
  191. ".", # sqlite3.h here must override the one in src/amalgamation/.
  192. "src/amalgamation",
  193. ]
  194. configs -= [ "//build/config/compiler:chromium_code" ]
  195. configs += [
  196. ":chromium_sqlite3_compile_options",
  197. "//build/config/compiler:no_chromium_code",
  198. "//build/config/sanitizers:cfi_icall_generalize_pointers",
  199. # Must be after no_chromium_code for warning flags to be ordered correctly.
  200. ":sqlite_warnings",
  201. ]
  202. if (is_apple) {
  203. frameworks = [ "CoreFoundation.framework" ]
  204. if (!is_ios) {
  205. frameworks += [ "CoreServices.framework" ]
  206. }
  207. } else if (is_android) {
  208. defines += [
  209. "SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576",
  210. "SQLITE_DEFAULT_AUTOVACUUM=1",
  211. ]
  212. }
  213. deps = [ "//third_party/icu" ]
  214. }
  215. # Similar to component("chromium_sqlite") above, but uses the developer tools
  216. # options at config("dev_sqlite3_compile_options").
  217. component("dev_sqlite3") {
  218. visibility = [ ":*" ]
  219. public = [ "dev/sqlite3.h" ]
  220. sources = [
  221. "dev/sqlite3_shim.c",
  222. "sqlite3_shim_fixups.h",
  223. "src/amalgamation_dev/rename_exports.h",
  224. "src/amalgamation_dev/sqlite3.h",
  225. ]
  226. inputs = [
  227. # This file is #included into sqlite3_shim.c, which injects Chrome-specific
  228. # definitions into the SQLite amalgamation code.
  229. "src/amalgamation_dev/sqlite3.c",
  230. ]
  231. cflags = []
  232. defines = []
  233. if (is_component_build) {
  234. if (is_win) {
  235. defines += [ "SQLITE_API=__declspec(dllexport)" ]
  236. } else {
  237. defines += [ "SQLITE_API=__attribute__((visibility(\"default\")))" ]
  238. }
  239. }
  240. if (is_linux || is_chromeos || is_android) {
  241. defines += [
  242. # Linux provides fdatasync(), a faster equivalent of fsync().
  243. "fdatasync=fdatasync",
  244. ]
  245. }
  246. if (is_posix || is_fuchsia) {
  247. defines += [
  248. # Allow xSleep() call on Unix to use usleep() rather than sleep(), so it
  249. # will have microsecond precision. Should only affect contended
  250. # databases via the busy callback. Browser profile databases are mostly
  251. # exclusive, but renderer databases may allow for contention.
  252. "HAVE_USLEEP=1",
  253. # Use pread/pwrite directly rather than emulating them.
  254. "USE_PREAD=1",
  255. ]
  256. }
  257. include_dirs = [
  258. "dev", # sqlite3.h here must override the one in amalgamation_dev/.
  259. "src/amalgamation_dev",
  260. ]
  261. configs -= [ "//build/config/compiler:chromium_code" ]
  262. configs += [
  263. ":dev_sqlite3_compile_options",
  264. "//build/config/compiler:no_chromium_code",
  265. "//build/config/sanitizers:cfi_icall_generalize_pointers",
  266. # Must be after no_chromium_code for warning flags to be ordered correctly.
  267. ":sqlite_warnings",
  268. ]
  269. if (is_apple) {
  270. frameworks = [ "CoreFoundation.framework" ]
  271. if (!is_ios) {
  272. frameworks += [ "CoreServices.framework" ]
  273. }
  274. } else if (is_android) {
  275. defines += [
  276. "SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576",
  277. "SQLITE_DEFAULT_AUTOVACUUM=1",
  278. ]
  279. }
  280. deps = [ "//third_party/icu" ]
  281. }
  282. config("sqlite_export") {
  283. if (is_component_build && is_win) {
  284. defines = [ "SQLITE_API=__declspec(dllimport)" ]
  285. }
  286. }
  287. # This is used to allow the SQLITE_API definition to be different when
  288. # building sqlite3.c than it is when clients include sqlite3.h.
  289. group("sqlite") {
  290. public_deps = [ ":chromium_sqlite3" ]
  291. public_configs = [
  292. ":chromium_sqlite3_compile_options",
  293. ":sqlite_export",
  294. ]
  295. }
  296. group("sqlite_dev") {
  297. public_deps = [ ":dev_sqlite3" ]
  298. public_configs = [
  299. ":dev_sqlite3_compile_options",
  300. ":sqlite_export",
  301. ]
  302. }
  303. if (is_win || is_mac || is_linux || is_chromeos) {
  304. executable("sqlite_shell") {
  305. include_dirs = [
  306. # SQLite's shell.c contains an '#include "sqlite3.h", which we want to be
  307. # resolved to //third_party/sqlite/sqlite3.h.
  308. ".",
  309. ]
  310. sources = [
  311. "sqlite_shell_icu_helper.cc",
  312. "sqlite_shell_icu_helper.h",
  313. "sqlite_shell_shim.c",
  314. ]
  315. deps = [
  316. ":sqlite",
  317. "//base",
  318. "//base:i18n",
  319. "//third_party/icu",
  320. ]
  321. configs -= [ "//build/config/compiler:chromium_code" ]
  322. configs += [
  323. ":chromium_sqlite3_compile_options",
  324. "//build/config/compiler:no_chromium_code",
  325. # Must be after no_chromium_code for warning flags to be ordered
  326. # correctly.
  327. ":sqlite_warnings",
  328. ]
  329. }
  330. # Similar to executable("sqlite_shell") above, but uses the developer tools
  331. # options at config("dev_sqlite3_compile_options") and group("sqlite_dev").
  332. executable("sqlite_dev_shell") {
  333. include_dirs = [
  334. # SQLite's shell.c contains an '#include "sqlite3.h", which we want to be
  335. # resolved to //third_party/sqlite/sqlite3.h.
  336. ".",
  337. ]
  338. sources = [
  339. "sqlite_shell_icu_helper.cc",
  340. "sqlite_shell_icu_helper.h",
  341. "sqlite_shell_shim.c",
  342. ]
  343. deps = [
  344. ":sqlite_dev",
  345. "//base",
  346. "//base:i18n",
  347. "//third_party/icu",
  348. ]
  349. configs -= [ "//build/config/compiler:chromium_code" ]
  350. configs += [
  351. ":dev_sqlite3_compile_options",
  352. "//build/config/compiler:no_chromium_code",
  353. # Must be after no_chromium_code for warning flags to be ordered
  354. # correctly.
  355. ":sqlite_warnings",
  356. ]
  357. }
  358. }
  359. # Libfuzzer-based fuzzer test from SQLite source tree.
  360. fuzzer_test("sqlite3_ossfuzz_fuzzer") {
  361. include_dirs = [ "." ]
  362. sources = [ "src/test/ossfuzz.c" ]
  363. deps = [ ":sqlite" ]
  364. dict = "fuzz/sql.dict"
  365. }
  366. source_set("sqlite3_lpm_fuzzer_core") {
  367. sources = [
  368. "fuzz/disabled_queries_parser.cc",
  369. "fuzz/disabled_queries_parser.h",
  370. "fuzz/sql_run_queries.cc",
  371. "fuzz/sql_run_queries.h",
  372. ]
  373. deps = [ ":sqlite" ]
  374. public_deps = [ ":sqlite3_lpm_fuzzer_input" ]
  375. configs += [
  376. ":sqlite_warnings",
  377. ":chromium_sqlite3_compile_options",
  378. ]
  379. all_dependent_configs = [
  380. ":lpm_fuzzer_omit_non_websql",
  381. ":chromium_sqlite3_compile_options",
  382. ]
  383. }
  384. # LPM-based fuzzer test.
  385. fuzzer_test("sqlite3_lpm_fuzzer") {
  386. sources = [
  387. "fuzz/sql_fuzzer.cc",
  388. "fuzz/sql_query_proto_to_string.cc",
  389. "fuzz/sql_query_proto_to_string.h",
  390. ]
  391. deps = [
  392. ":sqlite3_lpm_fuzzer_core",
  393. "//third_party/libprotobuf-mutator",
  394. ]
  395. additional_configs = [ ":sqlite_warnings" ]
  396. libfuzzer_options = [
  397. "max_len=2111000",
  398. "len_control=0",
  399. ]
  400. seed_corpus = "fuzz/lpm_fuzzer_seed_corpus/"
  401. }
  402. # FTS3-focused LPM-based fuzzer test.
  403. fuzzer_test("sqlite3_fts3_lpm_fuzzer") {
  404. sources = [
  405. "fuzz/sql_fuzzer.cc",
  406. "fuzz/sql_query_proto_to_string.cc",
  407. "fuzz/sql_query_proto_to_string.h",
  408. ]
  409. deps = [
  410. ":sqlite3_lpm_fuzzer_core",
  411. "//third_party/libprotobuf-mutator",
  412. ]
  413. additional_configs = [
  414. ":sqlite_warnings",
  415. ":sqlite3_fts3_lpm_fuzzer_config",
  416. ]
  417. libfuzzer_options = [
  418. "max_len=2111000",
  419. "len_control=0",
  420. ]
  421. }
  422. fuzzer_test("sqlite3_shadow_table_fuzzer") {
  423. sources = [ "fuzz/shadow_table_fuzzer.cc" ]
  424. deps = [ ":sqlite" ]
  425. additional_configs = [ ":sqlite_warnings" ]
  426. }
  427. fuzzer_test("sqlite3_select_printf_lpm_fuzzer") {
  428. sources = [
  429. "fuzz/sql_printf_fuzzer.cc",
  430. "fuzz/sql_query_proto_to_string.cc",
  431. "fuzz/sql_query_proto_to_string.h",
  432. ]
  433. deps = [
  434. ":sqlite3_lpm_fuzzer_core",
  435. "//third_party/libprotobuf-mutator",
  436. ]
  437. libfuzzer_options = [ "max_len=111000" ]
  438. }
  439. fuzzer_test("sqlite3_select_strftime_lpm_fuzzer") {
  440. sources = [
  441. "fuzz/sql_query_proto_to_string.cc",
  442. "fuzz/sql_query_proto_to_string.h",
  443. "fuzz/sql_strftime_fuzzer.cc",
  444. ]
  445. deps = [
  446. ":sqlite3_lpm_fuzzer_core",
  447. "//third_party/libprotobuf-mutator",
  448. ]
  449. libfuzzer_options = [ "max_len=111000" ]
  450. }
  451. fuzzer_test("sqlite3_select_expr_lpm_fuzzer") {
  452. sources = [
  453. "fuzz/sql_expr_fuzzer.cc",
  454. "fuzz/sql_query_proto_to_string.cc",
  455. "fuzz/sql_query_proto_to_string.h",
  456. ]
  457. deps = [
  458. ":sqlite3_lpm_fuzzer_core",
  459. "//third_party/libprotobuf-mutator",
  460. ]
  461. libfuzzer_options = [
  462. "max_len=111000",
  463. "len_control=0",
  464. ]
  465. }
  466. config("sqlite3_fts3_lpm_fuzzer_config") {
  467. defines = [ "FUZZ_FTS3" ]
  468. }
  469. config("lpm_fuzzer_omit_non_websql") {
  470. defines = [
  471. "FUZZ_OMIT_SAVEPOINT",
  472. "FUZZ_OMIT_PRAGMA",
  473. ]
  474. }
  475. proto_library("sqlite3_lpm_fuzzer_input") {
  476. sources = [
  477. "fuzz/icu_codes.proto",
  478. "fuzz/sql_queries.proto",
  479. "fuzz/sql_query_grammar.proto",
  480. ]
  481. }
  482. # Generates a good corpus for the sqlite_lpm_fuzzer
  483. # Don't build this tool on Windows since it uses a POSIX-only API and because it
  484. # only needs to be used on devs' machines.
  485. if (use_fuzzing_engine && !is_win) {
  486. executable("sqlite3_lpm_corpus_gen") {
  487. sources = [
  488. "fuzz/sql_generate_corpus.cc",
  489. "fuzz/sql_query_proto_to_string.cc",
  490. "fuzz/sql_query_proto_to_string.h",
  491. ]
  492. deps = [
  493. ":sqlite3_lpm_fuzzer_core",
  494. "//base",
  495. "//third_party/protobuf:protobuf_full",
  496. ]
  497. }
  498. }
  499. config("sqlite3_dbfuzz2_config") {
  500. cflags = [ "-Wno-sign-compare" ]
  501. configs = [ ":sqlite_warnings" ]
  502. }
  503. # Upstream fuzzer that tests corrupted database files.
  504. if (use_fuzzing_engine) {
  505. fuzzer_test("sqlite3_dbfuzz2_fuzzer") {
  506. include_dirs = [ "." ]
  507. sources = [ "src/test/dbfuzz2.c" ]
  508. deps = [ ":sqlite" ]
  509. additional_configs = [ ":sqlite3_dbfuzz2_config" ]
  510. seed_corpus = "fuzz/db_corpus"
  511. }
  512. }