Skip to content

Commit

Permalink
Add #[unsafe()] attribute markers and use &raw instead of addr_of
Browse files Browse the repository at this point in the history
Both stabilized in Rust 1.82.0, released today

This only affects the WASM demo
  • Loading branch information
ictrobot committed Oct 17, 2024
1 parent e72ddcb commit 9bbc1aa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions crates/aoc_wasm/src/custom_sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ macro_rules! matcher {
mod $day {
use aoc::utils::PuzzleExamples;

#[link_section = "aoc_puzzles"]
#[unsafe(link_section = "aoc_puzzles")]
#[used]
static PUZZLE_LIST: [u8; 6] = [
b'0' + ($y / 1000u16) as u8,
Expand All @@ -60,7 +60,7 @@ macro_rules! matcher {
b'0' + $d % 10,
];

#[link_section = concat!("aoc_examples_", stringify!($y), "_", stringify!($d))]
#[unsafe(link_section = concat!("aoc_examples_", stringify!($y), "_", stringify!($d)))]
#[used]
static PUZZLE_EXAMPLES: [u8; super::super::examples_len(aoc::$year::$day::EXAMPLES)]
= super::super::examples_section(aoc::$year::$day::EXAMPLES);
Expand Down
14 changes: 6 additions & 8 deletions crates/aoc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Simple WebAssembly interface without external libraries.
mod custom_sections;
#[cfg(feature = "multithreading")]
mod multithreading;
Expand All @@ -8,18 +7,17 @@ use aoc::all_puzzles;
use aoc::utils::input::InputType;
use std::error::Error;
use std::ffi::CStr;
use std::ptr::addr_of_mut;

const BUFFER_LENGTH: usize = 1024 * 1024;

#[no_mangle]
#[unsafe(no_mangle)]
static INPUT: [u8; BUFFER_LENGTH] = [0u8; BUFFER_LENGTH];
#[no_mangle]
#[unsafe(no_mangle)]
static mut PART1: [u8; BUFFER_LENGTH] = [0u8; BUFFER_LENGTH];
#[no_mangle]
#[unsafe(no_mangle)]
static mut PART2: [u8; BUFFER_LENGTH] = [0u8; BUFFER_LENGTH];

#[no_mangle]
#[unsafe(no_mangle)]
extern "C" fn run_puzzle(
year: u16,
day: u8,
Expand All @@ -35,8 +33,8 @@ extern "C" fn run_puzzle(
// SAFETY: No other Rust code accesses these variables or creates references - they're only read
// from JS.
unsafe {
write_string(addr_of_mut!(PART1).cast(), &part1);
write_string(addr_of_mut!(PART2).cast(), &part2);
write_string((&raw mut PART1).cast(), &part1);
write_string((&raw mut PART2).cast(), &part2);
}

success
Expand Down
4 changes: 2 additions & 2 deletions crates/aoc_wasm/src/multithreading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::alloc::{alloc_zeroed, Layout};
/// Allocate stack for worker threads.
///
/// **WARNING**: Stack overflows on worker threads will corrupt other parts of the linear memory.
#[no_mangle]
#[unsafe(no_mangle)]
extern "C" fn allocate_stack(size: usize, align: usize) -> *mut u8 {
let layout = Layout::from_size_align(size, align).unwrap();
unsafe { alloc_zeroed(layout) }
}

/// Run worker thread.
#[no_mangle]
#[unsafe(no_mangle)]
extern "C" fn worker_thread() {
worker();
}

0 comments on commit 9bbc1aa

Please sign in to comment.