@@ -507,36 +507,57 @@ wasi_nn_load(wasm_exec_env_t exec_env, graph_builder_array_wasm *builder,
507
507
return res ;
508
508
}
509
509
510
+ static wasi_nn_error
511
+ copyin_and_nul_terminate (wasm_module_inst_t inst , char * name , uint32_t name_len ,
512
+ char * * resultp )
513
+ {
514
+ char * nul_terminated_name ;
515
+ if (!wasm_runtime_validate_native_addr (inst , name , name_len )) {
516
+ return invalid_argument ;
517
+ }
518
+ nul_terminated_name = wasm_runtime_malloc (name_len + 1 );
519
+ if (nul_terminated_name == NULL ) {
520
+ return runtime_error ;
521
+ }
522
+ bh_memcpy_s (nul_terminated_name , name_len + 1 , name , name_len );
523
+ nul_terminated_name [name_len ] = '\0' ; /* ensure NUL termination */
524
+ if (strlen (nul_terminated_name ) != name_len ) {
525
+ /* reject names containing '\0' for now */
526
+ wasm_runtime_free (nul_terminated_name );
527
+ return invalid_argument ;
528
+ }
529
+ * resultp = nul_terminated_name ;
530
+ return success ;
531
+ }
532
+
510
533
wasi_nn_error
511
534
wasi_nn_load_by_name (wasm_exec_env_t exec_env , char * name , uint32_t name_len ,
512
535
graph * g )
513
536
{
537
+ WASINNContext * wasi_nn_ctx = NULL ;
538
+ char * nul_terminated_name = NULL ;
514
539
wasi_nn_error res ;
515
540
516
541
wasm_module_inst_t instance = wasm_runtime_get_module_inst (exec_env );
517
542
if (!instance ) {
518
543
return runtime_error ;
519
544
}
520
545
521
- if (!wasm_runtime_validate_native_addr (instance , name , name_len )) {
522
- NN_ERR_PRINTF ("name is invalid" );
523
- return invalid_argument ;
524
- }
525
-
526
546
if (!wasm_runtime_validate_native_addr (instance , g ,
527
547
(uint64 )sizeof (graph ))) {
528
548
NN_ERR_PRINTF ("graph is invalid" );
529
549
return invalid_argument ;
530
550
}
531
551
532
- if (name_len == 0 || name [name_len ] != '\0' ) {
533
- NN_ERR_PRINTF ("Invalid filename" );
534
- return invalid_argument ;
552
+ res = copyin_and_nul_terminate (instance , name , name_len ,
553
+ & nul_terminated_name );
554
+ if (res != success ) {
555
+ goto fail ;
535
556
}
536
557
537
- NN_DBG_PRINTF ("[WASI NN] LOAD_BY_NAME %s..." , name );
558
+ NN_DBG_PRINTF ("[WASI NN] LOAD_BY_NAME %s..." , nul_terminated_name );
538
559
539
- WASINNContext * wasi_nn_ctx = lock_ctx (instance );
560
+ wasi_nn_ctx = lock_ctx (instance );
540
561
if (wasi_nn_ctx == NULL ) {
541
562
res = busy ;
542
563
goto fail ;
@@ -547,14 +568,20 @@ wasi_nn_load_by_name(wasm_exec_env_t exec_env, char *name, uint32_t name_len,
547
568
goto fail ;
548
569
549
570
call_wasi_nn_func (wasi_nn_ctx -> backend , load_by_name , res ,
550
- wasi_nn_ctx -> backend_ctx , name , name_len , g );
571
+ wasi_nn_ctx -> backend_ctx , nul_terminated_name , name_len ,
572
+ g );
551
573
if (res != success )
552
574
goto fail ;
553
575
554
576
wasi_nn_ctx -> is_model_loaded = true;
555
577
res = success ;
556
578
fail :
557
- unlock_ctx (wasi_nn_ctx );
579
+ if (nul_terminated_name != NULL ) {
580
+ wasm_runtime_free (nul_terminated_name );
581
+ }
582
+ if (wasi_nn_ctx != NULL ) {
583
+ unlock_ctx (wasi_nn_ctx );
584
+ }
558
585
return res ;
559
586
}
560
587
@@ -563,37 +590,37 @@ wasi_nn_load_by_name_with_config(wasm_exec_env_t exec_env, char *name,
563
590
int32_t name_len , char * config ,
564
591
int32_t config_len , graph * g )
565
592
{
593
+ WASINNContext * wasi_nn_ctx = NULL ;
594
+ char * nul_terminated_name = NULL ;
595
+ char * nul_terminated_config = NULL ;
566
596
wasi_nn_error res ;
567
597
568
598
wasm_module_inst_t instance = wasm_runtime_get_module_inst (exec_env );
569
599
if (!instance ) {
570
600
return runtime_error ;
571
601
}
572
602
573
- if (!wasm_runtime_validate_native_addr (instance , name , name_len )) {
574
- NN_ERR_PRINTF ("name is invalid" );
575
- return invalid_argument ;
576
- }
577
-
578
603
if (!wasm_runtime_validate_native_addr (instance , g ,
579
604
(uint64 )sizeof (graph ))) {
580
605
NN_ERR_PRINTF ("graph is invalid" );
581
606
return invalid_argument ;
582
607
}
583
608
584
- if (name_len == 0 || name [name_len ] != '\0' ) {
585
- NN_ERR_PRINTF ("Invalid filename" );
586
- return invalid_argument ;
609
+ res = copyin_and_nul_terminate (instance , name , name_len ,
610
+ & nul_terminated_name );
611
+ if (res != success ) {
612
+ goto fail ;
587
613
}
588
-
589
- if (! config || config_len == 0 || config [ config_len ] != '\0' ) {
590
- NN_ERR_PRINTF ( "Invalid config" );
591
- return invalid_argument ;
614
+ res = copyin_and_nul_terminate ( instance , config , config_len ,
615
+ & nul_terminated_config );
616
+ if ( res != success ) {
617
+ goto fail ;
592
618
}
593
619
594
- NN_DBG_PRINTF ("[WASI NN] LOAD_BY_NAME_WITH_CONFIG %s %s..." , name , config );
620
+ NN_DBG_PRINTF ("[WASI NN] LOAD_BY_NAME_WITH_CONFIG %s %s..." ,
621
+ nul_terminated_name , nul_terminated_config );
595
622
596
- WASINNContext * wasi_nn_ctx = lock_ctx (instance );
623
+ wasi_nn_ctx = lock_ctx (instance );
597
624
if (wasi_nn_ctx == NULL ) {
598
625
res = busy ;
599
626
goto fail ;
@@ -605,15 +632,23 @@ wasi_nn_load_by_name_with_config(wasm_exec_env_t exec_env, char *name,
605
632
;
606
633
607
634
call_wasi_nn_func (wasi_nn_ctx -> backend , load_by_name_with_config , res ,
608
- wasi_nn_ctx -> backend_ctx , name , name_len , config ,
609
- config_len , g );
635
+ wasi_nn_ctx -> backend_ctx , nul_terminated_name , name_len ,
636
+ nul_terminated_config , config_len , g );
610
637
if (res != success )
611
638
goto fail ;
612
639
613
640
wasi_nn_ctx -> is_model_loaded = true;
614
641
res = success ;
615
642
fail :
616
- unlock_ctx (wasi_nn_ctx );
643
+ if (nul_terminated_name != NULL ) {
644
+ wasm_runtime_free (nul_terminated_name );
645
+ }
646
+ if (nul_terminated_config != NULL ) {
647
+ wasm_runtime_free (nul_terminated_config );
648
+ }
649
+ if (wasi_nn_ctx != NULL ) {
650
+ unlock_ctx (wasi_nn_ctx );
651
+ }
617
652
return res ;
618
653
}
619
654
0 commit comments