Skip to content

Fix fail label logic with bs_create_bin #1731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release-0.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ project(AtomVM)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)

find_package(Dialyzer)
find_package(Erlang)
find_package(Elixir)

option(AVM_DISABLE_FP "Disable floating point support." OFF)
Expand Down
32 changes: 32 additions & 0 deletions CMakeModules/FindErlang.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# This file is part of AtomVM.
#
# Copyright 2025 Paul Guyot <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
#

find_program(ERLC_PATH erlc)
find_program(ERL_PATH erl)

if (ERLC_PATH AND ERL_PATH)
set(Erlang_FOUND TRUE)

execute_process(COMMAND ${ERL_PATH} -eval "io:put_chars(erlang:system_info(otp_release))" -s init stop -noshell
OUTPUT_VARIABLE Erlang_VERSION)
message("Found Erlang OTP ${Erlang_VERSION}")
elseif(Erlang_FIND_REQUIRED)
message(FATAL_ERROR "Erlang or Erlang compiler not found")
endif()
257 changes: 155 additions & 102 deletions src/libAtomVM/opcodesswitch.h

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions tests/erlang_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ function(compile_erlang module_name)
)
endfunction()

function(compile_assembler module_name)
add_custom_command(
OUTPUT ${module_name}.beam
COMMAND erlc ${CMAKE_CURRENT_SOURCE_DIR}/${module_name}.S
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${module_name}.S
COMMENT "Compiling ${module_name}.S"
)
endfunction()

set(TO_HRL_PATH ${CMAKE_CURRENT_LIST_DIR})

function(generate_hrl out_file def_name in_file)
Expand Down Expand Up @@ -518,6 +527,17 @@ compile_erlang(maps_nifs)

compile_erlang(test_raw_raise)

compile_erlang(test_op_bs_create_bin)
compile_assembler(test_op_bs_create_bin_asm)

if(Erlang_VERSION VERSION_GREATER_EQUAL "25")
set(OTP25_OR_GREATER_TESTS
test_op_bs_create_bin_asm.beam
)
else()
set(OTP25_OR_GREATER_TESTS)
endif()

add_custom_target(erlang_test_modules DEPENDS
code_load_files

Expand Down Expand Up @@ -1000,4 +1020,8 @@ add_custom_target(erlang_test_modules DEPENDS
maps_nifs.beam

test_raw_raise.beam

test_op_bs_create_bin.beam

${OTP25_OR_GREATER_TESTS}
)
268 changes: 268 additions & 0 deletions tests/erlang_tests/test_op_bs_create_bin.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
%
% This file is part of AtomVM.
%
% Copyright 2025 Paul Guyot <[email protected]>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
%

-module(test_op_bs_create_bin).

-export([start/0]).

start() ->
HasBSCreateBin =
case erlang:system_info(machine) of
"BEAM" ->
erlang:system_info(otp_release) >= "25";
"ATOM" ->
% If code was compiled with OTP < 25, we won't have bs_create_bin asm file
?OTP_RELEASE >= 25
end,
ok =
if
HasBSCreateBin ->
ok = test_bs_create_bin_utf8(),
ok = test_bs_create_bin_utf16(),
ok = test_bs_create_bin_utf32(),
ok = test_bs_create_bin_integer(),
ok = test_bs_create_bin_binary();
true ->
ok
end,
0.

test_bs_create_bin_utf8() ->
<<0>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_no_fail(0, [], undefined),
<<0>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_fail(0, [], undefined),
<<1>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_no_fail(1, [], undefined),
<<1>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_fail(1, [], undefined),
<<227, 130, 162>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_no_fail(12450, [], undefined),
<<227, 130, 162>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_fail(12450, [], undefined),
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_utf8_no_fail(-1, [], undefined),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_utf8_fail(-1, [], undefined),
ok
catch
error:badarg -> unexpected
end,
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_utf8_no_fail(not_int, [], undefined),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_utf8_fail(not_int, [], undefined),
ok
catch
error:badarg -> unexpected
end,
<<0>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_no_fail(0, [], all),
<<0>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_no_fail(0, [], 8),
<<0>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_no_fail(0, [], 16),
<<0>> = test_op_bs_create_bin_asm:bs_create_bin_utf8_no_fail(0, [], foo),
ok.

test_bs_create_bin_utf16() ->
<<0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_no_fail(0, [], undefined),
<<0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_fail(0, [], undefined),
<<0, 1>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_no_fail(1, [], undefined),
<<0, 1>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_fail(1, [], undefined),
<<48, 162>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_no_fail(12450, [], undefined),
<<48, 162>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_fail(12450, [], undefined),
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_utf16_no_fail(-1, [], undefined),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_utf16_fail(-1, [], undefined),
ok
catch
error:badarg -> unexpected
end,
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_utf16_no_fail(not_int, [], undefined),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_utf16_fail(not_int, [], undefined),
ok
catch
error:badarg -> unexpected
end,
<<0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_no_fail(0, [], all),
<<0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_no_fail(0, [], 8),
<<0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_no_fail(0, [], 16),
<<0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf16_no_fail(0, [], foo),
ok.

test_bs_create_bin_utf32() ->
<<0, 0, 0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_no_fail(0, [], undefined),
<<0, 0, 0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_fail(0, [], undefined),
<<0, 0, 0, 1>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_no_fail(1, [], undefined),
<<0, 0, 0, 1>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_fail(1, [], undefined),
<<0, 0, 48, 162>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_no_fail(12450, [], undefined),
<<0, 0, 48, 162>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_fail(12450, [], undefined),
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_utf32_no_fail(-1, [], undefined),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_utf32_fail(-1, [], undefined),
ok
catch
error:badarg -> unexpected
end,
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_utf32_no_fail(not_int, [], undefined),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_utf32_fail(not_int, [], undefined),
ok
catch
error:badarg -> unexpected
end,
<<0, 0, 0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_no_fail(0, [], all),
<<0, 0, 0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_no_fail(0, [], 8),
<<0, 0, 0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_no_fail(0, [], 16),
<<0, 0, 0, 0>> = test_op_bs_create_bin_asm:bs_create_bin_utf32_no_fail(0, [], foo),
ok.

test_bs_create_bin_integer() ->
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_integer_no_fail(0, [], undefined),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_integer_fail(0, [], undefined),
ok
catch
error:badarg -> unexpected
end,
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_integer_no_fail(0, [], all),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_integer_fail(0, [], all),
ok
catch
error:badarg -> unexpected
end,
<<42>> = test_op_bs_create_bin_asm:bs_create_bin_integer_no_fail(42, [], 1),
<<42>> = test_op_bs_create_bin_asm:bs_create_bin_integer_fail(42, [], 1),
<<0, 42>> = test_op_bs_create_bin_asm:bs_create_bin_integer_no_fail(42, [], 2),
<<0, 42>> = test_op_bs_create_bin_asm:bs_create_bin_integer_fail(42, [], 2),
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_integer_no_fail(0, [], foo),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_integer_fail(0, [], foo),
ok
catch
error:badarg -> unexpected
end,
ok.

test_bs_create_bin_binary() ->
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_binary_no_fail(<<0>>, [], undefined),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_binary_fail(<<0>>, [], undefined),
ok
catch
error:badarg -> unexpected
end,
% AtomVM allows all for binaries while OTP28 seems to disallow it.
% It may only accept them for bs match state (?)
% ok = try test_op_bs_create_bin_asm:bs_create_bin_binary_no_fail(<<0>>, [], all), unexpected catch error:badarg -> ok end,
% ok = try fail = test_op_bs_create_bin_asm:bs_create_bin_binary_fail(<<0>>, [], all), ok catch error:badarg -> unexpected end,
<<42>> = test_op_bs_create_bin_asm:bs_create_bin_binary_no_fail(<<42>>, [], 1),
<<42>> = test_op_bs_create_bin_asm:bs_create_bin_binary_fail(<<42>>, [], 1),
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_binary_no_fail(<<42>>, [], 2),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_binary_fail(<<42>>, [], 2),
ok
catch
error:badarg -> unexpected
end,
ok =
try
test_op_bs_create_bin_asm:bs_create_bin_binary_no_fail(<<0>>, [], foo),
unexpected
catch
error:badarg -> ok
end,
ok =
try
fail = test_op_bs_create_bin_asm:bs_create_bin_binary_fail(<<0>>, [], foo),
ok
catch
error:badarg -> unexpected
end,
ok.
Loading
Loading