1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
|
/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* This implements 'user defined functions' */
#include "sql/sql_udf.h"
#include <stdio.h>
#include <string.h>
#include <iterator>
#include <memory>
#include <new>
#include <string>
#include <unordered_map>
#include <utility>
#include "m_ctype.h"
#include "m_string.h" // my_stpcpy
#include "map_helpers.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_config.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_psi_config.h"
#include "my_sharedlib.h"
#include "my_sys.h"
#include "my_thread_local.h"
#include "mysql/components/service_implementation.h"
#include "mysql/components/services/bits/mysql_rwlock_bits.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/components/services/bits/psi_memory_bits.h"
#include "mysql/components/services/bits/psi_rwlock_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/psi/mysql_memory.h"
#include "mysql/psi/mysql_rwlock.h"
#include "mysql_com.h"
#include "mysqld_error.h" // ER_*
#include "sql/derror.h" // ER_THD
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/iterators/row_iterator.h"
#include "sql/mdl.h"
#include "sql/mysqld.h" // opt_allow_suspicious_udfs
#include "sql/sql_base.h" // close_mysql_tables
#include "sql/sql_class.h" // THD
#include "sql/sql_const.h"
#include "sql/sql_executor.h" // unique_ptr_destroy_only<RowIterator>
#include "sql/sql_parse.h" // check_string_char_length
#include "sql/sql_plugin.h" // check_valid_path
#include "sql/sql_system_table_check.h" // System_table_intact
#include "sql/sql_table.h" // write_bin_log
#include "sql/table.h" // Table_ref
#include "sql/thd_raii.h"
#include "sql/thr_malloc.h"
#include "sql/transaction.h" // trans_*
#include "thr_lock.h"
#include "udf_registration_imp.h"
#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#endif
/**
@page page_ext_udf User Defined Functions
@todo Document me
@sa add_udf, udf_hash_delete.
*/
/**
A local flag indicating whether SQL based UDF operations are allowed.
Now the UDF structures are always allocated/deallocated due to
the component service.
So this variable does not cover initialization/deinitialization of these.
\ref mem and \ref THR_LOCK_udf are always initialized, even in
--skip-grant-tables mode.
*/
static bool initialized = false;
static MEM_ROOT mem;
static collation_unordered_map<std::string, udf_func *> *udf_hash;
static mysql_rwlock_t THR_LOCK_udf;
static constexpr const size_t UDF_ALLOC_BLOCK_SIZE{1024};
static udf_func *add_udf(LEX_STRING *name, Item_result ret, char *dl,
Item_udftype typ);
static void udf_hash_delete(udf_func *udf);
static void *find_udf_dl(const char *dl);
// mysql.func table definition.
static const int MYSQL_UDF_TABLE_FIELD_COUNT = 4;
static const TABLE_FIELD_TYPE
mysql_udf_table_fields[MYSQL_UDF_TABLE_FIELD_COUNT] = {
{{STRING_WITH_LEN("name")},
{STRING_WITH_LEN("char(64)")},
{nullptr, 0}},
{{STRING_WITH_LEN("ret")}, {STRING_WITH_LEN("tinyint")}, {nullptr, 0}},
{{STRING_WITH_LEN("dl")}, {STRING_WITH_LEN("char(128)")}, {nullptr, 0}},
{{STRING_WITH_LEN("type")},
{STRING_WITH_LEN("enum('function','aggregate')")},
{STRING_WITH_LEN("utf8mb3")}}};
static const TABLE_FIELD_DEF mysql_udf_table_def = {MYSQL_UDF_TABLE_FIELD_COUNT,
mysql_udf_table_fields};
static char *init_syms(udf_func *tmp, char *nm) {
char *end;
if (!((tmp->func = (Udf_func_any)dlsym(tmp->dlhandle, tmp->name.str))))
return tmp->name.str;
end = my_stpcpy(nm, tmp->name.str);
if (tmp->type == UDFTYPE_AGGREGATE) {
(void)my_stpcpy(end, "_clear");
if (!((tmp->func_clear = (Udf_func_clear)dlsym(tmp->dlhandle, nm))))
return nm;
(void)my_stpcpy(end, "_add");
if (!((tmp->func_add = (Udf_func_add)dlsym(tmp->dlhandle, nm)))) return nm;
}
(void)my_stpcpy(end, "_deinit");
tmp->func_deinit = (Udf_func_deinit)dlsym(tmp->dlhandle, nm);
(void)my_stpcpy(end, "_init");
tmp->func_init = (Udf_func_init)dlsym(tmp->dlhandle, nm);
/*
to prevent loading "udf" from, e.g. libc.so
let's ensure that at least one auxiliary symbol is defined
*/
if (!tmp->func_init && !tmp->func_deinit && tmp->type != UDFTYPE_AGGREGATE) {
if (!opt_allow_suspicious_udfs) return nm;
LogErr(WARNING_LEVEL, ER_FAILED_TO_FIND_DL_ENTRY, nm);
}
return nullptr;
}
static PSI_memory_key key_memory_udf_mem;
#ifdef HAVE_PSI_INTERFACE
static PSI_rwlock_key key_rwlock_THR_LOCK_udf;
static PSI_rwlock_info all_udf_rwlocks[] = {{&key_rwlock_THR_LOCK_udf,
"THR_LOCK_udf", PSI_FLAG_SINGLETON,
0, PSI_DOCUMENT_ME}};
static PSI_memory_info all_udf_memory[] = {{&key_memory_udf_mem, "udf_mem",
PSI_FLAG_ONLY_GLOBAL_STAT, 0,
"Shared structure of UDFs."}};
static void init_udf_psi_keys(void) {
const char *category = "sql";
int count;
count = static_cast<int>(array_elements(all_udf_rwlocks));
mysql_rwlock_register(category, all_udf_rwlocks, count);
count = static_cast<int>(array_elements(all_udf_memory));
mysql_memory_register(category, all_udf_memory, count);
}
#endif
/**
Initialize the UDF global structures.
This is done as a separate step so that the UDF registration
service can work when initializing plugins, which happens
before reading the UDF table.
*/
void udf_init_globals() {
DBUG_TRACE;
if (initialized) return;
#ifdef HAVE_PSI_INTERFACE
init_udf_psi_keys();
#endif
mysql_rwlock_init(key_rwlock_THR_LOCK_udf, &THR_LOCK_udf);
init_sql_alloc(key_memory_udf_mem, &mem, UDF_ALLOC_BLOCK_SIZE);
udf_hash = new collation_unordered_map<std::string, udf_func *>(
system_charset_info, key_memory_udf_mem);
}
/*
Read all predeclared functions from mysql.func and accept all that
can be used.
The global structures must be initialized first.
*/
void udf_read_functions_table() {
udf_func *tmp;
TABLE *table;
unique_ptr_destroy_only<RowIterator> iterator;
int error;
DBUG_TRACE;
char db[] = "mysql"; /* A subject to casednstr, can't be constant */
if (initialized) {
assert("wrong init order: reading UDFs from the table twice");
return;
}
initialized = true;
THD *new_thd = new (std::nothrow) THD;
if (new_thd == nullptr) {
LogErr(ERROR_LEVEL, ER_UDF_CANT_ALLOC_FOR_STRUCTURES);
mem.Clear();
delete new_thd;
return;
}
new_thd->thread_stack = (char *)&new_thd;
new_thd->store_globals();
{
LEX_CSTRING db_lex_cstr = {STRING_WITH_LEN(db)};
new_thd->set_db(db_lex_cstr);
}
Table_ref tables(db, "func", TL_READ, MDL_SHARED_READ_ONLY);
if (open_trans_system_tables_for_read(new_thd, &tables)) {
DBUG_PRINT("error", ("Can't open udf table"));
LogErr(ERROR_LEVEL, ER_UDF_CANT_OPEN_FUNCTION_TABLE);
goto end;
}
table = tables.table;
iterator =
init_table_iterator(new_thd, table, /*ignore_not_found_rows=*/false,
/*count_examined_rows=*/false);
if (iterator == nullptr) goto end;
while (!(error = iterator->Read())) {
DBUG_PRINT("info", ("init udf record"));
LEX_STRING name;
name.str = get_field(&mem, table->field[0]);
// Check the name.str is NULL or not.
if (name.str == nullptr) {
sql_print_error("Invalid row in mysql.func table for column 'name'");
continue;
}
name.length = strlen(name.str);
char *dl_name = get_field(&mem, table->field[2]);
if (dl_name == nullptr) {
sql_print_error("Invalid row in mysql.func table for function '%.64s'",
name.str);
continue;
}
bool new_dl = false;
Item_udftype udftype = UDFTYPE_FUNCTION;
if (table->s->fields >= 4) // New func table
udftype = (Item_udftype)table->field[3]->val_int();
/*
Ensure that the .dll doesn't have a path
This is done to ensure that only approved dll from the system
directories are used (to make this even remotely secure).
On windows we must check both FN_LIBCHAR and '/'.
*/
LEX_CSTRING name_cstr = {name.str, name.length};
if (check_valid_path(dl_name, strlen(dl_name)) ||
check_string_char_length(name_cstr, "", NAME_CHAR_LEN,
system_charset_info, true)) {
LogErr(ERROR_LEVEL, ER_UDF_INVALID_ROW_IN_FUNCTION_TABLE, name.str);
continue;
}
if (!(tmp = add_udf(&name, (Item_result)table->field[1]->val_int(), dl_name,
udftype))) {
LogErr(ERROR_LEVEL, ER_UDF_CANT_ALLOC_FOR_FUNCTION, name.str);
continue;
}
void *dl = find_udf_dl(tmp->dl);
if (dl == nullptr) {
char dlpath[FN_REFLEN];
strxnmov(dlpath, sizeof(dlpath) - 1, opt_plugin_dir, "/", tmp->dl, NullS);
(void)unpack_filename(dlpath, dlpath);
if (!(dl = dlopen(dlpath, RTLD_NOW))) {
const char *errmsg;
int error_number = dlopen_errno;
DLERROR_GENERATE(errmsg, error_number);
// Print warning to log
LogErr(ERROR_LEVEL, ER_FAILED_TO_OPEN_SHARED_LIBRARY, tmp->dl,
error_number, errmsg);
udf_hash_delete(tmp);
continue;
}
new_dl = true;
}
tmp->dlhandle = dl;
{
char buf[NAME_LEN + 16], *missing;
if ((missing = init_syms(tmp, buf))) {
LogErr(ERROR_LEVEL, ER_FAILED_TO_FIND_DL_ENTRY, missing);
udf_hash_delete(tmp);
if (new_dl) dlclose(dl);
}
}
}
if (error > 0) LogErr(ERROR_LEVEL, ER_UNKNOWN_ERROR_NUMBER, my_errno());
iterator.reset();
table->invalidate_dict(); // Force close to free memory
end:
if (new_thd->is_attachable_ro_transaction_active())
close_trans_system_tables(new_thd);
delete new_thd;
}
/**
Deinitialize the UDF subsystem.
This function closes the shared libraries.
*/
void udf_unload_udfs() {
DBUG_TRACE;
if (udf_hash != nullptr) {
for (auto it1 = udf_hash->begin(); it1 != udf_hash->end(); ++it1) {
udf_func *udf = it1->second;
if (udf->dlhandle) // Not closed before
{
/* Mark all versions using the same handler as closed */
for (auto it2 = std::next(it1); it2 != udf_hash->end(); ++it2) {
udf_func *tmp = it2->second;
if (udf->dlhandle == tmp->dlhandle)
tmp->dlhandle = nullptr; // Already closed
}
dlclose(udf->dlhandle);
}
}
}
}
/**
Deinitialize the UDF subsystem.
This function does the following:
1. Free the UDF hash.
2. Free the memroot allocated.
3. Destroy the RW mutex object.
*/
void udf_deinit_globals() {
DBUG_TRACE;
if (udf_hash != nullptr) {
delete udf_hash;
udf_hash = nullptr;
}
mem.Clear();
initialized = false;
mysql_rwlock_destroy(&THR_LOCK_udf);
}
/**
Delete the UDF function from the UDF hash.
@param udf Pointer to the UDF function.
@note The function remove the udf function from the udf
hash if it is not in use. If the function is in use,
the function name is renamed so that it is not used.
The function shall be removed when no threads use it.
*/
static void udf_hash_delete(udf_func *udf) {
DBUG_TRACE;
mysql_rwlock_wrlock(&THR_LOCK_udf);
const auto it = udf_hash->find(to_string(udf->name));
if (it == udf_hash->end()) {
assert(false);
return;
}
if (!--udf->usage_count) {
udf_hash->erase(it);
using_udf_functions = !udf_hash->empty();
} else {
/*
The functions is in use ; Rename the functions instead of removing it.
The functions will be automatically removed when the least threads
doesn't use it anymore
*/
udf_hash->erase(it);
char new_name[32];
snprintf(new_name, sizeof(new_name), "*<%p>", udf);
udf_hash->emplace(new_name, udf);
}
mysql_rwlock_unlock(&THR_LOCK_udf);
}
void free_udf(udf_func *udf) {
DBUG_TRACE;
if (!initialized) return;
mysql_rwlock_wrlock(&THR_LOCK_udf);
if (!--udf->usage_count) {
/*
We come here when someone has deleted the udf function
while another thread still was using the udf
*/
const auto it = udf_hash->find(to_string(udf->name));
if (it != udf_hash->end()) udf_hash->erase(it);
using_udf_functions = !udf_hash->empty();
if (udf->dlhandle && !find_udf_dl(udf->dl)) dlclose(udf->dlhandle);
}
mysql_rwlock_unlock(&THR_LOCK_udf);
}
/* This is only called if using_udf_functions != 0 */
udf_func *find_udf(const char *name, size_t length, bool mark_used) {
udf_func *udf = nullptr;
DBUG_TRACE;
if (!initialized) return nullptr;
/* TODO: This should be changed to reader locks someday! */
if (mark_used)
mysql_rwlock_wrlock(&THR_LOCK_udf); /* Called during fix_fields */
else
mysql_rwlock_rdlock(&THR_LOCK_udf); /* Called during parsing */
std::string key = length ? std::string(name, length) : std::string(name);
const auto it = udf_hash->find(key);
if (it != udf_hash->end()) {
udf = it->second;
if (mark_used) udf->usage_count++;
}
mysql_rwlock_unlock(&THR_LOCK_udf);
return udf;
}
static void *find_udf_dl(const char *dl) {
DBUG_TRACE;
if (!dl) return nullptr;
/*
Because only the function name is hashed, we have to search through
all rows to find the dl.
*/
for (const auto &key_and_value : *udf_hash) {
udf_func *udf = key_and_value.second;
if (udf->dl && !strcmp(dl, udf->dl) && udf->dlhandle != nullptr)
return udf->dlhandle;
}
return nullptr;
}
/* Assume that name && dl is already allocated */
static udf_func *add_udf(LEX_STRING *name, Item_result ret, char *dl,
Item_udftype type) {
if (!name || !dl || !(uint)type || (uint)type > (uint)UDFTYPE_AGGREGATE)
return nullptr;
udf_func *tmp = (udf_func *)mem.Alloc(sizeof(udf_func));
if (!tmp) return nullptr;
memset(tmp, 0, sizeof(*tmp));
tmp->name = *name; // dup !!
tmp->dl = dl;
tmp->returns = ret;
tmp->type = type;
tmp->usage_count = 1;
mysql_rwlock_wrlock(&THR_LOCK_udf);
udf_hash->emplace(to_string(tmp->name), tmp);
using_udf_functions = true;
mysql_rwlock_unlock(&THR_LOCK_udf);
return tmp;
}
/**
Commit or rollback a transaction. Also close tables
which it has opened and release metadata locks.
Add/Remove from the in-memory hash depending on transaction
commit or rollback and the bool flag passed to this function.
@param thd THD context.
@param rollback Rollback transaction if true.
@param udf Pointer to UDF function.
@param insert_udf Insert UDF in hash if true.
@retval False - Success.
@retval True - Error.
*/
static bool udf_end_transaction(THD *thd, bool rollback, udf_func *udf,
bool insert_udf) {
bool result;
bool rollback_transaction = thd->transaction_rollback_request || rollback;
udf_func *u_f = nullptr;
assert(stmt_causes_implicit_commit(thd, CF_IMPLICIT_COMMIT_END));
if (!rollback_transaction && insert_udf) {
udf->name.str = strdup_root(&mem, udf->name.str);
udf->dl = strdup_root(&mem, udf->dl);
// create entry in mysql.func table
u_f = add_udf(&udf->name, udf->returns, udf->dl, udf->type);
if (u_f != nullptr) {
u_f->dlhandle = udf->dlhandle;
u_f->func = udf->func;
u_f->func_init = udf->func_init;
u_f->func_deinit = udf->func_deinit;
u_f->func_clear = udf->func_clear;
u_f->func_add = udf->func_add;
}
}
rollback_transaction = rollback_transaction || (insert_udf && u_f == nullptr);
/*
CREATE/DROP UDF operations must acquire IX Backup Lock in order
to be mutually exclusive with LOCK INSTANCE FOR BACKUP.
*/
assert(thd->mdl_context.owns_equal_or_stronger_lock(
MDL_key::BACKUP_LOCK, "", "", MDL_INTENTION_EXCLUSIVE));
/*
Rollback the transaction if there is an error or there is a request by the
SE (which is unlikely).
*/
if (rollback_transaction) {
result = trans_rollback_stmt(thd);
result = result || trans_rollback_implicit(thd);
} else {
result = trans_commit_stmt(thd);
result = result || trans_commit_implicit(thd);
}
/*
Delete UDF from the hash if
* the transaction commit fails for CREATE UDF operation
* OR if the transaction is committed successfully for the DROP UDF
operation.
*/
if (!rollback_transaction &&
((insert_udf && result) || (!insert_udf && !result)))
udf_hash_delete(udf);
close_thread_tables(thd);
thd->mdl_context.release_transactional_locks();
return result || rollback || (insert_udf && u_f == nullptr);
}
/**
Create a user defined function.
Atomicity:
The operation to create a user defined function is atomic/crash-safe.
Changes to the Data-dictionary and writing event to binlog are
part of the same transaction. All the changes are done as part
of the same transaction or do not have any side effects on the
operation failure. UDF hash is in sync with operation state.
UDF hash do not contain any stale/incorrect data in case of failure.
In case of crash, there won't be any discrepancy between the
data-dictionary table and the binary log.
@param thd THD context.
@param udf Pointer to UDF function.
@param if_not_exists True if 'IF NOT EXISTS' clause was specified.
@note Like implementations of other DDL/DML in MySQL, this function
relies on the caller to close the thread tables. This is done in the
end of dispatch_command().
*/
bool mysql_create_function(THD *thd, udf_func *udf, bool if_not_exists) {
bool error = true;
void *dl = nullptr;
int new_dl = 0;
TABLE *table;
DBUG_TRACE;
if (!initialized) {
if (opt_noacl)
my_error(ER_CANT_INITIALIZE_UDF, MYF(0), udf->name.str,
"UDFs are unavailable with the --skip-grant-tables option");
else
my_error(ER_OUT_OF_RESOURCES, MYF(0));
return error;
}
/* must not be dynamically registered */
assert(udf->dl);
/*
Ensure that the .dll doesn't have a path
This is done to ensure that only approved dll from the system
directories are used (to make this even remotely secure).
*/
if (check_valid_path(udf->dl, strlen(udf->dl))) {
my_error(ER_UDF_NO_PATHS, MYF(0));
return error;
}
LEX_CSTRING udf_name_cstr = {udf->name.str, udf->name.length};
if (check_string_char_length(udf_name_cstr, "", NAME_CHAR_LEN,
system_charset_info, true)) {
my_error(ER_TOO_LONG_IDENT, MYF(0), udf->name.str);
return error;
}
/*
Acquire MDL SNRW for TL_WRITE type so that deadlock and
timeout errors are avoided from the Storage Engine.
*/
Table_ref tables("mysql", "func", TL_WRITE, MDL_SHARED_NO_READ_WRITE);
if (open_and_lock_tables(thd, &tables, MYSQL_LOCK_IGNORE_TIMEOUT))
return error;
table = tables.table;
/*
System table mysql.func is supported by only InnoDB engine. Changing system
table's engine is not allowed. But to support logical upgrade creating
system table is allowed in MyISAM engine. CREATE FUNCTION operation is
*not* allowed in this case.
*/
if ((table->file->ht->is_supported_system_table != nullptr) &&
!table->file->ht->is_supported_system_table(tables.db, tables.table_name,
true)) {
my_error(ER_UNSUPPORTED_ENGINE, MYF(0),
ha_resolve_storage_engine_name(table->file->ht), tables.db,
tables.table_name);
return error;
}
// CREATE FUNCTION operation is *not* allowed if table structure is changed.
System_table_intact table_intact(thd);
if (table_intact.check(thd, table, &mysql_udf_table_def)) return error;
/*
Turn off row binlogging of this statement and use statement-based
so that all supporting tables are updated for CREATE FUNCTION command.
*/
Save_and_Restore_binlog_format_state binlog_format_state(thd);
mysql_rwlock_rdlock(&THR_LOCK_udf);
if (udf_hash->count(to_string(udf->name)) != 0) {
mysql_rwlock_unlock(&THR_LOCK_udf);
// UDF with the same name already exists
if (if_not_exists) {
push_warning_printf(thd, Sql_condition::SL_NOTE, ER_UDF_EXISTS,
ER_THD(thd, ER_UDF_EXISTS), udf->name.str);
error = (write_bin_log(thd, true, thd->query().str, thd->query().length,
false) != 0);
if (error) error = udf_end_transaction(thd, error, nullptr, false);
} else {
my_error(ER_UDF_EXISTS, MYF(0), udf->name.str);
}
return error;
}
dl = find_udf_dl(udf->dl);
mysql_rwlock_unlock(&THR_LOCK_udf);
if (dl == nullptr) {
char dlpath[FN_REFLEN];
strxnmov(dlpath, sizeof(dlpath) - 1, opt_plugin_dir, "/", udf->dl, NullS);
(void)unpack_filename(dlpath, dlpath);
if (!(dl = dlopen(dlpath, RTLD_NOW))) {
const char *errmsg;
int error_number = dlopen_errno;
DLERROR_GENERATE(errmsg, error_number);
DBUG_PRINT("error", ("dlopen of %s failed, error: %d (%s)", udf->dl,
error_number, errmsg));
my_error(ER_CANT_OPEN_LIBRARY, MYF(0), udf->dl, error_number, errmsg);
return error;
}
new_dl = 1;
}
udf->dlhandle = dl;
{
char buf[NAME_LEN + 16], *missing;
if ((missing = init_syms(udf, buf))) {
my_error(ER_CANT_FIND_DL_ENTRY, MYF(0), missing);
if (new_dl) dlclose(dl);
return error;
}
}
// create entry in mysql.func table
table->use_all_columns();
restore_record(table, s->default_values); // Default values for fields
table->field[0]->store(udf->name.str, udf->name.length, system_charset_info);
table->field[1]->store((longlong)udf->returns, true);
table->field[2]->store(udf->dl, strlen(udf->dl), system_charset_info);
if (table->s->fields >= 4) // If not old func format
table->field[3]->store((longlong)udf->type, true);
error = (table->file->ha_write_row(table->record[0]) != 0);
// Binlog the create function.
if (!error)
error = (write_bin_log(thd, true, thd->query().str, thd->query().length,
true) != 0);
error = udf_end_transaction(thd, error, udf, true);
if (error) {
char errbuf[MYSYS_STRERROR_SIZE];
my_error(ER_ERROR_ON_WRITE, MYF(0), "mysql.func", error,
my_strerror(errbuf, sizeof(errbuf), error));
if (new_dl) dlclose(dl);
}
return error;
}
/**
Drop a user defined function.
Atomicity:
The operation to drop a user defined function is atomic/crash-safe.
Changes to the Data-dictionary and writing event to binlog are
part of the same transaction. All the changes are done as part
of the same transaction or do not have any side effects on the
operation failure. UDF hash is in sync with operation state.
UDF hash do not contain any stale/incorrect data in case of failure.
In case of crash, there won't be any discrepancy between the
data-dictionary table and the binary log.
@param thd THD context.
@param udf_name Name of the UDF function.
*/
bool mysql_drop_function(THD *thd, const LEX_STRING *udf_name) {
TABLE *table;
udf_func *udf;
bool error = true;
DBUG_TRACE;
if (!initialized) {
if (opt_noacl)
my_error(ER_FUNCTION_NOT_DEFINED, MYF(0), udf_name->str);
else
my_error(ER_OUT_OF_RESOURCES, MYF(0));
return error;
}
Table_ref tables("mysql", "func", TL_WRITE, MDL_SHARED_NO_READ_WRITE);
if (open_and_lock_tables(thd, &tables, MYSQL_LOCK_IGNORE_TIMEOUT))
return error;
table = tables.table;
/*
System table mysql.func is supported by only InnoDB engine. Changing system
table's engine is not allowed. But to support logical upgrade creating
system table is allowed in MyISAM engine. DROP and CREATE FUNCTION
operations are not allowed in this case.
*/
if (!table->file->ht->is_supported_system_table(tables.db, tables.table_name,
true)) {
my_error(ER_UNSUPPORTED_ENGINE, MYF(0),
ha_resolve_storage_engine_name(table->file->ht), tables.db,
tables.table_name);
return error;
}
// DROP FUNCTION operation is *not* allowed if table structure is changed.
System_table_intact table_intact(thd);
if (table_intact.check(thd, table, &mysql_udf_table_def)) return error;
/*
Turn off row binlogging of this statement and use statement-based
so that all supporting tables are updated for DROP FUNCTION command.
*/
Save_and_Restore_binlog_format_state binlog_format_state(thd);
mysql_rwlock_rdlock(&THR_LOCK_udf);
const auto it = udf_hash->find(to_string(*udf_name));
if (it == udf_hash->end()) {
my_error(ER_FUNCTION_NOT_DEFINED, MYF(0), udf_name->str);
mysql_rwlock_unlock(&THR_LOCK_udf);
return error;
}
udf = it->second;
if (!udf->dl) {
mysql_rwlock_unlock(&THR_LOCK_udf);
my_error(ER_UDF_DROP_DYNAMICALLY_REGISTERED, MYF(0));
return error;
}
mysql_rwlock_unlock(&THR_LOCK_udf);
table->use_all_columns();
table->field[0]->store(udf->name.str, udf->name.length, &my_charset_bin);
if (!table->file->ha_index_read_idx_map(table->record[0], 0,
table->field[0]->field_ptr(),
HA_WHOLE_KEY, HA_READ_KEY_EXACT)) {
int delete_err;
if ((delete_err = table->file->ha_delete_row(table->record[0])))
table->file->print_error(delete_err, MYF(0));
error = delete_err != 0;
}
/*
Binlog the drop function. Keep the table open and locked
while binlogging, to avoid binlog inconsistency.
*/
if (!error)
error = (write_bin_log(thd, true, thd->query().str, thd->query().length,
true) != 0);
error = udf_end_transaction(thd, error, udf, false);
/*
Close the handle if this was function that was found during boot or
CREATE FUNCTION and it's not in use by any other udf function
*/
if (udf->dlhandle && !find_udf_dl(udf->dl)) dlclose(udf->dlhandle);
return error;
}
bool mysql_udf_registration_imp::udf_register_inner(udf_func *ufunc) {
mysql_rwlock_wrlock(&THR_LOCK_udf);
assert(ufunc->dl == nullptr);
assert(ufunc->dlhandle == nullptr);
auto res = udf_hash->emplace(to_string(ufunc->name), ufunc);
if (!res.second)
ufunc = nullptr;
else
using_udf_functions = true;
mysql_rwlock_unlock(&THR_LOCK_udf);
return ufunc == nullptr;
}
udf_func *mysql_udf_registration_imp::alloc_udf(const char *name,
Item_result return_type,
Udf_func_any func,
Udf_func_init init_func,
Udf_func_deinit deinit_func) {
udf_func *ufunc;
ufunc = (udf_func *)mem.Alloc(sizeof(udf_func));
if (!ufunc) return nullptr;
memset(ufunc, 0, sizeof(udf_func));
ufunc->name.str = strdup_root(&mem, name);
ufunc->name.length = strlen(name);
ufunc->func = func;
ufunc->func_init = init_func;
ufunc->func_deinit = deinit_func;
ufunc->returns = return_type;
ufunc->usage_count = 1;
return ufunc;
}
DEFINE_BOOL_METHOD(mysql_udf_registration_imp::udf_register,
(const char *name, Item_result return_type,
Udf_func_any func, Udf_func_init init_func,
Udf_func_deinit deinit_func)) {
udf_func *ufunc;
if (!func && !init_func && !deinit_func) return true;
ufunc = alloc_udf(name, return_type, func, init_func, deinit_func);
if (!ufunc) return true;
ufunc->type = Item_udftype::UDFTYPE_FUNCTION;
return udf_register_inner(ufunc);
}
DEFINE_BOOL_METHOD(mysql_udf_registration_imp::udf_register_aggregate,
(const char *name, enum Item_result return_type,
Udf_func_any func, Udf_func_init init_func,
Udf_func_deinit deinit_func, Udf_func_add add_func,
Udf_func_clear clear_func)) {
udf_func *ufunc;
if (!func && !add_func && !clear_func && !init_func && !deinit_func)
return true;
ufunc = alloc_udf(name, return_type, func, init_func, deinit_func);
if (!ufunc) return true;
ufunc->type = Item_udftype::UDFTYPE_AGGREGATE;
ufunc->func_add = add_func;
ufunc->func_clear = clear_func;
return udf_register_inner(ufunc);
}
DEFINE_BOOL_METHOD(mysql_udf_registration_imp::udf_unregister,
(const char *name, int *was_present)) {
udf_func *udf = nullptr;
if (was_present) *was_present = 0;
mysql_rwlock_wrlock(&THR_LOCK_udf);
const auto it = udf_hash->find(name);
if (it != udf_hash->end()) {
if (was_present) *was_present = 1;
udf = it->second;
if (!udf->dl && !udf->dlhandle && // Not registered via CREATE FUNCTION
(udf->usage_count == 1)) // Not used
{
--udf->usage_count;
udf_hash->erase(it);
using_udf_functions = !udf_hash->empty();
} else // error
udf = nullptr;
}
mysql_rwlock_unlock(&THR_LOCK_udf);
return udf != nullptr ? false : true;
}
void udf_hash_rlock(void) { mysql_rwlock_rdlock(&THR_LOCK_udf); }
void udf_hash_unlock(void) { mysql_rwlock_unlock(&THR_LOCK_udf); }
ulong udf_hash_size(void) { return udf_hash->size(); }
void udf_hash_for_each(udf_hash_for_each_func_t *func, void *arg) {
for (auto it : *udf_hash) func(it.second, arg);
}
|