hs_common.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright (c) 2015-2019, Intel Corporation
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Intel Corporation nor the names of its contributors
  13. * may be used to endorse or promote products derived from this software
  14. * without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef HS_AVX512_COMMON_H
  29. #define HS_AVX512_COMMON_H
  30. #if defined(_WIN32)
  31. #define HS_CDECL __cdecl
  32. #else
  33. #define HS_CDECL
  34. #endif
  35. #include <stdlib.h>
  36. /**
  37. * @file
  38. * @brief The Hyperscan common API definition.
  39. *
  40. * Hyperscan is a high speed regular expression engine.
  41. *
  42. * This header contains functions available to both the Hyperscan compiler and
  43. * runtime.
  44. */
  45. #ifdef __cplusplus
  46. extern "C"
  47. {
  48. #endif
  49. struct hs_database;
  50. /**
  51. * A Hyperscan pattern database.
  52. *
  53. * Generated by one of the Hyperscan compiler functions:
  54. * - @ref hs_compile()
  55. * - @ref hs_compile_multi()
  56. * - @ref hs_compile_ext_multi()
  57. */
  58. typedef struct hs_database hs_database_t;
  59. /**
  60. * A type for errors returned by Hyperscan functions.
  61. */
  62. typedef int hs_error_t;
  63. /**
  64. * Free a compiled pattern database.
  65. *
  66. * The free callback set by @ref hs_set_database_allocator() (or @ref
  67. * hs_set_allocator()) will be used by this function.
  68. *
  69. * @param db
  70. * A compiled pattern database. NULL may also be safely provided, in which
  71. * case the function does nothing.
  72. *
  73. * @return
  74. * @ref HS_SUCCESS on success, other values on failure.
  75. */
  76. hs_error_t avx512_hs_free_database(hs_database_t *db);
  77. /**
  78. * Serialize a pattern database to a stream of bytes.
  79. *
  80. * The allocator callback set by @ref hs_set_misc_allocator() (or @ref
  81. * hs_set_allocator()) will be used by this function.
  82. *
  83. * @param db
  84. * A compiled pattern database.
  85. *
  86. * @param bytes
  87. * On success, a pointer to an array of bytes will be returned here.
  88. * These bytes can be subsequently relocated or written to disk. The
  89. * caller is responsible for freeing this block.
  90. *
  91. * @param length
  92. * On success, the number of bytes in the generated byte array will be
  93. * returned here.
  94. *
  95. * @return
  96. * @ref HS_SUCCESS on success, @ref HS_NOMEM if the byte array cannot be
  97. * allocated, other values may be returned if errors are detected.
  98. */
  99. hs_error_t avx512_hs_serialize_database(const hs_database_t *db, char **bytes,
  100. size_t *length);
  101. /**
  102. * Reconstruct a pattern database from a stream of bytes previously generated
  103. * by @ref hs_serialize_database().
  104. *
  105. * This function will allocate sufficient space for the database using the
  106. * allocator set with @ref hs_set_database_allocator() (or @ref
  107. * hs_set_allocator()); to use a pre-allocated region of memory, use the @ref
  108. * hs_deserialize_database_at() function.
  109. *
  110. * @param bytes
  111. * A byte array generated by @ref hs_serialize_database() representing a
  112. * compiled pattern database.
  113. *
  114. * @param length
  115. * The length of the byte array generated by @ref hs_serialize_database().
  116. * This should be the same value as that returned by @ref
  117. * hs_serialize_database().
  118. *
  119. * @param db
  120. * On success, a pointer to a newly allocated @ref hs_database_t will be
  121. * returned here. This database can then be used for scanning, and
  122. * eventually freed by the caller using @ref hs_free_database().
  123. *
  124. * @return
  125. * @ref HS_SUCCESS on success, other values on failure.
  126. */
  127. hs_error_t avx512_hs_deserialize_database(const char *bytes,
  128. const size_t length,
  129. hs_database_t **db);
  130. /**
  131. * Reconstruct a pattern database from a stream of bytes previously generated
  132. * by @ref hs_serialize_database() at a given memory location.
  133. *
  134. * This function (unlike @ref hs_deserialize_database()) will write the
  135. * reconstructed database to the memory location given in the @p db parameter.
  136. * The amount of space required at this location can be determined with the
  137. * @ref hs_serialized_database_size() function.
  138. *
  139. * @param bytes
  140. * A byte array generated by @ref hs_serialize_database() representing a
  141. * compiled pattern database.
  142. *
  143. * @param length
  144. * The length of the byte array generated by @ref hs_serialize_database().
  145. * This should be the same value as that returned by @ref
  146. * hs_serialize_database().
  147. *
  148. * @param db
  149. * Pointer to an 8-byte aligned block of memory of sufficient size to hold
  150. * the deserialized database. On success, the reconstructed database will
  151. * be written to this location. This database can then be used for pattern
  152. * matching. The user is responsible for freeing this memory; the @ref
  153. * hs_free_database() call should not be used.
  154. *
  155. * @return
  156. * @ref HS_SUCCESS on success, other values on failure.
  157. */
  158. hs_error_t avx512_hs_deserialize_database_at(const char *bytes,
  159. const size_t length,
  160. hs_database_t *db);
  161. /**
  162. * Provides the size of the stream state allocated by a single stream opened
  163. * against the given database.
  164. *
  165. * @param database
  166. * Pointer to a compiled (streaming mode) pattern database.
  167. *
  168. * @param stream_size
  169. * On success, the size in bytes of an individual stream opened against the
  170. * given database is placed in this parameter.
  171. *
  172. * @return
  173. * @ref HS_SUCCESS on success, other values on failure.
  174. */
  175. hs_error_t avx512_hs_stream_size(const hs_database_t *database,
  176. size_t *stream_size);
  177. /**
  178. * Provides the size of the given database in bytes.
  179. *
  180. * @param database
  181. * Pointer to compiled pattern database.
  182. *
  183. * @param database_size
  184. * On success, the size of the compiled database in bytes is placed in this
  185. * parameter.
  186. *
  187. * @return
  188. * @ref HS_SUCCESS on success, other values on failure.
  189. */
  190. hs_error_t avx512_hs_database_size(const hs_database_t *database,
  191. size_t *database_size);
  192. /**
  193. * Utility function for reporting the size that would be required by a
  194. * database if it were deserialized.
  195. *
  196. * This can be used to allocate a shared memory region or other "special"
  197. * allocation prior to deserializing with the @ref hs_deserialize_database_at()
  198. * function.
  199. *
  200. * @param bytes
  201. * Pointer to a byte array generated by @ref hs_serialize_database()
  202. * representing a compiled pattern database.
  203. *
  204. * @param length
  205. * The length of the byte array generated by @ref hs_serialize_database().
  206. * This should be the same value as that returned by @ref
  207. * hs_serialize_database().
  208. *
  209. * @param deserialized_size
  210. * On success, the size of the compiled database that would be generated
  211. * by @ref hs_deserialize_database_at() is returned here.
  212. *
  213. * @return
  214. * @ref HS_SUCCESS on success, other values on failure.
  215. */
  216. hs_error_t avx512_hs_serialized_database_size(const char *bytes,
  217. const size_t length,
  218. size_t *deserialized_size);
  219. /**
  220. * Utility function providing information about a database.
  221. *
  222. * @param database
  223. * Pointer to a compiled database.
  224. *
  225. * @param info
  226. * On success, a string containing the version and platform information for
  227. * the supplied database is placed in the parameter. The string is
  228. * allocated using the allocator supplied in @ref hs_set_misc_allocator()
  229. * (or malloc() if no allocator was set) and should be freed by the caller.
  230. *
  231. * @return
  232. * @ref HS_SUCCESS on success, other values on failure.
  233. */
  234. hs_error_t avx512_hs_database_info(const hs_database_t *database,
  235. char **info);
  236. /**
  237. * Utility function providing information about a serialized database.
  238. *
  239. * @param bytes
  240. * Pointer to a serialized database.
  241. *
  242. * @param length
  243. * Length in bytes of the serialized database.
  244. *
  245. * @param info
  246. * On success, a string containing the version and platform information
  247. * for the supplied serialized database is placed in the parameter. The
  248. * string is allocated using the allocator supplied in @ref
  249. * hs_set_misc_allocator() (or malloc() if no allocator was set) and
  250. * should be freed by the caller.
  251. *
  252. * @return
  253. * @ref HS_SUCCESS on success, other values on failure.
  254. */
  255. hs_error_t avx512_hs_serialized_database_info(const char *bytes,
  256. size_t length, char **info);
  257. /**
  258. * The type of the callback function that will be used by Hyperscan to allocate
  259. * more memory at runtime as required, for example in @ref hs_open_stream() to
  260. * allocate stream state.
  261. *
  262. * If Hyperscan is to be used in a multi-threaded, or similarly concurrent
  263. * environment, the allocation function will need to be re-entrant, or
  264. * similarly safe for concurrent use.
  265. *
  266. * @param size
  267. * The number of bytes to allocate.
  268. * @return
  269. * A pointer to the region of memory allocated, or NULL on error.
  270. */
  271. typedef void *(HS_CDECL *hs_alloc_t)(size_t size);
  272. /**
  273. * The type of the callback function that will be used by Hyperscan to free
  274. * memory regions previously allocated using the @ref hs_alloc_t function.
  275. *
  276. * @param ptr
  277. * The region of memory to be freed.
  278. */
  279. typedef void (HS_CDECL *hs_free_t)(void *ptr);
  280. /**
  281. * Set the allocate and free functions used by Hyperscan for allocating
  282. * memory at runtime for stream state, scratch space, database bytecode,
  283. * and various other data structure returned by the Hyperscan API.
  284. *
  285. * The function is equivalent to calling @ref hs_set_stream_allocator(),
  286. * @ref hs_set_scratch_allocator(), @ref hs_set_database_allocator() and
  287. * @ref hs_set_misc_allocator() with the provided parameters.
  288. *
  289. * This call will override any previous allocators that have been set.
  290. *
  291. * Note: there is no way to change the allocator used for temporary objects
  292. * created during the various compile calls (@ref hs_compile(), @ref
  293. * hs_compile_multi(), @ref hs_compile_ext_multi()).
  294. *
  295. * @param alloc_func
  296. * A callback function pointer that allocates memory. This function must
  297. * return memory suitably aligned for the largest representable data type
  298. * on this platform.
  299. *
  300. * @param free_func
  301. * A callback function pointer that frees allocated memory.
  302. *
  303. * @return
  304. * @ref HS_SUCCESS on success, other values on failure.
  305. */
  306. hs_error_t avx512_hs_set_allocator(hs_alloc_t alloc_func,
  307. hs_free_t free_func);
  308. /**
  309. * Set the allocate and free functions used by Hyperscan for allocating memory
  310. * for database bytecode produced by the compile calls (@ref hs_compile(), @ref
  311. * hs_compile_multi(), @ref hs_compile_ext_multi()) and by database
  312. * deserialization (@ref hs_deserialize_database()).
  313. *
  314. * If no database allocation functions are set, or if NULL is used in place of
  315. * both parameters, then memory allocation will default to standard methods
  316. * (such as the system malloc() and free() calls).
  317. *
  318. * This call will override any previous database allocators that have been set.
  319. *
  320. * Note: the database allocator may also be set by calling @ref
  321. * hs_set_allocator().
  322. *
  323. * Note: there is no way to change how temporary objects created during the
  324. * various compile calls (@ref hs_compile(), @ref hs_compile_multi(), @ref
  325. * hs_compile_ext_multi()) are allocated.
  326. *
  327. * @param alloc_func
  328. * A callback function pointer that allocates memory. This function must
  329. * return memory suitably aligned for the largest representable data type
  330. * on this platform.
  331. *
  332. * @param free_func
  333. * A callback function pointer that frees allocated memory.
  334. *
  335. * @return
  336. * @ref HS_SUCCESS on success, other values on failure.
  337. */
  338. hs_error_t avx512_hs_set_database_allocator(hs_alloc_t alloc_func,
  339. hs_free_t free_func);
  340. /**
  341. * Set the allocate and free functions used by Hyperscan for allocating memory
  342. * for items returned by the Hyperscan API such as @ref hs_compile_error_t, @ref
  343. * hs_expr_info_t and serialized databases.
  344. *
  345. * If no misc allocation functions are set, or if NULL is used in place of both
  346. * parameters, then memory allocation will default to standard methods (such as
  347. * the system malloc() and free() calls).
  348. *
  349. * This call will override any previous misc allocators that have been set.
  350. *
  351. * Note: the misc allocator may also be set by calling @ref hs_set_allocator().
  352. *
  353. * @param alloc_func
  354. * A callback function pointer that allocates memory. This function must
  355. * return memory suitably aligned for the largest representable data type
  356. * on this platform.
  357. *
  358. * @param free_func
  359. * A callback function pointer that frees allocated memory.
  360. *
  361. * @return
  362. * @ref HS_SUCCESS on success, other values on failure.
  363. */
  364. hs_error_t avx512_hs_set_misc_allocator(hs_alloc_t alloc_func,
  365. hs_free_t free_func);
  366. /**
  367. * Set the allocate and free functions used by Hyperscan for allocating memory
  368. * for scratch space by @ref hs_alloc_scratch() and @ref hs_clone_scratch().
  369. *
  370. * If no scratch allocation functions are set, or if NULL is used in place of
  371. * both parameters, then memory allocation will default to standard methods
  372. * (such as the system malloc() and free() calls).
  373. *
  374. * This call will override any previous scratch allocators that have been set.
  375. *
  376. * Note: the scratch allocator may also be set by calling @ref
  377. * hs_set_allocator().
  378. *
  379. * @param alloc_func
  380. * A callback function pointer that allocates memory. This function must
  381. * return memory suitably aligned for the largest representable data type
  382. * on this platform.
  383. *
  384. * @param free_func
  385. * A callback function pointer that frees allocated memory.
  386. *
  387. * @return
  388. * @ref HS_SUCCESS on success, other values on failure.
  389. */
  390. hs_error_t avx512_hs_set_scratch_allocator(hs_alloc_t alloc_func,
  391. hs_free_t free_func);
  392. /**
  393. * Set the allocate and free functions used by Hyperscan for allocating memory
  394. * for stream state by @ref hs_open_stream().
  395. *
  396. * If no stream allocation functions are set, or if NULL is used in place of
  397. * both parameters, then memory allocation will default to standard methods
  398. * (such as the system malloc() and free() calls).
  399. *
  400. * This call will override any previous stream allocators that have been set.
  401. *
  402. * Note: the stream allocator may also be set by calling @ref
  403. * hs_set_allocator().
  404. *
  405. * @param alloc_func
  406. * A callback function pointer that allocates memory. This function must
  407. * return memory suitably aligned for the largest representable data type
  408. * on this platform.
  409. *
  410. * @param free_func
  411. * A callback function pointer that frees allocated memory.
  412. *
  413. * @return
  414. * @ref HS_SUCCESS on success, other values on failure.
  415. */
  416. hs_error_t avx512_hs_set_stream_allocator(hs_alloc_t alloc_func,
  417. hs_free_t free_func);
  418. /**
  419. * Utility function for identifying this release version.
  420. *
  421. * @return
  422. * A string containing the version number of this release build and the
  423. * date of the build. It is allocated statically, so it does not need to
  424. * be freed by the caller.
  425. */
  426. const char * avx512_hs_version(void);
  427. /**
  428. * Utility function to test the current system architecture.
  429. *
  430. * Hyperscan requires the Supplemental Streaming SIMD Extensions 3 instruction
  431. * set. This function can be called on any x86 platform to determine if the
  432. * system provides the required instruction set.
  433. *
  434. * This function does not test for more advanced features if Hyperscan has
  435. * been built for a more specific architecture, for example the AVX2
  436. * instruction set.
  437. *
  438. * @return
  439. * @ref HS_SUCCESS on success, @ref HS_ARCH_ERROR if system does not
  440. * support Hyperscan.
  441. */
  442. hs_error_t avx512_hs_valid_platform(void);
  443. /**
  444. * @defgroup HS_ERROR hs_error_t values
  445. *
  446. * @{
  447. */
  448. /**
  449. * The engine completed normally.
  450. */
  451. #define HS_SUCCESS 0
  452. /**
  453. * A parameter passed to this function was invalid.
  454. *
  455. * This error is only returned in cases where the function can detect an
  456. * invalid parameter -- it cannot be relied upon to detect (for example)
  457. * pointers to freed memory or other invalid data.
  458. */
  459. #define HS_INVALID (-1)
  460. /**
  461. * A memory allocation failed.
  462. */
  463. #define HS_NOMEM (-2)
  464. /**
  465. * The engine was terminated by callback.
  466. *
  467. * This return value indicates that the target buffer was partially scanned,
  468. * but that the callback function requested that scanning cease after a match
  469. * was located.
  470. */
  471. #define HS_SCAN_TERMINATED (-3)
  472. /**
  473. * The pattern compiler failed, and the @ref hs_compile_error_t should be
  474. * inspected for more detail.
  475. */
  476. #define HS_COMPILER_ERROR (-4)
  477. /**
  478. * The given database was built for a different version of Hyperscan.
  479. */
  480. #define HS_DB_VERSION_ERROR (-5)
  481. /**
  482. * The given database was built for a different platform (i.e., CPU type).
  483. */
  484. #define HS_DB_PLATFORM_ERROR (-6)
  485. /**
  486. * The given database was built for a different mode of operation. This error
  487. * is returned when streaming calls are used with a block or vectored database
  488. * and vice versa.
  489. */
  490. #define HS_DB_MODE_ERROR (-7)
  491. /**
  492. * A parameter passed to this function was not correctly aligned.
  493. */
  494. #define HS_BAD_ALIGN (-8)
  495. /**
  496. * The memory allocator (either malloc() or the allocator set with @ref
  497. * hs_set_allocator()) did not correctly return memory suitably aligned for the
  498. * largest representable data type on this platform.
  499. */
  500. #define HS_BAD_ALLOC (-9)
  501. /**
  502. * The scratch region was already in use.
  503. *
  504. * This error is returned when Hyperscan is able to detect that the scratch
  505. * region given is already in use by another Hyperscan API call.
  506. *
  507. * A separate scratch region, allocated with @ref hs_alloc_scratch() or @ref
  508. * hs_clone_scratch(), is required for every concurrent caller of the Hyperscan
  509. * API.
  510. *
  511. * For example, this error might be returned when @ref hs_scan() has been
  512. * called inside a callback delivered by a currently-executing @ref hs_scan()
  513. * call using the same scratch region.
  514. *
  515. * Note: Not all concurrent uses of scratch regions may be detected. This error
  516. * is intended as a best-effort debugging tool, not a guarantee.
  517. */
  518. #define HS_SCRATCH_IN_USE (-10)
  519. /**
  520. * Unsupported CPU architecture.
  521. *
  522. * This error is returned when Hyperscan is able to detect that the current
  523. * system does not support the required instruction set.
  524. *
  525. * At a minimum, Hyperscan requires Supplemental Streaming SIMD Extensions 3
  526. * (SSSE3).
  527. */
  528. #define HS_ARCH_ERROR (-11)
  529. /**
  530. * Provided buffer was too small.
  531. *
  532. * This error indicates that there was insufficient space in the buffer. The
  533. * call should be repeated with a larger provided buffer.
  534. *
  535. * Note: in this situation, it is normal for the amount of space required to be
  536. * returned in the same manner as the used space would have been returned if the
  537. * call was successful.
  538. */
  539. #define HS_INSUFFICIENT_SPACE (-12)
  540. /**
  541. * Unexpected internal error.
  542. *
  543. * This error indicates that there was unexpected matching behaviors. This
  544. * could be related to invalid usage of stream and scratch space or invalid memory
  545. * operations by users.
  546. *
  547. */
  548. #define HS_UNKNOWN_ERROR (-13)
  549. /** @} */
  550. #ifdef __cplusplus
  551. } /* extern "C" */
  552. #endif
  553. #endif /* HS_AVX512_COMMON_H */